powerpc: Wire up sys_rt_tgsigqueueinfo
[zen-stable.git] / drivers / char / amiserial.c
blob72429b6b2fa831e8ce925145398acfbb7aa66170
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/delay.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
46 /* Sanity checks */
48 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
49 #define DBG_CNT(s) printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
50 tty->name, (info->flags), serial_driver->refcount,info->count,tty->count,s)
51 #else
52 #define DBG_CNT(s)
53 #endif
56 * End of serial driver configuration section.
59 #include <linux/module.h>
61 #include <linux/types.h>
62 #include <linux/serial.h>
63 #include <linux/serialP.h>
64 #include <linux/serial_reg.h>
65 static char *serial_version = "4.30";
67 #include <linux/errno.h>
68 #include <linux/signal.h>
69 #include <linux/sched.h>
70 #include <linux/kernel.h>
71 #include <linux/timer.h>
72 #include <linux/interrupt.h>
73 #include <linux/tty.h>
74 #include <linux/tty_flip.h>
75 #include <linux/console.h>
76 #include <linux/major.h>
77 #include <linux/string.h>
78 #include <linux/fcntl.h>
79 #include <linux/ptrace.h>
80 #include <linux/ioport.h>
81 #include <linux/mm.h>
82 #include <linux/seq_file.h>
83 #include <linux/slab.h>
84 #include <linux/init.h>
85 #include <linux/bitops.h>
87 #include <asm/setup.h>
89 #include <asm/system.h>
91 #include <asm/irq.h>
93 #include <asm/amigahw.h>
94 #include <asm/amigaints.h>
96 #define custom amiga_custom
97 static char *serial_name = "Amiga-builtin serial driver";
99 static struct tty_driver *serial_driver;
101 /* number of characters left in xmit buffer before we ask for more */
102 #define WAKEUP_CHARS 256
104 static struct async_struct *IRQ_ports;
106 static unsigned char current_ctl_bits;
108 static void change_speed(struct async_struct *info, struct ktermios *old);
109 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
112 static struct serial_state rs_table[1];
114 #define NR_PORTS ARRAY_SIZE(rs_table)
116 #include <asm/uaccess.h>
118 #define serial_isroot() (capable(CAP_SYS_ADMIN))
121 static inline int serial_paranoia_check(struct async_struct *info,
122 char *name, const char *routine)
124 #ifdef SERIAL_PARANOIA_CHECK
125 static const char *badmagic =
126 "Warning: bad magic number for serial struct (%s) in %s\n";
127 static const char *badinfo =
128 "Warning: null async_struct for (%s) in %s\n";
130 if (!info) {
131 printk(badinfo, name, routine);
132 return 1;
134 if (info->magic != SERIAL_MAGIC) {
135 printk(badmagic, name, routine);
136 return 1;
138 #endif
139 return 0;
142 /* some serial hardware definitions */
143 #define SDR_OVRUN (1<<15)
144 #define SDR_RBF (1<<14)
145 #define SDR_TBE (1<<13)
146 #define SDR_TSRE (1<<12)
148 #define SERPER_PARENB (1<<15)
150 #define AC_SETCLR (1<<15)
151 #define AC_UARTBRK (1<<11)
153 #define SER_DTR (1<<7)
154 #define SER_RTS (1<<6)
155 #define SER_DCD (1<<5)
156 #define SER_CTS (1<<4)
157 #define SER_DSR (1<<3)
159 static __inline__ void rtsdtr_ctrl(int bits)
161 ciab.pra = ((bits & (SER_RTS | SER_DTR)) ^ (SER_RTS | SER_DTR)) | (ciab.pra & ~(SER_RTS | SER_DTR));
165 * ------------------------------------------------------------
166 * rs_stop() and rs_start()
168 * This routines are called before setting or resetting tty->stopped.
169 * They enable or disable transmitter interrupts, as necessary.
170 * ------------------------------------------------------------
172 static void rs_stop(struct tty_struct *tty)
174 struct async_struct *info = tty->driver_data;
175 unsigned long flags;
177 if (serial_paranoia_check(info, tty->name, "rs_stop"))
178 return;
180 local_irq_save(flags);
181 if (info->IER & UART_IER_THRI) {
182 info->IER &= ~UART_IER_THRI;
183 /* disable Tx interrupt and remove any pending interrupts */
184 custom.intena = IF_TBE;
185 mb();
186 custom.intreq = IF_TBE;
187 mb();
189 local_irq_restore(flags);
192 static void rs_start(struct tty_struct *tty)
194 struct async_struct *info = tty->driver_data;
195 unsigned long flags;
197 if (serial_paranoia_check(info, tty->name, "rs_start"))
198 return;
200 local_irq_save(flags);
201 if (info->xmit.head != info->xmit.tail
202 && info->xmit.buf
203 && !(info->IER & UART_IER_THRI)) {
204 info->IER |= UART_IER_THRI;
205 custom.intena = IF_SETCLR | IF_TBE;
206 mb();
207 /* set a pending Tx Interrupt, transmitter should restart now */
208 custom.intreq = IF_SETCLR | IF_TBE;
209 mb();
211 local_irq_restore(flags);
215 * ----------------------------------------------------------------------
217 * Here starts the interrupt handling routines. All of the following
218 * subroutines are declared as inline and are folded into
219 * rs_interrupt(). They were separated out for readability's sake.
221 * Note: rs_interrupt() is a "fast" interrupt, which means that it
222 * runs with interrupts turned off. People who may want to modify
223 * rs_interrupt() should try to keep the interrupt handler as fast as
224 * possible. After you are done making modifications, it is not a bad
225 * idea to do:
227 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
229 * and look at the resulting assemble code in serial.s.
231 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
232 * -----------------------------------------------------------------------
236 * This routine is used by the interrupt handler to schedule
237 * processing in the software interrupt portion of the driver.
239 static void rs_sched_event(struct async_struct *info,
240 int event)
242 info->event |= 1 << event;
243 tasklet_schedule(&info->tlet);
246 static void receive_chars(struct async_struct *info)
248 int status;
249 int serdatr;
250 struct tty_struct *tty = info->tty;
251 unsigned char ch, flag;
252 struct async_icount *icount;
253 int oe = 0;
255 icount = &info->state->icount;
257 status = UART_LSR_DR; /* We obviously have a character! */
258 serdatr = custom.serdatr;
259 mb();
260 custom.intreq = IF_RBF;
261 mb();
263 if((serdatr & 0x1ff) == 0)
264 status |= UART_LSR_BI;
265 if(serdatr & SDR_OVRUN)
266 status |= UART_LSR_OE;
268 ch = serdatr & 0xff;
269 icount->rx++;
271 #ifdef SERIAL_DEBUG_INTR
272 printk("DR%02x:%02x...", ch, status);
273 #endif
274 flag = TTY_NORMAL;
277 * We don't handle parity or frame errors - but I have left
278 * the code in, since I'm not sure that the errors can't be
279 * detected.
282 if (status & (UART_LSR_BI | UART_LSR_PE |
283 UART_LSR_FE | UART_LSR_OE)) {
285 * For statistics only
287 if (status & UART_LSR_BI) {
288 status &= ~(UART_LSR_FE | UART_LSR_PE);
289 icount->brk++;
290 } else if (status & UART_LSR_PE)
291 icount->parity++;
292 else if (status & UART_LSR_FE)
293 icount->frame++;
294 if (status & UART_LSR_OE)
295 icount->overrun++;
298 * Now check to see if character should be
299 * ignored, and mask off conditions which
300 * should be ignored.
302 if (status & info->ignore_status_mask)
303 goto out;
305 status &= info->read_status_mask;
307 if (status & (UART_LSR_BI)) {
308 #ifdef SERIAL_DEBUG_INTR
309 printk("handling break....");
310 #endif
311 flag = TTY_BREAK;
312 if (info->flags & ASYNC_SAK)
313 do_SAK(tty);
314 } else if (status & UART_LSR_PE)
315 flag = TTY_PARITY;
316 else if (status & UART_LSR_FE)
317 flag = TTY_FRAME;
318 if (status & UART_LSR_OE) {
320 * Overrun is special, since it's
321 * reported immediately, and doesn't
322 * affect the current character
324 oe = 1;
327 tty_insert_flip_char(tty, ch, flag);
328 if (oe == 1)
329 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
330 tty_flip_buffer_push(tty);
331 out:
332 return;
335 static void transmit_chars(struct async_struct *info)
337 custom.intreq = IF_TBE;
338 mb();
339 if (info->x_char) {
340 custom.serdat = info->x_char | 0x100;
341 mb();
342 info->state->icount.tx++;
343 info->x_char = 0;
344 return;
346 if (info->xmit.head == info->xmit.tail
347 || info->tty->stopped
348 || info->tty->hw_stopped) {
349 info->IER &= ~UART_IER_THRI;
350 custom.intena = IF_TBE;
351 mb();
352 return;
355 custom.serdat = info->xmit.buf[info->xmit.tail++] | 0x100;
356 mb();
357 info->xmit.tail = info->xmit.tail & (SERIAL_XMIT_SIZE-1);
358 info->state->icount.tx++;
360 if (CIRC_CNT(info->xmit.head,
361 info->xmit.tail,
362 SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
363 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
365 #ifdef SERIAL_DEBUG_INTR
366 printk("THRE...");
367 #endif
368 if (info->xmit.head == info->xmit.tail) {
369 custom.intena = IF_TBE;
370 mb();
371 info->IER &= ~UART_IER_THRI;
375 static void check_modem_status(struct async_struct *info)
377 unsigned char status = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
378 unsigned char dstatus;
379 struct async_icount *icount;
381 /* Determine bits that have changed */
382 dstatus = status ^ current_ctl_bits;
383 current_ctl_bits = status;
385 if (dstatus) {
386 icount = &info->state->icount;
387 /* update input line counters */
388 if (dstatus & SER_DSR)
389 icount->dsr++;
390 if (dstatus & SER_DCD) {
391 icount->dcd++;
392 #ifdef CONFIG_HARD_PPS
393 if ((info->flags & ASYNC_HARDPPS_CD) &&
394 !(status & SER_DCD))
395 hardpps();
396 #endif
398 if (dstatus & SER_CTS)
399 icount->cts++;
400 wake_up_interruptible(&info->delta_msr_wait);
403 if ((info->flags & ASYNC_CHECK_CD) && (dstatus & SER_DCD)) {
404 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
405 printk("ttyS%d CD now %s...", info->line,
406 (!(status & SER_DCD)) ? "on" : "off");
407 #endif
408 if (!(status & SER_DCD))
409 wake_up_interruptible(&info->open_wait);
410 else {
411 #ifdef SERIAL_DEBUG_OPEN
412 printk("doing serial hangup...");
413 #endif
414 if (info->tty)
415 tty_hangup(info->tty);
418 if (info->flags & ASYNC_CTS_FLOW) {
419 if (info->tty->hw_stopped) {
420 if (!(status & SER_CTS)) {
421 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
422 printk("CTS tx start...");
423 #endif
424 info->tty->hw_stopped = 0;
425 info->IER |= UART_IER_THRI;
426 custom.intena = IF_SETCLR | IF_TBE;
427 mb();
428 /* set a pending Tx Interrupt, transmitter should restart now */
429 custom.intreq = IF_SETCLR | IF_TBE;
430 mb();
431 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
432 return;
434 } else {
435 if ((status & SER_CTS)) {
436 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
437 printk("CTS tx stop...");
438 #endif
439 info->tty->hw_stopped = 1;
440 info->IER &= ~UART_IER_THRI;
441 /* disable Tx interrupt and remove any pending interrupts */
442 custom.intena = IF_TBE;
443 mb();
444 custom.intreq = IF_TBE;
445 mb();
451 static irqreturn_t ser_vbl_int( int irq, void *data)
453 /* vbl is just a periodic interrupt we tie into to update modem status */
454 struct async_struct * info = IRQ_ports;
456 * TBD - is it better to unregister from this interrupt or to
457 * ignore it if MSI is clear ?
459 if(info->IER & UART_IER_MSI)
460 check_modem_status(info);
461 return IRQ_HANDLED;
464 static irqreturn_t ser_rx_int(int irq, void *dev_id)
466 struct async_struct * info;
468 #ifdef SERIAL_DEBUG_INTR
469 printk("ser_rx_int...");
470 #endif
472 info = IRQ_ports;
473 if (!info || !info->tty)
474 return IRQ_NONE;
476 receive_chars(info);
477 info->last_active = jiffies;
478 #ifdef SERIAL_DEBUG_INTR
479 printk("end.\n");
480 #endif
481 return IRQ_HANDLED;
484 static irqreturn_t ser_tx_int(int irq, void *dev_id)
486 struct async_struct * info;
488 if (custom.serdatr & SDR_TBE) {
489 #ifdef SERIAL_DEBUG_INTR
490 printk("ser_tx_int...");
491 #endif
493 info = IRQ_ports;
494 if (!info || !info->tty)
495 return IRQ_NONE;
497 transmit_chars(info);
498 info->last_active = jiffies;
499 #ifdef SERIAL_DEBUG_INTR
500 printk("end.\n");
501 #endif
503 return IRQ_HANDLED;
507 * -------------------------------------------------------------------
508 * Here ends the serial interrupt routines.
509 * -------------------------------------------------------------------
513 * This routine is used to handle the "bottom half" processing for the
514 * serial driver, known also the "software interrupt" processing.
515 * This processing is done at the kernel interrupt level, after the
516 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
517 * is where time-consuming activities which can not be done in the
518 * interrupt driver proper are done; the interrupt driver schedules
519 * them using rs_sched_event(), and they get done here.
522 static void do_softint(unsigned long private_)
524 struct async_struct *info = (struct async_struct *) private_;
525 struct tty_struct *tty;
527 tty = info->tty;
528 if (!tty)
529 return;
531 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event))
532 tty_wakeup(tty);
536 * ---------------------------------------------------------------
537 * Low level utility subroutines for the serial driver: routines to
538 * figure out the appropriate timeout for an interrupt chain, routines
539 * to initialize and startup a serial port, and routines to shutdown a
540 * serial port. Useful stuff like that.
541 * ---------------------------------------------------------------
544 static int startup(struct async_struct * info)
546 unsigned long flags;
547 int retval=0;
548 unsigned long page;
550 page = get_zeroed_page(GFP_KERNEL);
551 if (!page)
552 return -ENOMEM;
554 local_irq_save(flags);
556 if (info->flags & ASYNC_INITIALIZED) {
557 free_page(page);
558 goto errout;
561 if (info->xmit.buf)
562 free_page(page);
563 else
564 info->xmit.buf = (unsigned char *) page;
566 #ifdef SERIAL_DEBUG_OPEN
567 printk("starting up ttys%d ...", info->line);
568 #endif
570 /* Clear anything in the input buffer */
572 custom.intreq = IF_RBF;
573 mb();
575 retval = request_irq(IRQ_AMIGA_VERTB, ser_vbl_int, 0, "serial status", info);
576 if (retval) {
577 if (serial_isroot()) {
578 if (info->tty)
579 set_bit(TTY_IO_ERROR,
580 &info->tty->flags);
581 retval = 0;
583 goto errout;
586 /* enable both Rx and Tx interrupts */
587 custom.intena = IF_SETCLR | IF_RBF | IF_TBE;
588 mb();
589 info->IER = UART_IER_MSI;
591 /* remember current state of the DCD and CTS bits */
592 current_ctl_bits = ciab.pra & (SER_DCD | SER_CTS | SER_DSR);
594 IRQ_ports = info;
596 info->MCR = 0;
597 if (info->tty->termios->c_cflag & CBAUD)
598 info->MCR = SER_DTR | SER_RTS;
599 rtsdtr_ctrl(info->MCR);
601 if (info->tty)
602 clear_bit(TTY_IO_ERROR, &info->tty->flags);
603 info->xmit.head = info->xmit.tail = 0;
606 * Set up the tty->alt_speed kludge
608 if (info->tty) {
609 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
610 info->tty->alt_speed = 57600;
611 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
612 info->tty->alt_speed = 115200;
613 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
614 info->tty->alt_speed = 230400;
615 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
616 info->tty->alt_speed = 460800;
620 * and set the speed of the serial port
622 change_speed(info, NULL);
624 info->flags |= ASYNC_INITIALIZED;
625 local_irq_restore(flags);
626 return 0;
628 errout:
629 local_irq_restore(flags);
630 return retval;
634 * This routine will shutdown a serial port; interrupts are disabled, and
635 * DTR is dropped if the hangup on close termio flag is on.
637 static void shutdown(struct async_struct * info)
639 unsigned long flags;
640 struct serial_state *state;
642 if (!(info->flags & ASYNC_INITIALIZED))
643 return;
645 state = info->state;
647 #ifdef SERIAL_DEBUG_OPEN
648 printk("Shutting down serial port %d ....\n", info->line);
649 #endif
651 local_irq_save(flags); /* Disable interrupts */
654 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
655 * here so the queue might never be waken up
657 wake_up_interruptible(&info->delta_msr_wait);
659 IRQ_ports = NULL;
662 * Free the IRQ, if necessary
664 free_irq(IRQ_AMIGA_VERTB, info);
666 if (info->xmit.buf) {
667 free_page((unsigned long) info->xmit.buf);
668 info->xmit.buf = NULL;
671 info->IER = 0;
672 custom.intena = IF_RBF | IF_TBE;
673 mb();
675 /* disable break condition */
676 custom.adkcon = AC_UARTBRK;
677 mb();
679 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
680 info->MCR &= ~(SER_DTR|SER_RTS);
681 rtsdtr_ctrl(info->MCR);
683 if (info->tty)
684 set_bit(TTY_IO_ERROR, &info->tty->flags);
686 info->flags &= ~ASYNC_INITIALIZED;
687 local_irq_restore(flags);
692 * This routine is called to set the UART divisor registers to match
693 * the specified baud rate for a serial port.
695 static void change_speed(struct async_struct *info,
696 struct ktermios *old_termios)
698 int quot = 0, baud_base, baud;
699 unsigned cflag, cval = 0;
700 int bits;
701 unsigned long flags;
703 if (!info->tty || !info->tty->termios)
704 return;
705 cflag = info->tty->termios->c_cflag;
707 /* Byte size is always 8 bits plus parity bit if requested */
709 cval = 3; bits = 10;
710 if (cflag & CSTOPB) {
711 cval |= 0x04;
712 bits++;
714 if (cflag & PARENB) {
715 cval |= UART_LCR_PARITY;
716 bits++;
718 if (!(cflag & PARODD))
719 cval |= UART_LCR_EPAR;
720 #ifdef CMSPAR
721 if (cflag & CMSPAR)
722 cval |= UART_LCR_SPAR;
723 #endif
725 /* Determine divisor based on baud rate */
726 baud = tty_get_baud_rate(info->tty);
727 if (!baud)
728 baud = 9600; /* B0 transition handled in rs_set_termios */
729 baud_base = info->state->baud_base;
730 if (baud == 38400 &&
731 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
732 quot = info->state->custom_divisor;
733 else {
734 if (baud == 134)
735 /* Special case since 134 is really 134.5 */
736 quot = (2*baud_base / 269);
737 else if (baud)
738 quot = baud_base / baud;
740 /* If the quotient is zero refuse the change */
741 if (!quot && old_termios) {
742 /* FIXME: Will need updating for new tty in the end */
743 info->tty->termios->c_cflag &= ~CBAUD;
744 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
745 baud = tty_get_baud_rate(info->tty);
746 if (!baud)
747 baud = 9600;
748 if (baud == 38400 &&
749 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
750 quot = info->state->custom_divisor;
751 else {
752 if (baud == 134)
753 /* Special case since 134 is really 134.5 */
754 quot = (2*baud_base / 269);
755 else if (baud)
756 quot = baud_base / baud;
759 /* As a last resort, if the quotient is zero, default to 9600 bps */
760 if (!quot)
761 quot = baud_base / 9600;
762 info->quot = quot;
763 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
764 info->timeout += HZ/50; /* Add .02 seconds of slop */
766 /* CTS flow control flag and modem status interrupts */
767 info->IER &= ~UART_IER_MSI;
768 if (info->flags & ASYNC_HARDPPS_CD)
769 info->IER |= UART_IER_MSI;
770 if (cflag & CRTSCTS) {
771 info->flags |= ASYNC_CTS_FLOW;
772 info->IER |= UART_IER_MSI;
773 } else
774 info->flags &= ~ASYNC_CTS_FLOW;
775 if (cflag & CLOCAL)
776 info->flags &= ~ASYNC_CHECK_CD;
777 else {
778 info->flags |= ASYNC_CHECK_CD;
779 info->IER |= UART_IER_MSI;
781 /* TBD:
782 * Does clearing IER_MSI imply that we should disable the VBL interrupt ?
786 * Set up parity check flag
789 info->read_status_mask = UART_LSR_OE | UART_LSR_DR;
790 if (I_INPCK(info->tty))
791 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
792 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
793 info->read_status_mask |= UART_LSR_BI;
796 * Characters to ignore
798 info->ignore_status_mask = 0;
799 if (I_IGNPAR(info->tty))
800 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
801 if (I_IGNBRK(info->tty)) {
802 info->ignore_status_mask |= UART_LSR_BI;
804 * If we're ignore parity and break indicators, ignore
805 * overruns too. (For real raw support).
807 if (I_IGNPAR(info->tty))
808 info->ignore_status_mask |= UART_LSR_OE;
811 * !!! ignore all characters if CREAD is not set
813 if ((cflag & CREAD) == 0)
814 info->ignore_status_mask |= UART_LSR_DR;
815 local_irq_save(flags);
818 short serper;
820 /* Set up the baud rate */
821 serper = quot - 1;
823 /* Enable or disable parity bit */
825 if(cval & UART_LCR_PARITY)
826 serper |= (SERPER_PARENB);
828 custom.serper = serper;
829 mb();
832 info->LCR = cval; /* Save LCR */
833 local_irq_restore(flags);
836 static int rs_put_char(struct tty_struct *tty, unsigned char ch)
838 struct async_struct *info;
839 unsigned long flags;
841 info = tty->driver_data;
843 if (serial_paranoia_check(info, tty->name, "rs_put_char"))
844 return 0;
846 if (!info->xmit.buf)
847 return 0;
849 local_irq_save(flags);
850 if (CIRC_SPACE(info->xmit.head,
851 info->xmit.tail,
852 SERIAL_XMIT_SIZE) == 0) {
853 local_irq_restore(flags);
854 return 0;
857 info->xmit.buf[info->xmit.head++] = ch;
858 info->xmit.head &= SERIAL_XMIT_SIZE-1;
859 local_irq_restore(flags);
860 return 1;
863 static void rs_flush_chars(struct tty_struct *tty)
865 struct async_struct *info = tty->driver_data;
866 unsigned long flags;
868 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
869 return;
871 if (info->xmit.head == info->xmit.tail
872 || tty->stopped
873 || tty->hw_stopped
874 || !info->xmit.buf)
875 return;
877 local_irq_save(flags);
878 info->IER |= UART_IER_THRI;
879 custom.intena = IF_SETCLR | IF_TBE;
880 mb();
881 /* set a pending Tx Interrupt, transmitter should restart now */
882 custom.intreq = IF_SETCLR | IF_TBE;
883 mb();
884 local_irq_restore(flags);
887 static int rs_write(struct tty_struct * tty, const unsigned char *buf, int count)
889 int c, ret = 0;
890 struct async_struct *info;
891 unsigned long flags;
893 info = tty->driver_data;
895 if (serial_paranoia_check(info, tty->name, "rs_write"))
896 return 0;
898 if (!info->xmit.buf)
899 return 0;
901 local_irq_save(flags);
902 while (1) {
903 c = CIRC_SPACE_TO_END(info->xmit.head,
904 info->xmit.tail,
905 SERIAL_XMIT_SIZE);
906 if (count < c)
907 c = count;
908 if (c <= 0) {
909 break;
911 memcpy(info->xmit.buf + info->xmit.head, buf, c);
912 info->xmit.head = ((info->xmit.head + c) &
913 (SERIAL_XMIT_SIZE-1));
914 buf += c;
915 count -= c;
916 ret += c;
918 local_irq_restore(flags);
920 if (info->xmit.head != info->xmit.tail
921 && !tty->stopped
922 && !tty->hw_stopped
923 && !(info->IER & UART_IER_THRI)) {
924 info->IER |= UART_IER_THRI;
925 local_irq_disable();
926 custom.intena = IF_SETCLR | IF_TBE;
927 mb();
928 /* set a pending Tx Interrupt, transmitter should restart now */
929 custom.intreq = IF_SETCLR | IF_TBE;
930 mb();
931 local_irq_restore(flags);
933 return ret;
936 static int rs_write_room(struct tty_struct *tty)
938 struct async_struct *info = tty->driver_data;
940 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
941 return 0;
942 return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
945 static int rs_chars_in_buffer(struct tty_struct *tty)
947 struct async_struct *info = tty->driver_data;
949 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
950 return 0;
951 return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
954 static void rs_flush_buffer(struct tty_struct *tty)
956 struct async_struct *info = tty->driver_data;
957 unsigned long flags;
959 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
960 return;
961 local_irq_save(flags);
962 info->xmit.head = info->xmit.tail = 0;
963 local_irq_restore(flags);
964 tty_wakeup(tty);
968 * This function is used to send a high-priority XON/XOFF character to
969 * the device
971 static void rs_send_xchar(struct tty_struct *tty, char ch)
973 struct async_struct *info = tty->driver_data;
974 unsigned long flags;
976 if (serial_paranoia_check(info, tty->name, "rs_send_char"))
977 return;
979 info->x_char = ch;
980 if (ch) {
981 /* Make sure transmit interrupts are on */
983 /* Check this ! */
984 local_irq_save(flags);
985 if(!(custom.intenar & IF_TBE)) {
986 custom.intena = IF_SETCLR | IF_TBE;
987 mb();
988 /* set a pending Tx Interrupt, transmitter should restart now */
989 custom.intreq = IF_SETCLR | IF_TBE;
990 mb();
992 local_irq_restore(flags);
994 info->IER |= UART_IER_THRI;
999 * ------------------------------------------------------------
1000 * rs_throttle()
1002 * This routine is called by the upper-layer tty layer to signal that
1003 * incoming characters should be throttled.
1004 * ------------------------------------------------------------
1006 static void rs_throttle(struct tty_struct * tty)
1008 struct async_struct *info = tty->driver_data;
1009 unsigned long flags;
1010 #ifdef SERIAL_DEBUG_THROTTLE
1011 char buf[64];
1013 printk("throttle %s: %d....\n", tty_name(tty, buf),
1014 tty->ldisc.chars_in_buffer(tty));
1015 #endif
1017 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1018 return;
1020 if (I_IXOFF(tty))
1021 rs_send_xchar(tty, STOP_CHAR(tty));
1023 if (tty->termios->c_cflag & CRTSCTS)
1024 info->MCR &= ~SER_RTS;
1026 local_irq_save(flags);
1027 rtsdtr_ctrl(info->MCR);
1028 local_irq_restore(flags);
1031 static void rs_unthrottle(struct tty_struct * tty)
1033 struct async_struct *info = tty->driver_data;
1034 unsigned long flags;
1035 #ifdef SERIAL_DEBUG_THROTTLE
1036 char buf[64];
1038 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1039 tty->ldisc.chars_in_buffer(tty));
1040 #endif
1042 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1043 return;
1045 if (I_IXOFF(tty)) {
1046 if (info->x_char)
1047 info->x_char = 0;
1048 else
1049 rs_send_xchar(tty, START_CHAR(tty));
1051 if (tty->termios->c_cflag & CRTSCTS)
1052 info->MCR |= SER_RTS;
1053 local_irq_save(flags);
1054 rtsdtr_ctrl(info->MCR);
1055 local_irq_restore(flags);
1059 * ------------------------------------------------------------
1060 * rs_ioctl() and friends
1061 * ------------------------------------------------------------
1064 static int get_serial_info(struct async_struct * info,
1065 struct serial_struct __user * retinfo)
1067 struct serial_struct tmp;
1068 struct serial_state *state = info->state;
1070 if (!retinfo)
1071 return -EFAULT;
1072 memset(&tmp, 0, sizeof(tmp));
1073 lock_kernel();
1074 tmp.type = state->type;
1075 tmp.line = state->line;
1076 tmp.port = state->port;
1077 tmp.irq = state->irq;
1078 tmp.flags = state->flags;
1079 tmp.xmit_fifo_size = state->xmit_fifo_size;
1080 tmp.baud_base = state->baud_base;
1081 tmp.close_delay = state->close_delay;
1082 tmp.closing_wait = state->closing_wait;
1083 tmp.custom_divisor = state->custom_divisor;
1084 unlock_kernel();
1085 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1086 return -EFAULT;
1087 return 0;
1090 static int set_serial_info(struct async_struct * info,
1091 struct serial_struct __user * new_info)
1093 struct serial_struct new_serial;
1094 struct serial_state old_state, *state;
1095 unsigned int change_irq,change_port;
1096 int retval = 0;
1098 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1099 return -EFAULT;
1101 lock_kernel();
1102 state = info->state;
1103 old_state = *state;
1105 change_irq = new_serial.irq != state->irq;
1106 change_port = (new_serial.port != state->port);
1107 if(change_irq || change_port || (new_serial.xmit_fifo_size != state->xmit_fifo_size)) {
1108 unlock_kernel();
1109 return -EINVAL;
1112 if (!serial_isroot()) {
1113 if ((new_serial.baud_base != state->baud_base) ||
1114 (new_serial.close_delay != state->close_delay) ||
1115 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1116 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1117 (state->flags & ~ASYNC_USR_MASK)))
1118 return -EPERM;
1119 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1120 (new_serial.flags & ASYNC_USR_MASK));
1121 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1122 (new_serial.flags & ASYNC_USR_MASK));
1123 state->custom_divisor = new_serial.custom_divisor;
1124 goto check_and_exit;
1127 if (new_serial.baud_base < 9600) {
1128 unlock_kernel();
1129 return -EINVAL;
1133 * OK, past this point, all the error checking has been done.
1134 * At this point, we start making changes.....
1137 state->baud_base = new_serial.baud_base;
1138 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1139 (new_serial.flags & ASYNC_FLAGS));
1140 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1141 (info->flags & ASYNC_INTERNAL_FLAGS));
1142 state->custom_divisor = new_serial.custom_divisor;
1143 state->close_delay = new_serial.close_delay * HZ/100;
1144 state->closing_wait = new_serial.closing_wait * HZ/100;
1145 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1147 check_and_exit:
1148 if (info->flags & ASYNC_INITIALIZED) {
1149 if (((old_state.flags & ASYNC_SPD_MASK) !=
1150 (state->flags & ASYNC_SPD_MASK)) ||
1151 (old_state.custom_divisor != state->custom_divisor)) {
1152 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1153 info->tty->alt_speed = 57600;
1154 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1155 info->tty->alt_speed = 115200;
1156 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1157 info->tty->alt_speed = 230400;
1158 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1159 info->tty->alt_speed = 460800;
1160 change_speed(info, NULL);
1162 } else
1163 retval = startup(info);
1164 unlock_kernel();
1165 return retval;
1170 * get_lsr_info - get line status register info
1172 * Purpose: Let user call ioctl() to get info when the UART physically
1173 * is emptied. On bus types like RS485, the transmitter must
1174 * release the bus after transmitting. This must be done when
1175 * the transmit shift register is empty, not be done when the
1176 * transmit holding register is empty. This functionality
1177 * allows an RS485 driver to be written in user space.
1179 static int get_lsr_info(struct async_struct * info, unsigned int __user *value)
1181 unsigned char status;
1182 unsigned int result;
1183 unsigned long flags;
1185 local_irq_save(flags);
1186 status = custom.serdatr;
1187 mb();
1188 local_irq_restore(flags);
1189 result = ((status & SDR_TSRE) ? TIOCSER_TEMT : 0);
1190 if (copy_to_user(value, &result, sizeof(int)))
1191 return -EFAULT;
1192 return 0;
1196 static int rs_tiocmget(struct tty_struct *tty, struct file *file)
1198 struct async_struct * info = tty->driver_data;
1199 unsigned char control, status;
1200 unsigned long flags;
1202 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1203 return -ENODEV;
1204 if (tty->flags & (1 << TTY_IO_ERROR))
1205 return -EIO;
1207 control = info->MCR;
1208 local_irq_save(flags);
1209 status = ciab.pra;
1210 local_irq_restore(flags);
1211 return ((control & SER_RTS) ? TIOCM_RTS : 0)
1212 | ((control & SER_DTR) ? TIOCM_DTR : 0)
1213 | (!(status & SER_DCD) ? TIOCM_CAR : 0)
1214 | (!(status & SER_DSR) ? TIOCM_DSR : 0)
1215 | (!(status & SER_CTS) ? TIOCM_CTS : 0);
1218 static int rs_tiocmset(struct tty_struct *tty, struct file *file,
1219 unsigned int set, unsigned int clear)
1221 struct async_struct * info = tty->driver_data;
1222 unsigned long flags;
1224 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1225 return -ENODEV;
1226 if (tty->flags & (1 << TTY_IO_ERROR))
1227 return -EIO;
1229 local_irq_save(flags);
1230 if (set & TIOCM_RTS)
1231 info->MCR |= SER_RTS;
1232 if (set & TIOCM_DTR)
1233 info->MCR |= SER_DTR;
1234 if (clear & TIOCM_RTS)
1235 info->MCR &= ~SER_RTS;
1236 if (clear & TIOCM_DTR)
1237 info->MCR &= ~SER_DTR;
1238 rtsdtr_ctrl(info->MCR);
1239 local_irq_restore(flags);
1240 return 0;
1244 * rs_break() --- routine which turns the break handling on or off
1246 static int rs_break(struct tty_struct *tty, int break_state)
1248 struct async_struct * info = tty->driver_data;
1249 unsigned long flags;
1251 if (serial_paranoia_check(info, tty->name, "rs_break"))
1252 return -EINVAL;
1254 local_irq_save(flags);
1255 if (break_state == -1)
1256 custom.adkcon = AC_SETCLR | AC_UARTBRK;
1257 else
1258 custom.adkcon = AC_UARTBRK;
1259 mb();
1260 local_irq_restore(flags);
1261 return 0;
1265 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1266 unsigned int cmd, unsigned long arg)
1268 struct async_struct * info = tty->driver_data;
1269 struct async_icount cprev, cnow; /* kernel counter temps */
1270 struct serial_icounter_struct icount;
1271 void __user *argp = (void __user *)arg;
1272 unsigned long flags;
1274 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1275 return -ENODEV;
1277 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1278 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1279 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1280 if (tty->flags & (1 << TTY_IO_ERROR))
1281 return -EIO;
1284 switch (cmd) {
1285 case TIOCGSERIAL:
1286 return get_serial_info(info, argp);
1287 case TIOCSSERIAL:
1288 return set_serial_info(info, argp);
1289 case TIOCSERCONFIG:
1290 return 0;
1292 case TIOCSERGETLSR: /* Get line status register */
1293 return get_lsr_info(info, argp);
1295 case TIOCSERGSTRUCT:
1296 if (copy_to_user(argp,
1297 info, sizeof(struct async_struct)))
1298 return -EFAULT;
1299 return 0;
1302 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1303 * - mask passed in arg for lines of interest
1304 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1305 * Caller should use TIOCGICOUNT to see which one it was
1307 case TIOCMIWAIT:
1308 local_irq_save(flags);
1309 /* note the counters on entry */
1310 cprev = info->state->icount;
1311 local_irq_restore(flags);
1312 while (1) {
1313 interruptible_sleep_on(&info->delta_msr_wait);
1314 /* see if a signal did it */
1315 if (signal_pending(current))
1316 return -ERESTARTSYS;
1317 local_irq_save(flags);
1318 cnow = info->state->icount; /* atomic copy */
1319 local_irq_restore(flags);
1320 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1321 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1322 return -EIO; /* no change => error */
1323 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1324 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1325 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1326 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1327 return 0;
1329 cprev = cnow;
1331 /* NOTREACHED */
1334 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1335 * Return: write counters to the user passed counter struct
1336 * NB: both 1->0 and 0->1 transitions are counted except for
1337 * RI where only 0->1 is counted.
1339 case TIOCGICOUNT:
1340 local_irq_save(flags);
1341 cnow = info->state->icount;
1342 local_irq_restore(flags);
1343 icount.cts = cnow.cts;
1344 icount.dsr = cnow.dsr;
1345 icount.rng = cnow.rng;
1346 icount.dcd = cnow.dcd;
1347 icount.rx = cnow.rx;
1348 icount.tx = cnow.tx;
1349 icount.frame = cnow.frame;
1350 icount.overrun = cnow.overrun;
1351 icount.parity = cnow.parity;
1352 icount.brk = cnow.brk;
1353 icount.buf_overrun = cnow.buf_overrun;
1355 if (copy_to_user(argp, &icount, sizeof(icount)))
1356 return -EFAULT;
1357 return 0;
1358 case TIOCSERGWILD:
1359 case TIOCSERSWILD:
1360 /* "setserial -W" is called in Debian boot */
1361 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1362 return 0;
1364 default:
1365 return -ENOIOCTLCMD;
1367 return 0;
1370 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1372 struct async_struct *info = tty->driver_data;
1373 unsigned long flags;
1374 unsigned int cflag = tty->termios->c_cflag;
1376 change_speed(info, old_termios);
1378 /* Handle transition to B0 status */
1379 if ((old_termios->c_cflag & CBAUD) &&
1380 !(cflag & CBAUD)) {
1381 info->MCR &= ~(SER_DTR|SER_RTS);
1382 local_irq_save(flags);
1383 rtsdtr_ctrl(info->MCR);
1384 local_irq_restore(flags);
1387 /* Handle transition away from B0 status */
1388 if (!(old_termios->c_cflag & CBAUD) &&
1389 (cflag & CBAUD)) {
1390 info->MCR |= SER_DTR;
1391 if (!(tty->termios->c_cflag & CRTSCTS) ||
1392 !test_bit(TTY_THROTTLED, &tty->flags)) {
1393 info->MCR |= SER_RTS;
1395 local_irq_save(flags);
1396 rtsdtr_ctrl(info->MCR);
1397 local_irq_restore(flags);
1400 /* Handle turning off CRTSCTS */
1401 if ((old_termios->c_cflag & CRTSCTS) &&
1402 !(tty->termios->c_cflag & CRTSCTS)) {
1403 tty->hw_stopped = 0;
1404 rs_start(tty);
1407 #if 0
1409 * No need to wake up processes in open wait, since they
1410 * sample the CLOCAL flag once, and don't recheck it.
1411 * XXX It's not clear whether the current behavior is correct
1412 * or not. Hence, this may change.....
1414 if (!(old_termios->c_cflag & CLOCAL) &&
1415 (tty->termios->c_cflag & CLOCAL))
1416 wake_up_interruptible(&info->open_wait);
1417 #endif
1421 * ------------------------------------------------------------
1422 * rs_close()
1424 * This routine is called when the serial port gets closed. First, we
1425 * wait for the last remaining data to be sent. Then, we unlink its
1426 * async structure from the interrupt chain if necessary, and we free
1427 * that IRQ if nothing is left in the chain.
1428 * ------------------------------------------------------------
1430 static void rs_close(struct tty_struct *tty, struct file * filp)
1432 struct async_struct * info = tty->driver_data;
1433 struct serial_state *state;
1434 unsigned long flags;
1436 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1437 return;
1439 state = info->state;
1441 local_irq_save(flags);
1443 if (tty_hung_up_p(filp)) {
1444 DBG_CNT("before DEC-hung");
1445 local_irq_restore(flags);
1446 return;
1449 #ifdef SERIAL_DEBUG_OPEN
1450 printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1451 #endif
1452 if ((tty->count == 1) && (state->count != 1)) {
1454 * Uh, oh. tty->count is 1, which means that the tty
1455 * structure will be freed. state->count should always
1456 * be one in these conditions. If it's greater than
1457 * one, we've got real problems, since it means the
1458 * serial port won't be shutdown.
1460 printk("rs_close: bad serial port count; tty->count is 1, "
1461 "state->count is %d\n", state->count);
1462 state->count = 1;
1464 if (--state->count < 0) {
1465 printk("rs_close: bad serial port count for ttys%d: %d\n",
1466 info->line, state->count);
1467 state->count = 0;
1469 if (state->count) {
1470 DBG_CNT("before DEC-2");
1471 local_irq_restore(flags);
1472 return;
1474 info->flags |= ASYNC_CLOSING;
1476 * Now we wait for the transmit buffer to clear; and we notify
1477 * the line discipline to only process XON/XOFF characters.
1479 tty->closing = 1;
1480 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1481 tty_wait_until_sent(tty, info->closing_wait);
1483 * At this point we stop accepting input. To do this, we
1484 * disable the receive line status interrupts, and tell the
1485 * interrupt driver to stop checking the data ready bit in the
1486 * line status register.
1488 info->read_status_mask &= ~UART_LSR_DR;
1489 if (info->flags & ASYNC_INITIALIZED) {
1490 /* disable receive interrupts */
1491 custom.intena = IF_RBF;
1492 mb();
1493 /* clear any pending receive interrupt */
1494 custom.intreq = IF_RBF;
1495 mb();
1498 * Before we drop DTR, make sure the UART transmitter
1499 * has completely drained; this is especially
1500 * important if there is a transmit FIFO!
1502 rs_wait_until_sent(tty, info->timeout);
1504 shutdown(info);
1505 rs_flush_buffer(tty);
1507 tty_ldisc_flush(tty);
1508 tty->closing = 0;
1509 info->event = 0;
1510 info->tty = NULL;
1511 if (info->blocked_open) {
1512 if (info->close_delay) {
1513 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1515 wake_up_interruptible(&info->open_wait);
1517 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1518 wake_up_interruptible(&info->close_wait);
1519 local_irq_restore(flags);
1523 * rs_wait_until_sent() --- wait until the transmitter is empty
1525 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1527 struct async_struct * info = tty->driver_data;
1528 unsigned long orig_jiffies, char_time;
1529 int lsr;
1531 if (serial_paranoia_check(info, tty->name, "rs_wait_until_sent"))
1532 return;
1534 if (info->xmit_fifo_size == 0)
1535 return; /* Just in case.... */
1537 orig_jiffies = jiffies;
1539 lock_kernel();
1541 * Set the check interval to be 1/5 of the estimated time to
1542 * send a single character, and make it at least 1. The check
1543 * interval should also be less than the timeout.
1545 * Note: we have to use pretty tight timings here to satisfy
1546 * the NIST-PCTS.
1548 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1549 char_time = char_time / 5;
1550 if (char_time == 0)
1551 char_time = 1;
1552 if (timeout)
1553 char_time = min_t(unsigned long, char_time, timeout);
1555 * If the transmitter hasn't cleared in twice the approximate
1556 * amount of time to send the entire FIFO, it probably won't
1557 * ever clear. This assumes the UART isn't doing flow
1558 * control, which is currently the case. Hence, if it ever
1559 * takes longer than info->timeout, this is probably due to a
1560 * UART bug of some kind. So, we clamp the timeout parameter at
1561 * 2*info->timeout.
1563 if (!timeout || timeout > 2*info->timeout)
1564 timeout = 2*info->timeout;
1565 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1566 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1567 printk("jiff=%lu...", jiffies);
1568 #endif
1569 while(!((lsr = custom.serdatr) & SDR_TSRE)) {
1570 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1571 printk("serdatr = %d (jiff=%lu)...", lsr, jiffies);
1572 #endif
1573 msleep_interruptible(jiffies_to_msecs(char_time));
1574 if (signal_pending(current))
1575 break;
1576 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1577 break;
1579 __set_current_state(TASK_RUNNING);
1580 unlock_kernel();
1581 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1582 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1583 #endif
1587 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1589 static void rs_hangup(struct tty_struct *tty)
1591 struct async_struct * info = tty->driver_data;
1592 struct serial_state *state = info->state;
1594 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1595 return;
1597 state = info->state;
1599 rs_flush_buffer(tty);
1600 shutdown(info);
1601 info->event = 0;
1602 state->count = 0;
1603 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1604 info->tty = NULL;
1605 wake_up_interruptible(&info->open_wait);
1609 * ------------------------------------------------------------
1610 * rs_open() and friends
1611 * ------------------------------------------------------------
1613 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1614 struct async_struct *info)
1616 #ifdef DECLARE_WAITQUEUE
1617 DECLARE_WAITQUEUE(wait, current);
1618 #else
1619 struct wait_queue wait = { current, NULL };
1620 #endif
1621 struct serial_state *state = info->state;
1622 int retval;
1623 int do_clocal = 0, extra_count = 0;
1624 unsigned long flags;
1627 * If the device is in the middle of being closed, then block
1628 * until it's done, and then try again.
1630 if (tty_hung_up_p(filp) ||
1631 (info->flags & ASYNC_CLOSING)) {
1632 if (info->flags & ASYNC_CLOSING)
1633 interruptible_sleep_on(&info->close_wait);
1634 #ifdef SERIAL_DO_RESTART
1635 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1636 -EAGAIN : -ERESTARTSYS);
1637 #else
1638 return -EAGAIN;
1639 #endif
1643 * If non-blocking mode is set, or the port is not enabled,
1644 * then make the check up front and then exit.
1646 if ((filp->f_flags & O_NONBLOCK) ||
1647 (tty->flags & (1 << TTY_IO_ERROR))) {
1648 info->flags |= ASYNC_NORMAL_ACTIVE;
1649 return 0;
1652 if (tty->termios->c_cflag & CLOCAL)
1653 do_clocal = 1;
1656 * Block waiting for the carrier detect and the line to become
1657 * free (i.e., not in use by the callout). While we are in
1658 * this loop, state->count is dropped by one, so that
1659 * rs_close() knows when to free things. We restore it upon
1660 * exit, either normal or abnormal.
1662 retval = 0;
1663 add_wait_queue(&info->open_wait, &wait);
1664 #ifdef SERIAL_DEBUG_OPEN
1665 printk("block_til_ready before block: ttys%d, count = %d\n",
1666 state->line, state->count);
1667 #endif
1668 local_irq_save(flags);
1669 if (!tty_hung_up_p(filp)) {
1670 extra_count = 1;
1671 state->count--;
1673 local_irq_restore(flags);
1674 info->blocked_open++;
1675 while (1) {
1676 local_irq_save(flags);
1677 if (tty->termios->c_cflag & CBAUD)
1678 rtsdtr_ctrl(SER_DTR|SER_RTS);
1679 local_irq_restore(flags);
1680 set_current_state(TASK_INTERRUPTIBLE);
1681 if (tty_hung_up_p(filp) ||
1682 !(info->flags & ASYNC_INITIALIZED)) {
1683 #ifdef SERIAL_DO_RESTART
1684 if (info->flags & ASYNC_HUP_NOTIFY)
1685 retval = -EAGAIN;
1686 else
1687 retval = -ERESTARTSYS;
1688 #else
1689 retval = -EAGAIN;
1690 #endif
1691 break;
1693 if (!(info->flags & ASYNC_CLOSING) &&
1694 (do_clocal || (!(ciab.pra & SER_DCD)) ))
1695 break;
1696 if (signal_pending(current)) {
1697 retval = -ERESTARTSYS;
1698 break;
1700 #ifdef SERIAL_DEBUG_OPEN
1701 printk("block_til_ready blocking: ttys%d, count = %d\n",
1702 info->line, state->count);
1703 #endif
1704 schedule();
1706 __set_current_state(TASK_RUNNING);
1707 remove_wait_queue(&info->open_wait, &wait);
1708 if (extra_count)
1709 state->count++;
1710 info->blocked_open--;
1711 #ifdef SERIAL_DEBUG_OPEN
1712 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1713 info->line, state->count);
1714 #endif
1715 if (retval)
1716 return retval;
1717 info->flags |= ASYNC_NORMAL_ACTIVE;
1718 return 0;
1721 static int get_async_struct(int line, struct async_struct **ret_info)
1723 struct async_struct *info;
1724 struct serial_state *sstate;
1726 sstate = rs_table + line;
1727 sstate->count++;
1728 if (sstate->info) {
1729 *ret_info = sstate->info;
1730 return 0;
1732 info = kzalloc(sizeof(struct async_struct), GFP_KERNEL);
1733 if (!info) {
1734 sstate->count--;
1735 return -ENOMEM;
1737 #ifdef DECLARE_WAITQUEUE
1738 init_waitqueue_head(&info->open_wait);
1739 init_waitqueue_head(&info->close_wait);
1740 init_waitqueue_head(&info->delta_msr_wait);
1741 #endif
1742 info->magic = SERIAL_MAGIC;
1743 info->port = sstate->port;
1744 info->flags = sstate->flags;
1745 info->xmit_fifo_size = sstate->xmit_fifo_size;
1746 info->line = line;
1747 tasklet_init(&info->tlet, do_softint, (unsigned long)info);
1748 info->state = sstate;
1749 if (sstate->info) {
1750 kfree(info);
1751 *ret_info = sstate->info;
1752 return 0;
1754 *ret_info = sstate->info = info;
1755 return 0;
1759 * This routine is called whenever a serial port is opened. It
1760 * enables interrupts for a serial port, linking in its async structure into
1761 * the IRQ chain. It also performs the serial-specific
1762 * initialization for the tty structure.
1764 static int rs_open(struct tty_struct *tty, struct file * filp)
1766 struct async_struct *info;
1767 int retval, line;
1769 line = tty->index;
1770 if ((line < 0) || (line >= NR_PORTS)) {
1771 return -ENODEV;
1773 retval = get_async_struct(line, &info);
1774 if (retval) {
1775 return retval;
1777 tty->driver_data = info;
1778 info->tty = tty;
1779 if (serial_paranoia_check(info, tty->name, "rs_open"))
1780 return -ENODEV;
1782 #ifdef SERIAL_DEBUG_OPEN
1783 printk("rs_open %s, count = %d\n", tty->name, info->state->count);
1784 #endif
1785 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1788 * If the port is the middle of closing, bail out now
1790 if (tty_hung_up_p(filp) ||
1791 (info->flags & ASYNC_CLOSING)) {
1792 if (info->flags & ASYNC_CLOSING)
1793 interruptible_sleep_on(&info->close_wait);
1794 #ifdef SERIAL_DO_RESTART
1795 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1796 -EAGAIN : -ERESTARTSYS);
1797 #else
1798 return -EAGAIN;
1799 #endif
1803 * Start up serial port
1805 retval = startup(info);
1806 if (retval) {
1807 return retval;
1810 retval = block_til_ready(tty, filp, info);
1811 if (retval) {
1812 #ifdef SERIAL_DEBUG_OPEN
1813 printk("rs_open returning after block_til_ready with %d\n",
1814 retval);
1815 #endif
1816 return retval;
1819 #ifdef SERIAL_DEBUG_OPEN
1820 printk("rs_open %s successful...", tty->name);
1821 #endif
1822 return 0;
1826 * /proc fs routines....
1829 static inline void line_info(struct seq_file *m, struct serial_state *state)
1831 struct async_struct *info = state->info, scr_info;
1832 char stat_buf[30], control, status;
1833 unsigned long flags;
1835 seq_printf(m, "%d: uart:amiga_builtin",state->line);
1838 * Figure out the current RS-232 lines
1840 if (!info) {
1841 info = &scr_info; /* This is just for serial_{in,out} */
1843 info->magic = SERIAL_MAGIC;
1844 info->flags = state->flags;
1845 info->quot = 0;
1846 info->tty = NULL;
1848 local_irq_save(flags);
1849 status = ciab.pra;
1850 control = info ? info->MCR : status;
1851 local_irq_restore(flags);
1853 stat_buf[0] = 0;
1854 stat_buf[1] = 0;
1855 if(!(control & SER_RTS))
1856 strcat(stat_buf, "|RTS");
1857 if(!(status & SER_CTS))
1858 strcat(stat_buf, "|CTS");
1859 if(!(control & SER_DTR))
1860 strcat(stat_buf, "|DTR");
1861 if(!(status & SER_DSR))
1862 strcat(stat_buf, "|DSR");
1863 if(!(status & SER_DCD))
1864 strcat(stat_buf, "|CD");
1866 if (info->quot) {
1867 seq_printf(m, " baud:%d", state->baud_base / info->quot);
1870 seq_printf(m, " tx:%d rx:%d", state->icount.tx, state->icount.rx);
1872 if (state->icount.frame)
1873 seq_printf(m, " fe:%d", state->icount.frame);
1875 if (state->icount.parity)
1876 seq_printf(m, " pe:%d", state->icount.parity);
1878 if (state->icount.brk)
1879 seq_printf(m, " brk:%d", state->icount.brk);
1881 if (state->icount.overrun)
1882 seq_printf(m, " oe:%d", state->icount.overrun);
1885 * Last thing is the RS-232 status lines
1887 seq_printf(m, " %s\n", stat_buf+1);
1890 static int rs_proc_show(struct seq_file *m, void *v)
1892 seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
1893 line_info(m, &rs_table[0]);
1894 return 0;
1897 static int rs_proc_open(struct inode *inode, struct file *file)
1899 return single_open(file, rs_proc_show, NULL);
1902 static const struct file_operations rs_proc_fops = {
1903 .owner = THIS_MODULE,
1904 .open = rs_proc_open,
1905 .read = seq_read,
1906 .llseek = seq_lseek,
1907 .release = single_release,
1911 * ---------------------------------------------------------------------
1912 * rs_init() and friends
1914 * rs_init() is called at boot-time to initialize the serial driver.
1915 * ---------------------------------------------------------------------
1919 * This routine prints out the appropriate serial driver version
1920 * number, and identifies which options were configured into this
1921 * driver.
1923 static void show_serial_version(void)
1925 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
1929 static const struct tty_operations serial_ops = {
1930 .open = rs_open,
1931 .close = rs_close,
1932 .write = rs_write,
1933 .put_char = rs_put_char,
1934 .flush_chars = rs_flush_chars,
1935 .write_room = rs_write_room,
1936 .chars_in_buffer = rs_chars_in_buffer,
1937 .flush_buffer = rs_flush_buffer,
1938 .ioctl = rs_ioctl,
1939 .throttle = rs_throttle,
1940 .unthrottle = rs_unthrottle,
1941 .set_termios = rs_set_termios,
1942 .stop = rs_stop,
1943 .start = rs_start,
1944 .hangup = rs_hangup,
1945 .break_ctl = rs_break,
1946 .send_xchar = rs_send_xchar,
1947 .wait_until_sent = rs_wait_until_sent,
1948 .tiocmget = rs_tiocmget,
1949 .tiocmset = rs_tiocmset,
1950 .proc_fops = &rs_proc_fops,
1954 * The serial driver boot-time initialization code!
1956 static int __init rs_init(void)
1958 unsigned long flags;
1959 struct serial_state * state;
1960 int error;
1962 if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_SERIAL))
1963 return -ENODEV;
1965 serial_driver = alloc_tty_driver(1);
1966 if (!serial_driver)
1967 return -ENOMEM;
1970 * We request SERDAT and SERPER only, because the serial registers are
1971 * too spreaded over the custom register space
1973 if (!request_mem_region(CUSTOM_PHYSADDR+0x30, 4,
1974 "amiserial [Paula]")) {
1975 error = -EBUSY;
1976 goto fail_put_tty_driver;
1979 IRQ_ports = NULL;
1981 show_serial_version();
1983 /* Initialize the tty_driver structure */
1985 serial_driver->owner = THIS_MODULE;
1986 serial_driver->driver_name = "amiserial";
1987 serial_driver->name = "ttyS";
1988 serial_driver->major = TTY_MAJOR;
1989 serial_driver->minor_start = 64;
1990 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1991 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1992 serial_driver->init_termios = tty_std_termios;
1993 serial_driver->init_termios.c_cflag =
1994 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1995 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1996 tty_set_operations(serial_driver, &serial_ops);
1998 error = tty_register_driver(serial_driver);
1999 if (error)
2000 goto fail_release_mem_region;
2002 state = rs_table;
2003 state->magic = SSTATE_MAGIC;
2004 state->port = (int)&custom.serdatr; /* Just to give it a value */
2005 state->line = 0;
2006 state->custom_divisor = 0;
2007 state->close_delay = 5*HZ/10;
2008 state->closing_wait = 30*HZ;
2009 state->icount.cts = state->icount.dsr =
2010 state->icount.rng = state->icount.dcd = 0;
2011 state->icount.rx = state->icount.tx = 0;
2012 state->icount.frame = state->icount.parity = 0;
2013 state->icount.overrun = state->icount.brk = 0;
2015 printk(KERN_INFO "ttyS%d is the amiga builtin serial port\n",
2016 state->line);
2018 /* Hardware set up */
2020 state->baud_base = amiga_colorclock;
2021 state->xmit_fifo_size = 1;
2023 local_irq_save(flags);
2025 /* set ISRs, and then disable the rx interrupts */
2026 error = request_irq(IRQ_AMIGA_TBE, ser_tx_int, 0, "serial TX", state);
2027 if (error)
2028 goto fail_unregister;
2030 error = request_irq(IRQ_AMIGA_RBF, ser_rx_int, IRQF_DISABLED,
2031 "serial RX", state);
2032 if (error)
2033 goto fail_free_irq;
2035 /* turn off Rx and Tx interrupts */
2036 custom.intena = IF_RBF | IF_TBE;
2037 mb();
2039 /* clear any pending interrupt */
2040 custom.intreq = IF_RBF | IF_TBE;
2041 mb();
2043 local_irq_restore(flags);
2046 * set the appropriate directions for the modem control flags,
2047 * and clear RTS and DTR
2049 ciab.ddra |= (SER_DTR | SER_RTS); /* outputs */
2050 ciab.ddra &= ~(SER_DCD | SER_CTS | SER_DSR); /* inputs */
2052 return 0;
2054 fail_free_irq:
2055 free_irq(IRQ_AMIGA_TBE, state);
2056 fail_unregister:
2057 tty_unregister_driver(serial_driver);
2058 fail_release_mem_region:
2059 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2060 fail_put_tty_driver:
2061 put_tty_driver(serial_driver);
2062 return error;
2065 static __exit void rs_exit(void)
2067 int error;
2068 struct async_struct *info = rs_table[0].info;
2070 /* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2071 tasklet_kill(&info->tlet);
2072 if ((error = tty_unregister_driver(serial_driver)))
2073 printk("SERIAL: failed to unregister serial driver (%d)\n",
2074 error);
2075 put_tty_driver(serial_driver);
2077 if (info) {
2078 rs_table[0].info = NULL;
2079 kfree(info);
2082 free_irq(IRQ_AMIGA_TBE, rs_table);
2083 free_irq(IRQ_AMIGA_RBF, rs_table);
2085 release_mem_region(CUSTOM_PHYSADDR+0x30, 4);
2088 module_init(rs_init)
2089 module_exit(rs_exit)
2092 #if defined(CONFIG_SERIAL_CONSOLE) && !defined(MODULE)
2095 * ------------------------------------------------------------
2096 * Serial console driver
2097 * ------------------------------------------------------------
2100 static void amiga_serial_putc(char c)
2102 custom.serdat = (unsigned char)c | 0x100;
2103 while (!(custom.serdatr & 0x2000))
2104 barrier();
2108 * Print a string to the serial port trying not to disturb
2109 * any possible real use of the port...
2111 * The console must be locked when we get here.
2113 static void serial_console_write(struct console *co, const char *s,
2114 unsigned count)
2116 unsigned short intena = custom.intenar;
2118 custom.intena = IF_TBE;
2120 while (count--) {
2121 if (*s == '\n')
2122 amiga_serial_putc('\r');
2123 amiga_serial_putc(*s++);
2126 custom.intena = IF_SETCLR | (intena & IF_TBE);
2129 static struct tty_driver *serial_console_device(struct console *c, int *index)
2131 *index = 0;
2132 return serial_driver;
2135 static struct console sercons = {
2136 .name = "ttyS",
2137 .write = serial_console_write,
2138 .device = serial_console_device,
2139 .flags = CON_PRINTBUFFER,
2140 .index = -1,
2144 * Register console.
2146 static int __init amiserial_console_init(void)
2148 register_console(&sercons);
2149 return 0;
2151 console_initcall(amiserial_console_init);
2153 #endif /* CONFIG_SERIAL_CONSOLE && !MODULE */
2155 MODULE_LICENSE("GPL");