Import 2.3.18pre1
[davej-history.git] / drivers / sbus / char / su.c
blob21596e2cf6cdd2b113ae7f87085f26e2b6ebe9a1
1 /* $Id: su.c,v 1.28 1999/09/01 08:09:32 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 * Copyright (C) 1998-1999 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
95 enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
96 static char *su_typev[] = { "???", "mouse", "kbd", "serial" };
98 #define SU_PROPSIZE 128
101 * serial.c saves memory when it allocates async_info upon first open.
102 * We have parts of state structure together because we do call startup
103 * for keyboard and mouse.
105 struct su_struct {
106 int magic;
107 unsigned long port;
108 int baud_base;
109 int type; /* Hardware type: e.g. 16550 */
110 int irq;
111 int flags;
112 int line;
113 int cflag;
115 enum su_type port_type; /* Hookup type: e.g. mouse */
116 int port_node;
118 char name[16];
120 int xmit_fifo_size;
121 int custom_divisor;
122 unsigned short close_delay;
123 unsigned short closing_wait; /* time to wait before closing */
125 struct tty_struct *tty;
126 int read_status_mask;
127 int ignore_status_mask;
128 int timeout;
129 int quot;
130 int x_char; /* xon/xoff character */
131 int IER; /* Interrupt Enable Register */
132 int MCR; /* Modem control register */
133 unsigned long event;
134 int blocked_open; /* # of blocked opens */
135 long session; /* Session of opening process */
136 long pgrp; /* pgrp of opening process */
137 unsigned char *xmit_buf;
138 int xmit_head;
139 int xmit_tail;
140 int xmit_cnt;
141 struct tq_struct tqueue;
142 wait_queue_head_t open_wait;
143 wait_queue_head_t close_wait;
144 wait_queue_head_t delta_msr_wait;
146 int count;
147 struct async_icount icount;
148 struct termios normal_termios, callout_termios;
149 unsigned long last_active; /* For async_struct, to be */
153 * Scan status structure.
154 * "prop" is a local variable but it eats stack to keep it in each
155 * stack frame of a recursive procedure.
157 struct su_probe_scan {
158 int msnode, kbnode; /* PROM nodes for mouse and keyboard */
159 int msx, kbx; /* minors for mouse and keyboard */
160 int devices; /* scan index */
161 char prop[SU_PROPSIZE];
164 static char *serial_name = "PCIO serial driver";
165 static char serial_version[16];
167 static DECLARE_TASK_QUEUE(tq_serial);
169 static struct tty_driver serial_driver, callout_driver;
170 static int serial_refcount;
172 /* number of characters left in xmit buffer before we ask for more */
173 #define WAKEUP_CHARS 256
175 static void autoconfig(struct su_struct *info);
176 static void change_speed(struct su_struct *info, struct termios *old);
177 static void su_wait_until_sent(struct tty_struct *tty, int timeout);
180 * Here we define the default xmit fifo size used for each type of
181 * UART
183 static struct serial_uart_config uart_config[] = {
184 { "unknown", 1, 0 },
185 { "8250", 1, 0 },
186 { "16450", 1, 0 },
187 { "16550", 1, 0 },
188 { "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
189 { "cirrus", 1, 0 },
190 { "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
191 { "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
192 UART_STARTECH },
193 { "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
194 { 0, 0}
198 #define NR_PORTS 4
200 static struct su_struct su_table[NR_PORTS];
201 static struct tty_struct *serial_table[NR_PORTS];
202 static struct termios *serial_termios[NR_PORTS];
203 static struct termios *serial_termios_locked[NR_PORTS];
205 #ifndef MIN
206 #define MIN(a,b) ((a) < (b) ? (a) : (b))
207 #endif
210 * tmp_buf is used as a temporary buffer by serial_write. We need to
211 * lock it in case the copy_from_user blocks while swapping in a page,
212 * and some other program tries to do a serial write at the same time.
213 * Since the lock will only come under contention when the system is
214 * swapping and available memory is low, it makes sense to share one
215 * buffer across all the serial ports, since it significantly saves
216 * memory if large numbers of serial ports are open.
218 static unsigned char *tmp_buf;
219 static DECLARE_MUTEX(tmp_buf_sem);
221 static inline int serial_paranoia_check(struct su_struct *info,
222 kdev_t device, const char *routine)
224 #ifdef SERIAL_PARANOIA_CHECK
225 static const char *badmagic =
226 "Warning: bad magic number for serial struct (%s) in %s\n";
227 static const char *badinfo =
228 "Warning: null su_struct for (%s) in %s\n";
230 if (!info) {
231 printk(badinfo, kdevname(device), routine);
232 return 1;
234 if (info->magic != SERIAL_MAGIC) {
235 printk(badmagic, kdevname(device), routine);
236 return 1;
238 #endif
239 return 0;
242 static inline
243 unsigned int su_inb(struct su_struct *info, unsigned long offset)
245 return inb(info->port + offset);
248 static inline void
249 su_outb(struct su_struct *info, unsigned long offset, int value)
251 #ifndef __sparc_v9__
253 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
254 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
255 * gate outputs a logical one. Since we use level triggered interrupts
256 * we have lockup and watchdog reset. We cannot mask IRQ because
257 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
258 * This problem is similar to what Alpha people suffer, see serial.c.
260 if (offset == UART_MCR) value |= UART_MCR_OUT2;
261 #endif
262 outb(value, info->port + offset);
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->port_type == SU_PORT_KBD) {
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->port_type != SU_PORT_PORT) {
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->port_type == SU_PORT_PORT) {
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->port_type != SU_PORT_KBD)
1137 ++info;
1138 if (info->port_type != SU_PORT_KBD)
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->port_type != SU_PORT_MS)
1155 ++info;
1156 if (info->port_type != SU_PORT_MS)
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 schedule_timeout(char_time);
1840 if (signal_pending(current))
1841 break;
1842 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1843 break;
1845 current->state = TASK_RUNNING;
1846 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1847 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1848 #endif
1852 * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1854 static void
1855 su_hangup(struct tty_struct *tty)
1857 struct su_struct * info = (struct su_struct *)tty->driver_data;
1859 if (serial_paranoia_check(info, tty->device, "su_hangup"))
1860 return;
1862 su_flush_buffer(tty);
1863 shutdown(info);
1864 info->event = 0;
1865 info->count = 0;
1866 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1867 info->tty = 0;
1868 wake_up_interruptible(&info->open_wait);
1872 * ------------------------------------------------------------
1873 * su_open() and friends
1874 * ------------------------------------------------------------
1876 static int
1877 block_til_ready(struct tty_struct *tty, struct file * filp,
1878 struct su_struct *info)
1880 DECLARE_WAITQUEUE(wait, current);
1881 int retval;
1882 int do_clocal = 0, extra_count = 0;
1883 unsigned long flags;
1886 * If the device is in the middle of being closed, then block
1887 * until it's done, and then try again.
1889 if (tty_hung_up_p(filp) ||
1890 (info->flags & ASYNC_CLOSING)) {
1891 if (info->flags & ASYNC_CLOSING)
1892 interruptible_sleep_on(&info->close_wait);
1893 #ifdef SERIAL_DO_RESTART
1894 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1895 -EAGAIN : -ERESTARTSYS);
1896 #else
1897 return -EAGAIN;
1898 #endif
1902 * If this is a callout device, then just make sure the normal
1903 * device isn't being used.
1905 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1906 if (info->flags & ASYNC_NORMAL_ACTIVE)
1907 return -EBUSY;
1908 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1909 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1910 (info->session != current->session))
1911 return -EBUSY;
1912 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1913 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1914 (info->pgrp != current->pgrp))
1915 return -EBUSY;
1916 info->flags |= ASYNC_CALLOUT_ACTIVE;
1917 return 0;
1921 * If non-blocking mode is set, or the port is not enabled,
1922 * then make the check up front and then exit.
1924 if ((filp->f_flags & O_NONBLOCK) ||
1925 (tty->flags & (1 << TTY_IO_ERROR))) {
1926 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1927 return -EBUSY;
1928 info->flags |= ASYNC_NORMAL_ACTIVE;
1929 return 0;
1932 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1933 if (info->normal_termios.c_cflag & CLOCAL)
1934 do_clocal = 1;
1935 } else {
1936 if (tty->termios->c_cflag & CLOCAL)
1937 do_clocal = 1;
1941 * Block waiting for the carrier detect and the line to become
1942 * free (i.e., not in use by the callout). While we are in
1943 * this loop, info->count is dropped by one, so that
1944 * su_close() knows when to free things. We restore it upon
1945 * exit, either normal or abnormal.
1947 retval = 0;
1948 add_wait_queue(&info->open_wait, &wait);
1949 #ifdef SERIAL_DEBUG_OPEN
1950 printk("block_til_ready before block: ttys%d, count = %d\n",
1951 info->line, info->count);
1952 #endif
1953 save_flags(flags); cli();
1954 if (!tty_hung_up_p(filp)) {
1955 extra_count = 1;
1956 info->count--;
1958 restore_flags(flags);
1959 info->blocked_open++;
1960 while (1) {
1961 save_flags(flags); cli();
1962 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1963 (tty->termios->c_cflag & CBAUD))
1964 serial_out(info, UART_MCR,
1965 serial_inp(info, UART_MCR) |
1966 (UART_MCR_DTR | UART_MCR_RTS));
1967 restore_flags(flags);
1968 set_current_state(TASK_INTERRUPTIBLE);
1969 if (tty_hung_up_p(filp) ||
1970 !(info->flags & ASYNC_INITIALIZED)) {
1971 #ifdef SERIAL_DO_RESTART
1972 if (info->flags & ASYNC_HUP_NOTIFY)
1973 retval = -EAGAIN;
1974 else
1975 retval = -ERESTARTSYS;
1976 #else
1977 retval = -EAGAIN;
1978 #endif
1979 break;
1981 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1982 !(info->flags & ASYNC_CLOSING) &&
1983 (do_clocal || (serial_in(info, UART_MSR) &
1984 UART_MSR_DCD)))
1985 break;
1986 if (signal_pending(current)) {
1987 retval = -ERESTARTSYS;
1988 break;
1990 #ifdef SERIAL_DEBUG_OPEN
1991 printk("block_til_ready blocking: ttys%d, count = %d\n",
1992 info->line, info->count);
1993 #endif
1994 schedule();
1996 current->state = TASK_RUNNING;
1997 remove_wait_queue(&info->open_wait, &wait);
1998 if (extra_count)
1999 info->count++;
2000 info->blocked_open--;
2001 #ifdef SERIAL_DEBUG_OPEN
2002 printk("block_til_ready after blocking: ttys%d, count = %d\n",
2003 info->line, info->count);
2004 #endif
2005 if (retval)
2006 return retval;
2007 info->flags |= ASYNC_NORMAL_ACTIVE;
2008 return 0;
2012 * This routine is called whenever a serial port is opened. It
2013 * enables interrupts for a serial port, linking in its async structure into
2014 * the IRQ chain. It also performs the serial-specific
2015 * initialization for the tty structure.
2017 static int
2018 su_open(struct tty_struct *tty, struct file * filp)
2020 struct su_struct *info;
2021 int retval, line;
2022 unsigned long page;
2024 line = MINOR(tty->device) - tty->driver.minor_start;
2025 if ((line < 0) || (line >= NR_PORTS))
2026 return -ENODEV;
2027 info = su_table + line;
2028 info->count++;
2029 tty->driver_data = info;
2030 info->tty = tty;
2032 if (serial_paranoia_check(info, tty->device, "su_open")) {
2033 info->count--;
2034 return -ENODEV;
2037 #ifdef SERIAL_DEBUG_OPEN
2038 printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2039 info->count);
2040 #endif
2041 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2043 if (!tmp_buf) {
2044 page = get_free_page(GFP_KERNEL);
2045 if (!page)
2046 return -ENOMEM;
2047 if (tmp_buf)
2048 free_page(page);
2049 else
2050 tmp_buf = (unsigned char *) page;
2054 * If the port is the middle of closing, bail out now
2056 if (tty_hung_up_p(filp) ||
2057 (info->flags & ASYNC_CLOSING)) {
2058 if (info->flags & ASYNC_CLOSING)
2059 interruptible_sleep_on(&info->close_wait);
2060 #ifdef SERIAL_DO_RESTART
2061 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2062 -EAGAIN : -ERESTARTSYS);
2063 #else
2064 return -EAGAIN;
2065 #endif
2069 * Start up serial port
2071 retval = startup(info);
2072 if (retval)
2073 return retval;
2075 MOD_INC_USE_COUNT;
2076 retval = block_til_ready(tty, filp, info);
2077 if (retval) {
2078 #ifdef SERIAL_DEBUG_OPEN
2079 printk("su_open returning after block_til_ready with %d\n",
2080 retval);
2081 #endif
2082 return retval;
2085 if ((info->count == 1) &&
2086 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2087 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2088 *tty->termios = info->normal_termios;
2089 else
2090 *tty->termios = info->callout_termios;
2091 change_speed(info, 0);
2093 #ifdef CONFIG_SERIAL_CONSOLE
2094 if (sercons.cflag && sercons.index == line) {
2095 tty->termios->c_cflag = sercons.cflag;
2096 sercons.cflag = 0;
2097 change_speed(info, 0);
2099 #endif
2100 info->session = current->session;
2101 info->pgrp = current->pgrp;
2103 #ifdef SERIAL_DEBUG_OPEN
2104 printk("su_open ttys%d successful...", info->line);
2105 #endif
2106 return 0;
2110 * /proc fs routines....
2112 static __inline__ int
2113 line_info(char *buf, struct su_struct *info)
2115 char stat_buf[30], control, status;
2116 int ret;
2117 unsigned long flags;
2119 ret = sprintf(buf, "%d: uart:%s port:%X irq:%s",
2120 info->line, uart_config[info->type].name,
2121 (int)info->port, __irq_itoa(info->irq));
2123 if (info->port == 0 || info->type == PORT_UNKNOWN) {
2124 ret += sprintf(buf+ret, "\n");
2125 return ret;
2129 * Figure out the current RS-232 lines
2131 save_flags(flags); cli();
2132 status = serial_in(info, UART_MSR);
2133 control = info ? info->MCR : serial_in(info, UART_MCR);
2134 restore_flags(flags);
2136 stat_buf[0] = 0;
2137 stat_buf[1] = 0;
2138 if (control & UART_MCR_RTS)
2139 strcat(stat_buf, "|RTS");
2140 if (status & UART_MSR_CTS)
2141 strcat(stat_buf, "|CTS");
2142 if (control & UART_MCR_DTR)
2143 strcat(stat_buf, "|DTR");
2144 if (status & UART_MSR_DSR)
2145 strcat(stat_buf, "|DSR");
2146 if (status & UART_MSR_DCD)
2147 strcat(stat_buf, "|CD");
2148 if (status & UART_MSR_RI)
2149 strcat(stat_buf, "|RI");
2151 if (info->quot) {
2152 ret += sprintf(buf+ret, " baud:%d",
2153 info->baud_base / info->quot);
2156 ret += sprintf(buf+ret, " tx:%d rx:%d",
2157 info->icount.tx, info->icount.rx);
2159 if (info->icount.frame)
2160 ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
2162 if (info->icount.parity)
2163 ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
2165 if (info->icount.brk)
2166 ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
2168 if (info->icount.overrun)
2169 ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
2172 * Last thing is the RS-232 status lines
2174 ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2175 return ret;
2178 int su_read_proc(char *page, char **start, off_t off, int count,
2179 int *eof, void *data)
2181 int i, len = 0;
2182 off_t begin = 0;
2184 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2185 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2186 len += line_info(page + len, &su_table[i]);
2187 if (len+begin > off+count)
2188 goto done;
2189 if (len+begin < off) {
2190 begin += len;
2191 len = 0;
2194 *eof = 1;
2195 done:
2196 if (off >= len+begin)
2197 return 0;
2198 *start = page + (begin-off);
2199 return ((count < begin+len-off) ? count : begin+len-off);
2203 * ---------------------------------------------------------------------
2204 * su_XXX_init() and friends
2206 * su_XXX_init() is called at boot-time to initialize the serial driver.
2207 * ---------------------------------------------------------------------
2211 * This routine prints out the appropriate serial driver version
2212 * number, and identifies which options were configured into this
2213 * driver.
2215 static __inline__ void __init show_su_version(void)
2217 char *revision = "$Revision: 1.28 $";
2218 char *version, *p;
2220 version = strchr(revision, ' ');
2221 strcpy(serial_version, ++version);
2222 p = strchr(serial_version, ' ');
2223 *p = '\0';
2224 printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2228 * This routine is called by su_{serial|kbd_ms}_init() to initialize a specific
2229 * serial port. It determines what type of UART chip this serial port is
2230 * using: 8250, 16450, 16550, 16550A. The important question is
2231 * whether or not this UART is a 16550A, since this will determine
2232 * whether or not we can use its FIFO features.
2234 static void
2235 autoconfig(struct su_struct *info)
2237 unsigned char status1, status2, scratch, scratch2;
2238 struct linux_ebus_device *dev = 0;
2239 struct linux_ebus *ebus;
2240 #ifndef __sparc_v9__
2241 struct linux_prom_registers reg0;
2242 #endif
2243 unsigned long flags;
2245 if (!info->port_node || !info->port_type)
2246 return;
2249 * First we look for Ebus-bases su's
2251 for_each_ebus(ebus) {
2252 for_each_ebusdev(dev, ebus) {
2253 if (dev->prom_node == info->port_node) {
2254 info->port = dev->resource[0].start;
2255 info->irq = dev->irqs[0];
2256 goto ebus_done;
2261 #ifdef __sparc_v9__
2263 * Not on Ebus, bailing.
2265 return;
2266 #else
2268 * Not on Ebus, must be OBIO.
2270 if (prom_getproperty(info->port_node, "reg",
2271 (char *)&reg0, sizeof(reg0)) == -1) {
2272 prom_printf("su: no \"reg\" property\n");
2273 return;
2275 prom_apply_obio_ranges(&reg0, 1);
2276 if ((info->port = (unsigned long) sparc_alloc_io(reg0.phys_addr,
2277 0, reg0.reg_size, "su-regs", reg0.which_io, 0)) == 0) {
2278 prom_printf("su: cannot map\n");
2279 return;
2283 * There is no intr property on MrCoffee, so hardwire it.
2285 info->irq = IRQ_4M(13);
2286 #endif
2288 ebus_done:
2290 #ifdef SERIAL_DEBUG_OPEN
2291 printk("Found 'su' at %016lx IRQ %s\n", info->port,
2292 __irq_itoa(info->irq));
2293 #endif
2295 info->magic = SERIAL_MAGIC;
2297 save_flags(flags); cli();
2300 * Do a simple existence test first; if we fail this, there's
2301 * no point trying anything else.
2303 * 0x80 is used as a nonsense port to prevent against false
2304 * positives due to ISA bus float. The assumption is that
2305 * 0x80 is a non-existent port; which should be safe since
2306 * include/asm/io.h also makes this assumption.
2308 scratch = serial_inp(info, UART_IER);
2309 serial_outp(info, UART_IER, 0);
2310 scratch2 = serial_inp(info, UART_IER);
2311 serial_outp(info, UART_IER, scratch);
2312 if (scratch2) {
2313 restore_flags(flags);
2314 return; /* We failed; there's nothing here */
2317 scratch = serial_inp(info, UART_MCR);
2318 serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2319 serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2320 status1 = serial_inp(info, UART_MSR) & 0xF0;
2321 serial_outp(info, UART_MCR, scratch);
2322 if (status1 != 0x90) {
2324 * This code fragment used to fail, now it fixed itself.
2325 * We keep the printout for a case.
2327 printk("su: loopback returned status 0x%02x\n", status1);
2328 restore_flags(flags);
2329 return;
2332 scratch2 = serial_in(info, UART_LCR);
2333 serial_outp(info, UART_LCR, 0xBF); /* set up for StarTech test */
2334 serial_outp(info, UART_EFR, 0); /* EFR is the same as FCR */
2335 serial_outp(info, UART_LCR, 0);
2336 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2337 scratch = serial_in(info, UART_IIR) >> 6;
2338 switch (scratch) {
2339 case 0:
2340 info->type = PORT_16450;
2341 break;
2342 case 1:
2343 info->type = PORT_UNKNOWN;
2344 break;
2345 case 2:
2346 info->type = PORT_16550;
2347 break;
2348 case 3:
2349 info->type = PORT_16550A;
2350 break;
2352 if (info->type == PORT_16550A) {
2353 /* Check for Startech UART's */
2354 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2355 if (serial_in(info, UART_EFR) == 0) {
2356 info->type = PORT_16650;
2357 } else {
2358 serial_outp(info, UART_LCR, 0xBF);
2359 if (serial_in(info, UART_EFR) == 0)
2360 info->type = PORT_16650V2;
2363 if (info->type == PORT_16550A) {
2364 /* Check for TI 16750 */
2365 serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2366 serial_outp(info, UART_FCR,
2367 UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2368 scratch = serial_in(info, UART_IIR) >> 5;
2369 if (scratch == 7) {
2370 serial_outp(info, UART_LCR, 0);
2371 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2372 scratch = serial_in(info, UART_IIR) >> 5;
2373 if (scratch == 6)
2374 info->type = PORT_16750;
2376 serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2378 serial_outp(info, UART_LCR, scratch2);
2379 if (info->type == PORT_16450) {
2380 scratch = serial_in(info, UART_SCR);
2381 serial_outp(info, UART_SCR, 0xa5);
2382 status1 = serial_in(info, UART_SCR);
2383 serial_outp(info, UART_SCR, 0x5a);
2384 status2 = serial_in(info, UART_SCR);
2385 serial_outp(info, UART_SCR, scratch);
2387 if ((status1 != 0xa5) || (status2 != 0x5a))
2388 info->type = PORT_8250;
2390 info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2392 if (info->type == PORT_UNKNOWN) {
2393 restore_flags(flags);
2394 return;
2397 sprintf(info->name, "su(%s)", su_typev[info->port_type]);
2400 * Reset the UART.
2402 serial_outp(info, UART_MCR, 0x00);
2403 serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2404 (void)serial_in(info, UART_RX);
2405 serial_outp(info, UART_IER, 0x00);
2407 restore_flags(flags);
2411 * The serial driver boot-time initialization code!
2413 int __init su_serial_init(void)
2415 int i;
2416 struct su_struct *info;
2418 init_bh(SERIAL_BH, do_serial_bh);
2419 show_su_version();
2421 /* Initialize the tty_driver structure */
2423 memset(&serial_driver, 0, sizeof(struct tty_driver));
2424 serial_driver.magic = TTY_DRIVER_MAGIC;
2425 serial_driver.driver_name = "su";
2426 serial_driver.name = "ttyS";
2427 serial_driver.major = TTY_MAJOR;
2428 serial_driver.minor_start = 64;
2429 serial_driver.num = NR_PORTS;
2430 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2431 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2432 serial_driver.init_termios = tty_std_termios;
2433 serial_driver.init_termios.c_cflag =
2434 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2435 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2436 serial_driver.refcount = &serial_refcount;
2437 serial_driver.table = serial_table;
2438 serial_driver.termios = serial_termios;
2439 serial_driver.termios_locked = serial_termios_locked;
2441 serial_driver.open = su_open;
2442 serial_driver.close = su_close;
2443 serial_driver.write = su_write;
2444 serial_driver.put_char = su_put_char;
2445 serial_driver.flush_chars = su_flush_chars;
2446 serial_driver.write_room = su_write_room;
2447 serial_driver.chars_in_buffer = su_chars_in_buffer;
2448 serial_driver.flush_buffer = su_flush_buffer;
2449 serial_driver.ioctl = su_ioctl;
2450 serial_driver.throttle = su_throttle;
2451 serial_driver.unthrottle = su_unthrottle;
2452 serial_driver.send_xchar = su_send_xchar;
2453 serial_driver.set_termios = su_set_termios;
2454 serial_driver.stop = su_stop;
2455 serial_driver.start = su_start;
2456 serial_driver.hangup = su_hangup;
2457 serial_driver.break_ctl = su_break;
2458 serial_driver.wait_until_sent = su_wait_until_sent;
2459 serial_driver.read_proc = su_read_proc;
2462 * The callout device is just like normal device except for
2463 * major number and the subtype code.
2465 callout_driver = serial_driver;
2466 callout_driver.name = "cua";
2467 callout_driver.major = TTYAUX_MAJOR;
2468 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2469 callout_driver.read_proc = 0;
2470 callout_driver.proc_entry = 0;
2472 if (tty_register_driver(&serial_driver))
2473 panic("Couldn't register regular su\n");
2474 if (tty_register_driver(&callout_driver))
2475 panic("Couldn't register callout su\n");
2477 for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2478 info->line = i;
2479 info->type = PORT_UNKNOWN;
2480 info->baud_base = BAUD_BASE;
2481 /* info->flags = 0; */
2482 info->custom_divisor = 0;
2483 info->close_delay = 5*HZ/10;
2484 info->closing_wait = 30*HZ;
2485 info->callout_termios = callout_driver.init_termios;
2486 info->normal_termios = serial_driver.init_termios;
2487 info->icount.cts = info->icount.dsr =
2488 info->icount.rng = info->icount.dcd = 0;
2489 info->icount.rx = info->icount.tx = 0;
2490 info->icount.frame = info->icount.parity = 0;
2491 info->icount.overrun = info->icount.brk = 0;
2492 info->tqueue.routine = do_softint;
2493 info->tqueue.data = info;
2494 info->cflag = serial_driver.init_termios.c_cflag;
2495 init_waitqueue_head(&info->open_wait);
2496 init_waitqueue_head(&info->close_wait);
2497 init_waitqueue_head(&info->delta_msr_wait);
2499 autoconfig(info);
2500 if (info->type == PORT_UNKNOWN)
2501 continue;
2503 printk(KERN_INFO "%s at 0x%lx (tty %d irq %s) is a %s\n",
2504 info->name, (long)info->port, i, __irq_itoa(info->irq),
2505 uart_config[info->type].name);
2508 return 0;
2511 int __init su_kbd_ms_init(void)
2513 int i;
2514 struct su_struct *info;
2516 show_su_version();
2518 for (i = 0, info = su_table; i < 2; i++, info++) {
2519 info->line = i;
2520 info->type = PORT_UNKNOWN;
2521 info->baud_base = BAUD_BASE;
2523 if (info->port_type == SU_PORT_KBD)
2524 info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2525 else
2526 info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2528 init_waitqueue_head(&info->open_wait);
2529 init_waitqueue_head(&info->close_wait);
2530 init_waitqueue_head(&info->delta_msr_wait);
2532 autoconfig(info);
2533 if (info->type == PORT_UNKNOWN)
2534 continue;
2536 printk(KERN_INFO "%s at 0x%lx (irq = %s) is a %s\n",
2537 info->name, info->port, __irq_itoa(info->irq),
2538 uart_config[info->type].name);
2540 startup(info);
2541 if (info->port_type == SU_PORT_KBD)
2542 keyboard_zsinit(su_put_char_kbd);
2543 else
2544 sun_mouse_zsinit();
2546 return 0;
2550 * We got several platforms which present 'su' in different parts
2551 * of device tree. 'su' may be found under obio, ebus, isa and pci.
2552 * We walk over the tree and find them wherever PROM hides them.
2554 void __init su_probe_any(struct su_probe_scan *t, int sunode)
2556 struct su_struct *info;
2557 int len;
2559 if (t->devices >= NR_PORTS) return;
2561 for (; sunode != 0; sunode = prom_getsibling(sunode)) {
2562 len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
2563 if (len <= 1) continue; /* Broken PROM node */
2564 if (strncmp(t->prop, "su", len) == 0 ||
2565 strncmp(t->prop, "serial", len) == 0 ||
2566 strncmp(t->prop, "su_pnp", len) == 0) {
2567 info = &su_table[t->devices];
2568 if (t->kbnode != 0 && sunode == t->kbnode) {
2569 t->kbx = t->devices;
2570 info->port_type = SU_PORT_KBD;
2571 } else if (t->msnode != 0 && sunode == t->msnode) {
2572 t->msx = t->devices;
2573 info->port_type = SU_PORT_MS;
2574 } else {
2575 info->port_type = SU_PORT_PORT;
2577 info->port_node = sunode;
2578 ++t->devices;
2579 } else {
2580 su_probe_any(t, prom_getchild(sunode));
2585 int __init su_probe (unsigned long *memory_start)
2587 int node;
2588 int len;
2589 struct su_probe_scan scan;
2592 * First, we scan the tree.
2594 scan.devices = 0;
2595 scan.msx = -1;
2596 scan.kbx = -1;
2597 scan.kbnode = 0;
2598 scan.msnode = 0;
2601 * Get the nodes for keyboard and mouse from 'aliases'...
2603 node = prom_getchild(prom_root_node);
2604 node = prom_searchsiblings(node, "aliases");
2605 if (node != 0) {
2607 len = prom_getproperty(node, "keyboard", scan.prop,SU_PROPSIZE);
2608 if (len > 0) {
2609 scan.prop[len] = 0;
2610 scan.kbnode = prom_finddevice(scan.prop);
2613 len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
2614 if (len > 0) {
2615 scan.prop[len] = 0;
2616 scan.msnode = prom_finddevice(scan.prop);
2620 su_probe_any(&scan, prom_getchild(prom_root_node));
2623 * Second, we process the special case of keyboard and mouse.
2625 * Currently if we got keyboard and mouse hooked to "su" ports
2626 * we do not use any possible remaining "su" as a serial port.
2627 * Thus, we ignore values of .msx and .kbx, then compact ports.
2628 * Those who want to address this issue need to merge
2629 * su_serial_init() and su_ms_kbd_init().
2631 if (scan.msx != -1 && scan.kbx != -1) {
2632 su_table[0].port_type = SU_PORT_MS;
2633 su_table[0].port_node = scan.msnode;
2634 su_table[1].port_type = SU_PORT_KBD;
2635 su_table[1].port_node = scan.kbnode;
2637 sunserial_setinitfunc(memory_start, su_kbd_ms_init);
2638 rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2639 sunkbd_setinitfunc(memory_start, sun_kbd_init);
2640 kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2641 kbd_ops.setledstate = sun_setledstate;
2642 kbd_ops.getledstate = sun_getledstate;
2643 kbd_ops.setkeycode = sun_setkeycode;
2644 kbd_ops.getkeycode = sun_getkeycode;
2645 #ifdef CONFIG_PCI
2646 sunkbd_install_keymaps(memory_start, sun_key_maps,
2647 sun_keymap_count, sun_func_buf, sun_func_table,
2648 sun_funcbufsize, sun_funcbufleft,
2649 sun_accent_table, sun_accent_table_size);
2650 #endif
2651 return 0;
2653 if (scan.msx != -1 || scan.kbx != -1) {
2654 printk("su_probe: cannot match keyboard and mouse, confused\n");
2655 return -ENODEV;
2658 if (scan.devices == 0)
2659 return -ENODEV;
2661 #ifdef CONFIG_SERIAL_CONSOLE
2663 * Console must be initiated after the generic initialization.
2664 * sunserial_setinitfunc inverts order, so call this before next one.
2666 sunserial_setinitfunc(memory_start, su_serial_console_init);
2667 #endif
2668 sunserial_setinitfunc(memory_start, su_serial_init);
2669 return 0;
2673 * ------------------------------------------------------------
2674 * Serial console driver
2675 * ------------------------------------------------------------
2677 #ifdef CONFIG_SERIAL_CONSOLE
2679 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2682 * Wait for transmitter & holding register to empty
2684 static __inline__ void
2685 wait_for_xmitr(struct su_struct *info)
2687 int lsr;
2688 unsigned int tmout = 1000000;
2690 do {
2691 lsr = su_inb(info, UART_LSR);
2692 if (--tmout == 0)
2693 break;
2694 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2698 * Print a string to the serial port trying not to disturb
2699 * any possible real use of the port...
2701 static void
2702 serial_console_write(struct console *co, const char *s,
2703 unsigned count)
2705 struct su_struct *info;
2706 int ier;
2707 unsigned i;
2709 info = su_table + co->index;
2711 * First save the IER then disable the interrupts
2713 ier = su_inb(info, UART_IER);
2714 su_outb(info, UART_IER, 0x00);
2717 * Now, do each character
2719 for (i = 0; i < count; i++, s++) {
2720 wait_for_xmitr(info);
2723 * Send the character out.
2724 * If a LF, also do CR...
2726 su_outb(info, UART_TX, *s);
2727 if (*s == 10) {
2728 wait_for_xmitr(info);
2729 su_outb(info, UART_TX, 13);
2734 * Finally, Wait for transmitter & holding register to empty
2735 * and restore the IER
2737 wait_for_xmitr(info);
2738 su_outb(info, UART_IER, ier);
2742 * Receive character from the serial port
2744 static int
2745 serial_console_wait_key(struct console *co)
2747 struct su_struct *info;
2748 int ier;
2749 int lsr;
2750 int c;
2752 info = su_table + co->index;
2755 * First save the IER then disable the interrupts so
2756 * that the real driver for the port does not get the
2757 * character.
2759 ier = su_inb(info, UART_IER);
2760 su_outb(info, UART_IER, 0x00);
2762 do {
2763 lsr = su_inb(info, UART_LSR);
2764 } while (!(lsr & UART_LSR_DR));
2765 c = su_inb(info, UART_RX);
2768 * Restore the interrupts
2770 su_outb(info, UART_IER, ier);
2772 return c;
2775 static kdev_t
2776 serial_console_device(struct console *c)
2778 return MKDEV(TTY_MAJOR, 64 + c->index);
2782 * Setup initial baud/bits/parity. We do two things here:
2783 * - construct a cflag setting for the first su_open()
2784 * - initialize the serial port
2785 * Return non-zero if we didn't find a serial port.
2787 static int __init serial_console_setup(struct console *co, char *options)
2789 struct su_struct *info;
2790 unsigned cval;
2791 int baud = 9600;
2792 int bits = 8;
2793 int parity = 'n';
2794 int cflag = CREAD | HUPCL | CLOCAL;
2795 int quot = 0;
2796 char *s;
2798 if (options) {
2799 baud = simple_strtoul(options, NULL, 10);
2800 s = options;
2801 while(*s >= '0' && *s <= '9')
2802 s++;
2803 if (*s) parity = *s++;
2804 if (*s) bits = *s - '0';
2808 * Now construct a cflag setting.
2810 switch(baud) {
2811 case 1200:
2812 cflag |= B1200;
2813 break;
2814 case 2400:
2815 cflag |= B2400;
2816 break;
2817 case 4800:
2818 cflag |= B4800;
2819 break;
2820 case 19200:
2821 cflag |= B19200;
2822 break;
2823 case 38400:
2824 cflag |= B38400;
2825 break;
2826 case 57600:
2827 cflag |= B57600;
2828 break;
2829 case 115200:
2830 cflag |= B115200;
2831 break;
2832 case 9600:
2833 default:
2834 cflag |= B9600;
2835 break;
2837 switch(bits) {
2838 case 7:
2839 cflag |= CS7;
2840 break;
2841 default:
2842 case 8:
2843 cflag |= CS8;
2844 break;
2846 switch(parity) {
2847 case 'o': case 'O':
2848 cflag |= PARODD;
2849 break;
2850 case 'e': case 'E':
2851 cflag |= PARENB;
2852 break;
2854 co->cflag = cflag;
2857 * Divisor, bytesize and parity
2859 info = su_table + co->index;
2860 quot = BAUD_BASE / baud;
2861 cval = cflag & (CSIZE | CSTOPB);
2862 #if defined(__powerpc__) || defined(__alpha__)
2863 cval >>= 8;
2864 #else /* !__powerpc__ && !__alpha__ */
2865 cval >>= 4;
2866 #endif /* !__powerpc__ && !__alpha__ */
2867 if (cflag & PARENB)
2868 cval |= UART_LCR_PARITY;
2869 if (!(cflag & PARODD))
2870 cval |= UART_LCR_EPAR;
2873 * Disable UART interrupts, set DTR and RTS high
2874 * and set speed.
2876 su_outb(info, UART_IER, 0);
2877 su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2878 su_outb(info, UART_LCR, cval | UART_LCR_DLAB); /* set DLAB */
2879 su_outb(info, UART_DLL, quot & 0xff); /* LS of divisor */
2880 su_outb(info, UART_DLM, quot >> 8); /* MS of divisor */
2881 su_outb(info, UART_LCR, cval); /* reset DLAB */
2882 info->quot = quot;
2885 * If we read 0xff from the LSR, there is no UART here.
2887 if (su_inb(info, UART_LSR) == 0xff)
2888 return -1;
2890 return 0;
2893 static struct console sercons = {
2894 "ttyS",
2895 serial_console_write,
2896 NULL,
2897 serial_console_device,
2898 serial_console_wait_key,
2899 NULL,
2900 serial_console_setup,
2901 CON_PRINTBUFFER,
2904 NULL
2908 * Register console.
2910 int __init su_serial_console_init(void)
2912 extern int con_is_present(void);
2914 if (con_is_present())
2915 return 0;
2916 if (serial_console == 0)
2917 return 0;
2918 if (su_table[0].port == 0 || su_table[0].port_node == 0)
2919 return 0;
2920 sercons.index = 0;
2921 register_console(&sercons);
2922 return 0;
2925 #endif /* CONFIG_SERIAL_CONSOLE */