1/*
2 * decserial.c: Serial port driver for IOASIC DECstations.
3 *
4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras.
5 * Derived from drivers/macintosh/macserial.c by Harald Koerfgen.
6 *
7 * DECstation changes
8 * Copyright (C) 1998-2000 Harald Koerfgen
9 * Copyright (C) 2000, 2001, 2002  Maciej W. Rozycki <macro@ds2.pg.gda.pl>
10 *
11 * For the rest of the code the original Copyright applies:
12 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au)
13 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
14 *
15 *
16 * Note: for IOASIC systems the wiring is as follows:
17 *
18 * mouse/keyboard:
19 * DIN-7 MJ-4  signal        SCC
20 * 2     1     TxD       <-  A.TxD
21 * 3     4     RxD       ->  A.RxD
22 *
23 * EIA-232/EIA-423:
24 * DB-25 MMJ-6 signal        SCC
25 * 2     2     TxD       <-  B.TxD
26 * 3     5     RxD       ->  B.RxD
27 * 4           RTS       <- ~A.RTS
28 * 5           CTS       -> ~B.CTS
29 * 6     6     DSR       -> ~A.SYNC
30 * 8           CD        -> ~B.DCD
31 * 12          DSRS(DCE) -> ~A.CTS  (*)
32 * 15          TxC       ->  B.TxC
33 * 17          RxC       ->  B.RxC
34 * 20    1     DTR       <- ~A.DTR
35 * 22          RI        -> ~A.DCD
36 * 23          DSRS(DTE) <- ~B.RTS
37 *
38 * (*) EIA-232 defines the signal at this pin to be SCD, while DSRS(DCE)
39 *     is shared with DSRS(DTE) at pin 23.
40 */
41
42#include <linux/config.h>
43#include <linux/version.h>
44#include <linux/errno.h>
45#include <linux/signal.h>
46#include <linux/sched.h>
47#include <linux/timer.h>
48#include <linux/interrupt.h>
49#include <linux/tty.h>
50#include <linux/tty_flip.h>
51#include <linux/major.h>
52#include <linux/string.h>
53#include <linux/fcntl.h>
54#include <linux/mm.h>
55#include <linux/kernel.h>
56#include <linux/delay.h>
57#include <linux/init.h>
58#include <linux/ioport.h>
59#ifdef CONFIG_SERIAL_DEC_CONSOLE
60#include <linux/console.h>
61#endif
62
63#include <asm/io.h>
64#include <asm/pgtable.h>
65#include <asm/irq.h>
66#include <asm/system.h>
67#include <asm/segment.h>
68#include <asm/bitops.h>
69#include <asm/uaccess.h>
70#include <asm/bootinfo.h>
71#ifdef CONFIG_DECSTATION
72#include <asm/dec/interrupts.h>
73#include <asm/dec/machtype.h>
74#include <asm/dec/tc.h>
75#include <asm/dec/ioasic_addrs.h>
76#endif
77#ifdef CONFIG_BAGET_MIPS
78#include <asm/baget/baget.h>
79unsigned long system_base;
80#endif
81#ifdef CONFIG_KGDB
82#include <asm/kgdb.h>
83#endif
84#ifdef CONFIG_MAGIC_SYSRQ
85#include <linux/sysrq.h>
86#endif
87
88#include "zs.h"
89
90/*
91 * It would be nice to dynamically allocate everything that
92 * depends on NUM_SERIAL, so we could support any number of
93 * Z8530s, but for now...
94 */
95#define NUM_SERIAL	2		/* Max number of ZS chips supported */
96#define NUM_CHANNELS	(NUM_SERIAL * 2)	/* 2 channels per chip */
97#define CHANNEL_A_NR  (zs_parms->channel_a_offset > zs_parms->channel_b_offset)
98                                        /* Number of channel A in the chip */
99#define ZS_CHAN_IO_SIZE 8
100#define ZS_CLOCK        7372800 	/* Z8530 RTxC input clock rate */
101
102#define RECOVERY_DELAY  udelay(2)
103
104struct zs_parms {
105	unsigned long scc0;
106	unsigned long scc1;
107	int channel_a_offset;
108	int channel_b_offset;
109	int irq0;
110	int irq1;
111	int clock;
112};
113
114static struct zs_parms *zs_parms;
115
116#ifdef CONFIG_DECSTATION
117static struct zs_parms ds_parms = {
118	scc0 : SCC0,
119	scc1 : SCC1,
120	channel_a_offset : 1,
121	channel_b_offset : 9,
122	irq0 : -1,
123	irq1 : -1,
124	clock : ZS_CLOCK
125};
126#endif
127#ifdef CONFIG_BAGET_MIPS
128static struct zs_parms baget_parms = {
129	scc0 : UNI_SCC0,
130	scc1 : UNI_SCC1,
131	channel_a_offset : 9,
132	channel_b_offset : 1,
133	irq0 : BAGET_SCC_IRQ,
134	irq1 : BAGET_SCC_IRQ,
135	clock : 14745000
136};
137#endif
138
139#ifdef CONFIG_DECSTATION
140#define DS_BUS_PRESENT (IOASIC)
141#else
142#define DS_BUS_PRESENT 0
143#endif
144
145#ifdef CONFIG_BAGET_MIPS
146#define BAGET_BUS_PRESENT (mips_machtype == MACH_BAGET202)
147#else
148#define BAGET_BUS_PRESENT 0
149#endif
150
151#define BUS_PRESENT (DS_BUS_PRESENT || BAGET_BUS_PRESENT)
152
153struct dec_zschannel zs_channels[NUM_CHANNELS];
154struct dec_serial zs_soft[NUM_CHANNELS];
155int zs_channels_found;
156struct dec_serial *zs_chain;	/* list of all channels */
157
158struct tty_struct zs_ttys[NUM_CHANNELS];
159
160#ifdef CONFIG_SERIAL_DEC_CONSOLE
161static struct console sercons;
162#endif
163#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) \
164    && !defined(MODULE)
165static unsigned long break_pressed; /* break, really ... */
166#endif
167
168static unsigned char zs_init_regs[16] __initdata = {
169	0,                           /* write 0 */
170	0,			     /* write 1 */
171	0xf0,                        /* write 2 */
172	(Rx8),                       /* write 3 */
173	(X16CLK | SB1),              /* write 4 */
174	(Tx8),                       /* write 5 */
175	0, 0, 0,                     /* write 6, 7, 8 */
176	(VIS),                       /* write 9 */
177	(NRZ),                       /* write 10 */
178	(TCBR | RCBR),               /* write 11 */
179	0, 0,                        /* BRG time constant, write 12 + 13 */
180	(BRSRC | BRENABL),           /* write 14 */
181	0 			     /* write 15 */
182};
183
184DECLARE_TASK_QUEUE(tq_zs_serial);
185
186struct tty_driver serial_driver, callout_driver;
187static int serial_refcount;
188
189/* serial subtype definitions */
190#define SERIAL_TYPE_NORMAL	1
191#define SERIAL_TYPE_CALLOUT	2
192
193/* number of characters left in xmit buffer before we ask for more */
194#define WAKEUP_CHARS 256
195
196/*
197 * Debugging.
198 */
199#undef SERIAL_DEBUG_INTR
200#undef SERIAL_DEBUG_OPEN
201#undef SERIAL_DEBUG_FLOW
202#undef SERIAL_DEBUG_THROTTLE
203#undef SERIAL_PARANOIA_CHECK
204
205#undef ZS_DEBUG_REGS
206
207#ifdef SERIAL_DEBUG_THROTTLE
208#define _tty_name(tty,buf) tty_name(tty,buf)
209#endif
210
211#define RS_STROBE_TIME 10
212#define RS_ISR_PASS_LIMIT 256
213
214#define _INLINE_ inline
215
216static void probe_sccs(void);
217static void change_speed(struct dec_serial *info);
218static void rs_wait_until_sent(struct tty_struct *tty, int timeout);
219
220static struct tty_struct *serial_table[NUM_CHANNELS];
221static struct termios *serial_termios[NUM_CHANNELS];
222static struct termios *serial_termios_locked[NUM_CHANNELS];
223
224#ifndef MIN
225#define MIN(a,b)	((a) < (b) ? (a) : (b))
226#endif
227
228/*
229 * tmp_buf is used as a temporary buffer by serial_write.  We need to
230 * lock it in case the copy_from_user blocks while swapping in a page,
231 * and some other program tries to do a serial write at the same time.
232 * Since the lock will only come under contention when the system is
233 * swapping and available memory is low, it makes sense to share one
234 * buffer across all the serial ports, since it significantly saves
235 * memory if large numbers of serial ports are open.
236 */
237static unsigned char tmp_buf[4096]; /* This is cheating */
238static DECLARE_MUTEX(tmp_buf_sem);
239
240static inline int serial_paranoia_check(struct dec_serial *info,
241					dev_t device, const char *routine)
242{
243#ifdef SERIAL_PARANOIA_CHECK
244	static const char *badmagic =
245		"Warning: bad magic number for serial struct (%d, %d) in %s\n";
246	static const char *badinfo =
247		"Warning: null mac_serial for (%d, %d) in %s\n";
248
249	if (!info) {
250		printk(badinfo, MAJOR(device), MINOR(device), routine);
251		return 1;
252	}
253	if (info->magic != SERIAL_MAGIC) {
254		printk(badmagic, MAJOR(device), MINOR(device), routine);
255		return 1;
256	}
257#endif
258	return 0;
259}
260
261/*
262 * This is used to figure out the divisor speeds and the timeouts
263 */
264static int baud_table[] = {
265	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
266	9600, 19200, 38400, 57600, 115200, 0 };
267
268/*
269 * Reading and writing Z8530 registers.
270 */
271static inline unsigned char read_zsreg(struct dec_zschannel *channel,
272				       unsigned char reg)
273{
274	unsigned char retval;
275
276	if (reg != 0) {
277		*channel->control = reg & 0xf;
278		fast_iob(); RECOVERY_DELAY;
279	}
280	retval = *channel->control;
281	RECOVERY_DELAY;
282	return retval;
283}
284
285static inline void write_zsreg(struct dec_zschannel *channel,
286			       unsigned char reg, unsigned char value)
287{
288	if (reg != 0) {
289		*channel->control = reg & 0xf;
290		fast_iob(); RECOVERY_DELAY;
291	}
292	*channel->control = value;
293	fast_iob(); RECOVERY_DELAY;
294	return;
295}
296
297static inline unsigned char read_zsdata(struct dec_zschannel *channel)
298{
299	unsigned char retval;
300
301	retval = *channel->data;
302	RECOVERY_DELAY;
303	return retval;
304}
305
306static inline void write_zsdata(struct dec_zschannel *channel,
307				unsigned char value)
308{
309	*channel->data = value;
310	fast_iob(); RECOVERY_DELAY;
311	return;
312}
313
314static inline void load_zsregs(struct dec_zschannel *channel,
315			       unsigned char *regs)
316{
317/*	ZS_CLEARERR(channel);
318	ZS_CLEARFIFO(channel); */
319	/* Load 'em up */
320	write_zsreg(channel, R4, regs[R4]);
321	write_zsreg(channel, R3, regs[R3] & ~RxENABLE);
322	write_zsreg(channel, R5, regs[R5] & ~TxENAB);
323	write_zsreg(channel, R9, regs[R9]);
324	write_zsreg(channel, R1, regs[R1]);
325	write_zsreg(channel, R2, regs[R2]);
326	write_zsreg(channel, R10, regs[R10]);
327	write_zsreg(channel, R11, regs[R11]);
328	write_zsreg(channel, R12, regs[R12]);
329	write_zsreg(channel, R13, regs[R13]);
330	write_zsreg(channel, R14, regs[R14]);
331	write_zsreg(channel, R15, regs[R15]);
332	write_zsreg(channel, R3, regs[R3]);
333	write_zsreg(channel, R5, regs[R5]);
334	return;
335}
336
337/* Sets or clears DTR/RTS on the requested line */
338static inline void zs_rtsdtr(struct dec_serial *info, int which, int set)
339{
340        unsigned long flags;
341
342
343	save_flags(flags); cli();
344	if (info->zs_channel != info->zs_chan_a) {
345		if (set) {
346			info->zs_chan_a->curregs[5] |= (which & (RTS | DTR));
347		} else {
348			info->zs_chan_a->curregs[5] &= ~(which & (RTS | DTR));
349		}
350		write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
351	}
352	restore_flags(flags);
353}
354
355/* Utility routines for the Zilog */
356static inline int get_zsbaud(struct dec_serial *ss)
357{
358	struct dec_zschannel *channel = ss->zs_channel;
359	int brg;
360
361	/* The baud rate is split up between two 8-bit registers in
362	 * what is termed 'BRG time constant' format in my docs for
363	 * the chip, it is a function of the clk rate the chip is
364	 * receiving which happens to be constant.
365	 */
366	brg = (read_zsreg(channel, 13) << 8);
367	brg |= read_zsreg(channel, 12);
368	return BRG_TO_BPS(brg, (zs_parms->clock/(ss->clk_divisor)));
369}
370
371/* On receive, this clears errors and the receiver interrupts */
372static inline void rs_recv_clear(struct dec_zschannel *zsc)
373{
374	write_zsreg(zsc, 0, ERR_RES);
375	write_zsreg(zsc, 0, RES_H_IUS);
376}
377
378/*
379 * ----------------------------------------------------------------------
380 *
381 * Here starts the interrupt handling routines.  All of the following
382 * subroutines are declared as inline and are folded into
383 * rs_interrupt().  They were separated out for readability's sake.
384 *
385 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
386 * -----------------------------------------------------------------------
387 */
388
389static int tty_break;	/* Set whenever BREAK condition is detected.  */
390
391/*
392 * This routine is used by the interrupt handler to schedule
393 * processing in the software interrupt portion of the driver.
394 */
395static _INLINE_ void rs_sched_event(struct dec_serial *info,
396				  int event)
397{
398	info->event |= 1 << event;
399	queue_task(&info->tqueue, &tq_zs_serial);
400	mark_bh(SERIAL_BH);
401}
402
403static _INLINE_ void receive_chars(struct dec_serial *info,
404				   struct pt_regs *regs)
405{
406	struct tty_struct *tty = info->tty;
407	unsigned char ch, stat, flag;
408
409	while ((read_zsreg(info->zs_channel, R0) & Rx_CH_AV) != 0) {
410
411		stat = read_zsreg(info->zs_channel, R1);
412		ch = read_zsdata(info->zs_channel);
413
414		if (!tty && (!info->hook || !info->hook->rx_char))
415			continue;
416
417		if (tty_break) {
418			tty_break = 0;
419#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
420			if (info->line == sercons.index) {
421				if (!break_pressed) {
422					break_pressed = jiffies;
423					goto ignore_char;
424				}
425				break_pressed = 0;
426			}
427#endif
428			flag = TTY_BREAK;
429			if (info->flags & ZILOG_SAK)
430				do_SAK(tty);
431		} else {
432			if (stat & Rx_OVR) {
433				flag = TTY_OVERRUN;
434			} else if (stat & FRM_ERR) {
435				flag = TTY_FRAME;
436			} else if (stat & PAR_ERR) {
437				flag = TTY_PARITY;
438			} else
439				flag = 0;
440			if (flag)
441				/* reset the error indication */
442				write_zsreg(info->zs_channel, R0, ERR_RES);
443		}
444
445#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
446		if (break_pressed && info->line == sercons.index) {
447			if (ch != 0 &&
448			    time_before(jiffies, break_pressed + HZ*5)) {
449				handle_sysrq(ch, regs, NULL, NULL);
450				break_pressed = 0;
451				goto ignore_char;
452			}
453			break_pressed = 0;
454		}
455#endif
456
457		if (info->hook && info->hook->rx_char) {
458			(*info->hook->rx_char)(ch, flag);
459			return;
460  		}
461
462		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
463			static int flip_buf_ovf;
464			++flip_buf_ovf;
465			continue;
466		}
467		tty->flip.count++;
468		{
469			static int flip_max_cnt;
470			if (flip_max_cnt < tty->flip.count)
471				flip_max_cnt = tty->flip.count;
472		}
473
474		*tty->flip.flag_buf_ptr++ = flag;
475		*tty->flip.char_buf_ptr++ = ch;
476#if defined(CONFIG_SERIAL_DEC_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) && !defined(MODULE)
477	ignore_char:
478#endif
479	}
480	if (tty)
481		tty_flip_buffer_push(tty);
482}
483
484static void transmit_chars(struct dec_serial *info)
485{
486	if ((read_zsreg(info->zs_channel, R0) & Tx_BUF_EMP) == 0)
487		return;
488	info->tx_active = 0;
489
490	if (info->x_char) {
491		/* Send next char */
492		write_zsdata(info->zs_channel, info->x_char);
493		info->x_char = 0;
494		info->tx_active = 1;
495		return;
496	}
497
498	if ((info->xmit_cnt <= 0) || (info->tty && info->tty->stopped)
499	    || info->tx_stopped) {
500		write_zsreg(info->zs_channel, R0, RES_Tx_P);
501		return;
502	}
503	/* Send char */
504	write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]);
505	info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
506	info->xmit_cnt--;
507	info->tx_active = 1;
508
509	if (info->xmit_cnt < WAKEUP_CHARS)
510		rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511}
512
513static _INLINE_ void status_handle(struct dec_serial *info)
514{
515	unsigned char stat;
516
517	/* Get status from Read Register 0 */
518	stat = read_zsreg(info->zs_channel, R0);
519
520	if (stat & BRK_ABRT) {
521#ifdef SERIAL_DEBUG_INTR
522		printk("handling break....");
523#endif
524		tty_break = 1;
525	}
526
527	if (info->zs_channel != info->zs_chan_a) {
528
529		/* Check for DCD transitions */
530		if (info->tty && !C_CLOCAL(info->tty) &&
531		    ((stat ^ info->read_reg_zero) & DCD) != 0 ) {
532			if (stat & DCD) {
533				wake_up_interruptible(&info->open_wait);
534			} else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) {
535				tty_hangup(info->tty);
536			}
537		}
538
539		/* Check for CTS transitions */
540		if (info->tty && C_CRTSCTS(info->tty)) {
541			if ((stat & CTS) != 0) {
542				if (info->tx_stopped) {
543					info->tx_stopped = 0;
544					if (!info->tx_active)
545						transmit_chars(info);
546				}
547			} else {
548				info->tx_stopped = 1;
549			}
550		}
551
552	}
553
554	/* Clear status condition... */
555	write_zsreg(info->zs_channel, R0, RES_EXT_INT);
556	info->read_reg_zero = stat;
557}
558
559/*
560 * This is the serial driver's generic interrupt routine
561 */
562void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
563{
564	struct dec_serial *info = (struct dec_serial *) dev_id;
565	unsigned char zs_intreg;
566	int shift;
567
568	/* NOTE: The read register 3, which holds the irq status,
569	 *       does so for both channels on each chip.  Although
570	 *       the status value itself must be read from the A
571	 *       channel and is only valid when read from channel A.
572	 *       Yes... broken hardware...
573	 */
574#define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT)
575
576	if (info->zs_chan_a == info->zs_channel)
577		shift = 3;	/* Channel A */
578	else
579		shift = 0;	/* Channel B */
580
581	for (;;) {
582		zs_intreg = read_zsreg(info->zs_chan_a, R3) >> shift;
583		if ((zs_intreg & CHAN_IRQMASK) == 0)
584			break;
585
586		if (zs_intreg & CHBRxIP) {
587			receive_chars(info, regs);
588		}
589		if (zs_intreg & CHBTxIP) {
590			transmit_chars(info);
591		}
592		if (zs_intreg & CHBEXT) {
593			status_handle(info);
594		}
595	}
596
597	/* Why do we need this ? */
598	write_zsreg(info->zs_channel, 0, RES_H_IUS);
599}
600
601#ifdef ZS_DEBUG_REGS
602void zs_dump (void) {
603	int i, j;
604	for (i = 0; i < zs_channels_found; i++) {
605		struct dec_zschannel *ch = &zs_channels[i];
606		if ((long)ch->control == UNI_IO_BASE+UNI_SCC1A_CTRL) {
607			for (j = 0; j < 15; j++) {
608				printk("W%d = 0x%x\t",
609				       j, (int)ch->curregs[j]);
610			}
611			for (j = 0; j < 15; j++) {
612				printk("R%d = 0x%x\t",
613				       j, (int)read_zsreg(ch,j));
614			}
615			printk("\n\n");
616		}
617	}
618}
619#endif
620
621/*
622 * -------------------------------------------------------------------
623 * Here ends the serial interrupt routines.
624 * -------------------------------------------------------------------
625 */
626
627/*
628 * ------------------------------------------------------------
629 * rs_stop() and rs_start()
630 *
631 * This routines are called before setting or resetting tty->stopped.
632 * ------------------------------------------------------------
633 */
634static void rs_stop(struct tty_struct *tty)
635{
636	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
637	unsigned long flags;
638
639	if (serial_paranoia_check(info, tty->device, "rs_stop"))
640		return;
641
642	save_flags(flags); cli();
643	if (info->zs_channel->curregs[5] & TxENAB) {
644		info->zs_channel->curregs[5] &= ~TxENAB;
645		write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
646	}
647	restore_flags(flags);
648}
649
650static void rs_start(struct tty_struct *tty)
651{
652	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
653	unsigned long flags;
654
655	if (serial_paranoia_check(info, tty->device, "rs_start"))
656		return;
657
658	save_flags(flags); cli();
659	if (info->xmit_cnt && info->xmit_buf && !(info->zs_channel->curregs[5] & TxENAB)) {
660		info->zs_channel->curregs[5] |= TxENAB;
661		write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
662	}
663	restore_flags(flags);
664}
665
666/*
667 * This routine is used to handle the "bottom half" processing for the
668 * serial driver, known also the "software interrupt" processing.
669 * This processing is done at the kernel interrupt level, after the
670 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
671 * is where time-consuming activities which can not be done in the
672 * interrupt driver proper are done; the interrupt driver schedules
673 * them using rs_sched_event(), and they get done here.
674 */
675static void do_serial_bh(void)
676{
677	run_task_queue(&tq_zs_serial);
678}
679
680static void do_softint(void *private_)
681{
682	struct dec_serial	*info = (struct dec_serial *) private_;
683	struct tty_struct	*tty;
684
685	tty = info->tty;
686	if (!tty)
687		return;
688
689	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
690		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
691		    tty->ldisc.write_wakeup)
692			(tty->ldisc.write_wakeup)(tty);
693		wake_up_interruptible(&tty->write_wait);
694	}
695}
696
697int zs_startup(struct dec_serial * info)
698{
699	unsigned long flags;
700
701	if (info->flags & ZILOG_INITIALIZED)
702		return 0;
703
704	if (!info->xmit_buf) {
705		info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL);
706		if (!info->xmit_buf)
707			return -ENOMEM;
708	}
709
710	save_flags(flags); cli();
711
712#ifdef SERIAL_DEBUG_OPEN
713	printk("starting up ttyS%02d (irq %d)...", info->line, info->irq);
714#endif
715
716	/*
717	 * Clear the receive FIFO.
718	 */
719	ZS_CLEARFIFO(info->zs_channel);
720	info->xmit_fifo_size = 1;
721
722	/*
723	 * Clear the interrupt registers.
724	 */
725	write_zsreg(info->zs_channel, 0, ERR_RES);
726	write_zsreg(info->zs_channel, 0, RES_H_IUS);
727
728	/*
729	 * Turn on RTS and DTR.
730	 */
731	zs_rtsdtr(info, RTS | DTR, 1);
732
733	/*
734	 * Finally, enable sequencing and interrupts
735	 */
736	info->zs_channel->curregs[1] = (info->zs_channel->curregs[1] & ~0x18) | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB);
737	info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
738	info->zs_channel->curregs[5] |= (TxENAB | Tx8);
739	info->zs_channel->curregs[15] |= (DCDIE | CTSIE | TxUIE | BRKIE);
740	info->zs_channel->curregs[9] |= (VIS | MIE);
741	write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
742	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
743	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
744	write_zsreg(info->zs_channel, 15, info->zs_channel->curregs[15]);
745	write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
746
747	/*
748	 * And clear the interrupt registers again for luck.
749	 */
750	write_zsreg(info->zs_channel, 0, ERR_RES);
751	write_zsreg(info->zs_channel, 0, RES_H_IUS);
752
753	if (info->tty)
754		clear_bit(TTY_IO_ERROR, &info->tty->flags);
755	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
756
757	/*
758	 * Set the speed of the serial port
759	 */
760	change_speed(info);
761
762	/* Save the current value of RR0 */
763	info->read_reg_zero = read_zsreg(info->zs_channel, 0);
764
765	info->flags |= ZILOG_INITIALIZED;
766	restore_flags(flags);
767	return 0;
768}
769
770/*
771 * This routine will shutdown a serial port; interrupts are disabled, and
772 * DTR is dropped if the hangup on close termio flag is on.
773 */
774static void shutdown(struct dec_serial * info)
775{
776	unsigned long	flags;
777
778	if (!(info->flags & ZILOG_INITIALIZED))
779		return;
780
781#ifdef SERIAL_DEBUG_OPEN
782	printk("Shutting down serial port %d (irq %d)....", info->line,
783	       info->irq);
784#endif
785
786	save_flags(flags); cli(); /* Disable interrupts */
787
788	if (info->xmit_buf) {
789		free_page((unsigned long) info->xmit_buf);
790		info->xmit_buf = 0;
791	}
792
793	info->zs_channel->curregs[1] = 0;
794	write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);	/* no interrupts */
795
796	info->zs_channel->curregs[3] &= ~RxENABLE;
797	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
798
799	info->zs_channel->curregs[5] &= ~TxENAB;
800	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
801	if (!info->tty || C_HUPCL(info->tty)) {
802		zs_rtsdtr(info, RTS | DTR, 0);
803	}
804
805	if (info->tty)
806		set_bit(TTY_IO_ERROR, &info->tty->flags);
807
808	info->flags &= ~ZILOG_INITIALIZED;
809	restore_flags(flags);
810}
811
812/*
813 * This routine is called to set the UART divisor registers to match
814 * the specified baud rate for a serial port.
815 */
816static void change_speed(struct dec_serial *info)
817{
818	unsigned cflag;
819	int	i;
820	int	brg, bits;
821	unsigned long flags;
822
823	if (!info->hook) {
824		if (!info->tty || !info->tty->termios)
825			return;
826		cflag = info->tty->termios->c_cflag;
827		if (!info->port)
828			return;
829	} else {
830		cflag = info->hook->cflags;
831	}
832
833	i = cflag & CBAUD;
834	if (i & CBAUDEX) {
835		i &= ~CBAUDEX;
836		if (i < 1 || i > 2) {
837			if (!info->hook)
838				info->tty->termios->c_cflag &= ~CBAUDEX;
839			else
840				info->hook->cflags &= ~CBAUDEX;
841		} else
842			i += 15;
843	}
844
845	save_flags(flags); cli();
846	info->zs_baud = baud_table[i];
847	info->clk_divisor = 16;
848	if (info->zs_baud) {
849		info->zs_channel->curregs[4] = X16CLK;
850		brg = BPS_TO_BRG(info->zs_baud, zs_parms->clock/info->clk_divisor);
851		info->zs_channel->curregs[12] = (brg & 255);
852		info->zs_channel->curregs[13] = ((brg >> 8) & 255);
853		zs_rtsdtr(info, DTR, 1);
854	} else {
855		zs_rtsdtr(info, RTS | DTR, 0);
856		return;
857	}
858
859	/* byte size and parity */
860	info->zs_channel->curregs[3] &= ~RxNBITS_MASK;
861	info->zs_channel->curregs[5] &= ~TxNBITS_MASK;
862	switch (cflag & CSIZE) {
863	case CS5:
864		bits = 7;
865		info->zs_channel->curregs[3] |= Rx5;
866		info->zs_channel->curregs[5] |= Tx5;
867		break;
868	case CS6:
869		bits = 8;
870		info->zs_channel->curregs[3] |= Rx6;
871		info->zs_channel->curregs[5] |= Tx6;
872		break;
873	case CS7:
874		bits = 9;
875		info->zs_channel->curregs[3] |= Rx7;
876		info->zs_channel->curregs[5] |= Tx7;
877		break;
878	case CS8:
879	default: /* defaults to 8 bits */
880		bits = 10;
881		info->zs_channel->curregs[3] |= Rx8;
882		info->zs_channel->curregs[5] |= Tx8;
883		break;
884	}
885
886	info->timeout = ((info->xmit_fifo_size*HZ*bits) / info->zs_baud);
887        info->timeout += HZ/50;         /* Add .02 seconds of slop */
888
889	info->zs_channel->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN);
890	if (cflag & CSTOPB) {
891		info->zs_channel->curregs[4] |= SB2;
892	} else {
893		info->zs_channel->curregs[4] |= SB1;
894	}
895	if (cflag & PARENB) {
896		info->zs_channel->curregs[4] |= PAR_ENA;
897	}
898	if (!(cflag & PARODD)) {
899		info->zs_channel->curregs[4] |= PAR_EVEN;
900	}
901
902	if (!(cflag & CLOCAL)) {
903		if (!(info->zs_channel->curregs[15] & DCDIE))
904			info->read_reg_zero = read_zsreg(info->zs_channel, 0);
905		info->zs_channel->curregs[15] |= DCDIE;
906	} else
907		info->zs_channel->curregs[15] &= ~DCDIE;
908	if (cflag & CRTSCTS) {
909		info->zs_channel->curregs[15] |= CTSIE;
910		if ((read_zsreg(info->zs_channel, 0) & CTS) == 0)
911			info->tx_stopped = 1;
912	} else {
913		info->zs_channel->curregs[15] &= ~CTSIE;
914		info->tx_stopped = 0;
915	}
916
917	/* Load up the new values */
918	load_zsregs(info->zs_channel, info->zs_channel->curregs);
919
920	restore_flags(flags);
921}
922
923static void rs_flush_chars(struct tty_struct *tty)
924{
925	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
926	unsigned long flags;
927
928	if (serial_paranoia_check(info, tty->device, "rs_flush_chars"))
929		return;
930
931	if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped ||
932	    !info->xmit_buf)
933		return;
934
935	/* Enable transmitter */
936	save_flags(flags); cli();
937	transmit_chars(info);
938	restore_flags(flags);
939}
940
941static int rs_write(struct tty_struct * tty, int from_user,
942		    const unsigned char *buf, int count)
943{
944	int	c, total = 0;
945	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
946	unsigned long flags;
947
948	if (serial_paranoia_check(info, tty->device, "rs_write"))
949		return 0;
950
951	if (!tty || !info->xmit_buf)
952		return 0;
953
954	save_flags(flags);
955	while (1) {
956		cli();
957		c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
958				   SERIAL_XMIT_SIZE - info->xmit_head));
959		if (c <= 0)
960			break;
961
962		if (from_user) {
963			down(&tmp_buf_sem);
964			copy_from_user(tmp_buf, buf, c);
965			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
966				       SERIAL_XMIT_SIZE - info->xmit_head));
967			memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
968			up(&tmp_buf_sem);
969		} else
970			memcpy(info->xmit_buf + info->xmit_head, buf, c);
971		info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
972		info->xmit_cnt += c;
973		restore_flags(flags);
974		buf += c;
975		count -= c;
976		total += c;
977	}
978
979	if (info->xmit_cnt && !tty->stopped && !info->tx_stopped
980	    && !info->tx_active)
981		transmit_chars(info);
982	restore_flags(flags);
983	return total;
984}
985
986static int rs_write_room(struct tty_struct *tty)
987{
988	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
989	int	ret;
990
991	if (serial_paranoia_check(info, tty->device, "rs_write_room"))
992		return 0;
993	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
994	if (ret < 0)
995		ret = 0;
996	return ret;
997}
998
999static int rs_chars_in_buffer(struct tty_struct *tty)
1000{
1001	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1002
1003	if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1004		return 0;
1005	return info->xmit_cnt;
1006}
1007
1008static void rs_flush_buffer(struct tty_struct *tty)
1009{
1010	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1011
1012	if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1013		return;
1014	cli();
1015	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1016	sti();
1017	wake_up_interruptible(&tty->write_wait);
1018	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1019	    tty->ldisc.write_wakeup)
1020		(tty->ldisc.write_wakeup)(tty);
1021}
1022
1023/*
1024 * ------------------------------------------------------------
1025 * rs_throttle()
1026 *
1027 * This routine is called by the upper-layer tty layer to signal that
1028 * incoming characters should be throttled.
1029 * ------------------------------------------------------------
1030 */
1031static void rs_throttle(struct tty_struct * tty)
1032{
1033	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1034	unsigned long flags;
1035
1036#ifdef SERIAL_DEBUG_THROTTLE
1037	char	buf[64];
1038
1039	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1040	       tty->ldisc.chars_in_buffer(tty));
1041#endif
1042
1043	if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1044		return;
1045
1046	if (I_IXOFF(tty)) {
1047		save_flags(flags); cli();
1048		info->x_char = STOP_CHAR(tty);
1049		if (!info->tx_active)
1050			transmit_chars(info);
1051		restore_flags(flags);
1052	}
1053
1054	if (C_CRTSCTS(tty)) {
1055		zs_rtsdtr(info, RTS, 0);
1056	}
1057}
1058
1059static void rs_unthrottle(struct tty_struct * tty)
1060{
1061	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1062	unsigned long flags;
1063
1064#ifdef SERIAL_DEBUG_THROTTLE
1065	char	buf[64];
1066
1067	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1068	       tty->ldisc.chars_in_buffer(tty));
1069#endif
1070
1071	if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1072		return;
1073
1074	if (I_IXOFF(tty)) {
1075		save_flags(flags); cli();
1076		if (info->x_char)
1077			info->x_char = 0;
1078		else {
1079			info->x_char = START_CHAR(tty);
1080			if (!info->tx_active)
1081				transmit_chars(info);
1082		}
1083		restore_flags(flags);
1084	}
1085
1086	if (C_CRTSCTS(tty)) {
1087		zs_rtsdtr(info, RTS, 1);
1088	}
1089}
1090
1091/*
1092 * ------------------------------------------------------------
1093 * rs_ioctl() and friends
1094 * ------------------------------------------------------------
1095 */
1096
1097static int get_serial_info(struct dec_serial * info,
1098			   struct serial_struct * retinfo)
1099{
1100	struct serial_struct tmp;
1101
1102	if (!retinfo)
1103		return -EFAULT;
1104	memset(&tmp, 0, sizeof(tmp));
1105	tmp.type = info->type;
1106	tmp.line = info->line;
1107	tmp.port = info->port;
1108	tmp.irq = info->irq;
1109	tmp.flags = info->flags;
1110	tmp.baud_base = info->baud_base;
1111	tmp.close_delay = info->close_delay;
1112	tmp.closing_wait = info->closing_wait;
1113	tmp.custom_divisor = info->custom_divisor;
1114	return copy_to_user(retinfo,&tmp,sizeof(*retinfo));
1115}
1116
1117static int set_serial_info(struct dec_serial * info,
1118			   struct serial_struct * new_info)
1119{
1120	struct serial_struct new_serial;
1121	struct dec_serial old_info;
1122	int 			retval = 0;
1123
1124	if (!new_info)
1125		return -EFAULT;
1126	copy_from_user(&new_serial,new_info,sizeof(new_serial));
1127	old_info = *info;
1128
1129	if (!capable(CAP_SYS_ADMIN)) {
1130		if ((new_serial.baud_base != info->baud_base) ||
1131		    (new_serial.type != info->type) ||
1132		    (new_serial.close_delay != info->close_delay) ||
1133		    ((new_serial.flags & ~ZILOG_USR_MASK) !=
1134		     (info->flags & ~ZILOG_USR_MASK)))
1135			return -EPERM;
1136		info->flags = ((info->flags & ~ZILOG_USR_MASK) |
1137			       (new_serial.flags & ZILOG_USR_MASK));
1138		info->custom_divisor = new_serial.custom_divisor;
1139		goto check_and_exit;
1140	}
1141
1142	if (info->count > 1)
1143		return -EBUSY;
1144
1145	/*
1146	 * OK, past this point, all the error checking has been done.
1147	 * At this point, we start making changes.....
1148	 */
1149
1150	info->baud_base = new_serial.baud_base;
1151	info->flags = ((info->flags & ~ZILOG_FLAGS) |
1152			(new_serial.flags & ZILOG_FLAGS));
1153	info->type = new_serial.type;
1154	info->close_delay = new_serial.close_delay;
1155	info->closing_wait = new_serial.closing_wait;
1156
1157check_and_exit:
1158	retval = zs_startup(info);
1159	return retval;
1160}
1161
1162/*
1163 * get_lsr_info - get line status register info
1164 *
1165 * Purpose: Let user call ioctl() to get info when the UART physically
1166 * 	    is emptied.  On bus types like RS485, the transmitter must
1167 * 	    release the bus after transmitting. This must be done when
1168 * 	    the transmit shift register is empty, not be done when the
1169 * 	    transmit holding register is empty.  This functionality
1170 * 	    allows an RS485 driver to be written in user space.
1171 */
1172static int get_lsr_info(struct dec_serial * info, unsigned int *value)
1173{
1174	unsigned char status;
1175
1176	cli();
1177	status = read_zsreg(info->zs_channel, 0);
1178	sti();
1179	put_user(status,value);
1180	return 0;
1181}
1182
1183static int get_modem_info(struct dec_serial *info, unsigned int *value)
1184{
1185	unsigned char control, status_a, status_b;
1186	unsigned int result;
1187
1188	if (info->zs_channel == info->zs_chan_a)
1189		result = 0;
1190	else {
1191		cli();
1192		control = info->zs_chan_a->curregs[5];
1193		status_a = read_zsreg(info->zs_chan_a, 0);
1194		status_b = read_zsreg(info->zs_channel, 0);
1195		sti();
1196		result =  ((control  & RTS) ? TIOCM_RTS: 0)
1197			| ((control  & DTR) ? TIOCM_DTR: 0)
1198			| ((status_b & DCD) ? TIOCM_CAR: 0)
1199			| ((status_a & DCD) ? TIOCM_RNG: 0)
1200			| ((status_a & SYNC_HUNT) ? TIOCM_DSR: 0)
1201			| ((status_b & CTS) ? TIOCM_CTS: 0);
1202	}
1203	put_user(result, value);
1204	return 0;
1205}
1206
1207static int set_modem_info(struct dec_serial *info, unsigned int cmd,
1208			  unsigned int *value)
1209{
1210	int error;
1211	unsigned int arg, bits;
1212
1213	error = verify_area(VERIFY_READ, value, sizeof(int));
1214	if (error)
1215		return error;
1216
1217	if (info->zs_channel == info->zs_chan_a)
1218		return 0;
1219
1220	get_user(arg, value);
1221	bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0);
1222	cli();
1223	switch (cmd) {
1224	case TIOCMBIS:
1225		info->zs_chan_a->curregs[5] |= bits;
1226		break;
1227	case TIOCMBIC:
1228		info->zs_chan_a->curregs[5] &= ~bits;
1229		break;
1230	case TIOCMSET:
1231		info->zs_chan_a->curregs[5] =
1232			(info->zs_chan_a->curregs[5] & ~(DTR | RTS)) | bits;
1233		break;
1234	default:
1235		sti();
1236		return -EINVAL;
1237	}
1238	write_zsreg(info->zs_chan_a, 5, info->zs_chan_a->curregs[5]);
1239	sti();
1240	return 0;
1241}
1242
1243/*
1244 * rs_break - turn transmit break condition on/off
1245 */
1246static void rs_break(struct tty_struct *tty, int break_state)
1247{
1248	struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1249	unsigned long flags;
1250
1251	if (serial_paranoia_check(info, tty->device, "rs_break"))
1252		return;
1253	if (!info->port)
1254		return;
1255
1256	save_flags(flags); cli();
1257	if (break_state == -1)
1258		info->zs_channel->curregs[5] |= SND_BRK;
1259	else
1260		info->zs_channel->curregs[5] &= ~SND_BRK;
1261	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
1262	restore_flags(flags);
1263}
1264
1265static int rs_ioctl(struct tty_struct *tty, struct file * file,
1266		    unsigned int cmd, unsigned long arg)
1267{
1268	int error;
1269	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1270
1271	if (info->hook)
1272		return -ENODEV;
1273
1274	if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1275		return -ENODEV;
1276
1277	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1278	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1279	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1280		if (tty->flags & (1 << TTY_IO_ERROR))
1281		    return -EIO;
1282	}
1283
1284	switch (cmd) {
1285		case TIOCMGET:
1286			error = verify_area(VERIFY_WRITE, (void *) arg,
1287				sizeof(unsigned int));
1288			if (error)
1289				return error;
1290			return get_modem_info(info, (unsigned int *) arg);
1291		case TIOCMBIS:
1292		case TIOCMBIC:
1293		case TIOCMSET:
1294			return set_modem_info(info, cmd, (unsigned int *) arg);
1295		case TIOCGSERIAL:
1296			error = verify_area(VERIFY_WRITE, (void *) arg,
1297						sizeof(struct serial_struct));
1298			if (error)
1299				return error;
1300			return get_serial_info(info,
1301					       (struct serial_struct *) arg);
1302		case TIOCSSERIAL:
1303			return set_serial_info(info,
1304					       (struct serial_struct *) arg);
1305		case TIOCSERGETLSR: /* Get line status register */
1306			error = verify_area(VERIFY_WRITE, (void *) arg,
1307				sizeof(unsigned int));
1308			if (error)
1309				return error;
1310			else
1311			    return get_lsr_info(info, (unsigned int *) arg);
1312
1313		case TIOCSERGSTRUCT:
1314			error = verify_area(VERIFY_WRITE, (void *) arg,
1315						sizeof(struct dec_serial));
1316			if (error)
1317				return error;
1318			copy_from_user((struct dec_serial *) arg,
1319				       info, sizeof(struct dec_serial));
1320			return 0;
1321
1322		default:
1323			return -ENOIOCTLCMD;
1324		}
1325	return 0;
1326}
1327
1328static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1329{
1330	struct dec_serial *info = (struct dec_serial *)tty->driver_data;
1331	int was_stopped;
1332
1333	if (tty->termios->c_cflag == old_termios->c_cflag)
1334		return;
1335	was_stopped = info->tx_stopped;
1336
1337	change_speed(info);
1338
1339	if (was_stopped && !info->tx_stopped)
1340		rs_start(tty);
1341}
1342
1343/*
1344 * ------------------------------------------------------------
1345 * rs_close()
1346 *
1347 * This routine is called when the serial port gets closed.
1348 * Wait for the last remaining data to be sent.
1349 * ------------------------------------------------------------
1350 */
1351static void rs_close(struct tty_struct *tty, struct file * filp)
1352{
1353	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1354	unsigned long flags;
1355
1356	if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1357		return;
1358
1359	save_flags(flags); cli();
1360
1361	if (tty_hung_up_p(filp)) {
1362		restore_flags(flags);
1363		return;
1364	}
1365
1366#ifdef SERIAL_DEBUG_OPEN
1367	printk("rs_close ttyS%02d, count = %d\n", info->line, info->count);
1368#endif
1369	if ((tty->count == 1) && (info->count != 1)) {
1370		/*
1371		 * Uh, oh.  tty->count is 1, which means that the tty
1372		 * structure will be freed.  Info->count should always
1373		 * be one in these conditions.  If it's greater than
1374		 * one, we've got real problems, since it means the
1375		 * serial port won't be shutdown.
1376		 */
1377		printk("rs_close: bad serial port count; tty->count is 1, "
1378		       "info->count is %d\n", info->count);
1379		info->count = 1;
1380	}
1381	if (--info->count < 0) {
1382		printk("rs_close: bad serial port count for ttyS%02d: %d\n",
1383		       info->line, info->count);
1384		info->count = 0;
1385	}
1386	if (info->count) {
1387		restore_flags(flags);
1388		return;
1389	}
1390	info->flags |= ZILOG_CLOSING;
1391	/*
1392	 * Save the termios structure, since this port may have
1393	 * separate termios for callout and dialin.
1394	 */
1395	if (info->flags & ZILOG_NORMAL_ACTIVE)
1396		info->normal_termios = *tty->termios;
1397	if (info->flags & ZILOG_CALLOUT_ACTIVE)
1398		info->callout_termios = *tty->termios;
1399	/*
1400	 * Now we wait for the transmit buffer to clear; and we notify
1401	 * the line discipline to only process XON/XOFF characters.
1402	 */
1403	tty->closing = 1;
1404	if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE)
1405		tty_wait_until_sent(tty, info->closing_wait);
1406	/*
1407	 * At this point we stop accepting input.  To do this, we
1408	 * disable the receiver and receive interrupts.
1409	 */
1410	info->zs_channel->curregs[3] &= ~RxENABLE;
1411	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
1412	info->zs_channel->curregs[1] = 0;	/* disable any rx ints */
1413	write_zsreg(info->zs_channel, 1, info->zs_channel->curregs[1]);
1414	ZS_CLEARFIFO(info->zs_channel);
1415	if (info->flags & ZILOG_INITIALIZED) {
1416		/*
1417		 * Before we drop DTR, make sure the SCC transmitter
1418		 * has completely drained.
1419		 */
1420		rs_wait_until_sent(tty, info->timeout);
1421	}
1422
1423	shutdown(info);
1424	if (tty->driver.flush_buffer)
1425		tty->driver.flush_buffer(tty);
1426	if (tty->ldisc.flush_buffer)
1427		tty->ldisc.flush_buffer(tty);
1428	tty->closing = 0;
1429	info->event = 0;
1430	info->tty = 0;
1431	if (info->blocked_open) {
1432		if (info->close_delay) {
1433			current->state = TASK_INTERRUPTIBLE;
1434			schedule_timeout(info->close_delay);
1435		}
1436		wake_up_interruptible(&info->open_wait);
1437	}
1438	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE|
1439			 ZILOG_CLOSING);
1440	wake_up_interruptible(&info->close_wait);
1441	restore_flags(flags);
1442}
1443
1444/*
1445 * rs_wait_until_sent() --- wait until the transmitter is empty
1446 */
1447static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
1448{
1449	struct dec_serial *info = (struct dec_serial *) tty->driver_data;
1450	unsigned long orig_jiffies, char_time;
1451
1452	if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1453		return;
1454
1455	orig_jiffies = jiffies;
1456	/*
1457	 * Set the check interval to be 1/5 of the estimated time to
1458	 * send a single character, and make it at least 1.  The check
1459	 * interval should also be less than the timeout.
1460	 */
1461	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1462	char_time = char_time / 5;
1463	if (char_time == 0)
1464		char_time = 1;
1465	if (timeout)
1466		char_time = MIN(char_time, timeout);
1467	while ((read_zsreg(info->zs_channel, 1) & Tx_BUF_EMP) == 0) {
1468		current->state = TASK_INTERRUPTIBLE;
1469		schedule_timeout(char_time);
1470		if (signal_pending(current))
1471			break;
1472		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1473			break;
1474	}
1475	current->state = TASK_RUNNING;
1476}
1477
1478/*
1479 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1480 */
1481void rs_hangup(struct tty_struct *tty)
1482{
1483	struct dec_serial * info = (struct dec_serial *)tty->driver_data;
1484
1485	if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1486		return;
1487
1488	rs_flush_buffer(tty);
1489	shutdown(info);
1490	info->event = 0;
1491	info->count = 0;
1492	info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE);
1493	info->tty = 0;
1494	wake_up_interruptible(&info->open_wait);
1495}
1496
1497/*
1498 * ------------------------------------------------------------
1499 * rs_open() and friends
1500 * ------------------------------------------------------------
1501 */
1502static int block_til_ready(struct tty_struct *tty, struct file * filp,
1503			   struct dec_serial *info)
1504{
1505	DECLARE_WAITQUEUE(wait, current);
1506	int		retval;
1507	int		do_clocal = 0;
1508
1509	/*
1510	 * If the device is in the middle of being closed, then block
1511	 * until it's done, and then try again.
1512	 */
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
1521	}
1522
1523	/*
1524	 * If this is a callout device, then just make sure the normal
1525	 * device isn't being used.
1526	 */
1527	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1528		if (info->flags & ZILOG_NORMAL_ACTIVE)
1529			return -EBUSY;
1530		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1531		    (info->flags & ZILOG_SESSION_LOCKOUT) &&
1532		    (info->session != current->session))
1533		    return -EBUSY;
1534		if ((info->flags & ZILOG_CALLOUT_ACTIVE) &&
1535		    (info->flags & ZILOG_PGRP_LOCKOUT) &&
1536		    (info->pgrp != current->pgrp))
1537		    return -EBUSY;
1538		info->flags |= ZILOG_CALLOUT_ACTIVE;
1539		return 0;
1540	}
1541
1542	/*
1543	 * If non-blocking mode is set, or the port is not enabled,
1544	 * then make the check up front and then exit.
1545	 */
1546	if ((filp->f_flags & O_NONBLOCK) ||
1547	    (tty->flags & (1 << TTY_IO_ERROR))) {
1548		if (info->flags & ZILOG_CALLOUT_ACTIVE)
1549			return -EBUSY;
1550		info->flags |= ZILOG_NORMAL_ACTIVE;
1551		return 0;
1552	}
1553
1554	if (info->flags & ZILOG_CALLOUT_ACTIVE) {
1555		if (info->normal_termios.c_cflag & CLOCAL)
1556			do_clocal = 1;
1557	} else {
1558		if (tty->termios->c_cflag & CLOCAL)
1559			do_clocal = 1;
1560	}
1561
1562	/*
1563	 * Block waiting for the carrier detect and the line to become
1564	 * free (i.e., not in use by the callout).  While we are in
1565	 * this loop, info->count is dropped by one, so that
1566	 * rs_close() knows when to free things.  We restore it upon
1567	 * exit, either normal or abnormal.
1568	 */
1569	retval = 0;
1570	add_wait_queue(&info->open_wait, &wait);
1571#ifdef SERIAL_DEBUG_OPEN
1572	printk("block_til_ready before block: ttyS%02d, count = %d\n",
1573	       info->line, info->count);
1574#endif
1575	cli();
1576	if (!tty_hung_up_p(filp))
1577		info->count--;
1578	sti();
1579	info->blocked_open++;
1580	while (1) {
1581		cli();
1582		if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1583		    (tty->termios->c_cflag & CBAUD))
1584			zs_rtsdtr(info, RTS | DTR, 1);
1585		sti();
1586		set_current_state(TASK_INTERRUPTIBLE);
1587		if (tty_hung_up_p(filp) ||
1588		    !(info->flags & ZILOG_INITIALIZED)) {
1589#ifdef SERIAL_DO_RESTART
1590			if (info->flags & ZILOG_HUP_NOTIFY)
1591				retval = -EAGAIN;
1592			else
1593				retval = -ERESTARTSYS;
1594#else
1595			retval = -EAGAIN;
1596#endif
1597			break;
1598		}
1599		if (!(info->flags & ZILOG_CALLOUT_ACTIVE) &&
1600		    !(info->flags & ZILOG_CLOSING) &&
1601		    (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD)))
1602			break;
1603		if (signal_pending(current)) {
1604			retval = -ERESTARTSYS;
1605			break;
1606		}
1607#ifdef SERIAL_DEBUG_OPEN
1608		printk("block_til_ready blocking: ttyS%02d, count = %d\n",
1609		       info->line, info->count);
1610#endif
1611		schedule();
1612	}
1613	current->state = TASK_RUNNING;
1614	remove_wait_queue(&info->open_wait, &wait);
1615	if (!tty_hung_up_p(filp))
1616		info->count++;
1617	info->blocked_open--;
1618#ifdef SERIAL_DEBUG_OPEN
1619	printk("block_til_ready after blocking: ttyS%02d, count = %d\n",
1620	       info->line, info->count);
1621#endif
1622	if (retval)
1623		return retval;
1624	info->flags |= ZILOG_NORMAL_ACTIVE;
1625	return 0;
1626}
1627
1628/*
1629 * This routine is called whenever a serial port is opened.  It
1630 * enables interrupts for a serial port, linking in its ZILOG structure into
1631 * the IRQ chain.   It also performs the serial-specific
1632 * initialization for the tty structure.
1633 */
1634int rs_open(struct tty_struct *tty, struct file * filp)
1635{
1636	struct dec_serial	*info;
1637	int 			retval, line;
1638
1639	line = MINOR(tty->device) - tty->driver.minor_start;
1640	if ((line < 0) || (line >= zs_channels_found))
1641		return -ENODEV;
1642	info = zs_soft + line;
1643
1644	if (info->hook)
1645		return -ENODEV;
1646
1647	if (serial_paranoia_check(info, tty->device, "rs_open"))
1648		return -ENODEV;
1649#ifdef SERIAL_DEBUG_OPEN
1650	printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
1651	       info->count);
1652#endif
1653
1654	info->count++;
1655	tty->driver_data = info;
1656	info->tty = tty;
1657
1658	/*
1659	 * If the port is the middle of closing, bail out now
1660	 */
1661	if (tty_hung_up_p(filp) ||
1662	    (info->flags & ZILOG_CLOSING)) {
1663		if (info->flags & ZILOG_CLOSING)
1664			interruptible_sleep_on(&info->close_wait);
1665#ifdef SERIAL_DO_RESTART
1666		return ((info->flags & ZILOG_HUP_NOTIFY) ?
1667			-EAGAIN : -ERESTARTSYS);
1668#else
1669		return -EAGAIN;
1670#endif
1671	}
1672
1673	/*
1674	 * Start up serial port
1675	 */
1676	retval = zs_startup(info);
1677	if (retval)
1678		return retval;
1679
1680	retval = block_til_ready(tty, filp, info);
1681	if (retval) {
1682#ifdef SERIAL_DEBUG_OPEN
1683		printk("rs_open returning after block_til_ready with %d\n",
1684		       retval);
1685#endif
1686		return retval;
1687	}
1688
1689	if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) {
1690		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1691			*tty->termios = info->normal_termios;
1692		else
1693			*tty->termios = info->callout_termios;
1694		change_speed(info);
1695	}
1696#ifdef CONFIG_SERIAL_DEC_CONSOLE
1697	if (sercons.cflag && sercons.index == line) {
1698		tty->termios->c_cflag = sercons.cflag;
1699		sercons.cflag = 0;
1700		change_speed(info);
1701	}
1702#endif
1703
1704	info->session = current->session;
1705	info->pgrp = current->pgrp;
1706
1707#ifdef SERIAL_DEBUG_OPEN
1708	printk("rs_open ttyS%02d successful...", info->line);
1709#endif
1710/* tty->low_latency = 1; */
1711	return 0;
1712}
1713
1714/* Finally, routines used to initialize the serial driver. */
1715
1716static void __init show_serial_version(void)
1717{
1718	printk("DECstation Z8530 serial driver version 0.07\n");
1719}
1720
1721/*  Initialize Z8530s zs_channels
1722 */
1723
1724static void __init probe_sccs(void)
1725{
1726	struct dec_serial **pp;
1727	int i, n, n_chips = 0, n_channels, chip, channel;
1728
1729	/*
1730	 * did we get here by accident?
1731	 */
1732	if(!BUS_PRESENT) {
1733		printk("Not on JUNKIO machine, skipping probe_sccs\n");
1734		return;
1735	}
1736
1737	/*
1738	 * When serial console is activated, tc_init has not been called yet
1739	 * and system_base is undefined. Unfortunately we have to hardcode
1740	 * system_base for this case :-(. HK
1741	 */
1742	switch(mips_machtype) {
1743#ifdef CONFIG_DECSTATION
1744	case MACH_DS5000_2X0:
1745	case MACH_DS5900:
1746		system_base = KSEG1ADDR(0x1f800000);
1747		n_chips = 2;
1748		zs_parms = &ds_parms;
1749		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1750		zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1751		break;
1752	case MACH_DS5000_1XX:
1753		system_base = KSEG1ADDR(0x1c000000);
1754		n_chips = 2;
1755		zs_parms = &ds_parms;
1756		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1757		zs_parms->irq1 = dec_interrupt[DEC_IRQ_SCC1];
1758		break;
1759	case MACH_DS5000_XX:
1760		system_base = KSEG1ADDR(0x1c000000);
1761		n_chips = 1;
1762		zs_parms = &ds_parms;
1763		zs_parms->irq0 = dec_interrupt[DEC_IRQ_SCC0];
1764		break;
1765#endif
1766#ifdef CONFIG_BAGET_MIPS
1767	case MACH_BAGET202:
1768		system_base = UNI_IO_BASE;
1769		n_chips = 2;
1770		zs_parms = &baget_parms;
1771		zs_init_regs[2] = 0x8;
1772		break;
1773#endif
1774	default:
1775		panic("zs: unsupported bus");
1776	}
1777	if (!zs_parms)
1778		panic("zs: uninitialized parms");
1779
1780	pp = &zs_chain;
1781
1782	n_channels = 0;
1783
1784	for (chip = 0; chip < n_chips; chip++) {
1785		for (channel = 0; channel <= 1; channel++) {
1786			/*
1787			 * The sccs reside on the high byte of the 16 bit IOBUS
1788			 */
1789			zs_channels[n_channels].control =
1790				(volatile unsigned char *)system_base +
1791			  (0 == chip ? zs_parms->scc0 : zs_parms->scc1) +
1792			  (0 == channel ? zs_parms->channel_a_offset :
1793			                  zs_parms->channel_b_offset);
1794			zs_channels[n_channels].data =
1795				zs_channels[n_channels].control + 4;
1796
1797#ifndef CONFIG_SERIAL_DEC_CONSOLE
1798			/*
1799			 * We're called early and memory managment isn't up, yet.
1800			 * Thus check_region would fail.
1801			 */
1802			if (check_region((unsigned long)
1803					 zs_channels[n_channels].control,
1804					 ZS_CHAN_IO_SIZE) < 0) {
1805				panic("SCC I/O region is not free");
1806			}
1807			request_region((unsigned long)
1808				       zs_channels[n_channels].control,
1809				       ZS_CHAN_IO_SIZE, "SCC");
1810#endif
1811			zs_soft[n_channels].zs_channel = &zs_channels[n_channels];
1812			/* HACK alert! */
1813			if (!(chip & 1))
1814				zs_soft[n_channels].irq = zs_parms->irq0;
1815			else
1816				zs_soft[n_channels].irq = zs_parms->irq1;
1817
1818			/*
1819			 *  Identification of channel A. Location of channel A
1820                         *  inside chip depends on mapping of internal address
1821			 *  the chip decodes channels by.
1822			 *  CHANNEL_A_NR returns either 0 (in case of
1823			 *  DECstations) or 1 (in case of Baget).
1824			 */
1825			if (CHANNEL_A_NR == channel)
1826				zs_soft[n_channels].zs_chan_a =
1827				    &zs_channels[n_channels+1-2*CHANNEL_A_NR];
1828			else
1829				zs_soft[n_channels].zs_chan_a =
1830				    &zs_channels[n_channels];
1831
1832			*pp = &zs_soft[n_channels];
1833			pp = &zs_soft[n_channels].zs_next;
1834			n_channels++;
1835		}
1836	}
1837
1838	*pp = 0;
1839	zs_channels_found = n_channels;
1840
1841	for (n = 0; n < zs_channels_found; n++) {
1842		for (i = 0; i < 16; i++) {
1843			zs_soft[n].zs_channel->curregs[i] = zs_init_regs[i];
1844		}
1845	}
1846
1847/*	save_and_cli(flags);
1848	for (n = 0; n < zs_channels_found; n++) {
1849		if (((int)zs_channels[n].control & 0xf) == 1) {
1850			write_zsreg(zs_soft[n].zs_chan_a, R9, FHWRES);
1851			mdelay(10);
1852			write_zsreg(zs_soft[n].zs_chan_a, R9, 0);
1853		}
1854		load_zsregs(zs_soft[n].zs_channel, zs_soft[n].zs_channel->curregs);
1855	}
1856	restore_flags(flags); */
1857}
1858
1859/* zs_init inits the driver */
1860int __init zs_init(void)
1861{
1862	int channel, i;
1863	unsigned long flags;
1864	struct dec_serial *info;
1865
1866	if(!BUS_PRESENT)
1867		return -ENODEV;
1868
1869	/* Setup base handler, and timer table. */
1870	init_bh(SERIAL_BH, do_serial_bh);
1871
1872	/* Find out how many Z8530 SCCs we have */
1873	if (zs_chain == 0)
1874		probe_sccs();
1875
1876	show_serial_version();
1877
1878	/* Initialize the tty_driver structure */
1879	/* Not all of this is exactly right for us. */
1880
1881	memset(&serial_driver, 0, sizeof(struct tty_driver));
1882	serial_driver.magic = TTY_DRIVER_MAGIC;
1883#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1884	serial_driver.name = "tts/%d";
1885#else
1886	serial_driver.name = "ttyS";
1887#endif
1888	serial_driver.major = TTY_MAJOR;
1889	serial_driver.minor_start = 64;
1890	serial_driver.num = zs_channels_found;
1891	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
1892	serial_driver.subtype = SERIAL_TYPE_NORMAL;
1893	serial_driver.init_termios = tty_std_termios;
1894
1895	serial_driver.init_termios.c_cflag =
1896		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1897	serial_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
1898	serial_driver.refcount = &serial_refcount;
1899	serial_driver.table = serial_table;
1900	serial_driver.termios = serial_termios;
1901	serial_driver.termios_locked = serial_termios_locked;
1902
1903	serial_driver.open = rs_open;
1904	serial_driver.close = rs_close;
1905	serial_driver.write = rs_write;
1906	serial_driver.flush_chars = rs_flush_chars;
1907	serial_driver.write_room = rs_write_room;
1908	serial_driver.chars_in_buffer = rs_chars_in_buffer;
1909	serial_driver.flush_buffer = rs_flush_buffer;
1910	serial_driver.ioctl = rs_ioctl;
1911	serial_driver.throttle = rs_throttle;
1912	serial_driver.unthrottle = rs_unthrottle;
1913	serial_driver.set_termios = rs_set_termios;
1914	serial_driver.stop = rs_stop;
1915	serial_driver.start = rs_start;
1916	serial_driver.hangup = rs_hangup;
1917	serial_driver.break_ctl = rs_break;
1918	serial_driver.wait_until_sent = rs_wait_until_sent;
1919
1920	/*
1921	 * The callout device is just like normal device except for
1922	 * major number and the subtype code.
1923	 */
1924	callout_driver = serial_driver;
1925#if (LINUX_VERSION_CODE > 0x2032D && defined(CONFIG_DEVFS_FS))
1926	callout_driver.name = "cua/%d";
1927#else
1928	callout_driver.name = "cua";
1929#endif
1930	callout_driver.major = TTYAUX_MAJOR;
1931	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
1932
1933	if (tty_register_driver(&serial_driver))
1934		panic("Couldn't register serial driver");
1935	if (tty_register_driver(&callout_driver))
1936		panic("Couldn't register callout driver");
1937
1938	save_flags(flags); cli();
1939
1940	for (channel = 0; channel < zs_channels_found; ++channel) {
1941		if (request_irq(zs_soft[channel].irq, rs_interrupt, SA_SHIRQ,
1942				"scc", &zs_soft[channel]))
1943			printk(KERN_ERR "decserial: can't get irq %d\n",
1944			       zs_soft[channel].irq);
1945
1946		zs_soft[channel].clk_divisor = 16;
1947		zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]);
1948	}
1949
1950	for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1951
1952		/* Needed before interrupts are enabled. */
1953		info->tty = 0;
1954		info->x_char = 0;
1955
1956		if (info->hook && info->hook->init_info)
1957			continue;
1958
1959		info->magic = SERIAL_MAGIC;
1960		info->port = (int) info->zs_channel->control;
1961		info->line = i;
1962		info->custom_divisor = 16;
1963		info->close_delay = 50;
1964		info->closing_wait = 3000;
1965		info->event = 0;
1966		info->count = 0;
1967		info->blocked_open = 0;
1968		info->tqueue.routine = do_softint;
1969		info->tqueue.data = info;
1970		info->callout_termios = callout_driver.init_termios;
1971		info->normal_termios = serial_driver.init_termios;
1972		init_waitqueue_head(&info->open_wait);
1973		init_waitqueue_head(&info->close_wait);
1974		printk("ttyS%02d at 0x%08x (irq = %d)", info->line,
1975		       info->port, info->irq);
1976		printk(" is a Z85C30 SCC\n");
1977		tty_register_devfs(&serial_driver, 0,
1978				   serial_driver.minor_start + info->line);
1979		tty_register_devfs(&callout_driver, 0,
1980				   callout_driver.minor_start + info->line);
1981
1982	}
1983
1984	restore_flags(flags);
1985
1986	for (channel = 0; channel < zs_channels_found; ++channel) {
1987		if (zs_soft[channel].hook &&
1988		    zs_soft[channel].hook->init_channel)
1989			(*zs_soft[channel].hook->init_channel)
1990				(&zs_soft[channel]);
1991	}
1992
1993	for (info = zs_chain, i = 0; info; info = info->zs_next, i++) {
1994		if (info->hook && info->hook->init_info)
1995			(*info->hook->init_info)(info);
1996	}
1997
1998	return 0;
1999}
2000
2001/*
2002 * polling I/O routines
2003 */
2004static int
2005zs_poll_tx_char(struct dec_serial *info, unsigned char ch)
2006{
2007	struct dec_zschannel *chan = info->zs_channel;
2008	int    ret;
2009
2010	if(chan) {
2011		int loops = 10000;
2012
2013		while (loops && !(read_zsreg(chan, 0) & Tx_BUF_EMP))
2014			loops--;
2015
2016		if (loops) {
2017			write_zsdata(chan, ch);
2018			ret = 0;
2019		} else
2020			ret = -EAGAIN;
2021
2022		return ret;
2023	} else
2024		return -ENODEV;
2025}
2026
2027static int
2028zs_poll_rx_char(struct dec_serial *info)
2029{
2030        struct dec_zschannel *chan = info->zs_channel;
2031        int    ret;
2032
2033	if(chan) {
2034                int loops = 10000;
2035
2036		while (loops && !(read_zsreg(chan, 0) & Rx_CH_AV))
2037			loops--;
2038
2039                if (loops)
2040                        ret = read_zsdata(chan);
2041                else
2042                        ret = -EAGAIN;
2043
2044		return ret;
2045	} else
2046		return -ENODEV;
2047}
2048
2049unsigned int register_zs_hook(unsigned int channel, struct zs_hook *hook)
2050{
2051	struct dec_serial *info = &zs_soft[channel];
2052
2053	if (info->hook) {
2054		printk(__FUNCTION__": line %d has already a hook registered\n", channel);
2055
2056		return 0;
2057	} else {
2058		info->hook = hook;
2059
2060		if (zs_chain == 0)
2061			probe_sccs();
2062
2063		if (!(info->flags & ZILOG_INITIALIZED))
2064			zs_startup(info);
2065
2066		hook->poll_rx_char = zs_poll_rx_char;
2067		hook->poll_tx_char = zs_poll_tx_char;
2068
2069		return 1;
2070	}
2071}
2072
2073unsigned int unregister_zs_hook(unsigned int channel)
2074{
2075	struct dec_serial *info = &zs_soft[channel];
2076
2077        if (info->hook) {
2078                info->hook = NULL;
2079                return 1;
2080        } else {
2081                printk(__FUNCTION__": trying to unregister hook on line %d,"
2082                       " but none is registered\n", channel);
2083                return 0;
2084        }
2085}
2086
2087/*
2088 * ------------------------------------------------------------
2089 * Serial console driver
2090 * ------------------------------------------------------------
2091 */
2092#ifdef CONFIG_SERIAL_DEC_CONSOLE
2093
2094
2095/*
2096 *	Print a string to the serial port trying not to disturb
2097 *	any possible real use of the port...
2098 */
2099static void serial_console_write(struct console *co, const char *s,
2100				 unsigned count)
2101{
2102	struct dec_serial *info;
2103	int i;
2104
2105	info = zs_soft + co->index;
2106
2107	for (i = 0; i < count; i++, s++) {
2108		if(*s == '\n')
2109			zs_poll_tx_char(info, '\r');
2110		zs_poll_tx_char(info, *s);
2111	}
2112}
2113
2114static kdev_t serial_console_device(struct console *c)
2115{
2116	return MKDEV(TTY_MAJOR, 64 + c->index);
2117}
2118
2119/*
2120 *	Setup initial baud/bits/parity. We do two things here:
2121 *	- construct a cflag setting for the first rs_open()
2122 *	- initialize the serial port
2123 *	Return non-zero if we didn't find a serial port.
2124 */
2125static int __init serial_console_setup(struct console *co, char *options)
2126{
2127	struct dec_serial *info;
2128	int	baud = 9600;
2129	int	bits = 8;
2130	int	parity = 'n';
2131	int	cflag = CREAD | HUPCL | CLOCAL;
2132	char	*s;
2133	unsigned long flags;
2134
2135	if(!BUS_PRESENT)
2136		return -ENODEV;
2137
2138	info = zs_soft + co->index;
2139
2140	if (zs_chain == 0)
2141		probe_sccs();
2142
2143	info->is_cons = 1;
2144
2145	if (options) {
2146		baud = simple_strtoul(options, NULL, 10);
2147		s = options;
2148		while(*s >= '0' && *s <= '9')
2149			s++;
2150		if (*s)
2151			parity = *s++;
2152		if (*s)
2153			bits   = *s - '0';
2154	}
2155
2156	/*
2157	 *	Now construct a cflag setting.
2158	 */
2159	switch(baud) {
2160	case 1200:
2161		cflag |= B1200;
2162		break;
2163	case 2400:
2164		cflag |= B2400;
2165		break;
2166	case 4800:
2167		cflag |= B4800;
2168		break;
2169	case 19200:
2170		cflag |= B19200;
2171		break;
2172	case 38400:
2173		cflag |= B38400;
2174		break;
2175	case 57600:
2176		cflag |= B57600;
2177		break;
2178	case 115200:
2179		cflag |= B115200;
2180		break;
2181	case 9600:
2182	default:
2183		cflag |= B9600;
2184		break;
2185	}
2186	switch(bits) {
2187	case 7:
2188		cflag |= CS7;
2189		break;
2190	default:
2191	case 8:
2192		cflag |= CS8;
2193		break;
2194	}
2195	switch(parity) {
2196	case 'o': case 'O':
2197		cflag |= PARODD;
2198		break;
2199	case 'e': case 'E':
2200		cflag |= PARENB;
2201		break;
2202	}
2203	co->cflag = cflag;
2204	save_and_cli(flags);
2205
2206	/*
2207	 * Turn on RTS and DTR.
2208	 */
2209	zs_rtsdtr(info, RTS | DTR, 1);
2210
2211	/*
2212	 * Finally, enable sequencing
2213	 */
2214	info->zs_channel->curregs[3] |= (RxENABLE | Rx8);
2215	info->zs_channel->curregs[5] |= (TxENAB | Tx8);
2216	info->zs_channel->curregs[9] |= (VIS);
2217	write_zsreg(info->zs_channel, 3, info->zs_channel->curregs[3]);
2218	write_zsreg(info->zs_channel, 5, info->zs_channel->curregs[5]);
2219	write_zsreg(info->zs_channel, 9, info->zs_channel->curregs[9]);
2220
2221	/*
2222	 * Clear the interrupt registers.
2223	 */
2224	write_zsreg(info->zs_channel, 0, ERR_RES);
2225	write_zsreg(info->zs_channel, 0, RES_H_IUS);
2226
2227	/*
2228	 * Set the speed of the serial port
2229	 */
2230	change_speed(info);
2231
2232	/* Save the current value of RR0 */
2233	info->read_reg_zero = read_zsreg(info->zs_channel, 0);
2234
2235	zs_soft[co->index].clk_divisor = 16;
2236	zs_soft[co->index].zs_baud = get_zsbaud(&zs_soft[co->index]);
2237
2238	restore_flags(flags);
2239	return 0;
2240}
2241
2242static struct console sercons = {
2243	.name		= "ttyS",
2244	.write		= serial_console_write,
2245	.device		= serial_console_device,
2246	.setup		= serial_console_setup,
2247	.flags		= CON_PRINTBUFFER,
2248	.index		= -1,
2249};
2250
2251/*
2252 *	Register console.
2253 */
2254void __init zs_serial_console_init(void)
2255{
2256	register_console(&sercons);
2257}
2258#endif /* ifdef CONFIG_SERIAL_DEC_CONSOLE */
2259
2260#ifdef CONFIG_KGDB
2261struct dec_zschannel *zs_kgdbchan;
2262static unsigned char scc_inittab[] = {
2263	9,  0x80,	/* reset A side (CHRA) */
2264	13, 0,		/* set baud rate divisor */
2265	12, 1,
2266	14, 1,		/* baud rate gen enable, src=rtxc (BRENABL) */
2267	11, 0x50,	/* clocks = br gen (RCBR | TCBR) */
2268	5,  0x6a,	/* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */
2269	4,  0x44,	/* x16 clock, 1 stop (SB1 | X16CLK)*/
2270	3,  0xc1,	/* rx enable, 8 bits (RxENABLE | Rx8)*/
2271};
2272
2273/* These are for receiving and sending characters under the kgdb
2274 * source level kernel debugger.
2275 */
2276void putDebugChar(char kgdb_char)
2277{
2278	struct dec_zschannel *chan = zs_kgdbchan;
2279	while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0)
2280		RECOVERY_DELAY;
2281	write_zsdata(chan, kgdb_char);
2282}
2283char getDebugChar(void)
2284{
2285	struct dec_zschannel *chan = zs_kgdbchan;
2286	while((read_zsreg(chan, 0) & Rx_CH_AV) == 0)
2287		eieio(); /*barrier();*/
2288	return read_zsdata(chan);
2289}
2290void kgdb_interruptible(int yes)
2291{
2292	struct dec_zschannel *chan = zs_kgdbchan;
2293	int one, nine;
2294	nine = read_zsreg(chan, 9);
2295	if (yes == 1) {
2296		one = EXT_INT_ENAB|INT_ALL_Rx;
2297		nine |= MIE;
2298		printk("turning serial ints on\n");
2299	} else {
2300		one = RxINT_DISAB;
2301		nine &= ~MIE;
2302		printk("turning serial ints off\n");
2303	}
2304	write_zsreg(chan, 1, one);
2305	write_zsreg(chan, 9, nine);
2306}
2307
2308static int kgdbhook_init_channel(struct dec_serial* info)
2309{
2310	return 0;
2311}
2312
2313static void kgdbhook_init_info(struct dec_serial* info)
2314{
2315}
2316
2317static void kgdbhook_rx_char(struct dec_serial* info,
2318			     unsigned char ch, unsigned char stat)
2319{
2320	if (ch == 0x03 || ch == '$')
2321		breakpoint();
2322	if (stat & (Rx_OVR|FRM_ERR|PAR_ERR))
2323		write_zsreg(info->zs_channel, 0, ERR_RES);
2324}
2325
2326/* This sets up the serial port we're using, and turns on
2327 * interrupts for that channel, so kgdb is usable once we're done.
2328 */
2329static inline void kgdb_chaninit(struct dec_zschannel *ms, int intson, int bps)
2330{
2331	int brg;
2332	int i, x;
2333	volatile char *sccc = ms->control;
2334	brg = BPS_TO_BRG(bps, zs_parms->clock/16);
2335	printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg);
2336	for (i = 20000; i != 0; --i) {
2337		x = *sccc; eieio();
2338	}
2339	for (i = 0; i < sizeof(scc_inittab); ++i) {
2340		write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]);
2341		i++;
2342	}
2343}
2344/* This is called at boot time to prime the kgdb serial debugging
2345 * serial line.  The 'tty_num' argument is 0 for /dev/ttya and 1
2346 * for /dev/ttyb which is determined in setup_arch() from the
2347 * boot command line flags.
2348 */
2349struct zs_hook zs_kgdbhook = {
2350	init_channel : kgdbhook_init_channel,
2351	init_info    : kgdbhook_init_info,
2352	cflags       : B38400|CS8|CLOCAL,
2353	rx_char      : kgdbhook_rx_char,
2354}
2355
2356void __init zs_kgdb_hook(int tty_num)
2357{
2358	/* Find out how many Z8530 SCCs we have */
2359	if (zs_chain == 0)
2360		probe_sccs();
2361	zs_soft[tty_num].zs_channel = &zs_channels[tty_num];
2362	zs_kgdbchan = zs_soft[tty_num].zs_channel;
2363	zs_soft[tty_num].change_needed = 0;
2364	zs_soft[tty_num].clk_divisor = 16;
2365	zs_soft[tty_num].zs_baud = 38400;
2366 	zs_soft[tty_num].hook = &zs_kgdbhook; /* This runs kgdb */
2367	/* Turn on transmitter/receiver at 8-bits/char */
2368        kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400);
2369	printk("KGDB: on channel %d initialized\n", tty_num);
2370	set_debug_traps(); /* init stub */
2371}
2372#endif /* ifdef CONFIG_KGDB */
2373
2374
2375