File: /usr/src/linux/arch/mips/baget/vacserial.c

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