1 /* $Id: su.c,v 1.1.1.1 2005/04/11 02:50:34 jack Exp $
2 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
4 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
5 * Copyright (C) 1998-1999 Pete Zaitcev (zaitcev@yahoo.com)
7 * This is mainly a variation of drivers/char/serial.c,
8 * credits go to authors mentioned therein.
10 * Fixed to use tty_get_baud_rate().
11 * Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
15 * Configuration section.
17 #undef SERIAL_PARANOIA_CHECK
18 #define CONFIG_SERIAL_NOPAUSE_IO /* Unused on sparc */
19 #define SERIAL_DO_RESTART
21 /* Set of debugging defines */
23 #undef SERIAL_DEBUG_INTR
24 #undef SERIAL_DEBUG_OPEN
25 #undef SERIAL_DEBUG_FLOW
26 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
27 #undef SERIAL_DEBUG_THROTTLE
29 #define RS_ISR_PASS_LIMIT 256
32 * 0x20 is sun4m thing, Dave Redman heritage.
33 * See arch/sparc/kernel/irq.c.
35 #define IRQ_4M(n) ((n)|0x20)
37 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
40 printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
41 kdevname(tty->device), (info->flags), serial_refcount, \
42 info->count,tty->count,s); \
49 * End of serial driver configuration section.
51 #include <linux/config.h>
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/signal.h>
55 #include <linux/sched.h>
56 #include <linux/interrupt.h>
57 #include <linux/tty.h>
58 #include <linux/tty_flip.h>
59 #include <linux/serial.h>
60 #include <linux/serialP.h>
61 #include <linux/serial_reg.h>
62 #include <linux/string.h>
63 #include <linux/fcntl.h>
64 #include <linux/ptrace.h>
65 #include <linux/ioport.h>
67 #include <linux/slab.h>
68 #include <linux/init.h>
69 #include <linux/bootmem.h>
70 #include <linux/delay.h>
71 #ifdef CONFIG_SERIAL_CONSOLE
72 #include <linux/console.h>
73 #include <linux/major.h>
75 #include <linux/sysrq.h>
77 #include <asm/system.h>
78 #include <asm/oplib.h>
85 #include <asm/uaccess.h>
86 #include <asm/bitops.h>
88 #include "sunserial.h"
92 /* We are on a NS PC87303 clocked with 24.0 MHz, which results
93 * in a UART clock of 1.8462 MHz.
95 #define BAUD_BASE (1846200 / 16)
97 #ifdef CONFIG_SERIAL_CONSOLE
98 extern int serial_console;
99 static struct console sercons;
100 int su_serial_console_init(void);
103 enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
104 static char *su_typev[] = { "???", "mouse", "kbd", "serial" };
106 #define SU_PROPSIZE 128
109 * serial.c saves memory when it allocates async_info upon first open.
110 * We have parts of state structure together because we do call startup
111 * for keyboard and mouse.
117 int type; /* Hardware type: e.g. 16550 */
123 enum su_type port_type; /* Hookup type: e.g. mouse */
131 unsigned short close_delay;
132 unsigned short closing_wait; /* time to wait before closing */
134 struct tty_struct *tty;
135 int read_status_mask;
136 int ignore_status_mask;
139 int x_char; /* xon/xoff character */
140 int IER; /* Interrupt Enable Register */
141 int MCR; /* Modem control register */
143 int blocked_open; /* # of blocked opens */
144 long session; /* Session of opening process */
145 long pgrp; /* pgrp of opening process */
146 unsigned char *xmit_buf;
150 struct tq_struct tqueue;
151 wait_queue_head_t open_wait;
152 wait_queue_head_t close_wait;
153 wait_queue_head_t delta_msr_wait;
156 struct async_icount icount;
157 struct termios normal_termios, callout_termios;
158 unsigned long last_active; /* For async_struct, to be */
162 * Scan status structure.
163 * "prop" is a local variable but it eats stack to keep it in each
164 * stack frame of a recursive procedure.
166 struct su_probe_scan {
167 int msnode, kbnode; /* PROM nodes for mouse and keyboard */
168 int msx, kbx; /* minors for mouse and keyboard */
169 int devices; /* scan index */
170 char prop[SU_PROPSIZE];
173 static char *serial_name = "PCIO serial driver";
174 static char serial_version[16];
176 static DECLARE_TASK_QUEUE(tq_serial);
178 static struct tty_driver serial_driver, callout_driver;
179 static int serial_refcount;
181 /* number of characters left in xmit buffer before we ask for more */
182 #define WAKEUP_CHARS 256
184 static void autoconfig(struct su_struct *info);
185 static void change_speed(struct su_struct *info, struct termios *old);
186 static void su_wait_until_sent(struct tty_struct *tty, int timeout);
189 * Here we define the default xmit fifo size used for each type of
192 static struct serial_uart_config uart_config[] = {
197 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
199 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
200 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
202 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
209 static struct su_struct su_table[NR_PORTS];
210 static struct tty_struct *serial_table[NR_PORTS];
211 static struct termios *serial_termios[NR_PORTS];
212 static struct termios *serial_termios_locked[NR_PORTS];
215 #define MIN(a,b) ((a) < (b) ? (a) : (b))
219 * tmp_buf is used as a temporary buffer by serial_write. We need to
220 * lock it in case the copy_from_user blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
227 static unsigned char *tmp_buf;
228 static DECLARE_MUTEX(tmp_buf_sem);
230 static inline int serial_paranoia_check(struct su_struct *info,
231 kdev_t device, const char *routine)
233 #ifdef SERIAL_PARANOIA_CHECK
234 static const char *badmagic = KERN_WARNING
235 "Warning: bad magic number for serial struct (%s) in %s\n";
236 static const char *badinfo = KERN_WARNING
237 "Warning: null su_struct for (%s) in %s\n";
240 printk(badinfo, kdevname(device), routine);
243 if (info->magic != SERIAL_MAGIC) {
244 printk(badmagic, kdevname(device), routine);
252 unsigned int su_inb(struct su_struct *info, unsigned long offset)
254 return inb(info->port + offset);
258 su_outb(struct su_struct *info, unsigned long offset, int value)
262 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
263 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
264 * gate outputs a logical one. Since we use level triggered interrupts
265 * we have lockup and watchdog reset. We cannot mask IRQ because
266 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
267 * This problem is similar to what Alpha people suffer, see serial.c.
269 if (offset == UART_MCR) value |= UART_MCR_OUT2;
271 outb(value, info->port + offset);
274 #define serial_in(info, off) su_inb(info, off)
275 #define serial_inp(info, off) su_inb(info, off)
276 #define serial_out(info, off, val) su_outb(info, off, val)
277 #define serial_outp(info, off, val) su_outb(info, off, val)
280 * ------------------------------------------------------------
281 * su_stop() and su_start()
283 * This routines are called before setting or resetting tty->stopped.
284 * They enable or disable transmitter interrupts, as necessary.
285 * ------------------------------------------------------------
287 static void su_stop(struct tty_struct *tty)
289 struct su_struct *info = (struct su_struct *)tty->driver_data;
292 if (serial_paranoia_check(info, tty->device, "su_stop"))
295 save_flags(flags); cli();
296 if (info->IER & UART_IER_THRI) {
297 info->IER &= ~UART_IER_THRI;
298 serial_out(info, UART_IER, info->IER);
300 restore_flags(flags);
303 static void su_start(struct tty_struct *tty)
305 struct su_struct *info = (struct su_struct *)tty->driver_data;
308 if (serial_paranoia_check(info, tty->device, "su_start"))
311 save_flags(flags); cli();
312 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
313 info->IER |= UART_IER_THRI;
314 serial_out(info, UART_IER, info->IER);
316 restore_flags(flags);
320 * ----------------------------------------------------------------------
322 * Here starts the interrupt handling routines. All of the following
323 * subroutines are declared as inline and are folded into
324 * su_interrupt(). They were separated out for readability's sake.
326 * Note: rs_interrupt() is a "fast" interrupt, which means that it
327 * runs with interrupts turned off. People who may want to modify
328 * rs_interrupt() should try to keep the interrupt handler as fast as
329 * possible. After you are done making modifications, it is not a bad
332 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
334 * and look at the resulting assemble code in serial.s.
336 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
337 * -----------------------------------------------------------------------
341 * This routine is used by the interrupt handler to schedule
342 * processing in the software interrupt portion of the driver.
345 su_sched_event(struct su_struct *info, int event)
347 info->event |= 1 << event;
348 queue_task(&info->tqueue, &tq_serial);
353 receive_kbd_ms_chars(struct su_struct *info, struct pt_regs *regs, int is_brk)
355 unsigned char status = 0;
359 ch = serial_inp(info, UART_RX);
360 if (info->port_type == SU_PORT_KBD) {
361 if (ch == SUNKBD_RESET) {
362 l1a_state.kbd_id = 1;
363 l1a_state.l1_down = 0;
364 } else if (l1a_state.kbd_id) {
365 l1a_state.kbd_id = 0;
366 } else if (ch == SUNKBD_L1) {
367 l1a_state.l1_down = 1;
368 } else if (ch == (SUNKBD_L1|SUNKBD_UP)) {
369 l1a_state.l1_down = 0;
370 } else if (ch == SUNKBD_A && l1a_state.l1_down) {
372 batten_down_hatches();
373 /* Continue execution... */
374 l1a_state.l1_down = 0;
375 l1a_state.kbd_id = 0;
378 sunkbd_inchar(ch, regs);
380 sun_mouse_inbyte(ch, is_brk);
383 status = su_inb(info, UART_LSR);
384 } while (status & UART_LSR_DR);
388 receive_serial_chars(struct su_struct *info, int *status, struct pt_regs *regs)
390 struct tty_struct *tty = info->tty;
392 int ignored = 0, saw_console_brk = 0;
393 struct async_icount *icount;
395 icount = &info->icount;
397 ch = serial_inp(info, UART_RX);
398 if (info->is_console &&
399 (ch == 0 || (*status &UART_LSR_BI)))
401 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
403 *tty->flip.char_buf_ptr = ch;
406 #ifdef SERIAL_DEBUG_INTR
407 printk("D%02x:%02x.", ch, *status);
409 *tty->flip.flag_buf_ptr = 0;
410 if (*status & (UART_LSR_BI | UART_LSR_PE |
411 UART_LSR_FE | UART_LSR_OE)) {
413 * For statistics only
415 if (*status & UART_LSR_BI) {
416 *status &= ~(UART_LSR_FE | UART_LSR_PE);
418 } else if (*status & UART_LSR_PE)
420 else if (*status & UART_LSR_FE)
422 if (*status & UART_LSR_OE)
426 * Now check to see if character should be
427 * ignored, and mask off conditions which
430 if (*status & info->ignore_status_mask) {
431 if (++ignored > 100) {
432 #ifdef SERIAL_DEBUG_INTR
439 *status &= info->read_status_mask;
441 if (*status & (UART_LSR_BI)) {
442 #ifdef SERIAL_DEBUG_INTR
443 printk("handling break....");
445 *tty->flip.flag_buf_ptr = TTY_BREAK;
446 if (info->flags & ASYNC_SAK)
448 } else if (*status & UART_LSR_PE)
449 *tty->flip.flag_buf_ptr = TTY_PARITY;
450 else if (*status & UART_LSR_FE)
451 *tty->flip.flag_buf_ptr = TTY_FRAME;
452 if (*status & UART_LSR_OE) {
454 * Overrun is special, since it's
455 * reported immediately, and doesn't
456 * affect the current character
458 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
460 tty->flip.flag_buf_ptr++;
461 tty->flip.char_buf_ptr++;
462 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
466 tty->flip.flag_buf_ptr++;
467 tty->flip.char_buf_ptr++;
470 *status = serial_inp(info, UART_LSR);
471 } while (*status & UART_LSR_DR);
472 #ifdef SERIAL_DEBUG_INTR
473 printk("E%02x.R%d", *status, tty->flip.count);
475 tty_flip_buffer_push(tty);
476 if (saw_console_brk != 0)
477 batten_down_hatches();
481 transmit_chars(struct su_struct *info, int *intr_done)
486 serial_outp(info, UART_TX, info->x_char);
493 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
494 info->tty->hw_stopped) {
495 info->IER &= ~UART_IER_THRI;
496 serial_out(info, UART_IER, info->IER);
500 count = info->xmit_fifo_size;
502 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
503 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
505 if (--info->xmit_cnt <= 0)
507 } while (--count > 0);
509 if (info->xmit_cnt < WAKEUP_CHARS)
510 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
512 #ifdef SERIAL_DEBUG_INTR
513 printk("T%d...", info->xmit_cnt);
518 if (info->xmit_cnt <= 0) {
519 info->IER &= ~UART_IER_THRI;
520 serial_out(info, UART_IER, info->IER);
525 check_modem_status(struct su_struct *info)
528 struct async_icount *icount;
530 status = serial_in(info, UART_MSR);
532 if (status & UART_MSR_ANY_DELTA) {
533 icount = &info->icount;
534 /* update input line counters */
535 if (status & UART_MSR_TERI)
537 if (status & UART_MSR_DDSR)
539 if (status & UART_MSR_DDCD) {
541 #ifdef CONFIG_HARD_PPS
542 if ((info->flags & ASYNC_HARDPPS_CD) &&
543 (status & UART_MSR_DCD))
547 if (status & UART_MSR_DCTS)
549 wake_up_interruptible(&info->delta_msr_wait);
552 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
553 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
554 printk("ttys%d CD now %s...", info->line,
555 (status & UART_MSR_DCD) ? "on" : "off");
557 if (status & UART_MSR_DCD)
558 wake_up_interruptible(&info->open_wait);
559 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
560 (info->flags & ASYNC_CALLOUT_NOHUP))) {
561 #ifdef SERIAL_DEBUG_OPEN
562 printk("doing serial hangup...");
565 tty_hangup(info->tty);
568 if (info->flags & ASYNC_CTS_FLOW) {
569 if (info->tty->hw_stopped) {
570 if (status & UART_MSR_CTS) {
571 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
572 printk("CTS tx start...");
574 info->tty->hw_stopped = 0;
575 info->IER |= UART_IER_THRI;
576 serial_out(info, UART_IER, info->IER);
577 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
581 if (!(status & UART_MSR_CTS)) {
582 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
583 printk("CTS tx stop...");
585 info->tty->hw_stopped = 1;
586 info->IER &= ~UART_IER_THRI;
587 serial_out(info, UART_IER, info->IER);
594 * This is the kbd/mouse serial driver's interrupt routine
597 su_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs * regs)
599 struct su_struct *info = (struct su_struct *)dev_id;
600 unsigned char status;
602 #ifdef SERIAL_DEBUG_INTR
603 printk("su_kbd_ms_interrupt(%s)...", __irq_itoa(irq));
608 if (serial_in(info, UART_IIR) & UART_IIR_NO_INT)
611 status = serial_inp(info, UART_LSR);
612 #ifdef SERIAL_DEBUG_INTR
613 printk("status = %x...", status);
615 if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
616 receive_kbd_ms_chars(info, regs,
617 (status & UART_LSR_BI) != 0);
619 #ifdef SERIAL_DEBUG_INTR
625 * This is the serial driver's generic interrupt routine
628 su_serial_interrupt(int irq, void *dev_id, struct pt_regs * regs)
631 struct su_struct *info;
632 int pass_counter = 0;
634 #ifdef SERIAL_DEBUG_INTR
635 printk("su_serial_interrupt(%s)...", __irq_itoa(irq));
637 info = (struct su_struct *)dev_id;
638 if (!info || !info->tty) {
639 #ifdef SERIAL_DEBUG_INTR
646 status = serial_inp(info, UART_LSR);
647 #ifdef SERIAL_DEBUG_INTR
648 printk("status = %x...", status);
650 if (status & UART_LSR_DR)
651 receive_serial_chars(info, &status, regs);
652 check_modem_status(info);
653 if (status & UART_LSR_THRE)
654 transmit_chars(info, 0);
656 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
657 #ifdef SERIAL_DEBUG_INTR
658 printk("rs loop break");
660 break; /* Prevent infinite loops */
662 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
664 info->last_active = jiffies;
666 #ifdef SERIAL_DEBUG_INTR
672 * -------------------------------------------------------------------
673 * Here ends the serial interrupt routines.
674 * -------------------------------------------------------------------
678 * This routine is used to handle the "bottom half" processing for the
679 * serial driver, known also the "software interrupt" processing.
680 * This processing is done at the kernel interrupt level, after the
681 * su_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
682 * is where time-consuming activities which can not be done in the
683 * interrupt driver proper are done; the interrupt driver schedules
684 * them using su_sched_event(), and they get done here.
686 static void do_serial_bh(void)
688 run_task_queue(&tq_serial);
691 static void do_softint(void *private_)
693 struct su_struct *info = (struct su_struct *) private_;
694 struct tty_struct *tty;
700 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
701 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
702 tty->ldisc.write_wakeup)
703 (tty->ldisc.write_wakeup)(tty);
704 wake_up_interruptible(&tty->write_wait);
709 * ---------------------------------------------------------------
710 * Low level utility subroutines for the serial driver: routines to
711 * figure out the appropriate timeout for an interrupt chain, routines
712 * to initialize and startup a serial port, and routines to shutdown a
713 * serial port. Useful stuff like that.
714 * ---------------------------------------------------------------
718 startup(struct su_struct *info)
726 page = get_free_page(GFP_KERNEL);
732 if (info->flags & ASYNC_INITIALIZED) {
737 if (info->port == 0 || info->type == PORT_UNKNOWN) {
738 set_bit(TTY_IO_ERROR, &info->tty->flags);
745 info->xmit_buf = (unsigned char *) page;
749 #ifdef SERIAL_DEBUG_OPEN
750 printk("starting up ttys%d (irq %s)...", info->line,
751 __irq_itoa(info->irq));
754 if (uart_config[info->type].flags & UART_STARTECH) {
756 serial_outp(info, UART_LCR, 0xBF);
757 serial_outp(info, UART_EFR, UART_EFR_ECB);
758 serial_outp(info, UART_IER, 0);
759 serial_outp(info, UART_EFR, 0);
760 serial_outp(info, UART_LCR, 0);
763 if (info->type == PORT_16750) {
765 serial_outp(info, UART_IER, 0);
769 * Clear the FIFO buffers and disable them
770 * (they will be reenabled in change_speed())
772 if (uart_config[info->type].flags & UART_CLEAR_FIFO)
773 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
774 UART_FCR_CLEAR_XMIT));
777 * At this point there's no way the LSR could still be 0xFF;
778 * if it is, then bail out, because there's likely no UART
781 if (serial_inp(info, UART_LSR) == 0xff) {
782 if (capable(CAP_SYS_ADMIN)) {
784 set_bit(TTY_IO_ERROR, &info->tty->flags);
791 * Allocate the IRQ if necessary
793 if (info->port_type != SU_PORT_PORT) {
794 retval = request_irq(info->irq, su_kbd_ms_interrupt,
795 SA_SHIRQ, info->name, info);
797 retval = request_irq(info->irq, su_serial_interrupt,
798 SA_SHIRQ, info->name, info);
801 if (capable(CAP_SYS_ADMIN)) {
803 set_bit(TTY_IO_ERROR, &info->tty->flags);
810 * Clear the interrupt registers.
812 (void) serial_inp(info, UART_RX);
813 (void) serial_inp(info, UART_IIR);
814 (void) serial_inp(info, UART_MSR);
817 * Now, initialize the UART
819 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
822 if (info->tty && info->tty->termios->c_cflag & CBAUD)
823 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
825 info->MCR |= UART_MCR_OUT2;
826 serial_outp(info, UART_MCR, info->MCR);
829 * Finally, enable interrupts
831 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
832 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
835 * And clear the interrupt registers again for luck.
837 (void)serial_inp(info, UART_LSR);
838 (void)serial_inp(info, UART_RX);
839 (void)serial_inp(info, UART_IIR);
840 (void)serial_inp(info, UART_MSR);
843 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
847 * Set up the tty->alt_speed kludge
850 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
851 info->tty->alt_speed = 57600;
852 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
853 info->tty->alt_speed = 115200;
854 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
855 info->tty->alt_speed = 230400;
856 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
857 info->tty->alt_speed = 460800;
861 * and set the speed of the serial port
863 change_speed(info, 0);
865 info->flags |= ASYNC_INITIALIZED;
866 restore_flags(flags);
870 restore_flags(flags);
875 * This routine will shutdown a serial port; interrupts are disabled, and
876 * DTR is dropped if the hangup on close termio flag is on.
879 shutdown(struct su_struct *info)
883 if (!(info->flags & ASYNC_INITIALIZED))
886 save_flags(flags); cli(); /* Disable interrupts */
889 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
890 * here so the queue might never be waken up
892 wake_up_interruptible(&info->delta_msr_wait);
895 * Free the IRQ, if necessary
897 free_irq(info->irq, info);
899 if (info->xmit_buf) {
900 free_page((unsigned long) info->xmit_buf);
905 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
906 info->MCR &= ~UART_MCR_OUT2;
908 /* disable break condition */
909 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
911 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
912 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
913 serial_outp(info, UART_MCR, info->MCR);
916 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
917 UART_FCR_CLEAR_XMIT));
918 (void)serial_in(info, UART_RX); /* read data port to reset things */
921 set_bit(TTY_IO_ERROR, &info->tty->flags);
923 if (uart_config[info->type].flags & UART_STARTECH) {
924 /* Arrange to enter sleep mode */
925 serial_outp(info, UART_LCR, 0xBF);
926 serial_outp(info, UART_EFR, UART_EFR_ECB);
927 serial_outp(info, UART_IER, UART_IERX_SLEEP);
928 serial_outp(info, UART_LCR, 0);
930 if (info->type == PORT_16750) {
931 /* Arrange to enter sleep mode */
932 serial_outp(info, UART_IER, UART_IERX_SLEEP);
934 info->flags &= ~ASYNC_INITIALIZED;
935 restore_flags(flags);
939 su_get_baud_rate(struct su_struct *info)
941 static struct tty_struct c_tty;
942 static struct termios c_termios;
945 return tty_get_baud_rate(info->tty);
947 memset(&c_tty, 0, sizeof(c_tty));
948 memset(&c_termios, 0, sizeof(c_termios));
949 c_tty.termios = &c_termios;
950 c_termios.c_cflag = info->cflag;
952 return tty_get_baud_rate(&c_tty);
956 * This routine is called to set the UART divisor registers to match
957 * the specified baud rate for a serial port.
960 change_speed(struct su_struct *info,
961 struct termios *old_termios)
964 unsigned int cval, fcr = 0;
968 if (info->port_type == SU_PORT_PORT) {
969 if (!info->tty || !info->tty->termios)
973 info->cflag = info->tty->termios->c_cflag;
976 /* byte size and parity */
977 switch (info->cflag & CSIZE) {
978 case CS5: cval = 0x00; bits = 7; break;
979 case CS6: cval = 0x01; bits = 8; break;
980 case CS7: cval = 0x02; bits = 9; break;
981 case CS8: cval = 0x03; bits = 10; break;
982 /* Never happens, but GCC is too dumb to figure it out */
983 default: cval = 0x00; bits = 7; break;
985 if (info->cflag & CSTOPB) {
989 if (info->cflag & PARENB) {
990 cval |= UART_LCR_PARITY;
993 if (!(info->cflag & PARODD))
994 cval |= UART_LCR_EPAR;
996 if (info->cflag & CMSPAR)
997 cval |= UART_LCR_SPAR;
1000 /* Determine divisor based on baud rate */
1001 baud = su_get_baud_rate(info);
1002 if (baud == 38400 &&
1003 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1004 quot = info->custom_divisor;
1007 /* Special case since 134 is really 134.5 */
1008 quot = (2 * info->baud_base / 269);
1010 quot = info->baud_base / baud;
1012 /* If the quotient is zero refuse the change */
1013 if (!quot && old_termios) {
1014 info->tty->termios->c_cflag &= ~CBAUD;
1015 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1016 baud = tty_get_baud_rate(info->tty);
1019 if (baud == 38400 &&
1020 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1021 quot = info->custom_divisor;
1024 /* Special case since 134 is really 134.5 */
1025 quot = (2*info->baud_base / 269);
1027 quot = info->baud_base / baud;
1030 /* As a last resort, if the quotient is zero, default to 9600 bps */
1032 quot = info->baud_base / 9600;
1033 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / info->baud_base);
1034 info->timeout += HZ/50; /* Add .02 seconds of slop */
1037 if (uart_config[info->type].flags & UART_USE_FIFO) {
1038 if ((info->baud_base / quot) < 9600)
1039 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1041 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1043 if (info->type == PORT_16750)
1044 fcr |= UART_FCR7_64BYTE;
1046 /* CTS flow control flag and modem status interrupts */
1047 info->IER &= ~UART_IER_MSI;
1048 if (info->flags & ASYNC_HARDPPS_CD)
1049 info->IER |= UART_IER_MSI;
1050 if (info->cflag & CRTSCTS) {
1051 info->flags |= ASYNC_CTS_FLOW;
1052 info->IER |= UART_IER_MSI;
1054 info->flags &= ~ASYNC_CTS_FLOW;
1055 if (info->cflag & CLOCAL)
1056 info->flags &= ~ASYNC_CHECK_CD;
1058 info->flags |= ASYNC_CHECK_CD;
1059 info->IER |= UART_IER_MSI;
1061 serial_out(info, UART_IER, info->IER);
1064 * Set up parity check flag
1067 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1069 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE |
1071 if (I_INPCK(info->tty))
1072 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1073 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1074 info->read_status_mask |= UART_LSR_BI;
1077 * Characters to ignore
1079 info->ignore_status_mask = 0;
1080 if (I_IGNPAR(info->tty))
1081 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1082 if (I_IGNBRK(info->tty)) {
1083 info->ignore_status_mask |= UART_LSR_BI;
1085 * If we're ignore parity and break indicators, ignore
1086 * overruns too. (For real raw support).
1088 if (I_IGNPAR(info->tty))
1089 info->ignore_status_mask |= UART_LSR_OE;
1092 * !!! ignore all characters if CREAD is not set
1094 if ((info->cflag & CREAD) == 0)
1095 info->ignore_status_mask |= UART_LSR_DR;
1098 save_flags(flags); cli();
1099 if (uart_config[info->type].flags & UART_STARTECH) {
1100 serial_outp(info, UART_LCR, 0xBF);
1101 serial_outp(info, UART_EFR,
1102 (info->cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1104 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1105 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1106 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1107 if (info->type == PORT_16750)
1108 serial_outp(info, UART_FCR, fcr); /* set fcr */
1109 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1110 if (info->type != PORT_16750)
1111 serial_outp(info, UART_FCR, fcr); /* set fcr */
1112 restore_flags(flags);
1117 su_put_char(struct tty_struct *tty, unsigned char ch)
1119 struct su_struct *info = (struct su_struct *)tty->driver_data;
1120 unsigned long flags;
1122 if (serial_paranoia_check(info, tty->device, "su_put_char"))
1125 if (!tty || !info->xmit_buf)
1128 save_flags(flags); cli();
1129 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1130 restore_flags(flags);
1134 info->xmit_buf[info->xmit_head++] = ch;
1135 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1137 restore_flags(flags);
1140 static void su_put_char_kbd(unsigned char c)
1142 struct su_struct *info = su_table;
1145 if (info->port_type != SU_PORT_KBD)
1147 if (info->port_type != SU_PORT_KBD)
1151 lsr = serial_in(info, UART_LSR);
1152 } while (!(lsr & UART_LSR_THRE));
1154 /* Send the character out. */
1155 su_outb(info, UART_TX, c);
1159 su_change_mouse_baud(int baud)
1161 struct su_struct *info = su_table;
1163 if (info->port_type != SU_PORT_MS)
1165 if (info->port_type != SU_PORT_MS)
1168 info->cflag &= ~CBAUD;
1171 info->cflag |= B1200;
1174 info->cflag |= B2400;
1177 info->cflag |= B4800;
1180 info->cflag |= B9600;
1183 printk("su_change_mouse_baud: unknown baud rate %d, "
1184 "defaulting to 1200\n", baud);
1185 info->cflag |= 1200;
1188 change_speed(info, 0);
1192 su_flush_chars(struct tty_struct *tty)
1194 struct su_struct *info = (struct su_struct *)tty->driver_data;
1195 unsigned long flags;
1197 if (serial_paranoia_check(info, tty->device, "su_flush_chars"))
1200 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1204 save_flags(flags); cli();
1205 info->IER |= UART_IER_THRI;
1206 serial_out(info, UART_IER, info->IER);
1207 restore_flags(flags);
1211 su_write(struct tty_struct * tty, int from_user,
1212 const unsigned char *buf, int count)
1215 struct su_struct *info = (struct su_struct *)tty->driver_data;
1216 unsigned long flags;
1218 if (serial_paranoia_check(info, tty->device, "su_write"))
1221 if (!tty || !info->xmit_buf || !tmp_buf)
1229 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1230 SERIAL_XMIT_SIZE - info->xmit_head));
1234 c -= copy_from_user(tmp_buf, buf, c);
1241 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1242 SERIAL_XMIT_SIZE - info->xmit_head));
1243 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1244 info->xmit_head = ((info->xmit_head + c) &
1245 (SERIAL_XMIT_SIZE-1));
1246 info->xmit_cnt += c;
1247 restore_flags(flags);
1257 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1258 SERIAL_XMIT_SIZE - info->xmit_head));
1260 restore_flags(flags);
1263 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1264 info->xmit_head = ((info->xmit_head + c) &
1265 (SERIAL_XMIT_SIZE-1));
1266 info->xmit_cnt += c;
1267 restore_flags(flags);
1273 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1274 !(info->IER & UART_IER_THRI)) {
1275 info->IER |= UART_IER_THRI;
1276 serial_out(info, UART_IER, info->IER);
1282 su_write_room(struct tty_struct *tty)
1284 struct su_struct *info = (struct su_struct *)tty->driver_data;
1287 if (serial_paranoia_check(info, tty->device, "su_write_room"))
1289 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1296 su_chars_in_buffer(struct tty_struct *tty)
1298 struct su_struct *info = (struct su_struct *)tty->driver_data;
1300 if (serial_paranoia_check(info, tty->device, "su_chars_in_buffer"))
1302 return info->xmit_cnt;
1306 su_flush_buffer(struct tty_struct *tty)
1308 struct su_struct *info = (struct su_struct *)tty->driver_data;
1309 unsigned long flags;
1311 if (serial_paranoia_check(info, tty->device, "su_flush_buffer"))
1313 save_flags(flags); cli();
1314 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1315 restore_flags(flags);
1316 wake_up_interruptible(&tty->write_wait);
1317 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1318 tty->ldisc.write_wakeup)
1319 (tty->ldisc.write_wakeup)(tty);
1323 * This function is used to send a high-priority XON/XOFF character to
1327 su_send_xchar(struct tty_struct *tty, char ch)
1329 struct su_struct *info = (struct su_struct *)tty->driver_data;
1331 if (serial_paranoia_check(info, tty->device, "su_send_char"))
1334 if (!(info->flags & ASYNC_INITIALIZED))
1339 /* Make sure transmit interrupts are on */
1340 info->IER |= UART_IER_THRI;
1341 serial_out(info, UART_IER, info->IER);
1346 * ------------------------------------------------------------
1349 * This routine is called by the upper-layer tty layer to signal that
1350 * incoming characters should be throttled.
1351 * ------------------------------------------------------------
1354 su_throttle(struct tty_struct * tty)
1356 struct su_struct *info = (struct su_struct *)tty->driver_data;
1357 unsigned long flags;
1358 #ifdef SERIAL_DEBUG_THROTTLE
1361 printk("throttle %s: %d....\n", tty_name(tty, buf),
1362 tty->ldisc.chars_in_buffer(tty));
1365 if (serial_paranoia_check(info, tty->device, "su_throttle"))
1369 su_send_xchar(tty, STOP_CHAR(tty));
1371 if (tty->termios->c_cflag & CRTSCTS)
1372 info->MCR &= ~UART_MCR_RTS;
1374 save_flags(flags); cli();
1375 serial_out(info, UART_MCR, info->MCR);
1376 restore_flags(flags);
1380 su_unthrottle(struct tty_struct * tty)
1382 struct su_struct *info = (struct su_struct *)tty->driver_data;
1383 unsigned long flags;
1384 #ifdef SERIAL_DEBUG_THROTTLE
1387 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1388 tty->ldisc.chars_in_buffer(tty));
1391 if (serial_paranoia_check(info, tty->device, "su_unthrottle"))
1398 su_send_xchar(tty, START_CHAR(tty));
1400 if (tty->termios->c_cflag & CRTSCTS)
1401 info->MCR |= UART_MCR_RTS;
1402 save_flags(flags); cli();
1403 serial_out(info, UART_MCR, info->MCR);
1404 restore_flags(flags);
1408 * ------------------------------------------------------------
1409 * su_ioctl() and friends
1410 * ------------------------------------------------------------
1414 * get_serial_info - handle TIOCGSERIAL ioctl()
1416 * Purpose: Return standard serial struct information about
1417 * a serial port handled by this driver.
1419 * Added: 11-May-2001 Lars Kellogg-Stedman <lars@larsshack.org>
1421 static int get_serial_info(struct su_struct * info,
1422 struct serial_struct * retinfo)
1424 struct serial_struct tmp;
1428 memset(&tmp, 0, sizeof(tmp));
1430 tmp.type = info->type;
1431 tmp.line = info->line;
1432 tmp.port = info->port;
1433 tmp.irq = info->irq;
1434 tmp.flags = info->flags;
1435 tmp.xmit_fifo_size = info->xmit_fifo_size;
1436 tmp.baud_base = info->baud_base;
1437 tmp.close_delay = info->close_delay;
1438 tmp.closing_wait = info->closing_wait;
1439 tmp.custom_divisor = info->custom_divisor;
1442 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1449 * get_lsr_info - get line status register info
1451 * Purpose: Let user call ioctl() to get info when the UART physically
1452 * is emptied. On bus types like RS485, the transmitter must
1453 * release the bus after transmitting. This must be done when
1454 * the transmit shift register is empty, not be done when the
1455 * transmit holding register is empty. This functionality
1456 * allows an RS485 driver to be written in user space.
1459 get_lsr_info(struct su_struct * info, unsigned int *value)
1461 unsigned char status;
1462 unsigned int result;
1463 unsigned long flags;
1465 save_flags(flags); cli();
1466 status = serial_in(info, UART_LSR);
1467 restore_flags(flags);
1468 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1469 return put_user(result,value);
1474 get_modem_info(struct su_struct * info, unsigned int *value)
1476 unsigned char control, status;
1477 unsigned int result;
1478 unsigned long flags;
1480 control = info->MCR;
1481 save_flags(flags); cli();
1482 status = serial_in(info, UART_MSR);
1483 restore_flags(flags);
1484 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1485 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1487 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1488 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1490 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1491 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1492 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1493 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1494 return put_user(result,value);
1498 set_modem_info(struct su_struct * info, unsigned int cmd, unsigned int *value)
1501 unsigned long flags;
1503 if (get_user(arg, value))
1507 if (arg & TIOCM_RTS)
1508 info->MCR |= UART_MCR_RTS;
1509 if (arg & TIOCM_DTR)
1510 info->MCR |= UART_MCR_DTR;
1512 if (arg & TIOCM_OUT1)
1513 info->MCR |= UART_MCR_OUT1;
1514 if (arg & TIOCM_OUT2)
1515 info->MCR |= UART_MCR_OUT2;
1519 if (arg & TIOCM_RTS)
1520 info->MCR &= ~UART_MCR_RTS;
1521 if (arg & TIOCM_DTR)
1522 info->MCR &= ~UART_MCR_DTR;
1524 if (arg & TIOCM_OUT1)
1525 info->MCR &= ~UART_MCR_OUT1;
1526 if (arg & TIOCM_OUT2)
1527 info->MCR &= ~UART_MCR_OUT2;
1531 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1537 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1539 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1540 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1542 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1547 save_flags(flags); cli();
1548 serial_out(info, UART_MCR, info->MCR);
1549 restore_flags(flags);
1554 * su_break() --- routine which turns the break handling on or off
1557 su_break(struct tty_struct *tty, int break_state)
1559 struct su_struct * info = (struct su_struct *)tty->driver_data;
1560 unsigned long flags;
1562 if (serial_paranoia_check(info, tty->device, "su_break"))
1567 save_flags(flags); cli();
1568 if (break_state == -1)
1569 serial_out(info, UART_LCR,
1570 serial_inp(info, UART_LCR) | UART_LCR_SBC);
1572 serial_out(info, UART_LCR,
1573 serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1574 restore_flags(flags);
1578 su_ioctl(struct tty_struct *tty, struct file * file,
1579 unsigned int cmd, unsigned long arg)
1581 struct su_struct * info = (struct su_struct *)tty->driver_data;
1582 struct async_icount cprev, cnow; /* kernel counter temps */
1583 struct serial_icounter_struct *p_cuser; /* user space */
1585 if (serial_paranoia_check(info, tty->device, "su_ioctl"))
1588 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1589 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1590 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1591 if (tty->flags & (1 << TTY_IO_ERROR))
1597 return get_modem_info(info, (unsigned int *) arg);
1601 return set_modem_info(info, cmd, (unsigned int *) arg);
1604 return get_serial_info(info, (struct serial_struct *)arg);
1606 case TIOCSERGETLSR: /* Get line status register */
1607 return get_lsr_info(info, (unsigned int *) arg);
1610 case TIOCSERGSTRUCT:
1611 if (copy_to_user((struct async_struct *) arg,
1612 info, sizeof(struct async_struct)))
1618 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1619 * - mask passed in arg for lines of interest
1620 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1621 * Caller should use TIOCGICOUNT to see which one it was
1625 /* note the counters on entry */
1626 cprev = info->icount;
1629 interruptible_sleep_on(&info->delta_msr_wait);
1630 /* see if a signal did it */
1631 if (signal_pending(current))
1632 return -ERESTARTSYS;
1634 cnow = info->icount; /* atomic copy */
1636 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1637 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1638 return -EIO; /* no change => error */
1639 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1640 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1641 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1642 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1650 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1651 * Return: write counters to the user passed counter struct
1652 * NB: both 1->0 and 0->1 transitions are counted except for
1653 * RI where only 0->1 is counted.
1657 cnow = info->icount;
1659 p_cuser = (struct serial_icounter_struct *) arg;
1660 if (put_user(cnow.cts, &p_cuser->cts) ||
1661 put_user(cnow.dsr, &p_cuser->dsr) ||
1662 put_user(cnow.rng, &p_cuser->rng) ||
1663 put_user(cnow.dcd, &p_cuser->dcd))
1668 return -ENOIOCTLCMD;
1670 /* return 0; */ /* Trigger warnings if fall through by a chance. */
1674 su_set_termios(struct tty_struct *tty, struct termios *old_termios)
1676 struct su_struct *info = (struct su_struct *)tty->driver_data;
1677 unsigned long flags;
1679 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1680 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1681 == RELEVANT_IFLAG(old_termios->c_iflag)))
1684 change_speed(info, old_termios);
1686 /* Handle transition to B0 status */
1687 if ((old_termios->c_cflag & CBAUD) &&
1688 !(tty->termios->c_cflag & CBAUD)) {
1689 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1690 save_flags(flags); cli();
1691 serial_out(info, UART_MCR, info->MCR);
1692 restore_flags(flags);
1695 /* Handle transition away from B0 status */
1696 if (!(old_termios->c_cflag & CBAUD) &&
1697 (tty->termios->c_cflag & CBAUD)) {
1698 info->MCR |= UART_MCR_DTR;
1699 if (!(tty->termios->c_cflag & CRTSCTS) ||
1700 !test_bit(TTY_THROTTLED, &tty->flags)) {
1701 info->MCR |= UART_MCR_RTS;
1703 save_flags(flags); cli();
1704 serial_out(info, UART_MCR, info->MCR);
1705 restore_flags(flags);
1708 /* Handle turning off CRTSCTS */
1709 if ((old_termios->c_cflag & CRTSCTS) &&
1710 !(tty->termios->c_cflag & CRTSCTS)) {
1711 tty->hw_stopped = 0;
1717 * No need to wake up processes in open wait, since they
1718 * sample the CLOCAL flag once, and don't recheck it.
1719 * XXX It's not clear whether the current behavior is correct
1720 * or not. Hence, this may change.....
1722 if (!(old_termios->c_cflag & CLOCAL) &&
1723 (tty->termios->c_cflag & CLOCAL))
1724 wake_up_interruptible(&info->open_wait);
1729 * ------------------------------------------------------------
1732 * This routine is called when the serial port gets closed. First, we
1733 * wait for the last remaining data to be sent. Then, we unlink its
1734 * async structure from the interrupt chain if necessary, and we free
1735 * that IRQ if nothing is left in the chain.
1736 * ------------------------------------------------------------
1739 su_close(struct tty_struct *tty, struct file * filp)
1741 struct su_struct *info = (struct su_struct *)tty->driver_data;
1742 unsigned long flags;
1744 if (!info || serial_paranoia_check(info, tty->device, "su_close"))
1747 save_flags(flags); cli();
1749 if (tty_hung_up_p(filp)) {
1750 DBG_CNT("before DEC-hung");
1752 restore_flags(flags);
1756 #ifdef SERIAL_DEBUG_OPEN
1757 printk("su_close ttys%d, count = %d\n", info->line, info->count);
1759 if ((tty->count == 1) && (info->count != 1)) {
1761 * Uh, oh. tty->count is 1, which means that the tty
1762 * structure will be freed. info->count should always
1763 * be one in these conditions. If it's greater than
1764 * one, we've got real problems, since it means the
1765 * serial port won't be shutdown.
1767 printk("su_close: bad serial port count; tty->count is 1, "
1768 "info->count is %d\n", info->count);
1771 if (--info->count < 0) {
1772 printk("su_close: bad serial port count for ttys%d: %d\n",
1773 info->line, info->count);
1777 DBG_CNT("before DEC-2");
1779 restore_flags(flags);
1782 info->flags |= ASYNC_CLOSING;
1784 * Save the termios structure, since this port may have
1785 * separate termios for callout and dialin.
1787 if (info->flags & ASYNC_NORMAL_ACTIVE)
1788 info->normal_termios = *tty->termios;
1789 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1790 info->callout_termios = *tty->termios;
1792 * Now we wait for the transmit buffer to clear; and we notify
1793 * the line discipline to only process XON/XOFF characters.
1796 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1797 tty_wait_until_sent(tty, info->closing_wait);
1799 * At this point we stop accepting input. To do this, we
1800 * disable the receive line status interrupts, and tell the
1801 * interrupt driver to stop checking the data ready bit in the
1802 * line status register.
1804 info->IER &= ~UART_IER_RLSI;
1805 info->read_status_mask &= ~UART_LSR_DR;
1806 if (info->flags & ASYNC_INITIALIZED) {
1807 serial_out(info, UART_IER, info->IER);
1809 * Before we drop DTR, make sure the UART transmitter
1810 * has completely drained; this is especially
1811 * important if there is a transmit FIFO!
1813 su_wait_until_sent(tty, info->timeout);
1816 if (tty->driver.flush_buffer)
1817 tty->driver.flush_buffer(tty);
1818 if (tty->ldisc.flush_buffer)
1819 tty->ldisc.flush_buffer(tty);
1823 if (info->blocked_open) {
1824 if (info->close_delay) {
1825 current->state = TASK_INTERRUPTIBLE;
1826 schedule_timeout(info->close_delay);
1828 wake_up_interruptible(&info->open_wait);
1830 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1832 wake_up_interruptible(&info->close_wait);
1834 restore_flags(flags);
1838 * su_wait_until_sent() --- wait until the transmitter is empty
1841 su_wait_until_sent(struct tty_struct *tty, int timeout)
1843 struct su_struct * info = (struct su_struct *)tty->driver_data;
1844 unsigned long orig_jiffies, char_time;
1847 if (serial_paranoia_check(info, tty->device, "su_wait_until_sent"))
1850 if (info->type == PORT_UNKNOWN)
1853 if (info->xmit_fifo_size == 0)
1854 return; /* Just in case ... */
1856 orig_jiffies = jiffies;
1858 * Set the check interval to be 1/5 of the estimated time to
1859 * send a single character, and make it at least 1. The check
1860 * interval should also be less than the timeout.
1862 * Note: we have to use pretty tight timings here to satisfy
1865 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1866 char_time = char_time / 5;
1870 char_time = MIN(char_time, timeout);
1871 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1872 printk("In su_wait_until_sent(%d) check=%lu...", timeout, char_time);
1873 printk("jiff=%lu...", jiffies);
1875 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
1876 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1877 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1879 current->state = TASK_INTERRUPTIBLE;
1880 schedule_timeout(char_time);
1881 if (signal_pending(current))
1883 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1886 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1887 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1892 * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1895 su_hangup(struct tty_struct *tty)
1897 struct su_struct * info = (struct su_struct *)tty->driver_data;
1899 if (serial_paranoia_check(info, tty->device, "su_hangup"))
1902 su_flush_buffer(tty);
1906 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1908 wake_up_interruptible(&info->open_wait);
1912 * ------------------------------------------------------------
1913 * su_open() and friends
1914 * ------------------------------------------------------------
1917 block_til_ready(struct tty_struct *tty, struct file * filp,
1918 struct su_struct *info)
1920 DECLARE_WAITQUEUE(wait, current);
1922 int do_clocal = 0, extra_count = 0;
1923 unsigned long flags;
1926 * If the device is in the middle of being closed, then block
1927 * until it's done, and then try again.
1929 if (tty_hung_up_p(filp) ||
1930 (info->flags & ASYNC_CLOSING)) {
1931 if (info->flags & ASYNC_CLOSING)
1932 interruptible_sleep_on(&info->close_wait);
1933 #ifdef SERIAL_DO_RESTART
1934 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1935 -EAGAIN : -ERESTARTSYS);
1942 * If this is a callout device, then just make sure the normal
1943 * device isn't being used.
1945 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1946 if (info->flags & ASYNC_NORMAL_ACTIVE)
1948 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1949 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1950 (info->session != current->session))
1952 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1953 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1954 (info->pgrp != current->pgrp))
1956 info->flags |= ASYNC_CALLOUT_ACTIVE;
1961 * If non-blocking mode is set, or the port is not enabled,
1962 * then make the check up front and then exit.
1964 if ((filp->f_flags & O_NONBLOCK) ||
1965 (tty->flags & (1 << TTY_IO_ERROR))) {
1966 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1968 info->flags |= ASYNC_NORMAL_ACTIVE;
1972 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1973 if (info->normal_termios.c_cflag & CLOCAL)
1976 if (tty->termios->c_cflag & CLOCAL)
1981 * Block waiting for the carrier detect and the line to become
1982 * free (i.e., not in use by the callout). While we are in
1983 * this loop, info->count is dropped by one, so that
1984 * su_close() knows when to free things. We restore it upon
1985 * exit, either normal or abnormal.
1988 add_wait_queue(&info->open_wait, &wait);
1989 #ifdef SERIAL_DEBUG_OPEN
1990 printk("block_til_ready before block: ttys%d, count = %d\n",
1991 info->line, info->count);
1993 save_flags(flags); cli();
1994 if (!tty_hung_up_p(filp)) {
1998 restore_flags(flags);
1999 info->blocked_open++;
2001 save_flags(flags); cli();
2002 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2003 (tty->termios->c_cflag & CBAUD))
2004 serial_out(info, UART_MCR,
2005 serial_inp(info, UART_MCR) |
2006 (UART_MCR_DTR | UART_MCR_RTS));
2007 restore_flags(flags);
2008 set_current_state(TASK_INTERRUPTIBLE);
2009 if (tty_hung_up_p(filp) ||
2010 !(info->flags & ASYNC_INITIALIZED)) {
2011 #ifdef SERIAL_DO_RESTART
2012 if (info->flags & ASYNC_HUP_NOTIFY)
2015 retval = -ERESTARTSYS;
2021 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2022 !(info->flags & ASYNC_CLOSING) &&
2023 (do_clocal || (serial_in(info, UART_MSR) &
2026 if (signal_pending(current)) {
2027 retval = -ERESTARTSYS;
2030 #ifdef SERIAL_DEBUG_OPEN
2031 printk("block_til_ready blocking: ttys%d, count = %d\n",
2032 info->line, info->count);
2036 current->state = TASK_RUNNING;
2037 remove_wait_queue(&info->open_wait, &wait);
2040 info->blocked_open--;
2041 #ifdef SERIAL_DEBUG_OPEN
2042 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2043 info->line, info->count);
2047 info->flags |= ASYNC_NORMAL_ACTIVE;
2052 * This routine is called whenever a serial port is opened. It
2053 * enables interrupts for a serial port, linking in its async structure into
2054 * the IRQ chain. It also performs the serial-specific
2055 * initialization for the tty structure.
2058 su_open(struct tty_struct *tty, struct file * filp)
2060 struct su_struct *info;
2064 line = MINOR(tty->device) - tty->driver.minor_start;
2065 if ((line < 0) || (line >= NR_PORTS))
2067 info = su_table + line;
2069 tty->driver_data = info;
2072 if (serial_paranoia_check(info, tty->device, "su_open")) {
2077 #ifdef SERIAL_DEBUG_OPEN
2078 printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2081 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2084 page = get_free_page(GFP_KERNEL);
2090 tmp_buf = (unsigned char *) page;
2094 * If the port is the middle of closing, bail out now
2096 if (tty_hung_up_p(filp) ||
2097 (info->flags & ASYNC_CLOSING)) {
2098 if (info->flags & ASYNC_CLOSING)
2099 interruptible_sleep_on(&info->close_wait);
2100 #ifdef SERIAL_DO_RESTART
2101 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2102 -EAGAIN : -ERESTARTSYS);
2109 * Start up serial port
2111 retval = startup(info);
2116 retval = block_til_ready(tty, filp, info);
2118 #ifdef SERIAL_DEBUG_OPEN
2119 printk("su_open returning after block_til_ready with %d\n",
2125 if ((info->count == 1) &&
2126 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2127 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2128 *tty->termios = info->normal_termios;
2130 *tty->termios = info->callout_termios;
2131 change_speed(info, 0);
2133 #ifdef CONFIG_SERIAL_CONSOLE
2134 if (sercons.cflag && sercons.index == line) {
2135 tty->termios->c_cflag = sercons.cflag;
2137 change_speed(info, 0);
2140 info->session = current->session;
2141 info->pgrp = current->pgrp;
2143 #ifdef SERIAL_DEBUG_OPEN
2144 printk("su_open ttys%d successful...", info->line);
2150 * /proc fs routines....
2153 line_info(char *buf, struct su_struct *info)
2155 char stat_buf[30], control, status;
2157 unsigned long flags;
2159 if (info->port == 0 || info->type == PORT_UNKNOWN)
2162 ret = sprintf(buf, "%u: uart:%s port:%lX irq:%s",
2163 info->line, uart_config[info->type].name,
2164 (unsigned long)info->port, __irq_itoa(info->irq));
2167 * Figure out the current RS-232 lines
2169 save_flags(flags); cli();
2170 status = serial_in(info, UART_MSR);
2171 control = info ? info->MCR : serial_in(info, UART_MCR);
2172 restore_flags(flags);
2176 if (control & UART_MCR_RTS)
2177 strcat(stat_buf, "|RTS");
2178 if (status & UART_MSR_CTS)
2179 strcat(stat_buf, "|CTS");
2180 if (control & UART_MCR_DTR)
2181 strcat(stat_buf, "|DTR");
2182 if (status & UART_MSR_DSR)
2183 strcat(stat_buf, "|DSR");
2184 if (status & UART_MSR_DCD)
2185 strcat(stat_buf, "|CD");
2186 if (status & UART_MSR_RI)
2187 strcat(stat_buf, "|RI");
2190 ret += sprintf(buf+ret, " baud:%u",
2191 info->baud_base / info->quot);
2194 ret += sprintf(buf+ret, " tx:%u rx:%u",
2195 info->icount.tx, info->icount.rx);
2197 if (info->icount.frame)
2198 ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2200 if (info->icount.parity)
2201 ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2203 if (info->icount.brk)
2204 ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2206 if (info->icount.overrun)
2207 ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2210 * Last thing is the RS-232 status lines
2212 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2216 int su_read_proc(char *page, char **start, off_t off, int count,
2217 int *eof, void *data)
2222 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2223 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2224 len += line_info(page + len, &su_table[i]);
2225 if (len+begin > off+count)
2227 if (len+begin < off) {
2234 if (off >= len+begin)
2236 *start = page + (off-begin);
2237 return ((count < begin+len-off) ? count : begin+len-off);
2241 * ---------------------------------------------------------------------
2242 * su_XXX_init() and friends
2244 * su_XXX_init() is called at boot-time to initialize the serial driver.
2245 * ---------------------------------------------------------------------
2249 * This routine prints out the appropriate serial driver version
2250 * number, and identifies which options were configured into this
2253 static __inline__ void __init show_su_version(void)
2255 char *revision = "$Revision: 1.1.1.1 $";
2258 version = strchr(revision, ' ');
2259 strcpy(serial_version, ++version);
2260 p = strchr(serial_version, ' ');
2262 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2266 * This routine is called by su_{serial|kbd_ms}_init() to initialize a specific
2267 * serial port. It determines what type of UART chip this serial port is
2268 * using: 8250, 16450, 16550, 16550A. The important question is
2269 * whether or not this UART is a 16550A, since this will determine
2270 * whether or not we can use its FIFO features.
2273 autoconfig(struct su_struct *info)
2275 unsigned char status1, status2, scratch, scratch2;
2276 struct linux_ebus_device *dev = 0;
2277 struct linux_ebus *ebus;
2278 #ifdef CONFIG_SPARC64
2279 struct isa_bridge *isa_br;
2280 struct isa_device *isa_dev;
2282 #ifndef __sparc_v9__
2283 struct linux_prom_registers reg0;
2285 unsigned long flags;
2287 if (!info->port_node || !info->port_type)
2291 * First we look for Ebus-bases su's
2293 for_each_ebus(ebus) {
2294 for_each_ebusdev(dev, ebus) {
2295 if (dev->prom_node == info->port_node) {
2296 info->port = dev->resource[0].start;
2297 info->irq = dev->irqs[0];
2303 #ifdef CONFIG_SPARC64
2304 for_each_isa(isa_br) {
2305 for_each_isadev(isa_dev, isa_br) {
2306 if (isa_dev->prom_node == info->port_node) {
2307 info->port = isa_dev->resource.start;
2308 info->irq = isa_dev->irq;
2317 * Not on Ebus, bailing.
2322 * Not on Ebus, must be OBIO.
2324 if (prom_getproperty(info->port_node, "reg",
2325 (char *)®0, sizeof(reg0)) == -1) {
2326 prom_printf("su: no \"reg\" property\n");
2329 prom_apply_obio_ranges(®0, 1);
2330 if (reg0.which_io != 0) { /* Just in case... */
2331 prom_printf("su: bus number nonzero: 0x%x:%x\n",
2332 reg0.which_io, reg0.phys_addr);
2335 if ((info->port = (unsigned long) ioremap(reg0.phys_addr,
2336 reg0.reg_size)) == 0) {
2337 prom_printf("su: cannot map\n");
2342 * There is no intr property on MrCoffee, so hardwire it.
2344 info->irq = IRQ_4M(13);
2349 #ifdef SERIAL_DEBUG_OPEN
2350 printk("Found 'su' at %016lx IRQ %s\n", info->port,
2351 __irq_itoa(info->irq));
2354 info->magic = SERIAL_MAGIC;
2356 save_flags(flags); cli();
2359 * Do a simple existence test first; if we fail this, there's
2360 * no point trying anything else.
2362 * 0x80 is used as a nonsense port to prevent against false
2363 * positives due to ISA bus float. The assumption is that
2364 * 0x80 is a non-existent port; which should be safe since
2365 * include/asm/io.h also makes this assumption.
2367 scratch = serial_inp(info, UART_IER);
2368 serial_outp(info, UART_IER, 0);
2369 scratch2 = serial_inp(info, UART_IER);
2370 serial_outp(info, UART_IER, scratch);
2372 restore_flags(flags);
2373 return; /* We failed; there's nothing here */
2376 scratch = serial_inp(info, UART_MCR);
2377 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2378 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2379 status1 = serial_inp(info, UART_MSR) & 0xF0;
2380 serial_outp(info, UART_MCR, scratch);
2381 if (status1 != 0x90) {
2383 * This code fragment used to fail, now it fixed itself.
2384 * We keep the printout for a case.
2386 printk("su: loopback returned status 0x%02x\n", status1);
2387 restore_flags(flags);
2391 scratch2 = serial_in(info, UART_LCR);
2392 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
2393 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2394 serial_outp(info, UART_LCR, 0);
2395 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2396 scratch = serial_in(info, UART_IIR) >> 6;
2399 info->type = PORT_16450;
2402 info->type = PORT_UNKNOWN;
2405 info->type = PORT_16550;
2408 info->type = PORT_16550A;
2411 if (info->type == PORT_16550A) {
2412 /* Check for Startech UART's */
2413 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2414 if (serial_in(info, UART_EFR) == 0) {
2415 info->type = PORT_16650;
2417 serial_outp(info, UART_LCR, 0xBF);
2418 if (serial_in(info, UART_EFR) == 0)
2419 info->type = PORT_16650V2;
2422 if (info->type == PORT_16550A) {
2423 /* Check for TI 16750 */
2424 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2425 serial_outp(info, UART_FCR,
2426 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2427 scratch = serial_in(info, UART_IIR) >> 5;
2429 serial_outp(info, UART_LCR, 0);
2430 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2431 scratch = serial_in(info, UART_IIR) >> 5;
2433 info->type = PORT_16750;
2435 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2437 serial_outp(info, UART_LCR, scratch2);
2438 if (info->type == PORT_16450) {
2439 scratch = serial_in(info, UART_SCR);
2440 serial_outp(info, UART_SCR, 0xa5);
2441 status1 = serial_in(info, UART_SCR);
2442 serial_outp(info, UART_SCR, 0x5a);
2443 status2 = serial_in(info, UART_SCR);
2444 serial_outp(info, UART_SCR, scratch);
2446 if ((status1 != 0xa5) || (status2 != 0x5a))
2447 info->type = PORT_8250;
2449 info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2451 if (info->type == PORT_UNKNOWN) {
2452 restore_flags(flags);
2456 sprintf(info->name, "su(%s)", su_typev[info->port_type]);
2461 serial_outp(info, UART_MCR, 0x00);
2462 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2463 (void)serial_in(info, UART_RX);
2464 serial_outp(info, UART_IER, 0x00);
2466 restore_flags(flags);
2469 /* This is used by the SAB driver to adjust where its minor
2470 * numbers start, we always are probed for first.
2472 int su_num_ports = 0;
2473 EXPORT_SYMBOL(su_num_ports);
2476 * The serial driver boot-time initialization code!
2478 int __init su_serial_init(void)
2481 struct su_struct *info;
2483 init_bh(SERIAL_BH, do_serial_bh);
2486 /* Initialize the tty_driver structure */
2488 memset(&serial_driver, 0, sizeof(struct tty_driver));
2489 serial_driver.magic = TTY_DRIVER_MAGIC;
2490 serial_driver.driver_name = "su";
2491 #ifdef CONFIG_DEVFS_FS
2492 serial_driver.name = "tts/%d";
2494 serial_driver.name = "ttyS";
2496 serial_driver.major = TTY_MAJOR;
2497 serial_driver.minor_start = 64;
2498 serial_driver.num = NR_PORTS;
2499 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2500 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2501 serial_driver.init_termios = tty_std_termios;
2502 serial_driver.init_termios.c_cflag =
2503 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2504 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2505 serial_driver.refcount = &serial_refcount;
2506 serial_driver.table = serial_table;
2507 serial_driver.termios = serial_termios;
2508 serial_driver.termios_locked = serial_termios_locked;
2510 serial_driver.open = su_open;
2511 serial_driver.close = su_close;
2512 serial_driver.write = su_write;
2513 serial_driver.put_char = su_put_char;
2514 serial_driver.flush_chars = su_flush_chars;
2515 serial_driver.write_room = su_write_room;
2516 serial_driver.chars_in_buffer = su_chars_in_buffer;
2517 serial_driver.flush_buffer = su_flush_buffer;
2518 serial_driver.ioctl = su_ioctl;
2519 serial_driver.throttle = su_throttle;
2520 serial_driver.unthrottle = su_unthrottle;
2521 serial_driver.send_xchar = su_send_xchar;
2522 serial_driver.set_termios = su_set_termios;
2523 serial_driver.stop = su_stop;
2524 serial_driver.start = su_start;
2525 serial_driver.hangup = su_hangup;
2526 serial_driver.break_ctl = su_break;
2527 serial_driver.wait_until_sent = su_wait_until_sent;
2528 serial_driver.read_proc = su_read_proc;
2531 * The callout device is just like normal device except for
2532 * major number and the subtype code.
2534 callout_driver = serial_driver;
2535 #ifdef CONFIG_DEVFS_FS
2536 callout_driver.name = "cua/%d";
2538 callout_driver.name = "cua";
2540 callout_driver.major = TTYAUX_MAJOR;
2541 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2542 callout_driver.read_proc = 0;
2543 callout_driver.proc_entry = 0;
2545 if (tty_register_driver(&serial_driver))
2546 panic("Couldn't register regular su\n");
2547 if (tty_register_driver(&callout_driver))
2548 panic("Couldn't register callout su\n");
2550 for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2552 info->type = PORT_UNKNOWN;
2553 info->baud_base = BAUD_BASE;
2554 /* info->flags = 0; */
2555 info->custom_divisor = 0;
2556 info->close_delay = 5*HZ/10;
2557 info->closing_wait = 30*HZ;
2558 info->callout_termios = callout_driver.init_termios;
2559 info->normal_termios = serial_driver.init_termios;
2560 info->icount.cts = info->icount.dsr =
2561 info->icount.rng = info->icount.dcd = 0;
2562 info->icount.rx = info->icount.tx = 0;
2563 info->icount.frame = info->icount.parity = 0;
2564 info->icount.overrun = info->icount.brk = 0;
2565 info->tqueue.routine = do_softint;
2566 info->tqueue.data = info;
2567 info->cflag = serial_driver.init_termios.c_cflag;
2568 init_waitqueue_head(&info->open_wait);
2569 init_waitqueue_head(&info->close_wait);
2570 init_waitqueue_head(&info->delta_msr_wait);
2573 if (info->type == PORT_UNKNOWN)
2576 printk(KERN_INFO "%s at 0x%lx (tty %d irq %s) is a %s\n",
2577 info->name, (long)info->port, i, __irq_itoa(info->irq),
2578 uart_config[info->type].name);
2581 for (i = 0, info = su_table; i < NR_PORTS; i++, info++)
2582 if (info->type == PORT_UNKNOWN)
2586 serial_driver.num = callout_driver.num = i;
2591 int __init su_kbd_ms_init(void)
2594 struct su_struct *info;
2598 for (i = 0, info = su_table; i < 2; i++, info++) {
2600 info->type = PORT_UNKNOWN;
2601 info->baud_base = BAUD_BASE;
2603 if (info->port_type == SU_PORT_KBD)
2604 info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2606 info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2608 init_waitqueue_head(&info->open_wait);
2609 init_waitqueue_head(&info->close_wait);
2610 init_waitqueue_head(&info->delta_msr_wait);
2613 if (info->type == PORT_UNKNOWN)
2616 printk(KERN_INFO "%s at 0x%lx (irq = %s) is a %s\n",
2617 info->name, info->port, __irq_itoa(info->irq),
2618 uart_config[info->type].name);
2621 if (info->port_type == SU_PORT_KBD)
2622 keyboard_zsinit(su_put_char_kbd);
2629 static int su_node_ok(int node, char *name, int namelen)
2631 if (strncmp(name, "su", namelen) == 0 ||
2632 strncmp(name, "su_pnp", namelen) == 0)
2635 if (strncmp(name, "serial", namelen) == 0) {
2639 /* Is it _really_ a 'su' device? */
2640 clen = prom_getproperty(node, "compatible", compat, sizeof(compat));
2642 if (strncmp(compat, "sab82532", 8) == 0) {
2643 /* Nope, Siemens serial, not for us. */
2654 * We got several platforms which present 'su' in different parts
2655 * of device tree. 'su' may be found under obio, ebus, isa and pci.
2656 * We walk over the tree and find them wherever PROM hides them.
2658 void __init su_probe_any(struct su_probe_scan *t, int sunode)
2660 struct su_struct *info;
2663 if (t->devices >= NR_PORTS) return;
2665 for (; sunode != 0; sunode = prom_getsibling(sunode)) {
2666 len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
2667 if (len <= 1) continue; /* Broken PROM node */
2668 if (su_node_ok(sunode, t->prop, len)) {
2669 info = &su_table[t->devices];
2670 if (t->kbnode != 0 && sunode == t->kbnode) {
2671 t->kbx = t->devices;
2672 info->port_type = SU_PORT_KBD;
2673 } else if (t->msnode != 0 && sunode == t->msnode) {
2674 t->msx = t->devices;
2675 info->port_type = SU_PORT_MS;
2679 * Do not attempt to use the truncated
2680 * keyboard/mouse ports as serial ports
2681 * on Ultras with PC keyboard attached.
2683 if (prom_getbool(sunode, "mouse"))
2685 if (prom_getbool(sunode, "keyboard"))
2688 info->port_type = SU_PORT_PORT;
2690 info->is_console = 0;
2691 info->port_node = sunode;
2694 su_probe_any(t, prom_getchild(sunode));
2699 int __init su_probe(void)
2703 struct su_probe_scan scan;
2706 * First, we scan the tree.
2715 * Get the nodes for keyboard and mouse from 'aliases'...
2717 node = prom_getchild(prom_root_node);
2718 node = prom_searchsiblings(node, "aliases");
2721 len = prom_getproperty(node, "keyboard", scan.prop,SU_PROPSIZE);
2724 scan.kbnode = prom_finddevice(scan.prop);
2727 len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
2730 scan.msnode = prom_finddevice(scan.prop);
2734 su_probe_any(&scan, prom_getchild(prom_root_node));
2737 * Second, we process the special case of keyboard and mouse.
2739 * Currently if we got keyboard and mouse hooked to "su" ports
2740 * we do not use any possible remaining "su" as a serial port.
2741 * Thus, we ignore values of .msx and .kbx, then compact ports.
2742 * Those who want to address this issue need to merge
2743 * su_serial_init() and su_ms_kbd_init().
2745 if (scan.msx != -1 && scan.kbx != -1) {
2746 su_table[0].port_type = SU_PORT_MS;
2747 su_table[0].is_console = 0;
2748 su_table[0].port_node = scan.msnode;
2749 su_table[1].port_type = SU_PORT_KBD;
2750 su_table[1].is_console = 0;
2751 su_table[1].port_node = scan.kbnode;
2753 sunserial_setinitfunc(su_kbd_ms_init);
2754 rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2755 sunkbd_setinitfunc(sun_kbd_init);
2756 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2757 kbd_ops.setledstate = sun_setledstate;
2758 kbd_ops.getledstate = sun_getledstate;
2759 kbd_ops.setkeycode = sun_setkeycode;
2760 kbd_ops.getkeycode = sun_getkeycode;
2762 sunkbd_install_keymaps(sun_key_maps,
2763 sun_keymap_count, sun_func_buf, sun_func_table,
2764 sun_funcbufsize, sun_funcbufleft,
2765 sun_accent_table, sun_accent_table_size);
2769 if (scan.msx != -1 || scan.kbx != -1) {
2770 printk("su_probe: cannot match keyboard and mouse, confused\n");
2774 if (scan.devices == 0)
2777 #ifdef CONFIG_SERIAL_CONSOLE
2779 * Console must be initiated after the generic initialization.
2780 * sunserial_setinitfunc inverts order, so call this before next one.
2782 sunserial_setinitfunc(su_serial_console_init);
2784 sunserial_setinitfunc(su_serial_init);
2789 * ------------------------------------------------------------
2790 * Serial console driver
2791 * ------------------------------------------------------------
2793 #ifdef CONFIG_SERIAL_CONSOLE
2795 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2798 * Wait for transmitter & holding register to empty
2800 static __inline__ void
2801 wait_for_xmitr(struct su_struct *info)
2804 unsigned int tmout = 1000000;
2807 lsr = su_inb(info, UART_LSR);
2810 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2814 * Print a string to the serial port trying not to disturb
2815 * any possible real use of the port...
2818 serial_console_write(struct console *co, const char *s,
2821 struct su_struct *info;
2825 info = su_table + co->index;
2827 * First save the IER then disable the interrupts
2829 ier = su_inb(info, UART_IER);
2830 su_outb(info, UART_IER, 0x00);
2833 * Now, do each character
2835 for (i = 0; i < count; i++, s++) {
2836 wait_for_xmitr(info);
2839 * Send the character out.
2840 * If a LF, also do CR...
2842 su_outb(info, UART_TX, *s);
2844 wait_for_xmitr(info);
2845 su_outb(info, UART_TX, 13);
2850 * Finally, Wait for transmitter & holding register to empty
2851 * and restore the IER
2853 wait_for_xmitr(info);
2854 su_outb(info, UART_IER, ier);
2858 serial_console_device(struct console *c)
2860 return MKDEV(TTY_MAJOR, 64 + c->index);
2864 * Setup initial baud/bits/parity. We do two things here:
2865 * - construct a cflag setting for the first su_open()
2866 * - initialize the serial port
2867 * Return non-zero if we didn't find a serial port.
2869 static int __init serial_console_setup(struct console *co, char *options)
2871 struct su_struct *info;
2876 int cflag = CREAD | HUPCL | CLOCAL;
2881 baud = simple_strtoul(options, NULL, 10);
2883 while (*s >= '0' && *s <= '9')
2885 if (*s) parity = *s++;
2886 if (*s) bits = *s - '0';
2890 * Now construct a cflag setting.
2940 * Divisor, bytesize and parity
2942 info = su_table + co->index;
2943 quot = BAUD_BASE / baud;
2944 cval = cflag & (CSIZE | CSTOPB);
2945 #if defined(__powerpc__) || defined(__alpha__)
2947 #else /* !__powerpc__ && !__alpha__ */
2949 #endif /* !__powerpc__ && !__alpha__ */
2951 cval |= UART_LCR_PARITY;
2952 if (!(cflag & PARODD))
2953 cval |= UART_LCR_EPAR;
2956 * Disable UART interrupts, set DTR and RTS high
2959 su_outb(info, UART_IER, 0);
2960 su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2961 su_outb(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
2962 su_outb(info, UART_DLL, quot & 0xff); /* LS of divisor */
2963 su_outb(info, UART_DLM, quot >> 8); /* MS of divisor */
2964 su_outb(info, UART_LCR, cval); /* reset DLAB */
2968 * If we read 0xff from the LSR, there is no UART here.
2970 if (su_inb(info, UART_LSR) == 0xff)
2973 info->is_console = 1;
2978 static struct console sercons = {
2980 write: serial_console_write,
2981 device: serial_console_device,
2982 setup: serial_console_setup,
2983 flags: CON_PRINTBUFFER,
2987 int su_console_registered = 0;
2992 int __init su_serial_console_init(void)
2994 extern int con_is_present(void);
2997 if (con_is_present())
2999 if (serial_console == 0)
3001 index = serial_console - 1;
3002 if (su_table[index].port == 0 || su_table[index].port_node == 0)
3004 sercons.index = index;
3005 register_console(&sercons);
3006 su_console_registered = 1;
3010 #endif /* CONFIG_SERIAL_CONSOLE */