File: /usr/src/linux/drivers/sbus/char/su.c

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