Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / tc / zs.c
blob7fa446c0053af0e817c87fa3dfc6109e63bf7c1a
1 /*
2 * decserial.c: Serial port driver for IOASIC DECsatations.
4 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
5 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
7 * DECstation changes
8 * Copyright (C) 1998 Harald Koerfgen (Harald.Koerfgen@home.ivm.de)
10 * For the rest of the code the original Copyright applies:
11 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
12 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 * Keyboard and mouse are not supported right now. If you want to change this,
15 * you might want to have a look at drivers/sbus/char/sunserial.c to see
16 * how this might be done. HK
19 #include <linux/config.h>
20 #include <linux/errno.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/major.h>
28 #include <linux/string.h>
29 #include <linux/fcntl.h>
30 #include <linux/mm.h>
31 #include <linux/kernel.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #ifdef CONFIG_SERIAL_CONSOLE
35 #include <linux/console.h>
36 #endif
38 #include <asm/io.h>
39 #include <asm/pgtable.h>
40 #include <asm/irq.h>
41 #include <asm/system.h>
42 #include <asm/segment.h>
43 #include <asm/bitops.h>
44 #include <asm/uaccess.h>
45 #include <asm/wbflush.h>
46 #include <asm/dec/interrupts.h>
47 #include <asm/dec/machtype.h>
48 #include <asm/dec/tc.h>
49 #include <asm/dec/ioasic_addrs.h>
50 #ifdef CONFIG_KGDB
51 #include <asm/kgdb.h>
52 #endif
54 #include "zs.h"
58 * It would be nice to dynamically allocate everything that
59 * depends on NUM_SERIAL, so we could support any number of
60 * Z8530s, but for now...
62 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
63 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
65 #define RECOVERY_DELAY udelay(2)
67 struct dec_zschannel zs_channels[NUM_CHANNELS];
69 struct dec_serial zs_soft[NUM_CHANNELS];
70 int zs_channels_found;
71 struct dec_serial *zs_chain; /* list of all channels */
73 struct tty_struct zs_ttys[NUM_CHANNELS];
75 #ifdef CONFIG_SERIAL_CONSOLE
76 static struct console sercons;
77 #endif
79 #ifdef CONFIG_KGDB
80 struct dec_zschannel *zs_kgdbchan;
81 static unsigned char scc_inittab[] = {
82 9, 0x80, /* reset A side (CHRA) */
83 13, 0, /* set baud rate divisor */
84 12, 1,
85 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
86 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
87 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
88 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
89 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
91 #endif
93 static unsigned char zs_init_regs[16] __initdata = {
94 0, /* write 0 */
95 0, /* write 1 */
96 0xf0, /* write 2 */
97 (Rx8), /* write 3 */
98 (X16CLK | SB1), /* write 4 */
99 (Tx8), /* write 5 */
100 0, 0, 0, /* write 6, 7, 8 */
101 (VIS), /* write 9 */
102 (NRZ), /* write 10 */
103 (TCBR | RCBR), /* write 11 */
104 0, 0, /* BRG time constant, write 12 + 13 */
105 (BRSRC | BRENABL), /* write 14 */
106 0 /* write 15 */
109 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
111 DECLARE_TASK_QUEUE(tq_zs_serial);
113 struct tty_driver serial_driver, callout_driver;
114 static int serial_refcount;
116 /* serial subtype definitions */
117 #define SERIAL_TYPE_NORMAL 1
118 #define SERIAL_TYPE_CALLOUT 2
120 /* number of characters left in xmit buffer before we ask for more */
121 #define WAKEUP_CHARS 256
124 * Debugging.
126 #undef SERIAL_DEBUG_INTR
127 #undef SERIAL_DEBUG_OPEN
128 #undef SERIAL_DEBUG_FLOW
129 #undef SERIAL_DEBUG_THROTTLE
130 #undef SERIAL_PARANOIA_CHECK
132 #define RS_STROBE_TIME 10
133 #define RS_ISR_PASS_LIMIT 256
135 #define _INLINE_ inline
137 static void probe_sccs(void);
138 static void change_speed(struct dec_serial *info);
139 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
141 static struct tty_struct *serial_table[NUM_CHANNELS];
142 static struct termios *serial_termios[NUM_CHANNELS];
143 static struct termios *serial_termios_locked[NUM_CHANNELS];
145 #ifndef MIN
146 #define MIN(a,b) ((a) < (b) ? (a) : (b))
147 #endif
150 * tmp_buf is used as a temporary buffer by serial_write. We need to
151 * lock it in case the copy_from_user blocks while swapping in a page,
152 * and some other program tries to do a serial write at the same time.
153 * Since the lock will only come under contention when the system is
154 * swapping and available memory is low, it makes sense to share one
155 * buffer across all the serial ports, since it significantly saves
156 * memory if large numbers of serial ports are open.
158 static unsigned char tmp_buf[4096]; /* This is cheating */
159 static DECLARE_MUTEX(tmp_buf_sem);
161 static inline int serial_paranoia_check(struct dec_serial *info,
162 dev_t device, const char *routine)
164 #ifdef SERIAL_PARANOIA_CHECK
165 static const char *badmagic =
166 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
167 static const char *badinfo =
168 "Warning: null mac_serial for (%d, %d) in %s\n";
170 if (!info) {
171 printk(badinfo, MAJOR(device), MINOR(device), routine);
172 return 1;
174 if (info->magic != SERIAL_MAGIC) {
175 printk(badmagic, MAJOR(device), MINOR(device), routine);
176 return 1;
178 #endif
179 return 0;
183 * This is used to figure out the divisor speeds and the timeouts
185 static int baud_table[] = {
186 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
187 9600, 19200, 38400, 57600, 0, 0 };
190 * Reading and writing Z8530 registers.
192 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
193 unsigned char reg)
195 unsigned char retval;
197 if (reg != 0) {
198 *channel->control = reg & 0xf;
199 wbflush(); RECOVERY_DELAY;
201 retval = *channel->control;
202 RECOVERY_DELAY;
203 return retval;
206 static inline void write_zsreg(struct dec_zschannel *channel,
207 unsigned char reg, unsigned char value)
209 if (reg != 0) {
210 *channel->control = reg & 0xf;
211 wbflush(); RECOVERY_DELAY;
213 *channel->control = value;
214 wbflush(); RECOVERY_DELAY;
215 return;
218 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
220 unsigned char retval;
222 retval = *channel->data;
223 RECOVERY_DELAY;
224 return retval;
227 static inline void write_zsdata(struct dec_zschannel *channel,
228 unsigned char value)
230 *channel->data = value;
231 wbflush(); RECOVERY_DELAY;
232 return;
235 static inline void load_zsregs(struct dec_zschannel *channel,
236 unsigned char *regs)
238 /* ZS_CLEARERR(channel);
239 ZS_CLEARFIFO(channel); */
240 /* Load 'em up */
241 write_zsreg(channel, R4, regs[R4]);
242 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
243 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
244 write_zsreg(channel, R9, regs[R9]);
245 write_zsreg(channel, R1, regs[R1]);
246 write_zsreg(channel, R2, regs[R2]);
247 write_zsreg(channel, R10, regs[R10]);
248 write_zsreg(channel, R11, regs[R11]);
249 write_zsreg(channel, R12, regs[R12]);
250 write_zsreg(channel, R13, regs[R13]);
251 write_zsreg(channel, R14, regs[R14]);
252 write_zsreg(channel, R15, regs[R15]);
253 write_zsreg(channel, R3, regs[R3]);
254 write_zsreg(channel, R5, regs[R5]);
255 return;
258 /* Sets or clears DTR/RTS on the requested line */
259 static inline void zs_rtsdtr(struct dec_serial *ss, int set)
261 if (ss->zs_channel != ss->zs_chan_a) {
262 if (set)
263 ss->zs_chan_a->curregs[5] |= (RTS | DTR);
264 else
265 ss->zs_chan_a->curregs[5] &= ~(RTS | DTR);
266 write_zsreg(ss->zs_chan_a, 5, ss->zs_chan_a->curregs[5]);
268 return;
271 /* Utility routines for the Zilog */
272 static inline int get_zsbaud(struct dec_serial *ss)
274 struct dec_zschannel *channel = ss->zs_channel;
275 int brg;
277 /* The baud rate is split up between two 8-bit registers in
278 * what is termed 'BRG time constant' format in my docs for
279 * the chip, it is a function of the clk rate the chip is
280 * receiving which happens to be constant.
282 brg = (read_zsreg(channel, 13) << 8);
283 brg |= read_zsreg(channel, 12);
284 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
287 /* On receive, this clears errors and the receiver interrupts */
288 static inline void rs_recv_clear(struct dec_zschannel *zsc)
290 write_zsreg(zsc, 0, ERR_RES);
291 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
295 * ----------------------------------------------------------------------
297 * Here starts the interrupt handling routines. All of the following
298 * subroutines are declared as inline and are folded into
299 * rs_interrupt(). They were separated out for readability's sake.
301 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
302 * -----------------------------------------------------------------------
306 * This routine is used by the interrupt handler to schedule
307 * processing in the software interrupt portion of the driver.
309 static _INLINE_ void rs_sched_event(struct dec_serial *info,
310 int event)
312 info->event |= 1 << event;
313 queue_task(&info->tqueue, &tq_zs_serial);
314 mark_bh(SERIAL_BH);
317 static _INLINE_ void receive_chars(struct dec_serial *info,
318 struct pt_regs *regs)
320 struct tty_struct *tty = info->tty;
321 unsigned char ch, stat, flag;
323 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
325 stat = read_zsreg(info->zs_channel, R1);
326 ch = read_zsdata(info->zs_channel);
328 #ifdef CONFIG_KGDB
329 if (info->kgdb_channel) {
330 if (ch == 0x03 || ch == '$')
331 breakpoint();
332 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
333 write_zsreg(info->zs_channel, 0, ERR_RES);
334 return;
336 #endif
337 if (!tty)
338 continue;
340 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
341 static int flip_buf_ovf;
342 ++flip_buf_ovf;
343 continue;
345 tty->flip.count++;
347 static int flip_max_cnt;
348 if (flip_max_cnt < tty->flip.count)
349 flip_max_cnt = tty->flip.count;
351 if (stat & Rx_OVR) {
352 flag = TTY_OVERRUN;
353 } else if (stat & FRM_ERR) {
354 flag = TTY_FRAME;
355 } else if (stat & PAR_ERR) {
356 flag = TTY_PARITY;
357 } else
358 flag = 0;
359 if (flag)
360 /* reset the error indication */
361 write_zsreg(info->zs_channel, 0, ERR_RES);
362 *tty->flip.flag_buf_ptr++ = flag;
363 *tty->flip.char_buf_ptr++ = ch;
365 tty_flip_buffer_push(tty);
368 static void transmit_chars(struct dec_serial *info)
370 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
371 return;
372 info->tx_active = 0;
374 if (info->x_char) {
375 /* Send next char */
376 write_zsdata(info->zs_channel, info->x_char);
377 info->x_char = 0;
378 info->tx_active = 1;
379 return;
382 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped) {
383 write_zsreg(info->zs_channel, 0, RES_Tx_P);
384 return;
386 /* Send char */
387 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
388 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
389 info->xmit_cnt--;
390 info->tx_active = 1;
392 if (info->xmit_cnt < WAKEUP_CHARS)
393 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
396 static _INLINE_ void status_handle(struct dec_serial *info)
398 unsigned char status;
400 /* Get status from Read Register 0 */
401 status = read_zsreg(info->zs_channel, 0);
403 /* FIXEM: Check for DCD transitions */
404 if (((status ^ info->read_reg_zero) & DCD) != 0
405 && info->tty && !C_CLOCAL(info->tty)) {
406 if (status & DCD) {
407 wake_up_interruptible(&info->open_wait);
408 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
409 if (info->tty)
410 tty_hangup(info->tty);
414 /* Check for CTS transitions */
415 if (info->tty && C_CRTSCTS(info->tty)) {
417 * For some reason, on the Power Macintosh,
418 * it seems that the CTS bit is 1 when CTS is
419 * *negated* and 0 when it is asserted.
420 * The DCD bit doesn't seem to be inverted
421 * like this.
423 if ((status & CTS) != 0) {
424 if (info->tx_stopped) {
425 info->tx_stopped = 0;
426 if (!info->tx_active)
427 transmit_chars(info);
429 } else {
430 info->tx_stopped = 1;
434 /* Clear status condition... */
435 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
436 info->read_reg_zero = status;
440 * This is the serial driver's generic interrupt routine
442 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
444 struct dec_serial *info = (struct dec_serial *) dev_id;
445 unsigned char zs_intreg;
446 int shift;
448 /* NOTE: The read register 3, which holds the irq status,
449 * does so for both channels on each chip. Although
450 * the status value itself must be read from the A
451 * channel and is only valid when read from channel A.
452 * Yes... broken hardware...
454 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
456 if (info->zs_chan_a == info->zs_channel)
457 shift = 3; /* Channel A */
458 else
459 shift = 0; /* Channel B */
461 for (;;) {
462 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
463 if ((zs_intreg & CHAN_IRQMASK) == 0)
464 break;
466 if (zs_intreg & CHBRxIP) {
467 receive_chars(info, regs);
469 if (zs_intreg & CHBTxIP) {
470 transmit_chars(info);
472 if (zs_intreg & CHBEXT) {
473 status_handle(info);
479 * -------------------------------------------------------------------
480 * Here ends the serial interrupt routines.
481 * -------------------------------------------------------------------
485 * ------------------------------------------------------------
486 * rs_stop() and rs_start()
488 * This routines are called before setting or resetting tty->stopped.
489 * ------------------------------------------------------------
491 static void rs_stop(struct tty_struct *tty)
493 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
494 unsigned long flags;
496 if (serial_paranoia_check(info, tty->device, "rs_stop"))
497 return;
499 #if 1
500 save_flags(flags); cli();
501 if (info->zs_channel->curregs[5] & TxENAB) {
502 info->zs_channel->curregs[5] &= ~TxENAB;
503 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
505 restore_flags(flags);
506 #endif
509 static void rs_start(struct tty_struct *tty)
511 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
512 unsigned long flags;
514 if (serial_paranoia_check(info, tty->device, "rs_start"))
515 return;
517 save_flags(flags); cli();
518 #if 1
519 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
520 info->zs_channel->curregs[5] |= TxENAB;
521 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
523 #else
524 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
525 transmit_chars(info);
527 #endif
528 restore_flags(flags);
532 * This routine is used to handle the "bottom half" processing for the
533 * serial driver, known also the "software interrupt" processing.
534 * This processing is done at the kernel interrupt level, after the
535 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
536 * is where time-consuming activities which can not be done in the
537 * interrupt driver proper are done; the interrupt driver schedules
538 * them using rs_sched_event(), and they get done here.
540 static void do_serial_bh(void)
542 run_task_queue(&tq_zs_serial);
545 static void do_softint(void *private_)
547 struct dec_serial *info = (struct dec_serial *) private_;
548 struct tty_struct *tty;
550 tty = info->tty;
551 if (!tty)
552 return;
554 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
555 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
556 tty->ldisc.write_wakeup)
557 (tty->ldisc.write_wakeup)(tty);
558 wake_up_interruptible(&tty->write_wait);
562 static int startup(struct dec_serial * info)
564 unsigned long flags;
566 if (info->flags & ZILOG_INITIALIZED)
567 return 0;
569 if (!info->xmit_buf) {
570 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
571 if (!info->xmit_buf)
572 return -ENOMEM;
575 save_flags(flags); cli();
577 #ifdef SERIAL_DEBUG_OPEN
578 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
579 #endif
582 * Clear the receive FIFO.
584 ZS_CLEARFIFO(info->zs_channel);
585 info->xmit_fifo_size = 1;
588 * Clear the interrupt registers.
590 write_zsreg(info->zs_channel, 0, ERR_RES);
591 write_zsreg(info->zs_channel, 0, RES_H_IUS);
594 * Turn on RTS and DTR.
596 zs_rtsdtr(info, 1);
599 * Finally, enable sequencing and interrupts
601 info->zs_channel->curregs[1] = (info->zs_channel->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
602 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
603 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
604 info->zs_channel->curregs[15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
605 info->zs_channel->curregs[9] |= (VIS | MIE);
606 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
607 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
608 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
609 write_zsreg(info->zs_channel, 15, info->zs_channel->curregs[15]);
610 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
613 * And clear the interrupt registers again for luck.
615 write_zsreg(info->zs_channel, 0, ERR_RES);
616 write_zsreg(info->zs_channel, 0, RES_H_IUS);
618 if (info->tty)
619 clear_bit(TTY_IO_ERROR, &info->tty->flags);
620 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
623 * Set the speed of the serial port
625 change_speed(info);
627 /* Save the current value of RR0 */
628 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
630 info->flags |= ZILOG_INITIALIZED;
631 restore_flags(flags);
632 return 0;
636 * This routine will shutdown a serial port; interrupts are disabled, and
637 * DTR is dropped if the hangup on close termio flag is on.
639 static void shutdown(struct dec_serial * info)
641 unsigned long flags;
643 if (!(info->flags & ZILOG_INITIALIZED))
644 return;
646 #ifdef SERIAL_DEBUG_OPEN
647 printk("Shutting down serial port %d (irq %d)....", info->line,
648 info->irq);
649 #endif
651 save_flags(flags); cli(); /* Disable interrupts */
653 if (info->xmit_buf) {
654 free_page((unsigned long) info->xmit_buf);
655 info->xmit_buf = 0;
658 info->zs_channel->curregs[1] = 0;
659 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
661 info->zs_channel->curregs[3] &= ~RxENABLE;
662 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
664 info->zs_channel->curregs[5] &= ~TxENAB;
665 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
666 if (!info->tty || C_HUPCL(info->tty)) {
667 info->zs_chan_a->curregs[5] &= ~(DTR | RTS);
668 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
671 if (info->tty)
672 set_bit(TTY_IO_ERROR, &info->tty->flags);
674 info->flags &= ~ZILOG_INITIALIZED;
675 restore_flags(flags);
679 * This routine is called to set the UART divisor registers to match
680 * the specified baud rate for a serial port.
682 static void change_speed(struct dec_serial *info)
684 unsigned short port;
685 unsigned cflag;
686 int i;
687 int brg;
688 unsigned long flags;
690 if (!info->tty || !info->tty->termios)
691 return;
692 cflag = info->tty->termios->c_cflag;
693 if (!(port = info->port))
694 return;
695 i = cflag & CBAUD;
697 save_flags(flags); cli();
698 info->zs_baud = baud_table[i];
699 info->clk_divisor = 16;
701 switch (info->zs_baud) {
702 default:
703 info->zs_channel->curregs[4] = X16CLK;
704 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
705 info->zs_channel->curregs[12] = (brg & 255);
706 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
709 /* byte size and parity */
710 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
711 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
712 switch (cflag & CSIZE) {
713 case CS5:
714 info->zs_channel->curregs[3] |= Rx5;
715 info->zs_channel->curregs[5] |= Tx5;
716 break;
717 case CS6:
718 info->zs_channel->curregs[3] |= Rx6;
719 info->zs_channel->curregs[5] |= Tx6;
720 break;
721 case CS7:
722 info->zs_channel->curregs[3] |= Rx7;
723 info->zs_channel->curregs[5] |= Tx7;
724 break;
725 case CS8:
726 default: /* defaults to 8 bits */
727 info->zs_channel->curregs[3] |= Rx8;
728 info->zs_channel->curregs[5] |= Tx8;
729 break;
732 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
733 if (cflag & CSTOPB) {
734 info->zs_channel->curregs[4] |= SB2;
735 } else {
736 info->zs_channel->curregs[4] |= SB1;
738 if (cflag & PARENB) {
739 info->zs_channel->curregs[4] |= PAR_ENA;
741 if (!(cflag & PARODD)) {
742 info->zs_channel->curregs[4] |= PAR_EVEN;
745 if (!(cflag & CLOCAL)) {
746 if (!(info->zs_channel->curregs[15] & DCDIE))
747 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
748 info->zs_channel->curregs[15] |= DCDIE;
749 } else
750 info->zs_channel->curregs[15] &= ~DCDIE;
751 if (cflag & CRTSCTS) {
752 info->zs_channel->curregs[15] |= CTSIE;
753 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
754 info->tx_stopped = 1;
755 } else {
756 info->zs_channel->curregs[15] &= ~CTSIE;
757 info->tx_stopped = 0;
760 /* Load up the new values */
761 load_zsregs(info->zs_channel, info->zs_channel->curregs);
763 restore_flags(flags);
766 static void rs_flush_chars(struct tty_struct *tty)
768 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
769 unsigned long flags;
771 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
772 return;
774 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
775 !info->xmit_buf)
776 return;
778 /* Enable transmitter */
779 save_flags(flags); cli();
780 transmit_chars(info);
781 restore_flags(flags);
784 static int rs_write(struct tty_struct * tty, int from_user,
785 const unsigned char *buf, int count)
787 int c, total = 0;
788 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
789 unsigned long flags;
791 if (serial_paranoia_check(info, tty->device, "rs_write"))
792 return 0;
794 if (!tty || !info->xmit_buf)
795 return 0;
797 save_flags(flags);
798 while (1) {
799 cli();
800 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
801 SERIAL_XMIT_SIZE - info->xmit_head));
802 if (c <= 0)
803 break;
805 if (from_user) {
806 down(&tmp_buf_sem);
807 copy_from_user(tmp_buf, buf, c);
808 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
809 SERIAL_XMIT_SIZE - info->xmit_head));
810 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
811 up(&tmp_buf_sem);
812 } else
813 memcpy(info->xmit_buf + info->xmit_head, buf, c);
814 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
815 info->xmit_cnt += c;
816 restore_flags(flags);
817 buf += c;
818 count -= c;
819 total += c;
822 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
823 && !info->tx_active)
824 transmit_chars(info);
825 restore_flags(flags);
826 return total;
829 static int rs_write_room(struct tty_struct *tty)
831 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
832 int ret;
834 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
835 return 0;
836 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
837 if (ret < 0)
838 ret = 0;
839 return ret;
842 static int rs_chars_in_buffer(struct tty_struct *tty)
844 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
846 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
847 return 0;
848 return info->xmit_cnt;
851 static void rs_flush_buffer(struct tty_struct *tty)
853 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
855 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
856 return;
857 cli();
858 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
859 sti();
860 wake_up_interruptible(&tty->write_wait);
861 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
862 tty->ldisc.write_wakeup)
863 (tty->ldisc.write_wakeup)(tty);
867 * ------------------------------------------------------------
868 * rs_throttle()
870 * This routine is called by the upper-layer tty layer to signal that
871 * incoming characters should be throttled.
872 * ------------------------------------------------------------
874 static void rs_throttle(struct tty_struct * tty)
876 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
877 unsigned long flags;
879 #ifdef SERIAL_DEBUG_THROTTLE
880 char buf[64];
882 printk("throttle %s: %d....\n", _tty_name(tty, buf),
883 tty->ldisc.chars_in_buffer(tty));
884 #endif
886 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
887 return;
889 if (I_IXOFF(tty)) {
890 save_flags(flags); cli();
891 info->x_char = STOP_CHAR(tty);
892 if (!info->tx_active)
893 transmit_chars(info);
894 restore_flags(flags);
897 if (C_CRTSCTS(tty)) {
899 * Here we want to turn off the RTS line. On Macintoshes,
900 * we only get the DTR line, which goes to both DTR and
901 * RTS on the modem. RTS doesn't go out to the serial
902 * port socket. So you should make sure your modem is
903 * set to ignore DTR if you're using CRTSCTS.
905 save_flags(flags); cli();
906 info->zs_chan_a->curregs[5] &= ~(DTR | RTS);
907 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
908 restore_flags(flags);
912 static void rs_unthrottle(struct tty_struct * tty)
914 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
915 unsigned long flags;
917 #ifdef SERIAL_DEBUG_THROTTLE
918 char buf[64];
920 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
921 tty->ldisc.chars_in_buffer(tty));
922 #endif
924 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
925 return;
927 if (I_IXOFF(tty)) {
928 save_flags(flags); cli();
929 if (info->x_char)
930 info->x_char = 0;
931 else {
932 info->x_char = START_CHAR(tty);
933 if (!info->tx_active)
934 transmit_chars(info);
936 restore_flags(flags);
939 if (C_CRTSCTS(tty)) {
940 /* Assert RTS and DTR lines */
941 save_flags(flags); cli();
942 info->zs_chan_a->curregs[5] |= DTR | RTS;
943 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
944 restore_flags(flags);
949 * ------------------------------------------------------------
950 * rs_ioctl() and friends
951 * ------------------------------------------------------------
954 static int get_serial_info(struct dec_serial * info,
955 struct serial_struct * retinfo)
957 struct serial_struct tmp;
959 if (!retinfo)
960 return -EFAULT;
961 memset(&tmp, 0, sizeof(tmp));
962 tmp.type = info->type;
963 tmp.line = info->line;
964 tmp.port = info->port;
965 tmp.irq = info->irq;
966 tmp.flags = info->flags;
967 tmp.baud_base = info->baud_base;
968 tmp.close_delay = info->close_delay;
969 tmp.closing_wait = info->closing_wait;
970 tmp.custom_divisor = info->custom_divisor;
971 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
974 static int set_serial_info(struct dec_serial * info,
975 struct serial_struct * new_info)
977 struct serial_struct new_serial;
978 struct dec_serial old_info;
979 int retval = 0;
981 if (!new_info)
982 return -EFAULT;
983 copy_from_user(&new_serial,new_info,sizeof(new_serial));
984 old_info = *info;
986 if (!suser()) {
987 if ((new_serial.baud_base != info->baud_base) ||
988 (new_serial.type != info->type) ||
989 (new_serial.close_delay != info->close_delay) ||
990 ((new_serial.flags & ~ZILOG_USR_MASK) !=
991 (info->flags & ~ZILOG_USR_MASK)))
992 return -EPERM;
993 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
994 (new_serial.flags & ZILOG_USR_MASK));
995 info->custom_divisor = new_serial.custom_divisor;
996 goto check_and_exit;
999 if (info->count > 1)
1000 return -EBUSY;
1003 * OK, past this point, all the error checking has been done.
1004 * At this point, we start making changes.....
1007 info->baud_base = new_serial.baud_base;
1008 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1009 (new_serial.flags & ZILOG_FLAGS));
1010 info->type = new_serial.type;
1011 info->close_delay = new_serial.close_delay;
1012 info->closing_wait = new_serial.closing_wait;
1014 check_and_exit:
1015 retval = startup(info);
1016 return retval;
1020 * get_lsr_info - get line status register info
1022 * Purpose: Let user call ioctl() to get info when the UART physically
1023 * is emptied. On bus types like RS485, the transmitter must
1024 * release the bus after transmitting. This must be done when
1025 * the transmit shift register is empty, not be done when the
1026 * transmit holding register is empty. This functionality
1027 * allows an RS485 driver to be written in user space.
1029 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1031 unsigned char status;
1033 cli();
1034 status = read_zsreg(info->zs_channel, 0);
1035 sti();
1036 put_user(status,value);
1037 return 0;
1040 static int get_modem_info(struct dec_serial *info, unsigned int *value)
1042 unsigned char control, status;
1043 unsigned int result;
1045 cli();
1046 control = info->zs_chan_a->curregs[5];
1047 status = read_zsreg(info->zs_channel, 0);
1048 sti();
1049 result = ((control & RTS) ? TIOCM_RTS: 0)
1050 | ((control & DTR) ? TIOCM_DTR: 0)
1051 | ((status & DCD) ? TIOCM_CAR: 0)
1052 | ((status & CTS) ? 0: TIOCM_CTS);
1053 put_user(result,value);
1054 return 0;
1057 static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1058 unsigned int *value)
1060 int error;
1061 unsigned int arg, bits;
1063 error = verify_area(VERIFY_READ, value, sizeof(int));
1064 if (error)
1065 return error;
1066 get_user(arg, value);
1067 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1068 cli();
1069 switch (cmd) {
1070 case TIOCMBIS:
1071 info->zs_chan_a->curregs[5] |= bits;
1072 break;
1073 case TIOCMBIC:
1074 info->zs_chan_a->curregs[5] &= ~bits;
1075 break;
1076 case TIOCMSET:
1077 info->zs_chan_a->curregs[5] = (info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1078 break;
1079 default:
1080 sti();
1081 return -EINVAL;
1083 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1084 sti();
1085 return 0;
1089 * rs_break - turn transmit break condition on/off
1091 static void rs_break(struct tty_struct *tty, int break_state)
1093 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1094 unsigned long flags;
1096 if (serial_paranoia_check(info, tty->device, "rs_break"))
1097 return;
1098 if (!info->port)
1099 return;
1101 save_flags(flags); cli();
1102 if (break_state == -1)
1103 info->zs_channel->curregs[5] |= SND_BRK;
1104 else
1105 info->zs_channel->curregs[5] &= ~SND_BRK;
1106 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1107 restore_flags(flags);
1110 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1111 unsigned int cmd, unsigned long arg)
1113 int error;
1114 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1116 #ifdef CONFIG_KGDB
1117 if (info->kgdb_channel)
1118 return -ENODEV;
1119 #endif
1120 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1121 return -ENODEV;
1123 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1124 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1125 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1126 if (tty->flags & (1 << TTY_IO_ERROR))
1127 return -EIO;
1130 switch (cmd) {
1131 case TIOCMGET:
1132 error = verify_area(VERIFY_WRITE, (void *) arg,
1133 sizeof(unsigned int));
1134 if (error)
1135 return error;
1136 return get_modem_info(info, (unsigned int *) arg);
1137 case TIOCMBIS:
1138 case TIOCMBIC:
1139 case TIOCMSET:
1140 return set_modem_info(info, cmd, (unsigned int *) arg);
1141 case TIOCGSERIAL:
1142 error = verify_area(VERIFY_WRITE, (void *) arg,
1143 sizeof(struct serial_struct));
1144 if (error)
1145 return error;
1146 return get_serial_info(info,
1147 (struct serial_struct *) arg);
1148 case TIOCSSERIAL:
1149 return set_serial_info(info,
1150 (struct serial_struct *) arg);
1151 case TIOCSERGETLSR: /* Get line status register */
1152 error = verify_area(VERIFY_WRITE, (void *) arg,
1153 sizeof(unsigned int));
1154 if (error)
1155 return error;
1156 else
1157 return get_lsr_info(info, (unsigned int *) arg);
1159 case TIOCSERGSTRUCT:
1160 error = verify_area(VERIFY_WRITE, (void *) arg,
1161 sizeof(struct dec_serial));
1162 if (error)
1163 return error;
1164 copy_from_user((struct dec_serial *) arg,
1165 info, sizeof(struct dec_serial));
1166 return 0;
1168 default:
1169 return -ENOIOCTLCMD;
1171 return 0;
1174 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1176 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1177 int was_stopped;
1179 if (tty->termios->c_cflag == old_termios->c_cflag)
1180 return;
1181 was_stopped = info->tx_stopped;
1183 change_speed(info);
1185 if (was_stopped && !info->tx_stopped)
1186 rs_start(tty);
1190 * ------------------------------------------------------------
1191 * rs_close()
1193 * This routine is called when the serial port gets closed.
1194 * Wait for the last remaining data to be sent.
1195 * ------------------------------------------------------------
1197 static void rs_close(struct tty_struct *tty, struct file * filp)
1199 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1200 unsigned long flags;
1202 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1203 return;
1205 save_flags(flags); cli();
1207 if (tty_hung_up_p(filp)) {
1208 restore_flags(flags);
1209 return;
1212 #ifdef SERIAL_DEBUG_OPEN
1213 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1214 #endif
1215 if ((tty->count == 1) && (info->count != 1)) {
1217 * Uh, oh. tty->count is 1, which means that the tty
1218 * structure will be freed. Info->count should always
1219 * be one in these conditions. If it's greater than
1220 * one, we've got real problems, since it means the
1221 * serial port won't be shutdown.
1223 printk("rs_close: bad serial port count; tty->count is 1, "
1224 "info->count is %d\n", info->count);
1225 info->count = 1;
1227 if (--info->count < 0) {
1228 printk("rs_close: bad serial port count for ttys%d: %d\n",
1229 info->line, info->count);
1230 info->count = 0;
1232 if (info->count) {
1233 restore_flags(flags);
1234 return;
1236 info->flags |= ZILOG_CLOSING;
1238 * Save the termios structure, since this port may have
1239 * separate termios for callout and dialin.
1241 if (info->flags & ZILOG_NORMAL_ACTIVE)
1242 info->normal_termios = *tty->termios;
1243 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1244 info->callout_termios = *tty->termios;
1246 * Now we wait for the transmit buffer to clear; and we notify
1247 * the line discipline to only process XON/XOFF characters.
1249 tty->closing = 1;
1250 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1251 tty_wait_until_sent(tty, info->closing_wait);
1253 * At this point we stop accepting input. To do this, we
1254 * disable the receiver and receive interrupts.
1256 info->zs_channel->curregs[3] &= ~RxENABLE;
1257 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1258 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1259 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1260 ZS_CLEARFIFO(info->zs_channel);
1261 if (info->flags & ZILOG_INITIALIZED) {
1263 * Before we drop DTR, make sure the SCC transmitter
1264 * has completely drained.
1266 rs_wait_until_sent(tty, info->timeout);
1269 shutdown(info);
1270 if (tty->driver.flush_buffer)
1271 tty->driver.flush_buffer(tty);
1272 if (tty->ldisc.flush_buffer)
1273 tty->ldisc.flush_buffer(tty);
1274 tty->closing = 0;
1275 info->event = 0;
1276 info->tty = 0;
1277 if (info->blocked_open) {
1278 if (info->close_delay) {
1279 current->state = TASK_INTERRUPTIBLE;
1280 schedule_timeout(info->close_delay);
1282 wake_up_interruptible(&info->open_wait);
1284 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1285 ZILOG_CLOSING);
1286 wake_up_interruptible(&info->close_wait);
1287 restore_flags(flags);
1291 * rs_wait_until_sent() --- wait until the transmitter is empty
1293 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1295 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1296 unsigned long orig_jiffies, char_time;
1298 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1299 return;
1301 orig_jiffies = jiffies;
1303 * Set the check interval to be 1/5 of the estimated time to
1304 * send a single character, and make it at least 1. The check
1305 * interval should also be less than the timeout.
1307 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1308 char_time = char_time / 5;
1309 if (char_time == 0)
1310 char_time = 1;
1311 if (timeout)
1312 char_time = MIN(char_time, timeout);
1313 while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1314 current->state = TASK_INTERRUPTIBLE;
1315 schedule_timeout(char_time);
1316 if (signal_pending(current))
1317 break;
1318 if (timeout && ((orig_jiffies + timeout) < jiffies))
1319 break;
1321 current->state = TASK_RUNNING;
1325 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1327 void rs_hangup(struct tty_struct *tty)
1329 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1331 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1332 return;
1334 rs_flush_buffer(tty);
1335 shutdown(info);
1336 info->event = 0;
1337 info->count = 0;
1338 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1339 info->tty = 0;
1340 wake_up_interruptible(&info->open_wait);
1344 * ------------------------------------------------------------
1345 * rs_open() and friends
1346 * ------------------------------------------------------------
1348 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1349 struct dec_serial *info)
1351 DECLARE_WAITQUEUE(wait, current);
1352 int retval;
1353 int do_clocal = 0;
1356 * If the device is in the middle of being closed, then block
1357 * until it's done, and then try again.
1359 if (info->flags & ZILOG_CLOSING) {
1360 interruptible_sleep_on(&info->close_wait);
1361 #ifdef SERIAL_DO_RESTART
1362 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1363 -EAGAIN : -ERESTARTSYS);
1364 #else
1365 return -EAGAIN;
1366 #endif
1370 * If this is a callout device, then just make sure the normal
1371 * device isn't being used.
1373 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1374 if (info->flags & ZILOG_NORMAL_ACTIVE)
1375 return -EBUSY;
1376 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1377 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1378 (info->session != current->session))
1379 return -EBUSY;
1380 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1381 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1382 (info->pgrp != current->pgrp))
1383 return -EBUSY;
1384 info->flags |= ZILOG_CALLOUT_ACTIVE;
1385 return 0;
1389 * If non-blocking mode is set, or the port is not enabled,
1390 * then make the check up front and then exit.
1392 if ((filp->f_flags & O_NONBLOCK) ||
1393 (tty->flags & (1 << TTY_IO_ERROR))) {
1394 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1395 return -EBUSY;
1396 info->flags |= ZILOG_NORMAL_ACTIVE;
1397 return 0;
1400 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1401 if (info->normal_termios.c_cflag & CLOCAL)
1402 do_clocal = 1;
1403 } else {
1404 if (tty->termios->c_cflag & CLOCAL)
1405 do_clocal = 1;
1409 * Block waiting for the carrier detect and the line to become
1410 * free (i.e., not in use by the callout). While we are in
1411 * this loop, info->count is dropped by one, so that
1412 * rs_close() knows when to free things. We restore it upon
1413 * exit, either normal or abnormal.
1415 retval = 0;
1416 add_wait_queue(&info->open_wait, &wait);
1417 #ifdef SERIAL_DEBUG_OPEN
1418 printk("block_til_ready before block: ttys%d, count = %d\n",
1419 info->line, info->count);
1420 #endif
1421 cli();
1422 if (!tty_hung_up_p(filp))
1423 info->count--;
1424 sti();
1425 info->blocked_open++;
1426 while (1) {
1427 cli();
1428 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1429 (tty->termios->c_cflag & CBAUD))
1430 zs_rtsdtr(info, 1);
1431 sti();
1432 set_current_state(TASK_INTERRUPTIBLE);
1433 if (tty_hung_up_p(filp) ||
1434 !(info->flags & ZILOG_INITIALIZED)) {
1435 #ifdef SERIAL_DO_RESTART
1436 if (info->flags & ZILOG_HUP_NOTIFY)
1437 retval = -EAGAIN;
1438 else
1439 retval = -ERESTARTSYS;
1440 #else
1441 retval = -EAGAIN;
1442 #endif
1443 break;
1445 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1446 !(info->flags & ZILOG_CLOSING) &&
1447 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1448 break;
1449 if (signal_pending(current)) {
1450 retval = -ERESTARTSYS;
1451 break;
1453 #ifdef SERIAL_DEBUG_OPEN
1454 printk("block_til_ready blocking: ttys%d, count = %d\n",
1455 info->line, info->count);
1456 #endif
1457 schedule();
1459 current->state = TASK_RUNNING;
1460 remove_wait_queue(&info->open_wait, &wait);
1461 if (!tty_hung_up_p(filp))
1462 info->count++;
1463 info->blocked_open--;
1464 #ifdef SERIAL_DEBUG_OPEN
1465 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1466 info->line, info->count);
1467 #endif
1468 if (retval)
1469 return retval;
1470 info->flags |= ZILOG_NORMAL_ACTIVE;
1471 return 0;
1475 * This routine is called whenever a serial port is opened. It
1476 * enables interrupts for a serial port, linking in its ZILOG structure into
1477 * the IRQ chain. It also performs the serial-specific
1478 * initialization for the tty structure.
1480 int rs_open(struct tty_struct *tty, struct file * filp)
1482 struct dec_serial *info;
1483 int retval, line;
1485 line = MINOR(tty->device) - tty->driver.minor_start;
1486 if ((line < 0) || (line >= zs_channels_found))
1487 return -ENODEV;
1488 info = zs_soft + line;
1490 #ifdef CONFIG_KGDB
1491 if (info->kgdb_channel)
1492 return -ENODEV;
1493 #endif
1494 if (serial_paranoia_check(info, tty->device, "rs_open"))
1495 return -ENODEV;
1496 #ifdef SERIAL_DEBUG_OPEN
1497 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1498 info->count);
1499 #endif
1501 info->count++;
1502 tty->driver_data = info;
1503 info->tty = tty;
1506 * If the port is the middle of closing, bail out now
1508 if (tty_hung_up_p(filp) ||
1509 (info->flags & ZILOG_CLOSING)) {
1510 if (info->flags & ZILOG_CLOSING)
1511 interruptible_sleep_on(&info->close_wait);
1512 #ifdef SERIAL_DO_RESTART
1513 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1514 -EAGAIN : -ERESTARTSYS);
1515 #else
1516 return -EAGAIN;
1517 #endif
1521 * Start up serial port
1523 retval = startup(info);
1524 if (retval)
1525 return retval;
1527 retval = block_til_ready(tty, filp, info);
1528 if (retval) {
1529 #ifdef SERIAL_DEBUG_OPEN
1530 printk("rs_open returning after block_til_ready with %d\n",
1531 retval);
1532 #endif
1533 return retval;
1536 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1537 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1538 *tty->termios = info->normal_termios;
1539 else
1540 *tty->termios = info->callout_termios;
1541 change_speed(info);
1543 #ifdef CONFIG_SERIAL_CONSOLE
1544 if (sercons.cflag && sercons.index == line) {
1545 tty->termios->c_cflag = sercons.cflag;
1546 sercons.cflag = 0;
1547 change_speed(info);
1549 #endif
1551 info->session = current->session;
1552 info->pgrp = current->pgrp;
1554 #ifdef SERIAL_DEBUG_OPEN
1555 printk("rs_open ttys%d successful...", info->line);
1556 #endif
1557 /* tty->low_latency = 1; */
1558 return 0;
1561 /* Finally, routines used to initialize the serial driver. */
1563 static void __init show_serial_version(void)
1565 printk("DECstation Z8530 serial driver version 0.03\n");
1568 /* Initialize Z8530s zs_channels
1571 static void __init probe_sccs(void)
1573 struct dec_serial **pp;
1574 int i, n, n_chips = 0, n_channels, chip, channel;
1577 * did we get here by accident?
1579 if(!IOASIC) {
1580 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1581 return;
1585 * When serial console is activated, tc_init has not been called yet
1586 * and system_base is undefined. Unfortunately we have to hardcode
1587 * system_base for this case :-(. HK
1589 switch(mips_machtype) {
1590 case MACH_DS5000_2X0:
1591 system_base = 0xbf800000;
1592 n_chips = 2;
1593 break;
1594 case MACH_DS5000_1XX:
1595 system_base = 0xbc000000;
1596 n_chips = 2;
1597 break;
1598 case MACH_DS5000_XX:
1599 system_base = 0xbc000000;
1600 n_chips = 1;
1601 break;
1604 pp = &zs_chain;
1606 n_channels = 0;
1608 for (chip = 0; chip < n_chips; chip++) {
1609 for (channel = 0; channel <= 1; channel++) {
1611 * The sccs reside on the high byte of the 16 bit IOBUS
1613 zs_channels[n_channels].control = (volatile unsigned char *)
1614 system_base + (0 == chip ? SCC0 : SCC1) + (0 == channel ? 1 : 9);
1615 zs_channels[n_channels].data = zs_channels[n_channels].control + 4;
1616 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1617 zs_soft[n_channels].irq = SERIAL;
1619 if (0 == channel)
1620 zs_soft[n_channels].zs_chan_a = &zs_channels[n_channels+1];
1621 else
1622 zs_soft[n_channels].zs_chan_a = &zs_channels[n_channels];
1624 *pp = &zs_soft[n_channels];
1625 pp = &zs_soft[n_channels].zs_next;
1626 n_channels++;
1630 *pp = 0;
1631 zs_channels_found = n_channels;
1633 for (n = 0; n < zs_channels_found; n++) {
1634 for (i = 0; i < 16; i++) {
1635 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1639 /* save_and_cli(flags);
1640 for (n = 0; n < zs_channels_found; n++) {
1641 if (((int)zs_channels[n].control & 0xf) == 1) {
1642 write_zsreg(zs_soft[channel].zs_chan_a, R9, FHWRES);
1643 udelay(10000);
1644 write_zsreg(zs_soft[channel].zs_chan_a, R9, 0);
1646 load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1648 restore_flags(flags); */
1651 /* zs_init inits the driver */
1652 int __init zs_init(void)
1654 int channel, i;
1655 unsigned long flags;
1656 struct dec_serial *info;
1658 if(!IOASIC)
1659 return -ENODEV;
1661 /* Setup base handler, and timer table. */
1662 init_bh(SERIAL_BH, do_serial_bh);
1664 /* Find out how many Z8530 SCCs we have */
1665 if (zs_chain == 0)
1666 probe_sccs();
1668 show_serial_version();
1670 /* Initialize the tty_driver structure */
1671 /* Not all of this is exactly right for us. */
1673 memset(&serial_driver, 0, sizeof(struct tty_driver));
1674 serial_driver.magic = TTY_DRIVER_MAGIC;
1675 serial_driver.name = "ttyS";
1676 serial_driver.major = TTY_MAJOR;
1677 serial_driver.minor_start = 64;
1678 serial_driver.num = zs_channels_found;
1679 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1680 serial_driver.subtype = SERIAL_TYPE_NORMAL;
1681 serial_driver.init_termios = tty_std_termios;
1683 serial_driver.init_termios.c_cflag =
1684 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1685 serial_driver.flags = TTY_DRIVER_REAL_RAW;
1686 serial_driver.refcount = &serial_refcount;
1687 serial_driver.table = serial_table;
1688 serial_driver.termios = serial_termios;
1689 serial_driver.termios_locked = serial_termios_locked;
1691 serial_driver.open = rs_open;
1692 serial_driver.close = rs_close;
1693 serial_driver.write = rs_write;
1694 serial_driver.flush_chars = rs_flush_chars;
1695 serial_driver.write_room = rs_write_room;
1696 serial_driver.chars_in_buffer = rs_chars_in_buffer;
1697 serial_driver.flush_buffer = rs_flush_buffer;
1698 serial_driver.ioctl = rs_ioctl;
1699 serial_driver.throttle = rs_throttle;
1700 serial_driver.unthrottle = rs_unthrottle;
1701 serial_driver.set_termios = rs_set_termios;
1702 serial_driver.stop = rs_stop;
1703 serial_driver.start = rs_start;
1704 serial_driver.hangup = rs_hangup;
1705 serial_driver.break_ctl = rs_break;
1706 serial_driver.wait_until_sent = rs_wait_until_sent;
1709 * The callout device is just like normal device except for
1710 * major number and the subtype code.
1712 callout_driver = serial_driver;
1713 callout_driver.name = "cua";
1714 callout_driver.major = TTYAUX_MAJOR;
1715 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1717 if (tty_register_driver(&serial_driver))
1718 panic("Couldn't register serial driver\n");
1719 if (tty_register_driver(&callout_driver))
1720 panic("Couldn't register callout driver\n");
1722 save_flags(flags); cli();
1724 for (channel = 0; channel < zs_channels_found; ++channel) {
1725 #ifdef CONFIG_KGDB
1726 if (zs_soft[channel].kgdb_channel) {
1727 continue;
1729 #endif
1730 zs_soft[channel].clk_divisor = 16;
1731 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1733 if (request_irq(SERIAL, rs_interrupt, SA_SHIRQ,
1734 "SCC", &zs_soft[channel]))
1735 printk(KERN_ERR "decserial: can't get irq %d\n",
1736 SERIAL);
1738 /* If console serial line, then enable interrupts. */
1739 /* if (zs_soft[channel].is_cons) {
1740 write_zsreg(zs_soft[channel].zs_channel, R1,
1741 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
1742 write_zsreg(zs_soft[channel].zs_channel, R9,
1743 (VIS | MIE));
1748 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
1750 #ifdef CONFIG_KGDB
1751 if (info->kgdb_channel) {
1752 continue;
1754 #endif
1755 info->magic = SERIAL_MAGIC;
1756 info->port = (int) info->zs_channel->control;
1757 info->line = i;
1758 info->tty = 0;
1759 info->custom_divisor = 16;
1760 info->close_delay = 50;
1761 info->closing_wait = 3000;
1762 info->x_char = 0;
1763 info->event = 0;
1764 info->count = 0;
1765 info->blocked_open = 0;
1766 info->tqueue.routine = do_softint;
1767 info->tqueue.data = info;
1768 info->callout_termios =callout_driver.init_termios;
1769 info->normal_termios = serial_driver.init_termios;
1770 init_waitqueue_head(&info->open_wait);
1771 init_waitqueue_head(&info->close_wait);
1772 printk("tty%02d at 0x%08x (irq = %d)", info->line,
1773 info->port, info->irq);
1774 printk(" is a Z85C30 SCC\n");
1777 restore_flags(flags);
1779 return 0;
1783 * register_serial and unregister_serial allows for serial ports to be
1784 * configured at run-time, to support PCMCIA modems.
1786 /* PowerMac: Unused at this time, just here to make things link. */
1787 int register_serial(struct serial_struct *req)
1789 return -1;
1792 void unregister_serial(int line)
1794 return;
1798 * ------------------------------------------------------------
1799 * Serial console driver
1800 * ------------------------------------------------------------
1802 #ifdef CONFIG_SERIAL_CONSOLE
1806 * Print a string to the serial port trying not to disturb
1807 * any possible real use of the port...
1810 /* This is for console output */
1811 static void
1812 zs_console_putchar(struct dec_serial *info, char ch)
1814 int loops = 10000;
1815 unsigned long flags;
1817 if(!info->zs_channel)
1818 return;
1820 save_flags(flags); cli();
1822 while (!(*(info->zs_channel->control) & Tx_BUF_EMP) && --loops)
1823 RECOVERY_DELAY;
1824 *(info->zs_channel->data) = ch;
1825 wbflush(); RECOVERY_DELAY;
1827 restore_flags(flags);
1830 static void serial_console_write(struct console *co, const char *s,
1831 unsigned count)
1833 struct dec_serial *info;
1834 int i;
1836 info = zs_soft + co->index;
1838 #if 0
1840 * disable master interrupt if necessary
1842 nine = info->zs_channel->curregs[9];
1843 if(nine & MIE)
1844 write_zsreg(info->zs_channel, R9, nine & ~MIE);
1845 #endif
1847 * do it
1849 for (i = 0; i < count; i++, s++) {
1850 if(*s == '\n')
1851 zs_console_putchar(info, '\r');
1852 zs_console_putchar(info, *s);
1855 * restore master interrupt enable
1857 #if 0
1858 write_zsreg(info->zs_channel, R9, nine);
1859 #endif
1863 * Receive character from the serial port
1865 static int serial_console_wait_key(struct console *co)
1867 return 0;
1870 static kdev_t serial_console_device(struct console *c)
1872 return MKDEV(TTY_MAJOR, 64 + c->index);
1876 * Setup initial baud/bits/parity. We do two things here:
1877 * - construct a cflag setting for the first rs_open()
1878 * - initialize the serial port
1879 * Return non-zero if we didn't find a serial port.
1881 static int __init serial_console_setup(struct console *co, char *options)
1883 struct dec_serial *info;
1884 int baud = 9600;
1885 int bits = 8;
1886 int parity = 'n';
1887 int cflag = CREAD | HUPCL | CLOCAL;
1888 char *s;
1889 unsigned long flags;
1891 if(!IOASIC)
1892 return -ENODEV;
1894 info = zs_soft + co->index;
1896 if (zs_chain == 0)
1897 probe_sccs();
1899 info->is_cons = 1;
1901 if (options) {
1902 baud = simple_strtoul(options, NULL, 10);
1903 s = options;
1904 while(*s >= '0' && *s <= '9')
1905 s++;
1906 if (*s)
1907 parity = *s++;
1908 if (*s)
1909 bits = *s - '0';
1913 * Now construct a cflag setting.
1915 switch(baud) {
1916 case 1200:
1917 cflag |= B1200;
1918 break;
1919 case 2400:
1920 cflag |= B2400;
1921 break;
1922 case 4800:
1923 cflag |= B4800;
1924 break;
1925 case 19200:
1926 cflag |= B19200;
1927 break;
1928 case 38400:
1929 cflag |= B38400;
1930 break;
1931 case 57600:
1932 cflag |= B57600;
1933 break;
1934 case 115200:
1935 cflag |= B115200;
1936 break;
1937 case 9600:
1938 default:
1939 cflag |= B9600;
1940 break;
1942 switch(bits) {
1943 case 7:
1944 cflag |= CS7;
1945 break;
1946 default:
1947 case 8:
1948 cflag |= CS8;
1949 break;
1951 switch(parity) {
1952 case 'o': case 'O':
1953 cflag |= PARODD;
1954 break;
1955 case 'e': case 'E':
1956 cflag |= PARENB;
1957 break;
1959 co->cflag = cflag;
1960 #if 1
1961 save_and_cli(flags);
1964 * Turn on RTS and DTR.
1966 zs_rtsdtr(info, 1);
1969 * Finally, enable sequencing
1971 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
1972 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
1973 info->zs_channel->curregs[9] |= (VIS);
1974 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1975 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1976 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
1979 * Clear the interrupt registers.
1981 write_zsreg(info->zs_channel, 0, ERR_RES);
1982 write_zsreg(info->zs_channel, 0, RES_H_IUS);
1985 * Set the speed of the serial port
1987 change_speed(info);
1989 /* Save the current value of RR0 */
1990 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1992 zs_soft[co->index].clk_divisor = 16;
1993 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
1995 restore_flags(flags);
1996 #endif
1997 return 0;
2000 static struct console sercons = {
2001 "ttyS",
2002 serial_console_write,
2003 NULL,
2004 serial_console_device,
2005 serial_console_wait_key,
2006 NULL,
2007 serial_console_setup,
2008 CON_PRINTBUFFER,
2011 NULL
2015 * Register console.
2017 void __init zs_serial_console_init(void)
2019 register_console(&sercons);
2021 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2023 #ifdef CONFIG_KGDB
2024 /* These are for receiving and sending characters under the kgdb
2025 * source level kernel debugger.
2027 void putDebugChar(char kgdb_char)
2029 struct dec_zschannel *chan = zs_kgdbchan;
2030 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2031 RECOVERY_DELAY;
2032 write_zsdata(chan, kgdb_char);
2034 char getDebugChar(void)
2036 struct dec_zschannel *chan = zs_kgdbchan;
2037 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2038 eieio(); /*barrier();*/
2039 return read_zsdata(chan);
2041 void kgdb_interruptible(int yes)
2043 struct dec_zschannel *chan = zs_kgdbchan;
2044 int one, nine;
2045 nine = read_zsreg(chan, 9);
2046 if (yes == 1) {
2047 one = EXT_INT_ENAB|INT_ALL_Rx;
2048 nine |= MIE;
2049 printk("turning serial ints on\n");
2050 } else {
2051 one = RxINT_DISAB;
2052 nine &= ~MIE;
2053 printk("turning serial ints off\n");
2055 write_zsreg(chan, 1, one);
2056 write_zsreg(chan, 9, nine);
2058 /* This sets up the serial port we're using, and turns on
2059 * interrupts for that channel, so kgdb is usable once we're done.
2061 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2063 int brg;
2064 int i, x;
2065 volatile char *sccc = ms->control;
2066 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
2067 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2068 for (i = 20000; i != 0; --i) {
2069 x = *sccc; eieio();
2071 for (i = 0; i < sizeof(scc_inittab); ++i) {
2072 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2073 i++;
2076 /* This is called at boot time to prime the kgdb serial debugging
2077 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2078 * for /dev/ttyb which is determined in setup_arch() from the
2079 * boot command line flags.
2081 void __init zs_kgdb_hook(int tty_num)
2083 /* Find out how many Z8530 SCCs we have */
2084 if (zs_chain == 0)
2085 probe_sccs();
2086 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2087 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2088 zs_soft[tty_num].change_needed = 0;
2089 zs_soft[tty_num].clk_divisor = 16;
2090 zs_soft[tty_num].zs_baud = 38400;
2091 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2092 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2093 /* Turn on transmitter/receiver at 8-bits/char */
2094 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2095 printk("KGDB: on channel %d initialized\n", tty_num);
2096 set_debug_traps(); /* init stub */
2098 #endif /* ifdef CONFIG_KGDB */