Linux 2.3.1pre3
[davej-history.git] / drivers / sbus / char / su.c
blob5c2d38f72c67378f9576615d0ae8dcc0b75e8d8b
1 /* $Id: su.c,v 1.19 1999/05/12 11:15:14 davem 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 * Coypright (C) 1998 Pete Zaitcev (zaitcev@metabyte.com)
7 * This is mainly a variation of drivers/char/serial.c,
8 * credits go to authors mentioned therein.
9 */
12 * Configuration section.
14 #define SERIAL_PARANOIA_CHECK
15 #define CONFIG_SERIAL_NOPAUSE_IO /* Unused on sparc */
16 #define SERIAL_DO_RESTART
18 /* Set of debugging defines */
20 #undef SERIAL_DEBUG_INTR
21 #undef SERIAL_DEBUG_OPEN
22 #undef SERIAL_DEBUG_FLOW
23 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
24 #undef SERIAL_DEBUG_THROTTLE
26 #define RS_ISR_PASS_LIMIT 256
29 * 0x20 is sun4m thing, Dave Redman heritage.
30 * See arch/sparc/kernel/irq.c.
32 #define IRQ_4M(n) ((n)|0x20)
34 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
35 #define DBG_CNT(s) \
36 do { \
37 printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n", \
38 kdevname(tty->device), (info->flags), serial_refcount, \
39 info->count,tty->count,s); \
40 } while (0)
41 #else
42 #define DBG_CNT(s)
43 #endif
46 * End of serial driver configuration section.
48 #include <linux/config.h>
49 #include <linux/module.h>
50 #include <linux/errno.h>
51 #include <linux/signal.h>
52 #include <linux/sched.h>
53 #include <linux/interrupt.h>
54 #include <linux/tty.h>
55 #include <linux/tty_flip.h>
56 #include <linux/serial.h>
57 #include <linux/serial_reg.h>
58 #include <linux/string.h>
59 #include <linux/fcntl.h>
60 #include <linux/ptrace.h>
61 #include <linux/ioport.h>
62 #include <linux/mm.h>
63 #include <linux/malloc.h>
64 #include <linux/init.h>
65 #include <linux/delay.h>
66 #ifdef CONFIG_SERIAL_CONSOLE
67 #include <linux/console.h>
68 #include <linux/major.h>
69 #endif
70 #include <linux/sysrq.h>
72 #include <asm/system.h>
73 #include <asm/oplib.h>
74 #include <asm/io.h>
75 #include <asm/ebus.h>
76 #include <asm/irq.h>
77 #include <asm/uaccess.h>
78 #include <asm/bitops.h>
80 #include "sunserial.h"
81 #include "sunkbd.h"
82 #include "sunmouse.h"
84 /* We are on a NS PC87303 clocked with 24.0 MHz, which results
85 * in a UART clock of 1.8462 MHz.
87 #define BAUD_BASE (1846200 / 16)
89 #ifdef CONFIG_SERIAL_CONSOLE
90 extern int serial_console;
91 static struct console sercons;
92 int su_serial_console_init(void);
93 #endif
96 * serial.c saves memory when it allocates async_info upon first open.
97 * We have parts of state structure together because we do call startup
98 * for keyboard and mouse.
100 struct su_struct {
101 int magic;
102 unsigned long port;
103 int baud_base;
104 int type; /* Hardware type: e.g. 16550 */
105 int irq;
106 int flags;
107 int line;
108 int cflag;
110 int kbd_node;
111 int ms_node;
112 int port_node;
114 char name[16];
116 int xmit_fifo_size;
117 int custom_divisor;
118 unsigned short close_delay;
119 unsigned short closing_wait; /* time to wait before closing */
121 struct tty_struct *tty;
122 int read_status_mask;
123 int ignore_status_mask;
124 int timeout;
125 int quot;
126 int x_char; /* xon/xoff character */
127 int IER; /* Interrupt Enable Register */
128 int MCR; /* Modem control register */
129 unsigned long event;
130 int blocked_open; /* # of blocked opens */
131 long session; /* Session of opening process */
132 long pgrp; /* pgrp of opening process */
133 unsigned char *xmit_buf;
134 int xmit_head;
135 int xmit_tail;
136 int xmit_cnt;
137 struct tq_struct tqueue;
138 wait_queue_head_t open_wait;
139 wait_queue_head_t close_wait;
140 wait_queue_head_t delta_msr_wait;
142 int count;
143 struct async_icount icount;
144 struct termios normal_termios, callout_termios;
145 unsigned long last_active; /* For async_struct, to be */
148 static char *serial_name = "PCIO serial driver";
149 static char serial_version[16];
151 static DECLARE_TASK_QUEUE(tq_serial);
153 static struct tty_driver serial_driver, callout_driver;
154 static int serial_refcount;
156 /* number of characters left in xmit buffer before we ask for more */
157 #define WAKEUP_CHARS 256
159 static void autoconfig(struct su_struct *info);
160 static void change_speed(struct su_struct *info, struct termios *old);
161 static void su_wait_until_sent(struct tty_struct *tty, int timeout);
164 * Here we define the default xmit fifo size used for each type of
165 * UART
167 static struct serial_uart_config uart_config[] = {
168 { "unknown", 1, 0 },
169 { "8250", 1, 0 },
170 { "16450", 1, 0 },
171 { "16550", 1, 0 },
172 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
173 { "cirrus", 1, 0 },
174 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
175 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
176 UART_STARTECH },
177 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
178 { 0, 0}
182 #define NR_PORTS 4
184 static struct su_struct su_table[NR_PORTS];
185 static struct tty_struct *serial_table[NR_PORTS];
186 static struct termios *serial_termios[NR_PORTS];
187 static struct termios *serial_termios_locked[NR_PORTS];
189 #ifndef MIN
190 #define MIN(a,b) ((a) < (b) ? (a) : (b))
191 #endif
194 * tmp_buf is used as a temporary buffer by serial_write. We need to
195 * lock it in case the copy_from_user blocks while swapping in a page,
196 * and some other program tries to do a serial write at the same time.
197 * Since the lock will only come under contention when the system is
198 * swapping and available memory is low, it makes sense to share one
199 * buffer across all the serial ports, since it significantly saves
200 * memory if large numbers of serial ports are open.
202 static unsigned char *tmp_buf;
203 static DECLARE_MUTEX(tmp_buf_sem);
205 static inline int serial_paranoia_check(struct su_struct *info,
206 kdev_t device, const char *routine)
208 #ifdef SERIAL_PARANOIA_CHECK
209 static const char *badmagic =
210 "Warning: bad magic number for serial struct (%s) in %s\n";
211 static const char *badinfo =
212 "Warning: null su_struct for (%s) in %s\n";
214 if (!info) {
215 printk(badinfo, kdevname(device), routine);
216 return 1;
218 if (info->magic != SERIAL_MAGIC) {
219 printk(badmagic, kdevname(device), routine);
220 return 1;
222 #endif
223 return 0;
226 #ifdef __sparc_v9__
228 static inline
229 unsigned int su_inb(struct su_struct *info, unsigned long offset)
231 return inb(info->port + offset);
234 static inline void
235 su_outb(struct su_struct *info, unsigned long offset, int value)
237 outb(value, info->port + offset);
240 #else
242 static inline
243 unsigned int su_inb(struct su_struct *info, unsigned long offset)
245 return (unsigned int)(*(volatile unsigned char *)(info->port + offset));
248 static inline void
249 su_outb(struct su_struct *info, unsigned long offset, int value)
252 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
253 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
254 * gate outputs a logical one. Since we use level triggered interrupts
255 * we have lockup and watchdog reset. We cannot mask IRQ because
256 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
257 * This problem is similar to what Alpha people suffer, see serial.c.
259 if (offset == UART_MCR) value |= UART_MCR_OUT2;
260 *(volatile unsigned char *)(info->port + offset) = value;
263 #endif
265 #define serial_in(info, off) su_inb(info, off)
266 #define serial_inp(info, off) su_inb(info, off)
267 #define serial_out(info, off, val) su_outb(info, off, val)
268 #define serial_outp(info, off, val) su_outb(info, off, val)
271 * ------------------------------------------------------------
272 * su_stop() and su_start()
274 * This routines are called before setting or resetting tty->stopped.
275 * They enable or disable transmitter interrupts, as necessary.
276 * ------------------------------------------------------------
278 static void su_stop(struct tty_struct *tty)
280 struct su_struct *info = (struct su_struct *)tty->driver_data;
281 unsigned long flags;
283 if (serial_paranoia_check(info, tty->device, "su_stop"))
284 return;
286 save_flags(flags); cli();
287 if (info->IER & UART_IER_THRI) {
288 info->IER &= ~UART_IER_THRI;
289 serial_out(info, UART_IER, info->IER);
291 restore_flags(flags);
294 static void su_start(struct tty_struct *tty)
296 struct su_struct *info = (struct su_struct *)tty->driver_data;
297 unsigned long flags;
299 if (serial_paranoia_check(info, tty->device, "su_start"))
300 return;
302 save_flags(flags); cli();
303 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
304 info->IER |= UART_IER_THRI;
305 serial_out(info, UART_IER, info->IER);
307 restore_flags(flags);
311 * ----------------------------------------------------------------------
313 * Here starts the interrupt handling routines. All of the following
314 * subroutines are declared as inline and are folded into
315 * su_interrupt(). They were separated out for readability's sake.
317 * Note: rs_interrupt() is a "fast" interrupt, which means that it
318 * runs with interrupts turned off. People who may want to modify
319 * rs_interrupt() should try to keep the interrupt handler as fast as
320 * possible. After you are done making modifications, it is not a bad
321 * idea to do:
323 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
325 * and look at the resulting assemble code in serial.s.
327 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
328 * -----------------------------------------------------------------------
332 * This routine is used by the interrupt handler to schedule
333 * processing in the software interrupt portion of the driver.
335 static __inline__ void
336 su_sched_event(struct su_struct *info, int event)
338 info->event |= 1 << event;
339 queue_task(&info->tqueue, &tq_serial);
340 mark_bh(SERIAL_BH);
343 static __inline__ void
344 receive_kbd_ms_chars(struct su_struct *info, struct pt_regs *regs)
346 unsigned char status = 0;
347 unsigned char ch;
349 do {
350 ch = serial_inp(info, UART_RX);
351 if (info->kbd_node) {
352 if(ch == SUNKBD_RESET) {
353 l1a_state.kbd_id = 1;
354 l1a_state.l1_down = 0;
355 } else if(l1a_state.kbd_id) {
356 l1a_state.kbd_id = 0;
357 } else if(ch == SUNKBD_L1) {
358 l1a_state.l1_down = 1;
359 } else if(ch == (SUNKBD_L1|SUNKBD_UP)) {
360 l1a_state.l1_down = 0;
361 } else if(ch == SUNKBD_A && l1a_state.l1_down) {
362 /* whee... */
363 batten_down_hatches();
364 /* Continue execution... */
365 l1a_state.l1_down = 0;
366 l1a_state.kbd_id = 0;
367 return;
369 sunkbd_inchar(ch, regs);
370 } else {
371 sun_mouse_inbyte(ch);
374 status = su_inb(info, UART_LSR);
375 } while (status & UART_LSR_DR);
378 static __inline__ void
379 receive_serial_chars(struct su_struct *info, int *status, struct pt_regs *regs)
381 struct tty_struct *tty = info->tty;
382 unsigned char ch;
383 int ignored = 0;
384 struct async_icount *icount;
386 icount = &info->icount;
387 do {
388 ch = serial_inp(info, UART_RX);
389 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
390 break;
391 *tty->flip.char_buf_ptr = ch;
392 icount->rx++;
394 #ifdef SERIAL_DEBUG_INTR
395 printk("D%02x:%02x.", ch, *status);
396 #endif
397 *tty->flip.flag_buf_ptr = 0;
398 if (*status & (UART_LSR_BI | UART_LSR_PE |
399 UART_LSR_FE | UART_LSR_OE)) {
401 * For statistics only
403 if (*status & UART_LSR_BI) {
404 *status &= ~(UART_LSR_FE | UART_LSR_PE);
405 icount->brk++;
406 } else if (*status & UART_LSR_PE)
407 icount->parity++;
408 else if (*status & UART_LSR_FE)
409 icount->frame++;
410 if (*status & UART_LSR_OE)
411 icount->overrun++;
414 * Now check to see if character should be
415 * ignored, and mask off conditions which
416 * should be ignored.
418 if (*status & info->ignore_status_mask) {
419 if (++ignored > 100) {
420 #ifdef SERIAL_DEBUG_INTR
421 printk("ign100..");
422 #endif
423 break;
425 goto ignore_char;
427 *status &= info->read_status_mask;
429 if (*status & (UART_LSR_BI)) {
430 #ifdef SERIAL_DEBUG_INTR
431 printk("handling break....");
432 #endif
433 *tty->flip.flag_buf_ptr = TTY_BREAK;
434 if (info->flags & ASYNC_SAK)
435 do_SAK(tty);
436 } else if (*status & UART_LSR_PE)
437 *tty->flip.flag_buf_ptr = TTY_PARITY;
438 else if (*status & UART_LSR_FE)
439 *tty->flip.flag_buf_ptr = TTY_FRAME;
440 if (*status & UART_LSR_OE) {
442 * Overrun is special, since it's
443 * reported immediately, and doesn't
444 * affect the current character
446 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
447 tty->flip.count++;
448 tty->flip.flag_buf_ptr++;
449 tty->flip.char_buf_ptr++;
450 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
454 tty->flip.flag_buf_ptr++;
455 tty->flip.char_buf_ptr++;
456 tty->flip.count++;
457 ignore_char:
458 *status = serial_inp(info, UART_LSR);
459 } while (*status & UART_LSR_DR);
460 #ifdef SERIAL_DEBUG_INTR
461 printk("E%02x.R%d", *status, tty->flip.count);
462 #endif
463 tty_flip_buffer_push(tty);
466 static __inline__ void
467 transmit_chars(struct su_struct *info, int *intr_done)
469 int count;
471 if (info->x_char) {
472 serial_outp(info, UART_TX, info->x_char);
473 info->icount.tx++;
474 info->x_char = 0;
475 if (intr_done)
476 *intr_done = 0;
477 return;
479 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
480 info->tty->hw_stopped) {
481 info->IER &= ~UART_IER_THRI;
482 serial_out(info, UART_IER, info->IER);
483 return;
486 count = info->xmit_fifo_size;
487 do {
488 serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
489 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
490 info->icount.tx++;
491 if (--info->xmit_cnt <= 0)
492 break;
493 } while (--count > 0);
495 if (info->xmit_cnt < WAKEUP_CHARS)
496 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
498 #ifdef SERIAL_DEBUG_INTR
499 printk("T%d...", info->xmit_cnt);
500 #endif
501 if (intr_done)
502 *intr_done = 0;
504 if (info->xmit_cnt <= 0) {
505 info->IER &= ~UART_IER_THRI;
506 serial_out(info, UART_IER, info->IER);
510 static __inline__ void
511 check_modem_status(struct su_struct *info)
513 int status;
514 struct async_icount *icount;
516 status = serial_in(info, UART_MSR);
518 if (status & UART_MSR_ANY_DELTA) {
519 icount = &info->icount;
520 /* update input line counters */
521 if (status & UART_MSR_TERI)
522 icount->rng++;
523 if (status & UART_MSR_DDSR)
524 icount->dsr++;
525 if (status & UART_MSR_DDCD) {
526 icount->dcd++;
527 #ifdef CONFIG_HARD_PPS
528 if ((info->flags & ASYNC_HARDPPS_CD) &&
529 (status & UART_MSR_DCD))
530 hardpps();
531 #endif
533 if (status & UART_MSR_DCTS)
534 icount->cts++;
535 wake_up_interruptible(&info->delta_msr_wait);
538 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
539 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
540 printk("ttys%d CD now %s...", info->line,
541 (status & UART_MSR_DCD) ? "on" : "off");
542 #endif
543 if (status & UART_MSR_DCD)
544 wake_up_interruptible(&info->open_wait);
545 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
546 (info->flags & ASYNC_CALLOUT_NOHUP))) {
547 #ifdef SERIAL_DEBUG_OPEN
548 printk("doing serial hangup...");
549 #endif
550 if (info->tty)
551 tty_hangup(info->tty);
554 if (info->flags & ASYNC_CTS_FLOW) {
555 if (info->tty->hw_stopped) {
556 if (status & UART_MSR_CTS) {
557 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
558 printk("CTS tx start...");
559 #endif
560 info->tty->hw_stopped = 0;
561 info->IER |= UART_IER_THRI;
562 serial_out(info, UART_IER, info->IER);
563 su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
564 return;
566 } else {
567 if (!(status & UART_MSR_CTS)) {
568 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
569 printk("CTS tx stop...");
570 #endif
571 info->tty->hw_stopped = 1;
572 info->IER &= ~UART_IER_THRI;
573 serial_out(info, UART_IER, info->IER);
580 * This is the kbd/mouse serial driver's interrupt routine
582 static void
583 su_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs * regs)
585 struct su_struct *info = (struct su_struct *)dev_id;
586 unsigned char status;
588 #ifdef SERIAL_DEBUG_INTR
589 printk("su_kbd_ms_interrupt(%s)...", __irq_itoa(irq));
590 #endif
591 if (!info)
592 return;
594 if (serial_in(info, UART_IIR) & UART_IIR_NO_INT)
595 return;
597 status = serial_inp(info, UART_LSR);
598 #ifdef SERIAL_DEBUG_INTR
599 printk("status = %x...", status);
600 #endif
601 if (status & UART_LSR_DR)
602 receive_kbd_ms_chars(info, regs);
604 #ifdef SERIAL_DEBUG_INTR
605 printk("end.\n");
606 #endif
610 * This is the serial driver's generic interrupt routine
612 static void
613 su_serial_interrupt(int irq, void *dev_id, struct pt_regs * regs)
615 int status;
616 struct su_struct *info;
617 int pass_counter = 0;
619 #ifdef SERIAL_DEBUG_INTR
620 printk("su_serial_interrupt(%s)...", __irq_itoa(irq));
621 #endif
622 info = (struct su_struct *)dev_id;
623 if (!info || !info->tty) {
624 #ifdef SERIAL_DEBUG_INTR
625 printk("strain\n");
626 #endif
627 return;
630 do {
631 status = serial_inp(info, UART_LSR);
632 #ifdef SERIAL_DEBUG_INTR
633 printk("status = %x...", status);
634 #endif
635 if (status & UART_LSR_DR)
636 receive_serial_chars(info, &status, regs);
637 check_modem_status(info);
638 if (status & UART_LSR_THRE)
639 transmit_chars(info, 0);
641 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
642 #ifdef SERIAL_DEBUG_INTR
643 printk("rs loop break");
644 #endif
645 break; /* Prevent infinite loops */
647 } while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
649 info->last_active = jiffies;
651 #ifdef SERIAL_DEBUG_INTR
652 printk("end.\n");
653 #endif
657 * -------------------------------------------------------------------
658 * Here ends the serial interrupt routines.
659 * -------------------------------------------------------------------
663 * This routine is used to handle the "bottom half" processing for the
664 * serial driver, known also the "software interrupt" processing.
665 * This processing is done at the kernel interrupt level, after the
666 * su_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
667 * is where time-consuming activities which can not be done in the
668 * interrupt driver proper are done; the interrupt driver schedules
669 * them using su_sched_event(), and they get done here.
671 static void do_serial_bh(void)
673 run_task_queue(&tq_serial);
676 static void do_softint(void *private_)
678 struct su_struct *info = (struct su_struct *) private_;
679 struct tty_struct *tty;
681 tty = info->tty;
682 if (!tty)
683 return;
685 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
686 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
687 tty->ldisc.write_wakeup)
688 (tty->ldisc.write_wakeup)(tty);
689 wake_up_interruptible(&tty->write_wait);
694 * ---------------------------------------------------------------
695 * Low level utility subroutines for the serial driver: routines to
696 * figure out the appropriate timeout for an interrupt chain, routines
697 * to initialize and startup a serial port, and routines to shutdown a
698 * serial port. Useful stuff like that.
699 * ---------------------------------------------------------------
702 static int
703 startup(struct su_struct *info)
705 unsigned long flags;
706 int retval=0;
707 unsigned long page;
709 save_flags(flags);
710 if (info->tty) {
711 page = get_free_page(GFP_KERNEL);
712 if (!page)
713 return -ENOMEM;
715 cli();
717 if (info->flags & ASYNC_INITIALIZED) {
718 free_page(page);
719 goto errout;
722 if (info->port == 0 || info->type == PORT_UNKNOWN) {
723 set_bit(TTY_IO_ERROR, &info->tty->flags);
724 free_page(page);
725 goto errout;
727 if (info->xmit_buf)
728 free_page(page);
729 else
730 info->xmit_buf = (unsigned char *) page;
732 cli();
734 #ifdef SERIAL_DEBUG_OPEN
735 printk("starting up ttys%d (irq %s)...", info->line,
736 __irq_itoa(info->irq));
737 #endif
739 if (uart_config[info->type].flags & UART_STARTECH) {
740 /* Wake up UART */
741 serial_outp(info, UART_LCR, 0xBF);
742 serial_outp(info, UART_EFR, UART_EFR_ECB);
743 serial_outp(info, UART_IER, 0);
744 serial_outp(info, UART_EFR, 0);
745 serial_outp(info, UART_LCR, 0);
748 if (info->type == PORT_16750) {
749 /* Wake up UART */
750 serial_outp(info, UART_IER, 0);
754 * Clear the FIFO buffers and disable them
755 * (they will be reenabled in change_speed())
757 if (uart_config[info->type].flags & UART_CLEAR_FIFO)
758 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
759 UART_FCR_CLEAR_XMIT));
762 * At this point there's no way the LSR could still be 0xFF;
763 * if it is, then bail out, because there's likely no UART
764 * here.
766 if (serial_inp(info, UART_LSR) == 0xff) {
767 if (capable(CAP_SYS_ADMIN)) {
768 if (info->tty)
769 set_bit(TTY_IO_ERROR, &info->tty->flags);
770 } else
771 retval = -ENODEV;
772 goto errout;
776 * Allocate the IRQ if necessary
778 if (info->kbd_node || info->ms_node) {
779 retval = request_irq(info->irq, su_kbd_ms_interrupt,
780 SA_SHIRQ, info->name, info);
781 } else {
782 retval = request_irq(info->irq, su_serial_interrupt,
783 SA_SHIRQ, info->name, info);
785 if (retval) {
786 if (capable(CAP_SYS_ADMIN)) {
787 if (info->tty)
788 set_bit(TTY_IO_ERROR, &info->tty->flags);
789 retval = 0;
791 goto errout;
795 * Clear the interrupt registers.
797 (void) serial_inp(info, UART_RX);
798 (void) serial_inp(info, UART_IIR);
799 (void) serial_inp(info, UART_MSR);
802 * Now, initialize the UART
804 serial_outp(info, UART_LCR, UART_LCR_WLEN8); /* reset DLAB */
806 info->MCR = 0;
807 if (info->tty && info->tty->termios->c_cflag & CBAUD)
808 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
809 if (info->irq != 0)
810 info->MCR |= UART_MCR_OUT2;
811 serial_outp(info, UART_MCR, info->MCR);
814 * Finally, enable interrupts
816 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
817 serial_outp(info, UART_IER, info->IER); /* enable interrupts */
820 * And clear the interrupt registers again for luck.
822 (void)serial_inp(info, UART_LSR);
823 (void)serial_inp(info, UART_RX);
824 (void)serial_inp(info, UART_IIR);
825 (void)serial_inp(info, UART_MSR);
827 if (info->tty)
828 clear_bit(TTY_IO_ERROR, &info->tty->flags);
829 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
832 * Set up the tty->alt_speed kludge
834 if (info->tty) {
835 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
836 info->tty->alt_speed = 57600;
837 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
838 info->tty->alt_speed = 115200;
839 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
840 info->tty->alt_speed = 230400;
841 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
842 info->tty->alt_speed = 460800;
846 * and set the speed of the serial port
848 change_speed(info, 0);
850 info->flags |= ASYNC_INITIALIZED;
851 restore_flags(flags);
852 return 0;
854 errout:
855 restore_flags(flags);
856 return retval;
860 * This routine will shutdown a serial port; interrupts are disabled, and
861 * DTR is dropped if the hangup on close termio flag is on.
863 static void
864 shutdown(struct su_struct *info)
866 unsigned long flags;
868 if (!(info->flags & ASYNC_INITIALIZED))
869 return;
871 save_flags(flags); cli(); /* Disable interrupts */
874 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
875 * here so the queue might never be waken up
877 wake_up_interruptible(&info->delta_msr_wait);
880 * Free the IRQ, if necessary
882 free_irq(info->irq, info);
884 if (info->xmit_buf) {
885 free_page((unsigned long) info->xmit_buf);
886 info->xmit_buf = 0;
889 info->IER = 0;
890 serial_outp(info, UART_IER, 0x00); /* disable all intrs */
891 info->MCR &= ~UART_MCR_OUT2;
893 /* disable break condition */
894 serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
896 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
897 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
898 serial_outp(info, UART_MCR, info->MCR);
900 /* disable FIFO's */
901 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
902 UART_FCR_CLEAR_XMIT));
903 (void)serial_in(info, UART_RX); /* read data port to reset things */
905 if (info->tty)
906 set_bit(TTY_IO_ERROR, &info->tty->flags);
908 if (uart_config[info->type].flags & UART_STARTECH) {
909 /* Arrange to enter sleep mode */
910 serial_outp(info, UART_LCR, 0xBF);
911 serial_outp(info, UART_EFR, UART_EFR_ECB);
912 serial_outp(info, UART_IER, UART_IERX_SLEEP);
913 serial_outp(info, UART_LCR, 0);
915 if (info->type == PORT_16750) {
916 /* Arrange to enter sleep mode */
917 serial_outp(info, UART_IER, UART_IERX_SLEEP);
919 info->flags &= ~ASYNC_INITIALIZED;
920 restore_flags(flags);
923 static __inline__ int
924 su_get_baud_rate(struct su_struct *info)
926 static int baud_table[] = {
927 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
928 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
930 int i;
932 if (info->tty)
933 return tty_get_baud_rate(info->tty);
935 i = info->cflag & CBAUD;
936 if (i & CBAUDEX) {
937 i &= ~(CBAUDEX);
938 if (i < 1 || i > 4)
939 info->cflag &= ~(CBAUDEX);
940 else
941 i += 15;
943 return baud_table[i];
947 * This routine is called to set the UART divisor registers to match
948 * the specified baud rate for a serial port.
950 static void
951 change_speed(struct su_struct *info,
952 struct termios *old_termios)
954 int quot = 0, baud;
955 unsigned int cval, fcr = 0;
956 int bits;
957 unsigned long flags;
959 if (!info->kbd_node && !info->ms_node) {
960 if (!info->tty || !info->tty->termios)
961 return;
962 if (!info->port)
963 return;
964 info->cflag = info->tty->termios->c_cflag;
967 /* byte size and parity */
968 switch (info->cflag & CSIZE) {
969 case CS5: cval = 0x00; bits = 7; break;
970 case CS6: cval = 0x01; bits = 8; break;
971 case CS7: cval = 0x02; bits = 9; break;
972 case CS8: cval = 0x03; bits = 10; break;
973 /* Never happens, but GCC is too dumb to figure it out */
974 default: cval = 0x00; bits = 7; break;
976 if (info->cflag & CSTOPB) {
977 cval |= 0x04;
978 bits++;
980 if (info->cflag & PARENB) {
981 cval |= UART_LCR_PARITY;
982 bits++;
984 if (!(info->cflag & PARODD))
985 cval |= UART_LCR_EPAR;
986 #ifdef CMSPAR
987 if (info->cflag & CMSPAR)
988 cval |= UART_LCR_SPAR;
989 #endif
991 /* Determine divisor based on baud rate */
992 baud = su_get_baud_rate(info);
993 if (baud == 38400 &&
994 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
995 quot = info->custom_divisor;
996 else {
997 if (baud == 134)
998 /* Special case since 134 is really 134.5 */
999 quot = (2 * info->baud_base / 269);
1000 else if (baud)
1001 quot = info->baud_base / baud;
1003 /* If the quotient is zero refuse the change */
1004 if (!quot && old_termios) {
1005 info->tty->termios->c_cflag &= ~CBAUD;
1006 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1007 baud = tty_get_baud_rate(info->tty);
1008 if (!baud)
1009 baud = 9600;
1010 if (baud == 38400 &&
1011 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1012 quot = info->custom_divisor;
1013 else {
1014 if (baud == 134)
1015 /* Special case since 134 is really 134.5 */
1016 quot = (2*info->baud_base / 269);
1017 else if (baud)
1018 quot = info->baud_base / baud;
1021 /* As a last resort, if the quotient is zero, default to 9600 bps */
1022 if (!quot)
1023 quot = info->baud_base / 9600;
1024 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / info->baud_base);
1025 info->timeout += HZ/50; /* Add .02 seconds of slop */
1027 /* Set up FIFO's */
1028 if (uart_config[info->type].flags & UART_USE_FIFO) {
1029 if ((info->baud_base / quot) < 9600)
1030 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1031 else
1032 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1034 if (info->type == PORT_16750)
1035 fcr |= UART_FCR7_64BYTE;
1037 /* CTS flow control flag and modem status interrupts */
1038 info->IER &= ~UART_IER_MSI;
1039 if (info->flags & ASYNC_HARDPPS_CD)
1040 info->IER |= UART_IER_MSI;
1041 if (info->cflag & CRTSCTS) {
1042 info->flags |= ASYNC_CTS_FLOW;
1043 info->IER |= UART_IER_MSI;
1044 } else
1045 info->flags &= ~ASYNC_CTS_FLOW;
1046 if (info->cflag & CLOCAL)
1047 info->flags &= ~ASYNC_CHECK_CD;
1048 else {
1049 info->flags |= ASYNC_CHECK_CD;
1050 info->IER |= UART_IER_MSI;
1052 serial_out(info, UART_IER, info->IER);
1055 * Set up parity check flag
1057 if (info->tty) {
1058 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1060 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE |
1061 UART_LSR_DR;
1062 if (I_INPCK(info->tty))
1063 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1064 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1065 info->read_status_mask |= UART_LSR_BI;
1068 * Characters to ignore
1070 info->ignore_status_mask = 0;
1071 if (I_IGNPAR(info->tty))
1072 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1073 if (I_IGNBRK(info->tty)) {
1074 info->ignore_status_mask |= UART_LSR_BI;
1076 * If we're ignore parity and break indicators, ignore
1077 * overruns too. (For real raw support).
1079 if (I_IGNPAR(info->tty))
1080 info->ignore_status_mask |= UART_LSR_OE;
1083 * !!! ignore all characters if CREAD is not set
1085 if ((info->cflag & CREAD) == 0)
1086 info->ignore_status_mask |= UART_LSR_DR;
1089 save_flags(flags); cli();
1090 if (uart_config[info->type].flags & UART_STARTECH) {
1091 serial_outp(info, UART_LCR, 0xBF);
1092 serial_outp(info, UART_EFR,
1093 (info->cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1095 serial_outp(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
1096 serial_outp(info, UART_DLL, quot & 0xff); /* LS of divisor */
1097 serial_outp(info, UART_DLM, quot >> 8); /* MS of divisor */
1098 if (info->type == PORT_16750)
1099 serial_outp(info, UART_FCR, fcr); /* set fcr */
1100 serial_outp(info, UART_LCR, cval); /* reset DLAB */
1101 if (info->type != PORT_16750)
1102 serial_outp(info, UART_FCR, fcr); /* set fcr */
1103 restore_flags(flags);
1104 info->quot = quot;
1107 static void
1108 su_put_char(struct tty_struct *tty, unsigned char ch)
1110 struct su_struct *info = (struct su_struct *)tty->driver_data;
1111 unsigned long flags;
1113 if (serial_paranoia_check(info, tty->device, "su_put_char"))
1114 return;
1116 if (!tty || !info->xmit_buf)
1117 return;
1119 save_flags(flags); cli();
1120 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1121 restore_flags(flags);
1122 return;
1125 info->xmit_buf[info->xmit_head++] = ch;
1126 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1127 info->xmit_cnt++;
1128 restore_flags(flags);
1131 static void su_put_char_kbd(unsigned char c)
1133 struct su_struct *info = su_table;
1134 int lsr;
1136 if (!info->kbd_node)
1137 ++info;
1138 if (!info)
1139 return;
1141 do {
1142 lsr = serial_in(info, UART_LSR);
1143 } while (!(lsr & UART_LSR_THRE));
1145 /* Send the character out. */
1146 su_outb(info, UART_TX, c);
1149 static void
1150 su_change_mouse_baud(int baud)
1152 struct su_struct *info = su_table;
1154 if (!info->ms_node)
1155 ++info;
1156 if (!info)
1157 return;
1159 info->cflag &= ~(CBAUDEX | CBAUD);
1160 switch(baud) {
1161 case 1200:
1162 info->cflag |= B1200;
1163 break;
1164 case 2400:
1165 info->cflag |= B2400;
1166 break;
1167 case 4800:
1168 info->cflag |= B4800;
1169 break;
1170 case 9600:
1171 info->cflag |= B9600;
1172 break;
1173 default:
1174 printk("su_change_mouse_baud: unknown baud rate %d, "
1175 "defaulting to 1200\n", baud);
1176 info->cflag |= 1200;
1177 break;
1179 change_speed(info, 0);
1182 static void
1183 su_flush_chars(struct tty_struct *tty)
1185 struct su_struct *info = (struct su_struct *)tty->driver_data;
1186 unsigned long flags;
1188 if (serial_paranoia_check(info, tty->device, "su_flush_chars"))
1189 return;
1191 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1192 !info->xmit_buf)
1193 return;
1195 save_flags(flags); cli();
1196 info->IER |= UART_IER_THRI;
1197 serial_out(info, UART_IER, info->IER);
1198 restore_flags(flags);
1201 static int
1202 su_write(struct tty_struct * tty, int from_user,
1203 const unsigned char *buf, int count)
1205 int c, ret = 0;
1206 struct su_struct *info = (struct su_struct *)tty->driver_data;
1207 unsigned long flags;
1209 if (serial_paranoia_check(info, tty->device, "su_write"))
1210 return 0;
1212 if (!tty || !info->xmit_buf || !tmp_buf)
1213 return 0;
1215 save_flags(flags);
1216 if (from_user) {
1217 down(&tmp_buf_sem);
1218 while (1) {
1219 c = MIN(count,
1220 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1221 SERIAL_XMIT_SIZE - info->xmit_head));
1222 if (c <= 0)
1223 break;
1225 c -= copy_from_user(tmp_buf, buf, c);
1226 if (!c) {
1227 if (!ret)
1228 ret = -EFAULT;
1229 break;
1231 cli();
1232 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1233 SERIAL_XMIT_SIZE - info->xmit_head));
1234 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1235 info->xmit_head = ((info->xmit_head + c) &
1236 (SERIAL_XMIT_SIZE-1));
1237 info->xmit_cnt += c;
1238 restore_flags(flags);
1239 buf += c;
1240 count -= c;
1241 ret += c;
1243 up(&tmp_buf_sem);
1244 } else {
1245 while (1) {
1246 cli();
1247 c = MIN(count,
1248 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1249 SERIAL_XMIT_SIZE - info->xmit_head));
1250 if (c <= 0) {
1251 restore_flags(flags);
1252 break;
1254 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1255 info->xmit_head = ((info->xmit_head + c) &
1256 (SERIAL_XMIT_SIZE-1));
1257 info->xmit_cnt += c;
1258 restore_flags(flags);
1259 buf += c;
1260 count -= c;
1261 ret += c;
1264 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1265 !(info->IER & UART_IER_THRI)) {
1266 info->IER |= UART_IER_THRI;
1267 serial_out(info, UART_IER, info->IER);
1269 return ret;
1272 static int
1273 su_write_room(struct tty_struct *tty)
1275 struct su_struct *info = (struct su_struct *)tty->driver_data;
1276 int ret;
1278 if (serial_paranoia_check(info, tty->device, "su_write_room"))
1279 return 0;
1280 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1281 if (ret < 0)
1282 ret = 0;
1283 return ret;
1286 static int
1287 su_chars_in_buffer(struct tty_struct *tty)
1289 struct su_struct *info = (struct su_struct *)tty->driver_data;
1291 if (serial_paranoia_check(info, tty->device, "su_chars_in_buffer"))
1292 return 0;
1293 return info->xmit_cnt;
1296 static void
1297 su_flush_buffer(struct tty_struct *tty)
1299 struct su_struct *info = (struct su_struct *)tty->driver_data;
1300 unsigned long flags;
1302 if (serial_paranoia_check(info, tty->device, "su_flush_buffer"))
1303 return;
1304 save_flags(flags); cli();
1305 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1306 restore_flags(flags);
1307 wake_up_interruptible(&tty->write_wait);
1308 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1309 tty->ldisc.write_wakeup)
1310 (tty->ldisc.write_wakeup)(tty);
1314 * This function is used to send a high-priority XON/XOFF character to
1315 * the device
1317 static void
1318 su_send_xchar(struct tty_struct *tty, char ch)
1320 struct su_struct *info = (struct su_struct *)tty->driver_data;
1322 if (serial_paranoia_check(info, tty->device, "su_send_char"))
1323 return;
1325 if (!(info->flags & ASYNC_INITIALIZED))
1326 return;
1328 info->x_char = ch;
1329 if (ch) {
1330 /* Make sure transmit interrupts are on */
1331 info->IER |= UART_IER_THRI;
1332 serial_out(info, UART_IER, info->IER);
1337 * ------------------------------------------------------------
1338 * su_throttle()
1340 * This routine is called by the upper-layer tty layer to signal that
1341 * incoming characters should be throttled.
1342 * ------------------------------------------------------------
1344 static void
1345 su_throttle(struct tty_struct * tty)
1347 struct su_struct *info = (struct su_struct *)tty->driver_data;
1348 unsigned long flags;
1349 #ifdef SERIAL_DEBUG_THROTTLE
1350 char buf[64];
1352 printk("throttle %s: %d....\n", tty_name(tty, buf),
1353 tty->ldisc.chars_in_buffer(tty));
1354 #endif
1356 if (serial_paranoia_check(info, tty->device, "su_throttle"))
1357 return;
1359 if (I_IXOFF(tty))
1360 su_send_xchar(tty, STOP_CHAR(tty));
1362 if (tty->termios->c_cflag & CRTSCTS)
1363 info->MCR &= ~UART_MCR_RTS;
1365 save_flags(flags); cli();
1366 serial_out(info, UART_MCR, info->MCR);
1367 restore_flags(flags);
1370 static void
1371 su_unthrottle(struct tty_struct * tty)
1373 struct su_struct *info = (struct su_struct *)tty->driver_data;
1374 unsigned long flags;
1375 #ifdef SERIAL_DEBUG_THROTTLE
1376 char buf[64];
1378 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1379 tty->ldisc.chars_in_buffer(tty));
1380 #endif
1382 if (serial_paranoia_check(info, tty->device, "su_unthrottle"))
1383 return;
1385 if (I_IXOFF(tty)) {
1386 if (info->x_char)
1387 info->x_char = 0;
1388 else
1389 su_send_xchar(tty, START_CHAR(tty));
1391 if (tty->termios->c_cflag & CRTSCTS)
1392 info->MCR |= UART_MCR_RTS;
1393 save_flags(flags); cli();
1394 serial_out(info, UART_MCR, info->MCR);
1395 restore_flags(flags);
1399 * ------------------------------------------------------------
1400 * su_ioctl() and friends
1401 * ------------------------------------------------------------
1405 * get_lsr_info - get line status register info
1407 * Purpose: Let user call ioctl() to get info when the UART physically
1408 * is emptied. On bus types like RS485, the transmitter must
1409 * release the bus after transmitting. This must be done when
1410 * the transmit shift register is empty, not be done when the
1411 * transmit holding register is empty. This functionality
1412 * allows an RS485 driver to be written in user space.
1414 static int
1415 get_lsr_info(struct su_struct * info, unsigned int *value)
1417 unsigned char status;
1418 unsigned int result;
1419 unsigned long flags;
1421 save_flags(flags); cli();
1422 status = serial_in(info, UART_LSR);
1423 restore_flags(flags);
1424 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1425 return put_user(result,value);
1429 static int
1430 get_modem_info(struct su_struct * info, unsigned int *value)
1432 unsigned char control, status;
1433 unsigned int result;
1434 unsigned long flags;
1436 control = info->MCR;
1437 save_flags(flags); cli();
1438 status = serial_in(info, UART_MSR);
1439 restore_flags(flags);
1440 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1441 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1442 #ifdef TIOCM_OUT1
1443 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1444 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1445 #endif
1446 | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0)
1447 | ((status & UART_MSR_RI) ? TIOCM_RNG : 0)
1448 | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0)
1449 | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1450 return put_user(result,value);
1453 static int
1454 set_modem_info(struct su_struct * info, unsigned int cmd, unsigned int *value)
1456 int error;
1457 unsigned int arg;
1458 unsigned long flags;
1460 error = get_user(arg, value);
1461 if (error)
1462 return error;
1463 switch (cmd) {
1464 case TIOCMBIS:
1465 if (arg & TIOCM_RTS)
1466 info->MCR |= UART_MCR_RTS;
1467 if (arg & TIOCM_DTR)
1468 info->MCR |= UART_MCR_DTR;
1469 #ifdef TIOCM_OUT1
1470 if (arg & TIOCM_OUT1)
1471 info->MCR |= UART_MCR_OUT1;
1472 if (arg & TIOCM_OUT2)
1473 info->MCR |= UART_MCR_OUT2;
1474 #endif
1475 break;
1476 case TIOCMBIC:
1477 if (arg & TIOCM_RTS)
1478 info->MCR &= ~UART_MCR_RTS;
1479 if (arg & TIOCM_DTR)
1480 info->MCR &= ~UART_MCR_DTR;
1481 #ifdef TIOCM_OUT1
1482 if (arg & TIOCM_OUT1)
1483 info->MCR &= ~UART_MCR_OUT1;
1484 if (arg & TIOCM_OUT2)
1485 info->MCR &= ~UART_MCR_OUT2;
1486 #endif
1487 break;
1488 case TIOCMSET:
1489 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1490 #ifdef TIOCM_OUT1
1491 UART_MCR_OUT1 |
1492 UART_MCR_OUT2 |
1493 #endif
1494 UART_MCR_DTR))
1495 | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1496 #ifdef TIOCM_OUT1
1497 | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1498 | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1499 #endif
1500 | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1501 break;
1502 default:
1503 return -EINVAL;
1505 save_flags(flags); cli();
1506 serial_out(info, UART_MCR, info->MCR);
1507 restore_flags(flags);
1508 return 0;
1512 * su_break() --- routine which turns the break handling on or off
1514 static void
1515 su_break(struct tty_struct *tty, int break_state)
1517 struct su_struct * info = (struct su_struct *)tty->driver_data;
1518 unsigned long flags;
1520 if (serial_paranoia_check(info, tty->device, "su_break"))
1521 return;
1523 if (!info->port)
1524 return;
1525 save_flags(flags); cli();
1526 if (break_state == -1)
1527 serial_out(info, UART_LCR,
1528 serial_inp(info, UART_LCR) | UART_LCR_SBC);
1529 else
1530 serial_out(info, UART_LCR,
1531 serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1532 restore_flags(flags);
1535 static int
1536 su_ioctl(struct tty_struct *tty, struct file * file,
1537 unsigned int cmd, unsigned long arg)
1539 int error;
1540 struct su_struct * info = (struct su_struct *)tty->driver_data;
1541 struct async_icount cprev, cnow; /* kernel counter temps */
1542 struct serial_icounter_struct *p_cuser; /* user space */
1544 if (serial_paranoia_check(info, tty->device, "su_ioctl"))
1545 return -ENODEV;
1547 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1548 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1549 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1550 if (tty->flags & (1 << TTY_IO_ERROR))
1551 return -EIO;
1554 switch (cmd) {
1555 case TIOCMGET:
1556 return get_modem_info(info, (unsigned int *) arg);
1557 case TIOCMBIS:
1558 case TIOCMBIC:
1559 case TIOCMSET:
1560 return set_modem_info(info, cmd, (unsigned int *) arg);
1562 case TIOCSERGETLSR: /* Get line status register */
1563 return get_lsr_info(info, (unsigned int *) arg);
1565 #if 0
1566 case TIOCSERGSTRUCT:
1567 if (copy_to_user((struct async_struct *) arg,
1568 info, sizeof(struct async_struct)))
1569 return -EFAULT;
1570 return 0;
1571 #endif
1574 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1575 * - mask passed in arg for lines of interest
1576 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1577 * Caller should use TIOCGICOUNT to see which one it was
1579 case TIOCMIWAIT:
1580 cli();
1581 /* note the counters on entry */
1582 cprev = info->icount;
1583 sti();
1584 while (1) {
1585 interruptible_sleep_on(&info->delta_msr_wait);
1586 /* see if a signal did it */
1587 if (signal_pending(current))
1588 return -ERESTARTSYS;
1589 cli();
1590 cnow = info->icount; /* atomic copy */
1591 sti();
1592 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1593 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1594 return -EIO; /* no change => error */
1595 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1596 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1597 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1598 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1599 return 0;
1601 cprev = cnow;
1603 /* NOTREACHED */
1606 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1607 * Return: write counters to the user passed counter struct
1608 * NB: both 1->0 and 0->1 transitions are counted except for
1609 * RI where only 0->1 is counted.
1611 case TIOCGICOUNT:
1612 cli();
1613 cnow = info->icount;
1614 sti();
1615 p_cuser = (struct serial_icounter_struct *) arg;
1616 error = put_user(cnow.cts, &p_cuser->cts);
1617 if (error) return error;
1618 error = put_user(cnow.dsr, &p_cuser->dsr);
1619 if (error) return error;
1620 error = put_user(cnow.rng, &p_cuser->rng);
1621 if (error) return error;
1622 error = put_user(cnow.dcd, &p_cuser->dcd);
1623 if (error) return error;
1624 return 0;
1626 default:
1627 return -ENOIOCTLCMD;
1629 /* return 0; */ /* Trigger warnings if fall through by a chance. */
1632 static void
1633 su_set_termios(struct tty_struct *tty, struct termios *old_termios)
1635 struct su_struct *info = (struct su_struct *)tty->driver_data;
1636 unsigned long flags;
1638 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1639 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1640 == RELEVANT_IFLAG(old_termios->c_iflag)))
1641 return;
1643 change_speed(info, old_termios);
1645 /* Handle transition to B0 status */
1646 if ((old_termios->c_cflag & CBAUD) &&
1647 !(tty->termios->c_cflag & CBAUD)) {
1648 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1649 save_flags(flags); cli();
1650 serial_out(info, UART_MCR, info->MCR);
1651 restore_flags(flags);
1654 /* Handle transition away from B0 status */
1655 if (!(old_termios->c_cflag & CBAUD) &&
1656 (tty->termios->c_cflag & CBAUD)) {
1657 info->MCR |= UART_MCR_DTR;
1658 if (!(tty->termios->c_cflag & CRTSCTS) ||
1659 !test_bit(TTY_THROTTLED, &tty->flags)) {
1660 info->MCR |= UART_MCR_RTS;
1662 save_flags(flags); cli();
1663 serial_out(info, UART_MCR, info->MCR);
1664 restore_flags(flags);
1667 /* Handle turning off CRTSCTS */
1668 if ((old_termios->c_cflag & CRTSCTS) &&
1669 !(tty->termios->c_cflag & CRTSCTS)) {
1670 tty->hw_stopped = 0;
1671 su_start(tty);
1674 #if 0
1676 * No need to wake up processes in open wait, since they
1677 * sample the CLOCAL flag once, and don't recheck it.
1678 * XXX It's not clear whether the current behavior is correct
1679 * or not. Hence, this may change.....
1681 if (!(old_termios->c_cflag & CLOCAL) &&
1682 (tty->termios->c_cflag & CLOCAL))
1683 wake_up_interruptible(&info->open_wait);
1684 #endif
1688 * ------------------------------------------------------------
1689 * su_close()
1691 * This routine is called when the serial port gets closed. First, we
1692 * wait for the last remaining data to be sent. Then, we unlink its
1693 * async structure from the interrupt chain if necessary, and we free
1694 * that IRQ if nothing is left in the chain.
1695 * ------------------------------------------------------------
1697 static void
1698 su_close(struct tty_struct *tty, struct file * filp)
1700 struct su_struct *info = (struct su_struct *)tty->driver_data;
1701 unsigned long flags;
1703 if (!info || serial_paranoia_check(info, tty->device, "su_close"))
1704 return;
1706 save_flags(flags); cli();
1708 if (tty_hung_up_p(filp)) {
1709 DBG_CNT("before DEC-hung");
1710 MOD_DEC_USE_COUNT;
1711 restore_flags(flags);
1712 return;
1715 #ifdef SERIAL_DEBUG_OPEN
1716 printk("su_close ttys%d, count = %d\n", info->line, info->count);
1717 #endif
1718 if ((tty->count == 1) && (info->count != 1)) {
1720 * Uh, oh. tty->count is 1, which means that the tty
1721 * structure will be freed. info->count should always
1722 * be one in these conditions. If it's greater than
1723 * one, we've got real problems, since it means the
1724 * serial port won't be shutdown.
1726 printk("su_close: bad serial port count; tty->count is 1, "
1727 "info->count is %d\n", info->count);
1728 info->count = 1;
1730 if (--info->count < 0) {
1731 printk("su_close: bad serial port count for ttys%d: %d\n",
1732 info->line, info->count);
1733 info->count = 0;
1735 if (info->count) {
1736 DBG_CNT("before DEC-2");
1737 MOD_DEC_USE_COUNT;
1738 restore_flags(flags);
1739 return;
1741 info->flags |= ASYNC_CLOSING;
1743 * Save the termios structure, since this port may have
1744 * separate termios for callout and dialin.
1746 if (info->flags & ASYNC_NORMAL_ACTIVE)
1747 info->normal_termios = *tty->termios;
1748 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1749 info->callout_termios = *tty->termios;
1751 * Now we wait for the transmit buffer to clear; and we notify
1752 * the line discipline to only process XON/XOFF characters.
1754 tty->closing = 1;
1755 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1756 tty_wait_until_sent(tty, info->closing_wait);
1758 * At this point we stop accepting input. To do this, we
1759 * disable the receive line status interrupts, and tell the
1760 * interrupt driver to stop checking the data ready bit in the
1761 * line status register.
1763 info->IER &= ~UART_IER_RLSI;
1764 info->read_status_mask &= ~UART_LSR_DR;
1765 if (info->flags & ASYNC_INITIALIZED) {
1766 serial_out(info, UART_IER, info->IER);
1768 * Before we drop DTR, make sure the UART transmitter
1769 * has completely drained; this is especially
1770 * important if there is a transmit FIFO!
1772 su_wait_until_sent(tty, info->timeout);
1774 shutdown(info);
1775 if (tty->driver.flush_buffer)
1776 tty->driver.flush_buffer(tty);
1777 if (tty->ldisc.flush_buffer)
1778 tty->ldisc.flush_buffer(tty);
1779 tty->closing = 0;
1780 info->event = 0;
1781 info->tty = 0;
1782 if (info->blocked_open) {
1783 if (info->close_delay) {
1784 current->state = TASK_INTERRUPTIBLE;
1785 schedule_timeout(info->close_delay);
1787 wake_up_interruptible(&info->open_wait);
1789 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1790 ASYNC_CLOSING);
1791 wake_up_interruptible(&info->close_wait);
1792 MOD_DEC_USE_COUNT;
1793 restore_flags(flags);
1797 * su_wait_until_sent() --- wait until the transmitter is empty
1799 static void
1800 su_wait_until_sent(struct tty_struct *tty, int timeout)
1802 struct su_struct * info = (struct su_struct *)tty->driver_data;
1803 unsigned long orig_jiffies, char_time;
1804 int lsr;
1806 if (serial_paranoia_check(info, tty->device, "su_wait_until_sent"))
1807 return;
1809 if (info->type == PORT_UNKNOWN)
1810 return;
1812 if (info->xmit_fifo_size == 0)
1813 return; /* Just in case ... */
1815 orig_jiffies = jiffies;
1817 * Set the check interval to be 1/5 of the estimated time to
1818 * send a single character, and make it at least 1. The check
1819 * interval should also be less than the timeout.
1821 * Note: we have to use pretty tight timings here to satisfy
1822 * the NIST-PCTS.
1824 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1825 char_time = char_time / 5;
1826 if (char_time == 0)
1827 char_time = 1;
1828 if (timeout)
1829 char_time = MIN(char_time, timeout);
1830 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1831 printk("In su_wait_until_sent(%d) check=%lu...", timeout, char_time);
1832 printk("jiff=%lu...", jiffies);
1833 #endif
1834 while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
1835 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1836 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1837 #endif
1838 current->state = TASK_INTERRUPTIBLE;
1839 current->counter = 0; /* make us low-priority */
1840 schedule_timeout(char_time);
1841 if (signal_pending(current))
1842 break;
1843 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1844 break;
1846 current->state = TASK_RUNNING;
1847 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1848 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1849 #endif
1853 * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1855 static void
1856 su_hangup(struct tty_struct *tty)
1858 struct su_struct * info = (struct su_struct *)tty->driver_data;
1860 if (serial_paranoia_check(info, tty->device, "su_hangup"))
1861 return;
1863 su_flush_buffer(tty);
1864 shutdown(info);
1865 info->event = 0;
1866 info->count = 0;
1867 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1868 info->tty = 0;
1869 wake_up_interruptible(&info->open_wait);
1873 * ------------------------------------------------------------
1874 * su_open() and friends
1875 * ------------------------------------------------------------
1877 static int
1878 block_til_ready(struct tty_struct *tty, struct file * filp,
1879 struct su_struct *info)
1881 DECLARE_WAITQUEUE(wait, current);
1882 int retval;
1883 int do_clocal = 0, extra_count = 0;
1884 unsigned long flags;
1887 * If the device is in the middle of being closed, then block
1888 * until it's done, and then try again.
1890 if (tty_hung_up_p(filp) ||
1891 (info->flags & ASYNC_CLOSING)) {
1892 if (info->flags & ASYNC_CLOSING)
1893 interruptible_sleep_on(&info->close_wait);
1894 #ifdef SERIAL_DO_RESTART
1895 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1896 -EAGAIN : -ERESTARTSYS);
1897 #else
1898 return -EAGAIN;
1899 #endif
1903 * If this is a callout device, then just make sure the normal
1904 * device isn't being used.
1906 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1907 if (info->flags & ASYNC_NORMAL_ACTIVE)
1908 return -EBUSY;
1909 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1910 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1911 (info->session != current->session))
1912 return -EBUSY;
1913 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1914 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1915 (info->pgrp != current->pgrp))
1916 return -EBUSY;
1917 info->flags |= ASYNC_CALLOUT_ACTIVE;
1918 return 0;
1922 * If non-blocking mode is set, or the port is not enabled,
1923 * then make the check up front and then exit.
1925 if ((filp->f_flags & O_NONBLOCK) ||
1926 (tty->flags & (1 << TTY_IO_ERROR))) {
1927 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1928 return -EBUSY;
1929 info->flags |= ASYNC_NORMAL_ACTIVE;
1930 return 0;
1933 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1934 if (info->normal_termios.c_cflag & CLOCAL)
1935 do_clocal = 1;
1936 } else {
1937 if (tty->termios->c_cflag & CLOCAL)
1938 do_clocal = 1;
1942 * Block waiting for the carrier detect and the line to become
1943 * free (i.e., not in use by the callout). While we are in
1944 * this loop, info->count is dropped by one, so that
1945 * su_close() knows when to free things. We restore it upon
1946 * exit, either normal or abnormal.
1948 retval = 0;
1949 add_wait_queue(&info->open_wait, &wait);
1950 #ifdef SERIAL_DEBUG_OPEN
1951 printk("block_til_ready before block: ttys%d, count = %d\n",
1952 info->line, info->count);
1953 #endif
1954 save_flags(flags); cli();
1955 if (!tty_hung_up_p(filp)) {
1956 extra_count = 1;
1957 info->count--;
1959 restore_flags(flags);
1960 info->blocked_open++;
1961 while (1) {
1962 save_flags(flags); cli();
1963 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1964 (tty->termios->c_cflag & CBAUD))
1965 serial_out(info, UART_MCR,
1966 serial_inp(info, UART_MCR) |
1967 (UART_MCR_DTR | UART_MCR_RTS));
1968 restore_flags(flags);
1969 current->state = TASK_INTERRUPTIBLE;
1970 if (tty_hung_up_p(filp) ||
1971 !(info->flags & ASYNC_INITIALIZED)) {
1972 #ifdef SERIAL_DO_RESTART
1973 if (info->flags & ASYNC_HUP_NOTIFY)
1974 retval = -EAGAIN;
1975 else
1976 retval = -ERESTARTSYS;
1977 #else
1978 retval = -EAGAIN;
1979 #endif
1980 break;
1982 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1983 !(info->flags & ASYNC_CLOSING) &&
1984 (do_clocal || (serial_in(info, UART_MSR) &
1985 UART_MSR_DCD)))
1986 break;
1987 if (signal_pending(current)) {
1988 retval = -ERESTARTSYS;
1989 break;
1991 #ifdef SERIAL_DEBUG_OPEN
1992 printk("block_til_ready blocking: ttys%d, count = %d\n",
1993 info->line, info->count);
1994 #endif
1995 schedule();
1997 current->state = TASK_RUNNING;
1998 remove_wait_queue(&info->open_wait, &wait);
1999 if (extra_count)
2000 info->count++;
2001 info->blocked_open--;
2002 #ifdef SERIAL_DEBUG_OPEN
2003 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2004 info->line, info->count);
2005 #endif
2006 if (retval)
2007 return retval;
2008 info->flags |= ASYNC_NORMAL_ACTIVE;
2009 return 0;
2013 * This routine is called whenever a serial port is opened. It
2014 * enables interrupts for a serial port, linking in its async structure into
2015 * the IRQ chain. It also performs the serial-specific
2016 * initialization for the tty structure.
2018 static int
2019 su_open(struct tty_struct *tty, struct file * filp)
2021 struct su_struct *info;
2022 int retval, line;
2023 unsigned long page;
2025 line = MINOR(tty->device) - tty->driver.minor_start;
2026 if ((line < 0) || (line >= NR_PORTS))
2027 return -ENODEV;
2028 info = su_table + line;
2029 info->count++;
2030 tty->driver_data = info;
2031 info->tty = tty;
2033 if (serial_paranoia_check(info, tty->device, "su_open")) {
2034 info->count--;
2035 return -ENODEV;
2038 #ifdef SERIAL_DEBUG_OPEN
2039 printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2040 info->count);
2041 #endif
2042 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2044 if (!tmp_buf) {
2045 page = get_free_page(GFP_KERNEL);
2046 if (!page)
2047 return -ENOMEM;
2048 if (tmp_buf)
2049 free_page(page);
2050 else
2051 tmp_buf = (unsigned char *) page;
2055 * If the port is the middle of closing, bail out now
2057 if (tty_hung_up_p(filp) ||
2058 (info->flags & ASYNC_CLOSING)) {
2059 if (info->flags & ASYNC_CLOSING)
2060 interruptible_sleep_on(&info->close_wait);
2061 #ifdef SERIAL_DO_RESTART
2062 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2063 -EAGAIN : -ERESTARTSYS);
2064 #else
2065 return -EAGAIN;
2066 #endif
2070 * Start up serial port
2072 retval = startup(info);
2073 if (retval)
2074 return retval;
2076 MOD_INC_USE_COUNT;
2077 retval = block_til_ready(tty, filp, info);
2078 if (retval) {
2079 #ifdef SERIAL_DEBUG_OPEN
2080 printk("su_open returning after block_til_ready with %d\n",
2081 retval);
2082 #endif
2083 return retval;
2086 if ((info->count == 1) &&
2087 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2088 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2089 *tty->termios = info->normal_termios;
2090 else
2091 *tty->termios = info->callout_termios;
2092 change_speed(info, 0);
2094 #ifdef CONFIG_SERIAL_CONSOLE
2095 if (sercons.cflag && sercons.index == line) {
2096 tty->termios->c_cflag = sercons.cflag;
2097 sercons.cflag = 0;
2098 change_speed(info, 0);
2100 #endif
2101 info->session = current->session;
2102 info->pgrp = current->pgrp;
2104 #ifdef SERIAL_DEBUG_OPEN
2105 printk("su_open ttys%d successful...", info->line);
2106 #endif
2107 return 0;
2111 * /proc fs routines....
2113 static __inline__ int
2114 line_info(char *buf, struct su_struct *info)
2116 char stat_buf[30], control, status;
2117 int ret;
2118 unsigned long flags;
2120 ret = sprintf(buf, "%d: uart:%s port:%X irq:%s",
2121 info->line, uart_config[info->type].name,
2122 (int)info->port, __irq_itoa(info->irq));
2124 if (info->port == 0 || info->type == PORT_UNKNOWN) {
2125 ret += sprintf(buf+ret, "\n");
2126 return ret;
2130 * Figure out the current RS-232 lines
2132 save_flags(flags); cli();
2133 status = serial_in(info, UART_MSR);
2134 control = info ? info->MCR : serial_in(info, UART_MCR);
2135 restore_flags(flags);
2137 stat_buf[0] = 0;
2138 stat_buf[1] = 0;
2139 if (control & UART_MCR_RTS)
2140 strcat(stat_buf, "|RTS");
2141 if (status & UART_MSR_CTS)
2142 strcat(stat_buf, "|CTS");
2143 if (control & UART_MCR_DTR)
2144 strcat(stat_buf, "|DTR");
2145 if (status & UART_MSR_DSR)
2146 strcat(stat_buf, "|DSR");
2147 if (status & UART_MSR_DCD)
2148 strcat(stat_buf, "|CD");
2149 if (status & UART_MSR_RI)
2150 strcat(stat_buf, "|RI");
2152 if (info->quot) {
2153 ret += sprintf(buf+ret, " baud:%d",
2154 info->baud_base / info->quot);
2157 ret += sprintf(buf+ret, " tx:%d rx:%d",
2158 info->icount.tx, info->icount.rx);
2160 if (info->icount.frame)
2161 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2163 if (info->icount.parity)
2164 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2166 if (info->icount.brk)
2167 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2169 if (info->icount.overrun)
2170 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2173 * Last thing is the RS-232 status lines
2175 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2176 return ret;
2179 int su_read_proc(char *page, char **start, off_t off, int count,
2180 int *eof, void *data)
2182 int i, len = 0;
2183 off_t begin = 0;
2185 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2186 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2187 len += line_info(page + len, &su_table[i]);
2188 if (len+begin > off+count)
2189 goto done;
2190 if (len+begin < off) {
2191 begin += len;
2192 len = 0;
2195 *eof = 1;
2196 done:
2197 if (off >= len+begin)
2198 return 0;
2199 *start = page + (begin-off);
2200 return ((count < begin+len-off) ? count : begin+len-off);
2204 * ---------------------------------------------------------------------
2205 * su_init() and friends
2207 * su_init() is called at boot-time to initialize the serial driver.
2208 * ---------------------------------------------------------------------
2212 * This routine prints out the appropriate serial driver version
2213 * number, and identifies which options were configured into this
2214 * driver.
2216 __initfunc(static __inline__ void show_su_version(void))
2218 char *revision = "$Revision: 1.19 $";
2219 char *version, *p;
2221 version = strchr(revision, ' ');
2222 strcpy(serial_version, ++version);
2223 p = strchr(serial_version, ' ');
2224 *p = '\0';
2225 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2229 * This routine is called by su_init() to initialize a specific serial
2230 * port. It determines what type of UART chip this serial port is
2231 * using: 8250, 16450, 16550, 16550A. The important question is
2232 * whether or not this UART is a 16550A, since this will determine
2233 * whether or not we can use its FIFO features.
2235 static void
2236 autoconfig(struct su_struct *info)
2238 unsigned char status1, status2, scratch, scratch2;
2239 #ifdef __sparc_v9__
2240 struct linux_ebus_device *dev = 0;
2241 struct linux_ebus *ebus;
2242 #else
2243 struct linux_prom_registers reg0;
2244 #endif
2245 unsigned long flags;
2247 #ifdef __sparc_v9__
2248 for_each_ebus(ebus) {
2249 for_each_ebusdev(dev, ebus) {
2250 if (!strncmp(dev->prom_name, "su", 2)) {
2251 if (dev->prom_node == info->kbd_node)
2252 goto ebus_done;
2253 if (dev->prom_node == info->ms_node)
2254 goto ebus_done;
2258 ebus_done:
2259 if (!dev)
2260 return;
2262 info->port = dev->base_address[0];
2263 if (check_region(info->port, 8))
2264 return;
2266 info->irq = dev->irqs[0];
2267 #else
2268 if (!info->port_node)
2269 return;
2271 if (prom_getproperty(info->port_node, "reg",
2272 (char *)&reg0, sizeof(reg0)) == -1) {
2273 prom_printf("su: no \"reg\" property\n");
2274 return;
2276 prom_apply_obio_ranges(&reg0, 1);
2277 if ((info->port = (unsigned long) sparc_alloc_io(reg0.phys_addr,
2278 0, reg0.reg_size, "su-regs", reg0.which_io, 0)) == 0) {
2279 prom_printf("su: cannot map\n");
2280 return;
2283 * There is no intr property on MrCoffee, so hardwire it. Krups?
2285 info->irq = IRQ_4M(13);
2286 #endif
2288 #ifdef DEBUG_SERIAL_OPEN
2289 printk("Found 'su' at %016lx IRQ %s\n", dev->base_address[0],
2290 __irq_itoa(dev->irqs[0]));
2291 #endif
2293 info->magic = SERIAL_MAGIC;
2295 save_flags(flags); cli();
2298 * Do a simple existence test first; if we fail this, there's
2299 * no point trying anything else.
2301 * 0x80 is used as a nonsense port to prevent against false
2302 * positives due to ISA bus float. The assumption is that
2303 * 0x80 is a non-existent port; which should be safe since
2304 * include/asm/io.h also makes this assumption.
2306 scratch = serial_inp(info, UART_IER);
2307 serial_outp(info, UART_IER, 0);
2308 scratch2 = serial_inp(info, UART_IER);
2309 serial_outp(info, UART_IER, scratch);
2310 if (scratch2) {
2311 restore_flags(flags);
2312 return; /* We failed; there's nothing here */
2315 #if 0 /* P3: This does not work on MrCoffee. OUT2 is 0x80 - should work... */
2316 scratch = serial_inp(info, UART_MCR);
2317 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2318 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2319 status1 = serial_inp(info, UART_MSR) & 0xF0;
2320 serial_outp(info, UART_MCR, scratch);
2321 if (status1 != 0x90) {
2322 restore_flags(flags);
2323 return;
2325 #endif
2327 scratch2 = serial_in(info, UART_LCR);
2328 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
2329 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2330 serial_outp(info, UART_LCR, 0);
2331 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2332 scratch = serial_in(info, UART_IIR) >> 6;
2333 switch (scratch) {
2334 case 0:
2335 info->type = PORT_16450;
2336 break;
2337 case 1:
2338 info->type = PORT_UNKNOWN;
2339 break;
2340 case 2:
2341 info->type = PORT_16550;
2342 break;
2343 case 3:
2344 info->type = PORT_16550A;
2345 break;
2347 if (info->type == PORT_16550A) {
2348 /* Check for Startech UART's */
2349 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2350 if (serial_in(info, UART_EFR) == 0) {
2351 info->type = PORT_16650;
2352 } else {
2353 serial_outp(info, UART_LCR, 0xBF);
2354 if (serial_in(info, UART_EFR) == 0)
2355 info->type = PORT_16650V2;
2358 if (info->type == PORT_16550A) {
2359 /* Check for TI 16750 */
2360 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2361 serial_outp(info, UART_FCR,
2362 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2363 scratch = serial_in(info, UART_IIR) >> 5;
2364 if (scratch == 7) {
2365 serial_outp(info, UART_LCR, 0);
2366 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2367 scratch = serial_in(info, UART_IIR) >> 5;
2368 if (scratch == 6)
2369 info->type = PORT_16750;
2371 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2373 serial_outp(info, UART_LCR, scratch2);
2374 if (info->type == PORT_16450) {
2375 scratch = serial_in(info, UART_SCR);
2376 serial_outp(info, UART_SCR, 0xa5);
2377 status1 = serial_in(info, UART_SCR);
2378 serial_outp(info, UART_SCR, 0x5a);
2379 status2 = serial_in(info, UART_SCR);
2380 serial_outp(info, UART_SCR, scratch);
2382 if ((status1 != 0xa5) || (status2 != 0x5a))
2383 info->type = PORT_8250;
2385 info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2387 if (info->type == PORT_UNKNOWN) {
2388 restore_flags(flags);
2389 return;
2392 if (info->kbd_node || info->ms_node)
2393 sprintf(info->name, "su(%s)", info->ms_node ? "mouse" : "kbd");
2394 else
2395 strcpy(info->name, "su(serial)");
2397 #ifdef __sparc_v9__
2398 request_region(info->port, 8, info->name);
2399 #endif
2402 * Reset the UART.
2404 serial_outp(info, UART_MCR, 0x00);
2405 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2406 (void)serial_in(info, UART_RX);
2407 serial_outp(info, UART_IER, 0x00);
2409 restore_flags(flags);
2413 * The serial driver boot-time initialization code!
2415 __initfunc(int su_serial_init(void))
2417 int i;
2418 struct su_struct *info;
2420 init_bh(SERIAL_BH, do_serial_bh);
2421 show_su_version();
2423 /* Initialize the tty_driver structure */
2425 memset(&serial_driver, 0, sizeof(struct tty_driver));
2426 serial_driver.magic = TTY_DRIVER_MAGIC;
2427 serial_driver.driver_name = "su";
2428 serial_driver.name = "ttyS";
2429 serial_driver.major = TTY_MAJOR;
2430 serial_driver.minor_start = 64;
2431 serial_driver.num = NR_PORTS;
2432 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2433 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2434 serial_driver.init_termios = tty_std_termios;
2435 serial_driver.init_termios.c_cflag =
2436 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2437 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2438 serial_driver.refcount = &serial_refcount;
2439 serial_driver.table = serial_table;
2440 serial_driver.termios = serial_termios;
2441 serial_driver.termios_locked = serial_termios_locked;
2443 serial_driver.open = su_open;
2444 serial_driver.close = su_close;
2445 serial_driver.write = su_write;
2446 serial_driver.put_char = su_put_char;
2447 serial_driver.flush_chars = su_flush_chars;
2448 serial_driver.write_room = su_write_room;
2449 serial_driver.chars_in_buffer = su_chars_in_buffer;
2450 serial_driver.flush_buffer = su_flush_buffer;
2451 serial_driver.ioctl = su_ioctl;
2452 serial_driver.throttle = su_throttle;
2453 serial_driver.unthrottle = su_unthrottle;
2454 serial_driver.send_xchar = su_send_xchar;
2455 serial_driver.set_termios = su_set_termios;
2456 serial_driver.stop = su_stop;
2457 serial_driver.start = su_start;
2458 serial_driver.hangup = su_hangup;
2459 serial_driver.break_ctl = su_break;
2460 serial_driver.wait_until_sent = su_wait_until_sent;
2461 serial_driver.read_proc = su_read_proc;
2464 * The callout device is just like normal device except for
2465 * major number and the subtype code.
2467 callout_driver = serial_driver;
2468 callout_driver.name = "cua";
2469 callout_driver.major = TTYAUX_MAJOR;
2470 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2471 callout_driver.read_proc = 0;
2472 callout_driver.proc_entry = 0;
2474 if (tty_register_driver(&serial_driver))
2475 panic("Couldn't register regular su\n");
2476 if (tty_register_driver(&callout_driver))
2477 panic("Couldn't register callout su\n");
2479 for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2480 info->line = i;
2481 info->type = PORT_UNKNOWN;
2482 info->baud_base = BAUD_BASE;
2483 /* info->flags = 0; */
2484 info->custom_divisor = 0;
2485 info->close_delay = 5*HZ/10;
2486 info->closing_wait = 30*HZ;
2487 info->callout_termios = callout_driver.init_termios;
2488 info->normal_termios = serial_driver.init_termios;
2489 info->icount.cts = info->icount.dsr =
2490 info->icount.rng = info->icount.dcd = 0;
2491 info->icount.rx = info->icount.tx = 0;
2492 info->icount.frame = info->icount.parity = 0;
2493 info->icount.overrun = info->icount.brk = 0;
2494 info->tqueue.routine = do_softint;
2495 info->tqueue.data = info;
2496 info->cflag = serial_driver.init_termios.c_cflag;
2498 autoconfig(info);
2499 if (info->type == PORT_UNKNOWN)
2500 continue;
2502 printk(KERN_INFO "%s at %16lx (irq = %s) is a %s\n",
2503 info->name, info->port, __irq_itoa(info->irq),
2504 uart_config[info->type].name);
2507 return 0;
2510 __initfunc(int su_kbd_ms_init(void))
2512 int i;
2513 struct su_struct *info;
2515 show_su_version();
2517 for (i = 0, info = su_table; i < 2; i++, info++) {
2518 info->line = i;
2519 info->type = PORT_UNKNOWN;
2520 info->baud_base = BAUD_BASE;
2522 if (info->kbd_node)
2523 info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2524 else
2525 info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2527 autoconfig(info);
2528 if (info->type == PORT_UNKNOWN)
2529 continue;
2531 printk(KERN_INFO "%s at %16lx (irq = %s) is a %s\n",
2532 info->name, info->port, __irq_itoa(info->irq),
2533 uart_config[info->type].name);
2535 startup(info);
2536 if (info->kbd_node)
2537 keyboard_zsinit(su_put_char_kbd);
2538 else
2539 sun_mouse_zsinit();
2541 return 0;
2544 __initfunc(int su_probe (unsigned long *memory_start))
2546 struct su_struct *info = su_table;
2547 int node, enode, tnode, sunode;
2548 int kbnode = 0, msnode = 0;
2549 int devices = 0;
2550 char prop[128];
2551 int len;
2554 * Find su on MrCoffee. We return OK code if find any.
2555 * Then su_init finds every one and initializes them.
2556 * We do this early because MrCoffee got no aliases.
2558 node = prom_getchild(prom_root_node);
2559 if ((node = prom_searchsiblings(node, "obio")) != 0) {
2560 if ((sunode = prom_getchild(node)) != 0) {
2561 if ((sunode = prom_searchsiblings(sunode, "su")) != 0) {
2562 info->port_node = sunode;
2563 #ifdef CONFIG_SERIAL_CONSOLE
2565 * Console must be initiated after the generic
2566 * initialization.
2567 * sunserial_setinitfunc inverts order, so
2568 * call this before next one.
2570 sunserial_setinitfunc(memory_start,
2571 su_serial_console_init);
2572 #endif
2573 sunserial_setinitfunc(memory_start,
2574 su_serial_init);
2575 return 0;
2581 * Get the nodes for keyboard and mouse from 'aliases'...
2583 node = prom_getchild(prom_root_node);
2584 node = prom_searchsiblings(node, "aliases");
2585 if (!node)
2586 return -ENODEV;
2588 len = prom_getproperty(node, "keyboard", prop, sizeof(prop));
2589 if (len > 0) {
2590 prop[len] = 0;
2591 kbnode = prom_finddevice(prop);
2593 if (!kbnode)
2594 return -ENODEV;
2596 len = prom_getproperty(node, "mouse", prop, sizeof(prop));
2597 if (len > 0) {
2598 prop[len] = 0;
2599 msnode = prom_finddevice(prop);
2601 if (!msnode)
2602 return -ENODEV;
2605 * Find matching EBus nodes...
2607 node = prom_getchild(prom_root_node);
2608 if ((node = prom_searchsiblings(node, "pci")) == 0) {
2609 return -ENODEV; /* Plain sparc */
2613 * Check for SUNW,sabre on Ultra 5/10/AXi.
2615 len = prom_getproperty(node, "model", prop, sizeof(prop));
2616 if ((len > 0) && !strncmp(prop, "SUNW,sabre", len)) {
2617 node = prom_getchild(node);
2618 node = prom_searchsiblings(node, "pci");
2622 * For each PCI bus...
2624 while (node) {
2625 enode = prom_getchild(node);
2626 enode = prom_searchsiblings(enode, "ebus");
2629 * For each EBus on this PCI...
2631 while (enode) {
2632 sunode = prom_getchild(enode);
2633 tnode = prom_searchsiblings(sunode, "su");
2634 if (!tnode)
2635 tnode = prom_searchsiblings(sunode, "su_pnp");
2636 sunode = tnode;
2639 * For each 'su' on this EBus...
2641 while (sunode) {
2643 * Does it match?
2645 if (sunode == kbnode) {
2646 info->kbd_node = sunode;
2647 ++info;
2648 ++devices;
2650 if (sunode == msnode) {
2651 info->ms_node = sunode;
2652 ++info;
2653 ++devices;
2657 * Found everything we need?
2659 if (devices == 2)
2660 goto found;
2662 sunode = prom_getsibling(sunode);
2663 tnode = prom_searchsiblings(sunode, "su");
2664 if (!tnode)
2665 tnode = prom_searchsiblings(sunode,
2666 "su_pnp");
2667 sunode = tnode;
2669 enode = prom_getsibling(enode);
2670 enode = prom_searchsiblings(enode, "ebus");
2672 node = prom_getsibling(node);
2673 node = prom_searchsiblings(node, "pci");
2675 return -ENODEV;
2677 found:
2678 sunserial_setinitfunc(memory_start, su_kbd_ms_init);
2679 rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2680 sunkbd_setinitfunc(memory_start, sun_kbd_init);
2681 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2682 kbd_ops.setledstate = sun_setledstate;
2683 kbd_ops.getledstate = sun_getledstate;
2684 kbd_ops.setkeycode = sun_setkeycode;
2685 kbd_ops.getkeycode = sun_getkeycode;
2686 #ifdef CONFIG_PCI
2687 sunkbd_install_keymaps(memory_start, sun_key_maps, sun_keymap_count,
2688 sun_func_buf, sun_func_table,
2689 sun_funcbufsize, sun_funcbufleft,
2690 sun_accent_table, sun_accent_table_size);
2691 #endif
2692 return 0;
2696 * ------------------------------------------------------------
2697 * Serial console driver
2698 * ------------------------------------------------------------
2700 #ifdef CONFIG_SERIAL_CONSOLE
2702 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2705 * Wait for transmitter & holding register to empty
2707 static __inline__ void
2708 wait_for_xmitr(struct su_struct *info)
2710 int lsr;
2711 unsigned int tmout = 1000000;
2713 do {
2714 lsr = su_inb(info, UART_LSR);
2715 if (--tmout == 0)
2716 break;
2717 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2721 * Print a string to the serial port trying not to disturb
2722 * any possible real use of the port...
2724 static void
2725 serial_console_write(struct console *co, const char *s,
2726 unsigned count)
2728 struct su_struct *info;
2729 int ier;
2730 unsigned i;
2732 info = su_table + co->index;
2734 * First save the IER then disable the interrupts
2736 ier = su_inb(info, UART_IER);
2737 su_outb(info, UART_IER, 0x00);
2740 * Now, do each character
2742 for (i = 0; i < count; i++, s++) {
2743 wait_for_xmitr(info);
2746 * Send the character out.
2747 * If a LF, also do CR...
2749 su_outb(info, UART_TX, *s);
2750 if (*s == 10) {
2751 wait_for_xmitr(info);
2752 su_outb(info, UART_TX, 13);
2757 * Finally, Wait for transmitter & holding register to empty
2758 * and restore the IER
2760 wait_for_xmitr(info);
2761 su_outb(info, UART_IER, ier);
2765 * Receive character from the serial port
2767 static int
2768 serial_console_wait_key(struct console *co)
2770 struct su_struct *info;
2771 int ier;
2772 int lsr;
2773 int c;
2775 info = su_table + co->index;
2778 * First save the IER then disable the interrupts so
2779 * that the real driver for the port does not get the
2780 * character.
2782 ier = su_inb(info, UART_IER);
2783 su_outb(info, UART_IER, 0x00);
2785 do {
2786 lsr = su_inb(info, UART_LSR);
2787 } while (!(lsr & UART_LSR_DR));
2788 c = su_inb(info, UART_RX);
2791 * Restore the interrupts
2793 su_outb(info, UART_IER, ier);
2795 return c;
2798 static kdev_t
2799 serial_console_device(struct console *c)
2801 return MKDEV(TTY_MAJOR, 64 + c->index);
2805 * Setup initial baud/bits/parity. We do two things here:
2806 * - construct a cflag setting for the first su_open()
2807 * - initialize the serial port
2808 * Return non-zero if we didn't find a serial port.
2810 __initfunc(static int serial_console_setup(struct console *co, char *options))
2812 struct su_struct *info;
2813 unsigned cval;
2814 int baud = 9600;
2815 int bits = 8;
2816 int parity = 'n';
2817 int cflag = CREAD | HUPCL | CLOCAL;
2818 int quot = 0;
2819 char *s;
2821 if (options) {
2822 baud = simple_strtoul(options, NULL, 10);
2823 s = options;
2824 while(*s >= '0' && *s <= '9')
2825 s++;
2826 if (*s) parity = *s++;
2827 if (*s) bits = *s - '0';
2831 * Now construct a cflag setting.
2833 switch(baud) {
2834 case 1200:
2835 cflag |= B1200;
2836 break;
2837 case 2400:
2838 cflag |= B2400;
2839 break;
2840 case 4800:
2841 cflag |= B4800;
2842 break;
2843 case 19200:
2844 cflag |= B19200;
2845 break;
2846 case 38400:
2847 cflag |= B38400;
2848 break;
2849 case 57600:
2850 cflag |= B57600;
2851 break;
2852 case 115200:
2853 cflag |= B115200;
2854 break;
2855 case 9600:
2856 default:
2857 cflag |= B9600;
2858 break;
2860 switch(bits) {
2861 case 7:
2862 cflag |= CS7;
2863 break;
2864 default:
2865 case 8:
2866 cflag |= CS8;
2867 break;
2869 switch(parity) {
2870 case 'o': case 'O':
2871 cflag |= PARODD;
2872 break;
2873 case 'e': case 'E':
2874 cflag |= PARENB;
2875 break;
2877 co->cflag = cflag;
2880 * Divisor, bytesize and parity
2882 info = su_table + co->index;
2883 quot = BAUD_BASE / baud;
2884 cval = cflag & (CSIZE | CSTOPB);
2885 #if defined(__powerpc__) || defined(__alpha__)
2886 cval >>= 8;
2887 #else /* !__powerpc__ && !__alpha__ */
2888 cval >>= 4;
2889 #endif /* !__powerpc__ && !__alpha__ */
2890 if (cflag & PARENB)
2891 cval |= UART_LCR_PARITY;
2892 if (!(cflag & PARODD))
2893 cval |= UART_LCR_EPAR;
2896 * Disable UART interrupts, set DTR and RTS high
2897 * and set speed.
2899 su_outb(info, UART_IER, 0);
2900 su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2901 su_outb(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
2902 su_outb(info, UART_DLL, quot & 0xff); /* LS of divisor */
2903 su_outb(info, UART_DLM, quot >> 8); /* MS of divisor */
2904 su_outb(info, UART_LCR, cval); /* reset DLAB */
2905 info->quot = quot;
2908 * If we read 0xff from the LSR, there is no UART here.
2910 if (su_inb(info, UART_LSR) == 0xff)
2911 return -1;
2913 return 0;
2916 static struct console sercons = {
2917 "ttyS",
2918 serial_console_write,
2919 NULL,
2920 serial_console_device,
2921 serial_console_wait_key,
2922 NULL,
2923 serial_console_setup,
2924 CON_PRINTBUFFER,
2927 NULL
2931 * Register console.
2933 __initfunc(int su_serial_console_init(void))
2935 extern int con_is_present(void);
2937 if (con_is_present())
2938 return 0;
2939 if (serial_console == 0)
2940 return 0;
2941 if (su_table[0].port == 0 || su_table[0].port_node == 0)
2942 return 0;
2943 sercons.index = 0;
2944 register_console(&sercons);
2945 return 0;
2948 #endif /* CONFIG_SERIAL_CONSOLE */