* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / arch / mips / baget / vacserial.c
blobf099a9aed64e32d30232e073892f617bfc3fa7c0
1 /* $Id$
2 * vacserial.c: VAC UART serial driver
3 * This code stealed and adopted from linux/drivers/char/serial.c
4 * See that for author info
6 * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
7 */
9 #undef SERIAL_PARANOIA_CHECK
10 #define CONFIG_SERIAL_NOPAUSE_IO
11 #define SERIAL_DO_RESTART
13 #define CONFIG_SERIAL_SHARE_IRQ
15 /* Set of debugging defines */
17 #undef SERIAL_DEBUG_INTR
18 #undef SERIAL_DEBUG_OPEN
19 #undef SERIAL_DEBUG_FLOW
20 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
22 #define RS_STROBE_TIME (10*HZ)
23 #define RS_ISR_PASS_LIMIT 2 /* Beget is not a super-computer (old=256) */
25 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
27 #define SERIAL_INLINE
29 #if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
30 #define DBG_CNT(s) baget_printk("(%s):[%x] refc=%d, serc=%d, ttyc=%d-> %s\n", \
31 kdevname(tty->device),(info->flags),serial_refcount,info->count,tty->count,s)
32 #else
33 #define DBG_CNT(s)
34 #endif
36 #define QUAD_UART_SPEED /* Useful for Baget */
39 * End of serial driver configuration section.
42 #include <linux/config.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/signal.h>
46 #include <linux/sched.h>
47 #include <linux/timer.h>
48 #include <linux/interrupt.h>
49 #include <linux/tty.h>
50 #include <linux/tty_flip.h>
51 #include <linux/serial.h>
52 #include <linux/major.h>
53 #include <linux/string.h>
54 #include <linux/fcntl.h>
55 #include <linux/ptrace.h>
56 #include <linux/ioport.h>
57 #include <linux/mm.h>
58 #include <linux/malloc.h>
59 #include <linux/init.h>
60 #include <linux/delay.h>
61 #ifdef CONFIG_SERIAL_CONSOLE
62 #include <linux/console.h>
63 #endif
65 #include <asm/system.h>
66 #include <asm/io.h>
67 #include <asm/irq.h>
68 #include <asm/uaccess.h>
69 #include <asm/bitops.h>
70 #include <asm/serial.h>
71 #include <asm/baget/baget.h>
73 #define BAGET_VAC_UART_IRQ 0x35
76 * Implementation note:
77 * It was descovered by means of advanced electronic tools,
78 * if the driver works via TX_READY interrupts then VIC generates
79 * strange self-eliminating traps. Thus, the driver is rewritten to work
80 * via TX_EMPTY
83 /* VAC-specific check/debug switches */
85 #undef CHECK_REG_INDEX
86 #undef DEBUG_IO_PORT_A
88 #ifdef SERIAL_INLINE
89 #define _INLINE_ inline
90 #endif
92 static char *serial_name = "VAC Serial driver";
93 static char *serial_version = "4.26";
95 static DECLARE_TASK_QUEUE(tq_serial);
97 static struct tty_driver serial_driver, callout_driver;
98 static int serial_refcount;
100 /* number of characters left in xmit buffer before we ask for more */
101 #define WAKEUP_CHARS 256
104 * IRQ_timeout - How long the timeout should be for each IRQ
105 * should be after the IRQ has been active.
108 static struct async_struct *IRQ_ports[NR_IRQS];
109 static int IRQ_timeout[NR_IRQS];
110 #ifdef CONFIG_SERIAL_CONSOLE
111 static struct console sercons;
112 #endif
114 static void autoconfig(struct serial_state * info);
115 static void change_speed(struct async_struct *info);
116 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
119 * Here we define the default xmit fifo size used for each type of
120 * UART
122 static struct serial_uart_config uart_config[] = {
123 { "unknown", 1, 0 }, /* Must go first -- used as unasigned */
124 { "VAC UART", 1, 0 }
126 #define VAC_UART_TYPE 1 /* Just index in above array */
128 static struct serial_state rs_table[] = {
130 * VAC has tricky layout for pair of his SIO registers,
131 * so we need special function to access ones.
132 * To identify port we use their TX offset
134 { 0, 9600, VAC_UART_B_TX, BAGET_VAC_UART_IRQ,
135 STD_COM_FLAGS }, /* VAC UART B */
136 { 0, 9600, VAC_UART_A_TX, BAGET_VAC_UART_IRQ,
137 STD_COM_FLAGS } /* VAC UART A */
140 #define NR_PORTS (sizeof(rs_table)/sizeof(struct serial_state))
142 static struct tty_struct *serial_table[NR_PORTS];
143 static struct termios *serial_termios[NR_PORTS];
144 static struct termios *serial_termios_locked[NR_PORTS];
146 #ifndef MIN
147 #define MIN(a,b) ((a) < (b) ? (a) : (b))
148 #endif
151 * tmp_buf is used as a temporary buffer by serial_write. We need to
152 * lock it in case the copy_from_user blocks while swapping in a page,
153 * and some other program tries to do a serial write at the same time.
154 * Since the lock will only come under contention when the system is
155 * swapping and available memory is low, it makes sense to share one
156 * buffer across all the serial ports, since it significantly saves
157 * memory if large numbers of serial ports are open.
159 static unsigned char *tmp_buf;
160 static struct semaphore tmp_buf_sem = MUTEX;
162 static inline int serial_paranoia_check(struct async_struct *info,
163 kdev_t device, const char *routine)
165 #ifdef SERIAL_PARANOIA_CHECK
166 static const char *badmagic =
167 "Warning: bad magic number for serial struct (%s) in %s\n";
168 static const char *badinfo =
169 "Warning: null async_struct for (%s) in %s\n";
171 if (!info) {
172 printk(badinfo, kdevname(device), routine);
173 return 1;
175 if (info->magic != SERIAL_MAGIC) {
176 printk(badmagic, kdevname(device), routine);
177 return 1;
179 #endif
180 return 0;
184 To unify UART A/B access we will use following function
185 to compute register offsets by register index.
188 #define VAC_UART_MODE 0
189 #define VAC_UART_TX 1
190 #define VAC_UART_RX 2
191 #define VAC_UART_INT_MASK 3
192 #define VAC_UART_INT_STATUS 4
194 #define VAC_UART_REG_NR 5
196 static inline int uart_offset_map(unsigned long port, int reg_index)
198 static const unsigned int ind_to_reg[VAC_UART_REG_NR][NR_PORTS] = {
199 { VAC_UART_B_MODE, VAC_UART_A_MODE },
200 { VAC_UART_B_TX, VAC_UART_A_TX },
201 { VAC_UART_B_RX, VAC_UART_A_RX },
202 { VAC_UART_B_INT_MASK, VAC_UART_A_INT_MASK },
203 { VAC_UART_B_INT_STATUS, VAC_UART_A_INT_STATUS }
205 #ifdef CHECK_REG_INDEX
206 if (reg_index > VAC_UART_REG_NR) panic("vacserial: bad reg_index");
207 #endif
208 return ind_to_reg[reg_index][port == VAC_UART_B_TX ? 0 : 1];
211 static inline unsigned int serial_inw(struct async_struct *info, int offset)
213 int val = vac_inw(uart_offset_map(info->port,offset));
214 #ifdef DEBUG_IO_PORT_A
215 if (info->port == VAC_UART_A_TX)
216 printk("UART_A_IN: reg = 0x%04x, val = 0x%04x\n",
217 uart_offset_map(info->port,offset), val);
218 #endif
219 return val;
222 static inline unsigned int serial_inp(struct async_struct *info, int offset)
224 return serial_inw(info, offset);
227 static inline unsigned int serial_in(struct async_struct *info, int offset)
229 return serial_inw(info, offset);
232 static inline void serial_outw(struct async_struct *info,int offset, int value)
234 #ifdef DEBUG_IO_PORT_A
235 if (info->port == VAC_UART_A_TX)
236 printk("UART_A_OUT: offset = 0x%04x, val = 0x%04x\n",
237 uart_offset_map(info->port,offset), value);
238 #endif
239 vac_outw(value, uart_offset_map(info->port,offset));
242 static inline void serial_outp(struct async_struct *info,int offset, int value)
244 serial_outw(info,offset,value);
247 static inline void serial_out(struct async_struct *info,int offset, int value)
249 serial_outw(info,offset,value);
253 * ------------------------------------------------------------
254 * rs_stop() and rs_start()
256 * This routines are called before setting or resetting tty->stopped.
257 * They enable or disable transmitter interrupts, as necessary.
258 * ------------------------------------------------------------
260 static void rs_stop(struct tty_struct *tty)
262 struct async_struct *info = (struct async_struct *)tty->driver_data;
263 unsigned long flags;
265 if (serial_paranoia_check(info, tty->device, "rs_stop"))
266 return;
268 save_flags(flags); cli();
269 if (info->IER & VAC_UART_INT_TX_EMPTY) {
270 info->IER &= ~VAC_UART_INT_TX_EMPTY;
271 serial_out(info, VAC_UART_INT_MASK, info->IER);
273 restore_flags(flags);
276 static void rs_start(struct tty_struct *tty)
278 struct async_struct *info = (struct async_struct *)tty->driver_data;
279 unsigned long flags;
281 if (serial_paranoia_check(info, tty->device, "rs_start"))
282 return;
284 save_flags(flags); cli();
285 if (info->xmit_cnt && info->xmit_buf
286 && !(info->IER & VAC_UART_INT_TX_EMPTY)) {
287 info->IER |= VAC_UART_INT_TX_EMPTY;
288 serial_out(info, VAC_UART_INT_MASK, info->IER);
290 restore_flags(flags);
294 * ----------------------------------------------------------------------
296 * Here starts the interrupt handling routines. All of the following
297 * subroutines are declared as inline and are folded into
298 * rs_interrupt(). They were separated out for readability's sake.
300 * Note: rs_interrupt() is a "fast" interrupt, which means that it
301 * runs with interrupts turned off. People who may want to modify
302 * rs_interrupt() should try to keep the interrupt handler as fast as
303 * possible. After you are done making modifications, it is not a bad
304 * idea to do:
306 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
308 * and look at the resulting assemble code in serial.s.
310 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
311 * -----------------------------------------------------------------------
315 * This routine is used by the interrupt handler to schedule
316 * processing in the software interrupt portion of the driver.
318 static _INLINE_ void rs_sched_event(struct async_struct *info,
319 int event)
321 info->event |= 1 << event;
322 queue_task(&info->tqueue, &tq_serial);
323 mark_bh(SERIAL_BH);
326 static _INLINE_ void receive_chars(struct async_struct *info,
327 int *status)
329 struct tty_struct *tty = info->tty;
330 unsigned short rx;
331 unsigned char ch;
332 int ignored = 0;
333 struct async_icount *icount;
335 icount = &info->state->icount;
336 do {
337 rx = serial_inw(info, VAC_UART_RX);
338 ch = VAC_UART_RX_DATA_MASK & rx;
340 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
341 break;
342 *tty->flip.char_buf_ptr = ch;
343 icount->rx++;
345 #ifdef SERIAL_DEBUG_INTR
346 baget_printk("DR%02x:%02x...", rx, *status);
347 #endif
348 *tty->flip.flag_buf_ptr = 0;
349 if (*status & (VAC_UART_STATUS_RX_BREAK_CHANGE
350 | VAC_UART_STATUS_RX_ERR_PARITY
351 | VAC_UART_STATUS_RX_ERR_FRAME
352 | VAC_UART_STATUS_RX_ERR_OVERRUN)) {
354 * For statistics only
356 if (*status & VAC_UART_STATUS_RX_BREAK_CHANGE) {
357 *status &= ~(VAC_UART_STATUS_RX_ERR_FRAME
358 | VAC_UART_STATUS_RX_ERR_PARITY);
359 icount->brk++;
360 } else if (*status & VAC_UART_STATUS_RX_ERR_PARITY)
361 icount->parity++;
362 else if (*status & VAC_UART_STATUS_RX_ERR_FRAME)
363 icount->frame++;
364 if (*status & VAC_UART_STATUS_RX_ERR_OVERRUN)
365 icount->overrun++;
368 * Now check to see if character should be
369 * ignored, and mask off conditions which
370 * should be ignored.
372 if (*status & info->ignore_status_mask) {
373 if (++ignored > 100)
374 break;
375 goto ignore_char;
377 *status &= info->read_status_mask;
379 if (*status & (VAC_UART_STATUS_RX_BREAK_CHANGE)) {
380 #ifdef SERIAL_DEBUG_INTR
381 baget_printk("handling break....");
382 #endif
383 *tty->flip.flag_buf_ptr = TTY_BREAK;
384 if (info->flags & ASYNC_SAK)
385 do_SAK(tty);
386 } else if (*status & VAC_UART_STATUS_RX_ERR_PARITY)
387 *tty->flip.flag_buf_ptr = TTY_PARITY;
388 else if (*status & VAC_UART_STATUS_RX_ERR_FRAME)
389 *tty->flip.flag_buf_ptr = TTY_FRAME;
390 if (*status & VAC_UART_STATUS_RX_ERR_OVERRUN) {
392 * Overrun is special, since it's
393 * reported immediately, and doesn't
394 * affect the current character
396 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
397 tty->flip.count++;
398 tty->flip.flag_buf_ptr++;
399 tty->flip.char_buf_ptr++;
400 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
404 tty->flip.flag_buf_ptr++;
405 tty->flip.char_buf_ptr++;
406 tty->flip.count++;
407 ignore_char:
408 *status = serial_inw(info, VAC_UART_INT_STATUS);
409 } while ((*status & VAC_UART_STATUS_RX_READY));
410 tty_flip_buffer_push(tty);
413 static _INLINE_ void transmit_chars(struct async_struct *info, int *intr_done)
415 int count;
417 if (info->x_char) {
418 serial_outw(info, VAC_UART_TX,
419 (((unsigned short)info->x_char)<<8));
420 info->state->icount.tx++;
421 info->x_char = 0;
422 if (intr_done)
423 *intr_done = 0;
424 return;
426 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
427 info->tty->hw_stopped) {
428 info->IER &= ~VAC_UART_INT_TX_EMPTY;
429 serial_outw(info, VAC_UART_INT_MASK, info->IER);
430 return;
432 count = info->xmit_fifo_size;
433 do {
434 serial_out(info, VAC_UART_TX,
435 (unsigned short)info->xmit_buf[info->xmit_tail++] \
436 << 8);
437 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
438 info->state->icount.tx++;
439 if (--info->xmit_cnt <= 0)
440 break;
441 } while (--count > 0);
443 if (info->xmit_cnt < WAKEUP_CHARS)
444 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
446 #ifdef SERIAL_DEBUG_INTR
447 baget_printk("THRE...");
448 #endif
449 if (intr_done)
450 *intr_done = 0;
452 if (info->xmit_cnt <= 0) {
453 info->IER &= ~VAC_UART_INT_TX_EMPTY;
454 serial_outw(info, VAC_UART_INT_MASK, info->IER);
458 static _INLINE_ void check_modem_status(struct async_struct *info)
460 #if 0 /* VAC hasn't modem control */
461 wake_up_interruptible(&info->open_wait);
462 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
463 #endif
466 #ifdef CONFIG_SERIAL_SHARE_IRQ
470 * Specific functions needed for VAC UART interrupt enter/leave
473 #define VAC_INT_CTRL_UART_ENABLE \
474 (VAC_INT_CTRL_TIMER_PIO10|VAC_INT_CTRL_UART_B_PIO7|VAC_INT_CTRL_UART_A_PIO7)
476 #define VAC_INT_CTRL_UART_DISABLE(info) \
477 (VAC_INT_CTRL_TIMER_PIO10 | \
478 ((info->port == VAC_UART_A_TX) ? \
479 (VAC_INT_CTRL_UART_A_DISABLE|VAC_INT_CTRL_UART_B_PIO7) : \
480 (VAC_INT_CTRL_UART_A_PIO7|VAC_INT_CTRL_UART_B_DISABLE)))
483 * Following two functions were proposed by Pavel Osipenko
484 * to make VAC/VIC behaviour more regular.
486 static void intr_begin(struct async_struct* info)
488 serial_outw(info, VAC_UART_INT_MASK, 0);
491 static void intr_end(struct async_struct* info)
493 vac_outw(VAC_INT_CTRL_UART_DISABLE(info), VAC_INT_CTRL);
494 vac_outw(VAC_INT_CTRL_UART_ENABLE, VAC_INT_CTRL);
496 serial_outw(info, VAC_UART_INT_MASK, info->IER);
500 * This is the serial driver's generic interrupt routine
502 static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
504 int status;
505 struct async_struct * info;
506 int pass_counter = 0;
507 struct async_struct *end_mark = 0;
509 #ifdef SERIAL_DEBUG_INTR
510 baget_printk("rs_interrupt(%d)...", irq);
511 #endif
513 info = IRQ_ports[irq];
514 if (!info)
515 return;
517 do {
518 intr_begin(info); /* Mark we begin port handling */
520 if (!info->tty ||
521 (serial_inw (info, VAC_UART_INT_STATUS)
522 & VAC_UART_STATUS_INTS) == 0)
524 if (!end_mark)
525 end_mark = info;
526 goto next;
528 end_mark = 0;
530 info->last_active = jiffies;
532 status = serial_inw(info, VAC_UART_INT_STATUS);
533 #ifdef SERIAL_DEBUG_INTR
534 baget_printk("status = %x...", status);
535 #endif
536 if (status & VAC_UART_STATUS_RX_READY) {
537 receive_chars(info, &status);
539 check_modem_status(info);
540 if (status & VAC_UART_STATUS_TX_EMPTY)
541 transmit_chars(info, 0);
543 next:
544 intr_end(info); /* Mark this port handled */
546 info = info->next_port;
547 if (!info) {
548 info = IRQ_ports[irq];
549 if (pass_counter++ > RS_ISR_PASS_LIMIT) {
550 break; /* Prevent infinite loops */
552 continue;
554 } while (end_mark != info);
555 #ifdef SERIAL_DEBUG_INTR
556 baget_printk("end.\n");
557 #endif
561 #endif /* #ifdef CONFIG_SERIAL_SHARE_IRQ */
564 /* The original driver was simplified here:
565 two functions were joined to reduce code */
567 #define rs_interrupt_single rs_interrupt
571 * -------------------------------------------------------------------
572 * Here ends the serial interrupt routines.
573 * -------------------------------------------------------------------
577 * This routine is used to handle the "bottom half" processing for the
578 * serial driver, known also the "software interrupt" processing.
579 * This processing is done at the kernel interrupt level, after the
580 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
581 * is where time-consuming activities which can not be done in the
582 * interrupt driver proper are done; the interrupt driver schedules
583 * them using rs_sched_event(), and they get done here.
585 static void do_serial_bh(void)
587 run_task_queue(&tq_serial);
590 static void do_softint(void *private_)
592 struct async_struct *info = (struct async_struct *) private_;
593 struct tty_struct *tty;
595 tty = info->tty;
596 if (!tty)
597 return;
599 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
600 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
601 tty->ldisc.write_wakeup)
602 (tty->ldisc.write_wakeup)(tty);
603 wake_up_interruptible(&tty->write_wait);
608 * ---------------------------------------------------------------
609 * Low level utility subroutines for the serial driver: routines to
610 * figure out the appropriate timeout for an interrupt chain, routines
611 * to initialize and startup a serial port, and routines to shutdown a
612 * serial port. Useful stuff like that.
613 * ---------------------------------------------------------------
617 * This routine figures out the correct timeout for a particular IRQ.
618 * It uses the smallest timeout of all of the serial ports in a
619 * particular interrupt chain. Now only used for IRQ 0....
621 static void figure_IRQ_timeout(int irq)
623 struct async_struct *info;
624 int timeout = 60*HZ; /* 60 seconds === a long time :-) */
626 info = IRQ_ports[irq];
627 if (!info) {
628 IRQ_timeout[irq] = 60*HZ;
629 return;
631 while (info) {
632 if (info->timeout < timeout)
633 timeout = info->timeout;
634 info = info->next_port;
636 if (!irq)
637 timeout = timeout / 2;
638 IRQ_timeout[irq] = timeout ? timeout : 1;
641 static int startup(struct async_struct * info)
643 unsigned long flags;
644 int retval=0;
645 void (*handler)(int, void *, struct pt_regs *);
646 struct serial_state *state= info->state;
647 unsigned long page;
649 page = get_free_page(GFP_KERNEL);
650 if (!page)
651 return -ENOMEM;
653 save_flags(flags); cli();
655 if (info->flags & ASYNC_INITIALIZED) {
656 free_page(page);
657 goto errout;
659 if (!state->port || !state->type) {
660 if (info->tty)
661 set_bit(TTY_IO_ERROR, &info->tty->flags);
662 free_page(page);
663 goto errout;
665 if (info->xmit_buf)
666 free_page(page);
667 else
668 info->xmit_buf = (unsigned char *) page;
670 #ifdef SERIAL_DEBUG_OPEN
671 baget_printk("starting up ttys%d (irq %d)...", info->line, state->irq);
672 #endif
674 if (uart_config[info->state->type].flags & UART_STARTECH) {
675 /* Wake up UART */
676 serial_outp(info, VAC_UART_MODE, 0);
677 serial_outp(info, VAC_UART_INT_MASK, 0);
681 * Allocate the IRQ if necessary
683 if (state->irq && (!IRQ_ports[state->irq] ||
684 !IRQ_ports[state->irq]->next_port)) {
686 if (IRQ_ports[state->irq]) {
687 #ifdef CONFIG_SERIAL_SHARE_IRQ
688 free_irq(state->irq, NULL);
689 handler = rs_interrupt;
690 #else
691 retval = -EBUSY;
692 goto errout;
693 #endif /* CONFIG_SERIAL_SHARE_IRQ */
694 } else
695 handler = rs_interrupt_single;
698 retval = request_irq(state->irq, handler, IRQ_T(info),
699 "serial", NULL);
700 if (retval) {
701 if (capable(CAP_SYS_ADMIN)) {
702 if (info->tty)
703 set_bit(TTY_IO_ERROR,
704 &info->tty->flags);
705 retval = 0;
707 goto errout;
712 * Insert serial port into IRQ chain.
714 info->prev_port = 0;
715 info->next_port = IRQ_ports[state->irq];
716 if (info->next_port)
717 info->next_port->prev_port = info;
718 IRQ_ports[state->irq] = info;
719 figure_IRQ_timeout(state->irq);
722 * Clear the interrupt registers.
724 /* (void) serial_inw(info, VAC_UART_INT_STATUS); */ /* (see above) */
725 (void) serial_inw(info, VAC_UART_RX);
728 * Now, initialize the UART
730 serial_outp(info, VAC_UART_MODE, VAC_UART_MODE_INITIAL); /*reset DLAB*/
733 * Finally, enable interrupts
735 info->IER = VAC_UART_INT_RX_BREAK_CHANGE | VAC_UART_INT_RX_ERRS | \
736 VAC_UART_INT_RX_READY;
737 serial_outp(info, VAC_UART_INT_MASK, info->IER); /*enable interrupts*/
740 * And clear the interrupt registers again for luck.
742 (void)serial_inp(info, VAC_UART_INT_STATUS);
743 (void)serial_inp(info, VAC_UART_RX);
745 if (info->tty)
746 clear_bit(TTY_IO_ERROR, &info->tty->flags);
747 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
750 * Set up serial timers...
752 timer_table[RS_TIMER].expires = jiffies + 2*HZ/100;
753 timer_active |= 1 << RS_TIMER;
756 * and set the speed of the serial port
758 change_speed(info);
760 info->flags |= ASYNC_INITIALIZED;
761 restore_flags(flags);
762 return 0;
764 errout:
765 restore_flags(flags);
766 return retval;
770 * This routine will shutdown a serial port; interrupts are disabled, and
771 * DTR is dropped if the hangup on close termio flag is on.
773 static void shutdown(struct async_struct * info)
775 unsigned long flags;
776 struct serial_state *state;
777 int retval;
779 if (!(info->flags & ASYNC_INITIALIZED))
780 return;
782 state = info->state;
784 #ifdef SERIAL_DEBUG_OPEN
785 baget_printk("Shutting down serial port %d (irq %d)....", info->line,
786 state->irq);
787 #endif
789 save_flags(flags); cli(); /* Disable interrupts */
792 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
793 * here so the queue might never be waken up
795 wake_up_interruptible(&info->delta_msr_wait);
798 * First unlink the serial port from the IRQ chain...
800 if (info->next_port)
801 info->next_port->prev_port = info->prev_port;
802 if (info->prev_port)
803 info->prev_port->next_port = info->next_port;
804 else
805 IRQ_ports[state->irq] = info->next_port;
806 figure_IRQ_timeout(state->irq);
809 * Free the IRQ, if necessary
811 if (state->irq && (!IRQ_ports[state->irq] ||
812 !IRQ_ports[state->irq]->next_port)) {
813 if (IRQ_ports[state->irq]) {
814 free_irq(state->irq, NULL);
815 retval = request_irq(state->irq, rs_interrupt_single,
816 IRQ_T(info), "serial", NULL);
818 if (retval)
819 printk("serial shutdown: request_irq: error %d"
820 " Couldn't reacquire IRQ.\n", retval);
821 } else
822 free_irq(state->irq, NULL);
825 if (info->xmit_buf) {
826 free_page((unsigned long) info->xmit_buf);
827 info->xmit_buf = 0;
830 info->IER = 0;
831 serial_outp(info, VAC_UART_INT_MASK, 0x00); /* disable all intrs */
833 /* disable break condition */
834 serial_out(info, VAC_UART_MODE, serial_inp(info, VAC_UART_MODE) & \
835 ~VAC_UART_MODE_SEND_BREAK);
837 if (info->tty)
838 set_bit(TTY_IO_ERROR, &info->tty->flags);
840 info->flags &= ~ASYNC_INITIALIZED;
841 restore_flags(flags);
845 * When we set line mode, we call this function
846 * for Baget-specific adjustments.
849 static inline unsigned short vac_uart_mode_fixup (unsigned short cval)
851 #ifdef QUAD_UART_SPEED
853 * When we are using 4-x advantage in speed:
855 * Disadvantage : can't support 75, 150 bauds
856 * Advantage : can support 19200, 38400 bauds
858 char speed = 7 & (cval >> 10);
859 cval &= ~(7 << 10);
860 cval |= VAC_UART_MODE_BAUD(speed-2);
861 #endif
864 * In general, we have Tx and Rx ON all time
865 * and use int mask flag for their disabling.
867 cval |= VAC_UART_MODE_RX_ENABLE;
868 cval |= VAC_UART_MODE_TX_ENABLE;
869 cval |= VAC_UART_MODE_CHAR_RX_ENABLE;
870 cval |= VAC_UART_MODE_CHAR_TX_ENABLE;
872 /* Low 4 bits are not used in UART */
873 cval &= ~0xf;
875 return cval;
879 * This routine is called to set the UART divisor registers to match
880 * the specified baud rate for a serial port.
882 static void change_speed(struct async_struct *info)
884 unsigned short port;
885 int quot = 0, baud_base, baud;
886 unsigned cflag, cval;
887 int bits;
888 unsigned long flags;
890 if (!info->tty || !info->tty->termios)
891 return;
892 cflag = info->tty->termios->c_cflag;
893 if (!(port = info->port))
894 return;
896 /* byte size and parity */
897 switch (cflag & CSIZE) {
898 case CS7: cval = 0x0; bits = 9; break;
899 case CS8: cval = VAC_UART_MODE_8BIT_CHAR; bits = 10; break;
900 /* Never happens, but GCC is too dumb to figure it out */
901 case CS5:
902 case CS6:
903 default: cval = 0x0; bits = 9; break;
905 cval &= ~VAC_UART_MODE_PARITY_ENABLE;
906 if (cflag & PARENB) {
907 cval |= VAC_UART_MODE_PARITY_ENABLE;
908 bits++;
910 if (cflag & PARODD)
911 cval |= VAC_UART_MODE_PARITY_ODD;
913 /* Determine divisor based on baud rate */
914 baud = tty_get_baud_rate(info->tty);
915 baud_base = info->state->baud_base;
916 if (baud == 38400 &&
917 ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
918 quot = info->state->custom_divisor;
919 else {
920 if (baud == 134)
921 /* Special case since 134 is really 134.5 */
922 quot = (2*baud_base / 269);
923 else if (baud)
924 quot = baud_base / baud;
926 /* If the quotient is ever zero, default to 9600 bps */
927 if (!quot)
928 quot = baud_base / 9600;
929 info->quot = quot;
930 info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / baud_base);
931 info->timeout += HZ/50; /* Add .02 seconds of slop */
933 serial_out(info, VAC_UART_INT_MASK, info->IER);
936 * Set up parity check flag
938 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
940 info->read_status_mask = VAC_UART_STATUS_RX_ERR_OVERRUN | \
941 VAC_UART_STATUS_TX_EMPTY | VAC_UART_STATUS_RX_READY;
942 if (I_INPCK(info->tty))
943 info->read_status_mask |= VAC_UART_STATUS_RX_ERR_FRAME | \
944 VAC_UART_STATUS_RX_ERR_PARITY;
945 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
946 info->read_status_mask |= VAC_UART_STATUS_RX_BREAK_CHANGE;
949 * Characters to ignore
951 info->ignore_status_mask = 0;
952 if (I_IGNPAR(info->tty))
953 info->ignore_status_mask |= VAC_UART_STATUS_RX_ERR_PARITY | \
954 VAC_UART_STATUS_RX_ERR_FRAME;
955 if (I_IGNBRK(info->tty)) {
956 info->ignore_status_mask |= VAC_UART_STATUS_RX_BREAK_CHANGE;
958 * If we're ignore parity and break indicators, ignore
959 * overruns too. (For real raw support).
961 if (I_IGNPAR(info->tty))
962 info->ignore_status_mask |= \
963 VAC_UART_STATUS_RX_ERR_OVERRUN;
966 * !!! ignore all characters if CREAD is not set
968 if ((cflag & CREAD) == 0)
969 info->ignore_status_mask |= VAC_UART_STATUS_RX_READY;
970 save_flags(flags); cli();
973 switch (baud) {
974 default:
975 case 9600:
976 cval |= VAC_UART_MODE_BAUD(7);
977 break;
978 case 4800:
979 cval |= VAC_UART_MODE_BAUD(6);
980 break;
981 case 2400:
982 cval |= VAC_UART_MODE_BAUD(5);
983 break;
984 case 1200:
985 cval |= VAC_UART_MODE_BAUD(4);
986 break;
987 case 600:
988 cval |= VAC_UART_MODE_BAUD(3);
989 break;
990 case 300:
991 cval |= VAC_UART_MODE_BAUD(2);
992 break;
993 #ifndef QUAD_UART_SPEED
994 case 150:
995 #else
996 case 38400:
997 #endif
998 cval |= VAC_UART_MODE_BAUD(1);
999 break;
1000 #ifndef QUAD_UART_SPEED
1001 case 75:
1002 #else
1003 case 19200:
1004 #endif
1005 cval |= VAC_UART_MODE_BAUD(0);
1006 break;
1009 /* Baget VAC need some adjustments for computed value */
1010 cval = vac_uart_mode_fixup(cval);
1012 serial_outp(info, VAC_UART_MODE, cval);
1013 restore_flags(flags);
1016 static void rs_put_char(struct tty_struct *tty, unsigned char ch)
1018 struct async_struct *info = (struct async_struct *)tty->driver_data;
1019 unsigned long flags;
1021 if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1022 return;
1024 if (!tty || !info->xmit_buf)
1025 return;
1027 save_flags(flags); cli();
1028 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1029 restore_flags(flags);
1030 return;
1033 info->xmit_buf[info->xmit_head++] = ch;
1034 info->xmit_head &= SERIAL_XMIT_SIZE-1;
1035 info->xmit_cnt++;
1036 restore_flags(flags);
1039 static void rs_flush_chars(struct tty_struct *tty)
1041 struct async_struct *info = (struct async_struct *)tty->driver_data;
1042 unsigned long flags;
1044 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
1045 return;
1047 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1048 !info->xmit_buf)
1049 return;
1051 save_flags(flags); cli();
1052 info->IER |= VAC_UART_INT_TX_EMPTY;
1053 serial_out(info, VAC_UART_INT_MASK, info->IER);
1054 restore_flags(flags);
1057 static int rs_write(struct tty_struct * tty, int from_user,
1058 const unsigned char *buf, int count)
1060 int c, ret = 0;
1061 struct async_struct *info = (struct async_struct *)tty->driver_data;
1062 unsigned long flags;
1064 if (serial_paranoia_check(info, tty->device, "rs_write"))
1065 return 0;
1067 if (!tty || !info->xmit_buf || !tmp_buf)
1068 return 0;
1070 save_flags(flags);
1071 if (from_user) {
1072 down(&tmp_buf_sem);
1073 while (1) {
1074 c = MIN(count,
1075 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1076 SERIAL_XMIT_SIZE - info->xmit_head));
1077 if (c <= 0)
1078 break;
1080 c -= copy_from_user(tmp_buf, buf, c);
1081 if (!c) {
1082 if (!ret)
1083 ret = -EFAULT;
1084 break;
1086 cli();
1087 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1088 SERIAL_XMIT_SIZE - info->xmit_head));
1089 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1090 info->xmit_head = ((info->xmit_head + c) &
1091 (SERIAL_XMIT_SIZE-1));
1092 info->xmit_cnt += c;
1093 restore_flags(flags);
1094 buf += c;
1095 count -= c;
1096 ret += c;
1098 up(&tmp_buf_sem);
1099 } else {
1100 while (1) {
1101 cli();
1102 c = MIN(count,
1103 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1104 SERIAL_XMIT_SIZE - info->xmit_head));
1105 if (c <= 0) {
1106 restore_flags(flags);
1107 break;
1109 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1110 info->xmit_head = ((info->xmit_head + c) &
1111 (SERIAL_XMIT_SIZE-1));
1112 info->xmit_cnt += c;
1113 restore_flags(flags);
1114 buf += c;
1115 count -= c;
1116 ret += c;
1119 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1120 !(info->IER & VAC_UART_INT_TX_EMPTY)) {
1121 info->IER |= VAC_UART_INT_TX_EMPTY;
1122 serial_out(info, VAC_UART_INT_MASK, info->IER);
1124 return ret;
1127 static int rs_write_room(struct tty_struct *tty)
1129 struct async_struct *info = (struct async_struct *)tty->driver_data;
1130 int ret;
1132 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1133 return 0;
1134 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1135 if (ret < 0)
1136 ret = 0;
1137 return ret;
1140 static int rs_chars_in_buffer(struct tty_struct *tty)
1142 struct async_struct *info = (struct async_struct *)tty->driver_data;
1144 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1145 return 0;
1146 return info->xmit_cnt;
1149 static void rs_flush_buffer(struct tty_struct *tty)
1151 struct async_struct *info = (struct async_struct *)tty->driver_data;
1152 unsigned long flags;
1154 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1155 return;
1157 save_flags(flags); cli();
1158 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1159 restore_flags(flags);
1161 wake_up_interruptible(&tty->write_wait);
1162 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1163 tty->ldisc.write_wakeup)
1164 (tty->ldisc.write_wakeup)(tty);
1168 * This function is used to send a high-priority XON/XOFF character to
1169 * the device
1171 static void rs_send_xchar(struct tty_struct *tty, char ch)
1173 struct async_struct *info = (struct async_struct *)tty->driver_data;
1175 if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1176 return;
1178 info->x_char = ch;
1179 if (ch) {
1180 /* Make sure transmit interrupts are on */
1181 info->IER |= VAC_UART_INT_TX_EMPTY;
1182 serial_out(info, VAC_UART_INT_MASK, info->IER);
1187 * ------------------------------------------------------------
1188 * rs_throttle()
1190 * This routine is called by the upper-layer tty layer to signal that
1191 * incoming characters should be throttled.
1192 * ------------------------------------------------------------
1194 static void rs_throttle(struct tty_struct * tty)
1196 struct async_struct *info = (struct async_struct *)tty->driver_data;
1198 #ifdef SERIAL_DEBUG_THROTTLE
1199 char buf[64];
1201 baget_printk("throttle %s: %d....\n", tty_name(tty, buf),
1202 tty->ldisc.chars_in_buffer(tty));
1203 #endif
1205 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1206 return;
1208 if (I_IXOFF(tty))
1209 rs_send_xchar(tty, STOP_CHAR(tty));
1212 static void rs_unthrottle(struct tty_struct * tty)
1214 struct async_struct *info = (struct async_struct *)tty->driver_data;
1215 #ifdef SERIAL_DEBUG_THROTTLE
1216 char buf[64];
1218 baget_printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1219 tty->ldisc.chars_in_buffer(tty));
1220 #endif
1222 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1223 return;
1225 if (I_IXOFF(tty)) {
1226 if (info->x_char)
1227 info->x_char = 0;
1228 else
1229 rs_send_xchar(tty, START_CHAR(tty));
1234 * ------------------------------------------------------------
1235 * rs_ioctl() and friends
1236 * ------------------------------------------------------------
1239 static int get_serial_info(struct async_struct * info,
1240 struct serial_struct * retinfo)
1242 struct serial_struct tmp;
1243 struct serial_state *state = info->state;
1245 if (!retinfo)
1246 return -EFAULT;
1247 memset(&tmp, 0, sizeof(tmp));
1248 tmp.type = state->type;
1249 tmp.line = state->line;
1250 tmp.port = state->port;
1251 tmp.irq = state->irq;
1252 tmp.flags = state->flags;
1253 tmp.xmit_fifo_size = state->xmit_fifo_size;
1254 tmp.baud_base = state->baud_base;
1255 tmp.close_delay = state->close_delay;
1256 tmp.closing_wait = state->closing_wait;
1257 tmp.custom_divisor = state->custom_divisor;
1258 tmp.hub6 = state->hub6;
1259 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1260 return -EFAULT;
1261 return 0;
1264 static int set_serial_info(struct async_struct * info,
1265 struct serial_struct * new_info)
1267 struct serial_struct new_serial;
1268 struct serial_state old_state, *state;
1269 unsigned int i,change_irq,change_port;
1270 int retval = 0;
1272 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1273 return -EFAULT;
1274 state = info->state;
1275 old_state = *state;
1277 change_irq = new_serial.irq != state->irq;
1278 change_port = (new_serial.port != state->port) ||
1279 (new_serial.hub6 != state->hub6);
1281 if (!capable(CAP_SYS_ADMIN)) {
1282 if (change_irq || change_port ||
1283 (new_serial.baud_base != state->baud_base) ||
1284 (new_serial.type != state->type) ||
1285 (new_serial.close_delay != state->close_delay) ||
1286 (new_serial.xmit_fifo_size != state->xmit_fifo_size) ||
1287 ((new_serial.flags & ~ASYNC_USR_MASK) !=
1288 (state->flags & ~ASYNC_USR_MASK)))
1289 return -EPERM;
1290 state->flags = ((state->flags & ~ASYNC_USR_MASK) |
1291 (new_serial.flags & ASYNC_USR_MASK));
1292 info->flags = ((state->flags & ~ASYNC_USR_MASK) |
1293 (info->flags & ASYNC_USR_MASK));
1294 state->custom_divisor = new_serial.custom_divisor;
1295 goto check_and_exit;
1298 new_serial.irq = irq_cannonicalize(new_serial.irq);
1300 if ((new_serial.irq >= NR_IRQS) || (new_serial.port > 0xffff) ||
1301 (new_serial.baud_base == 0) || (new_serial.type < PORT_UNKNOWN) ||
1302 (new_serial.type > PORT_MAX) || (new_serial.type == PORT_CIRRUS) ||
1303 (new_serial.type == PORT_STARTECH)) {
1304 return -EINVAL;
1307 if ((new_serial.type != state->type) ||
1308 (new_serial.xmit_fifo_size <= 0))
1309 new_serial.xmit_fifo_size =
1310 uart_config[state->type].dfl_xmit_fifo_size;
1312 /* Make sure address is not already in use */
1313 if (new_serial.type) {
1314 for (i = 0 ; i < NR_PORTS; i++)
1315 if ((state != &rs_table[i]) &&
1316 (rs_table[i].port == new_serial.port) &&
1317 rs_table[i].type)
1318 return -EADDRINUSE;
1321 if ((change_port || change_irq) && (state->count > 1))
1322 return -EBUSY;
1325 * OK, past this point, all the error checking has been done.
1326 * At this point, we start making changes.....
1329 state->baud_base = new_serial.baud_base;
1330 state->flags = ((state->flags & ~ASYNC_FLAGS) |
1331 (new_serial.flags & ASYNC_FLAGS));
1332 info->flags = ((state->flags & ~ASYNC_INTERNAL_FLAGS) |
1333 (info->flags & ASYNC_INTERNAL_FLAGS));
1334 state->custom_divisor = new_serial.custom_divisor;
1335 state->type = new_serial.type;
1336 state->close_delay = new_serial.close_delay * HZ/100;
1337 state->closing_wait = new_serial.closing_wait * HZ/100;
1338 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1339 info->xmit_fifo_size = state->xmit_fifo_size =
1340 new_serial.xmit_fifo_size;
1342 release_region(state->port,8);
1343 if (change_port || change_irq) {
1345 * We need to shutdown the serial port at the old
1346 * port/irq combination.
1348 shutdown(info);
1349 state->irq = new_serial.irq;
1350 info->port = state->port = new_serial.port;
1351 info->hub6 = state->hub6 = new_serial.hub6;
1353 if (state->type != PORT_UNKNOWN)
1354 request_region(state->port,8,"serial(set)");
1357 check_and_exit:
1358 if (!state->port || !state->type)
1359 return 0;
1360 if (state->flags & ASYNC_INITIALIZED) {
1361 if (((old_state.flags & ASYNC_SPD_MASK) !=
1362 (state->flags & ASYNC_SPD_MASK)) ||
1363 (old_state.custom_divisor != state->custom_divisor)) {
1364 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1365 info->tty->alt_speed = 57600;
1366 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1367 info->tty->alt_speed = 115200;
1368 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1369 info->tty->alt_speed = 230400;
1370 if ((state->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1371 info->tty->alt_speed = 460800;
1372 change_speed(info);
1374 } else
1375 retval = startup(info);
1376 return retval;
1381 * get_lsr_info - get line status register info
1383 * Purpose: Let user call ioctl() to get info when the UART physically
1384 * is emptied. On bus types like RS485, the transmitter must
1385 * release the bus after transmitting. This must be done when
1386 * the transmit shift register is empty, not be done when the
1387 * transmit holding register is empty. This functionality
1388 * allows an RS485 driver to be written in user space.
1390 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1392 unsigned short status;
1393 unsigned int result;
1394 unsigned long flags;
1396 save_flags(flags); cli();
1397 status = serial_inw(info, VAC_UART_INT_STATUS);
1398 restore_flags(flags);
1399 result = ((status & VAC_UART_STATUS_TX_EMPTY) ? TIOCSER_TEMT : 0);
1400 return put_user(result,value);
1404 static int get_modem_info(struct async_struct * info, unsigned int *value)
1406 unsigned int result;
1408 result = TIOCM_CAR | TIOCM_DSR;
1409 return put_user(result,value);
1412 static int set_modem_info(struct async_struct * info, unsigned int cmd,
1413 unsigned int *value)
1415 int error;
1416 unsigned int arg;
1418 error = get_user(arg, value);
1419 if (error)
1420 return error;
1421 switch (cmd) {
1422 default:
1423 return -EINVAL;
1425 return 0;
1428 static int do_autoconfig(struct async_struct * info)
1430 int retval;
1432 if (!capable(CAP_SYS_ADMIN))
1433 return -EPERM;
1435 if (info->state->count > 1)
1436 return -EBUSY;
1438 shutdown(info);
1440 autoconfig(info->state);
1442 retval = startup(info);
1443 if (retval)
1444 return retval;
1445 return 0;
1449 * rs_break() --- routine which turns the break handling on or off
1451 static void rs_break(struct tty_struct *tty, int break_state)
1453 struct async_struct * info = (struct async_struct *)tty->driver_data;
1454 unsigned long flags;
1456 if (serial_paranoia_check(info, tty->device, "rs_break"))
1457 return;
1459 if (!info->port)
1460 return;
1461 save_flags(flags); cli();
1462 if (break_state == -1)
1463 serial_outp(info, VAC_UART_MODE,
1464 serial_inp(info, VAC_UART_MODE) | \
1465 VAC_UART_MODE_SEND_BREAK);
1466 else
1467 serial_outp(info, VAC_UART_MODE,
1468 serial_inp(info, VAC_UART_MODE) & \
1469 ~VAC_UART_MODE_SEND_BREAK);
1470 restore_flags(flags);
1473 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1474 unsigned int cmd, unsigned long arg)
1476 int error;
1477 struct async_struct * info = (struct async_struct *)tty->driver_data;
1478 struct async_icount cprev, cnow; /* kernel counter temps */
1479 struct serial_icounter_struct *p_cuser; /* user space */
1480 unsigned long flags;
1482 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1483 return -ENODEV;
1485 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1486 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1487 (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1488 if (tty->flags & (1 << TTY_IO_ERROR))
1489 return -EIO;
1492 switch (cmd) {
1493 case TIOCMGET:
1494 return get_modem_info(info, (unsigned int *) arg);
1495 case TIOCMBIS:
1496 case TIOCMBIC:
1497 case TIOCMSET:
1498 return set_modem_info(info, cmd, (unsigned int *) arg);
1499 case TIOCGSERIAL:
1500 return get_serial_info(info,
1501 (struct serial_struct *) arg);
1502 case TIOCSSERIAL:
1503 return set_serial_info(info,
1504 (struct serial_struct *) arg);
1505 case TIOCSERCONFIG:
1506 return do_autoconfig(info);
1508 case TIOCSERGETLSR: /* Get line status register */
1509 return get_lsr_info(info, (unsigned int *) arg);
1511 case TIOCSERGSTRUCT:
1512 if (copy_to_user((struct async_struct *) arg,
1513 info, sizeof(struct async_struct)))
1514 return -EFAULT;
1515 return 0;
1518 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)to change
1519 * - mask passed in arg for lines of interest
1520 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1521 * Caller should use TIOCGICOUNT to see which one it was
1523 case TIOCMIWAIT:
1524 save_flags(flags); cli();
1525 /* note the counters on entry */
1526 cprev = info->state->icount;
1527 restore_flags(flags);
1528 while (1) {
1529 interruptible_sleep_on(&info->delta_msr_wait);
1530 /* see if a signal did it */
1531 if (signal_pending(current))
1532 return -ERESTARTSYS;
1533 save_flags(flags); cli();
1534 cnow = info->state->icount; /* atomic copy */
1535 restore_flags(flags);
1536 if (cnow.rng == cprev.rng &&
1537 cnow.dsr == cprev.dsr &&
1538 cnow.dcd == cprev.dcd &&
1539 cnow.cts == cprev.cts)
1540 return -EIO; /* no change => error */
1541 if ( ((arg & TIOCM_RNG) &&
1542 (cnow.rng != cprev.rng)) ||
1543 ((arg & TIOCM_DSR) &&
1544 (cnow.dsr != cprev.dsr)) ||
1545 ((arg & TIOCM_CD) &&
1546 (cnow.dcd != cprev.dcd)) ||
1547 ((arg & TIOCM_CTS) &&
1548 (cnow.cts != cprev.cts)) ) {
1549 return 0;
1551 cprev = cnow;
1553 /* NOTREACHED */
1556 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1557 * Return: write counters to the user passed counter struct
1558 * NB: both 1->0 and 0->1 transitions are counted except for
1559 * RI where only 0->1 is counted.
1561 case TIOCGICOUNT:
1562 save_flags(flags); cli();
1563 cnow = info->state->icount;
1564 restore_flags(flags);
1565 p_cuser = (struct serial_icounter_struct *) arg;
1566 error = put_user(cnow.cts, &p_cuser->cts);
1567 if (error) return error;
1568 error = put_user(cnow.dsr, &p_cuser->dsr);
1569 if (error) return error;
1570 error = put_user(cnow.rng, &p_cuser->rng);
1571 if (error) return error;
1572 error = put_user(cnow.dcd, &p_cuser->dcd);
1573 if (error) return error;
1574 error = put_user(cnow.rx, &p_cuser->rx);
1575 if (error) return error;
1576 error = put_user(cnow.tx, &p_cuser->tx);
1577 if (error) return error;
1578 error = put_user(cnow.frame, &p_cuser->frame);
1579 if (error) return error;
1580 error = put_user(cnow.overrun, &p_cuser->overrun);
1581 if (error) return error;
1582 error = put_user(cnow.parity, &p_cuser->parity);
1583 if (error) return error;
1584 error = put_user(cnow.brk, &p_cuser->brk);
1585 if (error) return error;
1586 error = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
1588 if (error) return error;
1589 return 0;
1591 case TIOCSERGWILD:
1592 case TIOCSERSWILD:
1593 /* "setserial -W" is called in Debian boot */
1594 printk ("TIOCSER?WILD ioctl obsolete, ignored.\n");
1595 return 0;
1597 default:
1598 return -ENOIOCTLCMD;
1600 return 0;
1603 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1605 struct async_struct *info = (struct async_struct *)tty->driver_data;
1607 if ( (tty->termios->c_cflag == old_termios->c_cflag)
1608 && ( RELEVANT_IFLAG(tty->termios->c_iflag)
1609 == RELEVANT_IFLAG(old_termios->c_iflag)))
1610 return;
1612 change_speed(info);
1614 /* Handle turning off CRTSCTS */
1615 if ((old_termios->c_cflag & CRTSCTS) &&
1616 !(tty->termios->c_cflag & CRTSCTS)) {
1617 tty->hw_stopped = 0;
1618 rs_start(tty);
1624 * ------------------------------------------------------------
1625 * rs_close()
1627 * This routine is called when the serial port gets closed. First, we
1628 * wait for the last remaining data to be sent. Then, we unlink its
1629 * async structure from the interrupt chain if necessary, and we free
1630 * that IRQ if nothing is left in the chain.
1631 * ------------------------------------------------------------
1633 static void rs_close(struct tty_struct *tty, struct file * filp)
1635 struct async_struct * info = (struct async_struct *)tty->driver_data;
1636 struct serial_state *state;
1637 unsigned long flags;
1639 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1640 return;
1642 state = info->state;
1644 save_flags(flags); cli();
1646 if (tty_hung_up_p(filp)) {
1647 DBG_CNT("before DEC-hung");
1648 MOD_DEC_USE_COUNT;
1649 restore_flags(flags);
1650 return;
1653 #ifdef SERIAL_DEBUG_OPEN
1654 baget_printk("rs_close ttys%d, count = %d\n",
1655 info->line, state->count);
1656 #endif
1657 if ((tty->count == 1) && (state->count != 1)) {
1659 * Uh, oh. tty->count is 1, which means that the tty
1660 * structure will be freed. state->count should always
1661 * be one in these conditions. If it's greater than
1662 * one, we've got real problems, since it means the
1663 * serial port won't be shutdown.
1665 baget_printk("rs_close: bad serial port count; "
1666 "tty->count is 1, "
1667 "state->count is %d\n", state->count);
1668 state->count = 1;
1670 if (--state->count < 0) {
1671 baget_printk("rs_close: bad serial port count for "
1672 "ttys%d: %d\n",
1673 info->line, state->count);
1674 state->count = 0;
1676 if (state->count) {
1677 DBG_CNT("before DEC-2");
1678 MOD_DEC_USE_COUNT;
1679 restore_flags(flags);
1680 return;
1682 info->flags |= ASYNC_CLOSING;
1684 * Save the termios structure, since this port may have
1685 * separate termios for callout and dialin.
1687 if (info->flags & ASYNC_NORMAL_ACTIVE)
1688 info->state->normal_termios = *tty->termios;
1689 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1690 info->state->callout_termios = *tty->termios;
1692 * Now we wait for the transmit buffer to clear; and we notify
1693 * the line discipline to only process XON/XOFF characters.
1695 tty->closing = 1;
1696 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1697 tty_wait_until_sent(tty, info->closing_wait);
1699 * At this point we stop accepting input. To do this, we
1700 * disable the receive line status interrupts, and tell the
1701 * interrupt driver to stop checking the data ready bit in the
1702 * line status register.
1704 info->IER &= ~(VAC_UART_INT_RX_BREAK_CHANGE | VAC_UART_INT_RX_ERRS);
1705 info->read_status_mask &= ~VAC_UART_STATUS_RX_READY;
1706 if (info->flags & ASYNC_INITIALIZED) {
1707 serial_outw(info, VAC_UART_INT_MASK, info->IER);
1709 * Before we drop DTR, make sure the UART transmitter
1710 * has completely drained; this is especially
1711 * important if there is a transmit FIFO!
1713 rs_wait_until_sent(tty, info->timeout);
1715 shutdown(info);
1716 if (tty->driver.flush_buffer)
1717 tty->driver.flush_buffer(tty);
1718 if (tty->ldisc.flush_buffer)
1719 tty->ldisc.flush_buffer(tty);
1720 tty->closing = 0;
1721 info->event = 0;
1722 info->tty = 0;
1723 if (info->blocked_open) {
1724 if (info->close_delay) {
1725 current->state = TASK_INTERRUPTIBLE;
1726 schedule_timeout(info->close_delay);
1728 wake_up_interruptible(&info->open_wait);
1730 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1731 ASYNC_CLOSING);
1732 wake_up_interruptible(&info->close_wait);
1733 MOD_DEC_USE_COUNT;
1734 restore_flags(flags);
1738 * rs_wait_until_sent() --- wait until the transmitter is empty
1740 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1742 struct async_struct * info = (struct async_struct *)tty->driver_data;
1743 unsigned long orig_jiffies, char_time;
1744 int lsr;
1746 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1747 return;
1749 if (info->state->type == PORT_UNKNOWN)
1750 return;
1752 if (info->xmit_fifo_size == 0)
1753 return; /* Just in case.... */
1755 orig_jiffies = jiffies;
1757 * Set the check interval to be 1/5 of the estimated time to
1758 * send a single character, and make it at least 1. The check
1759 * interval should also be less than the timeout.
1761 * Note: we have to use pretty tight timings here to satisfy
1762 * the NIST-PCTS.
1764 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1765 char_time = char_time / 5;
1766 if (char_time == 0)
1767 char_time = 1;
1768 if (timeout)
1769 char_time = MIN(char_time, timeout);
1770 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1771 baget_printk("In rs_wait_until_sent(%d) check=%lu...",
1772 timeout, char_time);
1773 baget_printk("jiff=%lu...", jiffies);
1774 #endif
1775 while (!((lsr = serial_inp(info, VAC_UART_INT_STATUS)) & \
1776 VAC_UART_STATUS_TX_EMPTY)) {
1777 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1778 baget_printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1779 #endif
1780 current->state = TASK_INTERRUPTIBLE;
1781 schedule_timeout(char_time);
1782 if (signal_pending(current))
1783 break;
1784 if (timeout && ((orig_jiffies + timeout) < jiffies))
1785 break;
1787 current->state = TASK_RUNNING;
1788 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1789 baget_printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1790 #endif
1794 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1796 static void rs_hangup(struct tty_struct *tty)
1798 struct async_struct * info = (struct async_struct *)tty->driver_data;
1799 struct serial_state *state = info->state;
1801 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1802 return;
1804 state = info->state;
1806 rs_flush_buffer(tty);
1807 shutdown(info);
1808 info->event = 0;
1809 state->count = 0;
1810 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1811 info->tty = 0;
1812 wake_up_interruptible(&info->open_wait);
1816 * ------------------------------------------------------------
1817 * rs_open() and friends
1818 * ------------------------------------------------------------
1820 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1821 struct async_struct *info)
1823 struct wait_queue wait = { current, NULL };
1824 struct serial_state *state = info->state;
1825 int retval;
1826 int do_clocal = 0, extra_count = 0;
1827 unsigned long flags;
1830 * If the device is in the middle of being closed, then block
1831 * until it's done, and then try again.
1833 if (tty_hung_up_p(filp) ||
1834 (info->flags & ASYNC_CLOSING)) {
1835 if (info->flags & ASYNC_CLOSING)
1836 interruptible_sleep_on(&info->close_wait);
1837 #ifdef SERIAL_DO_RESTART
1838 return ((info->flags & ASYNC_HUP_NOTIFY) ?
1839 -EAGAIN : -ERESTARTSYS);
1840 #else
1841 return -EAGAIN;
1842 #endif
1846 * If this is a callout device, then just make sure the normal
1847 * device isn't being used.
1849 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1850 if (info->flags & ASYNC_NORMAL_ACTIVE)
1851 return -EBUSY;
1852 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1853 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1854 (info->session != current->session))
1855 return -EBUSY;
1856 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1857 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1858 (info->pgrp != current->pgrp))
1859 return -EBUSY;
1860 info->flags |= ASYNC_CALLOUT_ACTIVE;
1861 return 0;
1865 * If non-blocking mode is set, or the port is not enabled,
1866 * then make the check up front and then exit.
1868 if ((filp->f_flags & O_NONBLOCK) ||
1869 (tty->flags & (1 << TTY_IO_ERROR))) {
1870 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1871 return -EBUSY;
1872 info->flags |= ASYNC_NORMAL_ACTIVE;
1873 return 0;
1876 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1877 if (state->normal_termios.c_cflag & CLOCAL)
1878 do_clocal = 1;
1879 } else {
1880 if (tty->termios->c_cflag & CLOCAL)
1881 do_clocal = 1;
1885 * Block waiting for the carrier detect and the line to become
1886 * free (i.e., not in use by the callout). While we are in
1887 * this loop, state->count is dropped by one, so that
1888 * rs_close() knows when to free things. We restore it upon
1889 * exit, either normal or abnormal.
1891 retval = 0;
1892 add_wait_queue(&info->open_wait, &wait);
1893 #ifdef SERIAL_DEBUG_OPEN
1894 baget_printk("block_til_ready before block: ttys%d, count = %d\n",
1895 state->line, state->count);
1896 #endif
1897 save_flags(flags); cli();
1898 if (!tty_hung_up_p(filp)) {
1899 extra_count = 1;
1900 state->count--;
1902 restore_flags(flags);
1903 info->blocked_open++;
1904 while (1) {
1905 set_current_state(TASK_INTERRUPTIBLE);
1906 if (tty_hung_up_p(filp) ||
1907 !(info->flags & ASYNC_INITIALIZED)) {
1908 #ifdef SERIAL_DO_RESTART
1909 if (info->flags & ASYNC_HUP_NOTIFY)
1910 retval = -EAGAIN;
1911 else
1912 retval = -ERESTARTSYS;
1913 #else
1914 retval = -EAGAIN;
1915 #endif
1916 break;
1918 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1919 !(info->flags & ASYNC_CLOSING))
1920 break;
1921 if (signal_pending(current)) {
1922 retval = -ERESTARTSYS;
1923 break;
1925 #ifdef SERIAL_DEBUG_OPEN
1926 baget_printk("block_til_ready blocking: ttys%d, count = %d\n",
1927 info->line, state->count);
1928 #endif
1929 schedule();
1931 current->state = TASK_RUNNING;
1932 remove_wait_queue(&info->open_wait, &wait);
1933 if (extra_count)
1934 state->count++;
1935 info->blocked_open--;
1936 #ifdef SERIAL_DEBUG_OPEN
1937 baget_printk("block_til_ready after blocking: ttys%d, count = %d\n",
1938 info->line, state->count);
1939 #endif
1940 if (retval)
1941 return retval;
1942 info->flags |= ASYNC_NORMAL_ACTIVE;
1943 return 0;
1946 static int get_async_struct(int line, struct async_struct **ret_info)
1948 struct async_struct *info;
1949 struct serial_state *sstate;
1951 sstate = rs_table + line;
1952 sstate->count++;
1953 if (sstate->info) {
1954 *ret_info = sstate->info;
1955 return 0;
1957 info = kmalloc(sizeof(struct async_struct), GFP_KERNEL);
1958 if (!info) {
1959 sstate->count--;
1960 return -ENOMEM;
1962 memset(info, 0, sizeof(struct async_struct));
1963 info->magic = SERIAL_MAGIC;
1964 info->port = sstate->port;
1965 info->flags = sstate->flags;
1966 info->xmit_fifo_size = sstate->xmit_fifo_size;
1967 info->line = line;
1968 info->tqueue.routine = do_softint;
1969 info->tqueue.data = info;
1970 info->state = sstate;
1971 if (sstate->info) {
1972 kfree_s(info, sizeof(struct async_struct));
1973 *ret_info = sstate->info;
1974 return 0;
1976 *ret_info = sstate->info = info;
1977 return 0;
1981 * This routine is called whenever a serial port is opened. It
1982 * enables interrupts for a serial port, linking in its async structure into
1983 * the IRQ chain. It also performs the serial-specific
1984 * initialization for the tty structure.
1986 static int rs_open(struct tty_struct *tty, struct file * filp)
1988 struct async_struct *info;
1989 int retval, line;
1990 unsigned long page;
1992 MOD_INC_USE_COUNT;
1993 line = MINOR(tty->device) - tty->driver.minor_start;
1994 if ((line < 0) || (line >= NR_PORTS)) {
1995 MOD_DEC_USE_COUNT;
1996 return -ENODEV;
1998 retval = get_async_struct(line, &info);
1999 if (retval) {
2000 MOD_DEC_USE_COUNT;
2001 return retval;
2003 tty->driver_data = info;
2004 info->tty = tty;
2005 if (serial_paranoia_check(info, tty->device, "rs_open")) {
2006 /* MOD_DEC_USE_COUNT; "info->tty" will cause this */
2007 return -ENODEV;
2010 #ifdef SERIAL_DEBUG_OPEN
2011 baget_printk("rs_open %s%d, count = %d\n",
2012 tty->driver.name, info->line,
2013 info->state->count);
2014 #endif
2015 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2017 if (!tmp_buf) {
2018 page = get_free_page(GFP_KERNEL);
2019 if (!page) {
2020 /* MOD_DEC_USE_COUNT; "info->tty" will cause this */
2021 return -ENOMEM;
2023 if (tmp_buf)
2024 free_page(page);
2025 else
2026 tmp_buf = (unsigned char *) page;
2030 * If the port is the middle of closing, bail out now
2032 if (tty_hung_up_p(filp) ||
2033 (info->flags & ASYNC_CLOSING)) {
2034 if (info->flags & ASYNC_CLOSING)
2035 interruptible_sleep_on(&info->close_wait);
2036 /* MOD_DEC_USE_COUNT; "info->tty" will cause this */
2037 #ifdef SERIAL_DO_RESTART
2038 return ((info->flags & ASYNC_HUP_NOTIFY) ?
2039 -EAGAIN : -ERESTARTSYS);
2040 #else
2041 return -EAGAIN;
2042 #endif
2046 * Start up serial port
2048 retval = startup(info);
2049 if (retval) {
2050 /* MOD_DEC_USE_COUNT; "info->tty" will cause this */
2051 return retval;
2054 retval = block_til_ready(tty, filp, info);
2055 if (retval) {
2056 /* MOD_DEC_USE_COUNT; "info->tty" will cause this */
2057 #ifdef SERIAL_DEBUG_OPEN
2058 baget_printk("rs_open returning after block_til_ready "
2059 "with %d\n",
2060 retval);
2061 #endif
2062 return retval;
2065 if ((info->state->count == 1) &&
2066 (info->flags & ASYNC_SPLIT_TERMIOS)) {
2067 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2068 *tty->termios = info->state->normal_termios;
2069 else
2070 *tty->termios = info->state->callout_termios;
2071 change_speed(info);
2073 #ifdef CONFIG_SERIAL_CONSOLE
2074 if (sercons.cflag && sercons.index == line) {
2075 tty->termios->c_cflag = sercons.cflag;
2076 sercons.cflag = 0;
2077 change_speed(info);
2079 #endif
2080 info->session = current->session;
2081 info->pgrp = current->pgrp;
2083 #ifdef SERIAL_DEBUG_OPEN
2084 baget_printk("rs_open ttys%d successful...", info->line);
2085 #endif
2086 return 0;
2090 * /proc fs routines....
2093 static inline int line_info(char *buf, struct serial_state *state)
2095 struct async_struct *info = state->info, scr_info;
2096 int ret;
2098 ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2099 state->line, uart_config[state->type].name,
2100 state->port, state->irq);
2102 if (!state->port || (state->type == PORT_UNKNOWN)) {
2103 ret += sprintf(buf+ret, "\n");
2104 return ret;
2108 * Figure out the current RS-232 lines
2110 if (!info) {
2111 info = &scr_info; /* This is just for serial_{in,out} */
2113 info->magic = SERIAL_MAGIC;
2114 info->port = state->port;
2115 info->flags = state->flags;
2116 info->quot = 0;
2117 info->tty = 0;
2120 if (info->quot) {
2121 ret += sprintf(buf+ret, " baud:%d",
2122 state->baud_base / info->quot);
2125 ret += sprintf(buf+ret, " tx:%d rx:%d",
2126 state->icount.tx, state->icount.rx);
2128 if (state->icount.frame)
2129 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2131 if (state->icount.parity)
2132 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2134 if (state->icount.brk)
2135 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2137 if (state->icount.overrun)
2138 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2140 return ret;
2143 int rs_read_proc(char *page, char **start, off_t off, int count,
2144 int *eof, void *data)
2146 int i, len = 0, l;
2147 off_t begin = 0;
2149 len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2150 for (i = 0; i < NR_PORTS && len < 4000; i++) {
2151 l = line_info(page + len, &rs_table[i]);
2152 len += l;
2153 if (len+begin > off+count)
2154 goto done;
2155 if (len+begin < off) {
2156 begin += len;
2157 len = 0;
2160 *eof = 1;
2161 done:
2162 if (off >= len+begin)
2163 return 0;
2164 *start = page + (begin-off);
2165 return ((count < begin+len-off) ? count : begin+len-off);
2169 * ---------------------------------------------------------------------
2170 * rs_init() and friends
2172 * rs_init() is called at boot-time to initialize the serial driver.
2173 * ---------------------------------------------------------------------
2177 * This routine prints out the appropriate serial driver version
2178 * number, and identifies which options were configured into this
2179 * driver.
2181 static _INLINE_ void show_serial_version(void)
2183 printk(KERN_INFO "%s version %s with", serial_name, serial_version);
2184 #ifdef CONFIG_SERIAL_SHARE_IRQ
2185 printk(" SHARE_IRQ");
2186 #endif
2187 #define SERIAL_OPT
2188 #ifdef CONFIG_SERIAL_DETECT_IRQ
2189 printk(" DETECT_IRQ");
2190 #endif
2191 #ifdef SERIAL_OPT
2192 printk(" enabled\n");
2193 #else
2194 printk(" no serial options enabled\n");
2195 #endif
2196 #undef SERIAL_OPT
2201 * This routine is called by rs_init() to initialize a specific serial
2202 * port. It determines what type of UART chip this serial port is
2203 * using: 8250, 16450, 16550, 16550A. The important question is
2204 * whether or not this UART is a 16550A or not, since this will
2205 * determine whether or not we can use its FIFO features or not.
2209 * Functionality of this function is reduced: we already know we have a VAC,
2210 * but still need to perform some important actions (see code :-).
2212 static void autoconfig(struct serial_state * state)
2214 struct async_struct *info, scr_info;
2215 unsigned long flags;
2217 /* Setting up important parameters */
2218 state->type = VAC_UART_TYPE;
2219 state->xmit_fifo_size = uart_config[state->type].dfl_xmit_fifo_size;
2221 info = &scr_info; /* This is just for serial_{in,out} */
2223 info->magic = SERIAL_MAGIC;
2224 info->port = state->port;
2225 info->flags = state->flags;
2227 save_flags(flags); cli();
2229 /* + Flush VAC input fifo */
2230 (void)serial_in(info, VAC_UART_RX);
2231 (void)serial_in(info, VAC_UART_RX);
2232 (void)serial_in(info, VAC_UART_RX);
2233 (void)serial_in(info, VAC_UART_RX);
2235 /* Disable interrupts */
2236 serial_outp(info, VAC_UART_INT_MASK, 0);
2238 restore_flags(flags);
2241 int register_serial(struct serial_struct *req);
2242 void unregister_serial(int line);
2244 EXPORT_SYMBOL(register_serial);
2245 EXPORT_SYMBOL(unregister_serial);
2248 * Important function for VAC UART check and reanimation.
2251 static void rs_timer(void)
2253 static unsigned long last_strobe = 0;
2254 struct async_struct *info;
2255 unsigned int i;
2256 unsigned long flags;
2258 if ((jiffies - last_strobe) >= RS_STROBE_TIME) {
2259 for (i=1; i < NR_IRQS; i++) {
2260 info = IRQ_ports[i];
2261 if (!info)
2262 continue;
2263 save_flags(flags); cli();
2264 #ifdef CONFIG_SERIAL_SHARE_IRQ
2265 if (info->next_port) {
2266 do {
2267 serial_out(info, VAC_UART_INT_MASK, 0);
2268 info->IER |= VAC_UART_INT_TX_EMPTY;
2269 serial_out(info, VAC_UART_INT_MASK,
2270 info->IER);
2271 info = info->next_port;
2272 } while (info);
2273 rs_interrupt(i, NULL, NULL);
2274 } else
2275 #endif /* CONFIG_SERIAL_SHARE_IRQ */
2276 rs_interrupt_single(i, NULL, NULL);
2277 restore_flags(flags);
2280 last_strobe = jiffies;
2281 timer_table[RS_TIMER].expires = jiffies + RS_STROBE_TIME;
2282 timer_active |= 1 << RS_TIMER;
2285 * It looks this code for case we share IRQ with console...
2288 if (IRQ_ports[0]) {
2289 save_flags(flags); cli();
2290 #ifdef CONFIG_SERIAL_SHARE_IRQ
2291 rs_interrupt(0, NULL, NULL);
2292 #else
2293 rs_interrupt_single(0, NULL, NULL);
2294 #endif
2295 restore_flags(flags);
2297 timer_table[RS_TIMER].expires = jiffies + IRQ_timeout[0] - 2;
2302 * The serial driver boot-time initialization code!
2304 int __init rs_init(void)
2306 int i;
2307 struct serial_state * state;
2308 extern void atomwide_serial_init (void);
2309 extern void dualsp_serial_init (void);
2311 #ifdef CONFIG_ATOMWIDE_SERIAL
2312 atomwide_serial_init ();
2313 #endif
2314 #ifdef CONFIG_DUALSP_SERIAL
2315 dualsp_serial_init ();
2316 #endif
2318 init_bh(SERIAL_BH, do_serial_bh);
2319 timer_table[RS_TIMER].fn = rs_timer;
2320 timer_table[RS_TIMER].expires = 0;
2322 for (i = 0; i < NR_IRQS; i++) {
2323 IRQ_ports[i] = 0;
2324 IRQ_timeout[i] = 0;
2329 * It is not a good idea to share interrupts with console,
2330 * but it looks we cannot avoid it.
2332 #if 0
2334 #ifdef CONFIG_SERIAL_CONSOLE
2336 * The interrupt of the serial console port
2337 * can't be shared.
2339 if (sercons.flags & CON_CONSDEV) {
2340 for(i = 0; i < NR_PORTS; i++)
2341 if (i != sercons.index &&
2342 rs_table[i].irq == rs_table[sercons.index].irq)
2343 rs_table[i].irq = 0;
2345 #endif
2347 #endif
2348 show_serial_version();
2350 /* Initialize the tty_driver structure */
2352 memset(&serial_driver, 0, sizeof(struct tty_driver));
2353 serial_driver.magic = TTY_DRIVER_MAGIC;
2354 serial_driver.driver_name = "serial";
2355 serial_driver.name = "ttyS";
2356 serial_driver.major = TTY_MAJOR;
2357 serial_driver.minor_start = 64;
2358 serial_driver.num = NR_PORTS;
2359 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2360 serial_driver.subtype = SERIAL_TYPE_NORMAL;
2361 serial_driver.init_termios = tty_std_termios;
2362 serial_driver.init_termios.c_cflag =
2363 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2364 serial_driver.flags = TTY_DRIVER_REAL_RAW;
2365 serial_driver.refcount = &serial_refcount;
2366 serial_driver.table = serial_table;
2367 serial_driver.termios = serial_termios;
2368 serial_driver.termios_locked = serial_termios_locked;
2370 serial_driver.open = rs_open;
2371 serial_driver.close = rs_close;
2372 serial_driver.write = rs_write;
2373 serial_driver.put_char = rs_put_char;
2374 serial_driver.flush_chars = rs_flush_chars;
2375 serial_driver.write_room = rs_write_room;
2376 serial_driver.chars_in_buffer = rs_chars_in_buffer;
2377 serial_driver.flush_buffer = rs_flush_buffer;
2378 serial_driver.ioctl = rs_ioctl;
2379 serial_driver.throttle = rs_throttle;
2380 serial_driver.unthrottle = rs_unthrottle;
2381 serial_driver.send_xchar = rs_send_xchar;
2382 serial_driver.set_termios = rs_set_termios;
2383 serial_driver.stop = rs_stop;
2384 serial_driver.start = rs_start;
2385 serial_driver.hangup = rs_hangup;
2386 serial_driver.break_ctl = rs_break;
2387 serial_driver.wait_until_sent = rs_wait_until_sent;
2388 serial_driver.read_proc = rs_read_proc;
2391 * The callout device is just like normal device except for
2392 * major number and the subtype code.
2394 callout_driver = serial_driver;
2395 callout_driver.name = "cua";
2396 callout_driver.major = TTYAUX_MAJOR;
2397 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2398 callout_driver.read_proc = 0;
2399 callout_driver.proc_entry = 0;
2401 if (tty_register_driver(&serial_driver))
2402 panic("Couldn't register serial driver\n");
2403 if (tty_register_driver(&callout_driver))
2404 panic("Couldn't register callout driver\n");
2406 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2407 state->magic = SSTATE_MAGIC;
2408 state->line = i;
2409 state->type = PORT_UNKNOWN;
2410 state->custom_divisor = 0;
2411 state->close_delay = 5*HZ/10;
2412 state->closing_wait = 30*HZ;
2413 state->callout_termios = callout_driver.init_termios;
2414 state->normal_termios = serial_driver.init_termios;
2415 state->icount.cts = state->icount.dsr =
2416 state->icount.rng = state->icount.dcd = 0;
2417 state->icount.rx = state->icount.tx = 0;
2418 state->icount.frame = state->icount.parity = 0;
2419 state->icount.overrun = state->icount.brk = 0;
2420 state->irq = irq_cannonicalize(state->irq);
2421 if (check_region(state->port,8))
2422 continue;
2423 if (state->flags & ASYNC_BOOT_AUTOCONF)
2424 autoconfig(state);
2428 * Detect the IRQ only once every port is initialised,
2429 * because some 16450 do not reset to 0 the MCR register.
2431 for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2432 if (state->type == PORT_UNKNOWN)
2433 continue;
2434 printk(KERN_INFO "ttyS%02d%s at 0x%04x (irq = %d) is a %s\n",
2435 state->line,
2436 (state->flags & ASYNC_FOURPORT) ? " FourPort" : "",
2437 state->port, state->irq,
2438 uart_config[state->type].name);
2440 return 0;
2444 * register_serial and unregister_serial allows for serial ports to be
2445 * configured at run-time, to support PCMCIA modems.
2447 int register_serial(struct serial_struct *req)
2449 int i;
2450 unsigned long flags;
2451 struct serial_state *state;
2453 save_flags(flags);
2454 cli();
2455 for (i = 0; i < NR_PORTS; i++) {
2456 if (rs_table[i].port == req->port)
2457 break;
2459 if (i == NR_PORTS) {
2460 for (i = 0; i < NR_PORTS; i++)
2461 if ((rs_table[i].type == PORT_UNKNOWN) &&
2462 (rs_table[i].count == 0))
2463 break;
2465 if (i == NR_PORTS) {
2466 restore_flags(flags);
2467 return -1;
2469 state = &rs_table[i];
2470 if (rs_table[i].count) {
2471 restore_flags(flags);
2472 printk("Couldn't configure serial #%d (port=%d,irq=%d): "
2473 "device already open\n", i, req->port, req->irq);
2474 return -1;
2476 state->irq = req->irq;
2477 state->port = req->port;
2478 state->flags = req->flags;
2480 autoconfig(state);
2481 if (state->type == PORT_UNKNOWN) {
2482 restore_flags(flags);
2483 printk("register_serial(): autoconfig failed\n");
2484 return -1;
2486 restore_flags(flags);
2488 printk(KERN_INFO "tty%02d at 0x%04x (irq = %d) is a %s\n",
2489 state->line, state->port, state->irq,
2490 uart_config[state->type].name);
2491 return state->line;
2494 void unregister_serial(int line)
2496 unsigned long flags;
2497 struct serial_state *state = &rs_table[line];
2499 save_flags(flags);
2500 cli();
2501 if (state->info && state->info->tty)
2502 tty_hangup(state->info->tty);
2503 state->type = PORT_UNKNOWN;
2504 printk(KERN_INFO "tty%02d unloaded\n", state->line);
2505 restore_flags(flags);
2508 #ifdef MODULE
2509 int init_module(void)
2511 return rs_init();
2514 void cleanup_module(void)
2516 unsigned long flags;
2517 int e1, e2;
2518 int i;
2520 printk("Unloading %s: version %s\n", serial_name, serial_version);
2521 save_flags(flags);
2522 cli();
2524 timer_active &= ~(1 << RS_TIMER);
2525 timer_table[RS_TIMER].fn = NULL;
2526 timer_table[RS_TIMER].expires = 0;
2527 remove_bh(SERIAL_BH);
2529 if ((e1 = tty_unregister_driver(&serial_driver)))
2530 printk("SERIAL: failed to unregister serial driver (%d)\n",
2531 e1);
2532 if ((e2 = tty_unregister_driver(&callout_driver)))
2533 printk("SERIAL: failed to unregister callout driver (%d)\n",
2534 e2);
2535 restore_flags(flags);
2537 for (i = 0; i < NR_PORTS; i++) {
2538 if (rs_table[i].type != PORT_UNKNOWN)
2539 release_region(rs_table[i].port, 8);
2541 if (tmp_buf) {
2542 free_page((unsigned long) tmp_buf);
2543 tmp_buf = NULL;
2546 #endif /* MODULE */
2550 * ------------------------------------------------------------
2551 * Serial console driver
2552 * ------------------------------------------------------------
2554 #ifdef CONFIG_SERIAL_CONSOLE
2556 #define BOTH_EMPTY (VAC_UART_STATUS_TX_EMPTY | VAC_UART_STATUS_TX_EMPTY)
2559 * Wait for transmitter & holding register to empty
2561 static inline void wait_for_xmitr(struct async_struct *info)
2563 int lsr;
2564 unsigned int tmout = 1000000;
2566 do {
2567 lsr = serial_inp(info, VAC_UART_INT_STATUS);
2568 if (--tmout == 0) break;
2569 } while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2573 * Print a string to the serial port trying not to disturb
2574 * any possible real use of the port...
2576 static void serial_console_write(struct console *co, const char *s,
2577 unsigned count)
2579 struct serial_state *ser;
2580 int ier;
2581 unsigned i;
2582 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
2584 ser = rs_table + co->index;
2585 scr_info.magic = SERIAL_MAGIC;
2586 scr_info.port = ser->port;
2587 scr_info.flags = ser->flags;
2590 * First save the IER then disable the interrupts
2592 ier = serial_inp(&scr_info, VAC_UART_INT_MASK);
2593 serial_outw(&scr_info, VAC_UART_INT_MASK, 0x00);
2596 * Now, do each character
2598 for (i = 0; i < count; i++, s++) {
2599 wait_for_xmitr(&scr_info);
2602 * Send the character out.
2603 * If a LF, also do CR...
2605 serial_outp(&scr_info, VAC_UART_TX, (unsigned short)*s << 8);
2606 if (*s == 10) {
2607 wait_for_xmitr(&scr_info);
2608 serial_outp(&scr_info, VAC_UART_TX, 13 << 8);
2613 * Finally, Wait for transmitter & holding register to empty
2614 * and restore the IER
2616 wait_for_xmitr(&scr_info);
2617 serial_outp(&scr_info, VAC_UART_INT_MASK, ier);
2621 * Receive character from the serial port
2623 static int serial_console_wait_key(struct console *co)
2625 struct serial_state *ser;
2626 int ier;
2627 int lsr;
2628 int c;
2629 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
2631 ser = rs_table + co->index;
2632 scr_info.magic = SERIAL_MAGIC;
2633 scr_info.port = ser->port;
2634 scr_info.flags = ser->flags;
2637 * First save the IER then disable the interrupts so
2638 * that the real driver for the port does not get the
2639 * character.
2641 ier = serial_inp(&scr_info, VAC_UART_INT_MASK);
2642 serial_outp(&scr_info, VAC_UART_INT_MASK, 0x00);
2644 do {
2645 lsr = serial_inp(&scr_info, VAC_UART_INT_STATUS);
2646 } while (!(lsr & VAC_UART_STATUS_RX_READY));
2647 c = serial_inp(&scr_info, VAC_UART_RX);
2650 * Restore the interrupts
2652 serial_outp(&scr_info, VAC_UART_INT_MASK, ier);
2654 return c;
2657 static kdev_t serial_console_device(struct console *c)
2659 return MKDEV(TTY_MAJOR, 64 + c->index);
2663 * Setup initial baud/bits/parity. We do two things here:
2664 * - construct a cflag setting for the first rs_open()
2665 * - initialize the serial port
2666 * Return non-zero if we didn't find a serial port.
2668 static int __init serial_console_setup(struct console *co, char *options)
2670 struct serial_state *ser;
2671 unsigned cval;
2672 int baud = 9600;
2673 int bits = 8;
2674 int parity = 'n';
2675 int cflag = CREAD | HUPCL | CLOCAL;
2676 int quot = 0;
2677 char *s;
2678 struct async_struct scr_info; /* serial_{in,out} because HUB6 */
2680 if (options) {
2681 baud = simple_strtoul(options, NULL, 10);
2682 s = options;
2683 while(*s >= '0' && *s <= '9')
2684 s++;
2685 if (*s) parity = *s++;
2686 if (*s) bits = *s - '0';
2690 * Now construct a cflag setting.
2692 switch(baud) {
2693 case 1200:
2694 cflag |= B1200;
2695 break;
2696 case 2400:
2697 cflag |= B2400;
2698 break;
2699 case 4800:
2700 cflag |= B4800;
2701 break;
2702 case 19200:
2703 cflag |= B19200;
2704 break;
2705 case 38400:
2706 cflag |= B38400;
2707 break;
2708 case 57600:
2709 cflag |= B57600;
2710 break;
2711 case 115200:
2712 cflag |= B115200;
2713 break;
2714 case 9600:
2715 default:
2716 cflag |= B9600;
2717 break;
2719 switch(bits) {
2720 case 7:
2721 cflag |= CS7;
2722 break;
2723 default:
2724 case 8:
2725 cflag |= CS8;
2726 break;
2728 switch(parity) {
2729 case 'o': case 'O':
2730 cflag |= PARODD;
2731 break;
2732 case 'e': case 'E':
2733 cflag |= PARENB;
2734 break;
2736 co->cflag = cflag;
2739 * Divisor, bytesize and parity
2741 ser = rs_table + co->index;
2742 scr_info.magic = SERIAL_MAGIC;
2743 scr_info.port = ser->port;
2744 scr_info.flags = ser->flags;
2746 quot = ser->baud_base / baud;
2747 cval = cflag & (CSIZE | CSTOPB);
2749 cval >>= 4;
2751 cval &= ~VAC_UART_MODE_PARITY_ENABLE;
2752 if (cflag & PARENB)
2753 cval |= VAC_UART_MODE_PARITY_ENABLE;
2754 if (cflag & PARODD)
2755 cval |= VAC_UART_MODE_PARITY_ODD;
2758 * Disable UART interrupts, set DTR and RTS high
2759 * and set speed.
2761 switch (baud) {
2762 default:
2763 case 9600:
2764 cval |= VAC_UART_MODE_BAUD(7);
2765 break;
2766 case 4800:
2767 cval |= VAC_UART_MODE_BAUD(6);
2768 break;
2769 case 2400:
2770 cval |= VAC_UART_MODE_BAUD(5);
2771 break;
2772 case 1200:
2773 cval |= VAC_UART_MODE_BAUD(4);
2774 break;
2775 case 600:
2776 cval |= VAC_UART_MODE_BAUD(3);
2777 break;
2778 case 300:
2779 cval |= VAC_UART_MODE_BAUD(2);
2780 break;
2781 #ifndef QUAD_UART_SPEED
2782 case 150:
2783 #else
2784 case 38400:
2785 #endif
2786 cval |= VAC_UART_MODE_BAUD(1);
2787 break;
2788 #ifndef QUAD_UART_SPEED
2789 case 75:
2790 #else
2791 case 19200:
2792 #endif
2793 cval |= VAC_UART_MODE_BAUD(0);
2794 break;
2797 /* Baget VAC need some adjustments for computed value */
2798 cval = vac_uart_mode_fixup(cval);
2800 serial_outp(&scr_info, VAC_UART_MODE, cval);
2801 serial_outp(&scr_info, VAC_UART_INT_MASK, 0);
2803 return 0;
2806 static struct console sercons = {
2807 "ttyS",
2808 serial_console_write,
2809 NULL,
2810 serial_console_device,
2811 serial_console_wait_key,
2812 NULL,
2813 serial_console_setup,
2814 CON_PRINTBUFFER,
2817 NULL
2821 * Register console.
2823 long __init serial_console_init(long kmem_start, long kmem_end)
2825 register_console(&sercons);
2826 return kmem_start;
2828 #endif
2830 #ifdef CONFIG_REMOTE_DEBUG
2831 #undef PRINT_DEBUG_PORT_INFO
2834 * This is the interface to the remote debugger stub.
2835 * I've put that here to be able to control the serial
2836 * device more directly.
2839 static int initialized = 0;
2841 static int rs_debug_init(struct async_struct *info)
2843 int quot;
2845 autoconfig(info); /* autoconfigure ttyS0, whatever that is */
2847 #ifdef PRINT_DEBUG_PORT_INFO
2848 baget_printk("kgdb debug interface:: tty%02d at 0x%04x",
2849 info->line, info->port);
2850 switch (info->type) {
2851 case PORT_8250:
2852 baget_printk(" is a 8250\n");
2853 break;
2854 case PORT_16450:
2855 baget_printk(" is a 16450\n");
2856 break;
2857 case PORT_16550:
2858 baget_printk(" is a 16550\n");
2859 break;
2860 case PORT_16550A:
2861 baget_printk(" is a 16550A\n");
2862 break;
2863 case PORT_16650:
2864 baget_printk(" is a 16650\n");
2865 break;
2866 default:
2867 baget_printk(" is of unknown type -- unusable\n");
2868 break;
2870 #endif
2872 if (info->port == PORT_UNKNOWN)
2873 return -1;
2876 * Clear all interrupts
2879 (void)serial_inp(info, VAC_UART_INT_STATUS);
2880 (void)serial_inp(info, VAC_UART_RX);
2883 * Now, initialize the UART
2885 serial_outp(info,VAC_UART_MODE,VAC_UART_MODE_INITIAL); /* reset DLAB */
2886 if (info->flags & ASYNC_FOURPORT) {
2887 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2888 info->MCR_noint = UART_MCR_DTR | UART_MCR_OUT1;
2889 } else {
2890 info->MCR = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
2891 info->MCR_noint = UART_MCR_DTR | UART_MCR_RTS;
2894 info->MCR = info->MCR_noint; /* no interrupts, please */
2896 * and set the speed of the serial port
2897 * (currently hardwired to 9600 8N1
2900 quot = info->baud_base / 9600; /* baud rate is fixed to 9600 */
2901 /* FIXME: if rs_debug interface is needed, we need to set speed here */
2903 return 0;
2906 int putDebugChar(char c)
2908 struct async_struct *info = rs_table;
2910 if (!initialized) { /* need to init device first */
2911 if (rs_debug_init(info) == 0)
2912 initialized = 1;
2913 else
2914 return 0;
2917 while ((serial_inw(info, VAC_UART_INT_STATUS) & \
2918 VAC_UART_STATUS_TX_EMPTY) == 0)
2920 serial_out(info, VAC_UART_TX, (unsigned short)c << 8);
2922 return 1;
2925 char getDebugChar(void)
2927 struct async_struct *info = rs_table;
2929 if (!initialized) { /* need to init device first */
2930 if (rs_debug_init(info) == 0)
2931 initialized = 1;
2932 else
2933 return 0;
2935 while (!(serial_inw(info, VAC_UART_INT_STATUS) & \
2936 VAC_UART_STATUS_RX_READY))
2939 return(serial_inp(info, VAC_UART_RX));
2942 #endif /* CONFIG_REMOTE_DEBUG */