2 * dz.c: Serial port driver for DECStations equiped
5 * Copyright (C) 1998 Olivier A. D. Lebaillif
7 * Email: olivier.lebaillif@ifrsys.com
10 * Changed IRQ to use Harald's dec internals interrupts.h
11 * removed base_addr code - moving address assignment to setup.c
12 * Changed name of dz_init to rs_init to be consistent with tc code
13 * [13-NOV-98] triemer fixed code to receive characters
14 * after patches by harald to irq code.
15 * [09-JAN-99] triemer minor fix for schedule - due to removal of timeout
16 * field from "current" - somewhere between 2.1.121 and 2.1.131
17 Qua Jun 27 15:02:26 BRT 2001
18 * [27-JUN-2001] Arnaldo Carvalho de Melo <acme@conectiva.com.br> - cleanups
20 * Parts (C) 1999 David Airlie, airlied@linux.ie
21 * [07-SEP-99] Bugfixes
24 /* #define DEBUG_DZ 1 */
26 #include <linux/module.h>
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
34 #include <linux/major.h>
35 #include <linux/param.h>
36 #include <linux/interrupt.h>
37 #include <linux/serial.h>
38 #include <linux/serialP.h>
39 #include <asm-mips/wbflush.h>
40 #include <asm/dec/interrupts.h> /* for definition of SERIAL */
42 /* for definition of struct console */
43 #ifdef CONFIG_SERIAL_CONSOLE
44 #define CONSOLE_LINE (3)
45 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
46 #if defined(CONFIG_SERIAL_CONSOLE) || defined(DEBUG_DZ)
47 #include <linux/console.h>
48 #endif /* if defined(CONFIG_SERIAL_CONSOLE) || defined(DEBUG_DZ) */
50 #include <linux/tty.h>
51 #include <linux/tty_flip.h>
53 #include <asm/uaccess.h>
55 #include <asm/dec/machtype.h>
56 #include <asm/dec/kn01.h>
57 #include <asm/dec/kn02.h>
60 #include <linux/ptrace.h>
62 #include <asm/bootinfo.h>
64 extern int (*prom_printf) (char *,...);
71 #define DZ_INTR_DEBUG 1
73 DECLARE_TASK_QUEUE(tq_serial);
75 static struct dz_serial *lines[4];
76 static unsigned char tmp_buffer[256];
82 * debugging code to send out chars via prom
84 static void debug_console( const char *s,int count)
88 for (i = 0; i < count; i++) {
90 prom_printf("%c", 13);
91 prom_printf("%c", *s++);
97 * ------------------------------------------------------------
98 * dz_in () and dz_out ()
100 * These routines are used to access the registers of the DZ
101 * chip, hiding relocation differences between implementation.
102 * ------------------------------------------------------------
105 static inline unsigned short dz_in (struct dz_serial *info, unsigned offset)
107 volatile u16 *addr = (volatile u16 *)(info->port + offset);
112 static inline void dz_out (struct dz_serial *info, unsigned offset,
113 unsigned short value)
115 volatile u16 *addr = (volatile u16 *)(info->port + offset);
120 * ------------------------------------------------------------
121 * rs_stop () and rs_start ()
123 * These routines are called before setting or resetting
124 * tty->stopped. They enable or disable transmitter interrupts,
126 * ------------------------------------------------------------
129 static void dz_stop (struct tty_struct *tty)
131 struct dz_serial *info;
132 unsigned short mask, tmp;
137 info = (struct dz_serial *)tty->driver_data;
139 mask = 1 << info->line;
140 tmp = dz_in (info, DZ_TCR); /* read the TX flag */
142 tmp &= ~mask; /* clear the TX flag */
143 dz_out (info, DZ_TCR, tmp);
146 static void dz_start (struct tty_struct *tty)
148 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
149 unsigned short mask, tmp;
151 mask = 1 << info->line;
152 tmp = dz_in (info, DZ_TCR); /* read the TX flag */
154 tmp |= mask; /* set the TX flag */
155 dz_out (info, DZ_TCR, tmp);
159 * ------------------------------------------------------------
160 * Here starts the interrupt handling routines. All of the
161 * following subroutines are declared as inline and are folded
162 * into dz_interrupt. They were separated out for readability's
165 * Note: rs_interrupt() is a "fast" interrupt, which means that it
166 * runs with interrupts turned off. People who may want to modify
167 * rs_interrupt() should try to keep the interrupt handler as fast as
168 * possible. After you are done making modifications, it is not a bad
171 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer dz.c
173 * and look at the resulting assemble code in serial.s.
175 * ------------------------------------------------------------
179 * ------------------------------------------------------------
182 * This routine is used by the interrupt handler to schedule
183 * processing in the software interrupt portion of the driver.
184 * ------------------------------------------------------------
186 static inline void dz_sched_event (struct dz_serial *info, int event)
188 info->event |= 1 << event;
189 queue_task(&info->tqueue, &tq_serial);
194 * ------------------------------------------------------------
197 * This routine deals with inputs from any lines.
198 * ------------------------------------------------------------
200 static inline void receive_chars (struct dz_serial *info_in)
202 struct dz_serial *info;
203 struct tty_struct *tty = 0;
204 struct async_icount *icount;
206 unsigned short status, tmp;
210 * This code is going to be a problem... the call to tty_flip_buffer
211 * is going to need to be rethought...
214 status = dz_in (info_in, DZ_RBUF);
215 info = lines[LINE(status)];
217 /* punt so we don't get duplicate characters */
218 if (!(status & DZ_DVAL))
221 ch = UCHAR(status); /* grab the char */
224 if (info->is_console) {
226 return; /* it's a break ... */
230 tty = info->tty; /* now tty points to the proper dev */
231 icount = &info->icount;
235 if (tty->flip.count >= TTY_FLIPBUF_SIZE) break;
237 *tty->flip.char_buf_ptr = ch;
238 *tty->flip.flag_buf_ptr = 0;
241 /* keep track of the statistics */
242 if (status & (DZ_OERR | DZ_FERR | DZ_PERR)) {
243 if (status & DZ_PERR) /* parity error */
245 else if (status & DZ_FERR) /* frame error */
247 if (status & DZ_OERR) /* overrun error */
251 * Check to see if we should ignore the character and
252 * mask off conditions that should be ignored
255 if (status & info->ignore_status_mask) {
261 /* mask off the error conditions we want to ignore */
262 tmp = status & info->read_status_mask;
265 *tty->flip.flag_buf_ptr = TTY_PARITY;
267 debug_console("PERR\n",5);
268 #endif /* DEBUG_DZ */
269 } else if (tmp & DZ_FERR) {
270 *tty->flip.flag_buf_ptr = TTY_FRAME;
272 debug_console("FERR\n",5);
273 #endif /* DEBUG_DZ */
274 } if (tmp & DZ_OERR) {
276 debug_console("OERR\n",5);
277 #endif /* DEBUG_DZ */
278 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
280 tty->flip.flag_buf_ptr++;
281 tty->flip.char_buf_ptr++;
282 *tty->flip.flag_buf_ptr = TTY_OVERRUN;
286 tty->flip.flag_buf_ptr++;
287 tty->flip.char_buf_ptr++;
291 } while (status & DZ_DVAL);
294 tty_flip_buffer_push(tty);
298 * ------------------------------------------------------------
301 * This routine deals with outputs to any lines.
302 * ------------------------------------------------------------
304 static inline void transmit_chars (struct dz_serial *info)
308 if (info->x_char) { /* XON/XOFF chars */
309 dz_out(info, DZ_TDR, info->x_char);
315 /* if nothing to do or stopped or hardware stopped */
316 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
317 info->tty->hw_stopped) {
323 * If something to do ... (rember the dz has no output fifo so we go
324 * one char at a time :-<
326 tmp = (unsigned short) info->xmit_buf[info->xmit_tail++];
327 dz_out(info, DZ_TDR, tmp);
328 info->xmit_tail = info->xmit_tail & (DZ_XMIT_SIZE - 1);
331 if (--info->xmit_cnt < WAKEUP_CHARS)
332 dz_sched_event(info, DZ_EVENT_WRITE_WAKEUP);
335 if (info->xmit_cnt <= 0)
340 * ------------------------------------------------------------
341 * check_modem_status ()
343 * Only valid for the MODEM line duh !
344 * ------------------------------------------------------------
346 static inline void check_modem_status (struct dz_serial *info)
348 unsigned short status;
350 /* if not ne modem line just return */
351 if (info->line != DZ_MODEM)
354 status = dz_in(info, DZ_MSR);
356 /* it's easy, since DSR2 is the only bit in the register */
362 * ------------------------------------------------------------
365 * this is the main interrupt routine for the DZ chip.
366 * It deals with the multiple ports.
367 * ------------------------------------------------------------
369 static void dz_interrupt (int irq, void *dev, struct pt_regs *regs)
371 struct dz_serial *info;
372 unsigned short status;
374 /* get the reason why we just got an irq */
375 status = dz_in((struct dz_serial *)dev, DZ_CSR);
376 info = lines[LINE(status)]; /* re-arrange info the proper port */
378 if (status & DZ_RDONE)
379 receive_chars(info); /* the receive function */
381 if (status & DZ_TRDY)
382 transmit_chars (info);
386 * -------------------------------------------------------------------
387 * Here ends the DZ interrupt routines.
388 * -------------------------------------------------------------------
392 * This routine is used to handle the "bottom half" processing for the
393 * serial driver, known also the "software interrupt" processing.
394 * This processing is done at the kernel interrupt level, after the
395 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
396 * is where time-consuming activities which can not be done in the
397 * interrupt driver proper are done; the interrupt driver schedules
398 * them using rs_sched_event(), and they get done here.
400 static void do_serial_bh (void)
402 run_task_queue (&tq_serial);
405 static void do_softint (void *private_data)
407 struct dz_serial *info = (struct dz_serial *) private_data;
408 struct tty_struct *tty = info->tty;
413 if (test_and_clear_bit(DZ_EVENT_WRITE_WAKEUP, &info->event)) {
414 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
415 tty->ldisc.write_wakeup)
416 (tty->ldisc.write_wakeup) (tty);
417 wake_up_interruptible (&tty->write_wait);
422 * -------------------------------------------------------------------
423 * This routine is called from the scheduler tqueue when the interrupt
424 * routine has signalled that a hangup has occurred. The path of
425 * hangup processing is:
427 * serial interrupt routine -> (scheduler tqueue) ->
428 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
429 * -------------------------------------------------------------------
431 static void do_serial_hangup (void *private_data)
433 struct dz_serial *info = (struct dz_serial *) private_data;
434 struct tty_struct *tty = info->tty;
443 * -------------------------------------------------------------------
446 * various initialization tasks
447 * -------------------------------------------------------------------
449 static int startup (struct dz_serial *info)
451 unsigned long page, flags;
454 if (info->is_initialized)
460 if (info->tty) set_bit(TTY_IO_ERROR, &info->tty->flags);
461 restore_flags(flags);
465 if (!info->xmit_buf) {
466 page = get_zeroed_page(GFP_KERNEL);
468 restore_flags (flags);
471 info->xmit_buf = (unsigned char *)page;
475 clear_bit(TTY_IO_ERROR, &info->tty->flags);
477 /* enable the interrupt and the scanning */
478 tmp = dz_in(info, DZ_CSR);
479 tmp |= (DZ_RIE | DZ_TIE | DZ_MSE);
480 dz_out(info, DZ_CSR, tmp);
482 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
484 change_speed(info); /* set up the speed */
487 * Clear the line transmitter buffer I can't figure out why I need to
488 * do this - but its necessary - in order for the console portion and
489 * the interrupt portion to live happily side by side.
492 info->is_initialized = 1;
494 restore_flags(flags);
500 * -------------------------------------------------------------------
503 * This routine will shutdown a serial port; interrupts are disabled, and
504 * DTR is dropped if the hangup on close termio flag is on.
505 * -------------------------------------------------------------------
507 static void shutdown (struct dz_serial *info)
512 if (!info->is_initialized)
519 info->cflags &= ~DZ_CREAD; /* turn off receive enable flag */
520 dz_out(info, DZ_LPR, info->cflags);
522 if (info->xmit_buf) { /* free Tx buffer */
523 free_page((unsigned long)info->xmit_buf);
527 if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
528 tmp = dz_in(info, DZ_TCR);
529 if (tmp & DZ_MODEM_DTR) {
530 tmp &= ~DZ_MODEM_DTR;
531 dz_out(info, DZ_TCR, tmp);
536 set_bit (TTY_IO_ERROR, &info->tty->flags);
538 info->is_initialized = 0;
540 restore_flags (flags);
544 * -------------------------------------------------------------------
548 * -------------------------------------------------------------------
550 static void change_speed (struct dz_serial *info)
556 if (!info->tty || !info->tty->termios)
561 info->cflags = info->line;
563 cflag = info->tty->termios->c_cflag;
565 switch (cflag & CSIZE) {
567 info->cflags |= DZ_CS5;
570 info->cflags |= DZ_CS6;
573 info->cflags |= DZ_CS7;
577 info->cflags |= DZ_CS8;
581 info->cflags |= DZ_CSTOPB;
583 info->cflags |= DZ_PARENB;
585 info->cflags |= DZ_PARODD;
587 baud = tty_get_baud_rate(info->tty);
590 info->cflags |= DZ_B50;
593 info->cflags |= DZ_B75;
596 info->cflags |= DZ_B110;
599 info->cflags |= DZ_B134;
602 info->cflags |= DZ_B150;
605 info->cflags |= DZ_B300;
608 info->cflags |= DZ_B600;
611 info->cflags |= DZ_B1200;
614 info->cflags |= DZ_B1800;
617 info->cflags |= DZ_B2000;
620 info->cflags |= DZ_B2400;
623 info->cflags |= DZ_B3600;
626 info->cflags |= DZ_B4800;
629 info->cflags |= DZ_B7200;
633 info->cflags |= DZ_B9600;
636 info->cflags |= DZ_RXENAB;
637 dz_out(info, DZ_LPR, info->cflags);
639 /* setup accept flag */
640 info->read_status_mask = DZ_OERR;
641 if (I_INPCK(info->tty))
642 info->read_status_mask |= (DZ_FERR | DZ_PERR);
644 /* characters to ignore */
645 info->ignore_status_mask = 0;
646 if (I_IGNPAR(info->tty))
647 info->ignore_status_mask |= (DZ_FERR | DZ_PERR);
649 restore_flags(flags);
653 * -------------------------------------------------------------------
657 * -------------------------------------------------------------------
659 static void dz_flush_chars (struct tty_struct *tty)
661 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
664 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
669 dz_start (info->tty);
670 restore_flags(flags);
675 * -------------------------------------------------------------------
678 * main output routine.
679 * -------------------------------------------------------------------
681 static int dz_write (struct tty_struct *tty, int from_user,
682 const unsigned char *buf, int count)
684 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
693 tmp_buf = tmp_buffer;
698 c = MIN(count, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1,
699 DZ_XMIT_SIZE - info->xmit_head));
703 c -= copy_from_user (tmp_buf, buf, c);
712 c = MIN(c, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1,
713 DZ_XMIT_SIZE - info->xmit_head));
714 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
715 info->xmit_head = ((info->xmit_head + c) &
718 restore_flags(flags);
729 c = MIN(count, MIN(DZ_XMIT_SIZE - info->xmit_cnt - 1,
730 DZ_XMIT_SIZE - info->xmit_head));
732 restore_flags (flags);
735 memcpy(info->xmit_buf + info->xmit_head, buf, c);
736 info->xmit_head = ((info->xmit_head + c) &
739 restore_flags(flags);
747 if (info->xmit_cnt) {
749 if (!tty->hw_stopped) {
750 dz_start (info->tty);
759 * -------------------------------------------------------------------
762 * compute the amount of space available for writing.
763 * -------------------------------------------------------------------
765 static int dz_write_room (struct tty_struct *tty)
767 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
770 ret = DZ_XMIT_SIZE - info->xmit_cnt - 1;
778 * -------------------------------------------------------------------
779 * dz_chars_in_buffer ()
781 * compute the amount of char left to be transmitted
782 * -------------------------------------------------------------------
784 static int dz_chars_in_buffer (struct tty_struct *tty)
786 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
788 return info->xmit_cnt;
792 * -------------------------------------------------------------------
795 * Empty the output buffer
796 * -------------------------------------------------------------------
798 static void dz_flush_buffer (struct tty_struct *tty)
800 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
803 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
806 wake_up_interruptible (&tty->write_wait);
808 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
809 tty->ldisc.write_wakeup)
810 tty->ldisc.write_wakeup(tty);
814 * ------------------------------------------------------------
815 * dz_throttle () and dz_unthrottle ()
817 * This routine is called by the upper-layer tty layer to signal that
818 * incoming characters should be throttled (or not).
819 * ------------------------------------------------------------
821 static void dz_throttle (struct tty_struct *tty)
823 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
826 info->x_char = STOP_CHAR(tty);
829 static void dz_unthrottle (struct tty_struct *tty)
831 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
837 info->x_char = START_CHAR(tty);
841 static void dz_send_xchar (struct tty_struct *tty, char ch)
843 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
852 * ------------------------------------------------------------
853 * rs_ioctl () and friends
854 * ------------------------------------------------------------
856 static int get_serial_info(struct dz_serial *info,
857 struct serial_struct *retinfo)
859 struct serial_struct tmp;
864 memset (&tmp, 0, sizeof(tmp));
866 tmp.type = info->type;
867 tmp.line = info->line;
868 tmp.port = info->port;
870 tmp.flags = info->flags;
871 tmp.baud_base = info->baud_base;
872 tmp.close_delay = info->close_delay;
873 tmp.closing_wait = info->closing_wait;
875 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
878 static int set_serial_info (struct dz_serial *info,
879 struct serial_struct *new_info)
881 struct serial_struct new_serial;
882 struct dz_serial old_info;
888 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
893 if (!capable(CAP_SYS_ADMIN))
900 * OK, past this point, all the error checking has been done.
901 * At this point, we start making changes.....
904 info->baud_base = new_serial.baud_base;
905 info->type = new_serial.type;
906 info->close_delay = new_serial.close_delay;
907 info->closing_wait = new_serial.closing_wait;
909 retval = startup(info);
915 * get_lsr_info - get line status register info
917 * Purpose: Let user call ioctl() to get info when the UART physically
918 * is emptied. On bus types like RS485, the transmitter must
919 * release the bus after transmitting. This must be done when
920 * the transmit shift register is empty, not be done when the
921 * transmit holding register is empty. This functionality
922 * allows an RS485 driver to be written in user space.
924 static int get_lsr_info (struct dz_serial *info, unsigned int *value)
926 unsigned short status = dz_in (info, DZ_LPR);
928 return put_user (status, value);
932 * This routine sends a break character out the serial port.
934 static void send_break (struct dz_serial *info, int duration)
937 unsigned short tmp, mask;
942 mask = 1 << info->line;
943 tmp = dz_in (info, DZ_TCR);
946 current->state = TASK_INTERRUPTIBLE;
949 dz_out(info, DZ_TCR, tmp);
950 schedule_timeout(duration);
952 dz_out(info, DZ_TCR, tmp);
953 restore_flags(flags);
956 static int dz_ioctl(struct tty_struct *tty, struct file *file,
957 unsigned int cmd, unsigned long arg)
960 struct dz_serial * info = (struct dz_serial *)tty->driver_data;
963 if (cmd != TIOCGSERIAL && cmd != TIOCSSERIAL &&
964 cmd != TIOCSERCONFIG && cmd != TIOCSERGWILD &&
965 cmd != TIOCSERSWILD && cmd != TIOCSERGSTRUCT) {
966 if (tty->flags & (1 << TTY_IO_ERROR))
971 case TCSBRK: /* SVID version: non-zero arg --> no break */
972 retval = tty_check_change(tty);
975 tty_wait_until_sent(tty, 0);
977 send_break(info, HZ/4); /* 1/4 second */
980 case TCSBRKP: /* support for POSIX tcsendbreak() */
981 retval = tty_check_change(tty);
984 tty_wait_until_sent(tty, 0);
985 send_break(info, arg ? arg*(HZ/10) : HZ/4);
989 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *)arg);
992 if (get_user (arg, (unsigned long *)arg))
995 tty->termios->c_cflag = (tty->termios->c_cflag & ~CLOCAL) |
1000 return get_serial_info(info, (struct serial_struct *)arg);
1003 return set_serial_info(info, (struct serial_struct *) arg);
1005 case TIOCSERGETLSR: /* Get line status register */
1006 return get_lsr_info (info, (unsigned int *)arg);
1008 case TIOCSERGSTRUCT:
1009 return copy_to_user((struct dz_serial *)arg, info,
1010 sizeof(struct dz_serial)) ? -EFAULT : 0;
1013 return -ENOIOCTLCMD;
1019 static void dz_set_termios (struct tty_struct *tty,
1020 struct termios *old_termios)
1022 struct dz_serial *info = (struct dz_serial *)tty->driver_data;
1024 if (tty->termios->c_cflag == old_termios->c_cflag)
1027 change_speed (info);
1029 if ((old_termios->c_cflag & CRTSCTS) &&
1030 !(tty->termios->c_cflag & CRTSCTS)) {
1031 tty->hw_stopped = 0;
1037 * ------------------------------------------------------------
1040 * This routine is called when the serial port gets closed. First, we
1041 * wait for the last remaining data to be sent. Then, we turn off
1042 * the transmit enable and receive enable flags.
1043 * ------------------------------------------------------------
1045 static void dz_close(struct tty_struct *tty, struct file *filp)
1047 struct dz_serial * info = (struct dz_serial *)tty->driver_data;
1048 unsigned long flags;
1053 save_and_cli(flags);
1055 if (tty_hung_up_p(filp)) {
1056 restore_flags(flags);
1060 if ((tty->count == 1) && (info->count != 1)) {
1062 * Uh, oh. tty->count is 1, which means that the tty structure
1063 * will be freed. Info->count should always be one in these
1064 * conditions. If it's greater than one, we've got real
1065 * problems, since it means the serial port won't be shutdown.
1067 printk("dz_close: bad serial port count; tty->count is 1, "
1068 "info->count is %d\n", info->count);
1072 if (--info->count < 0) {
1073 printk("ds_close: bad serial port count for ttyS%02d: %d\n",
1074 info->line, info->count);
1079 restore_flags(flags);
1082 info->flags |= DZ_CLOSING;
1084 * Now we wait for the transmit buffer to clear; and we notify the line
1085 * discipline to only process XON/XOFF characters.
1089 if (info->closing_wait != DZ_CLOSING_WAIT_NONE)
1090 tty_wait_until_sent(tty, info->closing_wait);
1093 * At this point we stop accepting input. To do this, we disable the
1094 * receive line status interrupts.
1098 if (tty->driver->flush_buffer)
1099 tty->driver->flush_buffer (tty);
1100 if (tty->ldisc.flush_buffer)
1101 tty->ldisc.flush_buffer (tty);
1106 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1107 if (tty->ldisc.close)
1108 tty->ldisc.close(tty);
1109 tty->ldisc = ldiscs[N_TTY];
1110 tty->termios->c_line = N_TTY;
1111 if (tty->ldisc.open)
1112 tty->ldisc.open(tty);
1114 if (info->blocked_open) {
1115 if (info->close_delay) {
1116 current->state = TASK_INTERRUPTIBLE;
1117 schedule_timeout(info->close_delay);
1119 wake_up_interruptible(&info->open_wait);
1122 info->flags &= ~(DZ_NORMAL_ACTIVE | DZ_CLOSING);
1123 wake_up_interruptible(&info->close_wait);
1125 restore_flags(flags);
1129 * dz_hangup () --- called by tty_hangup() when a hangup is signaled.
1131 static void dz_hangup (struct tty_struct *tty)
1133 struct dz_serial *info = (struct dz_serial *) tty->driver_data;
1135 dz_flush_buffer(tty);
1139 info->flags &= ~DZ_NORMAL_ACTIVE;
1141 wake_up_interruptible(&info->open_wait);
1145 * ------------------------------------------------------------
1146 * rs_open() and friends
1147 * ------------------------------------------------------------
1149 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1150 struct dz_serial *info)
1152 DECLARE_WAITQUEUE(wait, current);
1157 * If the device is in the middle of being closed, then block
1158 * until it's done, and then try again.
1160 if (info->flags & DZ_CLOSING) {
1161 interruptible_sleep_on(&info->close_wait);
1166 * If non-blocking mode is set, or the port is not enabled, then make
1167 * the check up front and then exit.
1169 if ((filp->f_flags & O_NONBLOCK) ||
1170 (tty->flags & (1 << TTY_IO_ERROR))) {
1171 info->flags |= DZ_NORMAL_ACTIVE;
1176 if (tty->termios->c_cflag & CLOCAL)
1180 * Block waiting for the carrier detect and the line to become free
1181 * (i.e., not in use by the callout). While we are in this loop,
1182 * info->count is dropped by one, so that dz_close() knows when to free
1183 * things. We restore it upon exit, either normal or abnormal.
1186 add_wait_queue(&info->open_wait, &wait);
1189 info->blocked_open++;
1191 set_current_state(TASK_INTERRUPTIBLE);
1192 if (tty_hung_up_p (filp) || !(info->is_initialized)) {
1196 if (!(info->flags & DZ_CLOSING) && do_clocal)
1198 if (signal_pending(current)) {
1199 retval = -ERESTARTSYS;
1205 current->state = TASK_RUNNING;
1206 remove_wait_queue (&info->open_wait, &wait);
1207 if (!tty_hung_up_p(filp))
1209 info->blocked_open--;
1213 info->flags |= DZ_NORMAL_ACTIVE;
1218 * This routine is called whenever a serial port is opened. It
1219 * enables interrupts for a serial port. It also performs the
1220 * serial-specific initialization for the tty structure.
1222 static int dz_open (struct tty_struct *tty, struct file *filp)
1224 struct dz_serial *info;
1230 * The dz lines for the mouse/keyboard must be opened using their
1231 * respective drivers.
1233 if ((line < 0) || (line >= DZ_NB_PORT))
1236 if ((line == DZ_KEYBOARD) || (line == DZ_MOUSE))
1242 tty->driver_data = info;
1246 * Start up serial port
1248 retval = startup (info);
1252 retval = block_til_ready (tty, filp, info);
1259 static void show_serial_version (void)
1261 printk("%s%s\n", dz_name, dz_version);
1264 static struct tty_driver *serial_driver;
1266 static struct tty_operations serial_ops = {
1270 .flush_chars = dz_flush_chars,
1271 .write_room = dz_write_room,
1272 .chars_in_buffer = dz_chars_in_buffer,
1273 .flush_buffer = dz_flush_buffer,
1275 .throttle = dz_throttle,
1276 .unthrottle = dz_unthrottle,
1277 .send_xchar = dz_send_xchar,
1278 .set_termios = dz_set_termios,
1281 .hangup = dz_hangup,
1284 int __init dz_init(void)
1287 struct dz_serial *info;
1289 serial_driver = alloc_tty_driver(DZ_NB_PORT);
1293 /* Setup base handler, and timer table. */
1294 init_bh(SERIAL_BH, do_serial_bh);
1296 show_serial_version();
1298 serial_driver->owner = THIS_MODULE;
1299 serial_driver->devfs_name = "tts/";
1300 serial_driver->name = "ttyS";
1301 serial_driver->major = TTY_MAJOR;
1302 serial_driver->minor_start = 64;
1303 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1304 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1305 serial_driver->init_termios = tty_std_termios;
1306 serial_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL |
1308 serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1309 tty_set_operations(serial_driver, &serial_ops);
1311 if (tty_register_driver(serial_driver))
1312 panic("Couldn't register serial driver\n");
1314 save_flags(flags); cli();
1315 for (i=0; i < DZ_NB_PORT; i++) {
1318 info->magic = SERIAL_MAGIC;
1320 if ((mips_machtype == MACH_DS23100) ||
1321 (mips_machtype == MACH_DS5100))
1322 info->port = (unsigned long) KN01_DZ11_BASE;
1324 info->port = (unsigned long) KN02_DZ11_BASE;
1328 info->close_delay = 50;
1329 info->closing_wait = 3000;
1333 info->blocked_open = 0;
1334 info->tqueue.routine = do_softint;
1335 info->tqueue.data = info;
1336 info->tqueue_hangup.routine = do_serial_hangup;
1337 info->tqueue_hangup.data = info;
1338 init_waitqueue_head(&info->open_wait);
1339 init_waitqueue_head(&info->close_wait);
1342 * If we are pointing to address zero then punt - not correctly
1343 * set up in setup.c to handle this.
1348 printk("ttyS%02d at 0x%08x (irq = %d)\n", info->line,
1349 info->port, SERIAL);
1351 tty_register_device(serial_driver, info->line, NULL);
1354 /* Reset the chip */
1355 #ifndef CONFIG_SERIAL_CONSOLE
1358 dz_out(info, DZ_CSR, DZ_CLR);
1359 while ((tmp = dz_in(info,DZ_CSR)) & DZ_CLR);
1362 /* Enable scanning */
1363 dz_out(info, DZ_CSR, DZ_MSE);
1368 * Order matters here... the trick is that flags is updated... in
1369 * request_irq - to immediatedly obliterate it is unwise.
1371 restore_flags(flags);
1373 if (request_irq(SERIAL, dz_interrupt, SA_INTERRUPT, "DZ", lines[0]))
1374 panic("Unable to register DZ interrupt\n");
1379 #ifdef CONFIG_SERIAL_CONSOLE
1380 static void dz_console_put_char (unsigned char ch)
1382 unsigned long flags;
1384 unsigned short tmp = ch;
1386 * this code sends stuff out to serial device - spinning its wheels and
1390 /* force the issue - point it at lines[3]*/
1391 dz_console = &multi[CONSOLE_LINE];
1393 save_and_cli(flags);
1395 /* spin our wheels */
1396 while (((dz_in(dz_console, DZ_CSR) & DZ_TRDY) != DZ_TRDY) && loops--)
1399 /* Actually transmit the character. */
1400 dz_out(dz_console, DZ_TDR, tmp);
1402 restore_flags(flags);
1406 * -------------------------------------------------------------------
1407 * dz_console_print ()
1409 * dz_console_print is registered for printk.
1410 * The console must be locked when we get here.
1411 * -------------------------------------------------------------------
1413 static void dz_console_print (struct console *cons,
1418 prom_printf((char *)str);
1422 dz_console_put_char('\r');
1423 dz_console_put_char(*str++);
1427 static struct tty_driver *dz_console_device(struct console *c, int *index)
1430 return serial_driver;
1433 static int __init dz_console_setup(struct console *co, char *options)
1438 int cflag = CREAD | HUPCL | CLOCAL;
1440 unsigned short mask,tmp;
1443 baud = simple_strtoul(options, NULL, 10);
1445 while (*s >= '0' && *s <= '9')
1454 * Now construct a cflag setting.
1492 /* TOFIX: force to console line */
1493 dz_console = &multi[CONSOLE_LINE];
1494 if ((mips_machtype == MACH_DS23100) || (mips_machtype == MACH_DS5100))
1495 dz_console->port = KN01_DZ11_BASE;
1497 dz_console->port = KN02_DZ11_BASE;
1498 dz_console->line = CONSOLE_LINE;
1500 dz_out(dz_console, DZ_CSR, DZ_CLR);
1501 while ((tmp = dz_in(dz_console,DZ_CSR)) & DZ_CLR)
1504 /* enable scanning */
1505 dz_out(dz_console, DZ_CSR, DZ_MSE);
1507 /* Set up flags... */
1508 dz_console->cflags = 0;
1509 dz_console->cflags |= DZ_B9600;
1510 dz_console->cflags |= DZ_CS8;
1511 dz_console->cflags |= DZ_PARENB;
1512 dz_out(dz_console, DZ_LPR, dz_console->cflags);
1514 mask = 1 << dz_console->line;
1515 tmp = dz_in (dz_console, DZ_TCR); /* read the TX flag */
1516 if (!(tmp & mask)) {
1517 tmp |= mask; /* set the TX flag */
1518 dz_out (dz_console, DZ_TCR, tmp);
1524 static struct console dz_sercons = {
1526 .write = dz_console_print,
1527 .device = dz_console_device,
1528 .setup = dz_console_setup,
1529 .flags = CON_CONSDEV | CON_PRINTBUFFER,
1530 .index = CONSOLE_LINE,
1533 void __init dz_serial_console_init(void)
1535 register_console(&dz_sercons);
1538 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
1540 MODULE_LICENSE("GPL");