1/* $Id: sab82532.c,v 1.1.1.1 2008/10/15 03:26:47 james26_jang Exp $
2 * sab82532.c: ASYNC Driver for the SIEMENS SAB82532 DUSCC.
3 *
4 * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5 *
6 * Rewrote buffer handling to use CIRC(Circular Buffer) macros.
7 *   Maxim Krasnyanskiy <maxk@qualcomm.com>
8 *
9 * Fixed to use tty_get_baud_rate, and to allow for arbitrary baud
10 * rates to be programmed into the UART.  Also eliminated a lot of
11 * duplicated code in the console setup.
12 *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
13 */
14
15#include <linux/config.h>
16#include <linux/module.h>
17#include <linux/errno.h>
18#include <linux/signal.h>
19#include <linux/sched.h>
20#include <linux/timer.h>
21#include <linux/interrupt.h>
22#include <linux/tty.h>
23#include <linux/tty_flip.h>
24#include <linux/serial.h>
25#include <linux/serialP.h>
26#include <linux/serial_reg.h>
27#include <linux/console.h>
28#include <linux/major.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/ptrace.h>
32#include <linux/ioport.h>
33#include <linux/mm.h>
34#include <linux/slab.h>
35#include <linux/init.h>
36#include <linux/delay.h>
37
38#include <asm/sab82532.h>
39#include <asm/uaccess.h>
40#include <asm/ebus.h>
41#include <asm/irq.h>
42
43#include "sunserial.h"
44
45static DECLARE_TASK_QUEUE(tq_serial);
46
47/* This is (one of many) a special gross hack to allow SU and
48 * SAB serials to co-exist on the same machine. -DaveM
49 */
50#undef SERIAL_BH
51#define SERIAL_BH	AURORA_BH
52
53static struct tty_driver serial_driver, callout_driver;
54static int sab82532_refcount;
55
56/* number of characters left in xmit buffer before we ask for more */
57#define WAKEUP_CHARS 256
58
59#undef SERIAL_PARANOIA_CHECK
60#define SERIAL_DO_RESTART
61
62/* Set of debugging defines */
63#undef SERIAL_DEBUG_OPEN
64#undef SERIAL_DEBUG_FLOW
65#undef SERIAL_DEBUG_MODEM
66#undef SERIAL_DEBUG_WAIT_UNTIL_SENT
67#undef SERIAL_DEBUG_SEND_BREAK
68#undef SERIAL_DEBUG_INTR
69#undef SERIAL_DEBUG_FIFO
70#define SERIAL_DEBUG_OVERFLOW 1
71
72/* Trace things on serial device, useful for console debugging: */
73#undef SERIAL_LOG_DEVICE
74
75#ifdef SERIAL_LOG_DEVICE
76static void dprint_init(int tty);
77#endif
78
79static void change_speed(struct sab82532 *info);
80static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout);
81
82/*
83 * This assumes you have a 29.4912 MHz clock for your UART.
84 */
85#define BASE_BAUD ( 29491200 / 16 )
86
87static struct sab82532 *sab82532_chain = 0;
88static struct tty_struct *sab82532_table[NR_PORTS];
89static struct termios *sab82532_termios[NR_PORTS];
90static struct termios *sab82532_termios_locked[NR_PORTS];
91
92#ifdef MODULE
93#undef CONFIG_SERIAL_CONSOLE
94#endif
95
96#ifdef CONFIG_SERIAL_CONSOLE
97extern int serial_console;
98static struct console sab82532_console;
99static int sab82532_console_init(void);
100static void batten_down_hatches(struct sab82532 *info);
101#endif
102
103#ifndef MIN
104#define MIN(a,b)	((a) < (b) ? (a) : (b))
105#endif
106
107static char *sab82532_version[16] = {
108	"V1.0", "V2.0", "V3.2", "V(0x03)",
109	"V(0x04)", "V(0x05)", "V(0x06)", "V(0x07)",
110	"V(0x08)", "V(0x09)", "V(0x0a)", "V(0x0b)",
111	"V(0x0c)", "V(0x0d)", "V(0x0e)", "V(0x0f)"
112};
113static char serial_version[16];
114
115/*
116 * tmp_buf is used as a temporary buffer by sab82532_write.  We need to
117 * lock it in case the copy_from_user blocks while swapping in a page,
118 * and some other program tries to do a serial write at the same time.
119 * Since the lock will only come under contention when the system is
120 * swapping and available memory is low, it makes sense to share one
121 * buffer across all the serial ports, since it significantly saves
122 * memory if large numbers of serial ports are open.
123 */
124static unsigned char *tmp_buf = 0;
125static DECLARE_MUTEX(tmp_buf_sem);
126
127static inline int serial_paranoia_check(struct sab82532 *info,
128					kdev_t device, const char *routine)
129{
130#ifdef SERIAL_PARANOIA_CHECK
131	static const char *badmagic =
132		"Warning: bad magic number for serial struct (%s) in %s\n";
133	static const char *badinfo =
134		"Warning: null sab82532 for (%s) in %s\n";
135
136	if (!info) {
137		printk(badinfo, kdevname(device), routine);
138		return 1;
139	}
140	if (info->magic != SERIAL_MAGIC) {
141		printk(badmagic, kdevname(device), routine);
142		return 1;
143	}
144#endif
145	return 0;
146}
147
148/*
149 * This is used to figure out the divisor speeds.
150 *
151 * The formula is:    Baud = BASE_BAUD / ((N + 1) * (1 << M)),
152 *
153 * with               0 <= N < 64 and 0 <= M < 16
154 *
155 * 12-Oct-2001 - Replaced table driven approach with code written by
156 * Theodore Ts'o <tytso@alum.mit.edu> which exactly replicates the
157 * table.  (Modulo bugs for the 307200 and 61440 baud rates, which
158 * were clearly incorrectly calculated in the original table.  This is
159 * why tables filled with magic constants are evil.)
160 */
161
162static void calc_ebrg(int baud, int *n_ret, int *m_ret)
163{
164	int	n, m;
165
166	if (baud == 0) {
167		*n_ret = 0;
168		*m_ret = 0;
169		return;
170	}
171
172	/*
173	 * We scale numbers by 10 so that we get better accuracy
174	 * without having to use floating point.  Here we increment m
175	 * until n is within the valid range.
176	 */
177	n = (BASE_BAUD*10) / baud;
178	m = 0;
179	while (n >= 640) {
180		n = n / 2;
181		m++;
182	}
183	n = (n+5) / 10;
184	/*
185	 * We try very hard to avoid speeds with M == 0 since they may
186	 * not work correctly for XTAL frequences above 10 MHz.
187	 */
188	if ((m == 0) && ((n & 1) == 0)) {
189		n = n / 2;
190		m++;
191	}
192	*n_ret = n - 1;
193	*m_ret = m;
194}
195
196#define SAB82532_MAX_TEC_TIMEOUT 200000	/* 1 character time (at 50 baud) */
197#define SAB82532_MAX_CEC_TIMEOUT  50000	/* 2.5 TX CLKs (at 50 baud) */
198
199static __inline__ void sab82532_tec_wait(struct sab82532 *info)
200{
201	int timeout = info->tec_timeout;
202
203	while ((readb(&info->regs->r.star) & SAB82532_STAR_TEC) && --timeout)
204		udelay(1);
205}
206
207static __inline__ void sab82532_cec_wait(struct sab82532 *info)
208{
209	int timeout = info->cec_timeout;
210
211	while ((readb(&info->regs->r.star) & SAB82532_STAR_CEC) && --timeout)
212		udelay(1);
213}
214
215static __inline__ void sab82532_start_tx(struct sab82532 *info)
216{
217	unsigned long flags;
218	int i;
219
220	save_flags(flags); cli();
221
222	if (info->xmit.head == info->xmit.tail)
223		goto out;
224
225	if (!test_bit(SAB82532_XPR, &info->irqflags))
226		goto out;
227
228	info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
229	writeb(info->interrupt_mask1, &info->regs->w.imr1);
230	clear_bit(SAB82532_ALLS, &info->irqflags);
231
232	clear_bit(SAB82532_XPR, &info->irqflags);
233	for (i = 0; i < info->xmit_fifo_size; i++) {
234		writeb(info->xmit.buf[info->xmit.tail],
235		       &info->regs->w.xfifo[i]);
236		info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
237		info->icount.tx++;
238		if (info->xmit.head == info->xmit.tail)
239			break;
240	}
241
242	/* Issue a Transmit Frame command. */
243	sab82532_cec_wait(info);
244	writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
245
246out:
247	restore_flags(flags);
248}
249
250
251/*
252 * ------------------------------------------------------------
253 * sab82532_stop() and sab82532_start()
254 *
255 * This routines are called before setting or resetting tty->stopped.
256 * They enable or disable transmitter interrupts, as necessary.
257 * ------------------------------------------------------------
258 */
259static void sab82532_stop(struct tty_struct *tty)
260{
261	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
262	unsigned long flags;
263
264	if (serial_paranoia_check(info, tty->device, "sab82532_stop"))
265		return;
266
267	save_flags(flags); cli();
268	info->interrupt_mask1 |= SAB82532_IMR1_XPR;
269	writeb(info->interrupt_mask1, &info->regs->w.imr1);
270	restore_flags(flags);
271}
272
273static void sab82532_start(struct tty_struct *tty)
274{
275	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
276	unsigned long flags;
277
278	if (serial_paranoia_check(info, tty->device, "sab82532_start"))
279		return;
280
281	save_flags(flags); cli();
282	info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
283	writeb(info->interrupt_mask1, &info->regs->w.imr1);
284	sab82532_start_tx(info);
285	restore_flags(flags);
286}
287
288/*
289 * ----------------------------------------------------------------------
290 *
291 * Here starts the interrupt handling routines.  All of the following
292 * subroutines are declared as inline and are folded into
293 * sab82532_interrupt().  They were separated out for readability's sake.
294 *
295 * Note: sab82532_interrupt() is a "fast" interrupt, which means that it
296 * runs with interrupts turned off.  People who may want to modify
297 * sab82532_interrupt() should try to keep the interrupt handler as fast as
298 * possible.  After you are done making modifications, it is not a bad
299 * idea to do:
300 *
301 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
302 *
303 * and look at the resulting assemble code in serial.s.
304 *
305 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
306 * -----------------------------------------------------------------------
307 */
308
309/*
310 * This routine is used by the interrupt handler to schedule
311 * processing in the software interrupt portion of the driver.
312 */
313static void sab82532_sched_event(struct sab82532 *info, int event)
314{
315	info->event |= 1 << event;
316	queue_task(&info->tqueue, &tq_serial);
317	mark_bh(SERIAL_BH);
318}
319
320static void receive_chars(struct sab82532 *info,
321			  union sab82532_irq_status *stat)
322{
323	struct tty_struct *tty = info->tty;
324	unsigned char buf[32];
325	unsigned char status;
326	int free_fifo = 0;
327	int i, count = 0;
328
329	/* Read number of BYTES (Character + Status) available. */
330	if (stat->sreg.isr0 & SAB82532_ISR0_RPF) {
331		count = info->recv_fifo_size;
332		free_fifo++;
333	}
334
335	if (stat->sreg.isr0 & SAB82532_ISR0_TCD) {
336		count = readb(&info->regs->r.rbcl) & (info->recv_fifo_size - 1);
337		free_fifo++;
338	}
339
340	/* Issue a FIFO read command in case we where idle. */
341	if (stat->sreg.isr0 & SAB82532_ISR0_TIME) {
342		sab82532_cec_wait(info);
343		writeb(SAB82532_CMDR_RFRD, &info->regs->w.cmdr);
344		return;
345	}
346
347	if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
348#ifdef SERIAL_DEBUG_OVERFLOW
349		printk("sab82532: receive_chars: RFO");
350#endif
351		free_fifo++;
352	}
353
354	/* Read the FIFO. */
355	for (i = 0; i < count; i++)
356		buf[i] = readb(&info->regs->r.rfifo[i]);
357
358	/* Issue Receive Message Complete command. */
359	if (free_fifo) {
360		sab82532_cec_wait(info);
361		writeb(SAB82532_CMDR_RMC, &info->regs->w.cmdr);
362	}
363
364	if (!tty)
365		return;
366
367	for (i = 0; i < count; ) {
368		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
369#ifdef SERIAL_DEBUG_OVERFLOW
370			printk("sab82532: receive_chars: tty overrun\n");
371#endif
372			info->icount.buf_overrun++;
373			break;
374		}
375
376		tty->flip.count++;
377		*tty->flip.char_buf_ptr++ = buf[i++];
378		status = buf[i++];
379		info->icount.rx++;
380
381#ifdef SERIAL_DEBUG_INTR
382                printk("DR%02x:%02x...", (unsigned char)*(tty->flip.char_buf_ptr - 1), status);
383#endif
384
385		if (status & SAB82532_RSTAT_PE) {
386			*tty->flip.flag_buf_ptr++ = TTY_PARITY;
387			info->icount.parity++;
388		} else if (status & SAB82532_RSTAT_FE) {
389			*tty->flip.flag_buf_ptr++ = TTY_FRAME;
390			info->icount.frame++;
391		}
392		else
393			*tty->flip.flag_buf_ptr++ = TTY_NORMAL;
394	}
395
396	queue_task(&tty->flip.tqueue, &tq_timer);
397}
398
399static void transmit_chars(struct sab82532 *info,
400			   union sab82532_irq_status *stat)
401{
402	int i;
403
404	if (stat->sreg.isr1 & SAB82532_ISR1_ALLS) {
405		info->interrupt_mask1 |= SAB82532_IMR1_ALLS;
406		writeb(info->interrupt_mask1, &info->regs->w.imr1);
407		set_bit(SAB82532_ALLS, &info->irqflags);
408	}
409
410	if (!(stat->sreg.isr1 & SAB82532_ISR1_XPR))
411		return;
412
413	if (!(readb(&info->regs->r.star) & SAB82532_STAR_XFW)) {
414#ifdef SERIAL_DEBUG_FIFO
415		printk("%s: XPR, but no XFW (?)\n", __FUNCTION__);
416#endif
417		return;
418	}
419
420	set_bit(SAB82532_XPR, &info->irqflags);
421
422	if (!info->tty) {
423		info->interrupt_mask1 |= SAB82532_IMR1_XPR;
424		writeb(info->interrupt_mask1, &info->regs->w.imr1);
425		return;
426	}
427
428	if ((info->xmit.head == info->xmit.tail) ||
429	    info->tty->stopped || info->tty->hw_stopped) {
430		info->interrupt_mask1 |= SAB82532_IMR1_XPR;
431		writeb(info->interrupt_mask1, &info->regs->w.imr1);
432		return;
433	}
434
435	info->interrupt_mask1 &= ~(SAB82532_IMR1_ALLS);
436	writeb(info->interrupt_mask1, &info->regs->w.imr1);
437	clear_bit(SAB82532_ALLS, &info->irqflags);
438
439	/* Stuff 32 bytes into Transmit FIFO. */
440	clear_bit(SAB82532_XPR, &info->irqflags);
441	for (i = 0; i < info->xmit_fifo_size; i++) {
442		writeb(info->xmit.buf[info->xmit.tail],
443		       &info->regs->w.xfifo[i]);
444		info->xmit.tail = (info->xmit.tail + 1) & (SERIAL_XMIT_SIZE-1);
445		info->icount.tx++;
446		if (info->xmit.head == info->xmit.tail)
447			break;
448	}
449
450	/* Issue a Transmit Frame command. */
451	sab82532_cec_wait(info);
452	writeb(SAB82532_CMDR_XF, &info->regs->w.cmdr);
453
454	if (CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE) < WAKEUP_CHARS)
455		sab82532_sched_event(info, RS_EVENT_WRITE_WAKEUP);
456
457#ifdef SERIAL_DEBUG_INTR
458	printk("THRE...");
459#endif
460}
461
462static void check_status(struct sab82532 *info,
463			 union sab82532_irq_status *stat)
464{
465	struct tty_struct *tty = info->tty;
466	int modem_change = 0;
467
468	if (stat->sreg.isr1 & SAB82532_ISR1_BRK) {
469#ifdef CONFIG_SERIAL_CONSOLE
470		if (info->is_console) {
471			batten_down_hatches(info);
472			return;
473		}
474#endif
475		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
476			info->icount.buf_overrun++;
477			goto check_modem;
478		}
479		tty->flip.count++;
480		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
481		*tty->flip.char_buf_ptr++ = 0;
482		info->icount.brk++;
483	}
484
485	if (!tty)
486		return;
487
488	if (stat->sreg.isr0 & SAB82532_ISR0_RFO) {
489		if (tty->flip.count >= TTY_FLIPBUF_SIZE) {
490			info->icount.buf_overrun++;
491			goto check_modem;
492		}
493		tty->flip.count++;
494		*tty->flip.flag_buf_ptr++ = TTY_PARITY;
495		*tty->flip.char_buf_ptr++ = 0;
496		info->icount.overrun++;
497	}
498
499check_modem:
500	if (stat->sreg.isr0 & SAB82532_ISR0_CDSC) {
501		info->dcd = (readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : 1;
502		info->icount.dcd++;
503		modem_change++;
504#ifdef SERIAL_DEBUG_MODEM
505		printk("DCD change: %d\n", info->icount.dcd);
506#endif
507	}
508	if (stat->sreg.isr1 & SAB82532_ISR1_CSC) {
509		info->cts = readb(&info->regs->r.star) & SAB82532_STAR_CTS;
510		info->icount.cts++;
511		modem_change++;
512#ifdef SERIAL_DEBUG_MODEM
513		printk("CTS change: %d, CTS %s\n", info->icount.cts, info->cts ? "on" : "off");
514#endif
515	}
516	if ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ^ info->dsr) {
517		info->dsr = (readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : 1;
518		info->icount.dsr++;
519		modem_change++;
520#ifdef SERIAL_DEBUG_MODEM
521		printk("DSR change: %d\n", info->icount.dsr);
522#endif
523	}
524	if (modem_change)
525		wake_up_interruptible(&info->delta_msr_wait);
526
527	if ((info->flags & ASYNC_CHECK_CD) &&
528	    (stat->sreg.isr0 & SAB82532_ISR0_CDSC)) {
529
530#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
531		printk("ttys%d CD now %s...", info->line,
532		       (info->dcd) ? "on" : "off");
533#endif
534
535		if (info->dcd)
536			wake_up_interruptible(&info->open_wait);
537		else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
538			   (info->flags & ASYNC_CALLOUT_NOHUP))) {
539
540#ifdef SERIAL_DEBUG_OPEN
541			printk("scheduling hangup...");
542#endif
543			MOD_INC_USE_COUNT;
544			if (schedule_task(&info->tqueue_hangup) == 0)
545				MOD_DEC_USE_COUNT;
546		}
547	}
548
549	if (info->flags & ASYNC_CTS_FLOW) {
550		if (info->tty->hw_stopped) {
551			if (info->cts) {
552
553#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
554				printk("CTS tx start...");
555#endif
556				info->tty->hw_stopped = 0;
557				sab82532_sched_event(info,
558						     RS_EVENT_WRITE_WAKEUP);
559				info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
560				writeb(info->interrupt_mask1, &info->regs->w.imr1);
561				sab82532_start_tx(info);
562			}
563		} else {
564			if (!(info->cts)) {
565
566#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
567				printk("CTS tx stop...");
568#endif
569				info->tty->hw_stopped = 1;
570			}
571		}
572	}
573}
574
575/*
576 * This is the serial driver's generic interrupt routine
577 */
578static void sab82532_interrupt(int irq, void *dev_id, struct pt_regs *regs)
579{
580	struct sab82532 *info = dev_id;
581	union sab82532_irq_status status;
582
583#ifdef SERIAL_DEBUG_INTR
584	printk("sab82532_interrupt(%d)...", irq);
585#endif
586
587	status.stat = 0;
588	if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA0)
589		status.sreg.isr0 = readb(&info->regs->r.isr0);
590	if (readb(&info->regs->r.gis) & SAB82532_GIS_ISA1)
591		status.sreg.isr1 = readb(&info->regs->r.isr1);
592
593#ifdef SERIAL_DEBUG_INTR
594	printk("%d<%02x.%02x>", info->line,
595	       status.sreg.isr0, status.sreg.isr1);
596#endif
597
598	if (!status.stat)
599		goto next;
600
601	if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
602				SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
603		receive_chars(info, &status);
604	if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
605	    (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
606		check_status(info, &status);
607	if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
608		transmit_chars(info, &status);
609
610next:
611	info = info->next;
612	status.stat = 0;
613	if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB0)
614		status.sreg.isr0 = readb(&info->regs->r.isr0);
615	if (readb(&info->regs->r.gis) & SAB82532_GIS_ISB1)
616		status.sreg.isr1 = readb(&info->regs->r.isr1);
617
618#ifdef SERIAL_DEBUG_INTR
619	printk("%d<%02x.%02x>", info->line,
620	       status.sreg.isr0, status.sreg.isr1);
621#endif
622
623	if (!status.stat)
624		goto done;
625
626	if (status.sreg.isr0 & (SAB82532_ISR0_TCD | SAB82532_ISR0_TIME |
627				SAB82532_ISR0_RFO | SAB82532_ISR0_RPF))
628		receive_chars(info, &status);
629	if ((status.sreg.isr0 & SAB82532_ISR0_CDSC) ||
630	    (status.sreg.isr1 & (SAB82532_ISR1_BRK | SAB82532_ISR1_CSC)))
631		check_status(info, &status);
632	if (status.sreg.isr1 & (SAB82532_ISR1_ALLS | SAB82532_ISR1_XPR))
633		transmit_chars(info, &status);
634
635done:
636	;
637#ifdef SERIAL_DEBUG_INTR
638	printk("end.\n");
639#endif
640}
641
642/*
643 * -------------------------------------------------------------------
644 * Here ends the serial interrupt routines.
645 * -------------------------------------------------------------------
646 */
647
648/*
649 * This routine is used to handle the "bottom half" processing for the
650 * serial driver, known also the "software interrupt" processing.
651 * This processing is done at the kernel interrupt level, after the
652 * sab82532_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
653 * is where time-consuming activities which can not be done in the
654 * interrupt driver proper are done; the interrupt driver schedules
655 * them using sab82532_sched_event(), and they get done here.
656 */
657static void do_serial_bh(void)
658{
659	run_task_queue(&tq_serial);
660}
661
662static void do_softint(void *private_)
663{
664	struct sab82532	*info = (struct sab82532 *)private_;
665	struct tty_struct *tty;
666
667	tty = info->tty;
668	if (!tty)
669		return;
670
671	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
672		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
673		    tty->ldisc.write_wakeup)
674			(tty->ldisc.write_wakeup)(tty);
675		wake_up_interruptible(&tty->write_wait);
676	}
677}
678
679/*
680 * This routine is called from the scheduler tqueue when the interrupt
681 * routine has signalled that a hangup has occurred.  The path of
682 * hangup processing is:
683 *
684 * 	serial interrupt routine -> (scheduler tqueue) ->
685 * 	do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
686 *
687 */
688static void do_serial_hangup(void *private_)
689{
690	struct sab82532	*info = (struct sab82532 *) private_;
691	struct tty_struct *tty;
692
693	tty = info->tty;
694	if (tty)
695		tty_hangup(tty);
696	MOD_DEC_USE_COUNT;
697}
698
699static void
700sab82532_init_line(struct sab82532 *info)
701{
702	unsigned char stat, tmp;
703
704	/*
705	 * Wait for any commands or immediate characters
706	 */
707	sab82532_cec_wait(info);
708	sab82532_tec_wait(info);
709
710	/*
711	 * Clear the FIFO buffers.
712	 */
713	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
714	sab82532_cec_wait(info);
715	writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
716
717	/*
718	 * Clear the interrupt registers.
719	 */
720	stat = readb(&info->regs->r.isr0);
721	stat = readb(&info->regs->r.isr1);
722
723	/*
724	 * Now, initialize the UART
725	 */
726	writeb(0, &info->regs->w.ccr0);				/* power-down */
727	writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
728	       SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
729	writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
730	writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
731	       SAB82532_CCR2_TOE, &info->regs->w.ccr2);
732	writeb(0, &info->regs->w.ccr3);
733	writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
734	writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
735	       SAB82532_MODE_RAC, &info->regs->w.mode);
736	writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
737	switch (info->recv_fifo_size) {
738		case 1:
739			tmp = readb(&info->regs->w.rfc);
740			tmp |= SAB82532_RFC_RFTH_1;
741			writeb(tmp, &info->regs->w.rfc);
742			break;
743		case 4:
744			tmp = readb(&info->regs->w.rfc);
745			tmp |= SAB82532_RFC_RFTH_4;
746			writeb(tmp, &info->regs->w.rfc);
747			break;
748		case 16:
749			tmp = readb(&info->regs->w.rfc);
750			tmp |= SAB82532_RFC_RFTH_16;
751			writeb(tmp, &info->regs->w.rfc);
752			break;
753		default:
754			info->recv_fifo_size = 32;
755			/* fall through */
756		case 32:
757			tmp = readb(&info->regs->w.rfc);
758			tmp |= SAB82532_RFC_RFTH_32;
759			writeb(tmp, &info->regs->w.rfc);
760			break;
761	}
762	tmp = readb(&info->regs->rw.ccr0);
763	tmp |= SAB82532_CCR0_PU;	/* power-up */
764	writeb(tmp, &info->regs->rw.ccr0);
765}
766
767static int startup(struct sab82532 *info)
768{
769	unsigned long flags;
770	unsigned long page;
771	int retval = 0;
772
773	page = get_free_page(GFP_KERNEL);
774	if (!page)
775		return -ENOMEM;
776
777	save_flags(flags); cli();
778
779	if (info->flags & ASYNC_INITIALIZED) {
780		free_page(page);
781		goto errout;
782	}
783
784	if (!info->regs) {
785		if (info->tty)
786			set_bit(TTY_IO_ERROR, &info->tty->flags);
787		free_page(page);
788		retval = -ENODEV;
789		goto errout;
790	}
791	if (info->xmit.buf)
792		free_page(page);
793	else
794		info->xmit.buf = (unsigned char *)page;
795
796#ifdef SERIAL_DEBUG_OPEN
797	printk("starting up serial port %d...", info->line);
798#endif
799
800	/*
801	 * Initialize the Hardware
802	 */
803	sab82532_init_line(info);
804
805	if (info->tty->termios->c_cflag & CBAUD) {
806		u8 tmp;
807
808		tmp = readb(&info->regs->rw.mode);
809		tmp &= ~(SAB82532_MODE_FRTS);
810		tmp |= SAB82532_MODE_RTS;
811		writeb(tmp, &info->regs->rw.mode);
812
813		tmp = readb(&info->regs->rw.pvr);
814		tmp &= ~(info->pvr_dtr_bit);
815		writeb(tmp, &info->regs->rw.pvr);
816	}
817
818	/*
819	 * Finally, enable interrupts
820	 */
821	info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
822				SAB82532_IMR0_PLLA;
823	writeb(info->interrupt_mask0, &info->regs->w.imr0);
824	info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
825				SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
826				SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
827				SAB82532_IMR1_XPR;
828	writeb(info->interrupt_mask1, &info->regs->w.imr1);
829	set_bit(SAB82532_ALLS, &info->irqflags);
830
831	if (info->tty)
832		clear_bit(TTY_IO_ERROR, &info->tty->flags);
833	info->xmit.head = info->xmit.tail = 0;
834
835	set_bit(SAB82532_XPR, &info->irqflags);
836
837	/*
838	 * and set the speed of the serial port
839	 */
840	change_speed(info);
841
842	info->flags |= ASYNC_INITIALIZED;
843	restore_flags(flags);
844	return 0;
845
846errout:
847	restore_flags(flags);
848	return retval;
849}
850
851/*
852 * This routine will shutdown a serial port; interrupts are disabled, and
853 * DTR is dropped if the hangup on close termio flag is on.
854 */
855static void shutdown(struct sab82532 *info)
856{
857	unsigned long flags;
858	u8 tmp;
859
860	if (!(info->flags & ASYNC_INITIALIZED))
861		return;
862
863#ifdef SERIAL_DEBUG_OPEN
864	printk("Shutting down serial port %d...", info->line);
865#endif
866
867	save_flags(flags); cli(); /* Disable interrupts */
868
869	/*
870	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871	 * here so the queue might never be waken up
872	 */
873	wake_up_interruptible(&info->delta_msr_wait);
874
875	if (info->xmit.buf) {
876		free_page((unsigned long)info->xmit.buf);
877		info->xmit.buf = 0;
878	}
879
880#ifdef CONFIG_SERIAL_CONSOLE
881	if (info->is_console) {
882		info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
883					SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
884		writeb(info->interrupt_mask0, &info->regs->w.imr0);
885		info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
886					SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
887					SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
888					SAB82532_IMR1_XPR;
889		writeb(info->interrupt_mask1, &info->regs->w.imr1);
890		if (info->tty)
891			set_bit(TTY_IO_ERROR, &info->tty->flags);
892		info->flags &= ~ASYNC_INITIALIZED;
893		restore_flags(flags);
894		return;
895	}
896#endif
897
898	/* Disable Interrupts */
899	info->interrupt_mask0 = 0xff;
900	writeb(info->interrupt_mask0, &info->regs->w.imr0);
901	info->interrupt_mask1 = 0xff;
902	writeb(info->interrupt_mask1, &info->regs->w.imr1);
903
904	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
905		tmp = readb(&info->regs->r.mode);
906		tmp |= (SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
907		writeb(tmp, &info->regs->rw.mode);
908		writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit,
909		       &info->regs->rw.pvr);
910	}
911
912	/* Disable break condition */
913	tmp = readb(&info->regs->rw.dafo);
914	tmp &= ~(SAB82532_DAFO_XBRK);
915	writeb(tmp, &info->regs->rw.dafo);
916
917	/* Disable Receiver */
918	tmp = readb(&info->regs->rw.mode);
919	tmp &= ~(SAB82532_MODE_RAC);
920	writeb(tmp, &info->regs->rw.mode);
921
922	/* Power Down */
923	tmp = readb(&info->regs->rw.ccr0);
924	tmp &= ~(SAB82532_CCR0_PU);
925	writeb(tmp, &info->regs->rw.ccr0);
926
927	if (info->tty)
928		set_bit(TTY_IO_ERROR, &info->tty->flags);
929
930	info->flags &= ~ASYNC_INITIALIZED;
931	restore_flags(flags);
932}
933
934/*
935 * This routine is called to set the UART divisor registers to match
936 * the specified baud rate for a serial port.
937 */
938static void change_speed(struct sab82532 *info)
939{
940	unsigned long	flags;
941	unsigned int	ebrg;
942	tcflag_t	cflag;
943	unsigned char	dafo;
944	int		bits, n, m;
945
946	if (!info->tty || !info->tty->termios)
947		return;
948	cflag = info->tty->termios->c_cflag;
949
950	/* Byte size and parity */
951	switch (cflag & CSIZE) {
952	      case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
953	      case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
954	      case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
955	      case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
956	      /* Never happens, but GCC is too dumb to figure it out */
957	      default:  dafo = SAB82532_DAFO_CHL5; bits = 7; break;
958	}
959
960	if (cflag & CSTOPB) {
961		dafo |= SAB82532_DAFO_STOP;
962		bits++;
963	}
964
965	if (cflag & PARENB) {
966		dafo |= SAB82532_DAFO_PARE;
967		bits++;
968	}
969
970	if (cflag & PARODD) {
971#ifdef CMSPAR
972		if (cflag & CMSPAR)
973			dafo |= SAB82532_DAFO_PAR_MARK;
974		else
975#endif
976			dafo |= SAB82532_DAFO_PAR_ODD;
977	} else {
978#ifdef CMSPAR
979		if (cflag & CMSPAR)
980			dafo |= SAB82532_DAFO_PAR_SPACE;
981		else
982#endif
983			dafo |= SAB82532_DAFO_PAR_EVEN;
984	}
985
986	/* Determine EBRG values based on baud rate */
987	info->baud = tty_get_baud_rate(info->tty);
988	calc_ebrg(info->baud, &n, &m);
989
990	ebrg = n | (m << 6);
991
992	if (info->baud) {
993		info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
994		info->tec_timeout = (10 * 1000000) / info->baud;
995		info->cec_timeout = info->tec_timeout >> 2;
996	} else {
997		info->timeout = 0;
998		info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
999		info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
1000	}
1001	info->timeout += HZ / 50;		/* Add .02 seconds of slop */
1002
1003	/* CTS flow control flags */
1004	if (cflag & CRTSCTS)
1005		info->flags |= ASYNC_CTS_FLOW;
1006	else
1007		info->flags &= ~(ASYNC_CTS_FLOW);
1008
1009	if (cflag & CLOCAL)
1010		info->flags &= ~(ASYNC_CHECK_CD);
1011	else
1012		info->flags |= ASYNC_CHECK_CD;
1013	if (info->tty)
1014		info->tty->hw_stopped = 0;
1015
1016#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1017
1018
1019	if ((cflag & CREAD) == 0)
1020		info->ignore_status_mask |= SAB82532_ISR0_RPF |
1021					    SAB82532_ISR0_TCD |
1022					    SAB82532_ISR0_TIME;
1023
1024	save_flags(flags); cli();
1025	sab82532_cec_wait(info);
1026	sab82532_tec_wait(info);
1027	writeb(dafo, &info->regs->w.dafo);
1028	writeb(ebrg & 0xff, &info->regs->w.bgr);
1029	writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1030	writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1031	if (info->flags & ASYNC_CTS_FLOW) {
1032		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1033		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1034		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1035		info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1036		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1037	} else {
1038		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1039		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1040		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1041		info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1042		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1043	}
1044	writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1045	restore_flags(flags);
1046}
1047
1048static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1049{
1050	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1051	unsigned long flags;
1052
1053	if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1054		return;
1055
1056	if (!tty || !info->xmit.buf)
1057		return;
1058
1059	save_flags(flags); cli();
1060	if (!CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)) {
1061		restore_flags(flags);
1062		return;
1063	}
1064
1065	info->xmit.buf[info->xmit.head] = ch;
1066	info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1067	restore_flags(flags);
1068}
1069
1070static void sab82532_flush_chars(struct tty_struct *tty)
1071{
1072	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1073	unsigned long flags;
1074
1075	if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1076		return;
1077
1078	if ((info->xmit.head == info->xmit.tail) ||
1079	    tty->stopped || tty->hw_stopped || !info->xmit.buf)
1080		return;
1081
1082	save_flags(flags); cli();
1083	info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1084	writeb(info->interrupt_mask1, &info->regs->w.imr1);
1085	sab82532_start_tx(info);
1086	restore_flags(flags);
1087}
1088
1089static int sab82532_write(struct tty_struct * tty, int from_user,
1090			  const unsigned char *buf, int count)
1091{
1092	int c, ret = 0;
1093	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1094	unsigned long flags;
1095
1096	if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1097		return 0;
1098
1099	if (!tty || !info->xmit.buf || !tmp_buf)
1100		return 0;
1101
1102	save_flags(flags);
1103	if (from_user) {
1104		down(&tmp_buf_sem);
1105		while (1) {
1106			int c1;
1107			c = CIRC_SPACE_TO_END(info->xmit.head,
1108					      info->xmit.tail,
1109					      SERIAL_XMIT_SIZE);
1110			if (count < c)
1111				c = count;
1112			if (c <= 0)
1113				break;
1114
1115			c -= copy_from_user(tmp_buf, buf, c);
1116			if (!c) {
1117				if (!ret)
1118					ret = -EFAULT;
1119				break;
1120			}
1121			cli();
1122			c1 = CIRC_SPACE_TO_END(info->xmit.head,
1123					       info->xmit.tail,
1124					       SERIAL_XMIT_SIZE);
1125			if (c1 < c)
1126				c = c1;
1127			memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1128			info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1129			restore_flags(flags);
1130			buf += c;
1131			count -= c;
1132			ret += c;
1133		}
1134		up(&tmp_buf_sem);
1135	} else {
1136		cli();
1137		while (1) {
1138			c = CIRC_SPACE_TO_END(info->xmit.head,
1139					      info->xmit.tail,
1140					      SERIAL_XMIT_SIZE);
1141			if (count < c)
1142				c = count;
1143			if (c <= 0)
1144				break;
1145			memcpy(info->xmit.buf + info->xmit.head, buf, c);
1146			info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1147			buf += c;
1148			count -= c;
1149			ret += c;
1150		}
1151		restore_flags(flags);
1152	}
1153
1154	if ((info->xmit.head != info->xmit.tail) &&
1155	    !tty->stopped && !tty->hw_stopped) {
1156		info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1157		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1158		sab82532_start_tx(info);
1159	}
1160
1161	restore_flags(flags);
1162	return ret;
1163}
1164
1165static int sab82532_write_room(struct tty_struct *tty)
1166{
1167	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1168
1169	if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1170		return 0;
1171
1172	return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1173}
1174
1175static int sab82532_chars_in_buffer(struct tty_struct *tty)
1176{
1177	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1178
1179	if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1180		return 0;
1181
1182	return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1183}
1184
1185static void sab82532_flush_buffer(struct tty_struct *tty)
1186{
1187	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1188	unsigned long flags;
1189
1190	if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1191		return;
1192
1193	save_flags(flags); cli();
1194	info->xmit.head = info->xmit.tail = 0;
1195	restore_flags(flags);
1196
1197	wake_up_interruptible(&tty->write_wait);
1198	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1199	    tty->ldisc.write_wakeup)
1200		(tty->ldisc.write_wakeup)(tty);
1201}
1202
1203/*
1204 * This function is used to send a high-priority XON/XOFF character to
1205 * the device
1206 */
1207static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1208{
1209	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1210	unsigned long flags;
1211
1212	if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1213		return;
1214
1215	save_flags(flags); cli();
1216	sab82532_tec_wait(info);
1217	writeb(ch, &info->regs->w.tic);
1218	restore_flags(flags);
1219}
1220
1221/*
1222 * ------------------------------------------------------------
1223 * sab82532_throttle()
1224 *
1225 * This routine is called by the upper-layer tty layer to signal that
1226 * incoming characters should be throttled.
1227 * ------------------------------------------------------------
1228 */
1229static void sab82532_throttle(struct tty_struct * tty)
1230{
1231	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1232#ifdef SERIAL_DEBUG_THROTTLE
1233	char	buf[64];
1234
1235	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1236	       tty->ldisc.chars_in_buffer(tty));
1237#endif
1238
1239	if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1240		return;
1241
1242	if (I_IXOFF(tty))
1243		sab82532_send_xchar(tty, STOP_CHAR(tty));
1244
1245	if (tty->termios->c_cflag & CRTSCTS) {
1246		u8 mode = readb(&info->regs->r.mode);
1247		mode &= ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
1248		writeb(mode, &info->regs->w.mode);
1249	}
1250}
1251
1252static void sab82532_unthrottle(struct tty_struct * tty)
1253{
1254	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1255#ifdef SERIAL_DEBUG_THROTTLE
1256	char	buf[64];
1257
1258	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1259	       tty->ldisc.chars_in_buffer(tty));
1260#endif
1261
1262	if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1263		return;
1264
1265	if (I_IXOFF(tty)) {
1266		if (info->x_char)
1267			info->x_char = 0;
1268		else
1269			sab82532_send_xchar(tty, START_CHAR(tty));
1270	}
1271
1272	if (tty->termios->c_cflag & CRTSCTS) {
1273		u8 mode = readb(&info->regs->r.mode);
1274		mode &= ~(SAB82532_MODE_RTS);
1275		mode |= SAB82532_MODE_FRTS;
1276		writeb(mode, &info->regs->w.mode);
1277	}
1278}
1279
1280/*
1281 * ------------------------------------------------------------
1282 * sab82532_ioctl() and friends
1283 * ------------------------------------------------------------
1284 */
1285
1286static int get_serial_info(struct sab82532 *info,
1287			   struct serial_struct *retinfo)
1288{
1289	struct serial_struct tmp;
1290
1291	if (!retinfo)
1292		return -EFAULT;
1293	memset(&tmp, 0, sizeof(tmp));
1294	tmp.type = info->type;
1295	tmp.line = info->line;
1296	tmp.port = (unsigned long)info->regs;
1297	tmp.irq = info->irq;
1298	tmp.flags = info->flags;
1299	tmp.xmit_fifo_size = info->xmit_fifo_size;
1300	tmp.baud_base = info->baud_base;
1301	tmp.close_delay = info->close_delay;
1302	tmp.closing_wait = info->closing_wait;
1303	tmp.custom_divisor = info->custom_divisor;
1304	tmp.hub6 = 0;
1305	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1306		return -EFAULT;
1307	return 0;
1308}
1309
1310static int set_serial_info(struct sab82532 *info,
1311			   struct serial_struct *new_info)
1312{
1313	return 0;
1314}
1315
1316
1317/*
1318 * get_lsr_info - get line status register info
1319 *
1320 * Purpose: Let user call ioctl() to get info when the UART physically
1321 * 	    is emptied.  On bus types like RS485, the transmitter must
1322 * 	    release the bus after transmitting. This must be done when
1323 * 	    the transmit shift register is empty, not be done when the
1324 * 	    transmit holding register is empty.  This functionality
1325 * 	    allows an RS485 driver to be written in user space.
1326 */
1327static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1328{
1329	unsigned int result;
1330
1331	result = (!info->xmit.buf && test_bit(SAB82532_ALLS, &info->irqflags))
1332							? TIOCSER_TEMT : 0;
1333	return put_user(result, value);
1334}
1335
1336
1337static int get_modem_info(struct sab82532 * info, unsigned int *value)
1338{
1339	unsigned int result;
1340
1341	result =  ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1342		    ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1343							    : TIOCM_RTS)
1344		| ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1345		| ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1346		| ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1347		| ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1348	return put_user(result,value);
1349}
1350
1351static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1352			  unsigned int *value)
1353{
1354	unsigned int arg;
1355
1356	if (get_user(arg, value))
1357		return -EFAULT;
1358	switch (cmd) {
1359	case TIOCMBIS:
1360		if (arg & TIOCM_RTS) {
1361			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1362			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1363		}
1364		if (arg & TIOCM_DTR) {
1365			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1366		}
1367		break;
1368	case TIOCMBIC:
1369		if (arg & TIOCM_RTS) {
1370			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1371			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1372		}
1373		if (arg & TIOCM_DTR) {
1374			writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1375		}
1376		break;
1377	case TIOCMSET:
1378		if (arg & TIOCM_RTS) {
1379			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1380			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1381		} else {
1382			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1383			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1384		}
1385		if (arg & TIOCM_DTR) {
1386			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1387		} else {
1388			writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1389		}
1390		break;
1391	default:
1392		return -EINVAL;
1393	}
1394	return 0;
1395}
1396
1397/*
1398 * This routine sends a break character out the serial port.
1399 */
1400static void sab82532_break(struct tty_struct *tty, int break_state)
1401{
1402	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1403	unsigned long flags;
1404
1405	if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1406		return;
1407
1408	if (!info->regs)
1409		return;
1410
1411#ifdef SERIAL_DEBUG_SEND_BREAK
1412	printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1413#endif
1414	save_flags(flags); cli();
1415	if (break_state == -1)
1416		writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1417	else
1418		writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1419	restore_flags(flags);
1420}
1421
1422
1423static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1424		    unsigned int cmd, unsigned long arg)
1425{
1426	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1427	struct async_icount cprev, cnow;	/* kernel counter temps */
1428	struct serial_icounter_struct *p_cuser;	/* user space */
1429
1430	if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1431		return -ENODEV;
1432
1433	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1434	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1435	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1436	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1437		if (tty->flags & (1 << TTY_IO_ERROR))
1438		    return -EIO;
1439	}
1440
1441	switch (cmd) {
1442		case TIOCGSOFTCAR:
1443			return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1444		case TIOCSSOFTCAR:
1445			if (get_user(arg, (unsigned int *) arg))
1446				return -EFAULT;
1447			tty->termios->c_cflag =
1448				((tty->termios->c_cflag & ~CLOCAL) |
1449				 (arg ? CLOCAL : 0));
1450			return 0;
1451		case TIOCMGET:
1452			return get_modem_info(info, (unsigned int *) arg);
1453		case TIOCMBIS:
1454		case TIOCMBIC:
1455		case TIOCMSET:
1456			return set_modem_info(info, cmd, (unsigned int *) arg);
1457		case TIOCGSERIAL:
1458			return get_serial_info(info,
1459					       (struct serial_struct *) arg);
1460		case TIOCSSERIAL:
1461			return set_serial_info(info,
1462					       (struct serial_struct *) arg);
1463
1464		case TIOCSERGETLSR: /* Get line status register */
1465			return get_lsr_info(info, (unsigned int *) arg);
1466
1467		case TIOCSERGSTRUCT:
1468			if (copy_to_user((struct sab82532 *) arg,
1469					 info, sizeof(struct sab82532)))
1470				return -EFAULT;
1471			return 0;
1472
1473		/*
1474		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1475		 * - mask passed in arg for lines of interest
1476 		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1477		 * Caller should use TIOCGICOUNT to see which one it was
1478		 */
1479		 case TIOCMIWAIT:
1480			cli();
1481			/* note the counters on entry */
1482			cprev = info->icount;
1483			sti();
1484			while (1) {
1485				interruptible_sleep_on(&info->delta_msr_wait);
1486				/* see if a signal did it */
1487				if (signal_pending(current))
1488					return -ERESTARTSYS;
1489				cli();
1490				cnow = info->icount; /* atomic copy */
1491				sti();
1492				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1493				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1494					return -EIO; /* no change => error */
1495				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1496				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1497				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1498				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1499					return 0;
1500				}
1501				cprev = cnow;
1502			}
1503			/* NOTREACHED */
1504
1505		/*
1506		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1507		 * Return: write counters to the user passed counter struct
1508		 * NB: both 1->0 and 0->1 transitions are counted except for
1509		 *     RI where only 0->1 is counted.
1510		 */
1511		case TIOCGICOUNT:
1512			cli();
1513			cnow = info->icount;
1514			sti();
1515			p_cuser = (struct serial_icounter_struct *) arg;
1516			if (put_user(cnow.cts, &p_cuser->cts) ||
1517			    put_user(cnow.dsr, &p_cuser->dsr) ||
1518			    put_user(cnow.rng, &p_cuser->rng) ||
1519			    put_user(cnow.dcd, &p_cuser->dcd))
1520				return -EFAULT;
1521			return 0;
1522
1523		default:
1524			return -ENOIOCTLCMD;
1525		}
1526	return 0;
1527}
1528
1529static void sab82532_set_termios(struct tty_struct *tty,
1530				 struct termios *old_termios)
1531{
1532	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1533
1534	if (   (tty->termios->c_cflag == old_termios->c_cflag)
1535	    && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1536		== RELEVANT_IFLAG(old_termios->c_iflag)))
1537	  return;
1538
1539	change_speed(info);
1540
1541	/* Handle transition to B0 status */
1542	if ((old_termios->c_cflag & CBAUD) &&
1543	    !(tty->termios->c_cflag & CBAUD)) {
1544		writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1545		writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1546		writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1547	}
1548
1549	/* Handle transition away from B0 status */
1550	if (!(old_termios->c_cflag & CBAUD) &&
1551	    (tty->termios->c_cflag & CBAUD)) {
1552		writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1553		if (tty->termios->c_cflag & CRTSCTS) {
1554			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_RTS), &info->regs->w.mode);
1555			writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1556		} else if (test_bit(TTY_THROTTLED, &tty->flags)) {
1557			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS), &info->regs->w.mode);
1558		} else {
1559			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1560			writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1561		}
1562	}
1563
1564	/* Handle turning off CRTSCTS */
1565	if ((old_termios->c_cflag & CRTSCTS) &&
1566	    !(tty->termios->c_cflag & CRTSCTS)) {
1567		tty->hw_stopped = 0;
1568		sab82532_start(tty);
1569	}
1570}
1571
1572/*
1573 * ------------------------------------------------------------
1574 * sab82532_close()
1575 *
1576 * This routine is called when the serial port gets closed.  First, we
1577 * wait for the last remaining data to be sent.  Then, we unlink its
1578 * async structure from the interrupt chain if necessary, and we free
1579 * that IRQ if nothing is left in the chain.
1580 * ------------------------------------------------------------
1581 */
1582static void sab82532_close(struct tty_struct *tty, struct file * filp)
1583{
1584	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1585	unsigned long flags;
1586
1587	if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1588		return;
1589
1590	save_flags(flags); cli();
1591
1592	if (tty_hung_up_p(filp)) {
1593		MOD_DEC_USE_COUNT;
1594		restore_flags(flags);
1595		return;
1596	}
1597
1598#ifdef SERIAL_DEBUG_OPEN
1599	printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1600#endif
1601	if ((tty->count == 1) && (info->count != 1)) {
1602		/*
1603		 * Uh, oh.  tty->count is 1, which means that the tty
1604		 * structure will be freed.  info->count should always
1605		 * be one in these conditions.  If it's greater than
1606		 * one, we've got real problems, since it means the
1607		 * serial port won't be shutdown.
1608		 */
1609		printk("sab82532_close: bad serial port count; tty->count is 1,"
1610		       " info->count is %d\n", info->count);
1611		info->count = 1;
1612	}
1613	if (--info->count < 0) {
1614		printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1615		       info->line, info->count);
1616		info->count = 0;
1617	}
1618	if (info->count) {
1619		MOD_DEC_USE_COUNT;
1620		restore_flags(flags);
1621		return;
1622	}
1623	info->flags |= ASYNC_CLOSING;
1624	/*
1625	 * Save the termios structure, since this port may have
1626	 * separate termios for callout and dialin.
1627	 */
1628	if (info->flags & ASYNC_NORMAL_ACTIVE)
1629		info->normal_termios = *tty->termios;
1630	if (info->flags & ASYNC_CALLOUT_ACTIVE)
1631		info->callout_termios = *tty->termios;
1632	/*
1633	 * Now we wait for the transmit buffer to clear; and we notify
1634	 * the line discipline to only process XON/XOFF characters.
1635	 */
1636	tty->closing = 1;
1637	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1638		tty_wait_until_sent(tty, info->closing_wait);
1639
1640	/*
1641	 * At this point we stop accepting input.  To do this, we
1642	 * disable the receive line status interrupts, and turn off
1643	 * the receiver.
1644	 */
1645	info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1646	writeb(info->interrupt_mask0, &info->regs->w.imr0);
1647	if (info->flags & ASYNC_INITIALIZED) {
1648		/*
1649		 * Before we drop DTR, make sure the UART transmitter
1650		 * has completely drained; this is especially
1651		 * important if there is a transmit FIFO!
1652		 */
1653		sab82532_wait_until_sent(tty, info->timeout);
1654	}
1655	shutdown(info);
1656	if (tty->driver.flush_buffer)
1657		tty->driver.flush_buffer(tty);
1658	if (tty->ldisc.flush_buffer)
1659		tty->ldisc.flush_buffer(tty);
1660	tty->closing = 0;
1661	info->event = 0;
1662	info->tty = 0;
1663	if (info->blocked_open) {
1664		if (info->close_delay) {
1665			current->state = TASK_INTERRUPTIBLE;
1666			schedule_timeout(info->close_delay);
1667		}
1668		wake_up_interruptible(&info->open_wait);
1669	}
1670	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1671			 ASYNC_CLOSING);
1672	wake_up_interruptible(&info->close_wait);
1673	MOD_DEC_USE_COUNT;
1674	restore_flags(flags);
1675}
1676
1677/*
1678 * sab82532_wait_until_sent() --- wait until the transmitter is empty
1679 */
1680static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1681{
1682	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1683	unsigned long orig_jiffies, char_time;
1684
1685	if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1686		return;
1687
1688	/*
1689	 * Set the check interval to be 1/5 of the estimated time to
1690	 * send a single character, and make it at least 1.  The check
1691	 * interval should also be less than the timeout.
1692	 *
1693	 * Note: we have to use pretty tight timings here to satisfy
1694	 * the NIST-PCTS.
1695	 */
1696	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1697	char_time = char_time / 5;
1698	if (char_time == 0)
1699		char_time = 1;
1700	if (timeout)
1701	  char_time = MIN(char_time, timeout);
1702#ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1703	printk("In sab82532_wait_until_sent(%d) check=%lu "
1704	       "xmit_cnt = %ld, alls = %d (jiff=%lu)...\n",
1705	       timeout, char_time,
1706	       CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1707	       test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1708#endif
1709	orig_jiffies = jiffies;
1710	while ((info->xmit.head != info->xmit.tail) ||
1711	       !test_bit(SAB82532_ALLS, &info->irqflags)) {
1712		current->state = TASK_INTERRUPTIBLE;
1713		schedule_timeout(char_time);
1714		if (signal_pending(current))
1715			break;
1716		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1717			break;
1718	}
1719#ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1720	printk("xmit_cnt = %ld, alls = %d (jiff=%lu)...done\n",
1721	       CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1722	       test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1723#endif
1724}
1725
1726/*
1727 * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1728 */
1729static void sab82532_hangup(struct tty_struct *tty)
1730{
1731	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1732
1733	if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1734		return;
1735
1736#ifdef CONFIG_SERIAL_CONSOLE
1737	if (info->is_console)
1738		return;
1739#endif
1740
1741	sab82532_flush_buffer(tty);
1742	shutdown(info);
1743	info->event = 0;
1744	info->count = 0;
1745	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1746	info->tty = 0;
1747	wake_up_interruptible(&info->open_wait);
1748}
1749
1750/*
1751 * ------------------------------------------------------------
1752 * sab82532_open() and friends
1753 * ------------------------------------------------------------
1754 */
1755static int block_til_ready(struct tty_struct *tty, struct file * filp,
1756			   struct sab82532 *info)
1757{
1758	DECLARE_WAITQUEUE(wait, current);
1759	int retval;
1760	int do_clocal = 0;
1761
1762	/*
1763	 * If the device is in the middle of being closed, then block
1764	 * until it's done, and then try again.
1765	 */
1766	if (tty_hung_up_p(filp) ||
1767	    (info->flags & ASYNC_CLOSING)) {
1768		if (info->flags & ASYNC_CLOSING)
1769			interruptible_sleep_on(&info->close_wait);
1770#ifdef SERIAL_DO_RESTART
1771		if (info->flags & ASYNC_HUP_NOTIFY)
1772			return -EAGAIN;
1773		else
1774			return -ERESTARTSYS;
1775#else
1776		return -EAGAIN;
1777#endif
1778	}
1779
1780	/*
1781	 * If this is a callout device, then just make sure the normal
1782	 * device isn't being used.
1783	 */
1784	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1785		if (info->flags & ASYNC_NORMAL_ACTIVE)
1786			return -EBUSY;
1787		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1788		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1789		    (info->session != current->session))
1790		    return -EBUSY;
1791		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1792		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1793		    (info->pgrp != current->pgrp))
1794		    return -EBUSY;
1795		info->flags |= ASYNC_CALLOUT_ACTIVE;
1796		return 0;
1797	}
1798
1799	/*
1800	 * If non-blocking mode is set, or the port is not enabled,
1801	 * then make the check up front and then exit.
1802	 */
1803	if ((filp->f_flags & O_NONBLOCK) ||
1804	    (tty->flags & (1 << TTY_IO_ERROR))) {
1805		if (info->flags & ASYNC_CALLOUT_ACTIVE)
1806			return -EBUSY;
1807		info->flags |= ASYNC_NORMAL_ACTIVE;
1808		return 0;
1809	}
1810
1811	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1812		if (info->normal_termios.c_cflag & CLOCAL)
1813			do_clocal = 1;
1814	} else {
1815		if (tty->termios->c_cflag & CLOCAL)
1816			do_clocal = 1;
1817	}
1818
1819	/*
1820	 * Block waiting for the carrier detect and the line to become
1821	 * free (i.e., not in use by the callout).  While we are in
1822	 * this loop, info->count is dropped by one, so that
1823	 * sab82532_close() knows when to free things.  We restore it upon
1824	 * exit, either normal or abnormal.
1825	 */
1826	retval = 0;
1827	add_wait_queue(&info->open_wait, &wait);
1828#ifdef SERIAL_DEBUG_OPEN
1829	printk("block_til_ready before block: ttyS%d, count = %d\n",
1830	       info->line, info->count);
1831#endif
1832	cli();
1833	if (!tty_hung_up_p(filp))
1834		info->count--;
1835	sti();
1836	info->blocked_open++;
1837	while (1) {
1838		cli();
1839		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1840		    (tty->termios->c_cflag & CBAUD)) {
1841			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1842			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1843			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1844		}
1845		sti();
1846		set_current_state(TASK_INTERRUPTIBLE);
1847		if (tty_hung_up_p(filp) ||
1848		    !(info->flags & ASYNC_INITIALIZED)) {
1849#ifdef SERIAL_DO_RESTART
1850			if (info->flags & ASYNC_HUP_NOTIFY)
1851				retval = -EAGAIN;
1852			else
1853				retval = -ERESTARTSYS;
1854#else
1855			retval = -EAGAIN;
1856#endif
1857			break;
1858		}
1859		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1860		    !(info->flags & ASYNC_CLOSING) &&
1861		    (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1862			break;
1863		if (signal_pending(current)) {
1864			retval = -ERESTARTSYS;
1865			break;
1866		}
1867#ifdef SERIAL_DEBUG_OPEN
1868		printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1869		       info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1870#endif
1871		schedule();
1872	}
1873	current->state = TASK_RUNNING;
1874	remove_wait_queue(&info->open_wait, &wait);
1875	if (!tty_hung_up_p(filp))
1876		info->count++;
1877	info->blocked_open--;
1878#ifdef SERIAL_DEBUG_OPEN
1879	printk("block_til_ready after blocking: ttys%d, count = %d\n",
1880	       info->line, info->count);
1881#endif
1882	if (retval)
1883		return retval;
1884	info->flags |= ASYNC_NORMAL_ACTIVE;
1885	return 0;
1886}
1887
1888/*
1889 * This routine is called whenever a serial port is opened.  It
1890 * enables interrupts for a serial port, linking in its async structure into
1891 * the IRQ chain.   It also performs the serial-specific
1892 * initialization for the tty structure.
1893 */
1894static int sab82532_open(struct tty_struct *tty, struct file * filp)
1895{
1896	struct sab82532	*info = sab82532_chain;
1897	int retval, line;
1898	unsigned long page;
1899
1900#ifdef SERIAL_DEBUG_OPEN
1901	printk("sab82532_open: count = %d\n", info->count);
1902#endif
1903
1904	line = MINOR(tty->device) - tty->driver.minor_start;
1905	if ((line < 0) || (line >= NR_PORTS))
1906		return -ENODEV;
1907
1908	while (info) {
1909		if (info->line == line)
1910			break;
1911		info = info->next;
1912	}
1913	if (!info) {
1914		printk("sab82532_open: can't find info for line %d\n",
1915		       line);
1916		return -ENODEV;
1917	}
1918
1919	if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1920		return -ENODEV;
1921
1922#ifdef SERIAL_DEBUG_OPEN
1923	printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1924	       info->count);
1925#endif
1926
1927	if (!tmp_buf) {
1928		page = get_free_page(GFP_KERNEL);
1929		if (!page)
1930			return -ENOMEM;
1931		if (tmp_buf)
1932			free_page(page);
1933		else
1934			tmp_buf = (unsigned char *) page;
1935	}
1936
1937	info->count++;
1938	tty->driver_data = info;
1939	info->tty = tty;
1940
1941	/*
1942	 * If the port is in the middle of closing, bail out now.
1943	 */
1944	if (tty_hung_up_p(filp) ||
1945	    (info->flags & ASYNC_CLOSING)) {
1946		if (info->flags & ASYNC_CLOSING)
1947			interruptible_sleep_on(&info->close_wait);
1948#ifdef SERIAL_DO_RESTART
1949		return ((info->flags & ASYNC_HUP_NOTIFY) ?
1950			-EAGAIN : -ERESTARTSYS);
1951#else
1952		return -EAGAIN;
1953#endif
1954	}
1955
1956	/*
1957	 * Start up serial port
1958	 */
1959	retval = startup(info);
1960	if (retval)
1961		return retval;
1962
1963	MOD_INC_USE_COUNT;
1964	retval = block_til_ready(tty, filp, info);
1965	if (retval) {
1966#ifdef SERIAL_DEBUG_OPEN
1967		printk("sab82532_open returning after block_til_ready with %d\n",
1968		       retval);
1969#endif
1970		return retval;
1971	}
1972
1973	if ((info->count == 1) &&
1974	    (info->flags & ASYNC_SPLIT_TERMIOS)) {
1975		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1976			*tty->termios = info->normal_termios;
1977		else
1978			*tty->termios = info->callout_termios;
1979		change_speed(info);
1980	}
1981
1982#ifdef CONFIG_SERIAL_CONSOLE
1983	if (sab82532_console.cflag && sab82532_console.index == line) {
1984		tty->termios->c_cflag = sab82532_console.cflag;
1985		sab82532_console.cflag = 0;
1986		change_speed(info);
1987	}
1988#endif
1989
1990	info->session = current->session;
1991	info->pgrp = current->pgrp;
1992
1993#ifdef SERIAL_DEBUG_OPEN
1994	printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
1995#endif
1996	return 0;
1997}
1998
1999/*
2000 * /proc fs routines....
2001 */
2002
2003static __inline__ int
2004line_info(char *buf, struct sab82532 *info)
2005{
2006	unsigned long flags;
2007	char stat_buf[30];
2008	int ret;
2009
2010	ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
2011	switch (info->type) {
2012		case 0:
2013			ret += sprintf(buf+ret, "V1.0 ");
2014			break;
2015		case 1:
2016			ret += sprintf(buf+ret, "V2.0 ");
2017			break;
2018		case 2:
2019			ret += sprintf(buf+ret, "V3.2 ");
2020			break;
2021		default:
2022			ret += sprintf(buf+ret, "V?.? ");
2023			break;
2024	}
2025	ret += sprintf(buf+ret, "port:%lX irq:%s",
2026		       (unsigned long)info->regs, __irq_itoa(info->irq));
2027
2028	if (!info->regs) {
2029		ret += sprintf(buf+ret, "\n");
2030		return ret;
2031	}
2032
2033	/*
2034	 * Figure out the current RS-232 lines
2035	 */
2036	stat_buf[0] = 0;
2037	stat_buf[1] = 0;
2038	save_flags(flags); cli();
2039	if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2040		if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2041			strcat(stat_buf, "|RTS");
2042	} else {
2043		strcat(stat_buf, "|RTS");
2044	}
2045	if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2046		strcat(stat_buf, "|CTS");
2047	if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2048		strcat(stat_buf, "|DTR");
2049	if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2050		strcat(stat_buf, "|DSR");
2051	if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2052		strcat(stat_buf, "|CD");
2053	restore_flags(flags);
2054
2055	if (info->baud)
2056		ret += sprintf(buf+ret, " baud:%u", info->baud);
2057
2058	ret += sprintf(buf+ret, " tx:%u rx:%u",
2059		       info->icount.tx, info->icount.rx);
2060
2061	if (info->icount.frame)
2062		ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2063
2064	if (info->icount.parity)
2065		ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2066
2067	if (info->icount.brk)
2068		ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2069
2070	if (info->icount.overrun)
2071		ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2072
2073	/*
2074	 * Last thing is the RS-232 status lines.
2075	 */
2076	ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2077	return ret;
2078}
2079
2080int sab82532_read_proc(char *page, char **start, off_t off, int count,
2081		       int *eof, void *data)
2082{
2083	struct sab82532 *info = sab82532_chain;
2084	off_t begin = 0;
2085	int len = 0;
2086
2087	len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2088	for (info = sab82532_chain; info && len < 4000; info = info->next) {
2089		len += line_info(page + len, info);
2090		if (len+begin > off+count)
2091			goto done;
2092		if (len+begin < off) {
2093			begin += len;
2094			len = 0;
2095		}
2096	}
2097	*eof = 1;
2098done:
2099	if (off >= len+begin)
2100		return 0;
2101	*start = page + (off-begin);
2102	return ((count < begin+len-off) ? count : begin+len-off);
2103}
2104
2105/*
2106 * ---------------------------------------------------------------------
2107 * sab82532_init() and friends
2108 *
2109 * sab82532_init() is called at boot-time to initialize the serial driver.
2110 * ---------------------------------------------------------------------
2111 */
2112static int __init get_sab82532(unsigned long *memory_start)
2113{
2114	struct linux_ebus *ebus;
2115	struct linux_ebus_device *edev = 0;
2116	struct sab82532 *sab;
2117	unsigned long regs, offset;
2118	int i;
2119
2120	for_each_ebus(ebus) {
2121		for_each_ebusdev(edev, ebus) {
2122			if (!strcmp(edev->prom_name, "se"))
2123				goto ebus_done;
2124
2125			if (!strcmp(edev->prom_name, "serial")) {
2126				char compat[32];
2127				int clen;
2128
2129				/* On RIO this can be an SE, check it.  We could
2130				 * just check ebus->is_rio, but this is more portable.
2131				 */
2132				clen = prom_getproperty(edev->prom_node, "compatible",
2133							compat, sizeof(compat));
2134				if (clen > 0) {
2135					if (strncmp(compat, "sab82532", 8) == 0) {
2136						/* Yep. */
2137						goto ebus_done;
2138					}
2139				}
2140			}
2141		}
2142	}
2143ebus_done:
2144	if (!edev)
2145		return -ENODEV;
2146
2147	regs = edev->resource[0].start;
2148	offset = sizeof(union sab82532_async_regs);
2149
2150	for (i = 0; i < 2; i++) {
2151		if (memory_start) {
2152			*memory_start = (*memory_start + 7) & ~(7);
2153			sab = (struct sab82532 *)*memory_start;
2154			*memory_start += sizeof(struct sab82532);
2155		} else {
2156			sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2157							 GFP_KERNEL);
2158			if (!sab) {
2159				printk("sab82532: can't alloc sab struct\n");
2160				break;
2161			}
2162		}
2163		memset(sab, 0, sizeof(struct sab82532));
2164
2165		sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2166		sab->irq = edev->irqs[0];
2167		sab->line = 1 - i;
2168		sab->xmit_fifo_size = 32;
2169		sab->recv_fifo_size = 32;
2170
2171		writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2172
2173		sab->next = sab82532_chain;
2174		sab82532_chain = sab;
2175
2176		offset -= sizeof(union sab82532_async_regs);
2177	}
2178	return 0;
2179}
2180
2181#ifndef MODULE
2182static void __init sab82532_kgdb_hook(int line)
2183{
2184	prom_printf("sab82532: kgdb support is not implemented, yet\n");
2185	prom_halt();
2186}
2187#endif
2188
2189static inline void __init show_serial_version(void)
2190{
2191	char *revision = "$Revision: 1.1.1.1 $";
2192	char *version, *p;
2193
2194	version = strchr(revision, ' ');
2195	strcpy(serial_version, ++version);
2196	p = strchr(serial_version, ' ');
2197	*p = '\0';
2198	printk("SAB82532 serial driver version %s\n", serial_version);
2199}
2200
2201extern int su_num_ports;
2202
2203/*
2204 * The serial driver boot-time initialization code!
2205 */
2206int __init sab82532_init(void)
2207{
2208	struct sab82532 *info;
2209	int i;
2210
2211	if (!sab82532_chain)
2212		get_sab82532(0);
2213	if (!sab82532_chain)
2214		return -ENODEV;
2215
2216	init_bh(SERIAL_BH, do_serial_bh);
2217
2218	show_serial_version();
2219
2220	/* Initialize the tty_driver structure */
2221	memset(&serial_driver, 0, sizeof(struct tty_driver));
2222	serial_driver.magic = TTY_DRIVER_MAGIC;
2223	serial_driver.driver_name = "serial";
2224#ifdef CONFIG_DEVFS_FS
2225	serial_driver.name = "tts/%d";
2226#else
2227	serial_driver.name = "ttyS";
2228#endif
2229	serial_driver.major = TTY_MAJOR;
2230	serial_driver.minor_start = 64 + su_num_ports;
2231	serial_driver.num = NR_PORTS;
2232	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2233	serial_driver.subtype = SERIAL_TYPE_NORMAL;
2234	serial_driver.init_termios = tty_std_termios;
2235	serial_driver.init_termios.c_cflag =
2236		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2237	serial_driver.flags = TTY_DRIVER_REAL_RAW;
2238	serial_driver.refcount = &sab82532_refcount;
2239	serial_driver.table = sab82532_table;
2240	serial_driver.termios = sab82532_termios;
2241	serial_driver.termios_locked = sab82532_termios_locked;
2242
2243	serial_driver.open = sab82532_open;
2244	serial_driver.close = sab82532_close;
2245	serial_driver.write = sab82532_write;
2246	serial_driver.put_char = sab82532_put_char;
2247	serial_driver.flush_chars = sab82532_flush_chars;
2248	serial_driver.write_room = sab82532_write_room;
2249	serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2250	serial_driver.flush_buffer = sab82532_flush_buffer;
2251	serial_driver.ioctl = sab82532_ioctl;
2252	serial_driver.throttle = sab82532_throttle;
2253	serial_driver.unthrottle = sab82532_unthrottle;
2254	serial_driver.send_xchar = sab82532_send_xchar;
2255	serial_driver.set_termios = sab82532_set_termios;
2256	serial_driver.stop = sab82532_stop;
2257	serial_driver.start = sab82532_start;
2258	serial_driver.hangup = sab82532_hangup;
2259	serial_driver.break_ctl = sab82532_break;
2260	serial_driver.wait_until_sent = sab82532_wait_until_sent;
2261	serial_driver.read_proc = sab82532_read_proc;
2262
2263	/*
2264	 * The callout device is just like normal device except for
2265	 * major number and the subtype code.
2266	 */
2267	callout_driver = serial_driver;
2268#ifdef CONFIG_DEVFS_FS
2269	callout_driver.name = "cua/%d";
2270#else
2271	callout_driver.name = "cua";
2272#endif
2273	callout_driver.major = TTYAUX_MAJOR;
2274	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2275	callout_driver.read_proc = 0;
2276	callout_driver.proc_entry = 0;
2277
2278	if (tty_register_driver(&serial_driver))
2279		panic("Couldn't register serial driver\n");
2280	if (tty_register_driver(&callout_driver))
2281		panic("Couldn't register callout driver\n");
2282
2283	for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2284		info->magic = SERIAL_MAGIC;
2285
2286		info->type = readb(&info->regs->r.vstr) & 0x0f;
2287		writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2288		writeb(0xff, &info->regs->w.pim);
2289		if (info->line == 0) {
2290			info->pvr_dsr_bit = (1 << 0);
2291			info->pvr_dtr_bit = (1 << 1);
2292		} else {
2293			info->pvr_dsr_bit = (1 << 3);
2294			info->pvr_dtr_bit = (1 << 2);
2295		}
2296		writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2297		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2298		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2299
2300		info->custom_divisor = 16;
2301		info->close_delay = 5*HZ/10;
2302		info->closing_wait = 30*HZ;
2303		info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2304		info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2305		info->x_char = 0;
2306		info->event = 0;
2307		info->blocked_open = 0;
2308		info->tqueue.routine = do_softint;
2309		info->tqueue.data = info;
2310		info->tqueue_hangup.routine = do_serial_hangup;
2311		info->tqueue_hangup.data = info;
2312		info->callout_termios = callout_driver.init_termios;
2313		info->normal_termios = serial_driver.init_termios;
2314		init_waitqueue_head(&info->open_wait);
2315		init_waitqueue_head(&info->close_wait);
2316		init_waitqueue_head(&info->delta_msr_wait);
2317		info->icount.cts = info->icount.dsr =
2318			info->icount.rng = info->icount.dcd = 0;
2319		info->icount.rx = info->icount.tx = 0;
2320		info->icount.frame = info->icount.parity = 0;
2321		info->icount.overrun = info->icount.brk = 0;
2322
2323		if (!(info->line & 0x01)) {
2324			if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2325					"serial(sab82532)", info)) {
2326				printk("sab82532: can't get IRQ %x\n",
2327				       info->irq);
2328				panic("sab82532 initialization failed");
2329			}
2330		}
2331
2332		printk(KERN_INFO
2333		       "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2334		       info->line + su_num_ports, (unsigned long)info->regs,
2335		       __irq_itoa(info->irq), sab82532_version[info->type]);
2336	}
2337
2338#ifdef SERIAL_LOG_DEVICE
2339	dprint_init(SERIAL_LOG_DEVICE);
2340#endif
2341	return 0;
2342}
2343
2344int __init sab82532_probe(void)
2345{
2346	int node, enode, snode;
2347	char model[32];
2348	int len;
2349
2350        node = prom_getchild(prom_root_node);
2351	node = prom_searchsiblings(node, "pci");
2352
2353	/*
2354	 * Check for SUNW,sabre on Ultra 5/10/AXi.
2355	 */
2356	len = prom_getproperty(node, "model", model, sizeof(model));
2357	if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2358        	node = prom_getchild(node);
2359		node = prom_searchsiblings(node, "pci");
2360	}
2361
2362	/*
2363	 * For each PCI bus...
2364	 */
2365	while (node) {
2366		enode = prom_getchild(node);
2367		enode = prom_searchsiblings(enode, "ebus");
2368
2369		/*
2370		 * For each EBus on this PCI...
2371		 */
2372		while (enode) {
2373			int child;
2374
2375			child = prom_getchild(enode);
2376			snode = prom_searchsiblings(child, "se");
2377			if (snode)
2378				goto found;
2379
2380			snode = prom_searchsiblings(child, "serial");
2381			if (snode) {
2382				char compat[32];
2383				int clen;
2384
2385				clen = prom_getproperty(snode, "compatible",
2386							compat, sizeof(compat));
2387				if (clen > 0) {
2388					if (strncmp(compat, "sab82532", 8) == 0)
2389						goto found;
2390				}
2391			}
2392
2393			enode = prom_getsibling(enode);
2394			enode = prom_searchsiblings(enode, "ebus");
2395		}
2396		node = prom_getsibling(node);
2397		node = prom_searchsiblings(node, "pci");
2398	}
2399	return -ENODEV;
2400
2401found:
2402#ifdef CONFIG_SERIAL_CONSOLE
2403	sunserial_setinitfunc(sab82532_console_init);
2404#endif
2405#ifndef MODULE
2406	sunserial_setinitfunc(sab82532_init);
2407	rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2408#endif
2409	return 0;
2410}
2411
2412#ifdef MODULE
2413MODULE_LICENSE("GPL");
2414
2415int init_module(void)
2416{
2417	if (get_sab82532(0))
2418		return -ENODEV;
2419
2420	return sab82532_init();
2421}
2422
2423void cleanup_module(void)
2424{
2425	struct sab82532 *sab;
2426	unsigned long flags;
2427	int e1, e2;
2428
2429	/* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2430	save_flags(flags);
2431	cli();
2432        remove_bh(SERIAL_BH);
2433	if ((e1 = tty_unregister_driver(&serial_driver)))
2434		printk("SERIAL: failed to unregister serial driver (%d)\n",
2435		       e1);
2436	if ((e2 = tty_unregister_driver(&callout_driver)))
2437		printk("SERIAL: failed to unregister callout driver (%d)\n",
2438		       e2);
2439	restore_flags(flags);
2440
2441	if (tmp_buf) {
2442		free_page((unsigned long) tmp_buf);
2443		tmp_buf = NULL;
2444	}
2445	for (sab = sab82532_chain; sab; sab = sab->next) {
2446		if (!(sab->line & 0x01))
2447			free_irq(sab->irq, sab);
2448		iounmap(sab->regs);
2449	}
2450}
2451#endif /* MODULE */
2452
2453#ifdef CONFIG_SERIAL_CONSOLE
2454static void
2455batten_down_hatches(struct sab82532 *info)
2456{
2457	unsigned char saved_rfc, tmp;
2458
2459	if (!stop_a_enabled)
2460		return;
2461
2462	/* If we are doing kadb, we call the debugger
2463	 * else we just drop into the boot monitor.
2464	 * Note that we must flush the user windows
2465	 * first before giving up control.
2466	 */
2467	printk("\n");
2468	flush_user_windows();
2469
2470	/*
2471	 * Set FIFO to single character mode.
2472	 */
2473	saved_rfc = readb(&info->regs->r.rfc);
2474	tmp = readb(&info->regs->rw.rfc);
2475	tmp &= ~(SAB82532_RFC_RFDF);
2476	writeb(tmp, &info->regs->rw.rfc);
2477	sab82532_cec_wait(info);
2478	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2479
2480#ifndef __sparc_v9__
2481	if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2482	    (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2483		sp_enter_debugger();
2484	else
2485#endif
2486		prom_cmdline();
2487
2488	/*
2489	 * Reset FIFO to character + status mode.
2490	 */
2491	writeb(saved_rfc, &info->regs->w.rfc);
2492	sab82532_cec_wait(info);
2493	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2494}
2495
2496static __inline__ void
2497sab82532_console_putchar(struct sab82532 *info, char c)
2498{
2499	unsigned long flags;
2500
2501	save_flags(flags); cli();
2502	sab82532_tec_wait(info);
2503	writeb(c, &info->regs->w.tic);
2504	restore_flags(flags);
2505}
2506
2507static void
2508sab82532_console_write(struct console *con, const char *s, unsigned n)
2509{
2510	struct sab82532 *info;
2511	int i;
2512
2513	info = sab82532_chain;
2514	for (i = con->index; i; i--) {
2515		info = info->next;
2516		if (!info)
2517			return;
2518	}
2519
2520	for (i = 0; i < n; i++) {
2521		if (*s == '\n')
2522			sab82532_console_putchar(info, '\r');
2523		sab82532_console_putchar(info, *s++);
2524	}
2525	sab82532_tec_wait(info);
2526}
2527
2528static kdev_t
2529sab82532_console_device(struct console *con)
2530{
2531	return MKDEV(TTY_MAJOR, 64 + con->index);
2532}
2533
2534static int
2535sab82532_console_setup(struct console *con, char *options)
2536{
2537	static struct tty_struct c_tty;
2538	static struct termios c_termios;
2539	struct sab82532 *info;
2540	tcflag_t	cflag;
2541	int		i;
2542
2543	info = sab82532_chain;
2544	for (i = con->index; i; i--) {
2545		info = info->next;
2546		if (!info)
2547			return -ENODEV;
2548	}
2549	info->is_console = 1;
2550
2551	/*
2552	 * Initialize the hardware
2553	 */
2554	sab82532_init_line(info);
2555
2556	/*
2557	 * Finally, enable interrupts
2558	 */
2559	info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2560				SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2561	writeb(info->interrupt_mask0, &info->regs->w.imr0);
2562	info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2563				SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2564				SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2565				SAB82532_IMR1_XPR;
2566	writeb(info->interrupt_mask1, &info->regs->w.imr1);
2567
2568	printk("Console: ttyS%d (SAB82532)\n", info->line);
2569
2570	sunserial_console_termios(con);
2571	cflag = con->cflag;
2572
2573	/*
2574	 * Fake up the tty and tty->termios structures so we can use
2575	 * change_speed (and eliminate a lot of duplicate code).
2576	 */
2577	if (!info->tty) {
2578		memset(&c_tty, 0, sizeof(c_tty));
2579		info->tty = &c_tty;
2580	}
2581	if (!info->tty->termios) {
2582		memset(&c_termios, 0, sizeof(c_termios));
2583		info->tty->termios = &c_termios;
2584	}
2585	info->tty->termios->c_cflag = con->cflag;
2586
2587	change_speed(info);
2588
2589	/* Now take out the pointers to static structures if necessary */
2590	if (info->tty->termios == &c_termios)
2591		info->tty->termios = 0;
2592	if (info->tty == &c_tty)
2593		info->tty = 0;
2594
2595	return 0;
2596}
2597
2598static struct console sab82532_console = {
2599	name:		"ttyS",
2600	write:		sab82532_console_write,
2601	device:		sab82532_console_device,
2602	setup:		sab82532_console_setup,
2603	flags:		CON_PRINTBUFFER,
2604	index:		-1,
2605};
2606
2607int __init sab82532_console_init(void)
2608{
2609	extern int con_is_present(void);
2610	extern int su_console_registered;
2611
2612	if (con_is_present() || su_console_registered)
2613		return 0;
2614
2615	if (!sab82532_chain) {
2616		prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2617		prom_halt();
2618	}
2619
2620	sab82532_console.index = serial_console - 1;
2621	register_console(&sab82532_console);
2622	return 0;
2623}
2624
2625#ifdef SERIAL_LOG_DEVICE
2626
2627static int serial_log_device = 0;
2628
2629static void
2630dprint_init(int tty)
2631{
2632	serial_console = tty + 1;
2633	sab82532_console.index = tty;
2634	sab82532_console_setup(&sab82532_console, "");
2635	serial_console = 0;
2636	serial_log_device = tty + 1;
2637}
2638
2639int
2640dprintf(const char *fmt, ...)
2641{
2642	static char buffer[4096];
2643	va_list args;
2644	int i;
2645
2646	if (!serial_log_device)
2647		return 0;
2648
2649	va_start(args, fmt);
2650	i = vsprintf(buffer, fmt, args);
2651	va_end(args);
2652	sab82532_console.write(&sab82532_console, buffer, i);
2653	return i;
2654}
2655#endif /* SERIAL_LOG_DEVICE */
2656#endif /* CONFIG_SERIAL_CONSOLE */
2657