1/* $Id: su.c,v 1.1.1.1 2008/10/15 03:26:47 james26_jang Exp $
2 * su.c: Small serial driver for keyboard/mouse interface on sparc32/PCI
3 *
4 * Copyright (C) 1997  Eddie C. Dost  (ecd@skynet.be)
5 * Copyright (C) 1998-1999  Pete Zaitcev   (zaitcev@yahoo.com)
6 *
7 * This is mainly a variation of drivers/char/serial.c,
8 * credits go to authors mentioned therein.
9 *
10 * Fixed to use tty_get_baud_rate().
11 *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
12 */
13
14/*
15 * Configuration section.
16 */
17#undef SERIAL_PARANOIA_CHECK
18#define CONFIG_SERIAL_NOPAUSE_IO	/* Unused on sparc */
19#define SERIAL_DO_RESTART
20
21/* Set of debugging defines */
22
23#undef SERIAL_DEBUG_INTR
24#undef SERIAL_DEBUG_OPEN
25#undef SERIAL_DEBUG_FLOW
26#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
27#undef SERIAL_DEBUG_THROTTLE
28
29#define RS_ISR_PASS_LIMIT 256
30
31/*
32 * 0x20 is sun4m thing, Dave Redman heritage.
33 * See arch/sparc/kernel/irq.c.
34 */
35#define IRQ_4M(n)	((n)|0x20)
36
37#if defined(MODULE) && defined(SERIAL_DEBUG_MCOUNT)
38#define DBG_CNT(s)							\
39do {									\
40	printk("(%s): [%x] refc=%d, serc=%d, ttyc=%d -> %s\n",		\
41	       kdevname(tty->device), (info->flags), serial_refcount,	\
42	       info->count,tty->count,s);				\
43} while (0)
44#else
45#define DBG_CNT(s)
46#endif
47
48/*
49 * End of serial driver configuration section.
50 */
51#include <linux/config.h>
52#include <linux/module.h>
53#include <linux/errno.h>
54#include <linux/signal.h>
55#include <linux/sched.h>
56#include <linux/interrupt.h>
57#include <linux/tty.h>
58#include <linux/tty_flip.h>
59#include <linux/serial.h>
60#include <linux/serialP.h>
61#include <linux/serial_reg.h>
62#include <linux/string.h>
63#include <linux/fcntl.h>
64#include <linux/ptrace.h>
65#include <linux/ioport.h>
66#include <linux/mm.h>
67#include <linux/slab.h>
68#include <linux/init.h>
69#include <linux/bootmem.h>
70#include <linux/delay.h>
71#ifdef CONFIG_SERIAL_CONSOLE
72#include <linux/console.h>
73#include <linux/major.h>
74#endif
75#include <linux/sysrq.h>
76
77#include <asm/system.h>
78#include <asm/oplib.h>
79#include <asm/io.h>
80#include <asm/ebus.h>
81#ifdef CONFIG_SPARC64
82#include <asm/isa.h>
83#endif
84#include <asm/irq.h>
85#include <asm/uaccess.h>
86#include <asm/bitops.h>
87
88#include "sunserial.h"
89#include "sunkbd.h"
90#include "sunmouse.h"
91
92/* We are on a NS PC87303 clocked with 24.0 MHz, which results
93 * in a UART clock of 1.8462 MHz.
94 */
95#define BAUD_BASE	(1846200 / 16)
96
97#ifdef CONFIG_SERIAL_CONSOLE
98extern int serial_console;
99static struct console sercons;
100int su_serial_console_init(void);
101#endif
102
103enum su_type { SU_PORT_NONE, SU_PORT_MS, SU_PORT_KBD, SU_PORT_PORT };
104static char *su_typev[] = { "???", "mouse", "kbd", "serial" };
105
106#define SU_PROPSIZE	128
107
108/*
109 * serial.c saves memory when it allocates async_info upon first open.
110 * We have parts of state structure together because we do call startup
111 * for keyboard and mouse.
112 */
113struct su_struct {
114	int		 magic;
115	unsigned long	 port;
116	int		 baud_base;
117	int		 type;		/* Hardware type: e.g. 16550 */
118	int		 irq;
119	int		 flags;
120	int		 line;
121	int		 cflag;
122
123	enum su_type	 port_type;	/* Hookup type: e.g. mouse */
124	int		 is_console;
125	int		 port_node;
126
127	char		 name[16];
128
129	int		 xmit_fifo_size;
130	int		 custom_divisor;
131	unsigned short	 close_delay;
132	unsigned short	 closing_wait;	/* time to wait before closing */
133
134	struct tty_struct 	*tty;
135	int			read_status_mask;
136	int			ignore_status_mask;
137	int			timeout;
138	int			quot;
139	int			x_char;	/* xon/xoff character */
140	int			IER; 	/* Interrupt Enable Register */
141	int			MCR; 	/* Modem control register */
142	unsigned long		event;
143	int			blocked_open; /* # of blocked opens */
144	long			session; /* Session of opening process */
145	long			pgrp; /* pgrp of opening process */
146	unsigned char 		*xmit_buf;
147	int			xmit_head;
148	int			xmit_tail;
149	int			xmit_cnt;
150	struct tq_struct	tqueue;
151	wait_queue_head_t	open_wait;
152	wait_queue_head_t	close_wait;
153	wait_queue_head_t	delta_msr_wait;
154
155	int			count;
156	struct async_icount	icount;
157	struct termios		normal_termios, callout_termios;
158	unsigned long		last_active;	/* For async_struct, to be */
159};
160
161/*
162 * Scan status structure.
163 * "prop" is a local variable but it eats stack to keep it in each
164 * stack frame of a recursive procedure.
165 */
166struct su_probe_scan {
167	int msnode, kbnode;	/* PROM nodes for mouse and keyboard */
168	int msx, kbx;		/* minors for mouse and keyboard */
169	int devices;		/* scan index */
170	char prop[SU_PROPSIZE];
171};
172
173static char *serial_name = "PCIO serial driver";
174static char serial_version[16];
175
176static DECLARE_TASK_QUEUE(tq_serial);
177
178static struct tty_driver serial_driver, callout_driver;
179static int serial_refcount;
180
181/* number of characters left in xmit buffer before we ask for more */
182#define WAKEUP_CHARS 256
183
184static void autoconfig(struct su_struct *info);
185static void change_speed(struct su_struct *info, struct termios *old);
186static void su_wait_until_sent(struct tty_struct *tty, int timeout);
187
188/*
189 * Here we define the default xmit fifo size used for each type of
190 * UART
191 */
192static struct serial_uart_config uart_config[] = {
193	{ "unknown", 1, 0 },
194	{ "8250", 1, 0 },
195	{ "16450", 1, 0 },
196	{ "16550", 1, 0 },
197	{ "16550A", 16, UART_CLEAR_FIFO | UART_USE_FIFO },
198	{ "cirrus", 1, 0 },
199	{ "ST16650", 1, UART_CLEAR_FIFO |UART_STARTECH },
200	{ "ST16650V2", 32, UART_CLEAR_FIFO | UART_USE_FIFO |
201		  UART_STARTECH },
202	{ "TI16750", 64, UART_CLEAR_FIFO | UART_USE_FIFO},
203	{ 0, 0}
204};
205
206
207#define NR_PORTS	4
208
209static struct su_struct su_table[NR_PORTS];
210static struct tty_struct *serial_table[NR_PORTS];
211static struct termios *serial_termios[NR_PORTS];
212static struct termios *serial_termios_locked[NR_PORTS];
213
214#ifndef MIN
215#define MIN(a,b)	((a) < (b) ? (a) : (b))
216#endif
217
218/*
219 * tmp_buf is used as a temporary buffer by serial_write.  We need to
220 * lock it in case the copy_from_user blocks while swapping in a page,
221 * and some other program tries to do a serial write at the same time.
222 * Since the lock will only come under contention when the system is
223 * swapping and available memory is low, it makes sense to share one
224 * buffer across all the serial ports, since it significantly saves
225 * memory if large numbers of serial ports are open.
226 */
227static unsigned char *tmp_buf;
228static DECLARE_MUTEX(tmp_buf_sem);
229
230static inline int serial_paranoia_check(struct su_struct *info,
231					kdev_t device, const char *routine)
232{
233#ifdef SERIAL_PARANOIA_CHECK
234	static const char *badmagic = KERN_WARNING
235		"Warning: bad magic number for serial struct (%s) in %s\n";
236	static const char *badinfo = KERN_WARNING
237		"Warning: null su_struct for (%s) in %s\n";
238
239	if (!info) {
240		printk(badinfo, kdevname(device), routine);
241		return 1;
242	}
243	if (info->magic != SERIAL_MAGIC) {
244		printk(badmagic, kdevname(device), routine);
245		return 1;
246	}
247#endif
248	return 0;
249}
250
251static inline
252unsigned int su_inb(struct su_struct *info, unsigned long offset)
253{
254	return inb(info->port + offset);
255}
256
257static inline void
258su_outb(struct su_struct *info, unsigned long offset, int value)
259{
260#ifndef __sparc_v9__
261	/*
262	 * MrCoffee has weird schematics: IRQ4 & P10(?) pins of SuperIO are
263	 * connected with a gate then go to SlavIO. When IRQ4 goes tristated
264	 * gate outputs a logical one. Since we use level triggered interrupts
265	 * we have lockup and watchdog reset. We cannot mask IRQ because
266	 * keyboard shares IRQ with us (Word has it as Bob Smelik's design).
267	 * This problem is similar to what Alpha people suffer, see serial.c.
268	 */
269	if (offset == UART_MCR) value |= UART_MCR_OUT2;
270#endif
271	outb(value, info->port + offset);
272}
273
274#define serial_in(info, off)		su_inb(info, off)
275#define serial_inp(info, off)		su_inb(info, off)
276#define serial_out(info, off, val)	su_outb(info, off, val)
277#define serial_outp(info, off, val)	su_outb(info, off, val)
278
279/*
280 * ------------------------------------------------------------
281 * su_stop() and su_start()
282 *
283 * This routines are called before setting or resetting tty->stopped.
284 * They enable or disable transmitter interrupts, as necessary.
285 * ------------------------------------------------------------
286 */
287static void su_stop(struct tty_struct *tty)
288{
289	struct su_struct *info = (struct su_struct *)tty->driver_data;
290	unsigned long flags;
291
292	if (serial_paranoia_check(info, tty->device, "su_stop"))
293		return;
294
295	save_flags(flags); cli();
296	if (info->IER & UART_IER_THRI) {
297		info->IER &= ~UART_IER_THRI;
298		serial_out(info, UART_IER, info->IER);
299	}
300	restore_flags(flags);
301}
302
303static void su_start(struct tty_struct *tty)
304{
305	struct su_struct *info = (struct su_struct *)tty->driver_data;
306	unsigned long flags;
307
308	if (serial_paranoia_check(info, tty->device, "su_start"))
309		return;
310
311	save_flags(flags); cli();
312	if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
313		info->IER |= UART_IER_THRI;
314		serial_out(info, UART_IER, info->IER);
315	}
316	restore_flags(flags);
317}
318
319/*
320 * ----------------------------------------------------------------------
321 *
322 * Here starts the interrupt handling routines.  All of the following
323 * subroutines are declared as inline and are folded into
324 * su_interrupt().  They were separated out for readability's sake.
325 *
326 * Note: rs_interrupt() is a "fast" interrupt, which means that it
327 * runs with interrupts turned off.  People who may want to modify
328 * rs_interrupt() should try to keep the interrupt handler as fast as
329 * possible.  After you are done making modifications, it is not a bad
330 * idea to do:
331 *
332 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
333 *
334 * and look at the resulting assemble code in serial.s.
335 *
336 * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93
337 * -----------------------------------------------------------------------
338 */
339
340/*
341 * This routine is used by the interrupt handler to schedule
342 * processing in the software interrupt portion of the driver.
343 */
344static void
345su_sched_event(struct su_struct *info, int event)
346{
347	info->event |= 1 << event;
348	queue_task(&info->tqueue, &tq_serial);
349	mark_bh(SERIAL_BH);
350}
351
352static void
353receive_kbd_ms_chars(struct su_struct *info, struct pt_regs *regs, int is_brk)
354{
355	unsigned char status = 0;
356	unsigned char ch;
357
358	do {
359		ch = serial_inp(info, UART_RX);
360		if (info->port_type == SU_PORT_KBD) {
361			if (ch == SUNKBD_RESET) {
362                        	l1a_state.kbd_id = 1;
363                        	l1a_state.l1_down = 0;
364                	} else if (l1a_state.kbd_id) {
365                        	l1a_state.kbd_id = 0;
366                	} else if (ch == SUNKBD_L1) {
367                        	l1a_state.l1_down = 1;
368                	} else if (ch == (SUNKBD_L1|SUNKBD_UP)) {
369                        	l1a_state.l1_down = 0;
370                	} else if (ch == SUNKBD_A && l1a_state.l1_down) {
371                        	/* whee... */
372                        	batten_down_hatches();
373                        	/* Continue execution... */
374                        	l1a_state.l1_down = 0;
375                        	l1a_state.kbd_id = 0;
376                        	return;
377                	}
378                	sunkbd_inchar(ch, regs);
379		} else {
380			sun_mouse_inbyte(ch, is_brk);
381		}
382
383		status = su_inb(info, UART_LSR);
384	} while (status & UART_LSR_DR);
385}
386
387static void
388receive_serial_chars(struct su_struct *info, int *status, struct pt_regs *regs)
389{
390	struct tty_struct *tty = info->tty;
391	unsigned char ch;
392	int ignored = 0, saw_console_brk = 0;
393	struct	async_icount *icount;
394
395	icount = &info->icount;
396	do {
397		ch = serial_inp(info, UART_RX);
398		if (info->is_console &&
399		    (ch == 0 || (*status &UART_LSR_BI)))
400			saw_console_brk = 1;
401		if (tty->flip.count >= TTY_FLIPBUF_SIZE)
402			break;
403		*tty->flip.char_buf_ptr = ch;
404		icount->rx++;
405
406#ifdef SERIAL_DEBUG_INTR
407		printk("D%02x:%02x.", ch, *status);
408#endif
409		*tty->flip.flag_buf_ptr = 0;
410		if (*status & (UART_LSR_BI | UART_LSR_PE |
411			       UART_LSR_FE | UART_LSR_OE)) {
412			/*
413			 * For statistics only
414			 */
415			if (*status & UART_LSR_BI) {
416				*status &= ~(UART_LSR_FE | UART_LSR_PE);
417				icount->brk++;
418			} else if (*status & UART_LSR_PE)
419				icount->parity++;
420			else if (*status & UART_LSR_FE)
421				icount->frame++;
422			if (*status & UART_LSR_OE)
423				icount->overrun++;
424
425			/*
426			 * Now check to see if character should be
427			 * ignored, and mask off conditions which
428			 * should be ignored.
429			 */
430			if (*status & info->ignore_status_mask) {
431				if (++ignored > 100) {
432#ifdef SERIAL_DEBUG_INTR
433					printk("ign100..");
434#endif
435					break;
436				}
437				goto ignore_char;
438			}
439			*status &= info->read_status_mask;
440
441			if (*status & (UART_LSR_BI)) {
442#ifdef SERIAL_DEBUG_INTR
443				printk("handling break....");
444#endif
445				*tty->flip.flag_buf_ptr = TTY_BREAK;
446				if (info->flags & ASYNC_SAK)
447					do_SAK(tty);
448			} else if (*status & UART_LSR_PE)
449				*tty->flip.flag_buf_ptr = TTY_PARITY;
450			else if (*status & UART_LSR_FE)
451				*tty->flip.flag_buf_ptr = TTY_FRAME;
452			if (*status & UART_LSR_OE) {
453				/*
454				 * Overrun is special, since it's
455				 * reported immediately, and doesn't
456				 * affect the current character
457				 */
458				if (tty->flip.count < TTY_FLIPBUF_SIZE) {
459					tty->flip.count++;
460					tty->flip.flag_buf_ptr++;
461					tty->flip.char_buf_ptr++;
462					*tty->flip.flag_buf_ptr = TTY_OVERRUN;
463				}
464			}
465		}
466		tty->flip.flag_buf_ptr++;
467		tty->flip.char_buf_ptr++;
468		tty->flip.count++;
469	ignore_char:
470		*status = serial_inp(info, UART_LSR);
471	} while (*status & UART_LSR_DR);
472#ifdef SERIAL_DEBUG_INTR
473	printk("E%02x.R%d", *status, tty->flip.count);
474#endif
475	tty_flip_buffer_push(tty);
476	if (saw_console_brk != 0)
477		batten_down_hatches();
478}
479
480static void
481transmit_chars(struct su_struct *info, int *intr_done)
482{
483	int count;
484
485	if (info->x_char) {
486		serial_outp(info, UART_TX, info->x_char);
487		info->icount.tx++;
488		info->x_char = 0;
489		if (intr_done)
490			*intr_done = 0;
491		return;
492	}
493	if ((info->xmit_cnt <= 0) || info->tty->stopped ||
494	    info->tty->hw_stopped) {
495		info->IER &= ~UART_IER_THRI;
496		serial_out(info, UART_IER, info->IER);
497		return;
498	}
499
500	count = info->xmit_fifo_size;
501	do {
502		serial_out(info, UART_TX, info->xmit_buf[info->xmit_tail++]);
503		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
504		info->icount.tx++;
505		if (--info->xmit_cnt <= 0)
506			break;
507	} while (--count > 0);
508
509	if (info->xmit_cnt < WAKEUP_CHARS)
510		su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
511
512#ifdef SERIAL_DEBUG_INTR
513	printk("T%d...", info->xmit_cnt);
514#endif
515	if (intr_done)
516		*intr_done = 0;
517
518	if (info->xmit_cnt <= 0) {
519		info->IER &= ~UART_IER_THRI;
520		serial_out(info, UART_IER, info->IER);
521	}
522}
523
524static void
525check_modem_status(struct su_struct *info)
526{
527	int	status;
528	struct	async_icount *icount;
529
530	status = serial_in(info, UART_MSR);
531
532	if (status & UART_MSR_ANY_DELTA) {
533		icount = &info->icount;
534		/* update input line counters */
535		if (status & UART_MSR_TERI)
536			icount->rng++;
537		if (status & UART_MSR_DDSR)
538			icount->dsr++;
539		if (status & UART_MSR_DDCD) {
540			icount->dcd++;
541#ifdef CONFIG_HARD_PPS
542			if ((info->flags & ASYNC_HARDPPS_CD) &&
543			    (status & UART_MSR_DCD))
544				hardpps();
545#endif
546		}
547		if (status & UART_MSR_DCTS)
548			icount->cts++;
549		wake_up_interruptible(&info->delta_msr_wait);
550	}
551
552	if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
553#if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
554		printk("ttys%d CD now %s...", info->line,
555		       (status & UART_MSR_DCD) ? "on" : "off");
556#endif
557		if (status & UART_MSR_DCD)
558			wake_up_interruptible(&info->open_wait);
559		else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
560			   (info->flags & ASYNC_CALLOUT_NOHUP))) {
561#ifdef SERIAL_DEBUG_OPEN
562			printk("doing serial hangup...");
563#endif
564			if (info->tty)
565				tty_hangup(info->tty);
566	}
567	}
568	if (info->flags & ASYNC_CTS_FLOW) {
569		if (info->tty->hw_stopped) {
570			if (status & UART_MSR_CTS) {
571#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
572				printk("CTS tx start...");
573#endif
574				info->tty->hw_stopped = 0;
575				info->IER |= UART_IER_THRI;
576				serial_out(info, UART_IER, info->IER);
577				su_sched_event(info, RS_EVENT_WRITE_WAKEUP);
578				return;
579			}
580		} else {
581			if (!(status & UART_MSR_CTS)) {
582#if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
583				printk("CTS tx stop...");
584#endif
585				info->tty->hw_stopped = 1;
586				info->IER &= ~UART_IER_THRI;
587				serial_out(info, UART_IER, info->IER);
588			}
589		}
590	}
591}
592
593/*
594 * This is the kbd/mouse serial driver's interrupt routine
595 */
596static void
597su_kbd_ms_interrupt(int irq, void *dev_id, struct pt_regs * regs)
598{
599	struct su_struct *info = (struct su_struct *)dev_id;
600	unsigned char status;
601
602#ifdef SERIAL_DEBUG_INTR
603	printk("su_kbd_ms_interrupt(%s)...", __irq_itoa(irq));
604#endif
605	if (!info)
606		return;
607
608	if (serial_in(info, UART_IIR) & UART_IIR_NO_INT)
609		return;
610
611	status = serial_inp(info, UART_LSR);
612#ifdef SERIAL_DEBUG_INTR
613	printk("status = %x...", status);
614#endif
615	if ((status & UART_LSR_DR) || (status & UART_LSR_BI))
616		receive_kbd_ms_chars(info, regs,
617				     (status & UART_LSR_BI) != 0);
618
619#ifdef SERIAL_DEBUG_INTR
620	printk("end.\n");
621#endif
622}
623
624/*
625 * This is the serial driver's generic interrupt routine
626 */
627static void
628su_serial_interrupt(int irq, void *dev_id, struct pt_regs * regs)
629{
630	int status;
631	struct su_struct *info;
632	int pass_counter = 0;
633
634#ifdef SERIAL_DEBUG_INTR
635	printk("su_serial_interrupt(%s)...", __irq_itoa(irq));
636#endif
637	info = (struct su_struct *)dev_id;
638	if (!info || !info->tty) {
639#ifdef SERIAL_DEBUG_INTR
640		printk("strain\n");
641#endif
642		return;
643	}
644
645	do {
646		status = serial_inp(info, UART_LSR);
647#ifdef SERIAL_DEBUG_INTR
648		printk("status = %x...", status);
649#endif
650		if (status & UART_LSR_DR)
651			receive_serial_chars(info, &status, regs);
652		check_modem_status(info);
653		if (status & UART_LSR_THRE)
654			transmit_chars(info, 0);
655
656		if (pass_counter++ > RS_ISR_PASS_LIMIT) {
657#ifdef SERIAL_DEBUG_INTR
658			printk("rs loop break");
659#endif
660			break; 	/* Prevent infinite loops */
661		}
662	} while (!(serial_in(info, UART_IIR) & UART_IIR_NO_INT));
663
664	info->last_active = jiffies;
665
666#ifdef SERIAL_DEBUG_INTR
667	printk("end.\n");
668#endif
669}
670
671/*
672 * -------------------------------------------------------------------
673 * Here ends the serial interrupt routines.
674 * -------------------------------------------------------------------
675 */
676
677/*
678 * This routine is used to handle the "bottom half" processing for the
679 * serial driver, known also the "software interrupt" processing.
680 * This processing is done at the kernel interrupt level, after the
681 * su_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
682 * is where time-consuming activities which can not be done in the
683 * interrupt driver proper are done; the interrupt driver schedules
684 * them using su_sched_event(), and they get done here.
685 */
686static void do_serial_bh(void)
687{
688	run_task_queue(&tq_serial);
689}
690
691static void do_softint(void *private_)
692{
693	struct su_struct	*info = (struct su_struct *) private_;
694	struct tty_struct	*tty;
695
696	tty = info->tty;
697	if (!tty)
698		return;
699
700	if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
701		if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
702		    tty->ldisc.write_wakeup)
703			(tty->ldisc.write_wakeup)(tty);
704		wake_up_interruptible(&tty->write_wait);
705	}
706}
707
708/*
709 * ---------------------------------------------------------------
710 * Low level utility subroutines for the serial driver:  routines to
711 * figure out the appropriate timeout for an interrupt chain, routines
712 * to initialize and startup a serial port, and routines to shutdown a
713 * serial port.  Useful stuff like that.
714 * ---------------------------------------------------------------
715 */
716
717static int
718startup(struct su_struct *info)
719{
720	unsigned long flags;
721	int	retval=0;
722	unsigned long page;
723
724	save_flags(flags);
725	if (info->tty) {
726		page = get_free_page(GFP_KERNEL);
727		if (!page)
728			return -ENOMEM;
729
730		cli();
731
732		if (info->flags & ASYNC_INITIALIZED) {
733			free_page(page);
734			goto errout;
735		}
736
737		if (info->port == 0 || info->type == PORT_UNKNOWN) {
738			set_bit(TTY_IO_ERROR, &info->tty->flags);
739			free_page(page);
740			goto errout;
741		}
742		if (info->xmit_buf)
743			free_page(page);
744		else
745			info->xmit_buf = (unsigned char *) page;
746	}
747	cli();
748
749#ifdef SERIAL_DEBUG_OPEN
750	printk("starting up ttys%d (irq %s)...", info->line,
751	       __irq_itoa(info->irq));
752#endif
753
754	if (uart_config[info->type].flags & UART_STARTECH) {
755		/* Wake up UART */
756		serial_outp(info, UART_LCR, 0xBF);
757		serial_outp(info, UART_EFR, UART_EFR_ECB);
758		serial_outp(info, UART_IER, 0);
759		serial_outp(info, UART_EFR, 0);
760		serial_outp(info, UART_LCR, 0);
761	}
762
763	if (info->type == PORT_16750) {
764		/* Wake up UART */
765		serial_outp(info, UART_IER, 0);
766	}
767
768	/*
769	 * Clear the FIFO buffers and disable them
770	 * (they will be reenabled in change_speed())
771	 */
772	if (uart_config[info->type].flags & UART_CLEAR_FIFO)
773		serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
774					     UART_FCR_CLEAR_XMIT));
775
776	/*
777	 * At this point there's no way the LSR could still be 0xFF;
778	 * if it is, then bail out, because there's likely no UART
779	 * here.
780	 */
781	if (serial_inp(info, UART_LSR) == 0xff) {
782		if (capable(CAP_SYS_ADMIN)) {
783			if (info->tty)
784				set_bit(TTY_IO_ERROR, &info->tty->flags);
785		} else
786			retval = -ENODEV;
787		goto errout;
788	}
789
790	/*
791	 * Allocate the IRQ if necessary
792	 */
793	if (info->port_type != SU_PORT_PORT) {
794		retval = request_irq(info->irq, su_kbd_ms_interrupt,
795				     SA_SHIRQ, info->name, info);
796	} else {
797		retval = request_irq(info->irq, su_serial_interrupt,
798				     SA_SHIRQ, info->name, info);
799	}
800	if (retval) {
801		if (capable(CAP_SYS_ADMIN)) {
802			if (info->tty)
803				set_bit(TTY_IO_ERROR, &info->tty->flags);
804			retval = 0;
805		}
806		goto errout;
807	}
808
809	/*
810	 * Clear the interrupt registers.
811	 */
812	(void) serial_inp(info, UART_RX);
813	(void) serial_inp(info, UART_IIR);
814	(void) serial_inp(info, UART_MSR);
815
816	/*
817	 * Now, initialize the UART
818	 */
819	serial_outp(info, UART_LCR, UART_LCR_WLEN8);	/* reset DLAB */
820
821	info->MCR = 0;
822	if (info->tty && info->tty->termios->c_cflag & CBAUD)
823		info->MCR = UART_MCR_DTR | UART_MCR_RTS;
824	if (info->irq != 0)
825		info->MCR |= UART_MCR_OUT2;
826	serial_outp(info, UART_MCR, info->MCR);
827
828	/*
829	 * Finally, enable interrupts
830	 */
831	info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
832	serial_outp(info, UART_IER, info->IER);	/* enable interrupts */
833
834	/*
835	 * And clear the interrupt registers again for luck.
836	 */
837	(void)serial_inp(info, UART_LSR);
838	(void)serial_inp(info, UART_RX);
839	(void)serial_inp(info, UART_IIR);
840	(void)serial_inp(info, UART_MSR);
841
842	if (info->tty)
843		clear_bit(TTY_IO_ERROR, &info->tty->flags);
844	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
845
846	/*
847	 * Set up the tty->alt_speed kludge
848	 */
849	if (info->tty) {
850		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
851			info->tty->alt_speed = 57600;
852		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
853			info->tty->alt_speed = 115200;
854		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
855			info->tty->alt_speed = 230400;
856		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
857			info->tty->alt_speed = 460800;
858	}
859
860	/*
861	 * and set the speed of the serial port
862	 */
863	change_speed(info, 0);
864
865	info->flags |= ASYNC_INITIALIZED;
866	restore_flags(flags);
867	return 0;
868
869errout:
870	restore_flags(flags);
871	return retval;
872}
873
874/*
875 * This routine will shutdown a serial port; interrupts are disabled, and
876 * DTR is dropped if the hangup on close termio flag is on.
877 */
878static void
879shutdown(struct su_struct *info)
880{
881	unsigned long	flags;
882
883	if (!(info->flags & ASYNC_INITIALIZED))
884		return;
885
886	save_flags(flags); cli(); /* Disable interrupts */
887
888	/*
889	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
890	 * here so the queue might never be waken up
891	 */
892	wake_up_interruptible(&info->delta_msr_wait);
893
894	/*
895	 * Free the IRQ, if necessary
896	 */
897	free_irq(info->irq, info);
898
899	if (info->xmit_buf) {
900		free_page((unsigned long) info->xmit_buf);
901		info->xmit_buf = 0;
902	}
903
904	info->IER = 0;
905	serial_outp(info, UART_IER, 0x00);	/* disable all intrs */
906	info->MCR &= ~UART_MCR_OUT2;
907
908	/* disable break condition */
909	serial_out(info, UART_LCR, serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
910
911	if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
912		info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
913	serial_outp(info, UART_MCR, info->MCR);
914
915	/* disable FIFO's */
916	serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR |
917				     UART_FCR_CLEAR_XMIT));
918	(void)serial_in(info, UART_RX);    /* read data port to reset things */
919
920	if (info->tty)
921		set_bit(TTY_IO_ERROR, &info->tty->flags);
922
923	if (uart_config[info->type].flags & UART_STARTECH) {
924		/* Arrange to enter sleep mode */
925		serial_outp(info, UART_LCR, 0xBF);
926		serial_outp(info, UART_EFR, UART_EFR_ECB);
927		serial_outp(info, UART_IER, UART_IERX_SLEEP);
928		serial_outp(info, UART_LCR, 0);
929	}
930	if (info->type == PORT_16750) {
931		/* Arrange to enter sleep mode */
932		serial_outp(info, UART_IER, UART_IERX_SLEEP);
933	}
934	info->flags &= ~ASYNC_INITIALIZED;
935	restore_flags(flags);
936}
937
938static int
939su_get_baud_rate(struct su_struct *info)
940{
941	static struct tty_struct c_tty;
942	static struct termios c_termios;
943
944	if (info->tty)
945		return tty_get_baud_rate(info->tty);
946
947	memset(&c_tty, 0, sizeof(c_tty));
948	memset(&c_termios, 0, sizeof(c_termios));
949	c_tty.termios = &c_termios;
950	c_termios.c_cflag = info->cflag;
951
952	return tty_get_baud_rate(&c_tty);
953}
954
955/*
956 * This routine is called to set the UART divisor registers to match
957 * the specified baud rate for a serial port.
958 */
959static void
960change_speed(struct su_struct *info,
961	     struct termios *old_termios)
962{
963	int		quot = 0, baud;
964	unsigned int	cval, fcr = 0;
965	int		bits;
966	unsigned long	flags;
967
968	if (info->port_type == SU_PORT_PORT) {
969		if (!info->tty || !info->tty->termios)
970			return;
971		if (!info->port)
972			return;
973		info->cflag = info->tty->termios->c_cflag;
974	}
975
976	/* byte size and parity */
977	switch (info->cflag & CSIZE) {
978	      case CS5: cval = 0x00; bits = 7; break;
979	      case CS6: cval = 0x01; bits = 8; break;
980	      case CS7: cval = 0x02; bits = 9; break;
981	      case CS8: cval = 0x03; bits = 10; break;
982		/* Never happens, but GCC is too dumb to figure it out */
983	      default:  cval = 0x00; bits = 7; break;
984	}
985	if (info->cflag & CSTOPB) {
986		cval |= 0x04;
987		bits++;
988	}
989	if (info->cflag & PARENB) {
990		cval |= UART_LCR_PARITY;
991		bits++;
992	}
993	if (!(info->cflag & PARODD))
994		cval |= UART_LCR_EPAR;
995#ifdef CMSPAR
996	if (info->cflag & CMSPAR)
997		cval |= UART_LCR_SPAR;
998#endif
999
1000	/* Determine divisor based on baud rate */
1001	baud = su_get_baud_rate(info);
1002	if (baud == 38400 &&
1003	    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1004		quot = info->custom_divisor;
1005	else {
1006		if (baud == 134)
1007			/* Special case since 134 is really 134.5 */
1008			quot = (2 * info->baud_base / 269);
1009		else if (baud)
1010			quot = info->baud_base / baud;
1011	}
1012	/* If the quotient is zero refuse the change */
1013	if (!quot && old_termios) {
1014		info->tty->termios->c_cflag &= ~CBAUD;
1015		info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1016		baud = tty_get_baud_rate(info->tty);
1017		if (!baud)
1018			baud = 9600;
1019		if (baud == 38400 &&
1020		    ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST))
1021			quot = info->custom_divisor;
1022		else {
1023			if (baud == 134)
1024				/* Special case since 134 is really 134.5 */
1025				quot = (2*info->baud_base / 269);
1026			else if (baud)
1027				quot = info->baud_base / baud;
1028		}
1029	}
1030	/* As a last resort, if the quotient is zero, default to 9600 bps */
1031	if (!quot)
1032		quot = info->baud_base / 9600;
1033	info->timeout = ((info->xmit_fifo_size*HZ*bits*quot) / info->baud_base);
1034	info->timeout += HZ/50;		/* Add .02 seconds of slop */
1035
1036	/* Set up FIFO's */
1037	if (uart_config[info->type].flags & UART_USE_FIFO) {
1038		if ((info->baud_base / quot) < 9600)
1039			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
1040		else
1041			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_8;
1042	}
1043	if (info->type == PORT_16750)
1044		fcr |= UART_FCR7_64BYTE;
1045
1046	/* CTS flow control flag and modem status interrupts */
1047	info->IER &= ~UART_IER_MSI;
1048	if (info->flags & ASYNC_HARDPPS_CD)
1049		info->IER |= UART_IER_MSI;
1050	if (info->cflag & CRTSCTS) {
1051		info->flags |= ASYNC_CTS_FLOW;
1052		info->IER |= UART_IER_MSI;
1053	} else
1054		info->flags &= ~ASYNC_CTS_FLOW;
1055	if (info->cflag & CLOCAL)
1056		info->flags &= ~ASYNC_CHECK_CD;
1057	else {
1058		info->flags |= ASYNC_CHECK_CD;
1059		info->IER |= UART_IER_MSI;
1060	}
1061	serial_out(info, UART_IER, info->IER);
1062
1063	/*
1064	 * Set up parity check flag
1065	 */
1066	if (info->tty) {
1067#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1068
1069		info->read_status_mask = UART_LSR_OE | UART_LSR_THRE |
1070					 UART_LSR_DR;
1071		if (I_INPCK(info->tty))
1072			info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
1073		if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1074			info->read_status_mask |= UART_LSR_BI;
1075
1076		/*
1077		 * Characters to ignore
1078		 */
1079		info->ignore_status_mask = 0;
1080		if (I_IGNPAR(info->tty))
1081			info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
1082		if (I_IGNBRK(info->tty)) {
1083			info->ignore_status_mask |= UART_LSR_BI;
1084			/*
1085			 * If we're ignore parity and break indicators, ignore
1086			 * overruns too.  (For real raw support).
1087			 */
1088			if (I_IGNPAR(info->tty))
1089				info->ignore_status_mask |= UART_LSR_OE;
1090		}
1091		/*
1092		 * !!! ignore all characters if CREAD is not set
1093		 */
1094		if ((info->cflag & CREAD) == 0)
1095			info->ignore_status_mask |= UART_LSR_DR;
1096	}
1097
1098	save_flags(flags); cli();
1099	if (uart_config[info->type].flags & UART_STARTECH) {
1100		serial_outp(info, UART_LCR, 0xBF);
1101		serial_outp(info, UART_EFR,
1102			    (info->cflag & CRTSCTS) ? UART_EFR_CTS : 0);
1103	}
1104	serial_outp(info, UART_LCR, cval | UART_LCR_DLAB);	/* set DLAB */
1105	serial_outp(info, UART_DLL, quot & 0xff);	/* LS of divisor */
1106	serial_outp(info, UART_DLM, quot >> 8);		/* MS of divisor */
1107	if (info->type == PORT_16750)
1108		serial_outp(info, UART_FCR, fcr); 	/* set fcr */
1109	serial_outp(info, UART_LCR, cval);		/* reset DLAB */
1110	if (info->type != PORT_16750)
1111		serial_outp(info, UART_FCR, fcr); 	/* set fcr */
1112	restore_flags(flags);
1113	info->quot = quot;
1114}
1115
1116static void
1117su_put_char(struct tty_struct *tty, unsigned char ch)
1118{
1119	struct su_struct *info = (struct su_struct *)tty->driver_data;
1120	unsigned long flags;
1121
1122	if (serial_paranoia_check(info, tty->device, "su_put_char"))
1123		return;
1124
1125	if (!tty || !info->xmit_buf)
1126		return;
1127
1128	save_flags(flags); cli();
1129	if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1130		restore_flags(flags);
1131		return;
1132	}
1133
1134	info->xmit_buf[info->xmit_head++] = ch;
1135	info->xmit_head &= SERIAL_XMIT_SIZE-1;
1136	info->xmit_cnt++;
1137	restore_flags(flags);
1138}
1139
1140static void su_put_char_kbd(unsigned char c)
1141{
1142	struct su_struct *info = su_table;
1143	int lsr;
1144
1145	if (info->port_type != SU_PORT_KBD)
1146		++info;
1147	if (info->port_type != SU_PORT_KBD)
1148		return;
1149
1150	do {
1151		lsr = serial_in(info, UART_LSR);
1152	} while (!(lsr & UART_LSR_THRE));
1153
1154	/* Send the character out. */
1155	su_outb(info, UART_TX, c);
1156}
1157
1158static void
1159su_change_mouse_baud(int baud)
1160{
1161	struct su_struct *info = su_table;
1162
1163	if (info->port_type != SU_PORT_MS)
1164		++info;
1165	if (info->port_type != SU_PORT_MS)
1166		return;
1167
1168	info->cflag &= ~CBAUD;
1169	switch (baud) {
1170		case 1200:
1171			info->cflag |= B1200;
1172			break;
1173		case 2400:
1174			info->cflag |= B2400;
1175			break;
1176		case 4800:
1177			info->cflag |= B4800;
1178			break;
1179		case 9600:
1180			info->cflag |= B9600;
1181			break;
1182		default:
1183			printk("su_change_mouse_baud: unknown baud rate %d, "
1184			       "defaulting to 1200\n", baud);
1185			info->cflag |= 1200;
1186			break;
1187	}
1188	change_speed(info, 0);
1189}
1190
1191static void
1192su_flush_chars(struct tty_struct *tty)
1193{
1194	struct su_struct *info = (struct su_struct *)tty->driver_data;
1195	unsigned long flags;
1196
1197	if (serial_paranoia_check(info, tty->device, "su_flush_chars"))
1198		return;
1199
1200	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1201	    !info->xmit_buf)
1202		return;
1203
1204	save_flags(flags); cli();
1205	info->IER |= UART_IER_THRI;
1206	serial_out(info, UART_IER, info->IER);
1207	restore_flags(flags);
1208}
1209
1210static int
1211su_write(struct tty_struct * tty, int from_user,
1212		    const unsigned char *buf, int count)
1213{
1214	int	c, ret = 0;
1215	struct su_struct *info = (struct su_struct *)tty->driver_data;
1216	unsigned long flags;
1217
1218	if (serial_paranoia_check(info, tty->device, "su_write"))
1219		return 0;
1220
1221	if (!tty || !info->xmit_buf || !tmp_buf)
1222		return 0;
1223
1224	save_flags(flags);
1225	if (from_user) {
1226		down(&tmp_buf_sem);
1227		while (1) {
1228			c = MIN(count,
1229				MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1230				    SERIAL_XMIT_SIZE - info->xmit_head));
1231			if (c <= 0)
1232				break;
1233
1234			c -= copy_from_user(tmp_buf, buf, c);
1235			if (!c) {
1236				if (!ret)
1237					ret = -EFAULT;
1238				break;
1239			}
1240			cli();
1241			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1242				       SERIAL_XMIT_SIZE - info->xmit_head));
1243			memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1244			info->xmit_head = ((info->xmit_head + c) &
1245					   (SERIAL_XMIT_SIZE-1));
1246			info->xmit_cnt += c;
1247			restore_flags(flags);
1248			buf += c;
1249			count -= c;
1250			ret += c;
1251		}
1252		up(&tmp_buf_sem);
1253	} else {
1254		while (1) {
1255			cli();
1256			c = MIN(count,
1257				MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1258				    SERIAL_XMIT_SIZE - info->xmit_head));
1259			if (c <= 0) {
1260				restore_flags(flags);
1261				break;
1262			}
1263			memcpy(info->xmit_buf + info->xmit_head, buf, c);
1264			info->xmit_head = ((info->xmit_head + c) &
1265					   (SERIAL_XMIT_SIZE-1));
1266			info->xmit_cnt += c;
1267			restore_flags(flags);
1268			buf += c;
1269			count -= c;
1270			ret += c;
1271		}
1272	}
1273	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1274	    !(info->IER & UART_IER_THRI)) {
1275		info->IER |= UART_IER_THRI;
1276		serial_out(info, UART_IER, info->IER);
1277	}
1278	return ret;
1279}
1280
1281static int
1282su_write_room(struct tty_struct *tty)
1283{
1284	struct su_struct *info = (struct su_struct *)tty->driver_data;
1285	int	ret;
1286
1287	if (serial_paranoia_check(info, tty->device, "su_write_room"))
1288		return 0;
1289	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1290	if (ret < 0)
1291		ret = 0;
1292	return ret;
1293}
1294
1295static int
1296su_chars_in_buffer(struct tty_struct *tty)
1297{
1298	struct su_struct *info = (struct su_struct *)tty->driver_data;
1299
1300	if (serial_paranoia_check(info, tty->device, "su_chars_in_buffer"))
1301		return 0;
1302	return info->xmit_cnt;
1303}
1304
1305static void
1306su_flush_buffer(struct tty_struct *tty)
1307{
1308	struct su_struct *info = (struct su_struct *)tty->driver_data;
1309	unsigned long flags;
1310
1311	if (serial_paranoia_check(info, tty->device, "su_flush_buffer"))
1312		return;
1313	save_flags(flags); cli();
1314	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1315	restore_flags(flags);
1316	wake_up_interruptible(&tty->write_wait);
1317	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1318	    tty->ldisc.write_wakeup)
1319		(tty->ldisc.write_wakeup)(tty);
1320}
1321
1322/*
1323 * This function is used to send a high-priority XON/XOFF character to
1324 * the device
1325 */
1326static void
1327su_send_xchar(struct tty_struct *tty, char ch)
1328{
1329	struct su_struct *info = (struct su_struct *)tty->driver_data;
1330
1331	if (serial_paranoia_check(info, tty->device, "su_send_char"))
1332		return;
1333
1334	if (!(info->flags & ASYNC_INITIALIZED))
1335		return;
1336
1337	info->x_char = ch;
1338	if (ch) {
1339		/* Make sure transmit interrupts are on */
1340		info->IER |= UART_IER_THRI;
1341		serial_out(info, UART_IER, info->IER);
1342	}
1343}
1344
1345/*
1346 * ------------------------------------------------------------
1347 * su_throttle()
1348 *
1349 * This routine is called by the upper-layer tty layer to signal that
1350 * incoming characters should be throttled.
1351 * ------------------------------------------------------------
1352 */
1353static void
1354su_throttle(struct tty_struct * tty)
1355{
1356	struct su_struct *info = (struct su_struct *)tty->driver_data;
1357	unsigned long flags;
1358#ifdef SERIAL_DEBUG_THROTTLE
1359	char	buf[64];
1360
1361	printk("throttle %s: %d....\n", tty_name(tty, buf),
1362	       tty->ldisc.chars_in_buffer(tty));
1363#endif
1364
1365	if (serial_paranoia_check(info, tty->device, "su_throttle"))
1366		return;
1367
1368	if (I_IXOFF(tty))
1369		su_send_xchar(tty, STOP_CHAR(tty));
1370
1371	if (tty->termios->c_cflag & CRTSCTS)
1372		info->MCR &= ~UART_MCR_RTS;
1373
1374	save_flags(flags); cli();
1375	serial_out(info, UART_MCR, info->MCR);
1376	restore_flags(flags);
1377}
1378
1379static void
1380su_unthrottle(struct tty_struct * tty)
1381{
1382	struct su_struct *info = (struct su_struct *)tty->driver_data;
1383	unsigned long flags;
1384#ifdef SERIAL_DEBUG_THROTTLE
1385	char	buf[64];
1386
1387	printk("unthrottle %s: %d....\n", tty_name(tty, buf),
1388	       tty->ldisc.chars_in_buffer(tty));
1389#endif
1390
1391	if (serial_paranoia_check(info, tty->device, "su_unthrottle"))
1392		return;
1393
1394	if (I_IXOFF(tty)) {
1395		if (info->x_char)
1396			info->x_char = 0;
1397		else
1398			su_send_xchar(tty, START_CHAR(tty));
1399	}
1400	if (tty->termios->c_cflag & CRTSCTS)
1401		info->MCR |= UART_MCR_RTS;
1402	save_flags(flags); cli();
1403	serial_out(info, UART_MCR, info->MCR);
1404	restore_flags(flags);
1405}
1406
1407/*
1408 * ------------------------------------------------------------
1409 * su_ioctl() and friends
1410 * ------------------------------------------------------------
1411 */
1412
1413/*
1414 * get_serial_info - handle TIOCGSERIAL ioctl()
1415 *
1416 * Purpose: Return standard serial struct information about
1417 *          a serial port handled by this driver.
1418 *
1419 * Added:   11-May-2001 Lars Kellogg-Stedman <lars@larsshack.org>
1420 */
1421static int get_serial_info(struct su_struct * info,
1422			   struct serial_struct * retinfo)
1423{
1424	struct serial_struct	tmp;
1425
1426	if (!retinfo)
1427		return -EFAULT;
1428	memset(&tmp, 0, sizeof(tmp));
1429
1430	tmp.type		= info->type;
1431	tmp.line		= info->line;
1432	tmp.port		= info->port;
1433	tmp.irq			= info->irq;
1434	tmp.flags		= info->flags;
1435	tmp.xmit_fifo_size	= info->xmit_fifo_size;
1436	tmp.baud_base		= info->baud_base;
1437	tmp.close_delay		= info->close_delay;
1438	tmp.closing_wait	= info->closing_wait;
1439	tmp.custom_divisor	= info->custom_divisor;
1440	tmp.hub6		= 0;
1441
1442	if (copy_to_user(retinfo,&tmp,sizeof(*retinfo)))
1443		return -EFAULT;
1444
1445	return 0;
1446}
1447
1448/*
1449 * get_lsr_info - get line status register info
1450 *
1451 * Purpose: Let user call ioctl() to get info when the UART physically
1452 * 	    is emptied.  On bus types like RS485, the transmitter must
1453 * 	    release the bus after transmitting. This must be done when
1454 * 	    the transmit shift register is empty, not be done when the
1455 * 	    transmit holding register is empty.  This functionality
1456 * 	    allows an RS485 driver to be written in user space.
1457 */
1458static int
1459get_lsr_info(struct su_struct * info, unsigned int *value)
1460{
1461	unsigned char status;
1462	unsigned int result;
1463	unsigned long flags;
1464
1465	save_flags(flags); cli();
1466	status = serial_in(info, UART_LSR);
1467	restore_flags(flags);
1468	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1469	return put_user(result,value);
1470}
1471
1472
1473static int
1474get_modem_info(struct su_struct * info, unsigned int *value)
1475{
1476	unsigned char control, status;
1477	unsigned int result;
1478	unsigned long flags;
1479
1480	control = info->MCR;
1481	save_flags(flags); cli();
1482	status = serial_in(info, UART_MSR);
1483	restore_flags(flags);
1484	result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1485		| ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1486#ifdef TIOCM_OUT1
1487		| ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1488		| ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1489#endif
1490		| ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1491		| ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1492		| ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1493		| ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1494	return put_user(result,value);
1495}
1496
1497static int
1498set_modem_info(struct su_struct * info, unsigned int cmd, unsigned int *value)
1499{
1500	unsigned int arg;
1501	unsigned long flags;
1502
1503	if (get_user(arg, value))
1504		return -EFAULT;
1505	switch (cmd) {
1506	case TIOCMBIS:
1507		if (arg & TIOCM_RTS)
1508			info->MCR |= UART_MCR_RTS;
1509		if (arg & TIOCM_DTR)
1510			info->MCR |= UART_MCR_DTR;
1511#ifdef TIOCM_OUT1
1512		if (arg & TIOCM_OUT1)
1513			info->MCR |= UART_MCR_OUT1;
1514		if (arg & TIOCM_OUT2)
1515			info->MCR |= UART_MCR_OUT2;
1516#endif
1517		break;
1518	case TIOCMBIC:
1519		if (arg & TIOCM_RTS)
1520			info->MCR &= ~UART_MCR_RTS;
1521		if (arg & TIOCM_DTR)
1522			info->MCR &= ~UART_MCR_DTR;
1523#ifdef TIOCM_OUT1
1524		if (arg & TIOCM_OUT1)
1525			info->MCR &= ~UART_MCR_OUT1;
1526		if (arg & TIOCM_OUT2)
1527			info->MCR &= ~UART_MCR_OUT2;
1528#endif
1529		break;
1530	case TIOCMSET:
1531		info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1532#ifdef TIOCM_OUT1
1533					    UART_MCR_OUT1 |
1534					    UART_MCR_OUT2 |
1535#endif
1536					    UART_MCR_DTR))
1537			     | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1538#ifdef TIOCM_OUT1
1539			     | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1540			     | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1541#endif
1542			     | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1543		break;
1544	default:
1545		return -EINVAL;
1546	}
1547	save_flags(flags); cli();
1548	serial_out(info, UART_MCR, info->MCR);
1549	restore_flags(flags);
1550	return 0;
1551}
1552
1553/*
1554 * su_break() --- routine which turns the break handling on or off
1555 */
1556static void
1557su_break(struct tty_struct *tty, int break_state)
1558{
1559	struct su_struct * info = (struct su_struct *)tty->driver_data;
1560	unsigned long flags;
1561
1562	if (serial_paranoia_check(info, tty->device, "su_break"))
1563		return;
1564
1565	if (!info->port)
1566		return;
1567	save_flags(flags); cli();
1568	if (break_state == -1)
1569		serial_out(info, UART_LCR,
1570			   serial_inp(info, UART_LCR) | UART_LCR_SBC);
1571	else
1572		serial_out(info, UART_LCR,
1573			   serial_inp(info, UART_LCR) & ~UART_LCR_SBC);
1574	restore_flags(flags);
1575}
1576
1577static int
1578su_ioctl(struct tty_struct *tty, struct file * file,
1579		    unsigned int cmd, unsigned long arg)
1580{
1581	struct su_struct * info = (struct su_struct *)tty->driver_data;
1582	struct async_icount cprev, cnow;	/* kernel counter temps */
1583	struct serial_icounter_struct *p_cuser;	/* user space */
1584
1585	if (serial_paranoia_check(info, tty->device, "su_ioctl"))
1586		return -ENODEV;
1587
1588	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1589	    (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT) &&
1590	    (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1591		if (tty->flags & (1 << TTY_IO_ERROR))
1592		    return -EIO;
1593	}
1594
1595	switch (cmd) {
1596		case TIOCMGET:
1597			return get_modem_info(info, (unsigned int *) arg);
1598		case TIOCMBIS:
1599		case TIOCMBIC:
1600		case TIOCMSET:
1601			return set_modem_info(info, cmd, (unsigned int *) arg);
1602
1603		case TIOCGSERIAL:
1604			return get_serial_info(info, (struct serial_struct *)arg);
1605
1606		case TIOCSERGETLSR: /* Get line status register */
1607			return get_lsr_info(info, (unsigned int *) arg);
1608
1609
1610		/*
1611		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1612		 * - mask passed in arg for lines of interest
1613 		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1614		 * Caller should use TIOCGICOUNT to see which one it was
1615		 */
1616		case TIOCMIWAIT:
1617			cli();
1618			/* note the counters on entry */
1619			cprev = info->icount;
1620			sti();
1621			while (1) {
1622				interruptible_sleep_on(&info->delta_msr_wait);
1623				/* see if a signal did it */
1624				if (signal_pending(current))
1625					return -ERESTARTSYS;
1626				cli();
1627				cnow = info->icount; /* atomic copy */
1628				sti();
1629				if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1630				    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1631					return -EIO; /* no change => error */
1632				if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1633				     ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1634				     ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1635				     ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1636					return 0;
1637				}
1638				cprev = cnow;
1639			}
1640			/* NOTREACHED */
1641
1642		/*
1643		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1644		 * Return: write counters to the user passed counter struct
1645		 * NB: both 1->0 and 0->1 transitions are counted except for
1646		 *     RI where only 0->1 is counted.
1647		 */
1648		case TIOCGICOUNT:
1649			cli();
1650			cnow = info->icount;
1651			sti();
1652			p_cuser = (struct serial_icounter_struct *) arg;
1653			if (put_user(cnow.cts, &p_cuser->cts) ||
1654			    put_user(cnow.dsr, &p_cuser->dsr) ||
1655			    put_user(cnow.rng, &p_cuser->rng) ||
1656			    put_user(cnow.dcd, &p_cuser->dcd))
1657				return -EFAULT;
1658			return 0;
1659
1660		default:
1661			return -ENOIOCTLCMD;
1662		}
1663	/* return 0; */ /* Trigger warnings if fall through by a chance. */
1664}
1665
1666static void
1667su_set_termios(struct tty_struct *tty, struct termios *old_termios)
1668{
1669	struct su_struct *info = (struct su_struct *)tty->driver_data;
1670	unsigned long flags;
1671
1672	if (   (tty->termios->c_cflag == old_termios->c_cflag)
1673	    && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1674		== RELEVANT_IFLAG(old_termios->c_iflag)))
1675	  return;
1676
1677	change_speed(info, old_termios);
1678
1679	/* Handle transition to B0 status */
1680	if ((old_termios->c_cflag & CBAUD) &&
1681	    !(tty->termios->c_cflag & CBAUD)) {
1682		info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1683		save_flags(flags); cli();
1684		serial_out(info, UART_MCR, info->MCR);
1685		restore_flags(flags);
1686	}
1687
1688	/* Handle transition away from B0 status */
1689	if (!(old_termios->c_cflag & CBAUD) &&
1690	    (tty->termios->c_cflag & CBAUD)) {
1691		info->MCR |= UART_MCR_DTR;
1692		if (!(tty->termios->c_cflag & CRTSCTS) ||
1693		    !test_bit(TTY_THROTTLED, &tty->flags)) {
1694			info->MCR |= UART_MCR_RTS;
1695		}
1696		save_flags(flags); cli();
1697		serial_out(info, UART_MCR, info->MCR);
1698		restore_flags(flags);
1699	}
1700
1701	/* Handle turning off CRTSCTS */
1702	if ((old_termios->c_cflag & CRTSCTS) &&
1703	    !(tty->termios->c_cflag & CRTSCTS)) {
1704		tty->hw_stopped = 0;
1705		su_start(tty);
1706	}
1707
1708}
1709
1710/*
1711 * ------------------------------------------------------------
1712 * su_close()
1713 *
1714 * This routine is called when the serial port gets closed.  First, we
1715 * wait for the last remaining data to be sent.  Then, we unlink its
1716 * async structure from the interrupt chain if necessary, and we free
1717 * that IRQ if nothing is left in the chain.
1718 * ------------------------------------------------------------
1719 */
1720static void
1721su_close(struct tty_struct *tty, struct file * filp)
1722{
1723	struct su_struct *info = (struct su_struct *)tty->driver_data;
1724	unsigned long flags;
1725
1726	if (!info || serial_paranoia_check(info, tty->device, "su_close"))
1727		return;
1728
1729	save_flags(flags); cli();
1730
1731	if (tty_hung_up_p(filp)) {
1732		DBG_CNT("before DEC-hung");
1733		MOD_DEC_USE_COUNT;
1734		restore_flags(flags);
1735		return;
1736	}
1737
1738#ifdef SERIAL_DEBUG_OPEN
1739	printk("su_close ttys%d, count = %d\n", info->line, info->count);
1740#endif
1741	if ((tty->count == 1) && (info->count != 1)) {
1742		/*
1743		 * Uh, oh.  tty->count is 1, which means that the tty
1744		 * structure will be freed.  info->count should always
1745		 * be one in these conditions.  If it's greater than
1746		 * one, we've got real problems, since it means the
1747		 * serial port won't be shutdown.
1748		 */
1749		printk("su_close: bad serial port count; tty->count is 1, "
1750		       "info->count is %d\n", info->count);
1751		info->count = 1;
1752	}
1753	if (--info->count < 0) {
1754		printk("su_close: bad serial port count for ttys%d: %d\n",
1755		       info->line, info->count);
1756		info->count = 0;
1757	}
1758	if (info->count) {
1759		DBG_CNT("before DEC-2");
1760		MOD_DEC_USE_COUNT;
1761		restore_flags(flags);
1762		return;
1763	}
1764	info->flags |= ASYNC_CLOSING;
1765	/*
1766	 * Save the termios structure, since this port may have
1767	 * separate termios for callout and dialin.
1768	 */
1769	if (info->flags & ASYNC_NORMAL_ACTIVE)
1770		info->normal_termios = *tty->termios;
1771	if (info->flags & ASYNC_CALLOUT_ACTIVE)
1772		info->callout_termios = *tty->termios;
1773	/*
1774	 * Now we wait for the transmit buffer to clear; and we notify
1775	 * the line discipline to only process XON/XOFF characters.
1776	 */
1777	tty->closing = 1;
1778	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1779		tty_wait_until_sent(tty, info->closing_wait);
1780	/*
1781	 * At this point we stop accepting input.  To do this, we
1782	 * disable the receive line status interrupts, and tell the
1783	 * interrupt driver to stop checking the data ready bit in the
1784	 * line status register.
1785	 */
1786	info->IER &= ~UART_IER_RLSI;
1787	info->read_status_mask &= ~UART_LSR_DR;
1788	if (info->flags & ASYNC_INITIALIZED) {
1789		serial_out(info, UART_IER, info->IER);
1790		/*
1791		 * Before we drop DTR, make sure the UART transmitter
1792		 * has completely drained; this is especially
1793		 * important if there is a transmit FIFO!
1794		 */
1795		su_wait_until_sent(tty, info->timeout);
1796	}
1797	shutdown(info);
1798	if (tty->driver.flush_buffer)
1799		tty->driver.flush_buffer(tty);
1800	if (tty->ldisc.flush_buffer)
1801		tty->ldisc.flush_buffer(tty);
1802	tty->closing = 0;
1803	info->event = 0;
1804	info->tty = 0;
1805	if (info->blocked_open) {
1806		if (info->close_delay) {
1807			current->state = TASK_INTERRUPTIBLE;
1808			schedule_timeout(info->close_delay);
1809		}
1810		wake_up_interruptible(&info->open_wait);
1811	}
1812	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1813			 ASYNC_CLOSING);
1814	wake_up_interruptible(&info->close_wait);
1815	MOD_DEC_USE_COUNT;
1816	restore_flags(flags);
1817}
1818
1819/*
1820 * su_wait_until_sent() --- wait until the transmitter is empty
1821 */
1822static void
1823su_wait_until_sent(struct tty_struct *tty, int timeout)
1824{
1825	struct su_struct * info = (struct su_struct *)tty->driver_data;
1826	unsigned long orig_jiffies, char_time;
1827	int lsr;
1828
1829	if (serial_paranoia_check(info, tty->device, "su_wait_until_sent"))
1830		return;
1831
1832	if (info->type == PORT_UNKNOWN)
1833		return;
1834
1835	if (info->xmit_fifo_size == 0)
1836		return; /* Just in case ... */
1837
1838	orig_jiffies = jiffies;
1839	/*
1840	 * Set the check interval to be 1/5 of the estimated time to
1841	 * send a single character, and make it at least 1.  The check
1842	 * interval should also be less than the timeout.
1843	 *
1844	 * Note: we have to use pretty tight timings here to satisfy
1845	 * the NIST-PCTS.
1846	 */
1847	char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1848	char_time = char_time / 5;
1849	if (char_time == 0)
1850		char_time = 1;
1851	if (timeout)
1852	  char_time = MIN(char_time, timeout);
1853#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1854	printk("In su_wait_until_sent(%d) check=%lu...", timeout, char_time);
1855	printk("jiff=%lu...", jiffies);
1856#endif
1857	while (!((lsr = serial_inp(info, UART_LSR)) & UART_LSR_TEMT)) {
1858#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1859		printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1860#endif
1861		current->state = TASK_INTERRUPTIBLE;
1862		schedule_timeout(char_time);
1863		if (signal_pending(current))
1864			break;
1865		if (timeout && time_after(jiffies, orig_jiffies + timeout))
1866			break;
1867	}
1868#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1869	printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1870#endif
1871}
1872
1873/*
1874 * su_hangup() --- called by tty_hangup() when a hangup is signaled.
1875 */
1876static void
1877su_hangup(struct tty_struct *tty)
1878{
1879	struct su_struct * info = (struct su_struct *)tty->driver_data;
1880
1881	if (serial_paranoia_check(info, tty->device, "su_hangup"))
1882		return;
1883
1884	su_flush_buffer(tty);
1885	shutdown(info);
1886	info->event = 0;
1887	info->count = 0;
1888	info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1889	info->tty = 0;
1890	wake_up_interruptible(&info->open_wait);
1891}
1892
1893/*
1894 * ------------------------------------------------------------
1895 * su_open() and friends
1896 * ------------------------------------------------------------
1897 */
1898static int
1899block_til_ready(struct tty_struct *tty, struct file * filp,
1900		struct su_struct *info)
1901{
1902	DECLARE_WAITQUEUE(wait, current);
1903	int		  retval;
1904	int		  do_clocal = 0, extra_count = 0;
1905	unsigned long	  flags;
1906
1907	/*
1908	 * If the device is in the middle of being closed, then block
1909	 * until it's done, and then try again.
1910	 */
1911	if (tty_hung_up_p(filp) ||
1912	    (info->flags & ASYNC_CLOSING)) {
1913		if (info->flags & ASYNC_CLOSING)
1914			interruptible_sleep_on(&info->close_wait);
1915#ifdef SERIAL_DO_RESTART
1916		return ((info->flags & ASYNC_HUP_NOTIFY) ?
1917			-EAGAIN : -ERESTARTSYS);
1918#else
1919		return -EAGAIN;
1920#endif
1921	}
1922
1923	/*
1924	 * If this is a callout device, then just make sure the normal
1925	 * device isn't being used.
1926	 */
1927	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1928		if (info->flags & ASYNC_NORMAL_ACTIVE)
1929			return -EBUSY;
1930		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1931		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1932		    (info->session != current->session))
1933		    return -EBUSY;
1934		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1935		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1936		    (info->pgrp != current->pgrp))
1937		    return -EBUSY;
1938		info->flags |= ASYNC_CALLOUT_ACTIVE;
1939		return 0;
1940	}
1941
1942	/*
1943	 * If non-blocking mode is set, or the port is not enabled,
1944	 * then make the check up front and then exit.
1945	 */
1946	if ((filp->f_flags & O_NONBLOCK) ||
1947	    (tty->flags & (1 << TTY_IO_ERROR))) {
1948		if (info->flags & ASYNC_CALLOUT_ACTIVE)
1949			return -EBUSY;
1950		info->flags |= ASYNC_NORMAL_ACTIVE;
1951		return 0;
1952	}
1953
1954	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1955		if (info->normal_termios.c_cflag & CLOCAL)
1956			do_clocal = 1;
1957	} else {
1958		if (tty->termios->c_cflag & CLOCAL)
1959			do_clocal = 1;
1960	}
1961
1962	/*
1963	 * Block waiting for the carrier detect and the line to become
1964	 * free (i.e., not in use by the callout).  While we are in
1965	 * this loop, info->count is dropped by one, so that
1966	 * su_close() knows when to free things.  We restore it upon
1967	 * exit, either normal or abnormal.
1968	 */
1969	retval = 0;
1970	add_wait_queue(&info->open_wait, &wait);
1971#ifdef SERIAL_DEBUG_OPEN
1972	printk("block_til_ready before block: ttys%d, count = %d\n",
1973	       info->line, info->count);
1974#endif
1975	save_flags(flags); cli();
1976	if (!tty_hung_up_p(filp)) {
1977		extra_count = 1;
1978		info->count--;
1979	}
1980	restore_flags(flags);
1981	info->blocked_open++;
1982	while (1) {
1983		save_flags(flags); cli();
1984		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1985		    (tty->termios->c_cflag & CBAUD))
1986			serial_out(info, UART_MCR,
1987				   serial_inp(info, UART_MCR) |
1988				   (UART_MCR_DTR | UART_MCR_RTS));
1989		restore_flags(flags);
1990		set_current_state(TASK_INTERRUPTIBLE);
1991		if (tty_hung_up_p(filp) ||
1992		    !(info->flags & ASYNC_INITIALIZED)) {
1993#ifdef SERIAL_DO_RESTART
1994			if (info->flags & ASYNC_HUP_NOTIFY)
1995				retval = -EAGAIN;
1996			else
1997				retval = -ERESTARTSYS;
1998#else
1999			retval = -EAGAIN;
2000#endif
2001			break;
2002		}
2003		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2004		    !(info->flags & ASYNC_CLOSING) &&
2005		    (do_clocal || (serial_in(info, UART_MSR) &
2006				   UART_MSR_DCD)))
2007			break;
2008		if (signal_pending(current)) {
2009			retval = -ERESTARTSYS;
2010			break;
2011		}
2012#ifdef SERIAL_DEBUG_OPEN
2013		printk("block_til_ready blocking: ttys%d, count = %d\n",
2014		       info->line, info->count);
2015#endif
2016		schedule();
2017	}
2018	current->state = TASK_RUNNING;
2019	remove_wait_queue(&info->open_wait, &wait);
2020	if (extra_count)
2021		info->count++;
2022	info->blocked_open--;
2023#ifdef SERIAL_DEBUG_OPEN
2024	printk("block_til_ready after blocking: ttys%d, count = %d\n",
2025	       info->line, info->count);
2026#endif
2027	if (retval)
2028		return retval;
2029	info->flags |= ASYNC_NORMAL_ACTIVE;
2030	return 0;
2031}
2032
2033/*
2034 * This routine is called whenever a serial port is opened.  It
2035 * enables interrupts for a serial port, linking in its async structure into
2036 * the IRQ chain.   It also performs the serial-specific
2037 * initialization for the tty structure.
2038 */
2039static int
2040su_open(struct tty_struct *tty, struct file * filp)
2041{
2042	struct su_struct	*info;
2043	int 			retval, line;
2044	unsigned long		page;
2045
2046	line = MINOR(tty->device) - tty->driver.minor_start;
2047	if ((line < 0) || (line >= NR_PORTS))
2048		return -ENODEV;
2049	info = su_table + line;
2050	info->count++;
2051	tty->driver_data = info;
2052	info->tty = tty;
2053
2054	if (serial_paranoia_check(info, tty->device, "su_open")) {
2055		info->count--;
2056		return -ENODEV;
2057	}
2058
2059#ifdef SERIAL_DEBUG_OPEN
2060	printk("su_open %s%d, count = %d\n", tty->driver.name, info->line,
2061	       info->count);
2062#endif
2063	info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2064
2065	if (!tmp_buf) {
2066		page = get_free_page(GFP_KERNEL);
2067		if (!page)
2068			return -ENOMEM;
2069		if (tmp_buf)
2070			free_page(page);
2071		else
2072			tmp_buf = (unsigned char *) page;
2073	}
2074
2075	/*
2076	 * If the port is the middle of closing, bail out now
2077	 */
2078	if (tty_hung_up_p(filp) ||
2079	    (info->flags & ASYNC_CLOSING)) {
2080		if (info->flags & ASYNC_CLOSING)
2081			interruptible_sleep_on(&info->close_wait);
2082#ifdef SERIAL_DO_RESTART
2083		return ((info->flags & ASYNC_HUP_NOTIFY) ?
2084			-EAGAIN : -ERESTARTSYS);
2085#else
2086		return -EAGAIN;
2087#endif
2088	}
2089
2090	/*
2091	 * Start up serial port
2092	 */
2093	retval = startup(info);
2094	if (retval)
2095		return retval;
2096
2097	MOD_INC_USE_COUNT;
2098	retval = block_til_ready(tty, filp, info);
2099	if (retval) {
2100#ifdef SERIAL_DEBUG_OPEN
2101		printk("su_open returning after block_til_ready with %d\n",
2102		       retval);
2103#endif
2104		return retval;
2105	}
2106
2107	if ((info->count == 1) &&
2108	    (info->flags & ASYNC_SPLIT_TERMIOS)) {
2109		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2110			*tty->termios = info->normal_termios;
2111		else
2112			*tty->termios = info->callout_termios;
2113		change_speed(info, 0);
2114	}
2115#ifdef CONFIG_SERIAL_CONSOLE
2116	if (sercons.cflag && sercons.index == line) {
2117		tty->termios->c_cflag = sercons.cflag;
2118		sercons.cflag = 0;
2119		change_speed(info, 0);
2120	}
2121#endif
2122	info->session = current->session;
2123	info->pgrp = current->pgrp;
2124
2125#ifdef SERIAL_DEBUG_OPEN
2126	printk("su_open ttys%d successful...", info->line);
2127#endif
2128	return 0;
2129}
2130
2131/*
2132 * /proc fs routines....
2133 */
2134static int
2135line_info(char *buf, struct su_struct *info)
2136{
2137	char		stat_buf[30], control, status;
2138	int		ret;
2139	unsigned long	flags;
2140
2141	if (info->port == 0 || info->type == PORT_UNKNOWN)
2142		return 0;
2143
2144	ret = sprintf(buf, "%u: uart:%s port:%lX irq:%s",
2145		      info->line, uart_config[info->type].name,
2146		      (unsigned long)info->port, __irq_itoa(info->irq));
2147
2148	/*
2149	 * Figure out the current RS-232 lines
2150	 */
2151	save_flags(flags); cli();
2152	status = serial_in(info, UART_MSR);
2153	control = info ? info->MCR : serial_in(info, UART_MCR);
2154	restore_flags(flags);
2155
2156	stat_buf[0] = 0;
2157	stat_buf[1] = 0;
2158	if (control & UART_MCR_RTS)
2159		strcat(stat_buf, "|RTS");
2160	if (status & UART_MSR_CTS)
2161		strcat(stat_buf, "|CTS");
2162	if (control & UART_MCR_DTR)
2163		strcat(stat_buf, "|DTR");
2164	if (status & UART_MSR_DSR)
2165		strcat(stat_buf, "|DSR");
2166	if (status & UART_MSR_DCD)
2167		strcat(stat_buf, "|CD");
2168	if (status & UART_MSR_RI)
2169		strcat(stat_buf, "|RI");
2170
2171	if (info->quot) {
2172		ret += sprintf(buf+ret, " baud:%u",
2173			       info->baud_base / info->quot);
2174	}
2175
2176	ret += sprintf(buf+ret, " tx:%u rx:%u",
2177		       info->icount.tx, info->icount.rx);
2178
2179	if (info->icount.frame)
2180		ret += sprintf(buf+ret, " fe:%u", info->icount.frame);
2181
2182	if (info->icount.parity)
2183		ret += sprintf(buf+ret, " pe:%u", info->icount.parity);
2184
2185	if (info->icount.brk)
2186		ret += sprintf(buf+ret, " brk:%u", info->icount.brk);
2187
2188	if (info->icount.overrun)
2189		ret += sprintf(buf+ret, " oe:%u", info->icount.overrun);
2190
2191	/*
2192	 * Last thing is the RS-232 status lines
2193	 */
2194	ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2195	return ret;
2196}
2197
2198int su_read_proc(char *page, char **start, off_t off, int count,
2199		 int *eof, void *data)
2200{
2201	int i, len = 0;
2202	off_t	begin = 0;
2203
2204	len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2205	for (i = 0; i < NR_PORTS && len < 4000; i++) {
2206		len += line_info(page + len, &su_table[i]);
2207		if (len+begin > off+count)
2208			goto done;
2209		if (len+begin < off) {
2210			begin += len;
2211			len = 0;
2212		}
2213	}
2214	*eof = 1;
2215done:
2216	if (off >= len+begin)
2217		return 0;
2218	*start = page + (off-begin);
2219	return ((count < begin+len-off) ? count : begin+len-off);
2220}
2221
2222/*
2223 * ---------------------------------------------------------------------
2224 * su_XXX_init() and friends
2225 *
2226 * su_XXX_init() is called at boot-time to initialize the serial driver.
2227 * ---------------------------------------------------------------------
2228 */
2229
2230/*
2231 * This routine prints out the appropriate serial driver version
2232 * number, and identifies which options were configured into this
2233 * driver.
2234 */
2235static __inline__ void __init show_su_version(void)
2236{
2237	char *revision = "$Revision: 1.1.1.1 $";
2238	char *version, *p;
2239
2240	version = strchr(revision, ' ');
2241	strcpy(serial_version, ++version);
2242	p = strchr(serial_version, ' ');
2243	*p = '\0';
2244 	printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2245}
2246
2247/*
2248 * This routine is called by su_{serial|kbd_ms}_init() to initialize a specific
2249 * serial port.  It determines what type of UART chip this serial port is
2250 * using: 8250, 16450, 16550, 16550A.  The important question is
2251 * whether or not this UART is a 16550A, since this will determine
2252 * whether or not we can use its FIFO features.
2253 */
2254static void
2255autoconfig(struct su_struct *info)
2256{
2257	unsigned char status1, status2, scratch, scratch2;
2258	struct linux_ebus_device *dev = 0;
2259	struct linux_ebus *ebus;
2260#ifdef CONFIG_SPARC64
2261	struct isa_bridge *isa_br;
2262	struct isa_device *isa_dev;
2263#endif
2264#ifndef __sparc_v9__
2265	struct linux_prom_registers reg0;
2266#endif
2267	unsigned long flags;
2268
2269	if (!info->port_node || !info->port_type)
2270		return;
2271
2272	/*
2273	 * First we look for Ebus-bases su's
2274	 */
2275	for_each_ebus(ebus) {
2276		for_each_ebusdev(dev, ebus) {
2277			if (dev->prom_node == info->port_node) {
2278				info->port = dev->resource[0].start;
2279				info->irq = dev->irqs[0];
2280				goto ebus_done;
2281			}
2282		}
2283	}
2284
2285#ifdef CONFIG_SPARC64
2286	for_each_isa(isa_br) {
2287		for_each_isadev(isa_dev, isa_br) {
2288			if (isa_dev->prom_node == info->port_node) {
2289				info->port = isa_dev->resource.start;
2290				info->irq = isa_dev->irq;
2291				goto ebus_done;
2292			}
2293		}
2294	}
2295#endif
2296
2297#ifdef __sparc_v9__
2298	/*
2299	 * Not on Ebus, bailing.
2300	 */
2301	return;
2302#else
2303	/*
2304	 * Not on Ebus, must be OBIO.
2305	 */
2306	if (prom_getproperty(info->port_node, "reg",
2307	    (char *)&reg0, sizeof(reg0)) == -1) {
2308		prom_printf("su: no \"reg\" property\n");
2309		return;
2310	}
2311	prom_apply_obio_ranges(&reg0, 1);
2312	if (reg0.which_io != 0) {	/* Just in case... */
2313		prom_printf("su: bus number nonzero: 0x%x:%x\n",
2314		    reg0.which_io, reg0.phys_addr);
2315		return;
2316	}
2317	if ((info->port = (unsigned long) ioremap(reg0.phys_addr,
2318	    reg0.reg_size)) == 0) {
2319		prom_printf("su: cannot map\n");
2320		return;
2321	}
2322
2323	/*
2324	 * There is no intr property on MrCoffee, so hardwire it.
2325	 */
2326	info->irq = IRQ_4M(13);
2327#endif
2328
2329ebus_done:
2330
2331#ifdef SERIAL_DEBUG_OPEN
2332	printk("Found 'su' at %016lx IRQ %s\n", info->port,
2333		__irq_itoa(info->irq));
2334#endif
2335
2336	info->magic = SERIAL_MAGIC;
2337
2338	save_flags(flags); cli();
2339
2340	/*
2341	 * Do a simple existence test first; if we fail this, there's
2342	 * no point trying anything else.
2343	 *
2344	 * 0x80 is used as a nonsense port to prevent against false
2345	 * positives due to ISA bus float.  The assumption is that
2346	 * 0x80 is a non-existent port; which should be safe since
2347	 * include/asm/io.h also makes this assumption.
2348	 */
2349	scratch = serial_inp(info, UART_IER);
2350	serial_outp(info, UART_IER, 0);
2351	scratch2 = serial_inp(info, UART_IER);
2352	serial_outp(info, UART_IER, scratch);
2353	if (scratch2) {
2354		restore_flags(flags);
2355		return;		/* We failed; there's nothing here */
2356	}
2357
2358	scratch = serial_inp(info, UART_MCR);
2359	serial_outp(info, UART_MCR, UART_MCR_LOOP | scratch);
2360	serial_outp(info, UART_MCR, UART_MCR_LOOP | 0x0A);
2361	status1 = serial_inp(info, UART_MSR) & 0xF0;
2362	serial_outp(info, UART_MCR, scratch);
2363	if (status1 != 0x90) {
2364		/*
2365		 * This code fragment used to fail, now it fixed itself.
2366		 * We keep the printout for a case.
2367		 */
2368		printk("su: loopback returned status 0x%02x\n", status1);
2369		restore_flags(flags);
2370		return;
2371	}
2372
2373	scratch2 = serial_in(info, UART_LCR);
2374	serial_outp(info, UART_LCR, 0xBF);	/* set up for StarTech test */
2375	serial_outp(info, UART_EFR, 0);		/* EFR is the same as FCR */
2376	serial_outp(info, UART_LCR, 0);
2377	serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2378	scratch = serial_in(info, UART_IIR) >> 6;
2379	switch (scratch) {
2380		case 0:
2381			info->type = PORT_16450;
2382			break;
2383		case 1:
2384			info->type = PORT_UNKNOWN;
2385			break;
2386		case 2:
2387			info->type = PORT_16550;
2388			break;
2389		case 3:
2390			info->type = PORT_16550A;
2391			break;
2392	}
2393	if (info->type == PORT_16550A) {
2394		/* Check for Startech UART's */
2395		serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2396		if (serial_in(info, UART_EFR) == 0) {
2397			info->type = PORT_16650;
2398		} else {
2399			serial_outp(info, UART_LCR, 0xBF);
2400			if (serial_in(info, UART_EFR) == 0)
2401				info->type = PORT_16650V2;
2402		}
2403	}
2404	if (info->type == PORT_16550A) {
2405		/* Check for TI 16750 */
2406		serial_outp(info, UART_LCR, scratch2 | UART_LCR_DLAB);
2407		serial_outp(info, UART_FCR,
2408			    UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE);
2409		scratch = serial_in(info, UART_IIR) >> 5;
2410		if (scratch == 7) {
2411			serial_outp(info, UART_LCR, 0);
2412			serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2413			scratch = serial_in(info, UART_IIR) >> 5;
2414			if (scratch == 6)
2415				info->type = PORT_16750;
2416		}
2417		serial_outp(info, UART_FCR, UART_FCR_ENABLE_FIFO);
2418	}
2419	serial_outp(info, UART_LCR, scratch2);
2420	if (info->type == PORT_16450) {
2421		scratch = serial_in(info, UART_SCR);
2422		serial_outp(info, UART_SCR, 0xa5);
2423		status1 = serial_in(info, UART_SCR);
2424		serial_outp(info, UART_SCR, 0x5a);
2425		status2 = serial_in(info, UART_SCR);
2426		serial_outp(info, UART_SCR, scratch);
2427
2428		if ((status1 != 0xa5) || (status2 != 0x5a))
2429			info->type = PORT_8250;
2430	}
2431	info->xmit_fifo_size = uart_config[info->type].dfl_xmit_fifo_size;
2432
2433	if (info->type == PORT_UNKNOWN) {
2434		restore_flags(flags);
2435		return;
2436	}
2437
2438	sprintf(info->name, "su(%s)", su_typev[info->port_type]);
2439
2440	/*
2441	 * Reset the UART.
2442	 */
2443	serial_outp(info, UART_MCR, 0x00);
2444	serial_outp(info, UART_FCR, (UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT));
2445	(void)serial_in(info, UART_RX);
2446	serial_outp(info, UART_IER, 0x00);
2447
2448	restore_flags(flags);
2449}
2450
2451/* This is used by the SAB driver to adjust where its minor
2452 * numbers start, we always are probed for first.
2453 */
2454int su_num_ports = 0;
2455EXPORT_SYMBOL(su_num_ports);
2456
2457/*
2458 * The serial driver boot-time initialization code!
2459 */
2460int __init su_serial_init(void)
2461{
2462	int i;
2463	struct su_struct *info;
2464
2465	init_bh(SERIAL_BH, do_serial_bh);
2466	show_su_version();
2467
2468	/* Initialize the tty_driver structure */
2469
2470	memset(&serial_driver, 0, sizeof(struct tty_driver));
2471	serial_driver.magic = TTY_DRIVER_MAGIC;
2472	serial_driver.driver_name = "su";
2473#ifdef CONFIG_DEVFS_FS
2474	serial_driver.name = "tts/%d";
2475#else
2476	serial_driver.name = "ttyS";
2477#endif
2478	serial_driver.major = TTY_MAJOR;
2479	serial_driver.minor_start = 64;
2480	serial_driver.num = NR_PORTS;
2481	serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2482	serial_driver.subtype = SERIAL_TYPE_NORMAL;
2483	serial_driver.init_termios = tty_std_termios;
2484	serial_driver.init_termios.c_cflag =
2485		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2486	serial_driver.flags = TTY_DRIVER_REAL_RAW;
2487	serial_driver.refcount = &serial_refcount;
2488	serial_driver.table = serial_table;
2489	serial_driver.termios = serial_termios;
2490	serial_driver.termios_locked = serial_termios_locked;
2491
2492	serial_driver.open = su_open;
2493	serial_driver.close = su_close;
2494	serial_driver.write = su_write;
2495	serial_driver.put_char = su_put_char;
2496	serial_driver.flush_chars = su_flush_chars;
2497	serial_driver.write_room = su_write_room;
2498	serial_driver.chars_in_buffer = su_chars_in_buffer;
2499	serial_driver.flush_buffer = su_flush_buffer;
2500	serial_driver.ioctl = su_ioctl;
2501	serial_driver.throttle = su_throttle;
2502	serial_driver.unthrottle = su_unthrottle;
2503	serial_driver.send_xchar = su_send_xchar;
2504	serial_driver.set_termios = su_set_termios;
2505	serial_driver.stop = su_stop;
2506	serial_driver.start = su_start;
2507	serial_driver.hangup = su_hangup;
2508	serial_driver.break_ctl = su_break;
2509	serial_driver.wait_until_sent = su_wait_until_sent;
2510	serial_driver.read_proc = su_read_proc;
2511
2512	/*
2513	 * The callout device is just like normal device except for
2514	 * major number and the subtype code.
2515	 */
2516	callout_driver = serial_driver;
2517#ifdef CONFIG_DEVFS_FS
2518	callout_driver.name = "cua/%d";
2519#else
2520	callout_driver.name = "cua";
2521#endif
2522	callout_driver.major = TTYAUX_MAJOR;
2523	callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2524	callout_driver.read_proc = 0;
2525	callout_driver.proc_entry = 0;
2526
2527	if (tty_register_driver(&serial_driver))
2528		panic("Couldn't register regular su\n");
2529	if (tty_register_driver(&callout_driver))
2530		panic("Couldn't register callout su\n");
2531
2532	for (i = 0, info = su_table; i < NR_PORTS; i++, info++) {
2533		info->line = i;
2534		info->type = PORT_UNKNOWN;
2535		info->baud_base = BAUD_BASE;
2536		/* info->flags = 0; */
2537		info->custom_divisor = 0;
2538		info->close_delay = 5*HZ/10;
2539		info->closing_wait = 30*HZ;
2540		info->callout_termios = callout_driver.init_termios;
2541		info->normal_termios = serial_driver.init_termios;
2542		info->icount.cts = info->icount.dsr =
2543			info->icount.rng = info->icount.dcd = 0;
2544		info->icount.rx = info->icount.tx = 0;
2545		info->icount.frame = info->icount.parity = 0;
2546		info->icount.overrun = info->icount.brk = 0;
2547		info->tqueue.routine = do_softint;
2548		info->tqueue.data = info;
2549		info->cflag = serial_driver.init_termios.c_cflag;
2550		init_waitqueue_head(&info->open_wait);
2551		init_waitqueue_head(&info->close_wait);
2552		init_waitqueue_head(&info->delta_msr_wait);
2553
2554		autoconfig(info);
2555		if (info->type == PORT_UNKNOWN)
2556			continue;
2557
2558		printk(KERN_INFO "%s at 0x%lx (tty %d irq %s) is a %s\n",
2559		       info->name, (long)info->port, i, __irq_itoa(info->irq),
2560		       uart_config[info->type].name);
2561	}
2562
2563	for (i = 0, info = su_table; i < NR_PORTS; i++, info++)
2564		if (info->type == PORT_UNKNOWN)
2565			break;
2566
2567	su_num_ports = i;
2568	serial_driver.num = callout_driver.num = i;
2569
2570	return 0;
2571}
2572
2573int __init su_kbd_ms_init(void)
2574{
2575	int i;
2576	struct su_struct *info;
2577
2578	show_su_version();
2579
2580	for (i = 0, info = su_table; i < 2; i++, info++) {
2581		info->line = i;
2582		info->type = PORT_UNKNOWN;
2583		info->baud_base = BAUD_BASE;
2584
2585		if (info->port_type == SU_PORT_KBD)
2586			info->cflag = B1200 | CS8 | CLOCAL | CREAD;
2587		else
2588			info->cflag = B4800 | CS8 | CLOCAL | CREAD;
2589
2590		init_waitqueue_head(&info->open_wait);
2591		init_waitqueue_head(&info->close_wait);
2592		init_waitqueue_head(&info->delta_msr_wait);
2593
2594		autoconfig(info);
2595		if (info->type == PORT_UNKNOWN)
2596			continue;
2597
2598		printk(KERN_INFO "%s at 0x%lx (irq = %s) is a %s\n",
2599		       info->name, info->port, __irq_itoa(info->irq),
2600		       uart_config[info->type].name);
2601
2602		startup(info);
2603		if (info->port_type == SU_PORT_KBD)
2604			keyboard_zsinit(su_put_char_kbd);
2605		else
2606			sun_mouse_zsinit();
2607	}
2608	return 0;
2609}
2610
2611static int su_node_ok(int node, char *name, int namelen)
2612{
2613	if (strncmp(name, "su", namelen) == 0 ||
2614	    strncmp(name, "su_pnp", namelen) == 0)
2615		return 1;
2616
2617	if (strncmp(name, "serial", namelen) == 0) {
2618		char compat[32];
2619		int clen;
2620
2621		/* Is it _really_ a 'su' device? */
2622		clen = prom_getproperty(node, "compatible", compat, sizeof(compat));
2623		if (clen > 0) {
2624			if (strncmp(compat, "sab82532", 8) == 0) {
2625				/* Nope, Siemens serial, not for us. */
2626				return 0;
2627			}
2628		}
2629		return 1;
2630	}
2631
2632	return 0;
2633}
2634
2635/*
2636 * We got several platforms which present 'su' in different parts
2637 * of device tree. 'su' may be found under obio, ebus, isa and pci.
2638 * We walk over the tree and find them wherever PROM hides them.
2639 */
2640void __init su_probe_any(struct su_probe_scan *t, int sunode)
2641{
2642	struct su_struct *info;
2643	int len;
2644
2645	if (t->devices >= NR_PORTS) return;
2646
2647	for (; sunode != 0; sunode = prom_getsibling(sunode)) {
2648		len = prom_getproperty(sunode, "name", t->prop, SU_PROPSIZE);
2649		if (len <= 1) continue;		/* Broken PROM node */
2650		if (su_node_ok(sunode, t->prop, len)) {
2651			info = &su_table[t->devices];
2652			if (t->kbnode != 0 && sunode == t->kbnode) {
2653				t->kbx = t->devices;
2654				info->port_type = SU_PORT_KBD;
2655			} else if (t->msnode != 0 && sunode == t->msnode) {
2656				t->msx = t->devices;
2657				info->port_type = SU_PORT_MS;
2658			} else {
2659#ifdef __sparc_v9__
2660				/*
2661				 * Do not attempt to use the truncated
2662				 * keyboard/mouse ports as serial ports
2663				 * on Ultras with PC keyboard attached.
2664				 */
2665				if (prom_getbool(sunode, "mouse"))
2666					continue;
2667				if (prom_getbool(sunode, "keyboard"))
2668					continue;
2669#endif
2670				info->port_type = SU_PORT_PORT;
2671			}
2672			info->is_console = 0;
2673			info->port_node = sunode;
2674			++t->devices;
2675		} else {
2676			su_probe_any(t, prom_getchild(sunode));
2677		}
2678	}
2679}
2680
2681int __init su_probe(void)
2682{
2683	int node;
2684	int len;
2685	struct su_probe_scan scan;
2686
2687	/*
2688	 * First, we scan the tree.
2689	 */
2690	scan.devices = 0;
2691	scan.msx = -1;
2692	scan.kbx = -1;
2693	scan.kbnode = 0;
2694	scan.msnode = 0;
2695
2696	/*
2697	 * Get the nodes for keyboard and mouse from 'aliases'...
2698	 */
2699        node = prom_getchild(prom_root_node);
2700	node = prom_searchsiblings(node, "aliases");
2701	if (node != 0) {
2702
2703		len = prom_getproperty(node, "keyboard", scan.prop,SU_PROPSIZE);
2704		if (len > 0) {
2705			scan.prop[len] = 0;
2706			scan.kbnode = prom_finddevice(scan.prop);
2707		}
2708
2709		len = prom_getproperty(node, "mouse", scan.prop, SU_PROPSIZE);
2710		if (len > 0) {
2711			scan.prop[len] = 0;
2712			scan.msnode = prom_finddevice(scan.prop);
2713		}
2714	}
2715
2716	su_probe_any(&scan, prom_getchild(prom_root_node));
2717
2718	/*
2719	 * Second, we process the special case of keyboard and mouse.
2720	 *
2721	 * Currently if we got keyboard and mouse hooked to "su" ports
2722	 * we do not use any possible remaining "su" as a serial port.
2723	 * Thus, we ignore values of .msx and .kbx, then compact ports.
2724	 * Those who want to address this issue need to merge
2725	 * su_serial_init() and su_ms_kbd_init().
2726	 */
2727	if (scan.msx != -1 && scan.kbx != -1) {
2728		su_table[0].port_type = SU_PORT_MS;
2729		su_table[0].is_console = 0;
2730		su_table[0].port_node = scan.msnode;
2731		su_table[1].port_type = SU_PORT_KBD;
2732		su_table[1].is_console = 0;
2733		su_table[1].port_node = scan.kbnode;
2734
2735        	sunserial_setinitfunc(su_kbd_ms_init);
2736        	rs_ops.rs_change_mouse_baud = su_change_mouse_baud;
2737		sunkbd_setinitfunc(sun_kbd_init);
2738		kbd_ops.compute_shiftstate = sun_compute_shiftstate;
2739		kbd_ops.setledstate = sun_setledstate;
2740		kbd_ops.getledstate = sun_getledstate;
2741		kbd_ops.setkeycode = sun_setkeycode;
2742		kbd_ops.getkeycode = sun_getkeycode;
2743#ifdef CONFIG_PCI
2744		sunkbd_install_keymaps(sun_key_maps,
2745		    sun_keymap_count, sun_func_buf, sun_func_table,
2746		    sun_funcbufsize, sun_funcbufleft,
2747		    sun_accent_table, sun_accent_table_size);
2748#endif
2749		return 0;
2750	}
2751	if (scan.msx != -1 || scan.kbx != -1) {
2752		printk("su_probe: cannot match keyboard and mouse, confused\n");
2753		return -ENODEV;
2754	}
2755
2756	if (scan.devices == 0)
2757		return -ENODEV;
2758
2759#ifdef CONFIG_SERIAL_CONSOLE
2760	/*
2761	 * Console must be initiated after the generic initialization.
2762	 * sunserial_setinitfunc inverts order, so call this before next one.
2763	 */
2764	sunserial_setinitfunc(su_serial_console_init);
2765#endif
2766       	sunserial_setinitfunc(su_serial_init);
2767	return 0;
2768}
2769
2770/*
2771 * ------------------------------------------------------------
2772 * Serial console driver
2773 * ------------------------------------------------------------
2774 */
2775#ifdef CONFIG_SERIAL_CONSOLE
2776
2777#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
2778
2779/*
2780 *	Wait for transmitter & holding register to empty
2781 */
2782static __inline__ void
2783wait_for_xmitr(struct su_struct *info)
2784{
2785	int lsr;
2786	unsigned int tmout = 1000000;
2787
2788	do {
2789		lsr = su_inb(info, UART_LSR);
2790		if (--tmout == 0)
2791			break;
2792	} while ((lsr & BOTH_EMPTY) != BOTH_EMPTY);
2793}
2794
2795/*
2796 *	Print a string to the serial port trying not to disturb
2797 *	any possible real use of the port...
2798 */
2799static void
2800serial_console_write(struct console *co, const char *s,
2801				unsigned count)
2802{
2803	struct su_struct *info;
2804	int ier;
2805	unsigned i;
2806
2807	info = su_table + co->index;
2808	/*
2809	 *	First save the IER then disable the interrupts
2810	 */
2811	ier = su_inb(info, UART_IER);
2812	su_outb(info, UART_IER, 0x00);
2813
2814	/*
2815	 *	Now, do each character
2816	 */
2817	for (i = 0; i < count; i++, s++) {
2818		wait_for_xmitr(info);
2819
2820		/*
2821		 *	Send the character out.
2822		 *	If a LF, also do CR...
2823		 */
2824		su_outb(info, UART_TX, *s);
2825		if (*s == 10) {
2826			wait_for_xmitr(info);
2827			su_outb(info, UART_TX, 13);
2828		}
2829	}
2830
2831	/*
2832	 *	Finally, Wait for transmitter & holding register to empty
2833	 * 	and restore the IER
2834	 */
2835	wait_for_xmitr(info);
2836	su_outb(info, UART_IER, ier);
2837}
2838
2839static kdev_t
2840serial_console_device(struct console *c)
2841{
2842	return MKDEV(TTY_MAJOR, 64 + c->index);
2843}
2844
2845/*
2846 *	Setup initial baud/bits/parity. We do two things here:
2847 *	- construct a cflag setting for the first su_open()
2848 *	- initialize the serial port
2849 *	Return non-zero if we didn't find a serial port.
2850 */
2851static int __init serial_console_setup(struct console *co, char *options)
2852{
2853	struct su_struct *info;
2854	unsigned cval;
2855	int	baud = 9600;
2856	int	bits = 8;
2857	int	parity = 'n';
2858	int	cflag = CREAD | HUPCL | CLOCAL;
2859	int	quot = 0;
2860	char	*s;
2861
2862	if (options) {
2863		baud = simple_strtoul(options, NULL, 10);
2864		s = options;
2865		while (*s >= '0' && *s <= '9')
2866			s++;
2867		if (*s) parity = *s++;
2868		if (*s) bits   = *s - '0';
2869	}
2870
2871	/*
2872	 *	Now construct a cflag setting.
2873	 */
2874	switch (baud) {
2875		case 1200:
2876			cflag |= B1200;
2877			break;
2878		case 2400:
2879			cflag |= B2400;
2880			break;
2881		case 4800:
2882			cflag |= B4800;
2883			break;
2884		case 19200:
2885			cflag |= B19200;
2886			break;
2887		case 38400:
2888			cflag |= B38400;
2889			break;
2890		case 57600:
2891			cflag |= B57600;
2892			break;
2893		case 115200:
2894			cflag |= B115200;
2895			break;
2896		case 9600:
2897		default:
2898			cflag |= B9600;
2899			baud = 9600;
2900			break;
2901	}
2902	switch (bits) {
2903		case 7:
2904			cflag |= CS7;
2905			break;
2906		default:
2907		case 8:
2908			cflag |= CS8;
2909			break;
2910	}
2911	switch (parity) {
2912		case 'o': case 'O':
2913			cflag |= PARODD;
2914			break;
2915		case 'e': case 'E':
2916			cflag |= PARENB;
2917			break;
2918	}
2919	co->cflag = cflag;
2920
2921	/*
2922	 *	Divisor, bytesize and parity
2923	 */
2924	info = su_table + co->index;
2925	quot = BAUD_BASE / baud;
2926	cval = cflag & (CSIZE | CSTOPB);
2927#if defined(__powerpc__) || defined(__alpha__)
2928	cval >>= 8;
2929#else /* !__powerpc__ && !__alpha__ */
2930	cval >>= 4;
2931#endif /* !__powerpc__ && !__alpha__ */
2932	if (cflag & PARENB)
2933		cval |= UART_LCR_PARITY;
2934	if (!(cflag & PARODD))
2935		cval |= UART_LCR_EPAR;
2936
2937	/*
2938	 *	Disable UART interrupts, set DTR and RTS high
2939	 *	and set speed.
2940	 */
2941	su_outb(info, UART_IER, 0);
2942	su_outb(info, UART_MCR, UART_MCR_DTR | UART_MCR_RTS);
2943	su_outb(info, UART_LCR, cval | UART_LCR_DLAB);	/* set DLAB */
2944	su_outb(info, UART_DLL, quot & 0xff);		/* LS of divisor */
2945	su_outb(info, UART_DLM, quot >> 8);		/* MS of divisor */
2946	su_outb(info, UART_LCR, cval);			/* reset DLAB */
2947	info->quot = quot;
2948
2949	/*
2950	 *	If we read 0xff from the LSR, there is no UART here.
2951	 */
2952	if (su_inb(info, UART_LSR) == 0xff)
2953		return -1;
2954
2955	info->is_console = 1;
2956
2957	return 0;
2958}
2959
2960static struct console sercons = {
2961	name:		"ttyS",
2962	write:		serial_console_write,
2963	device:		serial_console_device,
2964	setup:		serial_console_setup,
2965	flags:		CON_PRINTBUFFER,
2966	index:		-1,
2967};
2968
2969int su_console_registered = 0;
2970
2971/*
2972 *	Register console.
2973 */
2974int __init su_serial_console_init(void)
2975{
2976	extern int con_is_present(void);
2977	int index;
2978
2979	if (con_is_present())
2980		return 0;
2981	if (serial_console == 0)
2982		return 0;
2983	index = serial_console - 1;
2984	if (su_table[index].port == 0 || su_table[index].port_node == 0)
2985		return 0;
2986	sercons.index = index;
2987	register_console(&sercons);
2988	su_console_registered = 1;
2989	return 0;
2990}
2991
2992#endif /* CONFIG_SERIAL_CONSOLE */
2993