* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / tc / zs.c
blob46f4c152ca9cab30d64d71a98c3fdd6ee6eca060
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/dec/interrupts.h>
46 #include <asm/dec/machtype.h>
47 #include <asm/dec/tc.h>
48 #include <asm/dec/ioasic_addrs.h>
49 #ifdef CONFIG_KGDB
50 #include <asm/kgdb.h>
51 #endif
53 #include "zs.h"
57 * It would be nice to dynamically allocate everything that
58 * depends on NUM_SERIAL, so we could support any number of
59 * Z8530s, but for now...
61 #define NUM_SERIAL 2 /* Max number of ZS chips supported */
62 #define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */
64 #define RECOVERY_DELAY udelay(2)
66 struct dec_zschannel zs_channels[NUM_CHANNELS];
68 struct dec_serial zs_soft[NUM_CHANNELS];
69 int zs_channels_found;
70 struct dec_serial *zs_chain; /* list of all channels */
72 struct tty_struct zs_ttys[NUM_CHANNELS];
74 #ifdef CONFIG_SERIAL_CONSOLE
75 static struct console sercons;
76 #endif
78 #ifdef CONFIG_KGDB
79 struct dec_zschannel *zs_kgdbchan;
80 static unsigned char scc_inittab[] = {
81 9, 0x80, /* reset A side (CHRA) */
82 13, 0, /* set baud rate divisor */
83 12, 1,
84 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */
85 11, 0x50, /* clocks = br gen (RCBR | TCBR) */
86 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
87 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/
88 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/
90 #endif
92 static unsigned char zs_init_regs[16] __initdata = {
93 0, /* write 0 */
94 0, /* write 1 */
95 0xf0, /* write 2 */
96 (Rx8), /* write 3 */
97 (X16CLK | SB1), /* write 4 */
98 (Tx8), /* write 5 */
99 0, 0, 0, /* write 6, 7, 8 */
100 (VIS), /* write 9 */
101 (NRZ), /* write 10 */
102 (TCBR | RCBR), /* write 11 */
103 0, 0, /* BRG time constant, write 12 + 13 */
104 (BRSRC | BRENABL), /* write 14 */
105 0 /* write 15 */
108 #define ZS_CLOCK 7372800 /* Z8530 RTxC input clock rate */
110 DECLARE_TASK_QUEUE(tq_zs_serial);
112 struct tty_driver serial_driver, callout_driver;
113 static int serial_refcount;
115 /* serial subtype definitions */
116 #define SERIAL_TYPE_NORMAL 1
117 #define SERIAL_TYPE_CALLOUT 2
119 /* number of characters left in xmit buffer before we ask for more */
120 #define WAKEUP_CHARS 256
123 * Debugging.
125 #undef SERIAL_DEBUG_INTR
126 #undef SERIAL_DEBUG_OPEN
127 #undef SERIAL_DEBUG_FLOW
128 #undef SERIAL_DEBUG_THROTTLE
129 #undef SERIAL_PARANOIA_CHECK
131 #define RS_STROBE_TIME 10
132 #define RS_ISR_PASS_LIMIT 256
134 #define _INLINE_ inline
136 static void probe_sccs(void);
137 static void change_speed(struct dec_serial *info);
138 static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
140 static struct tty_struct *serial_table[NUM_CHANNELS];
141 static struct termios *serial_termios[NUM_CHANNELS];
142 static struct termios *serial_termios_locked[NUM_CHANNELS];
144 #ifndef MIN
145 #define MIN(a,b) ((a) < (b) ? (a) : (b))
146 #endif
149 * tmp_buf is used as a temporary buffer by serial_write. We need to
150 * lock it in case the copy_from_user blocks while swapping in a page,
151 * and some other program tries to do a serial write at the same time.
152 * Since the lock will only come under contention when the system is
153 * swapping and available memory is low, it makes sense to share one
154 * buffer across all the serial ports, since it significantly saves
155 * memory if large numbers of serial ports are open.
157 static unsigned char tmp_buf[4096]; /* This is cheating */
158 static struct semaphore tmp_buf_sem = MUTEX;
160 static inline int serial_paranoia_check(struct dec_serial *info,
161 dev_t device, const char *routine)
163 #ifdef SERIAL_PARANOIA_CHECK
164 static const char *badmagic =
165 "Warning: bad magic number for serial struct (%d, %d) in %s\n";
166 static const char *badinfo =
167 "Warning: null mac_serial for (%d, %d) in %s\n";
169 if (!info) {
170 printk(badinfo, MAJOR(device), MINOR(device), routine);
171 return 1;
173 if (info->magic != SERIAL_MAGIC) {
174 printk(badmagic, MAJOR(device), MINOR(device), routine);
175 return 1;
177 #endif
178 return 0;
182 * This is used to figure out the divisor speeds and the timeouts
184 static int baud_table[] = {
185 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
186 9600, 19200, 38400, 57600, 0, 0 };
189 * Reading and writing Z8530 registers.
191 static inline unsigned char read_zsreg(struct dec_zschannel *channel,
192 unsigned char reg)
194 unsigned char retval;
196 if (reg != 0) {
197 *channel->control = reg & 0xf;
198 RECOVERY_DELAY;
200 retval = *channel->control;
201 RECOVERY_DELAY;
202 return retval;
205 static inline void write_zsreg(struct dec_zschannel *channel,
206 unsigned char reg, unsigned char value)
208 if (reg != 0) {
209 *channel->control = reg & 0xf;
210 RECOVERY_DELAY;
212 *channel->control = value;
213 RECOVERY_DELAY;
214 return;
217 static inline unsigned char read_zsdata(struct dec_zschannel *channel)
219 unsigned char retval;
221 retval = *channel->data;
222 RECOVERY_DELAY;
223 return retval;
226 static inline void write_zsdata(struct dec_zschannel *channel,
227 unsigned char value)
229 *channel->data = value;
230 RECOVERY_DELAY;
231 return;
234 static inline void load_zsregs(struct dec_zschannel *channel,
235 unsigned char *regs)
237 /* ZS_CLEARERR(channel);
238 ZS_CLEARFIFO(channel); */
239 /* Load 'em up */
240 write_zsreg(channel, R4, regs[R4]);
241 write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
242 write_zsreg(channel, R5, regs[R5] & ~TxENAB);
243 write_zsreg(channel, R9, regs[R9]);
244 write_zsreg(channel, R1, regs[R1]);
245 write_zsreg(channel, R2, regs[R2]);
246 write_zsreg(channel, R10, regs[R10]);
247 write_zsreg(channel, R11, regs[R11]);
248 write_zsreg(channel, R12, regs[R12]);
249 write_zsreg(channel, R13, regs[R13]);
250 write_zsreg(channel, R14, regs[R14]);
251 write_zsreg(channel, R15, regs[R15]);
252 write_zsreg(channel, R3, regs[R3]);
253 write_zsreg(channel, R5, regs[R5]);
254 return;
257 /* Sets or clears DTR/RTS on the requested line */
258 static inline void zs_rtsdtr(struct dec_serial *ss, int set)
260 if (ss->zs_channel != ss->zs_chan_a) {
261 if (set)
262 ss->zs_chan_a->curregs[5] |= (RTS | DTR);
263 else
264 ss->zs_chan_a->curregs[5] &= ~(RTS | DTR);
265 write_zsreg(ss->zs_chan_a, 5, ss->zs_chan_a->curregs[5]);
267 return;
270 /* Utility routines for the Zilog */
271 static inline int get_zsbaud(struct dec_serial *ss)
273 struct dec_zschannel *channel = ss->zs_channel;
274 int brg;
276 /* The baud rate is split up between two 8-bit registers in
277 * what is termed 'BRG time constant' format in my docs for
278 * the chip, it is a function of the clk rate the chip is
279 * receiving which happens to be constant.
281 brg = (read_zsreg(channel, 13) << 8);
282 brg |= read_zsreg(channel, 12);
283 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor)));
286 /* On receive, this clears errors and the receiver interrupts */
287 static inline void rs_recv_clear(struct dec_zschannel *zsc)
289 write_zsreg(zsc, 0, ERR_RES);
290 write_zsreg(zsc, 0, RES_H_IUS); /* XXX this is unnecessary */
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 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
301 * -----------------------------------------------------------------------
305 * This routine is used by the interrupt handler to schedule
306 * processing in the software interrupt portion of the driver.
308 static _INLINE_ void rs_sched_event(struct dec_serial *info,
309 int event)
311 info->event |= 1 << event;
312 queue_task(&info->tqueue, &tq_zs_serial);
313 mark_bh(SERIAL_BH);
316 static _INLINE_ void receive_chars(struct dec_serial *info,
317 struct pt_regs *regs)
319 struct tty_struct *tty = info->tty;
320 unsigned char ch, stat, flag;
322 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) {
324 stat = read_zsreg(info->zs_channel, R1);
325 ch = read_zsdata(info->zs_channel);
327 #ifdef CONFIG_KGDB
328 if (info->kgdb_channel) {
329 if (ch == 0x03 || ch == '$')
330 breakpoint();
331 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
332 write_zsreg(info->zs_channel, 0, ERR_RES);
333 return;
335 #endif
336 if (!tty)
337 continue;
339 if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
340 static int flip_buf_ovf;
341 ++flip_buf_ovf;
342 continue;
344 tty->flip.count++;
346 static int flip_max_cnt;
347 if (flip_max_cnt < tty->flip.count)
348 flip_max_cnt = tty->flip.count;
350 if (stat & Rx_OVR) {
351 flag = TTY_OVERRUN;
352 } else if (stat & FRM_ERR) {
353 flag = TTY_FRAME;
354 } else if (stat & PAR_ERR) {
355 flag = TTY_PARITY;
356 } else
357 flag = 0;
358 if (flag)
359 /* reset the error indication */
360 write_zsreg(info->zs_channel, 0, ERR_RES);
361 *tty->flip.flag_buf_ptr++ = flag;
362 *tty->flip.char_buf_ptr++ = ch;
364 tty_flip_buffer_push(tty);
367 static void transmit_chars(struct dec_serial *info)
369 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0)
370 return;
371 info->tx_active = 0;
373 if (info->x_char) {
374 /* Send next char */
375 write_zsdata(info->zs_channel, info->x_char);
376 info->x_char = 0;
377 info->tx_active = 1;
378 return;
381 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped) {
382 write_zsreg(info->zs_channel, 0, RES_Tx_P);
383 return;
385 /* Send char */
386 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
387 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
388 info->xmit_cnt--;
389 info->tx_active = 1;
391 if (info->xmit_cnt < WAKEUP_CHARS)
392 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
395 static _INLINE_ void status_handle(struct dec_serial *info)
397 unsigned char status;
399 /* Get status from Read Register 0 */
400 status = read_zsreg(info->zs_channel, 0);
402 /* FIXEM: Check for DCD transitions */
403 if (((status ^ info->read_reg_zero) & DCD) != 0
404 && info->tty && !C_CLOCAL(info->tty)) {
405 if (status & DCD) {
406 wake_up_interruptible(&info->open_wait);
407 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
408 if (info->tty)
409 tty_hangup(info->tty);
413 /* Check for CTS transitions */
414 if (info->tty && C_CRTSCTS(info->tty)) {
416 * For some reason, on the Power Macintosh,
417 * it seems that the CTS bit is 1 when CTS is
418 * *negated* and 0 when it is asserted.
419 * The DCD bit doesn't seem to be inverted
420 * like this.
422 if ((status & CTS) != 0) {
423 if (info->tx_stopped) {
424 info->tx_stopped = 0;
425 if (!info->tx_active)
426 transmit_chars(info);
428 } else {
429 info->tx_stopped = 1;
433 /* Clear status condition... */
434 write_zsreg(info->zs_channel, 0, RES_EXT_INT);
435 info->read_reg_zero = status;
439 * This is the serial driver's generic interrupt routine
441 void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
443 struct dec_serial *info = (struct dec_serial *) dev_id;
444 unsigned char zs_intreg;
445 int shift;
447 /* NOTE: The read register 3, which holds the irq status,
448 * does so for both channels on each chip. Although
449 * the status value itself must be read from the A
450 * channel and is only valid when read from channel A.
451 * Yes... broken hardware...
453 #define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
455 if (info->zs_chan_a == info->zs_channel)
456 shift = 3; /* Channel A */
457 else
458 shift = 0; /* Channel B */
460 for (;;) {
461 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift;
462 if ((zs_intreg & CHAN_IRQMASK) == 0)
463 break;
465 if (zs_intreg & CHBRxIP) {
466 receive_chars(info, regs);
468 if (zs_intreg & CHBTxIP) {
469 transmit_chars(info);
471 if (zs_intreg & CHBEXT) {
472 status_handle(info);
478 * -------------------------------------------------------------------
479 * Here ends the serial interrupt routines.
480 * -------------------------------------------------------------------
484 * ------------------------------------------------------------
485 * rs_stop() and rs_start()
487 * This routines are called before setting or resetting tty->stopped.
488 * ------------------------------------------------------------
490 static void rs_stop(struct tty_struct *tty)
492 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
493 unsigned long flags;
495 if (serial_paranoia_check(info, tty->device, "rs_stop"))
496 return;
498 #if 1
499 save_flags(flags); cli();
500 if (info->zs_channel->curregs[5] & TxENAB) {
501 info->zs_channel->curregs[5] &= ~TxENAB;
502 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
504 restore_flags(flags);
505 #endif
508 static void rs_start(struct tty_struct *tty)
510 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
511 unsigned long flags;
513 if (serial_paranoia_check(info, tty->device, "rs_start"))
514 return;
516 save_flags(flags); cli();
517 #if 1
518 if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
519 info->zs_channel->curregs[5] |= TxENAB;
520 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
522 #else
523 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) {
524 transmit_chars(info);
526 #endif
527 restore_flags(flags);
531 * This routine is used to handle the "bottom half" processing for the
532 * serial driver, known also the "software interrupt" processing.
533 * This processing is done at the kernel interrupt level, after the
534 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This
535 * is where time-consuming activities which can not be done in the
536 * interrupt driver proper are done; the interrupt driver schedules
537 * them using rs_sched_event(), and they get done here.
539 static void do_serial_bh(void)
541 run_task_queue(&tq_zs_serial);
544 static void do_softint(void *private_)
546 struct dec_serial *info = (struct dec_serial *) private_;
547 struct tty_struct *tty;
549 tty = info->tty;
550 if (!tty)
551 return;
553 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
554 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
555 tty->ldisc.write_wakeup)
556 (tty->ldisc.write_wakeup)(tty);
557 wake_up_interruptible(&tty->write_wait);
561 static void rs_timer(void)
565 static int startup(struct dec_serial * info)
567 unsigned long flags;
569 if (info->flags & ZILOG_INITIALIZED)
570 return 0;
572 if (!info->xmit_buf) {
573 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
574 if (!info->xmit_buf)
575 return -ENOMEM;
578 save_flags(flags); cli();
580 #ifdef SERIAL_DEBUG_OPEN
581 printk("starting up ttyS%d (irq %d)...", info->line, info->irq);
582 #endif
585 * Clear the receive FIFO.
587 ZS_CLEARFIFO(info->zs_channel);
588 info->xmit_fifo_size = 1;
591 * Clear the interrupt registers.
593 write_zsreg(info->zs_channel, 0, ERR_RES);
594 write_zsreg(info->zs_channel, 0, RES_H_IUS);
597 * Turn on RTS and DTR.
599 zs_rtsdtr(info, 1);
602 * Finally, enable sequencing and interrupts
604 info->zs_channel->curregs[1] = (info->zs_channel->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
605 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
606 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
607 info->zs_channel->curregs[15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
608 info->zs_channel->curregs[9] |= (VIS | MIE);
609 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
610 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
611 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
612 write_zsreg(info->zs_channel, 15, info->zs_channel->curregs[15]);
613 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
616 * And clear the interrupt registers again for luck.
618 write_zsreg(info->zs_channel, 0, ERR_RES);
619 write_zsreg(info->zs_channel, 0, RES_H_IUS);
621 if (info->tty)
622 clear_bit(TTY_IO_ERROR, &info->tty->flags);
623 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
626 * Set the speed of the serial port
628 change_speed(info);
630 /* Save the current value of RR0 */
631 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
633 info->flags |= ZILOG_INITIALIZED;
634 restore_flags(flags);
635 return 0;
639 * This routine will shutdown a serial port; interrupts are disabled, and
640 * DTR is dropped if the hangup on close termio flag is on.
642 static void shutdown(struct dec_serial * info)
644 unsigned long flags;
646 if (!(info->flags & ZILOG_INITIALIZED))
647 return;
649 #ifdef SERIAL_DEBUG_OPEN
650 printk("Shutting down serial port %d (irq %d)....", info->line,
651 info->irq);
652 #endif
654 save_flags(flags); cli(); /* Disable interrupts */
656 if (info->xmit_buf) {
657 free_page((unsigned long) info->xmit_buf);
658 info->xmit_buf = 0;
661 info->zs_channel->curregs[1] = 0;
662 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]); /* no interrupts */
664 info->zs_channel->curregs[3] &= ~RxENABLE;
665 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
667 info->zs_channel->curregs[5] &= ~TxENAB;
668 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
669 if (!info->tty || C_HUPCL(info->tty)) {
670 info->zs_chan_a->curregs[5] &= ~(DTR | RTS);
671 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
674 if (info->tty)
675 set_bit(TTY_IO_ERROR, &info->tty->flags);
677 info->flags &= ~ZILOG_INITIALIZED;
678 restore_flags(flags);
682 * This routine is called to set the UART divisor registers to match
683 * the specified baud rate for a serial port.
685 static void change_speed(struct dec_serial *info)
687 unsigned short port;
688 unsigned cflag;
689 int i;
690 int brg;
691 unsigned long flags;
693 if (!info->tty || !info->tty->termios)
694 return;
695 cflag = info->tty->termios->c_cflag;
696 if (!(port = info->port))
697 return;
698 i = cflag & CBAUD;
700 save_flags(flags); cli();
701 info->zs_baud = baud_table[i];
702 info->clk_divisor = 16;
704 switch (info->zs_baud) {
705 default:
706 info->zs_channel->curregs[4] = X16CLK;
707 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor);
708 info->zs_channel->curregs[12] = (brg & 255);
709 info->zs_channel->curregs[13] = ((brg >> 8) & 255);
712 /* byte size and parity */
713 info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
714 info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
715 switch (cflag & CSIZE) {
716 case CS5:
717 info->zs_channel->curregs[3] |= Rx5;
718 info->zs_channel->curregs[5] |= Tx5;
719 break;
720 case CS6:
721 info->zs_channel->curregs[3] |= Rx6;
722 info->zs_channel->curregs[5] |= Tx6;
723 break;
724 case CS7:
725 info->zs_channel->curregs[3] |= Rx7;
726 info->zs_channel->curregs[5] |= Tx7;
727 break;
728 case CS8:
729 default: /* defaults to 8 bits */
730 info->zs_channel->curregs[3] |= Rx8;
731 info->zs_channel->curregs[5] |= Tx8;
732 break;
735 info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
736 if (cflag & CSTOPB) {
737 info->zs_channel->curregs[4] |= SB2;
738 } else {
739 info->zs_channel->curregs[4] |= SB1;
741 if (cflag & PARENB) {
742 info->zs_channel->curregs[4] |= PAR_ENA;
744 if (!(cflag & PARODD)) {
745 info->zs_channel->curregs[4] |= PAR_EVEN;
748 if (!(cflag & CLOCAL)) {
749 if (!(info->zs_channel->curregs[15] & DCDIE))
750 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
751 info->zs_channel->curregs[15] |= DCDIE;
752 } else
753 info->zs_channel->curregs[15] &= ~DCDIE;
754 if (cflag & CRTSCTS) {
755 info->zs_channel->curregs[15] |= CTSIE;
756 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0)
757 info->tx_stopped = 1;
758 } else {
759 info->zs_channel->curregs[15] &= ~CTSIE;
760 info->tx_stopped = 0;
763 /* Load up the new values */
764 load_zsregs(info->zs_channel, info->zs_channel->curregs);
766 restore_flags(flags);
769 static void rs_flush_chars(struct tty_struct *tty)
771 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
772 unsigned long flags;
774 if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
775 return;
777 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
778 !info->xmit_buf)
779 return;
781 /* Enable transmitter */
782 save_flags(flags); cli();
783 transmit_chars(info);
784 restore_flags(flags);
787 static int rs_write(struct tty_struct * tty, int from_user,
788 const unsigned char *buf, int count)
790 int c, total = 0;
791 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
792 unsigned long flags;
794 if (serial_paranoia_check(info, tty->device, "rs_write"))
795 return 0;
797 if (!tty || !info->xmit_buf)
798 return 0;
800 save_flags(flags);
801 while (1) {
802 cli();
803 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
804 SERIAL_XMIT_SIZE - info->xmit_head));
805 if (c <= 0)
806 break;
808 if (from_user) {
809 down(&tmp_buf_sem);
810 copy_from_user(tmp_buf, buf, c);
811 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
812 SERIAL_XMIT_SIZE - info->xmit_head));
813 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
814 up(&tmp_buf_sem);
815 } else
816 memcpy(info->xmit_buf + info->xmit_head, buf, c);
817 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
818 info->xmit_cnt += c;
819 restore_flags(flags);
820 buf += c;
821 count -= c;
822 total += c;
825 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
826 && !info->tx_active)
827 transmit_chars(info);
828 restore_flags(flags);
829 return total;
832 static int rs_write_room(struct tty_struct *tty)
834 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
835 int ret;
837 if (serial_paranoia_check(info, tty->device, "rs_write_room"))
838 return 0;
839 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
840 if (ret < 0)
841 ret = 0;
842 return ret;
845 static int rs_chars_in_buffer(struct tty_struct *tty)
847 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
849 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
850 return 0;
851 return info->xmit_cnt;
854 static void rs_flush_buffer(struct tty_struct *tty)
856 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
858 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
859 return;
860 cli();
861 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
862 sti();
863 wake_up_interruptible(&tty->write_wait);
864 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
865 tty->ldisc.write_wakeup)
866 (tty->ldisc.write_wakeup)(tty);
870 * ------------------------------------------------------------
871 * rs_throttle()
873 * This routine is called by the upper-layer tty layer to signal that
874 * incoming characters should be throttled.
875 * ------------------------------------------------------------
877 static void rs_throttle(struct tty_struct * tty)
879 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
880 unsigned long flags;
882 #ifdef SERIAL_DEBUG_THROTTLE
883 char buf[64];
885 printk("throttle %s: %d....\n", _tty_name(tty, buf),
886 tty->ldisc.chars_in_buffer(tty));
887 #endif
889 if (serial_paranoia_check(info, tty->device, "rs_throttle"))
890 return;
892 if (I_IXOFF(tty)) {
893 save_flags(flags); cli();
894 info->x_char = STOP_CHAR(tty);
895 if (!info->tx_active)
896 transmit_chars(info);
897 restore_flags(flags);
900 if (C_CRTSCTS(tty)) {
902 * Here we want to turn off the RTS line. On Macintoshes,
903 * we only get the DTR line, which goes to both DTR and
904 * RTS on the modem. RTS doesn't go out to the serial
905 * port socket. So you should make sure your modem is
906 * set to ignore DTR if you're using CRTSCTS.
908 save_flags(flags); cli();
909 info->zs_chan_a->curregs[5] &= ~(DTR | RTS);
910 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
911 restore_flags(flags);
915 static void rs_unthrottle(struct tty_struct * tty)
917 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
918 unsigned long flags;
920 #ifdef SERIAL_DEBUG_THROTTLE
921 char buf[64];
923 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
924 tty->ldisc.chars_in_buffer(tty));
925 #endif
927 if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
928 return;
930 if (I_IXOFF(tty)) {
931 save_flags(flags); cli();
932 if (info->x_char)
933 info->x_char = 0;
934 else {
935 info->x_char = START_CHAR(tty);
936 if (!info->tx_active)
937 transmit_chars(info);
939 restore_flags(flags);
942 if (C_CRTSCTS(tty)) {
943 /* Assert RTS and DTR lines */
944 save_flags(flags); cli();
945 info->zs_chan_a->curregs[5] |= DTR | RTS;
946 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
947 restore_flags(flags);
952 * ------------------------------------------------------------
953 * rs_ioctl() and friends
954 * ------------------------------------------------------------
957 static int get_serial_info(struct dec_serial * info,
958 struct serial_struct * retinfo)
960 struct serial_struct tmp;
962 if (!retinfo)
963 return -EFAULT;
964 memset(&tmp, 0, sizeof(tmp));
965 tmp.type = info->type;
966 tmp.line = info->line;
967 tmp.port = info->port;
968 tmp.irq = info->irq;
969 tmp.flags = info->flags;
970 tmp.baud_base = info->baud_base;
971 tmp.close_delay = info->close_delay;
972 tmp.closing_wait = info->closing_wait;
973 tmp.custom_divisor = info->custom_divisor;
974 return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
977 static int set_serial_info(struct dec_serial * info,
978 struct serial_struct * new_info)
980 struct serial_struct new_serial;
981 struct dec_serial old_info;
982 int retval = 0;
984 if (!new_info)
985 return -EFAULT;
986 copy_from_user(&new_serial,new_info,sizeof(new_serial));
987 old_info = *info;
989 if (!suser()) {
990 if ((new_serial.baud_base != info->baud_base) ||
991 (new_serial.type != info->type) ||
992 (new_serial.close_delay != info->close_delay) ||
993 ((new_serial.flags & ~ZILOG_USR_MASK) !=
994 (info->flags & ~ZILOG_USR_MASK)))
995 return -EPERM;
996 info->flags = ((info->flags & ~ZILOG_USR_MASK) |
997 (new_serial.flags & ZILOG_USR_MASK));
998 info->custom_divisor = new_serial.custom_divisor;
999 goto check_and_exit;
1002 if (info->count > 1)
1003 return -EBUSY;
1006 * OK, past this point, all the error checking has been done.
1007 * At this point, we start making changes.....
1010 info->baud_base = new_serial.baud_base;
1011 info->flags = ((info->flags & ~ZILOG_FLAGS) |
1012 (new_serial.flags & ZILOG_FLAGS));
1013 info->type = new_serial.type;
1014 info->close_delay = new_serial.close_delay;
1015 info->closing_wait = new_serial.closing_wait;
1017 check_and_exit:
1018 retval = startup(info);
1019 return retval;
1023 * get_lsr_info - get line status register info
1025 * Purpose: Let user call ioctl() to get info when the UART physically
1026 * is emptied. On bus types like RS485, the transmitter must
1027 * release the bus after transmitting. This must be done when
1028 * the transmit shift register is empty, not be done when the
1029 * transmit holding register is empty. This functionality
1030 * allows an RS485 driver to be written in user space.
1032 static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1034 unsigned char status;
1036 cli();
1037 status = read_zsreg(info->zs_channel, 0);
1038 sti();
1039 put_user(status,value);
1040 return 0;
1043 static int get_modem_info(struct dec_serial *info, unsigned int *value)
1045 unsigned char control, status;
1046 unsigned int result;
1048 cli();
1049 control = info->zs_chan_a->curregs[5];
1050 status = read_zsreg(info->zs_channel, 0);
1051 sti();
1052 result = ((control & RTS) ? TIOCM_RTS: 0)
1053 | ((control & DTR) ? TIOCM_DTR: 0)
1054 | ((status & DCD) ? TIOCM_CAR: 0)
1055 | ((status & CTS) ? 0: TIOCM_CTS);
1056 put_user(result,value);
1057 return 0;
1060 static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1061 unsigned int *value)
1063 int error;
1064 unsigned int arg, bits;
1066 error = verify_area(VERIFY_READ, value, sizeof(int));
1067 if (error)
1068 return error;
1069 get_user(arg, value);
1070 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1071 cli();
1072 switch (cmd) {
1073 case TIOCMBIS:
1074 info->zs_chan_a->curregs[5] |= bits;
1075 break;
1076 case TIOCMBIC:
1077 info->zs_chan_a->curregs[5] &= ~bits;
1078 break;
1079 case TIOCMSET:
1080 info->zs_chan_a->curregs[5] = (info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1081 break;
1082 default:
1083 sti();
1084 return -EINVAL;
1086 write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1087 sti();
1088 return 0;
1092 * rs_break - turn transmit break condition on/off
1094 static void rs_break(struct tty_struct *tty, int break_state)
1096 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1097 unsigned long flags;
1099 if (serial_paranoia_check(info, tty->device, "rs_break"))
1100 return;
1101 if (!info->port)
1102 return;
1104 save_flags(flags); cli();
1105 if (break_state == -1)
1106 info->zs_channel->curregs[5] |= SND_BRK;
1107 else
1108 info->zs_channel->curregs[5] &= ~SND_BRK;
1109 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1110 restore_flags(flags);
1113 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1114 unsigned int cmd, unsigned long arg)
1116 int error;
1117 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1119 #ifdef CONFIG_KGDB
1120 if (info->kgdb_channel)
1121 return -ENODEV;
1122 #endif
1123 if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1124 return -ENODEV;
1126 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1127 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1128 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1129 if (tty->flags & (1 << TTY_IO_ERROR))
1130 return -EIO;
1133 switch (cmd) {
1134 case TIOCMGET:
1135 error = verify_area(VERIFY_WRITE, (void *) arg,
1136 sizeof(unsigned int));
1137 if (error)
1138 return error;
1139 return get_modem_info(info, (unsigned int *) arg);
1140 case TIOCMBIS:
1141 case TIOCMBIC:
1142 case TIOCMSET:
1143 return set_modem_info(info, cmd, (unsigned int *) arg);
1144 case TIOCGSERIAL:
1145 error = verify_area(VERIFY_WRITE, (void *) arg,
1146 sizeof(struct serial_struct));
1147 if (error)
1148 return error;
1149 return get_serial_info(info,
1150 (struct serial_struct *) arg);
1151 case TIOCSSERIAL:
1152 return set_serial_info(info,
1153 (struct serial_struct *) arg);
1154 case TIOCSERGETLSR: /* Get line status register */
1155 error = verify_area(VERIFY_WRITE, (void *) arg,
1156 sizeof(unsigned int));
1157 if (error)
1158 return error;
1159 else
1160 return get_lsr_info(info, (unsigned int *) arg);
1162 case TIOCSERGSTRUCT:
1163 error = verify_area(VERIFY_WRITE, (void *) arg,
1164 sizeof(struct dec_serial));
1165 if (error)
1166 return error;
1167 copy_from_user((struct dec_serial *) arg,
1168 info, sizeof(struct dec_serial));
1169 return 0;
1171 default:
1172 return -ENOIOCTLCMD;
1174 return 0;
1177 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1179 struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1180 int was_stopped;
1182 if (tty->termios->c_cflag == old_termios->c_cflag)
1183 return;
1184 was_stopped = info->tx_stopped;
1186 change_speed(info);
1188 if (was_stopped && !info->tx_stopped)
1189 rs_start(tty);
1193 * ------------------------------------------------------------
1194 * rs_close()
1196 * This routine is called when the serial port gets closed.
1197 * Wait for the last remaining data to be sent.
1198 * ------------------------------------------------------------
1200 static void rs_close(struct tty_struct *tty, struct file * filp)
1202 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1203 unsigned long flags;
1205 if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1206 return;
1208 save_flags(flags); cli();
1210 if (tty_hung_up_p(filp)) {
1211 restore_flags(flags);
1212 return;
1215 #ifdef SERIAL_DEBUG_OPEN
1216 printk("rs_close ttys%d, count = %d\n", info->line, info->count);
1217 #endif
1218 if ((tty->count == 1) && (info->count != 1)) {
1220 * Uh, oh. tty->count is 1, which means that the tty
1221 * structure will be freed. Info->count should always
1222 * be one in these conditions. If it's greater than
1223 * one, we've got real problems, since it means the
1224 * serial port won't be shutdown.
1226 printk("rs_close: bad serial port count; tty->count is 1, "
1227 "info->count is %d\n", info->count);
1228 info->count = 1;
1230 if (--info->count < 0) {
1231 printk("rs_close: bad serial port count for ttys%d: %d\n",
1232 info->line, info->count);
1233 info->count = 0;
1235 if (info->count) {
1236 restore_flags(flags);
1237 return;
1239 info->flags |= ZILOG_CLOSING;
1241 * Save the termios structure, since this port may have
1242 * separate termios for callout and dialin.
1244 if (info->flags & ZILOG_NORMAL_ACTIVE)
1245 info->normal_termios = *tty->termios;
1246 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1247 info->callout_termios = *tty->termios;
1249 * Now we wait for the transmit buffer to clear; and we notify
1250 * the line discipline to only process XON/XOFF characters.
1252 tty->closing = 1;
1253 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1254 tty_wait_until_sent(tty, info->closing_wait);
1256 * At this point we stop accepting input. To do this, we
1257 * disable the receiver and receive interrupts.
1259 info->zs_channel->curregs[3] &= ~RxENABLE;
1260 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1261 info->zs_channel->curregs[1] = 0; /* disable any rx ints */
1262 write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1263 ZS_CLEARFIFO(info->zs_channel);
1264 if (info->flags & ZILOG_INITIALIZED) {
1266 * Before we drop DTR, make sure the SCC transmitter
1267 * has completely drained.
1269 rs_wait_until_sent(tty, info->timeout);
1272 shutdown(info);
1273 if (tty->driver.flush_buffer)
1274 tty->driver.flush_buffer(tty);
1275 if (tty->ldisc.flush_buffer)
1276 tty->ldisc.flush_buffer(tty);
1277 tty->closing = 0;
1278 info->event = 0;
1279 info->tty = 0;
1280 if (info->blocked_open) {
1281 if (info->close_delay) {
1282 current->state = TASK_INTERRUPTIBLE;
1283 schedule_timeout(info->close_delay);
1285 wake_up_interruptible(&info->open_wait);
1287 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1288 ZILOG_CLOSING);
1289 wake_up_interruptible(&info->close_wait);
1290 restore_flags(flags);
1294 * rs_wait_until_sent() --- wait until the transmitter is empty
1296 static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1298 struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1299 unsigned long orig_jiffies, char_time;
1301 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1302 return;
1304 orig_jiffies = jiffies;
1306 * Set the check interval to be 1/5 of the estimated time to
1307 * send a single character, and make it at least 1. The check
1308 * interval should also be less than the timeout.
1310 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1311 char_time = char_time / 5;
1312 if (char_time == 0)
1313 char_time = 1;
1314 if (timeout)
1315 char_time = MIN(char_time, timeout);
1316 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) {
1317 current->state = TASK_INTERRUPTIBLE;
1318 schedule_timeout(char_time);
1319 if (signal_pending(current))
1320 break;
1321 if (timeout && ((orig_jiffies + timeout) < jiffies))
1322 break;
1324 current->state = TASK_RUNNING;
1328 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1330 void rs_hangup(struct tty_struct *tty)
1332 struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1334 if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1335 return;
1337 rs_flush_buffer(tty);
1338 shutdown(info);
1339 info->event = 0;
1340 info->count = 0;
1341 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1342 info->tty = 0;
1343 wake_up_interruptible(&info->open_wait);
1347 * ------------------------------------------------------------
1348 * rs_open() and friends
1349 * ------------------------------------------------------------
1351 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1352 struct dec_serial *info)
1354 struct wait_queue wait = { current, NULL };
1355 int retval;
1356 int do_clocal = 0;
1359 * If the device is in the middle of being closed, then block
1360 * until it's done, and then try again.
1362 if (info->flags & ZILOG_CLOSING) {
1363 interruptible_sleep_on(&info->close_wait);
1364 #ifdef SERIAL_DO_RESTART
1365 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1366 -EAGAIN : -ERESTARTSYS);
1367 #else
1368 return -EAGAIN;
1369 #endif
1373 * If this is a callout device, then just make sure the normal
1374 * device isn't being used.
1376 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1377 if (info->flags & ZILOG_NORMAL_ACTIVE)
1378 return -EBUSY;
1379 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1380 (info->flags & ZILOG_SESSION_LOCKOUT) &&
1381 (info->session != current->session))
1382 return -EBUSY;
1383 if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1384 (info->flags & ZILOG_PGRP_LOCKOUT) &&
1385 (info->pgrp != current->pgrp))
1386 return -EBUSY;
1387 info->flags |= ZILOG_CALLOUT_ACTIVE;
1388 return 0;
1392 * If non-blocking mode is set, or the port is not enabled,
1393 * then make the check up front and then exit.
1395 if ((filp->f_flags & O_NONBLOCK) ||
1396 (tty->flags & (1 << TTY_IO_ERROR))) {
1397 if (info->flags & ZILOG_CALLOUT_ACTIVE)
1398 return -EBUSY;
1399 info->flags |= ZILOG_NORMAL_ACTIVE;
1400 return 0;
1403 if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1404 if (info->normal_termios.c_cflag & CLOCAL)
1405 do_clocal = 1;
1406 } else {
1407 if (tty->termios->c_cflag & CLOCAL)
1408 do_clocal = 1;
1412 * Block waiting for the carrier detect and the line to become
1413 * free (i.e., not in use by the callout). While we are in
1414 * this loop, info->count is dropped by one, so that
1415 * rs_close() knows when to free things. We restore it upon
1416 * exit, either normal or abnormal.
1418 retval = 0;
1419 add_wait_queue(&info->open_wait, &wait);
1420 #ifdef SERIAL_DEBUG_OPEN
1421 printk("block_til_ready before block: ttys%d, count = %d\n",
1422 info->line, info->count);
1423 #endif
1424 cli();
1425 if (!tty_hung_up_p(filp))
1426 info->count--;
1427 sti();
1428 info->blocked_open++;
1429 while (1) {
1430 cli();
1431 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1432 (tty->termios->c_cflag & CBAUD))
1433 zs_rtsdtr(info, 1);
1434 sti();
1435 set_current_state(TASK_INTERRUPTIBLE);
1436 if (tty_hung_up_p(filp) ||
1437 !(info->flags & ZILOG_INITIALIZED)) {
1438 #ifdef SERIAL_DO_RESTART
1439 if (info->flags & ZILOG_HUP_NOTIFY)
1440 retval = -EAGAIN;
1441 else
1442 retval = -ERESTARTSYS;
1443 #else
1444 retval = -EAGAIN;
1445 #endif
1446 break;
1448 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1449 !(info->flags & ZILOG_CLOSING) &&
1450 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1451 break;
1452 if (signal_pending(current)) {
1453 retval = -ERESTARTSYS;
1454 break;
1456 #ifdef SERIAL_DEBUG_OPEN
1457 printk("block_til_ready blocking: ttys%d, count = %d\n",
1458 info->line, info->count);
1459 #endif
1460 schedule();
1462 current->state = TASK_RUNNING;
1463 remove_wait_queue(&info->open_wait, &wait);
1464 if (!tty_hung_up_p(filp))
1465 info->count++;
1466 info->blocked_open--;
1467 #ifdef SERIAL_DEBUG_OPEN
1468 printk("block_til_ready after blocking: ttys%d, count = %d\n",
1469 info->line, info->count);
1470 #endif
1471 if (retval)
1472 return retval;
1473 info->flags |= ZILOG_NORMAL_ACTIVE;
1474 return 0;
1478 * This routine is called whenever a serial port is opened. It
1479 * enables interrupts for a serial port, linking in its ZILOG structure into
1480 * the IRQ chain. It also performs the serial-specific
1481 * initialization for the tty structure.
1483 int rs_open(struct tty_struct *tty, struct file * filp)
1485 struct dec_serial *info;
1486 int retval, line;
1488 line = MINOR(tty->device) - tty->driver.minor_start;
1489 if ((line < 0) || (line >= zs_channels_found))
1490 return -ENODEV;
1491 info = zs_soft + line;
1493 #ifdef CONFIG_KGDB
1494 if (info->kgdb_channel)
1495 return -ENODEV;
1496 #endif
1497 if (serial_paranoia_check(info, tty->device, "rs_open"))
1498 return -ENODEV;
1499 #ifdef SERIAL_DEBUG_OPEN
1500 printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1501 info->count);
1502 #endif
1504 info->count++;
1505 tty->driver_data = info;
1506 info->tty = tty;
1509 * If the port is the middle of closing, bail out now
1511 if (tty_hung_up_p(filp) ||
1512 (info->flags & ZILOG_CLOSING)) {
1513 if (info->flags & ZILOG_CLOSING)
1514 interruptible_sleep_on(&info->close_wait);
1515 #ifdef SERIAL_DO_RESTART
1516 return ((info->flags & ZILOG_HUP_NOTIFY) ?
1517 -EAGAIN : -ERESTARTSYS);
1518 #else
1519 return -EAGAIN;
1520 #endif
1524 * Start up serial port
1526 retval = startup(info);
1527 if (retval)
1528 return retval;
1530 retval = block_til_ready(tty, filp, info);
1531 if (retval) {
1532 #ifdef SERIAL_DEBUG_OPEN
1533 printk("rs_open returning after block_til_ready with %d\n",
1534 retval);
1535 #endif
1536 return retval;
1539 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1540 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1541 *tty->termios = info->normal_termios;
1542 else
1543 *tty->termios = info->callout_termios;
1544 change_speed(info);
1546 #ifdef CONFIG_SERIAL_CONSOLE
1547 if (sercons.cflag && sercons.index == line) {
1548 tty->termios->c_cflag = sercons.cflag;
1549 sercons.cflag = 0;
1550 change_speed(info);
1552 #endif
1554 info->session = current->session;
1555 info->pgrp = current->pgrp;
1557 #ifdef SERIAL_DEBUG_OPEN
1558 printk("rs_open ttys%d successful...", info->line);
1559 #endif
1560 /* tty->low_latency = 1; */
1561 return 0;
1564 /* Finally, routines used to initialize the serial driver. */
1566 static void __init show_serial_version(void)
1568 printk("DECstation Z8530 serial driver version 0.03\n");
1571 /* Initialize Z8530s zs_channels
1574 static void __init probe_sccs(void)
1576 struct dec_serial **pp;
1577 int i, n, n_chips = 0, n_channels, chip, channel;
1578 unsigned long flags;
1581 * did we get here by accident?
1583 if(!IOASIC) {
1584 printk("Not on JUNKIO machine, skipping probe_sccs\n");
1585 return;
1589 * When serial console is activated, tc_init has not been called yet
1590 * and system_base is undefined. Unfortunately we have to hardcode
1591 * system_base for this case :-(. HK
1593 switch(mips_machtype) {
1594 case MACH_DS5000_2X0:
1595 system_base = 0xbf800000;
1596 n_chips = 2;
1597 break;
1598 case MACH_DS5000_1XX:
1599 system_base = 0xbc000000;
1600 n_chips = 2;
1601 break;
1602 case MACH_DS5000_XX:
1603 system_base = 0xbc000000;
1604 n_chips = 1;
1605 break;
1608 pp = &zs_chain;
1610 n_channels = 0;
1612 for (chip = 0; chip < n_chips; chip++) {
1613 for (channel = 0; channel <= 1; channel++) {
1615 * The sccs reside on the high byte of the 16 bit IOBUS
1617 zs_channels[n_channels].control = (volatile unsigned char *)
1618 system_base + (0 == chip ? SCC0 : SCC1) + (0 == channel ? 1 : 9);
1619 zs_channels[n_channels].data = zs_channels[n_channels].control + 4;
1620 zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1621 zs_soft[n_channels].irq = SERIAL;
1623 if (0 == channel)
1624 zs_soft[n_channels].zs_chan_a = &zs_channels[n_channels+1];
1625 else
1626 zs_soft[n_channels].zs_chan_a = &zs_channels[n_channels];
1628 *pp = &zs_soft[n_channels];
1629 pp = &zs_soft[n_channels].zs_next;
1630 n_channels++;
1634 *pp = 0;
1635 zs_channels_found = n_channels;
1637 for (n = 0; n < zs_channels_found; n++) {
1638 for (i = 0; i < 16; i++) {
1639 zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1643 /* save_and_cli(flags);
1644 for (n = 0; n < zs_channels_found; n++) {
1645 if (((int)zs_channels[n].control & 0xf) == 1) {
1646 write_zsreg(zs_soft[channel].zs_chan_a, R9, FHWRES);
1647 udelay(10000);
1648 write_zsreg(zs_soft[channel].zs_chan_a, R9, 0);
1650 load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1652 restore_flags(flags); */
1655 /* zs_init inits the driver */
1656 int __init zs_init(void)
1658 int channel, i;
1659 unsigned long flags;
1660 struct dec_serial *info;
1662 if(!IOASIC)
1663 return -ENODEV;
1665 /* Setup base handler, and timer table. */
1666 init_bh(SERIAL_BH, do_serial_bh);
1667 timer_table[RS_TIMER].fn = rs_timer;
1668 timer_table[RS_TIMER].expires = 0;
1670 /* Find out how many Z8530 SCCs we have */
1671 if (zs_chain == 0)
1672 probe_sccs();
1674 show_serial_version();
1676 /* Initialize the tty_driver structure */
1677 /* Not all of this is exactly right for us. */
1679 memset(&serial_driver, 0, sizeof(struct tty_driver));
1680 serial_driver.magic = TTY_DRIVER_MAGIC;
1681 serial_driver.name = "ttyS";
1682 serial_driver.major = TTY_MAJOR;
1683 serial_driver.minor_start = 64;
1684 serial_driver.num = zs_channels_found;
1685 serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1686 serial_driver.subtype = SERIAL_TYPE_NORMAL;
1687 serial_driver.init_termios = tty_std_termios;
1689 serial_driver.init_termios.c_cflag =
1690 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1691 serial_driver.flags = TTY_DRIVER_REAL_RAW;
1692 serial_driver.refcount = &serial_refcount;
1693 serial_driver.table = serial_table;
1694 serial_driver.termios = serial_termios;
1695 serial_driver.termios_locked = serial_termios_locked;
1697 serial_driver.open = rs_open;
1698 serial_driver.close = rs_close;
1699 serial_driver.write = rs_write;
1700 serial_driver.flush_chars = rs_flush_chars;
1701 serial_driver.write_room = rs_write_room;
1702 serial_driver.chars_in_buffer = rs_chars_in_buffer;
1703 serial_driver.flush_buffer = rs_flush_buffer;
1704 serial_driver.ioctl = rs_ioctl;
1705 serial_driver.throttle = rs_throttle;
1706 serial_driver.unthrottle = rs_unthrottle;
1707 serial_driver.set_termios = rs_set_termios;
1708 serial_driver.stop = rs_stop;
1709 serial_driver.start = rs_start;
1710 serial_driver.hangup = rs_hangup;
1711 serial_driver.break_ctl = rs_break;
1712 serial_driver.wait_until_sent = rs_wait_until_sent;
1715 * The callout device is just like normal device except for
1716 * major number and the subtype code.
1718 callout_driver = serial_driver;
1719 callout_driver.name = "cua";
1720 callout_driver.major = TTYAUX_MAJOR;
1721 callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1723 if (tty_register_driver(&serial_driver))
1724 panic("Couldn't register serial driver\n");
1725 if (tty_register_driver(&callout_driver))
1726 panic("Couldn't register callout driver\n");
1728 save_flags(flags); cli();
1730 for (channel = 0; channel < zs_channels_found; ++channel) {
1731 #ifdef CONFIG_KGDB
1732 if (zs_soft[channel].kgdb_channel) {
1733 continue;
1735 #endif
1736 zs_soft[channel].clk_divisor = 16;
1737 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1739 if (request_irq(SERIAL, rs_interrupt, SA_SHIRQ,
1740 "SCC", &zs_soft[channel]))
1741 printk(KERN_ERR "decserial: can't get irq %d\n",
1742 SERIAL);
1744 /* If console serial line, then enable interrupts. */
1745 /* if (zs_soft[channel].is_cons) {
1746 write_zsreg(zs_soft[channel].zs_channel, R1,
1747 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB));
1748 write_zsreg(zs_soft[channel].zs_channel, R9,
1749 (VIS | MIE));
1754 for (info = zs_chain, i = 0; info; info = info->zs_next, i++)
1756 #ifdef CONFIG_KGDB
1757 if (info->kgdb_channel) {
1758 continue;
1760 #endif
1761 info->magic = SERIAL_MAGIC;
1762 info->port = (int) info->zs_channel->control;
1763 info->line = i;
1764 info->tty = 0;
1765 info->custom_divisor = 16;
1766 info->close_delay = 50;
1767 info->closing_wait = 3000;
1768 info->x_char = 0;
1769 info->event = 0;
1770 info->count = 0;
1771 info->blocked_open = 0;
1772 info->tqueue.routine = do_softint;
1773 info->tqueue.data = info;
1774 info->callout_termios =callout_driver.init_termios;
1775 info->normal_termios = serial_driver.init_termios;
1776 info->open_wait = 0;
1777 info->close_wait = 0;
1778 printk("tty%02d at 0x%08x (irq = %d)", info->line,
1779 info->port, info->irq);
1780 printk(" is a Z85C30 SCC\n");
1783 restore_flags(flags);
1785 return 0;
1789 * register_serial and unregister_serial allows for serial ports to be
1790 * configured at run-time, to support PCMCIA modems.
1792 /* PowerMac: Unused at this time, just here to make things link. */
1793 int register_serial(struct serial_struct *req)
1795 return -1;
1798 void unregister_serial(int line)
1800 return;
1804 * ------------------------------------------------------------
1805 * Serial console driver
1806 * ------------------------------------------------------------
1808 #ifdef CONFIG_SERIAL_CONSOLE
1812 * Print a string to the serial port trying not to disturb
1813 * any possible real use of the port...
1816 /* This is for console output */
1817 static void
1818 zs_console_putchar(struct dec_serial *info, char ch)
1820 int loops = 10000;
1821 unsigned long flags;
1823 if(!info->zs_channel)
1824 return;
1826 save_flags(flags); cli();
1828 while (!(*(info->zs_channel->control) & Tx_BUF_EMP) && --loops)
1829 RECOVERY_DELAY;
1830 *(info->zs_channel->data) = ch;
1831 RECOVERY_DELAY;
1833 restore_flags(flags);
1836 static void serial_console_write(struct console *co, const char *s,
1837 unsigned count)
1839 struct dec_serial *info;
1840 int i;
1841 unsigned char nine;
1843 info = zs_soft + co->index;
1845 #if 0
1847 * disable master interrupt if necessary
1849 nine = info->zs_channel->curregs[9];
1850 if(nine & MIE)
1851 write_zsreg(info->zs_channel, R9, nine & ~MIE);
1852 #endif
1854 * do it
1856 for (i = 0; i < count; i++, s++) {
1857 if(*s == '\n')
1858 zs_console_putchar(info, '\r');
1859 zs_console_putchar(info, *s);
1862 * restore master interrupt enable
1864 #if 0
1865 write_zsreg(info->zs_channel, R9, nine);
1866 #endif
1870 * Receive character from the serial port
1872 static int serial_console_wait_key(struct console *co)
1874 return 0;
1877 static kdev_t serial_console_device(struct console *c)
1879 return MKDEV(TTY_MAJOR, 64 + c->index);
1883 * Setup initial baud/bits/parity. We do two things here:
1884 * - construct a cflag setting for the first rs_open()
1885 * - initialize the serial port
1886 * Return non-zero if we didn't find a serial port.
1888 static int __init serial_console_setup(struct console *co, char *options)
1890 struct dec_serial *info;
1891 int baud = 9600;
1892 int bits = 8;
1893 int parity = 'n';
1894 int cflag = CREAD | HUPCL | CLOCAL;
1895 char *s;
1896 unsigned long flags;
1898 if(!IOASIC)
1899 return -ENODEV;
1901 info = zs_soft + co->index;
1903 if (zs_chain == 0)
1904 probe_sccs();
1906 info->is_cons = 1;
1908 if (options) {
1909 baud = simple_strtoul(options, NULL, 10);
1910 s = options;
1911 while(*s >= '0' && *s <= '9')
1912 s++;
1913 if (*s)
1914 parity = *s++;
1915 if (*s)
1916 bits = *s - '0';
1920 * Now construct a cflag setting.
1922 switch(baud) {
1923 case 1200:
1924 cflag |= B1200;
1925 break;
1926 case 2400:
1927 cflag |= B2400;
1928 break;
1929 case 4800:
1930 cflag |= B4800;
1931 break;
1932 case 19200:
1933 cflag |= B19200;
1934 break;
1935 case 38400:
1936 cflag |= B38400;
1937 break;
1938 case 57600:
1939 cflag |= B57600;
1940 break;
1941 case 115200:
1942 cflag |= B115200;
1943 break;
1944 case 9600:
1945 default:
1946 cflag |= B9600;
1947 break;
1949 switch(bits) {
1950 case 7:
1951 cflag |= CS7;
1952 break;
1953 default:
1954 case 8:
1955 cflag |= CS8;
1956 break;
1958 switch(parity) {
1959 case 'o': case 'O':
1960 cflag |= PARODD;
1961 break;
1962 case 'e': case 'E':
1963 cflag |= PARENB;
1964 break;
1966 co->cflag = cflag;
1967 #if 1
1968 save_and_cli(flags);
1971 * Turn on RTS and DTR.
1973 zs_rtsdtr(info, 1);
1976 * Finally, enable sequencing
1978 info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
1979 info->zs_channel->curregs[5] |= (TxENAB | Tx8);
1980 info->zs_channel->curregs[9] |= (VIS);
1981 write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1982 write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1983 write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
1986 * Clear the interrupt registers.
1988 write_zsreg(info->zs_channel, 0, ERR_RES);
1989 write_zsreg(info->zs_channel, 0, RES_H_IUS);
1992 * Set the speed of the serial port
1994 change_speed(info);
1996 /* Save the current value of RR0 */
1997 info->read_reg_zero = read_zsreg(info->zs_channel, 0);
1999 zs_soft[co->index].clk_divisor = 16;
2000 zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2002 restore_flags(flags);
2003 #endif
2004 return 0;
2007 static struct console sercons = {
2008 "ttyS",
2009 serial_console_write,
2010 NULL,
2011 serial_console_device,
2012 serial_console_wait_key,
2013 NULL,
2014 serial_console_setup,
2015 CON_PRINTBUFFER,
2018 NULL
2022 * Register console.
2024 long __init zs_serial_console_init(long kmem_start, long kmem_end)
2026 register_console(&sercons);
2027 return kmem_start;
2029 #endif /* ifdef CONFIG_SERIAL_CONSOLE */
2031 #ifdef CONFIG_KGDB
2032 /* These are for receiving and sending characters under the kgdb
2033 * source level kernel debugger.
2035 void putDebugChar(char kgdb_char)
2037 struct dec_zschannel *chan = zs_kgdbchan;
2038 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2039 RECOVERY_DELAY;
2040 write_zsdata(chan, kgdb_char);
2042 char getDebugChar(void)
2044 struct dec_zschannel *chan = zs_kgdbchan;
2045 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2046 eieio(); /*barrier();*/
2047 return read_zsdata(chan);
2049 void kgdb_interruptible(int yes)
2051 struct dec_zschannel *chan = zs_kgdbchan;
2052 int one, nine;
2053 nine = read_zsreg(chan, 9);
2054 if (yes == 1) {
2055 one = EXT_INT_ENAB|INT_ALL_Rx;
2056 nine |= MIE;
2057 printk("turning serial ints on\n");
2058 } else {
2059 one = RxINT_DISAB;
2060 nine &= ~MIE;
2061 printk("turning serial ints off\n");
2063 write_zsreg(chan, 1, one);
2064 write_zsreg(chan, 9, nine);
2066 /* This sets up the serial port we're using, and turns on
2067 * interrupts for that channel, so kgdb is usable once we're done.
2069 static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2071 int brg;
2072 int i, x;
2073 volatile char *sccc = ms->control;
2074 brg = BPS_TO_BRG(bps, ZS_CLOCK/16);
2075 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2076 for (i = 20000; i != 0; --i) {
2077 x = *sccc; eieio();
2079 for (i = 0; i < sizeof(scc_inittab); ++i) {
2080 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2081 i++;
2084 /* This is called at boot time to prime the kgdb serial debugging
2085 * serial line. The 'tty_num' argument is 0 for /dev/ttya and 1
2086 * for /dev/ttyb which is determined in setup_arch() from the
2087 * boot command line flags.
2089 void __init zs_kgdb_hook(int tty_num)
2091 /* Find out how many Z8530 SCCs we have */
2092 if (zs_chain == 0)
2093 probe_sccs();
2094 zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2095 zs_kgdbchan = zs_soft[tty_num].zs_channel;
2096 zs_soft[tty_num].change_needed = 0;
2097 zs_soft[tty_num].clk_divisor = 16;
2098 zs_soft[tty_num].zs_baud = 38400;
2099 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */
2100 zs_soft[tty_num ^ 1].kgdb_channel = 0; /* This does not */
2101 /* Turn on transmitter/receiver at 8-bits/char */
2102 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2103 printk("KGDB: on channel %d initialized\n", tty_num);
2104 set_debug_traps(); /* init stub */
2106 #endif /* ifdef CONFIG_KGDB */