1 /* $Id: sab82532.c,v 1.65 2001/10/13 08:27:50 davem 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 
45 static 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 
53 static struct tty_driver serial_driver, callout_driver;
54 static 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
76 static void dprint_init(int tty);
77 #endif
78 
79 static void change_speed(struct sab82532 *info);
80 static 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 
87 static struct sab82532 *sab82532_chain = 0;
88 static struct tty_struct *sab82532_table[NR_PORTS];
89 static struct termios *sab82532_termios[NR_PORTS];
90 static struct termios *sab82532_termios_locked[NR_PORTS];
91 
92 #ifdef MODULE
93 #undef CONFIG_SERIAL_CONSOLE
94 #endif
95 
96 #ifdef CONFIG_SERIAL_CONSOLE
97 extern int serial_console;
98 static struct console sab82532_console;
99 static int sab82532_console_init(void);
100 static 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 
107 static 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 };
113 static 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  */
124 static unsigned char *tmp_buf = 0;
125 static DECLARE_MUTEX(tmp_buf_sem);
126 
serial_paranoia_check(struct sab82532 * info,kdev_t device,const char * routine)127 static 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 
calc_ebrg(int baud,int * n_ret,int * m_ret)162 static 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 
sab82532_tec_wait(struct sab82532 * info)199 static __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 
sab82532_cec_wait(struct sab82532 * info)207 static __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 
sab82532_start_tx(struct sab82532 * info)215 static __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 
246 out:
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  */
sab82532_stop(struct tty_struct * tty)259 static 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 
sab82532_start(struct tty_struct * tty)273 static 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  */
sab82532_sched_event(struct sab82532 * info,int event)313 static 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 
receive_chars(struct sab82532 * info,union sab82532_irq_status * stat)320 static 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 
transmit_chars(struct sab82532 * info,union sab82532_irq_status * stat)399 static 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 
check_status(struct sab82532 * info,union sab82532_irq_status * stat)462 static 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 
499 check_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  */
sab82532_interrupt(int irq,void * dev_id,struct pt_regs * regs)578 static 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 
610 next:
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 
635 done:
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  */
do_serial_bh(void)657 static void do_serial_bh(void)
658 {
659 	run_task_queue(&tq_serial);
660 }
661 
do_softint(void * private_)662 static 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 		tty_wakeup(tty);
673 	}
674 }
675 
676 /*
677  * This routine is called from the scheduler tqueue when the interrupt
678  * routine has signalled that a hangup has occurred.  The path of
679  * hangup processing is:
680  *
681  * 	serial interrupt routine -> (scheduler tqueue) ->
682  * 	do_serial_hangup() -> tty->hangup() -> sab82532_hangup()
683  *
684  */
do_serial_hangup(void * private_)685 static void do_serial_hangup(void *private_)
686 {
687 	struct sab82532	*info = (struct sab82532 *) private_;
688 	struct tty_struct *tty;
689 
690 	tty = info->tty;
691 	if (tty)
692 		tty_hangup(tty);
693 	MOD_DEC_USE_COUNT;
694 }
695 
696 static void
sab82532_init_line(struct sab82532 * info)697 sab82532_init_line(struct sab82532 *info)
698 {
699 	unsigned char stat, tmp;
700 
701 	/*
702 	 * Wait for any commands or immediate characters
703 	 */
704 	sab82532_cec_wait(info);
705 	sab82532_tec_wait(info);
706 
707 	/*
708 	 * Clear the FIFO buffers.
709 	 */
710 	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
711 	sab82532_cec_wait(info);
712 	writeb(SAB82532_CMDR_XRES, &info->regs->w.cmdr);
713 
714 	/*
715 	 * Clear the interrupt registers.
716 	 */
717 	stat = readb(&info->regs->r.isr0);
718 	stat = readb(&info->regs->r.isr1);
719 
720 	/*
721 	 * Now, initialize the UART
722 	 */
723 	writeb(0, &info->regs->w.ccr0);				/* power-down */
724 	writeb(SAB82532_CCR0_MCE | SAB82532_CCR0_SC_NRZ |
725 	       SAB82532_CCR0_SM_ASYNC, &info->regs->w.ccr0);
726 	writeb(SAB82532_CCR1_ODS | SAB82532_CCR1_BCR | 7, &info->regs->w.ccr1);
727 	writeb(SAB82532_CCR2_BDF | SAB82532_CCR2_SSEL |
728 	       SAB82532_CCR2_TOE, &info->regs->w.ccr2);
729 	writeb(0, &info->regs->w.ccr3);
730 	writeb(SAB82532_CCR4_MCK4 | SAB82532_CCR4_EBRG, &info->regs->w.ccr4);
731 	writeb(SAB82532_MODE_RTS | SAB82532_MODE_FCTS |
732 	       SAB82532_MODE_RAC, &info->regs->w.mode);
733 	writeb(SAB82532_RFC_DPS | SAB82532_RFC_RFDF, &info->regs->w.rfc);
734 	switch (info->recv_fifo_size) {
735 		case 1:
736 			tmp = readb(&info->regs->w.rfc);
737 			tmp |= SAB82532_RFC_RFTH_1;
738 			writeb(tmp, &info->regs->w.rfc);
739 			break;
740 		case 4:
741 			tmp = readb(&info->regs->w.rfc);
742 			tmp |= SAB82532_RFC_RFTH_4;
743 			writeb(tmp, &info->regs->w.rfc);
744 			break;
745 		case 16:
746 			tmp = readb(&info->regs->w.rfc);
747 			tmp |= SAB82532_RFC_RFTH_16;
748 			writeb(tmp, &info->regs->w.rfc);
749 			break;
750 		default:
751 			info->recv_fifo_size = 32;
752 			/* fall through */
753 		case 32:
754 			tmp = readb(&info->regs->w.rfc);
755 			tmp |= SAB82532_RFC_RFTH_32;
756 			writeb(tmp, &info->regs->w.rfc);
757 			break;
758 	}
759 	tmp = readb(&info->regs->rw.ccr0);
760 	tmp |= SAB82532_CCR0_PU;	/* power-up */
761 	writeb(tmp, &info->regs->rw.ccr0);
762 }
763 
startup(struct sab82532 * info)764 static int startup(struct sab82532 *info)
765 {
766 	unsigned long flags;
767 	unsigned long page;
768 	int retval = 0;
769 
770 	page = get_free_page(GFP_KERNEL);
771 	if (!page)
772 		return -ENOMEM;
773 
774 	save_flags(flags); cli();
775 
776 	if (info->flags & ASYNC_INITIALIZED) {
777 		free_page(page);
778 		goto errout;
779 	}
780 
781 	if (!info->regs) {
782 		if (info->tty)
783 			set_bit(TTY_IO_ERROR, &info->tty->flags);
784 		free_page(page);
785 		retval = -ENODEV;
786 		goto errout;
787 	}
788 	if (info->xmit.buf)
789 		free_page(page);
790 	else
791 		info->xmit.buf = (unsigned char *)page;
792 
793 #ifdef SERIAL_DEBUG_OPEN
794 	printk("starting up serial port %d...", info->line);
795 #endif
796 
797 	/*
798 	 * Initialize the Hardware
799 	 */
800 	sab82532_init_line(info);
801 
802 	if (info->tty->termios->c_cflag & CBAUD) {
803 		u8 tmp;
804 
805 		tmp = readb(&info->regs->rw.mode);
806 		tmp &= ~(SAB82532_MODE_FRTS);
807 		tmp |= SAB82532_MODE_RTS;
808 		writeb(tmp, &info->regs->rw.mode);
809 
810 		tmp = readb(&info->regs->rw.pvr);
811 		tmp &= ~(info->pvr_dtr_bit);
812 		writeb(tmp, &info->regs->rw.pvr);
813 	}
814 
815 	/*
816 	 * Finally, enable interrupts
817 	 */
818 	info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
819 				SAB82532_IMR0_PLLA;
820 	writeb(info->interrupt_mask0, &info->regs->w.imr0);
821 	info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
822 				SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
823 				SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
824 				SAB82532_IMR1_XPR;
825 	writeb(info->interrupt_mask1, &info->regs->w.imr1);
826 	set_bit(SAB82532_ALLS, &info->irqflags);
827 
828 	if (info->tty)
829 		clear_bit(TTY_IO_ERROR, &info->tty->flags);
830 	info->xmit.head = info->xmit.tail = 0;
831 
832 	set_bit(SAB82532_XPR, &info->irqflags);
833 
834 	/*
835 	 * and set the speed of the serial port
836 	 */
837 	change_speed(info);
838 
839 	info->flags |= ASYNC_INITIALIZED;
840 	restore_flags(flags);
841 	return 0;
842 
843 errout:
844 	restore_flags(flags);
845 	return retval;
846 }
847 
848 /*
849  * This routine will shutdown a serial port; interrupts are disabled, and
850  * DTR is dropped if the hangup on close termio flag is on.
851  */
shutdown(struct sab82532 * info)852 static void shutdown(struct sab82532 *info)
853 {
854 	unsigned long flags;
855 	u8 tmp;
856 
857 	if (!(info->flags & ASYNC_INITIALIZED))
858 		return;
859 
860 #ifdef SERIAL_DEBUG_OPEN
861 	printk("Shutting down serial port %d...", info->line);
862 #endif
863 
864 	save_flags(flags); cli(); /* Disable interrupts */
865 
866 	/*
867 	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
868 	 * here so the queue might never be waken up
869 	 */
870 	wake_up_interruptible(&info->delta_msr_wait);
871 
872 	if (info->xmit.buf) {
873 		free_page((unsigned long)info->xmit.buf);
874 		info->xmit.buf = 0;
875 	}
876 
877 #ifdef CONFIG_SERIAL_CONSOLE
878 	if (info->is_console) {
879 		info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
880 					SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
881 		writeb(info->interrupt_mask0, &info->regs->w.imr0);
882 		info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
883 					SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
884 					SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
885 					SAB82532_IMR1_XPR;
886 		writeb(info->interrupt_mask1, &info->regs->w.imr1);
887 		if (info->tty)
888 			set_bit(TTY_IO_ERROR, &info->tty->flags);
889 		info->flags &= ~ASYNC_INITIALIZED;
890 		restore_flags(flags);
891 		return;
892 	}
893 #endif
894 
895 	/* Disable Interrupts */
896 	info->interrupt_mask0 = 0xff;
897 	writeb(info->interrupt_mask0, &info->regs->w.imr0);
898 	info->interrupt_mask1 = 0xff;
899 	writeb(info->interrupt_mask1, &info->regs->w.imr1);
900 
901 	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
902 		tmp = readb(&info->regs->r.mode);
903 		tmp |= (SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
904 		writeb(tmp, &info->regs->rw.mode);
905 		writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit,
906 		       &info->regs->rw.pvr);
907 	}
908 
909 	/* Disable break condition */
910 	tmp = readb(&info->regs->rw.dafo);
911 	tmp &= ~(SAB82532_DAFO_XBRK);
912 	writeb(tmp, &info->regs->rw.dafo);
913 
914 	/* Disable Receiver */
915 	tmp = readb(&info->regs->rw.mode);
916 	tmp &= ~(SAB82532_MODE_RAC);
917 	writeb(tmp, &info->regs->rw.mode);
918 
919 	/* Power Down */
920 	tmp = readb(&info->regs->rw.ccr0);
921 	tmp &= ~(SAB82532_CCR0_PU);
922 	writeb(tmp, &info->regs->rw.ccr0);
923 
924 	if (info->tty)
925 		set_bit(TTY_IO_ERROR, &info->tty->flags);
926 
927 	info->flags &= ~ASYNC_INITIALIZED;
928 	restore_flags(flags);
929 }
930 
931 /*
932  * This routine is called to set the UART divisor registers to match
933  * the specified baud rate for a serial port.
934  */
change_speed(struct sab82532 * info)935 static void change_speed(struct sab82532 *info)
936 {
937 	unsigned long	flags;
938 	unsigned int	ebrg;
939 	tcflag_t	cflag;
940 	unsigned char	dafo;
941 	int		bits, n, m;
942 
943 	if (!info->tty || !info->tty->termios)
944 		return;
945 	cflag = info->tty->termios->c_cflag;
946 
947 	/* Byte size and parity */
948 	switch (cflag & CSIZE) {
949 	      case CS5: dafo = SAB82532_DAFO_CHL5; bits = 7; break;
950 	      case CS6: dafo = SAB82532_DAFO_CHL6; bits = 8; break;
951 	      case CS7: dafo = SAB82532_DAFO_CHL7; bits = 9; break;
952 	      case CS8: dafo = SAB82532_DAFO_CHL8; bits = 10; break;
953 	      /* Never happens, but GCC is too dumb to figure it out */
954 	      default:  dafo = SAB82532_DAFO_CHL5; bits = 7; break;
955 	}
956 
957 	if (cflag & CSTOPB) {
958 		dafo |= SAB82532_DAFO_STOP;
959 		bits++;
960 	}
961 
962 	if (cflag & PARENB) {
963 		dafo |= SAB82532_DAFO_PARE;
964 		bits++;
965 	}
966 
967 	if (cflag & PARODD) {
968 #ifdef CMSPAR
969 		if (cflag & CMSPAR)
970 			dafo |= SAB82532_DAFO_PAR_MARK;
971 		else
972 #endif
973 			dafo |= SAB82532_DAFO_PAR_ODD;
974 	} else {
975 #ifdef CMSPAR
976 		if (cflag & CMSPAR)
977 			dafo |= SAB82532_DAFO_PAR_SPACE;
978 		else
979 #endif
980 			dafo |= SAB82532_DAFO_PAR_EVEN;
981 	}
982 
983 	/* Determine EBRG values based on baud rate */
984 	info->baud = tty_get_baud_rate(info->tty);
985 	calc_ebrg(info->baud, &n, &m);
986 
987 	ebrg = n | (m << 6);
988 
989 	if (info->baud) {
990 		info->timeout = (info->xmit_fifo_size * HZ * bits) / info->baud;
991 		info->tec_timeout = (10 * 1000000) / info->baud;
992 		info->cec_timeout = info->tec_timeout >> 2;
993 	} else {
994 		info->timeout = 0;
995 		info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
996 		info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
997 	}
998 	info->timeout += HZ / 50;		/* Add .02 seconds of slop */
999 
1000 	/* CTS flow control flags */
1001 	if (cflag & CRTSCTS)
1002 		info->flags |= ASYNC_CTS_FLOW;
1003 	else
1004 		info->flags &= ~(ASYNC_CTS_FLOW);
1005 
1006 	if (cflag & CLOCAL)
1007 		info->flags &= ~(ASYNC_CHECK_CD);
1008 	else
1009 		info->flags |= ASYNC_CHECK_CD;
1010 	if (info->tty)
1011 		info->tty->hw_stopped = 0;
1012 
1013 	/*
1014 	 * Set up parity check flag
1015 	 * XXX: not implemented, yet.
1016 	 */
1017 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1018 
1019 	/*
1020 	 * Characters to ignore
1021 	 * XXX: not implemented, yet.
1022 	 */
1023 
1024 	/*
1025 	 * !!! ignore all characters if CREAD is not set
1026 	 * XXX: not implemented, yet.
1027 	 */
1028 	if ((cflag & CREAD) == 0)
1029 		info->ignore_status_mask |= SAB82532_ISR0_RPF |
1030 					    SAB82532_ISR0_TCD |
1031 					    SAB82532_ISR0_TIME;
1032 
1033 	save_flags(flags); cli();
1034 	sab82532_cec_wait(info);
1035 	sab82532_tec_wait(info);
1036 	writeb(dafo, &info->regs->w.dafo);
1037 	writeb(ebrg & 0xff, &info->regs->w.bgr);
1038 	writeb(readb(&info->regs->rw.ccr2) & ~(0xc0), &info->regs->rw.ccr2);
1039 	writeb(readb(&info->regs->rw.ccr2) | ((ebrg >> 2) & 0xc0), &info->regs->rw.ccr2);
1040 	if (info->flags & ASYNC_CTS_FLOW) {
1041 		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1042 		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1043 		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FCTS), &info->regs->rw.mode);
1044 		info->interrupt_mask1 &= ~(SAB82532_IMR1_CSC);
1045 		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1046 	} else {
1047 		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1048 		writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1049 		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FCTS, &info->regs->rw.mode);
1050 		info->interrupt_mask1 |= SAB82532_IMR1_CSC;
1051 		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1052 	}
1053 	writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RAC, &info->regs->rw.mode);
1054 	restore_flags(flags);
1055 }
1056 
sab82532_put_char(struct tty_struct * tty,unsigned char ch)1057 static void sab82532_put_char(struct tty_struct *tty, unsigned char ch)
1058 {
1059 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1060 	unsigned long flags;
1061 
1062 	if (serial_paranoia_check(info, tty->device, "sab82532_put_char"))
1063 		return;
1064 
1065 	if (!tty || !info->xmit.buf)
1066 		return;
1067 
1068 	save_flags(flags); cli();
1069 	if (!CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE)) {
1070 		restore_flags(flags);
1071 		return;
1072 	}
1073 
1074 	info->xmit.buf[info->xmit.head] = ch;
1075 	info->xmit.head = (info->xmit.head + 1) & (SERIAL_XMIT_SIZE-1);
1076 	restore_flags(flags);
1077 }
1078 
sab82532_flush_chars(struct tty_struct * tty)1079 static void sab82532_flush_chars(struct tty_struct *tty)
1080 {
1081 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1082 	unsigned long flags;
1083 
1084 	if (serial_paranoia_check(info, tty->device, "sab82532_flush_chars"))
1085 		return;
1086 
1087 	if ((info->xmit.head == info->xmit.tail) ||
1088 	    tty->stopped || tty->hw_stopped || !info->xmit.buf)
1089 		return;
1090 
1091 	save_flags(flags); cli();
1092 	info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1093 	writeb(info->interrupt_mask1, &info->regs->w.imr1);
1094 	sab82532_start_tx(info);
1095 	restore_flags(flags);
1096 }
1097 
sab82532_write(struct tty_struct * tty,int from_user,const unsigned char * buf,int count)1098 static int sab82532_write(struct tty_struct * tty, int from_user,
1099 			  const unsigned char *buf, int count)
1100 {
1101 	int c, ret = 0;
1102 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1103 	unsigned long flags;
1104 
1105 	if (serial_paranoia_check(info, tty->device, "sab82532_write"))
1106 		return 0;
1107 
1108 	if (!tty || !info->xmit.buf || !tmp_buf)
1109 		return 0;
1110 
1111 	save_flags(flags);
1112 	if (from_user) {
1113 		down(&tmp_buf_sem);
1114 		while (1) {
1115 			int c1;
1116 			c = CIRC_SPACE_TO_END(info->xmit.head,
1117 					      info->xmit.tail,
1118 					      SERIAL_XMIT_SIZE);
1119 			if (count < c)
1120 				c = count;
1121 			if (c <= 0)
1122 				break;
1123 
1124 			c -= copy_from_user(tmp_buf, buf, c);
1125 			if (!c) {
1126 				if (!ret)
1127 					ret = -EFAULT;
1128 				break;
1129 			}
1130 			cli();
1131 			c1 = CIRC_SPACE_TO_END(info->xmit.head,
1132 					       info->xmit.tail,
1133 					       SERIAL_XMIT_SIZE);
1134 			if (c1 < c)
1135 				c = c1;
1136 			memcpy(info->xmit.buf + info->xmit.head, tmp_buf, c);
1137 			info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1138 			restore_flags(flags);
1139 			buf += c;
1140 			count -= c;
1141 			ret += c;
1142 		}
1143 		up(&tmp_buf_sem);
1144 	} else {
1145 		cli();
1146 		while (1) {
1147 			c = CIRC_SPACE_TO_END(info->xmit.head,
1148 					      info->xmit.tail,
1149 					      SERIAL_XMIT_SIZE);
1150 			if (count < c)
1151 				c = count;
1152 			if (c <= 0)
1153 				break;
1154 			memcpy(info->xmit.buf + info->xmit.head, buf, c);
1155 			info->xmit.head = (info->xmit.head + c) & (SERIAL_XMIT_SIZE-1);
1156 			buf += c;
1157 			count -= c;
1158 			ret += c;
1159 		}
1160 		restore_flags(flags);
1161 	}
1162 
1163 	if ((info->xmit.head != info->xmit.tail) &&
1164 	    !tty->stopped && !tty->hw_stopped) {
1165 		info->interrupt_mask1 &= ~(SAB82532_IMR1_XPR);
1166 		writeb(info->interrupt_mask1, &info->regs->w.imr1);
1167 		sab82532_start_tx(info);
1168 	}
1169 
1170 	restore_flags(flags);
1171 	return ret;
1172 }
1173 
sab82532_write_room(struct tty_struct * tty)1174 static int sab82532_write_room(struct tty_struct *tty)
1175 {
1176 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1177 
1178 	if (serial_paranoia_check(info, tty->device, "sab82532_write_room"))
1179 		return 0;
1180 
1181 	return CIRC_SPACE(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1182 }
1183 
sab82532_chars_in_buffer(struct tty_struct * tty)1184 static int sab82532_chars_in_buffer(struct tty_struct *tty)
1185 {
1186 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1187 
1188 	if (serial_paranoia_check(info, tty->device, "sab82532_chars_in_buffer"))
1189 		return 0;
1190 
1191 	return CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE);
1192 }
1193 
sab82532_flush_buffer(struct tty_struct * tty)1194 static void sab82532_flush_buffer(struct tty_struct *tty)
1195 {
1196 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1197 	unsigned long flags;
1198 
1199 	if (serial_paranoia_check(info, tty->device, "sab82532_flush_buffer"))
1200 		return;
1201 
1202 	save_flags(flags); cli();
1203 	info->xmit.head = info->xmit.tail = 0;
1204 	restore_flags(flags);
1205 
1206 	tty_wakeup(tty);
1207 }
1208 
1209 /*
1210  * This function is used to send a high-priority XON/XOFF character to
1211  * the device
1212  */
sab82532_send_xchar(struct tty_struct * tty,char ch)1213 static void sab82532_send_xchar(struct tty_struct *tty, char ch)
1214 {
1215 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1216 	unsigned long flags;
1217 
1218 	if (serial_paranoia_check(info, tty->device, "sab82532_send_xchar"))
1219 		return;
1220 
1221 	save_flags(flags); cli();
1222 	sab82532_tec_wait(info);
1223 	writeb(ch, &info->regs->w.tic);
1224 	restore_flags(flags);
1225 }
1226 
1227 /*
1228  * ------------------------------------------------------------
1229  * sab82532_throttle()
1230  *
1231  * This routine is called by the upper-layer tty layer to signal that
1232  * incoming characters should be throttled.
1233  * ------------------------------------------------------------
1234  */
sab82532_throttle(struct tty_struct * tty)1235 static void sab82532_throttle(struct tty_struct * tty)
1236 {
1237 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1238 #ifdef SERIAL_DEBUG_THROTTLE
1239 	char	buf[64];
1240 
1241 	printk("throttle %s: %d....\n", _tty_name(tty, buf),
1242 	       tty->ldisc.chars_in_buffer(tty));
1243 #endif
1244 
1245 	if (serial_paranoia_check(info, tty->device, "sab82532_throttle"))
1246 		return;
1247 
1248 	if (I_IXOFF(tty))
1249 		sab82532_send_xchar(tty, STOP_CHAR(tty));
1250 
1251 	if (tty->termios->c_cflag & CRTSCTS) {
1252 		u8 mode = readb(&info->regs->r.mode);
1253 		mode &= ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS);
1254 		writeb(mode, &info->regs->w.mode);
1255 	}
1256 }
1257 
sab82532_unthrottle(struct tty_struct * tty)1258 static void sab82532_unthrottle(struct tty_struct * tty)
1259 {
1260 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1261 #ifdef SERIAL_DEBUG_THROTTLE
1262 	char	buf[64];
1263 
1264 	printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1265 	       tty->ldisc.chars_in_buffer(tty));
1266 #endif
1267 
1268 	if (serial_paranoia_check(info, tty->device, "sab82532_unthrottle"))
1269 		return;
1270 
1271 	if (I_IXOFF(tty)) {
1272 		if (info->x_char)
1273 			info->x_char = 0;
1274 		else
1275 			sab82532_send_xchar(tty, START_CHAR(tty));
1276 	}
1277 
1278 	if (tty->termios->c_cflag & CRTSCTS) {
1279 		u8 mode = readb(&info->regs->r.mode);
1280 		mode &= ~(SAB82532_MODE_RTS);
1281 		mode |= SAB82532_MODE_FRTS;
1282 		writeb(mode, &info->regs->w.mode);
1283 	}
1284 }
1285 
1286 /*
1287  * ------------------------------------------------------------
1288  * sab82532_ioctl() and friends
1289  * ------------------------------------------------------------
1290  */
1291 
get_serial_info(struct sab82532 * info,struct serial_struct * retinfo)1292 static int get_serial_info(struct sab82532 *info,
1293 			   struct serial_struct *retinfo)
1294 {
1295 	struct serial_struct tmp;
1296 
1297 	if (!retinfo)
1298 		return -EFAULT;
1299 	memset(&tmp, 0, sizeof(tmp));
1300 	tmp.type = info->type;
1301 	tmp.line = info->line;
1302 	tmp.port = (unsigned long)info->regs;
1303 	tmp.irq = info->irq;
1304 	tmp.flags = info->flags;
1305 	tmp.xmit_fifo_size = info->xmit_fifo_size;
1306 	tmp.baud_base = info->baud_base;
1307 	tmp.close_delay = info->close_delay;
1308 	tmp.closing_wait = info->closing_wait;
1309 	tmp.custom_divisor = info->custom_divisor;
1310 	tmp.hub6 = 0;
1311 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1312 		return -EFAULT;
1313 	return 0;
1314 }
1315 
set_serial_info(struct sab82532 * info,struct serial_struct * new_info)1316 static int set_serial_info(struct sab82532 *info,
1317 			   struct serial_struct *new_info)
1318 {
1319 	return 0;
1320 }
1321 
1322 
1323 /*
1324  * get_lsr_info - get line status register info
1325  *
1326  * Purpose: Let user call ioctl() to get info when the UART physically
1327  * 	    is emptied.  On bus types like RS485, the transmitter must
1328  * 	    release the bus after transmitting. This must be done when
1329  * 	    the transmit shift register is empty, not be done when the
1330  * 	    transmit holding register is empty.  This functionality
1331  * 	    allows an RS485 driver to be written in user space.
1332  */
get_lsr_info(struct sab82532 * info,unsigned int * value)1333 static int get_lsr_info(struct sab82532 * info, unsigned int *value)
1334 {
1335 	unsigned int result;
1336 
1337 	result = (!info->xmit.buf && test_bit(SAB82532_ALLS, &info->irqflags))
1338 							? TIOCSER_TEMT : 0;
1339 	return put_user(result, value);
1340 }
1341 
1342 
get_modem_info(struct sab82532 * info,unsigned int * value)1343 static int get_modem_info(struct sab82532 * info, unsigned int *value)
1344 {
1345 	unsigned int result;
1346 
1347 	result =  ((readb(&info->regs->r.mode) & SAB82532_MODE_RTS) ?
1348 		    ((readb(&info->regs->r.mode) & SAB82532_MODE_FRTS) ? 0 : TIOCM_RTS)
1349 							    : TIOCM_RTS)
1350 		| ((readb(&info->regs->r.pvr) & info->pvr_dtr_bit) ? 0 : TIOCM_DTR)
1351 		| ((readb(&info->regs->r.vstr) & SAB82532_VSTR_CD) ? 0 : TIOCM_CAR)
1352 		| ((readb(&info->regs->r.pvr) & info->pvr_dsr_bit) ? 0 : TIOCM_DSR)
1353 		| ((readb(&info->regs->r.star) & SAB82532_STAR_CTS) ? TIOCM_CTS : 0);
1354 	return put_user(result,value);
1355 }
1356 
set_modem_info(struct sab82532 * info,unsigned int cmd,unsigned int * value)1357 static int set_modem_info(struct sab82532 * info, unsigned int cmd,
1358 			  unsigned int *value)
1359 {
1360 	unsigned int arg;
1361 
1362 	if (get_user(arg, value))
1363 		return -EFAULT;
1364 	switch (cmd) {
1365 	case TIOCMBIS:
1366 		if (arg & TIOCM_RTS) {
1367 			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1368 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1369 		}
1370 		if (arg & TIOCM_DTR) {
1371 			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1372 		}
1373 		break;
1374 	case TIOCMBIC:
1375 		if (arg & TIOCM_RTS) {
1376 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1377 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1378 		}
1379 		if (arg & TIOCM_DTR) {
1380 			writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1381 		}
1382 		break;
1383 	case TIOCMSET:
1384 		if (arg & TIOCM_RTS) {
1385 			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_FRTS), &info->regs->rw.mode);
1386 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1387 		} else {
1388 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1389 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
1390 		}
1391 		if (arg & TIOCM_DTR) {
1392 			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1393 		} else {
1394 			writeb(readb(&info->regs->rw.pvr) | info->pvr_dtr_bit, &info->regs->rw.pvr);
1395 		}
1396 		break;
1397 	default:
1398 		return -EINVAL;
1399 	}
1400 	return 0;
1401 }
1402 
1403 /*
1404  * This routine sends a break character out the serial port.
1405  */
sab82532_break(struct tty_struct * tty,int break_state)1406 static void sab82532_break(struct tty_struct *tty, int break_state)
1407 {
1408 	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1409 	unsigned long flags;
1410 
1411 	if (serial_paranoia_check(info, tty->device, "sab82532_break"))
1412 		return;
1413 
1414 	if (!info->regs)
1415 		return;
1416 
1417 #ifdef SERIAL_DEBUG_SEND_BREAK
1418 	printk("sab82532_break(%d) jiff=%lu...", break_state, jiffies);
1419 #endif
1420 	save_flags(flags); cli();
1421 	if (break_state == -1)
1422 		writeb(readb(&info->regs->rw.dafo) | SAB82532_DAFO_XBRK, &info->regs->rw.dafo);
1423 	else
1424 		writeb(readb(&info->regs->rw.dafo) & ~(SAB82532_DAFO_XBRK), &info->regs->rw.dafo);
1425 	restore_flags(flags);
1426 }
1427 
1428 
sab82532_ioctl(struct tty_struct * tty,struct file * file,unsigned int cmd,unsigned long arg)1429 static int sab82532_ioctl(struct tty_struct *tty, struct file * file,
1430 		    unsigned int cmd, unsigned long arg)
1431 {
1432 	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1433 	struct async_icount cprev, cnow;	/* kernel counter temps */
1434 	struct serial_icounter_struct *p_cuser;	/* user space */
1435 
1436 	if (serial_paranoia_check(info, tty->device, "sab82532_ioctl"))
1437 		return -ENODEV;
1438 
1439 	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1440 	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1441 	    (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT) &&
1442 	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1443 		if (tty->flags & (1 << TTY_IO_ERROR))
1444 		    return -EIO;
1445 	}
1446 
1447 	switch (cmd) {
1448 		case TIOCGSOFTCAR:
1449 			return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1450 		case TIOCSSOFTCAR:
1451 			if (get_user(arg, (unsigned int *) arg))
1452 				return -EFAULT;
1453 			tty->termios->c_cflag =
1454 				((tty->termios->c_cflag & ~CLOCAL) |
1455 				 (arg ? CLOCAL : 0));
1456 			return 0;
1457 		case TIOCMGET:
1458 			return get_modem_info(info, (unsigned int *) arg);
1459 		case TIOCMBIS:
1460 		case TIOCMBIC:
1461 		case TIOCMSET:
1462 			return set_modem_info(info, cmd, (unsigned int *) arg);
1463 		case TIOCGSERIAL:
1464 			return get_serial_info(info,
1465 					       (struct serial_struct *) arg);
1466 		case TIOCSSERIAL:
1467 			return set_serial_info(info,
1468 					       (struct serial_struct *) arg);
1469 
1470 		case TIOCSERGETLSR: /* Get line status register */
1471 			return get_lsr_info(info, (unsigned int *) arg);
1472 
1473 		case TIOCSERGSTRUCT:
1474 			if (copy_to_user((struct sab82532 *) arg,
1475 					 info, sizeof(struct sab82532)))
1476 				return -EFAULT;
1477 			return 0;
1478 
1479 		/*
1480 		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1481 		 * - mask passed in arg for lines of interest
1482  		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1483 		 * Caller should use TIOCGICOUNT to see which one it was
1484 		 */
1485 		 case TIOCMIWAIT:
1486 			cli();
1487 			/* note the counters on entry */
1488 			cprev = info->icount;
1489 			sti();
1490 			while (1) {
1491 				interruptible_sleep_on(&info->delta_msr_wait);
1492 				/* see if a signal did it */
1493 				if (signal_pending(current))
1494 					return -ERESTARTSYS;
1495 				cli();
1496 				cnow = info->icount; /* atomic copy */
1497 				sti();
1498 				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1499 				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1500 					return -EIO; /* no change => error */
1501 				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1502 				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1503 				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1504 				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1505 					return 0;
1506 				}
1507 				cprev = cnow;
1508 			}
1509 			/* NOTREACHED */
1510 
1511 		/*
1512 		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1513 		 * Return: write counters to the user passed counter struct
1514 		 * NB: both 1->0 and 0->1 transitions are counted except for
1515 		 *     RI where only 0->1 is counted.
1516 		 */
1517 		case TIOCGICOUNT:
1518 			cli();
1519 			cnow = info->icount;
1520 			sti();
1521 			p_cuser = (struct serial_icounter_struct *) arg;
1522 			if (put_user(cnow.cts, &p_cuser->cts) ||
1523 			    put_user(cnow.dsr, &p_cuser->dsr) ||
1524 			    put_user(cnow.rng, &p_cuser->rng) ||
1525 			    put_user(cnow.dcd, &p_cuser->dcd))
1526 				return -EFAULT;
1527 			return 0;
1528 
1529 		default:
1530 			return -ENOIOCTLCMD;
1531 		}
1532 	return 0;
1533 }
1534 
sab82532_set_termios(struct tty_struct * tty,struct termios * old_termios)1535 static void sab82532_set_termios(struct tty_struct *tty,
1536 				 struct termios *old_termios)
1537 {
1538 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1539 
1540 	if (   (tty->termios->c_cflag == old_termios->c_cflag)
1541 	    && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1542 		== RELEVANT_IFLAG(old_termios->c_iflag)))
1543 	  return;
1544 
1545 	change_speed(info);
1546 
1547 	/* Handle transition to B0 status */
1548 	if ((old_termios->c_cflag & CBAUD) &&
1549 	    !(tty->termios->c_cflag & CBAUD)) {
1550 		writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1551 		writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1552 		writeb(readb(&info->regs->w.pvr) | info->pvr_dtr_bit, &info->regs->w.pvr);
1553 	}
1554 
1555 	/* Handle transition away from B0 status */
1556 	if (!(old_termios->c_cflag & CBAUD) &&
1557 	    (tty->termios->c_cflag & CBAUD)) {
1558 		writeb(readb(&info->regs->w.pvr) & ~(info->pvr_dtr_bit), &info->regs->w.pvr);
1559 		if (tty->termios->c_cflag & CRTSCTS) {
1560 			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_RTS), &info->regs->w.mode);
1561 			writeb(readb(&info->regs->w.mode) | SAB82532_MODE_FRTS, &info->regs->w.mode);
1562 		} else if (test_bit(TTY_THROTTLED, &tty->flags)) {
1563 			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS | SAB82532_MODE_RTS), &info->regs->w.mode);
1564 		} else {
1565 			writeb(readb(&info->regs->w.mode) & ~(SAB82532_MODE_FRTS), &info->regs->w.mode);
1566 			writeb(readb(&info->regs->w.mode) | SAB82532_MODE_RTS, &info->regs->w.mode);
1567 		}
1568 	}
1569 
1570 	/* Handle turning off CRTSCTS */
1571 	if ((old_termios->c_cflag & CRTSCTS) &&
1572 	    !(tty->termios->c_cflag & CRTSCTS)) {
1573 		tty->hw_stopped = 0;
1574 		sab82532_start(tty);
1575 	}
1576 }
1577 
1578 /*
1579  * ------------------------------------------------------------
1580  * sab82532_close()
1581  *
1582  * This routine is called when the serial port gets closed.  First, we
1583  * wait for the last remaining data to be sent.  Then, we unlink its
1584  * async structure from the interrupt chain if necessary, and we free
1585  * that IRQ if nothing is left in the chain.
1586  * ------------------------------------------------------------
1587  */
sab82532_close(struct tty_struct * tty,struct file * filp)1588 static void sab82532_close(struct tty_struct *tty, struct file * filp)
1589 {
1590 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1591 	unsigned long flags;
1592 
1593 	if (!info || serial_paranoia_check(info, tty->device, "sab82532_close"))
1594 		return;
1595 
1596 	save_flags(flags); cli();
1597 
1598 	if (tty_hung_up_p(filp)) {
1599 		MOD_DEC_USE_COUNT;
1600 		restore_flags(flags);
1601 		return;
1602 	}
1603 
1604 #ifdef SERIAL_DEBUG_OPEN
1605 	printk("sab82532_close ttys%d, count = %d\n", info->line, info->count);
1606 #endif
1607 	if ((tty->count == 1) && (info->count != 1)) {
1608 		/*
1609 		 * Uh, oh.  tty->count is 1, which means that the tty
1610 		 * structure will be freed.  info->count should always
1611 		 * be one in these conditions.  If it's greater than
1612 		 * one, we've got real problems, since it means the
1613 		 * serial port won't be shutdown.
1614 		 */
1615 		printk("sab82532_close: bad serial port count; tty->count is 1,"
1616 		       " info->count is %d\n", info->count);
1617 		info->count = 1;
1618 	}
1619 	if (--info->count < 0) {
1620 		printk("sab82532_close: bad serial port count for ttys%d: %d\n",
1621 		       info->line, info->count);
1622 		info->count = 0;
1623 	}
1624 	if (info->count) {
1625 		MOD_DEC_USE_COUNT;
1626 		restore_flags(flags);
1627 		return;
1628 	}
1629 	info->flags |= ASYNC_CLOSING;
1630 	/*
1631 	 * Save the termios structure, since this port may have
1632 	 * separate termios for callout and dialin.
1633 	 */
1634 	if (info->flags & ASYNC_NORMAL_ACTIVE)
1635 		info->normal_termios = *tty->termios;
1636 	if (info->flags & ASYNC_CALLOUT_ACTIVE)
1637 		info->callout_termios = *tty->termios;
1638 	/*
1639 	 * Now we wait for the transmit buffer to clear; and we notify
1640 	 * the line discipline to only process XON/XOFF characters.
1641 	 */
1642 	tty->closing = 1;
1643 	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1644 		tty_wait_until_sent(tty, info->closing_wait);
1645 
1646 	/*
1647 	 * At this point we stop accepting input.  To do this, we
1648 	 * disable the receive line status interrupts, and turn off
1649 	 * the receiver.
1650 	 */
1651 	info->interrupt_mask0 |= SAB82532_IMR0_TCD;
1652 	writeb(info->interrupt_mask0, &info->regs->w.imr0);
1653 	if (info->flags & ASYNC_INITIALIZED) {
1654 		/*
1655 		 * Before we drop DTR, make sure the UART transmitter
1656 		 * has completely drained; this is especially
1657 		 * important if there is a transmit FIFO!
1658 		 */
1659 		sab82532_wait_until_sent(tty, info->timeout);
1660 	}
1661 	shutdown(info);
1662 	if (tty->driver.flush_buffer)
1663 		tty->driver.flush_buffer(tty);
1664 	tty_ldisc_flush(tty);
1665 	tty->closing = 0;
1666 	info->event = 0;
1667 	info->tty = 0;
1668 	if (info->blocked_open) {
1669 		if (info->close_delay) {
1670 			current->state = TASK_INTERRUPTIBLE;
1671 			schedule_timeout(info->close_delay);
1672 		}
1673 		wake_up_interruptible(&info->open_wait);
1674 	}
1675 	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1676 			 ASYNC_CLOSING);
1677 	wake_up_interruptible(&info->close_wait);
1678 	MOD_DEC_USE_COUNT;
1679 	restore_flags(flags);
1680 }
1681 
1682 /*
1683  * sab82532_wait_until_sent() --- wait until the transmitter is empty
1684  */
sab82532_wait_until_sent(struct tty_struct * tty,int timeout)1685 static void sab82532_wait_until_sent(struct tty_struct *tty, int timeout)
1686 {
1687 	struct sab82532 *info = (struct sab82532 *)tty->driver_data;
1688 	unsigned long orig_jiffies, char_time;
1689 
1690 	if (serial_paranoia_check(info,tty->device,"sab82532_wait_until_sent"))
1691 		return;
1692 
1693 	/*
1694 	 * Set the check interval to be 1/5 of the estimated time to
1695 	 * send a single character, and make it at least 1.  The check
1696 	 * interval should also be less than the timeout.
1697 	 *
1698 	 * Note: we have to use pretty tight timings here to satisfy
1699 	 * the NIST-PCTS.
1700 	 */
1701 	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1702 	char_time = char_time / 5;
1703 	if (char_time == 0)
1704 		char_time = 1;
1705 	if (timeout)
1706 	  char_time = MIN(char_time, timeout);
1707 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1708 	printk("In sab82532_wait_until_sent(%d) check=%lu "
1709 	       "xmit_cnt = %ld, alls = %d (jiff=%lu)...\n",
1710 	       timeout, char_time,
1711 	       CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1712 	       test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1713 #endif
1714 	orig_jiffies = jiffies;
1715 	while ((info->xmit.head != info->xmit.tail) ||
1716 	       !test_bit(SAB82532_ALLS, &info->irqflags)) {
1717 		current->state = TASK_INTERRUPTIBLE;
1718 		schedule_timeout(char_time);
1719 		if (signal_pending(current))
1720 			break;
1721 		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1722 			break;
1723 	}
1724 #ifdef SERIAL_DEBUG_WAIT_UNTIL_SENT
1725 	printk("xmit_cnt = %ld, alls = %d (jiff=%lu)...done\n",
1726 	       CIRC_CNT(info->xmit.head, info->xmit.tail, SERIAL_XMIT_SIZE),
1727 	       test_bit(SAB82532_ALLS, &info->irqflags), jiffies);
1728 #endif
1729 }
1730 
1731 /*
1732  * sab82532_hangup() --- called by tty_hangup() when a hangup is signaled.
1733  */
sab82532_hangup(struct tty_struct * tty)1734 static void sab82532_hangup(struct tty_struct *tty)
1735 {
1736 	struct sab82532 * info = (struct sab82532 *)tty->driver_data;
1737 
1738 	if (serial_paranoia_check(info, tty->device, "sab82532_hangup"))
1739 		return;
1740 
1741 #ifdef CONFIG_SERIAL_CONSOLE
1742 	if (info->is_console)
1743 		return;
1744 #endif
1745 
1746 	sab82532_flush_buffer(tty);
1747 	shutdown(info);
1748 	info->event = 0;
1749 	info->count = 0;
1750 	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1751 	info->tty = 0;
1752 	wake_up_interruptible(&info->open_wait);
1753 }
1754 
1755 /*
1756  * ------------------------------------------------------------
1757  * sab82532_open() and friends
1758  * ------------------------------------------------------------
1759  */
block_til_ready(struct tty_struct * tty,struct file * filp,struct sab82532 * info)1760 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1761 			   struct sab82532 *info)
1762 {
1763 	DECLARE_WAITQUEUE(wait, current);
1764 	int retval;
1765 	int do_clocal = 0;
1766 
1767 	/*
1768 	 * If the device is in the middle of being closed, then block
1769 	 * until it's done, and then try again.
1770 	 */
1771 	if (tty_hung_up_p(filp) ||
1772 	    (info->flags & ASYNC_CLOSING)) {
1773 		if (info->flags & ASYNC_CLOSING)
1774 			interruptible_sleep_on(&info->close_wait);
1775 #ifdef SERIAL_DO_RESTART
1776 		if (info->flags & ASYNC_HUP_NOTIFY)
1777 			return -EAGAIN;
1778 		else
1779 			return -ERESTARTSYS;
1780 #else
1781 		return -EAGAIN;
1782 #endif
1783 	}
1784 
1785 	/*
1786 	 * If this is a callout device, then just make sure the normal
1787 	 * device isn't being used.
1788 	 */
1789 	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1790 		if (info->flags & ASYNC_NORMAL_ACTIVE)
1791 			return -EBUSY;
1792 		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1793 		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1794 		    (info->session != current->session))
1795 		    return -EBUSY;
1796 		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1797 		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1798 		    (info->pgrp != current->pgrp))
1799 		    return -EBUSY;
1800 		info->flags |= ASYNC_CALLOUT_ACTIVE;
1801 		return 0;
1802 	}
1803 
1804 	/*
1805 	 * If non-blocking mode is set, or the port is not enabled,
1806 	 * then make the check up front and then exit.
1807 	 */
1808 	if ((filp->f_flags & O_NONBLOCK) ||
1809 	    (tty->flags & (1 << TTY_IO_ERROR))) {
1810 		if (info->flags & ASYNC_CALLOUT_ACTIVE)
1811 			return -EBUSY;
1812 		info->flags |= ASYNC_NORMAL_ACTIVE;
1813 		return 0;
1814 	}
1815 
1816 	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1817 		if (info->normal_termios.c_cflag & CLOCAL)
1818 			do_clocal = 1;
1819 	} else {
1820 		if (tty->termios->c_cflag & CLOCAL)
1821 			do_clocal = 1;
1822 	}
1823 
1824 	/*
1825 	 * Block waiting for the carrier detect and the line to become
1826 	 * free (i.e., not in use by the callout).  While we are in
1827 	 * this loop, info->count is dropped by one, so that
1828 	 * sab82532_close() knows when to free things.  We restore it upon
1829 	 * exit, either normal or abnormal.
1830 	 */
1831 	retval = 0;
1832 	add_wait_queue(&info->open_wait, &wait);
1833 #ifdef SERIAL_DEBUG_OPEN
1834 	printk("block_til_ready before block: ttyS%d, count = %d\n",
1835 	       info->line, info->count);
1836 #endif
1837 	cli();
1838 	if (!tty_hung_up_p(filp))
1839 		info->count--;
1840 	sti();
1841 	info->blocked_open++;
1842 	while (1) {
1843 		cli();
1844 		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1845 		    (tty->termios->c_cflag & CBAUD)) {
1846 			writeb(readb(&info->regs->rw.pvr) & ~(info->pvr_dtr_bit), &info->regs->rw.pvr);
1847 			writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
1848 			writeb(readb(&info->regs->rw.mode) & ~(SAB82532_MODE_RTS), &info->regs->rw.mode);
1849 		}
1850 		sti();
1851 		set_current_state(TASK_INTERRUPTIBLE);
1852 		if (tty_hung_up_p(filp) ||
1853 		    !(info->flags & ASYNC_INITIALIZED)) {
1854 #ifdef SERIAL_DO_RESTART
1855 			if (info->flags & ASYNC_HUP_NOTIFY)
1856 				retval = -EAGAIN;
1857 			else
1858 				retval = -ERESTARTSYS;
1859 #else
1860 			retval = -EAGAIN;
1861 #endif
1862 			break;
1863 		}
1864 		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1865 		    !(info->flags & ASYNC_CLOSING) &&
1866 		    (do_clocal || !(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD)))
1867 			break;
1868 		if (signal_pending(current)) {
1869 			retval = -ERESTARTSYS;
1870 			break;
1871 		}
1872 #ifdef SERIAL_DEBUG_OPEN
1873 		printk("block_til_ready blocking: ttyS%d, count = %d, flags = %x, clocal = %d, vstr = %02x\n",
1874 		       info->line, info->count, info->flags, do_clocal, readb(&info->regs->r.vstr));
1875 #endif
1876 		schedule();
1877 	}
1878 	current->state = TASK_RUNNING;
1879 	remove_wait_queue(&info->open_wait, &wait);
1880 	if (!tty_hung_up_p(filp))
1881 		info->count++;
1882 	info->blocked_open--;
1883 #ifdef SERIAL_DEBUG_OPEN
1884 	printk("block_til_ready after blocking: ttys%d, count = %d\n",
1885 	       info->line, info->count);
1886 #endif
1887 	if (retval)
1888 		return retval;
1889 	info->flags |= ASYNC_NORMAL_ACTIVE;
1890 	return 0;
1891 }
1892 
1893 /*
1894  * This routine is called whenever a serial port is opened.  It
1895  * enables interrupts for a serial port, linking in its async structure into
1896  * the IRQ chain.   It also performs the serial-specific
1897  * initialization for the tty structure.
1898  */
sab82532_open(struct tty_struct * tty,struct file * filp)1899 static int sab82532_open(struct tty_struct *tty, struct file * filp)
1900 {
1901 	struct sab82532	*info = sab82532_chain;
1902 	int retval, line;
1903 	unsigned long page;
1904 
1905 #ifdef SERIAL_DEBUG_OPEN
1906 	printk("sab82532_open: count = %d\n", info->count);
1907 #endif
1908 
1909 	line = MINOR(tty->device) - tty->driver.minor_start;
1910 	if ((line < 0) || (line >= NR_PORTS))
1911 		return -ENODEV;
1912 
1913 	while (info) {
1914 		if (info->line == line)
1915 			break;
1916 		info = info->next;
1917 	}
1918 	if (!info) {
1919 		printk("sab82532_open: can't find info for line %d\n",
1920 		       line);
1921 		return -ENODEV;
1922 	}
1923 
1924 	if (serial_paranoia_check(info, tty->device, "sab82532_open"))
1925 		return -ENODEV;
1926 
1927 #ifdef SERIAL_DEBUG_OPEN
1928 	printk("sab82532_open %s%d, count = %d\n", tty->driver.name, info->line,
1929 	       info->count);
1930 #endif
1931 
1932 	if (!tmp_buf) {
1933 		page = get_free_page(GFP_KERNEL);
1934 		if (!page)
1935 			return -ENOMEM;
1936 		if (tmp_buf)
1937 			free_page(page);
1938 		else
1939 			tmp_buf = (unsigned char *) page;
1940 	}
1941 
1942 	info->count++;
1943 	tty->driver_data = info;
1944 	info->tty = tty;
1945 
1946 	/*
1947 	 * If the port is in the middle of closing, bail out now.
1948 	 */
1949 	if (tty_hung_up_p(filp) ||
1950 	    (info->flags & ASYNC_CLOSING)) {
1951 		if (info->flags & ASYNC_CLOSING)
1952 			interruptible_sleep_on(&info->close_wait);
1953 #ifdef SERIAL_DO_RESTART
1954 		return ((info->flags & ASYNC_HUP_NOTIFY) ?
1955 			-EAGAIN : -ERESTARTSYS);
1956 #else
1957 		return -EAGAIN;
1958 #endif
1959 	}
1960 
1961 	/*
1962 	 * Start up serial port
1963 	 */
1964 	retval = startup(info);
1965 	if (retval)
1966 		return retval;
1967 
1968 	MOD_INC_USE_COUNT;
1969 	retval = block_til_ready(tty, filp, info);
1970 	if (retval) {
1971 #ifdef SERIAL_DEBUG_OPEN
1972 		printk("sab82532_open returning after block_til_ready with %d\n",
1973 		       retval);
1974 #endif
1975 		return retval;
1976 	}
1977 
1978 	if ((info->count == 1) &&
1979 	    (info->flags & ASYNC_SPLIT_TERMIOS)) {
1980 		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
1981 			*tty->termios = info->normal_termios;
1982 		else
1983 			*tty->termios = info->callout_termios;
1984 		change_speed(info);
1985 	}
1986 
1987 #ifdef CONFIG_SERIAL_CONSOLE
1988 	if (sab82532_console.cflag && sab82532_console.index == line) {
1989 		tty->termios->c_cflag = sab82532_console.cflag;
1990 		sab82532_console.cflag = 0;
1991 		change_speed(info);
1992 	}
1993 #endif
1994 
1995 	info->session = current->session;
1996 	info->pgrp = current->pgrp;
1997 
1998 #ifdef SERIAL_DEBUG_OPEN
1999 	printk("sab82532_open ttys%d successful... count %d", info->line, info->count);
2000 #endif
2001 	return 0;
2002 }
2003 
2004 /*
2005  * /proc fs routines....
2006  */
2007 
2008 static __inline__ int
line_info(char * buf,struct sab82532 * info)2009 line_info(char *buf, struct sab82532 *info)
2010 {
2011 	unsigned long flags;
2012 	char stat_buf[30];
2013 	int ret;
2014 
2015 	ret = sprintf(buf, "%u: uart:SAB82532 ", info->line);
2016 	switch (info->type) {
2017 		case 0:
2018 			ret += sprintf(buf+ret, "V1.0 ");
2019 			break;
2020 		case 1:
2021 			ret += sprintf(buf+ret, "V2.0 ");
2022 			break;
2023 		case 2:
2024 			ret += sprintf(buf+ret, "V3.2 ");
2025 			break;
2026 		default:
2027 			ret += sprintf(buf+ret, "V?.? ");
2028 			break;
2029 	}
2030 	ret += sprintf(buf+ret, "port:%lX irq:%s",
2031 		       (unsigned long)info->regs, __irq_itoa(info->irq));
2032 
2033 	if (!info->regs) {
2034 		ret += sprintf(buf+ret, "\n");
2035 		return ret;
2036 	}
2037 
2038 	/*
2039 	 * Figure out the current RS-232 lines
2040 	 */
2041 	stat_buf[0] = 0;
2042 	stat_buf[1] = 0;
2043 	save_flags(flags); cli();
2044 	if (readb(&info->regs->r.mode) & SAB82532_MODE_RTS) {
2045 		if (!(readb(&info->regs->r.mode) & SAB82532_MODE_FRTS))
2046 			strcat(stat_buf, "|RTS");
2047 	} else {
2048 		strcat(stat_buf, "|RTS");
2049 	}
2050 	if (readb(&info->regs->r.star) & SAB82532_STAR_CTS)
2051 		strcat(stat_buf, "|CTS");
2052 	if (!(readb(&info->regs->r.pvr) & info->pvr_dtr_bit))
2053 		strcat(stat_buf, "|DTR");
2054 	if (!(readb(&info->regs->r.pvr) & info->pvr_dsr_bit))
2055 		strcat(stat_buf, "|DSR");
2056 	if (!(readb(&info->regs->r.vstr) & SAB82532_VSTR_CD))
2057 		strcat(stat_buf, "|CD");
2058 	restore_flags(flags);
2059 
2060 	if (info->baud)
2061 		ret += sprintf(buf+ret, " baud:%u", info->baud);
2062 
2063 	ret += sprintf(buf+ret, " tx:%u rx:%u",
2064 		       info->icount.tx, info->icount.rx);
2065 
2066 	if (info->icount.frame)
2067 		ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2068 
2069 	if (info->icount.parity)
2070 		ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2071 
2072 	if (info->icount.brk)
2073 		ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2074 
2075 	if (info->icount.overrun)
2076 		ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2077 
2078 	/*
2079 	 * Last thing is the RS-232 status lines.
2080 	 */
2081 	ret += sprintf(buf+ret, " %s\n", stat_buf + 1);
2082 	return ret;
2083 }
2084 
sab82532_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)2085 int sab82532_read_proc(char *page, char **start, off_t off, int count,
2086 		       int *eof, void *data)
2087 {
2088 	struct sab82532 *info = sab82532_chain;
2089 	off_t begin = 0;
2090 	int len = 0;
2091 
2092 	len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2093 	for (info = sab82532_chain; info && len < 4000; info = info->next) {
2094 		len += line_info(page + len, info);
2095 		if (len+begin > off+count)
2096 			goto done;
2097 		if (len+begin < off) {
2098 			begin += len;
2099 			len = 0;
2100 		}
2101 	}
2102 	*eof = 1;
2103 done:
2104 	if (off >= len+begin)
2105 		return 0;
2106 	*start = page + (off-begin);
2107 	return ((count < begin+len-off) ? count : begin+len-off);
2108 }
2109 
2110 /*
2111  * ---------------------------------------------------------------------
2112  * sab82532_init() and friends
2113  *
2114  * sab82532_init() is called at boot-time to initialize the serial driver.
2115  * ---------------------------------------------------------------------
2116  */
get_sab82532(unsigned long * memory_start)2117 static int __init get_sab82532(unsigned long *memory_start)
2118 {
2119 	struct linux_ebus *ebus;
2120 	struct linux_ebus_device *edev = 0;
2121 	struct sab82532 *sab;
2122 	unsigned long regs, offset;
2123 	int i;
2124 
2125 	for_each_ebus(ebus) {
2126 		for_each_ebusdev(edev, ebus) {
2127 			if (!strcmp(edev->prom_name, "se"))
2128 				goto ebus_done;
2129 
2130 			if (!strcmp(edev->prom_name, "serial")) {
2131 				char compat[32];
2132 				int clen;
2133 
2134 				/* On RIO this can be an SE, check it.  We could
2135 				 * just check ebus->is_rio, but this is more portable.
2136 				 */
2137 				clen = prom_getproperty(edev->prom_node, "compatible",
2138 							compat, sizeof(compat));
2139 				if (clen > 0) {
2140 					if (strncmp(compat, "sab82532", 8) == 0) {
2141 						/* Yep. */
2142 						goto ebus_done;
2143 					}
2144 				}
2145 			}
2146 		}
2147 	}
2148 ebus_done:
2149 	if (!edev)
2150 		return -ENODEV;
2151 
2152 	regs = edev->resource[0].start;
2153 	offset = sizeof(union sab82532_async_regs);
2154 
2155 	for (i = 0; i < 2; i++) {
2156 		if (memory_start) {
2157 			*memory_start = (*memory_start + 7) & ~(7);
2158 			sab = (struct sab82532 *)*memory_start;
2159 			*memory_start += sizeof(struct sab82532);
2160 		} else {
2161 			sab = (struct sab82532 *)kmalloc(sizeof(struct sab82532),
2162 							 GFP_KERNEL);
2163 			if (!sab) {
2164 				printk("sab82532: can't alloc sab struct\n");
2165 				break;
2166 			}
2167 		}
2168 		memset(sab, 0, sizeof(struct sab82532));
2169 
2170 		sab->regs = ioremap(regs + offset, sizeof(union sab82532_async_regs));
2171 		sab->irq = edev->irqs[0];
2172 		sab->line = 1 - i;
2173 		sab->xmit_fifo_size = 32;
2174 		sab->recv_fifo_size = 32;
2175 
2176 		writeb(SAB82532_IPC_IC_ACT_LOW, &sab->regs->w.ipc);
2177 
2178 		sab->next = sab82532_chain;
2179 		sab82532_chain = sab;
2180 
2181 		offset -= sizeof(union sab82532_async_regs);
2182 	}
2183 	return 0;
2184 }
2185 
2186 #ifndef MODULE
sab82532_kgdb_hook(int line)2187 static void __init sab82532_kgdb_hook(int line)
2188 {
2189 	prom_printf("sab82532: kgdb support is not implemented, yet\n");
2190 	prom_halt();
2191 }
2192 #endif
2193 
show_serial_version(void)2194 static inline void __init show_serial_version(void)
2195 {
2196 	char *revision = "$Revision: 1.65 $";
2197 	char *version, *p;
2198 
2199 	version = strchr(revision, ' ');
2200 	strcpy(serial_version, ++version);
2201 	p = strchr(serial_version, ' ');
2202 	*p = '\0';
2203 	printk("SAB82532 serial driver version %s\n", serial_version);
2204 }
2205 
2206 extern int su_num_ports;
2207 
2208 /*
2209  * The serial driver boot-time initialization code!
2210  */
sab82532_init(void)2211 int __init sab82532_init(void)
2212 {
2213 	struct sab82532 *info;
2214 	int i;
2215 
2216 	if (!sab82532_chain)
2217 		get_sab82532(0);
2218 	if (!sab82532_chain)
2219 		return -ENODEV;
2220 
2221 	init_bh(SERIAL_BH, do_serial_bh);
2222 
2223 	show_serial_version();
2224 
2225 	/* Initialize the tty_driver structure */
2226 	memset(&serial_driver, 0, sizeof(struct tty_driver));
2227 	serial_driver.magic = TTY_DRIVER_MAGIC;
2228 	serial_driver.driver_name = "serial";
2229 #ifdef CONFIG_DEVFS_FS
2230 	serial_driver.name = "tts/%d";
2231 #else
2232 	serial_driver.name = "ttyS";
2233 #endif
2234 	serial_driver.major = TTY_MAJOR;
2235 	serial_driver.minor_start = 64 + su_num_ports;
2236 	serial_driver.num = NR_PORTS;
2237 	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2238 	serial_driver.subtype = SERIAL_TYPE_NORMAL;
2239 	serial_driver.init_termios = tty_std_termios;
2240 	serial_driver.init_termios.c_cflag =
2241 		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2242 	serial_driver.flags = TTY_DRIVER_REAL_RAW;
2243 	serial_driver.refcount = &sab82532_refcount;
2244 	serial_driver.table = sab82532_table;
2245 	serial_driver.termios = sab82532_termios;
2246 	serial_driver.termios_locked = sab82532_termios_locked;
2247 
2248 	serial_driver.open = sab82532_open;
2249 	serial_driver.close = sab82532_close;
2250 	serial_driver.write = sab82532_write;
2251 	serial_driver.put_char = sab82532_put_char;
2252 	serial_driver.flush_chars = sab82532_flush_chars;
2253 	serial_driver.write_room = sab82532_write_room;
2254 	serial_driver.chars_in_buffer = sab82532_chars_in_buffer;
2255 	serial_driver.flush_buffer = sab82532_flush_buffer;
2256 	serial_driver.ioctl = sab82532_ioctl;
2257 	serial_driver.throttle = sab82532_throttle;
2258 	serial_driver.unthrottle = sab82532_unthrottle;
2259 	serial_driver.send_xchar = sab82532_send_xchar;
2260 	serial_driver.set_termios = sab82532_set_termios;
2261 	serial_driver.stop = sab82532_stop;
2262 	serial_driver.start = sab82532_start;
2263 	serial_driver.hangup = sab82532_hangup;
2264 	serial_driver.break_ctl = sab82532_break;
2265 	serial_driver.wait_until_sent = sab82532_wait_until_sent;
2266 	serial_driver.read_proc = sab82532_read_proc;
2267 
2268 	/*
2269 	 * The callout device is just like normal device except for
2270 	 * major number and the subtype code.
2271 	 */
2272 	callout_driver = serial_driver;
2273 #ifdef CONFIG_DEVFS_FS
2274 	callout_driver.name = "cua/%d";
2275 #else
2276 	callout_driver.name = "cua";
2277 #endif
2278 	callout_driver.major = TTYAUX_MAJOR;
2279 	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2280 	callout_driver.read_proc = 0;
2281 	callout_driver.proc_entry = 0;
2282 
2283 	if (tty_register_driver(&serial_driver))
2284 		panic("Couldn't register serial driver\n");
2285 	if (tty_register_driver(&callout_driver))
2286 		panic("Couldn't register callout driver\n");
2287 
2288 	for (info = sab82532_chain, i = 0; info; info = info->next, i++) {
2289 		info->magic = SERIAL_MAGIC;
2290 
2291 		info->type = readb(&info->regs->r.vstr) & 0x0f;
2292 		writeb(~((1 << 1) | (1 << 2) | (1 << 4)), &info->regs->w.pcr);
2293 		writeb(0xff, &info->regs->w.pim);
2294 		if (info->line == 0) {
2295 			info->pvr_dsr_bit = (1 << 0);
2296 			info->pvr_dtr_bit = (1 << 1);
2297 		} else {
2298 			info->pvr_dsr_bit = (1 << 3);
2299 			info->pvr_dtr_bit = (1 << 2);
2300 		}
2301 		writeb((1 << 1) | (1 << 2) | (1 << 4), &info->regs->w.pvr);
2302 		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_FRTS, &info->regs->rw.mode);
2303 		writeb(readb(&info->regs->rw.mode) | SAB82532_MODE_RTS, &info->regs->rw.mode);
2304 
2305 		info->custom_divisor = 16;
2306 		info->close_delay = 5*HZ/10;
2307 		info->closing_wait = 30*HZ;
2308 		info->tec_timeout = SAB82532_MAX_TEC_TIMEOUT;
2309 		info->cec_timeout = SAB82532_MAX_CEC_TIMEOUT;
2310 		info->x_char = 0;
2311 		info->event = 0;
2312 		info->blocked_open = 0;
2313 		info->tqueue.routine = do_softint;
2314 		info->tqueue.data = info;
2315 		info->tqueue_hangup.routine = do_serial_hangup;
2316 		info->tqueue_hangup.data = info;
2317 		info->callout_termios = callout_driver.init_termios;
2318 		info->normal_termios = serial_driver.init_termios;
2319 		init_waitqueue_head(&info->open_wait);
2320 		init_waitqueue_head(&info->close_wait);
2321 		init_waitqueue_head(&info->delta_msr_wait);
2322 		info->icount.cts = info->icount.dsr =
2323 			info->icount.rng = info->icount.dcd = 0;
2324 		info->icount.rx = info->icount.tx = 0;
2325 		info->icount.frame = info->icount.parity = 0;
2326 		info->icount.overrun = info->icount.brk = 0;
2327 
2328 		if (!(info->line & 0x01)) {
2329 			if (request_irq(info->irq, sab82532_interrupt, SA_SHIRQ,
2330 					"serial(sab82532)", info)) {
2331 				printk("sab82532: can't get IRQ %x\n",
2332 				       info->irq);
2333 				panic("sab82532 initialization failed");
2334 			}
2335 		}
2336 
2337 		printk(KERN_INFO
2338 		       "ttyS%02d at 0x%lx (irq = %s) is a SAB82532 %s\n",
2339 		       info->line + su_num_ports, (unsigned long)info->regs,
2340 		       __irq_itoa(info->irq), sab82532_version[info->type]);
2341 	}
2342 
2343 #ifdef SERIAL_LOG_DEVICE
2344 	dprint_init(SERIAL_LOG_DEVICE);
2345 #endif
2346 	return 0;
2347 }
2348 
sab82532_probe(void)2349 int __init sab82532_probe(void)
2350 {
2351 	int node, enode, snode;
2352 	char model[32];
2353 	int len;
2354 
2355         node = prom_getchild(prom_root_node);
2356 	node = prom_searchsiblings(node, "pci");
2357 
2358 	/*
2359 	 * Check for SUNW,sabre on Ultra 5/10/AXi.
2360 	 */
2361 	len = prom_getproperty(node, "model", model, sizeof(model));
2362 	if ((len > 0) && !strncmp(model, "SUNW,sabre", len)) {
2363         	node = prom_getchild(node);
2364 		node = prom_searchsiblings(node, "pci");
2365 	}
2366 
2367 	/*
2368 	 * For each PCI bus...
2369 	 */
2370 	while (node) {
2371 		enode = prom_getchild(node);
2372 		enode = prom_searchsiblings(enode, "ebus");
2373 
2374 		/*
2375 		 * For each EBus on this PCI...
2376 		 */
2377 		while (enode) {
2378 			int child;
2379 
2380 			child = prom_getchild(enode);
2381 			snode = prom_searchsiblings(child, "se");
2382 			if (snode)
2383 				goto found;
2384 
2385 			snode = prom_searchsiblings(child, "serial");
2386 			if (snode) {
2387 				char compat[32];
2388 				int clen;
2389 
2390 				clen = prom_getproperty(snode, "compatible",
2391 							compat, sizeof(compat));
2392 				if (clen > 0) {
2393 					if (strncmp(compat, "sab82532", 8) == 0)
2394 						goto found;
2395 				}
2396 			}
2397 
2398 			enode = prom_getsibling(enode);
2399 			enode = prom_searchsiblings(enode, "ebus");
2400 		}
2401 		node = prom_getsibling(node);
2402 		node = prom_searchsiblings(node, "pci");
2403 	}
2404 	return -ENODEV;
2405 
2406 found:
2407 #ifdef CONFIG_SERIAL_CONSOLE
2408 	sunserial_setinitfunc(sab82532_console_init);
2409 #endif
2410 #ifndef MODULE
2411 	sunserial_setinitfunc(sab82532_init);
2412 	rs_ops.rs_kgdb_hook = sab82532_kgdb_hook;
2413 #endif
2414 	return 0;
2415 }
2416 
2417 #ifdef MODULE
2418 MODULE_LICENSE("GPL");
2419 
init_module(void)2420 int init_module(void)
2421 {
2422 	if (get_sab82532(0))
2423 		return -ENODEV;
2424 
2425 	return sab82532_init();
2426 }
2427 
cleanup_module(void)2428 void cleanup_module(void)
2429 {
2430 	struct sab82532 *sab;
2431 	unsigned long flags;
2432 	int e1, e2;
2433 
2434 	/* printk("Unloading %s: version %s\n", serial_name, serial_version); */
2435 	save_flags(flags);
2436 	cli();
2437         remove_bh(SERIAL_BH);
2438 	if ((e1 = tty_unregister_driver(&serial_driver)))
2439 		printk("SERIAL: failed to unregister serial driver (%d)\n",
2440 		       e1);
2441 	if ((e2 = tty_unregister_driver(&callout_driver)))
2442 		printk("SERIAL: failed to unregister callout driver (%d)\n",
2443 		       e2);
2444 	restore_flags(flags);
2445 
2446 	if (tmp_buf) {
2447 		free_page((unsigned long) tmp_buf);
2448 		tmp_buf = NULL;
2449 	}
2450 	for (sab = sab82532_chain; sab; sab = sab->next) {
2451 		if (!(sab->line & 0x01))
2452 			free_irq(sab->irq, sab);
2453 		iounmap(sab->regs);
2454 	}
2455 }
2456 #endif /* MODULE */
2457 
2458 #ifdef CONFIG_SERIAL_CONSOLE
2459 static void
batten_down_hatches(struct sab82532 * info)2460 batten_down_hatches(struct sab82532 *info)
2461 {
2462 	unsigned char saved_rfc, tmp;
2463 
2464 	if (!stop_a_enabled)
2465 		return;
2466 
2467 	/* If we are doing kadb, we call the debugger
2468 	 * else we just drop into the boot monitor.
2469 	 * Note that we must flush the user windows
2470 	 * first before giving up control.
2471 	 */
2472 	printk("\n");
2473 	flush_user_windows();
2474 
2475 	/*
2476 	 * Set FIFO to single character mode.
2477 	 */
2478 	saved_rfc = readb(&info->regs->r.rfc);
2479 	tmp = readb(&info->regs->rw.rfc);
2480 	tmp &= ~(SAB82532_RFC_RFDF);
2481 	writeb(tmp, &info->regs->rw.rfc);
2482 	sab82532_cec_wait(info);
2483 	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2484 
2485 #ifndef __sparc_v9__
2486 	if ((((unsigned long)linux_dbvec) >= DEBUG_FIRSTVADDR) &&
2487 	    (((unsigned long)linux_dbvec) <= DEBUG_LASTVADDR))
2488 		sp_enter_debugger();
2489 	else
2490 #endif
2491 		prom_cmdline();
2492 
2493 	/*
2494 	 * Reset FIFO to character + status mode.
2495 	 */
2496 	writeb(saved_rfc, &info->regs->w.rfc);
2497 	sab82532_cec_wait(info);
2498 	writeb(SAB82532_CMDR_RRES, &info->regs->w.cmdr);
2499 }
2500 
2501 static __inline__ void
sab82532_console_putchar(struct sab82532 * info,char c)2502 sab82532_console_putchar(struct sab82532 *info, char c)
2503 {
2504 	unsigned long flags;
2505 
2506 	save_flags(flags); cli();
2507 	sab82532_tec_wait(info);
2508 	writeb(c, &info->regs->w.tic);
2509 	restore_flags(flags);
2510 }
2511 
2512 static void
sab82532_console_write(struct console * con,const char * s,unsigned n)2513 sab82532_console_write(struct console *con, const char *s, unsigned n)
2514 {
2515 	struct sab82532 *info;
2516 	int i;
2517 
2518 	info = sab82532_chain;
2519 	for (i = con->index; i; i--) {
2520 		info = info->next;
2521 		if (!info)
2522 			return;
2523 	}
2524 
2525 	for (i = 0; i < n; i++) {
2526 		if (*s == '\n')
2527 			sab82532_console_putchar(info, '\r');
2528 		sab82532_console_putchar(info, *s++);
2529 	}
2530 	sab82532_tec_wait(info);
2531 }
2532 
2533 static kdev_t
sab82532_console_device(struct console * con)2534 sab82532_console_device(struct console *con)
2535 {
2536 	return MKDEV(TTY_MAJOR, 64 + con->index);
2537 }
2538 
2539 static int
sab82532_console_setup(struct console * con,char * options)2540 sab82532_console_setup(struct console *con, char *options)
2541 {
2542 	static struct tty_struct c_tty;
2543 	static struct termios c_termios;
2544 	struct sab82532 *info;
2545 	tcflag_t	cflag;
2546 	int		i;
2547 
2548 	info = sab82532_chain;
2549 	for (i = con->index; i; i--) {
2550 		info = info->next;
2551 		if (!info)
2552 			return -ENODEV;
2553 	}
2554 	info->is_console = 1;
2555 
2556 	/*
2557 	 * Initialize the hardware
2558 	 */
2559 	sab82532_init_line(info);
2560 
2561 	/*
2562 	 * Finally, enable interrupts
2563 	 */
2564 	info->interrupt_mask0 = SAB82532_IMR0_PERR | SAB82532_IMR0_FERR |
2565 				SAB82532_IMR0_PLLA | SAB82532_IMR0_CDSC;
2566 	writeb(info->interrupt_mask0, &info->regs->w.imr0);
2567 	info->interrupt_mask1 = SAB82532_IMR1_BRKT | SAB82532_IMR1_ALLS |
2568 				SAB82532_IMR1_XOFF | SAB82532_IMR1_TIN |
2569 				SAB82532_IMR1_CSC | SAB82532_IMR1_XON |
2570 				SAB82532_IMR1_XPR;
2571 	writeb(info->interrupt_mask1, &info->regs->w.imr1);
2572 
2573 	printk("Console: ttyS%d (SAB82532)\n", info->line);
2574 
2575 	sunserial_console_termios(con);
2576 	cflag = con->cflag;
2577 
2578 	/*
2579 	 * Fake up the tty and tty->termios structures so we can use
2580 	 * change_speed (and eliminate a lot of duplicate code).
2581 	 */
2582 	if (!info->tty) {
2583 		memset(&c_tty, 0, sizeof(c_tty));
2584 		info->tty = &c_tty;
2585 	}
2586 	if (!info->tty->termios) {
2587 		memset(&c_termios, 0, sizeof(c_termios));
2588 		info->tty->termios = &c_termios;
2589 	}
2590 	info->tty->termios->c_cflag = con->cflag;
2591 
2592 	change_speed(info);
2593 
2594 	/* Now take out the pointers to static structures if necessary */
2595 	if (info->tty->termios == &c_termios)
2596 		info->tty->termios = 0;
2597 	if (info->tty == &c_tty)
2598 		info->tty = 0;
2599 
2600 	return 0;
2601 }
2602 
2603 static struct console sab82532_console = {
2604 	name:		"ttyS",
2605 	write:		sab82532_console_write,
2606 	device:		sab82532_console_device,
2607 	setup:		sab82532_console_setup,
2608 	flags:		CON_PRINTBUFFER,
2609 	index:		-1,
2610 };
2611 
sab82532_console_init(void)2612 int __init sab82532_console_init(void)
2613 {
2614 	extern int con_is_present(void);
2615 	extern int su_console_registered;
2616 
2617 	if (con_is_present() || su_console_registered)
2618 		return 0;
2619 
2620 	if (!sab82532_chain) {
2621 		prom_printf("sab82532_console_setup: can't get SAB82532 chain");
2622 		prom_halt();
2623 	}
2624 
2625 	sab82532_console.index = serial_console - 1;
2626 	register_console(&sab82532_console);
2627 	return 0;
2628 }
2629 
2630 #ifdef SERIAL_LOG_DEVICE
2631 
2632 static int serial_log_device = 0;
2633 
2634 static void
dprint_init(int tty)2635 dprint_init(int tty)
2636 {
2637 	serial_console = tty + 1;
2638 	sab82532_console.index = tty;
2639 	sab82532_console_setup(&sab82532_console, "");
2640 	serial_console = 0;
2641 	serial_log_device = tty + 1;
2642 }
2643 
2644 int
dprintf(const char * fmt,...)2645 dprintf(const char *fmt, ...)
2646 {
2647 	static char buffer[4096];
2648 	va_list args;
2649 	int i;
2650 
2651 	if (!serial_log_device)
2652 		return 0;
2653 
2654 	va_start(args, fmt);
2655 	i = vsprintf(buffer, fmt, args);
2656 	va_end(args);
2657 	sab82532_console.write(&sab82532_console, buffer, i);
2658 	return i;
2659 }
2660 #endif /* SERIAL_LOG_DEVICE */
2661 #endif /* CONFIG_SERIAL_CONSOLE */
2662