File: /usr/src/linux/drivers/char/serial167.c

1     /*
2      * linux/drivers/char/serial167.c
3      *
4      * Driver for MVME166/7 board serial ports, which are via a CD2401.
5      * Based very much on cyclades.c.
6      *
7      * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
8      *
9      * ==============================================================
10      *
11      * static char rcsid[] =
12      * "$Revision: 1.36.1.4 $$Date: 1995/03/29 06:14:14 $";
13      *
14      *  linux/kernel/cyclades.c
15      *
16      * Maintained by Marcio Saito (cyclades@netcom.com) and
17      * Randolph Bentson (bentson@grieg.seaslug.org)
18      *
19      * Much of the design and some of the code came from serial.c
20      * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
21      * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
22      * and then fixed as suggested by Michael K. Johnson 12/12/92.
23      *
24      * This version does not support shared irq's.
25      *
26      * This module exports the following rs232 io functions:
27      *   int cy_init(void);
28      *   int  cy_open(struct tty_struct *tty, struct file *filp);
29      *
30      * $Log: cyclades.c,v $
31      * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
32      * disambiguate between Cyclom-16Y and Cyclom-32Ye;
33      *
34      * Changes:
35      *
36      * 200 lines of changes record removed - RGH 11-10-95, starting work on
37      * converting this to drive serial ports on mvme166 (cd2401).
38      *
39      * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
40      * - get rid of verify_area
41      * - use get_user to access memory from userspace in set_threshold,
42      *   set_default_threshold and set_timeout
43      * - don't use the panic function in serial167_init
44      * - do resource release on failure on serial167_init
45      * - include missing restore_flags in mvme167_serial_console_setup
46      */
47     
48     #include <linux/config.h>
49     #include <linux/errno.h>
50     #include <linux/signal.h>
51     #include <linux/sched.h>
52     #include <linux/timer.h>
53     #include <linux/tty.h>
54     #include <linux/interrupt.h>
55     #include <linux/serial.h>
56     #include <linux/serialP.h>
57     #include <linux/string.h>
58     #include <linux/fcntl.h>
59     #include <linux/ptrace.h>
60     #include <linux/serial167.h>
61     #include <linux/delay.h>
62     #include <linux/major.h>
63     #include <linux/mm.h>
64     #include <linux/console.h>
65     
66     #include <asm/system.h>
67     #include <asm/io.h>
68     #include <asm/segment.h>
69     #include <asm/bitops.h>
70     #include <asm/mvme16xhw.h>
71     #include <asm/bootinfo.h>
72     #include <asm/setup.h>
73     
74     #include <linux/types.h>
75     #include <linux/kernel.h>
76     
77     #include <linux/version.h>
78     #include <asm/uaccess.h>
79     #include <linux/init.h>
80     
81     #define SERIAL_PARANOIA_CHECK
82     #undef  SERIAL_DEBUG_OPEN
83     #undef  SERIAL_DEBUG_THROTTLE
84     #undef  SERIAL_DEBUG_OTHER
85     #undef  SERIAL_DEBUG_IO
86     #undef  SERIAL_DEBUG_COUNT
87     #undef  SERIAL_DEBUG_DTR
88     #undef  CYCLOM_16Y_HACK
89     #define  CYCLOM_ENABLE_MONITORING
90     
91     #ifndef MIN
92     #define MIN(a,b)	((a) < (b) ? (a) : (b))
93     #endif
94     
95     #define WAKEUP_CHARS 256
96     
97     #define STD_COM_FLAGS (0)
98     
99     #define SERIAL_TYPE_NORMAL  1
100     #define SERIAL_TYPE_CALLOUT 2
101     
102     
103     DECLARE_TASK_QUEUE(tq_cyclades);
104     
105     struct tty_driver cy_serial_driver, cy_callout_driver;
106     extern int serial_console;
107     static struct cyclades_port *serial_console_info = NULL;
108     static unsigned int serial_console_cflag = 0;
109     u_char initial_console_speed;
110     
111     /* Base address of cd2401 chip on mvme166/7 */
112     
113     #define BASE_ADDR (0xfff45000)
114     #define pcc2chip	((volatile u_char *)0xfff42000)
115     #define PccSCCMICR	0x1d
116     #define PccSCCTICR	0x1e
117     #define PccSCCRICR	0x1f
118     #define PccTPIACKR	0x25
119     #define PccRPIACKR	0x27
120     #define PccIMLR		0x3f
121     
122     /* This is the per-port data structure */
123     struct cyclades_port cy_port[] = {
124           /* CARD#  */
125             {-1 },      /* ttyS0 */
126             {-1 },      /* ttyS1 */
127             {-1 },      /* ttyS2 */
128             {-1 },      /* ttyS3 */
129     };
130     #define NR_PORTS        (sizeof(cy_port)/sizeof(struct cyclades_port))
131     
132     static int serial_refcount;
133     
134     static struct tty_struct *serial_table[NR_PORTS];
135     static struct termios *serial_termios[NR_PORTS];
136     static struct termios *serial_termios_locked[NR_PORTS];
137     
138     
139     /*
140      * tmp_buf is used as a temporary buffer by serial_write.  We need to
141      * lock it in case the copy_from_user blocks while swapping in a page,
142      * and some other program tries to do a serial write at the same time.
143      * Since the lock will only come under contention when the system is
144      * swapping and available memory is low, it makes sense to share one
145      * buffer across all the serial ports, since it significantly saves
146      * memory if large numbers of serial ports are open.
147      */
148     static unsigned char *tmp_buf = 0;
149     DECLARE_MUTEX(tmp_buf_sem);
150     
151     /*
152      * This is used to look up the divisor speeds and the timeouts
153      * We're normally limited to 15 distinct baud rates.  The extra
154      * are accessed via settings in info->flags.
155      *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
156      *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
157      *                                                  HI            VHI
158      */
159     static int baud_table[] = {
160                0,    50,    75,   110,   134,   150,   200,   300,   600,  1200,
161             1800,  2400,  4800,  9600, 19200, 38400, 57600, 76800,115200,150000,
162             0};
163     
164     #if 0
165     static char baud_co[] = {  /* 25 MHz clock option table */
166             /* value =>    00    01   02    03    04 */
167             /* divide by    8    32   128   512  2048 */
168             0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,  0x03,  0x02,
169             0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
170     
171     static char baud_bpr[] = {  /* 25 MHz baud rate period table */
172             0x00,  0xf5,  0xa3,  0x6f,  0x5c,  0x51,  0xf5,  0xa3,  0x51,  0xa3,
173             0x6d,  0x51,  0xa3,  0x51,  0xa3,  0x51,  0x36,  0x29,  0x1b,  0x15};
174     #endif
175     
176     /* I think 166 brd clocks 2401 at 20MHz.... */
177     
178     /* These values are written directly to tcor, and >> 5 for writing to rcor */
179     static u_char baud_co[] = {  /* 20 MHz clock option table */
180             0x00,  0x80,  0x80,  0x80,  0x80,  0x80,  0x80,  0x60,  0x60,  0x40,
181             0x40,  0x40,  0x20,  0x20,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
182     
183     /* These values written directly to tbpr/rbpr */
184     static u_char baud_bpr[] = {  /* 20 MHz baud rate period table */
185             0x00,  0xc0,  0x80,  0x58,  0x6c,  0x40,  0xc0,  0x81,  0x40,  0x81,
186             0x57,  0x40,  0x81,  0x40,  0x81,  0x40,  0x2b,  0x20,  0x15,  0x10};
187     
188     static u_char baud_cor4[] = {  /* receive threshold */
189             0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
190             0x0a,  0x0a,  0x0a,  0x09,  0x09,  0x08,  0x08,  0x08,  0x08,  0x07};
191     
192     
193     
194     static void shutdown(struct cyclades_port *);
195     static int startup (struct cyclades_port *);
196     static void cy_throttle(struct tty_struct *);
197     static void cy_unthrottle(struct tty_struct *);
198     static void config_setup(struct cyclades_port *);
199     extern void console_print(const char *);
200     #ifdef CYCLOM_SHOW_STATUS
201     static void show_status(int);
202     #endif
203     
204     #ifdef CONFIG_REMOTE_DEBUG
205     static void debug_setup(void);
206     void queueDebugChar (int c);
207     int getDebugChar(void);
208     
209     #define DEBUG_PORT	1
210     #define DEBUG_LEN	256
211     
212     typedef struct {
213     	int	in;
214     	int	out;
215     	unsigned char	buf[DEBUG_LEN];
216     } debugq;
217     
218     debugq debugiq;
219     #endif
220     
221     /*
222      * I have my own version of udelay(), as it is needed when initialising
223      * the chip, before the delay loop has been calibrated.  Should probably
224      * reference one of the vmechip2 or pccchip2 counter for an accurate
225      * delay, but this wild guess will do for now.
226      */
227     
228     void my_udelay (long us)
229     {
230     	u_char x;
231     	volatile u_char *p = &x;
232     	int i;
233     
234     	while (us--)
235     		for (i = 100; i; i--)
236     			x |= *p;
237     }
238     
239     static inline int
240     serial_paranoia_check(struct cyclades_port *info,
241     			dev_t device, const char *routine)
242     {
243     #ifdef SERIAL_PARANOIA_CHECK
244         static const char *badmagic =
245     	"Warning: bad magic number for serial struct (%d, %d) in %s\n";
246         static const char *badinfo =
247     	"Warning: null cyclades_port for (%d, %d) in %s\n";
248         static const char *badrange =
249     	"Warning: cyclades_port out of range for (%d, %d) in %s\n";
250     
251         if (!info) {
252     	printk(badinfo, MAJOR(device), MINOR(device), routine);
253     	return 1;
254         }
255     
256         if( (long)info < (long)(&cy_port[0])
257         || (long)(&cy_port[NR_PORTS]) < (long)info ){
258     	printk(badrange, MAJOR(device), MINOR(device), routine);
259     	return 1;
260         }
261     
262         if (info->magic != CYCLADES_MAGIC) {
263     	printk(badmagic, MAJOR(device), MINOR(device), routine);
264     	return 1;
265         }
266     #endif
267     	return 0;
268     } /* serial_paranoia_check */
269     
270     #if 0
271     /* The following diagnostic routines allow the driver to spew
272        information on the screen, even (especially!) during interrupts.
273      */
274     void
275     SP(char *data){
276       unsigned long flags;
277         save_flags(flags); cli();
278             console_print(data);
279         restore_flags(flags);
280     }
281     char scrn[2];
282     void
283     CP(char data){
284       unsigned long flags;
285         save_flags(flags); cli();
286             scrn[0] = data;
287             console_print(scrn);
288         restore_flags(flags);
289     }/* CP */
290     
291     void CP1(int data) { (data<10)?  CP(data+'0'): CP(data+'A'-10); }/* CP1 */
292     void CP2(int data) { CP1((data>>4) & 0x0f); CP1( data & 0x0f); }/* CP2 */
293     void CP4(int data) { CP2((data>>8) & 0xff); CP2(data & 0xff); }/* CP4 */
294     void CP8(long data) { CP4((data>>16) & 0xffff); CP4(data & 0xffff); }/* CP8 */
295     #endif
296     
297     /* This routine waits up to 1000 micro-seconds for the previous
298        command to the Cirrus chip to complete and then issues the
299        new command.  An error is returned if the previous command
300        didn't finish within the time limit.
301      */
302     u_short
303     write_cy_cmd(volatile u_char *base_addr, u_char cmd)
304     {
305       unsigned long flags;
306       volatile int  i;
307     
308         save_flags(flags); cli();
309     	/* Check to see that the previous command has completed */
310     	for(i = 0 ; i < 100 ; i++){
311     	    if (base_addr[CyCCR] == 0){
312     		break;
313     	    }
314     	    my_udelay(10L);
315     	}
316     	/* if the CCR never cleared, the previous command
317     	    didn't finish within the "reasonable time" */
318     	if ( i == 10 ) {
319     	    restore_flags(flags);
320     	    return (-1);
321     	}
322     
323     	/* Issue the new command */
324     	base_addr[CyCCR] = cmd;
325         restore_flags(flags);
326         return(0);
327     } /* write_cy_cmd */
328     
329     
330     /* cy_start and cy_stop provide software output flow control as a
331        function of XON/XOFF, software CTS, and other such stuff. */
332     
333     static void
334     cy_stop(struct tty_struct *tty)
335     {
336       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
337       volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
338       int channel;
339       unsigned long flags;
340     
341     #ifdef SERIAL_DEBUG_OTHER
342         printk("cy_stop ttyS%d\n", info->line); /* */
343     #endif
344     
345         if (serial_paranoia_check(info, tty->device, "cy_stop"))
346     	return;
347     	
348         channel = info->line;
349     
350         save_flags(flags); cli();
351             base_addr[CyCAR] = (u_char)(channel); /* index channel */
352             base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
353         restore_flags(flags);
354     
355         return;
356     } /* cy_stop */
357     
358     static void
359     cy_start(struct tty_struct *tty)
360     {
361       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
362       volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
363       int channel;
364       unsigned long flags;
365     
366     #ifdef SERIAL_DEBUG_OTHER
367         printk("cy_start ttyS%d\n", info->line); /* */
368     #endif
369     
370         if (serial_paranoia_check(info, tty->device, "cy_start"))
371     	return;
372     	
373         channel = info->line;
374     
375         save_flags(flags); cli();
376             base_addr[CyCAR] = (u_char)(channel);
377             base_addr[CyIER] |= CyTxMpty;
378         restore_flags(flags);
379     
380         return;
381     } /* cy_start */
382     
383     
384     /*
385      * This routine is used by the interrupt handler to schedule
386      * processing in the software interrupt portion of the driver
387      * (also known as the "bottom half").  This can be called any
388      * number of times for any channel without harm.
389      */
390     static inline void
391     cy_sched_event(struct cyclades_port *info, int event)
392     {
393         info->event |= 1 << event; /* remember what kind of event and who */
394         queue_task(&info->tqueue, &tq_cyclades); /* it belongs to */
395         mark_bh(CYCLADES_BH);                       /* then trigger event */
396     } /* cy_sched_event */
397     
398     
399     /* The real interrupt service routines are called
400        whenever the card wants its hand held--chars
401        received, out buffer empty, modem change, etc.
402      */
403     static void
404     cd2401_rxerr_interrupt(int irq, void *dev_id, struct pt_regs *fp)
405     {
406         struct tty_struct *tty;
407         struct cyclades_port *info;
408         volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
409         unsigned char err, rfoc;
410         int channel;
411         char data;
412     
413         /* determine the channel and change to that context */
414         channel = (u_short ) (base_addr[CyLICR] >> 2);
415         info = &cy_port[channel];
416         info->last_active = jiffies;
417     
418         if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
419     	/* This is a receive timeout interrupt, ignore it */
420     	base_addr[CyREOIR] = CyNOTRANS;
421     	return;
422         }
423     
424         /* Read a byte of data if there is any - assume the error
425          * is associated with this character */
426     
427         if ((rfoc = base_addr[CyRFOC]) != 0)
428     	data = base_addr[CyRDR];
429         else
430     	data = 0;
431     
432         /* if there is nowhere to put the data, discard it */
433         if(info->tty == 0) {
434     	base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
435     	return;
436         }
437         else { /* there is an open port for this data */
438     	tty = info->tty;
439     	if(err & info->ignore_status_mask){
440     	    base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
441     	    return;
442     	}
443     	if (tty->flip.count < TTY_FLIPBUF_SIZE){
444     	    tty->flip.count++;
445     	    if (err & info->read_status_mask){
446     		if(err & CyBREAK){
447     		    *tty->flip.flag_buf_ptr++ = TTY_BREAK;
448     		    *tty->flip.char_buf_ptr++ = data;
449     		    if (info->flags & ASYNC_SAK){
450     			do_SAK(tty);
451     		    }
452     		}else if(err & CyFRAME){
453     		    *tty->flip.flag_buf_ptr++ = TTY_FRAME;
454     		    *tty->flip.char_buf_ptr++ = data;
455     		}else if(err & CyPARITY){
456     		    *tty->flip.flag_buf_ptr++ = TTY_PARITY;
457     		    *tty->flip.char_buf_ptr++ = data;
458     		}else if(err & CyOVERRUN){
459     		    *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
460     		    *tty->flip.char_buf_ptr++ = 0;
461     		    /*
462     		       If the flip buffer itself is
463     		       overflowing, we still loose
464     		       the next incoming character.
465     		     */
466     		    if(tty->flip.count < TTY_FLIPBUF_SIZE){
467     			tty->flip.count++;
468     			*tty->flip.flag_buf_ptr++ = TTY_NORMAL;
469     			*tty->flip.char_buf_ptr++ = data;
470     		    }
471     		/* These two conditions may imply */
472     		/* a normal read should be done. */
473     		/* else if(data & CyTIMEOUT) */
474     		/* else if(data & CySPECHAR) */
475     		}else{
476     		    *tty->flip.flag_buf_ptr++ = 0;
477     		    *tty->flip.char_buf_ptr++ = 0;
478     		}
479     	    }else{
480     		*tty->flip.flag_buf_ptr++ = 0;
481     		*tty->flip.char_buf_ptr++ = 0;
482     	    }
483     	}else{
484     	    /* there was a software buffer overrun
485     	       and nothing could be done about it!!! */
486     	}
487         }
488         queue_task(&tty->flip.tqueue, &tq_timer);
489         /* end of service */
490         base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
491     } /* cy_rxerr_interrupt */
492     
493     static void
494     cd2401_modem_interrupt(int irq, void *dev_id, struct pt_regs *fp)
495     {
496         struct cyclades_port *info;
497         volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
498         int channel;
499         int mdm_change;
500         int mdm_status;
501     
502     
503         /* determine the channel and change to that context */
504         channel = (u_short ) (base_addr[CyLICR] >> 2);
505         info = &cy_port[channel];
506         info->last_active = jiffies;
507     
508         mdm_change = base_addr[CyMISR];
509         mdm_status = base_addr[CyMSVR1];
510     
511         if(info->tty == 0){ /* nowhere to put the data, ignore it */
512     	;
513         }else{
514     	if((mdm_change & CyDCD)
515     	&& (info->flags & ASYNC_CHECK_CD)){
516     	    if(mdm_status & CyDCD){
517     /* CP('!'); */
518     		cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
519     	    }else if(!((info->flags & ASYNC_CALLOUT_ACTIVE)
520     		     &&(info->flags & ASYNC_CALLOUT_NOHUP))){
521     /* CP('@'); */
522     		cy_sched_event(info, Cy_EVENT_HANGUP);
523     	    }
524     	}
525     	if((mdm_change & CyCTS)
526     	&& (info->flags & ASYNC_CTS_FLOW)){
527     	    if(info->tty->stopped){
528     		if(mdm_status & CyCTS){
529     		    /* !!! cy_start isn't used because... */
530     		    info->tty->stopped = 0;
531             	    base_addr[CyIER] |= CyTxMpty;
532     		    cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
533     		}
534     	    }else{
535     		if(!(mdm_status & CyCTS)){
536     		    /* !!! cy_stop isn't used because... */
537     		    info->tty->stopped = 1;
538             	    base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
539     		}
540     	    }
541     	}
542     	if(mdm_status & CyDSR){
543     	}
544         }
545         base_addr[CyMEOIR] = 0;
546     } /* cy_modem_interrupt */
547     
548     static void
549     cd2401_tx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
550     {
551         struct cyclades_port *info;
552         volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
553         int channel;
554         int char_count, saved_cnt;
555         int outch;
556     
557         /* determine the channel and change to that context */
558         channel = (u_short ) (base_addr[CyLICR] >> 2);
559     
560     #ifdef CONFIG_REMOTE_DEBUG
561         if (channel == DEBUG_PORT) {
562     	panic ("TxInt on debug port!!!");
563         }
564     #endif
565     
566         info = &cy_port[channel];
567     
568         /* validate the port number (as configured and open) */
569         if( (channel < 0) || (NR_PORTS <= channel) ){
570     	base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
571     	base_addr[CyTEOIR] = CyNOTRANS;
572     	return;
573         }
574         info->last_active = jiffies;
575         if(info->tty == 0){
576     	base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
577             if (info->xmit_cnt < WAKEUP_CHARS) {
578     	    cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
579             }
580     	base_addr[CyTEOIR] = CyNOTRANS;
581     	return;
582         }
583     
584         /* load the on-chip space available for outbound data */
585         saved_cnt = char_count = base_addr[CyTFTC];
586     
587         if(info->x_char) { /* send special char */
588     	outch = info->x_char;
589     	base_addr[CyTDR] = outch;
590     	char_count--;
591     	info->x_char = 0;
592         }
593     
594         if (info->x_break){
595     	/*  The Cirrus chip requires the "Embedded Transmit
596     	    Commands" of start break, delay, and end break
597     	    sequences to be sent.  The duration of the
598     	    break is given in TICs, which runs at HZ
599     	    (typically 100) and the PPR runs at 200 Hz,
600     	    so the delay is duration * 200/HZ, and thus a
601     	    break can run from 1/100 sec to about 5/4 sec.
602     	    Need to check these values - RGH 141095.
603     	 */
604     	base_addr[CyTDR] = 0; /* start break */
605     	base_addr[CyTDR] = 0x81;
606     	base_addr[CyTDR] = 0; /* delay a bit */
607     	base_addr[CyTDR] = 0x82;
608     	base_addr[CyTDR] = info->x_break*200/HZ;
609     	base_addr[CyTDR] = 0; /* terminate break */
610     	base_addr[CyTDR] = 0x83;
611     	char_count -= 7;
612     	info->x_break = 0;
613         }
614     
615         while (char_count > 0){
616     	if (!info->xmit_cnt){
617     	    base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
618     	    break;
619     	}
620     	if (info->xmit_buf == 0){
621     	    base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
622     	    break;
623     	}
624     	if (info->tty->stopped || info->tty->hw_stopped){
625     	    base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
626     	    break;
627     	}
628     	/* Because the Embedded Transmit Commands have been
629     	   enabled, we must check to see if the escape
630     	   character, NULL, is being sent.  If it is, we
631     	   must ensure that there is room for it to be
632     	   doubled in the output stream.  Therefore we
633     	   no longer advance the pointer when the character
634     	   is fetched, but rather wait until after the check
635     	   for a NULL output character. (This is necessary
636     	   because there may not be room for the two chars
637     	   needed to send a NULL.
638     	 */
639     	outch = info->xmit_buf[info->xmit_tail];
640     	if( outch ){
641     	    info->xmit_cnt--;
642     	    info->xmit_tail = (info->xmit_tail + 1)
643     				      & (PAGE_SIZE - 1);
644     	    base_addr[CyTDR] = outch;
645     	    char_count--;
646     	}else{
647     	    if(char_count > 1){
648     		info->xmit_cnt--;
649     		info->xmit_tail = (info->xmit_tail + 1)
650     					  & (PAGE_SIZE - 1);
651     		base_addr[CyTDR] = outch;
652     		base_addr[CyTDR] = 0;
653     		char_count--;
654     		char_count--;
655     	    }else{
656     		break;
657     	    }
658     	}
659         }
660     
661         if (info->xmit_cnt < WAKEUP_CHARS) {
662     	cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
663         }
664         base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
665     } /* cy_tx_interrupt */
666     
667     static void
668     cd2401_rx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
669     {
670         struct tty_struct *tty;
671         struct cyclades_port *info;
672         volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
673         int channel;
674         char data;
675         int char_count;
676         int save_cnt;
677     
678         /* determine the channel and change to that context */
679         channel = (u_short ) (base_addr[CyLICR] >> 2);
680         info = &cy_port[channel];
681         info->last_active = jiffies;
682         save_cnt = char_count = base_addr[CyRFOC];
683     
684     #ifdef CONFIG_REMOTE_DEBUG
685         if (channel == DEBUG_PORT) {
686     	while (char_count--) {
687                 data = base_addr[CyRDR];
688     	    queueDebugChar(data);
689     	}
690         }
691         else
692     #endif
693         /* if there is nowhere to put the data, discard it */
694         if(info->tty == 0){
695     	while(char_count--){
696     	    data = base_addr[CyRDR];
697     	}
698         }else{ /* there is an open port for this data */
699     	tty = info->tty;
700     	/* load # characters available from the chip */
701     
702     #ifdef CYCLOM_ENABLE_MONITORING
703     	++info->mon.int_count;
704     	info->mon.char_count += char_count;
705     	if (char_count > info->mon.char_max)
706     	    info->mon.char_max = char_count;
707     	info->mon.char_last = char_count;
708     #endif
709     	while(char_count--){
710     	    data = base_addr[CyRDR];
711     	    if (tty->flip.count >= TTY_FLIPBUF_SIZE){
712     		continue;
713     	    }
714     	    tty->flip.count++;
715     	    *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
716     	    *tty->flip.char_buf_ptr++ = data;
717     #ifdef CYCLOM_16Y_HACK
718     	    udelay(10L);
719     #endif
720             }
721     	queue_task(&tty->flip.tqueue, &tq_timer);
722         }
723         /* end of service */
724         base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
725     } /* cy_rx_interrupt */
726     
727     /*
728      * This routine is used to handle the "bottom half" processing for the
729      * serial driver, known also the "software interrupt" processing.
730      * This processing is done at the kernel interrupt level, after the
731      * cy_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
732      * is where time-consuming activities which can not be done in the
733      * interrupt driver proper are done; the interrupt driver schedules
734      * them using cy_sched_event(), and they get done here.
735      *
736      * This is done through one level of indirection--the task queue.
737      * When a hardware interrupt service routine wants service by the
738      * driver's bottom half, it enqueues the appropriate tq_struct (one
739      * per port) to the tq_cyclades work queue and sets a request flag
740      * via mark_bh for processing that queue.  When the time is right,
741      * do_cyclades_bh is called (because of the mark_bh) and it requests
742      * that the work queue be processed.
743      *
744      * Although this may seem unwieldy, it gives the system a way to
745      * pass an argument (in this case the pointer to the cyclades_port
746      * structure) to the bottom half of the driver.  Previous kernels
747      * had to poll every port to see if that port needed servicing.
748      */
749     static void
750     do_cyclades_bh(void)
751     {
752         run_task_queue(&tq_cyclades);
753     } /* do_cyclades_bh */
754     
755     static void
756     do_softint(void *private_)
757     {
758       struct cyclades_port *info = (struct cyclades_port *) private_;
759       struct tty_struct    *tty;
760     
761         tty = info->tty;
762         if (!tty)
763     	return;
764     
765         if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
766     	tty_hangup(info->tty);
767     	wake_up_interruptible(&info->open_wait);
768     	info->flags &= ~(ASYNC_NORMAL_ACTIVE|
769     			     ASYNC_CALLOUT_ACTIVE);
770         }
771         if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
772     	wake_up_interruptible(&info->open_wait);
773         }
774         if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
775     	if((tty->flags & (1<< TTY_DO_WRITE_WAKEUP))
776     	&& tty->ldisc.write_wakeup){
777     	    (tty->ldisc.write_wakeup)(tty);
778     	}
779     	wake_up_interruptible(&tty->write_wait);
780         }
781     } /* do_softint */
782     
783     
784     /* This is called whenever a port becomes active;
785        interrupts are enabled and DTR & RTS are turned on.
786      */
787     static int
788     startup(struct cyclades_port * info)
789     {
790       unsigned long flags;
791       volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
792       int channel;
793     
794         if (info->flags & ASYNC_INITIALIZED){
795     	return 0;
796         }
797     
798         if (!info->type){
799     	if (info->tty){
800     	    set_bit(TTY_IO_ERROR, &info->tty->flags);
801     	}
802     	return 0;
803         }
804         if (!info->xmit_buf){
805     	info->xmit_buf = (unsigned char *) get_free_page (GFP_KERNEL);
806     	if (!info->xmit_buf){
807     	    return -ENOMEM;
808     	}
809         }
810     
811         config_setup(info);
812     
813         channel = info->line;
814     
815     #ifdef SERIAL_DEBUG_OPEN
816         printk("startup channel %d\n", channel);
817     #endif
818     
819         save_flags(flags); cli();
820     	base_addr[CyCAR] = (u_char)channel;
821     	write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
822     
823     	base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
824     	base_addr[CyMSVR1] = CyRTS;
825     /* CP('S');CP('1'); */
826     	base_addr[CyMSVR2] = CyDTR;
827     
828     #ifdef SERIAL_DEBUG_DTR
829             printk("cyc: %d: raising DTR\n", __LINE__);
830             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
831     #endif
832     
833     	base_addr[CyIER] |= CyRxData;
834     	info->flags |= ASYNC_INITIALIZED;
835     
836     	if (info->tty){
837     	    clear_bit(TTY_IO_ERROR, &info->tty->flags);
838     	}
839     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
840     
841         restore_flags(flags);
842     
843     #ifdef SERIAL_DEBUG_OPEN
844         printk(" done\n");
845     #endif
846         return 0;
847     } /* startup */
848     
849     void
850     start_xmit( struct cyclades_port *info )
851     {
852       unsigned long flags;
853       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
854       int channel;
855     
856         channel = info->line;
857         save_flags(flags); cli();
858     	base_addr[CyCAR] = channel;
859     	base_addr[CyIER] |= CyTxMpty;
860         restore_flags(flags);
861     } /* start_xmit */
862     
863     /*
864      * This routine shuts down a serial port; interrupts are disabled,
865      * and DTR is dropped if the hangup on close termio flag is on.
866      */
867     static void
868     shutdown(struct cyclades_port * info)
869     {
870       unsigned long flags;
871       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
872       int channel;
873     
874         if (!(info->flags & ASYNC_INITIALIZED)){
875     /* CP('$'); */
876     	return;
877         }
878     
879         channel = info->line;
880     
881     #ifdef SERIAL_DEBUG_OPEN
882         printk("shutdown channel %d\n", channel);
883     #endif
884     
885         /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
886            SENT BEFORE DROPPING THE LINE !!!  (Perhaps
887            set some flag that is read when XMTY happens.)
888            Other choices are to delay some fixed interval
889            or schedule some later processing.
890          */
891         save_flags(flags); cli();
892     	if (info->xmit_buf){
893     	    free_page((unsigned long) info->xmit_buf);
894     	    info->xmit_buf = 0;
895     	}
896     
897     	base_addr[CyCAR] = (u_char)channel;
898     	if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
899     	    base_addr[CyMSVR1] = 0;
900     /* CP('C');CP('1'); */
901     	    base_addr[CyMSVR2] = 0;
902     #ifdef SERIAL_DEBUG_DTR
903                 printk("cyc: %d: dropping DTR\n", __LINE__);
904                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
905     #endif
906             }
907     	write_cy_cmd(base_addr,CyDIS_RCVR);
908              /* it may be appropriate to clear _XMIT at
909                some later date (after testing)!!! */
910     
911     	if (info->tty){
912     	    set_bit(TTY_IO_ERROR, &info->tty->flags);
913     	}
914     	info->flags &= ~ASYNC_INITIALIZED;
915         restore_flags(flags);
916     
917     #ifdef SERIAL_DEBUG_OPEN
918         printk(" done\n");
919     #endif
920         return;
921     } /* shutdown */
922     
923     /*
924      * This routine finds or computes the various line characteristics.
925      */
926     static void
927     config_setup(struct cyclades_port * info)
928     {
929       unsigned long flags;
930       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
931       int channel;
932       unsigned cflag;
933       int   i;
934       unsigned char ti, need_init_chan = 0;
935     
936         if (!info->tty || !info->tty->termios){
937             return;
938         }
939         if (info->line == -1){
940             return;
941         }
942         cflag = info->tty->termios->c_cflag;
943     
944         /* baud rate */
945         i = cflag & CBAUD;
946     #ifdef CBAUDEX
947     /* Starting with kernel 1.1.65, there is direct support for
948        higher baud rates.  The following code supports those
949        changes.  The conditional aspect allows this driver to be
950        used for earlier as well as later kernel versions.  (The
951        mapping is slightly different from serial.c because there
952        is still the possibility of supporting 75 kbit/sec with
953        the Cyclades board.)
954      */
955         if (i & CBAUDEX) {
956     	if (i == B57600)
957     	    i = 16;
958     	else if(i == B115200) 
959     	    i = 18;
960     #ifdef B78600
961     	else if(i == B78600) 
962     	    i = 17;
963     #endif
964     	else
965     	    info->tty->termios->c_cflag &= ~CBAUDEX;
966         }
967     #endif
968         if (i == 15) {
969     	    if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
970     		    i += 1;
971     	    if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
972     		    i += 3;
973         }
974         /* Don't ever change the speed of the console port.  It will
975          * run at the speed specified in bootinfo, or at 19.2K */
976         /* Actually, it should run at whatever speed 166Bug was using */
977         /* Note info->timeout isn't used at present */
978         if (info != serial_console_info) {
979     	info->tbpr = baud_bpr[i]; /* Tx BPR */
980     	info->tco = baud_co[i]; /* Tx CO */
981     	info->rbpr = baud_bpr[i]; /* Rx BPR */
982     	info->rco = baud_co[i] >> 5; /* Rx CO */
983     	if (baud_table[i] == 134) {
984                 info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
985                 /* get it right for 134.5 baud */
986     	} else if (baud_table[i]) {
987                 info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
988             /* this needs to be propagated into the card info */
989     	} else {
990                 info->timeout = 0;
991     	}
992         }
993         /* By tradition (is it a standard?) a baud rate of zero
994            implies the line should be/has been closed.  A bit
995            later in this routine such a test is performed. */
996     
997         /* byte size and parity */
998         info->cor7 = 0;
999         info->cor6 = 0;
1000         info->cor5 = 0;
1001         info->cor4 = (info->default_threshold
1002     		  ? info->default_threshold
1003     		  : baud_cor4[i]); /* receive threshold */
1004         /* Following two lines added 101295, RGH. */
1005         /* It is obviously wrong to access CyCORx, and not info->corx here,
1006          * try and remember to fix it later! */
1007         channel = info->line;
1008         base_addr[CyCAR] = (u_char)channel;
1009         if (C_CLOCAL(info->tty)) {
1010     	if (base_addr[CyIER] & CyMdmCh)
1011     	    base_addr[CyIER] &= ~CyMdmCh; /* without modem intr */
1012     			       /* ignore 1->0 modem transitions */
1013     	if (base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD))
1014     	    base_addr[CyCOR4] &= ~(CyDSR|CyCTS|CyDCD);
1015     			       /* ignore 0->1 modem transitions */
1016     	if (base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD))
1017     	    base_addr[CyCOR5] &= ~(CyDSR|CyCTS|CyDCD);
1018         } else {
1019     	if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
1020     	    base_addr[CyIER] |= CyMdmCh; /* with modem intr */
1021     			       /* act on 1->0 modem transitions */
1022     	if ((base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
1023     	    base_addr[CyCOR4] |= CyDSR|CyCTS|CyDCD;
1024     			       /* act on 0->1 modem transitions */
1025     	if ((base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
1026     	    base_addr[CyCOR5] |= CyDSR|CyCTS|CyDCD;
1027         }
1028         info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
1029         info->cor2 = CyETC;
1030         switch(cflag & CSIZE){
1031         case CS5:
1032             info->cor1 = Cy_5_BITS;
1033             break;
1034         case CS6:
1035             info->cor1 = Cy_6_BITS;
1036             break;
1037         case CS7:
1038             info->cor1 = Cy_7_BITS;
1039             break;
1040         case CS8:
1041             info->cor1 = Cy_8_BITS;
1042             break;
1043         }
1044         if (cflag & PARENB){
1045             if (cflag & PARODD){
1046                 info->cor1 |= CyPARITY_O;
1047             }else{
1048                 info->cor1 |= CyPARITY_E;
1049             }
1050         }else{
1051             info->cor1 |= CyPARITY_NONE;
1052         }
1053     	
1054         /* CTS flow control flag */
1055     #if 0
1056         /* Don't complcate matters for now! RGH 141095 */
1057         if (cflag & CRTSCTS){
1058     	info->flags |= ASYNC_CTS_FLOW;
1059     	info->cor2 |= CyCtsAE;
1060         }else{
1061     	info->flags &= ~ASYNC_CTS_FLOW;
1062     	info->cor2 &= ~CyCtsAE;
1063         }
1064     #endif
1065         if (cflag & CLOCAL)
1066     	info->flags &= ~ASYNC_CHECK_CD;
1067         else
1068     	info->flags |= ASYNC_CHECK_CD;
1069     
1070          /***********************************************
1071     	The hardware option, CyRtsAO, presents RTS when
1072     	the chip has characters to send.  Since most modems
1073     	use RTS as reverse (inbound) flow control, this
1074     	option is not used.  If inbound flow control is
1075     	necessary, DTR can be programmed to provide the
1076     	appropriate signals for use with a non-standard
1077     	cable.  Contact Marcio Saito for details.
1078          ***********************************************/
1079     
1080         channel = info->line;
1081     
1082         save_flags(flags); cli();
1083     	base_addr[CyCAR] = (u_char)channel;
1084     
1085     	/* CyCMR set once only in mvme167_init_serial() */
1086     	if (base_addr[CyLICR] != channel << 2)
1087     	    base_addr[CyLICR] = channel << 2;
1088     	if (base_addr[CyLIVR] != 0x5c)
1089     	    base_addr[CyLIVR] = 0x5c;
1090     
1091            /* tx and rx baud rate */
1092     
1093     	if (base_addr[CyCOR1] != info->cor1)
1094     	    need_init_chan = 1;
1095     	if (base_addr[CyTCOR] != info->tco)
1096     	    base_addr[CyTCOR] = info->tco;
1097     	if (base_addr[CyTBPR] != info->tbpr)
1098     	    base_addr[CyTBPR] = info->tbpr;
1099     	if (base_addr[CyRCOR] != info->rco)
1100     	    base_addr[CyRCOR] = info->rco;
1101     	if (base_addr[CyRBPR] != info->rbpr)
1102     	    base_addr[CyRBPR] = info->rbpr;
1103     
1104     	/* set line characteristics  according configuration */
1105     
1106     	if (base_addr[CySCHR1] != START_CHAR(info->tty))
1107     	    base_addr[CySCHR1] = START_CHAR(info->tty);
1108     	if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
1109     	    base_addr[CySCHR2] = STOP_CHAR(info->tty);
1110     	if (base_addr[CySCRL] != START_CHAR(info->tty))
1111     	    base_addr[CySCRL] = START_CHAR(info->tty);
1112     	if (base_addr[CySCRH] != START_CHAR(info->tty))
1113     	    base_addr[CySCRH] = START_CHAR(info->tty);
1114     	if (base_addr[CyCOR1] != info->cor1)
1115     	    base_addr[CyCOR1] = info->cor1;
1116     	if (base_addr[CyCOR2] != info->cor2)
1117     	    base_addr[CyCOR2] = info->cor2;
1118     	if (base_addr[CyCOR3] != info->cor3)
1119     	    base_addr[CyCOR3] = info->cor3;
1120     	if (base_addr[CyCOR4] != info->cor4)
1121     	    base_addr[CyCOR4] = info->cor4;
1122     	if (base_addr[CyCOR5] != info->cor5)
1123     	    base_addr[CyCOR5] = info->cor5;
1124     	if (base_addr[CyCOR6] != info->cor6)
1125     	    base_addr[CyCOR6] = info->cor6;
1126     	if (base_addr[CyCOR7] != info->cor7)
1127     	    base_addr[CyCOR7] = info->cor7;
1128     
1129     	if (need_init_chan)
1130     	    write_cy_cmd(base_addr,CyINIT_CHAN);
1131     
1132     	base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
1133     
1134     	/* 2ms default rx timeout */
1135     	ti = info->default_timeout ? info->default_timeout : 0x02;
1136     	if (base_addr[CyRTPRL] != ti)
1137     	    base_addr[CyRTPRL] = ti;
1138     	if (base_addr[CyRTPRH] != 0)
1139     	    base_addr[CyRTPRH] = 0;
1140     
1141     	/* Set up RTS here also ????? RGH 141095 */
1142     	if(i == 0){ /* baud rate is zero, turn off line */
1143     	    if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
1144     	        base_addr[CyMSVR2] = 0;
1145     #ifdef SERIAL_DEBUG_DTR
1146                 printk("cyc: %d: dropping DTR\n", __LINE__);
1147                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1148     #endif
1149     	}else{
1150     	    if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
1151     	        base_addr[CyMSVR2] = CyDTR;
1152     #ifdef SERIAL_DEBUG_DTR
1153                 printk("cyc: %d: raising DTR\n", __LINE__);
1154                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1155     #endif
1156     	}
1157     
1158     	if (info->tty){
1159     	    clear_bit(TTY_IO_ERROR, &info->tty->flags);
1160     	}
1161     
1162         restore_flags(flags);
1163     
1164     } /* config_setup */
1165     
1166     
1167     static void
1168     cy_put_char(struct tty_struct *tty, unsigned char ch)
1169     {
1170       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1171       unsigned long flags;
1172     
1173     #ifdef SERIAL_DEBUG_IO
1174         printk("cy_put_char ttyS%d(0x%02x)\n", info->line, ch);
1175     #endif
1176     
1177         if (serial_paranoia_check(info, tty->device, "cy_put_char"))
1178     	return;
1179     
1180         if (!tty || !info->xmit_buf)
1181     	return;
1182     
1183         save_flags(flags); cli();
1184     	if (info->xmit_cnt >= PAGE_SIZE - 1) {
1185     	    restore_flags(flags);
1186     	    return;
1187     	}
1188     
1189     	info->xmit_buf[info->xmit_head++] = ch;
1190     	info->xmit_head &= PAGE_SIZE - 1;
1191     	info->xmit_cnt++;
1192         restore_flags(flags);
1193     } /* cy_put_char */
1194     
1195     
1196     static void
1197     cy_flush_chars(struct tty_struct *tty)
1198     {
1199       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1200       unsigned long flags;
1201       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1202       int channel;
1203     				
1204     #ifdef SERIAL_DEBUG_IO
1205         printk("cy_flush_chars ttyS%d\n", info->line); /* */
1206     #endif
1207     
1208         if (serial_paranoia_check(info, tty->device, "cy_flush_chars"))
1209     	return;
1210     
1211         if (info->xmit_cnt <= 0 || tty->stopped
1212         || tty->hw_stopped || !info->xmit_buf)
1213     	return;
1214     
1215         channel = info->line;
1216     
1217         save_flags(flags); cli();
1218     	base_addr[CyCAR] = channel;
1219     	base_addr[CyIER] |= CyTxMpty;
1220         restore_flags(flags);
1221     } /* cy_flush_chars */
1222     
1223     
1224     /* This routine gets called when tty_write has put something into
1225         the write_queue.  If the port is not already transmitting stuff,
1226         start it off by enabling interrupts.  The interrupt service
1227         routine will then ensure that the characters are sent.  If the
1228         port is already active, there is no need to kick it.
1229      */
1230     static int
1231     cy_write(struct tty_struct * tty, int from_user,
1232                const unsigned char *buf, int count)
1233     {
1234       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1235       unsigned long flags;
1236       int c, total = 0;
1237     
1238     #ifdef SERIAL_DEBUG_IO
1239         printk("cy_write ttyS%d\n", info->line); /* */
1240     #endif
1241     
1242         if (serial_paranoia_check(info, tty->device, "cy_write")){
1243     	return 0;
1244         }
1245     	
1246         if (!tty || !info->xmit_buf || !tmp_buf){
1247             return 0;
1248         }
1249     
1250         if (from_user) {
1251     	    down(&tmp_buf_sem);
1252     	    while (1) {
1253     		    c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1254     				       SERIAL_XMIT_SIZE - info->xmit_head));
1255     		    if (c <= 0)
1256     			    break;
1257     
1258     		    c -= copy_from_user(tmp_buf, buf, c);
1259     		    if (!c) {
1260     			    if (!total)
1261     				    total = -EFAULT;
1262     			    break;
1263     		    }
1264     
1265     		    cli();
1266     		    c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1267     				   SERIAL_XMIT_SIZE - info->xmit_head));
1268     		    memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1269     		    info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1270     		    info->xmit_cnt += c;
1271     		    restore_flags(flags);
1272     
1273     		    buf += c;
1274     		    count -= c;
1275     		    total += c;
1276     	    }
1277     	    up(&tmp_buf_sem);
1278         } else {
1279     	    while (1) {
1280     		    cli();
1281     		    c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1282     				       SERIAL_XMIT_SIZE - info->xmit_head));
1283     		    if (c <= 0) {
1284     			    restore_flags(flags);
1285     			    break;
1286     		    }
1287     
1288     		    memcpy(info->xmit_buf + info->xmit_head, buf, c);
1289     		    info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1290     		    info->xmit_cnt += c;
1291     		    restore_flags(flags);
1292     
1293     		    buf += c;
1294     		    count -= c;
1295     		    total += c;
1296     	    }
1297         }
1298     
1299         if (info->xmit_cnt
1300         && !tty->stopped
1301         && !tty->hw_stopped ) {
1302             start_xmit(info);
1303         }
1304         return total;
1305     } /* cy_write */
1306     
1307     
1308     static int
1309     cy_write_room(struct tty_struct *tty)
1310     {
1311       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1312       int	ret;
1313     				
1314     #ifdef SERIAL_DEBUG_IO
1315         printk("cy_write_room ttyS%d\n", info->line); /* */
1316     #endif
1317     
1318         if (serial_paranoia_check(info, tty->device, "cy_write_room"))
1319     	return 0;
1320         ret = PAGE_SIZE - info->xmit_cnt - 1;
1321         if (ret < 0)
1322     	ret = 0;
1323         return ret;
1324     } /* cy_write_room */
1325     
1326     
1327     static int
1328     cy_chars_in_buffer(struct tty_struct *tty)
1329     {
1330       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1331     				
1332     #ifdef SERIAL_DEBUG_IO
1333         printk("cy_chars_in_buffer ttyS%d %d\n", info->line, info->xmit_cnt); /* */
1334     #endif
1335     
1336         if (serial_paranoia_check(info, tty->device, "cy_chars_in_buffer"))
1337     	return 0;
1338     
1339         return info->xmit_cnt;
1340     } /* cy_chars_in_buffer */
1341     
1342     
1343     static void
1344     cy_flush_buffer(struct tty_struct *tty)
1345     {
1346       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1347       unsigned long flags;
1348     				
1349     #ifdef SERIAL_DEBUG_IO
1350         printk("cy_flush_buffer ttyS%d\n", info->line); /* */
1351     #endif
1352     
1353         if (serial_paranoia_check(info, tty->device, "cy_flush_buffer"))
1354     	return;
1355         save_flags(flags); cli();
1356     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1357         restore_flags(flags);
1358         wake_up_interruptible(&tty->write_wait);
1359         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
1360         && tty->ldisc.write_wakeup)
1361     	(tty->ldisc.write_wakeup)(tty);
1362     } /* cy_flush_buffer */
1363     
1364     
1365     /* This routine is called by the upper-layer tty layer to signal
1366        that incoming characters should be throttled or that the
1367        throttle should be released.
1368      */
1369     static void
1370     cy_throttle(struct tty_struct * tty)
1371     {
1372       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1373       unsigned long flags;
1374       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1375       int channel;
1376     
1377     #ifdef SERIAL_DEBUG_THROTTLE
1378       char buf[64];
1379     	
1380         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1381     	   tty->ldisc.chars_in_buffer(tty));
1382         printk("cy_throttle ttyS%d\n", info->line);
1383     #endif
1384     
1385         if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1386     	    return;
1387         }
1388     
1389         if (I_IXOFF(tty)) {
1390     	info->x_char = STOP_CHAR(tty);
1391     	    /* Should use the "Send Special Character" feature!!! */
1392         }
1393     
1394         channel = info->line;
1395     
1396         save_flags(flags); cli();
1397     	base_addr[CyCAR] = (u_char)channel;
1398     	base_addr[CyMSVR1] = 0;
1399         restore_flags(flags);
1400     
1401         return;
1402     } /* cy_throttle */
1403     
1404     
1405     static void
1406     cy_unthrottle(struct tty_struct * tty)
1407     {
1408       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1409       unsigned long flags;
1410       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1411       int channel;
1412     
1413     #ifdef SERIAL_DEBUG_THROTTLE
1414       char buf[64];
1415     	
1416         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1417     	   tty->ldisc.chars_in_buffer(tty));
1418         printk("cy_unthrottle ttyS%d\n", info->line);
1419     #endif
1420     
1421         if (serial_paranoia_check(info, tty->device, "cy_nthrottle")){
1422     	    return;
1423         }
1424     
1425         if (I_IXOFF(tty)) {
1426     	info->x_char = START_CHAR(tty);
1427     	/* Should use the "Send Special Character" feature!!! */
1428         }
1429     
1430         channel = info->line;
1431     
1432         save_flags(flags); cli();
1433     	base_addr[CyCAR] = (u_char)channel;
1434     	base_addr[CyMSVR1] = CyRTS;
1435         restore_flags(flags);
1436     
1437         return;
1438     } /* cy_unthrottle */
1439     
1440     static int
1441     get_serial_info(struct cyclades_port * info,
1442                                struct serial_struct * retinfo)
1443     {
1444       struct serial_struct tmp;
1445     
1446     /* CP('g'); */
1447         if (!retinfo)
1448                 return -EFAULT;
1449         memset(&tmp, 0, sizeof(tmp));
1450         tmp.type = info->type;
1451         tmp.line = info->line;
1452         tmp.port = info->line;
1453         tmp.irq = 0;
1454         tmp.flags = info->flags;
1455         tmp.baud_base = 0;          /*!!!*/
1456         tmp.close_delay = info->close_delay;
1457         tmp.custom_divisor = 0;     /*!!!*/
1458         tmp.hub6 = 0;               /*!!!*/
1459         return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1460     } /* get_serial_info */
1461     
1462     static int
1463     set_serial_info(struct cyclades_port * info,
1464                                struct serial_struct * new_info)
1465     {
1466       struct serial_struct new_serial;
1467       struct cyclades_port old_info;
1468     
1469     /* CP('s'); */
1470         if (!new_info)
1471     	    return -EFAULT;
1472         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1473     	    return -EFAULT;
1474         old_info = *info;
1475     
1476         if (!suser()) {
1477     	    if ((new_serial.close_delay != info->close_delay) ||
1478     		((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1479     		 (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1480     		    return -EPERM;
1481     	    info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1482     			   (new_serial.flags & ASYNC_USR_MASK));
1483     	    goto check_and_exit;
1484         }
1485     
1486     
1487         /*
1488          * OK, past this point, all the error checking has been done.
1489          * At this point, we start making changes.....
1490          */
1491     
1492         info->flags = ((info->flags & ~ASYNC_FLAGS) |
1493     		    (new_serial.flags & ASYNC_FLAGS));
1494         info->close_delay = new_serial.close_delay;
1495     
1496     
1497     check_and_exit:
1498         if (info->flags & ASYNC_INITIALIZED){
1499     	config_setup(info);
1500     	return 0;
1501         }else{
1502             return startup(info);
1503         }
1504     } /* set_serial_info */
1505     
1506     static int
1507     get_modem_info(struct cyclades_port * info, unsigned int *value)
1508     {
1509       int channel;
1510       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1511       unsigned long flags;
1512       unsigned char status;
1513       unsigned int result;
1514     
1515         channel = info->line;
1516     
1517         save_flags(flags); cli();
1518             base_addr[CyCAR] = (u_char)channel;
1519             status = base_addr[CyMSVR1] | base_addr[CyMSVR2];
1520         restore_flags(flags);
1521     
1522         result =  ((status  & CyRTS) ? TIOCM_RTS : 0)
1523                 | ((status  & CyDTR) ? TIOCM_DTR : 0)
1524                 | ((status  & CyDCD) ? TIOCM_CAR : 0)
1525                 | ((status  & CyDSR) ? TIOCM_DSR : 0)
1526                 | ((status  & CyCTS) ? TIOCM_CTS : 0);
1527         return put_user(result,(unsigned int *) value);
1528     } /* get_modem_info */
1529     
1530     static int
1531     set_modem_info(struct cyclades_port * info, unsigned int cmd,
1532                               unsigned int *value)
1533     {
1534       int channel;
1535       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1536       unsigned long flags;
1537       unsigned int arg;
1538     	  
1539         if (get_user(arg, (unsigned long *) value))
1540     	return -EFAULT;
1541         channel = info->line;
1542     
1543         switch (cmd) {
1544         case TIOCMBIS:
1545     	if (arg & TIOCM_RTS){
1546     	    save_flags(flags); cli();
1547     		base_addr[CyCAR] = (u_char)channel;
1548     		base_addr[CyMSVR1] = CyRTS;
1549     	    restore_flags(flags);
1550     	}
1551     	if (arg & TIOCM_DTR){
1552     	    save_flags(flags); cli();
1553     	    base_addr[CyCAR] = (u_char)channel;
1554     /* CP('S');CP('2'); */
1555     	    base_addr[CyMSVR2] = CyDTR;
1556     #ifdef SERIAL_DEBUG_DTR
1557                 printk("cyc: %d: raising DTR\n", __LINE__);
1558                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1559     #endif
1560     	    restore_flags(flags);
1561     	}
1562     	break;
1563         case TIOCMBIC:
1564     	if (arg & TIOCM_RTS){
1565     	    save_flags(flags); cli();
1566     		base_addr[CyCAR] = (u_char)channel;
1567     		base_addr[CyMSVR1] = 0;
1568     	    restore_flags(flags);
1569     	}
1570     	if (arg & TIOCM_DTR){
1571     	    save_flags(flags); cli();
1572     	    base_addr[CyCAR] = (u_char)channel;
1573     /* CP('C');CP('2'); */
1574     	    base_addr[CyMSVR2] = 0;
1575     #ifdef SERIAL_DEBUG_DTR
1576                 printk("cyc: %d: dropping DTR\n", __LINE__);
1577                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1578     #endif
1579     	    restore_flags(flags);
1580     	}
1581     	break;
1582         case TIOCMSET:
1583     	if (arg & TIOCM_RTS){
1584     	    save_flags(flags); cli();
1585     		base_addr[CyCAR] = (u_char)channel;
1586     		base_addr[CyMSVR1] = CyRTS;
1587     	    restore_flags(flags);
1588     	}else{
1589     	    save_flags(flags); cli();
1590     		base_addr[CyCAR] = (u_char)channel;
1591     		base_addr[CyMSVR1] = 0;
1592     	    restore_flags(flags);
1593     	}
1594     	if (arg & TIOCM_DTR){
1595     	    save_flags(flags); cli();
1596     	    base_addr[CyCAR] = (u_char)channel;
1597     /* CP('S');CP('3'); */
1598     	    base_addr[CyMSVR2] = CyDTR;
1599     #ifdef SERIAL_DEBUG_DTR
1600                 printk("cyc: %d: raising DTR\n", __LINE__);
1601                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1602     #endif
1603     	    restore_flags(flags);
1604     	}else{
1605     	    save_flags(flags); cli();
1606     	    base_addr[CyCAR] = (u_char)channel;
1607     /* CP('C');CP('3'); */
1608     	    base_addr[CyMSVR2] = 0;
1609     #ifdef SERIAL_DEBUG_DTR
1610                 printk("cyc: %d: dropping DTR\n", __LINE__);
1611                 printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1612     #endif
1613     	    restore_flags(flags);
1614     	}
1615     	break;
1616         default:
1617     		return -EINVAL;
1618             }
1619         return 0;
1620     } /* set_modem_info */
1621     
1622     static void
1623     send_break( struct cyclades_port * info, int duration)
1624     { /* Let the transmit ISR take care of this (since it
1625          requires stuffing characters into the output stream).
1626        */
1627         info->x_break = duration;
1628         if (!info->xmit_cnt ) {
1629     	start_xmit(info);
1630         }
1631     } /* send_break */
1632     
1633     static int
1634     get_mon_info(struct cyclades_port * info, struct cyclades_monitor * mon)
1635     {
1636     
1637        if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
1638     	   return -EFAULT;
1639        info->mon.int_count  = 0;
1640        info->mon.char_count = 0;
1641        info->mon.char_max   = 0;
1642        info->mon.char_last  = 0;
1643        return 0;
1644     }
1645     
1646     static int
1647     set_threshold(struct cyclades_port * info, unsigned long *arg)
1648     {
1649        volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1650        unsigned long value;
1651        int channel;
1652        
1653        if (get_user(value, arg))
1654     	   return -EFAULT;
1655     
1656        channel = info->line;
1657        info->cor4 &= ~CyREC_FIFO;
1658        info->cor4 |= value & CyREC_FIFO;
1659        base_addr[CyCOR4] = info->cor4;
1660        return 0;
1661     }
1662     
1663     static int
1664     get_threshold(struct cyclades_port * info, unsigned long *value)
1665     {
1666        volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1667        int channel;
1668        unsigned long tmp;
1669        
1670        channel = info->line;
1671     
1672        tmp = base_addr[CyCOR4] & CyREC_FIFO;
1673        return put_user(tmp,value);
1674     }
1675     
1676     static int
1677     set_default_threshold(struct cyclades_port * info, unsigned long *arg)
1678     {
1679        unsigned long value;
1680     
1681        if (get_user(value, arg))
1682     	return -EFAULT;
1683     
1684        info->default_threshold = value & 0x0f;
1685        return 0;
1686     }
1687     
1688     static int
1689     get_default_threshold(struct cyclades_port * info, unsigned long *value)
1690     {
1691        return put_user(info->default_threshold,value);
1692     }
1693     
1694     static int
1695     set_timeout(struct cyclades_port * info, unsigned long *arg)
1696     {
1697        volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1698        int channel;
1699        unsigned long value;
1700     
1701        if (get_user(value, arg))
1702     	   return -EFAULT;
1703        
1704        channel = info->line;
1705     
1706        base_addr[CyRTPRL] = value & 0xff;
1707        base_addr[CyRTPRH] = (value >> 8) & 0xff;
1708        return 0;
1709     }
1710     
1711     static int
1712     get_timeout(struct cyclades_port * info, unsigned long *value)
1713     {
1714        volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1715        int channel;
1716        unsigned long tmp;
1717        
1718        channel = info->line;
1719     
1720        tmp = base_addr[CyRTPRL];
1721        return put_user(tmp,value);
1722     }
1723     
1724     static int
1725     set_default_timeout(struct cyclades_port * info, unsigned long value)
1726     {
1727        info->default_timeout = value & 0xff;
1728        return 0;
1729     }
1730     
1731     static int
1732     get_default_timeout(struct cyclades_port * info, unsigned long *value)
1733     {
1734        return put_user(info->default_timeout,value);
1735     }
1736     
1737     static int
1738     cy_ioctl(struct tty_struct *tty, struct file * file,
1739                 unsigned int cmd, unsigned long arg)
1740     {
1741       unsigned long val;
1742       struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1743       int ret_val = 0;
1744     
1745     #ifdef SERIAL_DEBUG_OTHER
1746         printk("cy_ioctl ttyS%d, cmd = %x arg = %lx\n", info->line, cmd, arg); /* */
1747     #endif
1748     
1749         switch (cmd) {
1750             case CYGETMON:
1751                 ret_val = get_mon_info(info, (struct cyclades_monitor *)arg);
1752     	    break;
1753             case CYGETTHRESH:
1754     	    ret_val = get_threshold(info, (unsigned long *)arg);
1755      	    break;
1756             case CYSETTHRESH:
1757                 ret_val = set_threshold(info, (unsigned long *)arg);
1758     	    break;
1759             case CYGETDEFTHRESH:
1760     	    ret_val = get_default_threshold(info, (unsigned long *)arg);
1761      	    break;
1762             case CYSETDEFTHRESH:
1763                 ret_val = set_default_threshold(info, (unsigned long *)arg);
1764     	    break;
1765             case CYGETTIMEOUT:
1766     	    ret_val = get_timeout(info, (unsigned long *)arg);
1767      	    break;
1768             case CYSETTIMEOUT:
1769                 ret_val = set_timeout(info, (unsigned long *)arg);
1770     	    break;
1771             case CYGETDEFTIMEOUT:
1772     	    ret_val = get_default_timeout(info, (unsigned long *)arg);
1773      	    break;
1774             case CYSETDEFTIMEOUT:
1775                 ret_val = set_default_timeout(info, (unsigned long)arg);
1776     	    break;
1777             case TCSBRK:    /* SVID version: non-zero arg --> no break */
1778     	    ret_val = tty_check_change(tty);
1779     	    if (ret_val)
1780     		    break;
1781                 tty_wait_until_sent(tty,0);
1782                 if (!arg)
1783                     send_break(info, HZ/4); /* 1/4 second */
1784                 break;
1785             case TCSBRKP:   /* support for POSIX tcsendbreak() */
1786     	    ret_val = tty_check_change(tty);
1787     	    if (ret_val)
1788     		break;
1789                 tty_wait_until_sent(tty,0);
1790                 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1791                 break;
1792             case TIOCMBIS:
1793             case TIOCMBIC:
1794             case TIOCMSET:
1795                 ret_val = set_modem_info(info, cmd, (unsigned int *) arg);
1796                 break;
1797     
1798     /* The following commands are incompletely implemented!!! */
1799             case TIOCGSOFTCAR:
1800                 ret_val = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1801                 break;
1802             case TIOCSSOFTCAR:
1803                 ret_val = get_user(val, (unsigned long *) arg);
1804     	    if (ret_val)
1805     		    break;
1806                 tty->termios->c_cflag =
1807                         ((tty->termios->c_cflag & ~CLOCAL) | (val ? CLOCAL : 0));
1808                 break;
1809             case TIOCMGET:
1810                 ret_val = get_modem_info(info, (unsigned int *) arg);
1811                 break;
1812             case TIOCGSERIAL:
1813                 ret_val = get_serial_info(info, (struct serial_struct *) arg);
1814                 break;
1815             case TIOCSSERIAL:
1816                 ret_val = set_serial_info(info,
1817                                        (struct serial_struct *) arg);
1818                 break;
1819             default:
1820     	    ret_val = -ENOIOCTLCMD;
1821         }
1822     
1823     #ifdef SERIAL_DEBUG_OTHER
1824         printk("cy_ioctl done\n");
1825     #endif
1826     
1827         return ret_val;
1828     } /* cy_ioctl */
1829     
1830     
1831     
1832     
1833     static void
1834     cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
1835     {
1836       struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1837     
1838     #ifdef SERIAL_DEBUG_OTHER
1839         printk("cy_set_termios ttyS%d\n", info->line);
1840     #endif
1841     
1842         if (tty->termios->c_cflag == old_termios->c_cflag)
1843             return;
1844         config_setup(info);
1845     
1846         if ((old_termios->c_cflag & CRTSCTS) &&
1847             !(tty->termios->c_cflag & CRTSCTS)) {
1848                 tty->stopped = 0;
1849                 cy_start(tty);
1850         }
1851     #ifdef tytso_patch_94Nov25_1726
1852         if (!(old_termios->c_cflag & CLOCAL) &&
1853             (tty->termios->c_cflag & CLOCAL))
1854                 wake_up_interruptible(&info->open_wait);
1855     #endif
1856     
1857         return;
1858     } /* cy_set_termios */
1859     
1860     
1861     static void
1862     cy_close(struct tty_struct * tty, struct file * filp)
1863     {
1864       struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1865     
1866     /* CP('C'); */
1867     #ifdef SERIAL_DEBUG_OTHER
1868         printk("cy_close ttyS%d\n", info->line);
1869     #endif
1870     
1871         if (!info
1872         || serial_paranoia_check(info, tty->device, "cy_close")){
1873             return;
1874         }
1875     #ifdef SERIAL_DEBUG_OPEN
1876         printk("cy_close ttyS%d, count = %d\n", info->line, info->count);
1877     #endif
1878     
1879         if ((tty->count == 1) && (info->count != 1)) {
1880     	/*
1881     	 * Uh, oh.  tty->count is 1, which means that the tty
1882     	 * structure will be freed.  Info->count should always
1883     	 * be one in these conditions.  If it's greater than
1884     	 * one, we've got real problems, since it means the
1885     	 * serial port won't be shutdown.
1886     	 */
1887     	printk("cy_close: bad serial port count; tty->count is 1, "
1888     	   "info->count is %d\n", info->count);
1889     	info->count = 1;
1890         }
1891     #ifdef SERIAL_DEBUG_COUNT
1892         printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count - 1);
1893     #endif
1894         if (--info->count < 0) {
1895     	printk("cy_close: bad serial port count for ttys%d: %d\n",
1896     	       info->line, info->count);
1897     #ifdef SERIAL_DEBUG_COUNT
1898         printk("cyc: %d: setting count to 0\n", __LINE__);
1899     #endif
1900     	info->count = 0;
1901         }
1902         if (info->count)
1903     	return;
1904         info->flags |= ASYNC_CLOSING;
1905         /*
1906          * Save the termios structure, since this port may have
1907          * separate termios for callout and dialin.
1908          */
1909         if (info->flags & ASYNC_NORMAL_ACTIVE)
1910     	info->normal_termios = *tty->termios;
1911         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1912     	info->callout_termios = *tty->termios;
1913         if (info->flags & ASYNC_INITIALIZED)
1914     	tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
1915         shutdown(info);
1916         if (tty->driver.flush_buffer)
1917     	tty->driver.flush_buffer(tty);
1918         if (tty->ldisc.flush_buffer)
1919     	tty->ldisc.flush_buffer(tty);
1920         info->event = 0;
1921         info->tty = 0;
1922         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1923     	if (tty->ldisc.close)
1924     	    (tty->ldisc.close)(tty);
1925     	tty->ldisc = ldiscs[N_TTY];
1926     	tty->termios->c_line = N_TTY;
1927     	if (tty->ldisc.open)
1928     	    (tty->ldisc.open)(tty);
1929         }
1930         if (info->blocked_open) {
1931     	if (info->close_delay) {
1932     	    current->state = TASK_INTERRUPTIBLE;
1933     	    schedule_timeout(info->close_delay);
1934     	}
1935     	wake_up_interruptible(&info->open_wait);
1936         }
1937         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1938     		     ASYNC_CLOSING);
1939         wake_up_interruptible(&info->close_wait);
1940     
1941     #ifdef SERIAL_DEBUG_OTHER
1942         printk("cy_close done\n");
1943     #endif
1944     
1945         return;
1946     } /* cy_close */
1947     
1948     /*
1949      * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
1950      */
1951     void
1952     cy_hangup(struct tty_struct *tty)
1953     {
1954       struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1955     	
1956     #ifdef SERIAL_DEBUG_OTHER
1957         printk("cy_hangup ttyS%d\n", info->line); /* */
1958     #endif
1959     
1960         if (serial_paranoia_check(info, tty->device, "cy_hangup"))
1961     	return;
1962         
1963         shutdown(info);
1964     #if 0
1965         info->event = 0;
1966         info->count = 0;
1967     #ifdef SERIAL_DEBUG_COUNT
1968         printk("cyc: %d: setting count to 0\n", __LINE__);
1969     #endif
1970         info->tty = 0;
1971     #endif
1972         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1973         wake_up_interruptible(&info->open_wait);
1974     } /* cy_hangup */
1975     
1976     
1977     
1978     /*
1979      * ------------------------------------------------------------
1980      * cy_open() and friends
1981      * ------------------------------------------------------------
1982      */
1983     
1984     static int
1985     block_til_ready(struct tty_struct *tty, struct file * filp,
1986                                struct cyclades_port *info)
1987     {
1988       DECLARE_WAITQUEUE(wait, current);
1989       unsigned long flags;
1990       int channel;
1991       int retval;
1992       volatile u_char *base_addr = (u_char *)BASE_ADDR;
1993     
1994         /*
1995          * If the device is in the middle of being closed, then block
1996          * until it's done, and then try again.
1997          */
1998         if (info->flags & ASYNC_CLOSING) {
1999     	interruptible_sleep_on(&info->close_wait);
2000     	if (info->flags & ASYNC_HUP_NOTIFY){
2001     	    return -EAGAIN;
2002     	}else{
2003     	    return -ERESTARTSYS;
2004     	}
2005         }
2006     
2007         /*
2008          * If this is a callout device, then just make sure the normal
2009          * device isn't being used.
2010          */
2011         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
2012     	if (info->flags & ASYNC_NORMAL_ACTIVE){
2013     	    return -EBUSY;
2014     	}
2015     	if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2016     	    (info->flags & ASYNC_SESSION_LOCKOUT) &&
2017     	    (info->session != current->session)){
2018     	    return -EBUSY;
2019     	}
2020     	if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2021     	    (info->flags & ASYNC_PGRP_LOCKOUT) &&
2022     	    (info->pgrp != current->pgrp)){
2023     	    return -EBUSY;
2024     	}
2025     	info->flags |= ASYNC_CALLOUT_ACTIVE;
2026     	return 0;
2027         }
2028     
2029         /*
2030          * If non-blocking mode is set, then make the check up front
2031          * and then exit.
2032          */
2033         if (filp->f_flags & O_NONBLOCK) {
2034     	if (info->flags & ASYNC_CALLOUT_ACTIVE){
2035     	    return -EBUSY;
2036     	}
2037     	info->flags |= ASYNC_NORMAL_ACTIVE;
2038     	return 0;
2039         }
2040     
2041         /*
2042          * Block waiting for the carrier detect and the line to become
2043          * free (i.e., not in use by the callout).  While we are in
2044          * this loop, info->count is dropped by one, so that
2045          * cy_close() knows when to free things.  We restore it upon
2046          * exit, either normal or abnormal.
2047          */
2048         retval = 0;
2049         add_wait_queue(&info->open_wait, &wait);
2050     #ifdef SERIAL_DEBUG_OPEN
2051         printk("block_til_ready before block: ttyS%d, count = %d\n",
2052     	   info->line, info->count);/**/
2053     #endif
2054         info->count--;
2055     #ifdef SERIAL_DEBUG_COUNT
2056         printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count);
2057     #endif
2058         info->blocked_open++;
2059     
2060         channel = info->line;
2061     
2062         while (1) {
2063     	save_flags(flags); cli();
2064     	    if (!(info->flags & ASYNC_CALLOUT_ACTIVE)){
2065     		base_addr[CyCAR] = (u_char)channel;
2066     		base_addr[CyMSVR1] = CyRTS;
2067     /* CP('S');CP('4'); */
2068     		base_addr[CyMSVR2] = CyDTR;
2069     #ifdef SERIAL_DEBUG_DTR
2070                     printk("cyc: %d: raising DTR\n", __LINE__);
2071                     printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
2072     #endif
2073     	    }
2074     	restore_flags(flags);
2075     	set_current_state(TASK_INTERRUPTIBLE);
2076     	if (tty_hung_up_p(filp)
2077     	|| !(info->flags & ASYNC_INITIALIZED) ){
2078     	    if (info->flags & ASYNC_HUP_NOTIFY) {
2079     		retval = -EAGAIN;
2080     	    }else{
2081     		retval = -ERESTARTSYS;
2082     	    }
2083     	    break;
2084     	}
2085     	save_flags(flags); cli();
2086     	    base_addr[CyCAR] = (u_char)channel;
2087     /* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1] & CyDCD) ); */
2088     	    if (!(info->flags & ASYNC_CALLOUT_ACTIVE)
2089     	    && !(info->flags & ASYNC_CLOSING)
2090     	    && (C_CLOCAL(tty)
2091     	        || (base_addr[CyMSVR1] & CyDCD))) {
2092     		    restore_flags(flags);
2093     		    break;
2094     	    }
2095     	restore_flags(flags);
2096     	if (signal_pending(current)) {
2097     	    retval = -ERESTARTSYS;
2098     	    break;
2099     	}
2100     #ifdef SERIAL_DEBUG_OPEN
2101     	printk("block_til_ready blocking: ttyS%d, count = %d\n",
2102     	       info->line, info->count);/**/
2103     #endif
2104     	schedule();
2105         }
2106         current->state = TASK_RUNNING;
2107         remove_wait_queue(&info->open_wait, &wait);
2108         if (!tty_hung_up_p(filp)){
2109     	info->count++;
2110     #ifdef SERIAL_DEBUG_COUNT
2111         printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
2112     #endif
2113         }
2114         info->blocked_open--;
2115     #ifdef SERIAL_DEBUG_OPEN
2116         printk("block_til_ready after blocking: ttyS%d, count = %d\n",
2117     	   info->line, info->count);/**/
2118     #endif
2119         if (retval)
2120     	    return retval;
2121         info->flags |= ASYNC_NORMAL_ACTIVE;
2122         return 0;
2123     } /* block_til_ready */
2124     
2125     /*
2126      * This routine is called whenever a serial port is opened.  It
2127      * performs the serial-specific initialization for the tty structure.
2128      */
2129     int
2130     cy_open(struct tty_struct *tty, struct file * filp)
2131     {
2132       struct cyclades_port  *info;
2133       int retval, line;
2134     
2135     /* CP('O'); */
2136         line = MINOR(tty->device) - tty->driver.minor_start;
2137         if ((line < 0) || (NR_PORTS <= line)){
2138             return -ENODEV;
2139         }
2140         info = &cy_port[line];
2141         if (info->line < 0){
2142             return -ENODEV;
2143         }
2144     #ifdef SERIAL_DEBUG_OTHER
2145         printk("cy_open ttyS%d\n", info->line); /* */
2146     #endif
2147         if (serial_paranoia_check(info, tty->device, "cy_open")){
2148             return -ENODEV;
2149         }
2150     #ifdef SERIAL_DEBUG_OPEN
2151         printk("cy_open ttyS%d, count = %d\n", info->line, info->count);/**/
2152     #endif
2153         info->count++;
2154     #ifdef SERIAL_DEBUG_COUNT
2155         printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
2156     #endif
2157         tty->driver_data = info;
2158         info->tty = tty;
2159     
2160         if (!tmp_buf) {
2161     	tmp_buf = (unsigned char *) get_free_page(GFP_KERNEL);
2162     	if (!tmp_buf){
2163     	    return -ENOMEM;
2164             }
2165         }
2166     
2167         if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
2168     	if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2169     	    *tty->termios = info->normal_termios;
2170     	else 
2171     	    *tty->termios = info->callout_termios;
2172         }
2173         /*
2174          * Start up serial port
2175          */
2176         retval = startup(info);
2177         if (retval){
2178     	return retval;
2179         }
2180     
2181         retval = block_til_ready(tty, filp, info);
2182         if (retval) {
2183     #ifdef SERIAL_DEBUG_OPEN
2184     	printk("cy_open returning after block_til_ready with %d\n",
2185     	       retval);
2186     #endif
2187     	return retval;
2188         }
2189     
2190         info->session = current->session;
2191         info->pgrp = current->pgrp;
2192     
2193     #ifdef SERIAL_DEBUG_OPEN
2194         printk("cy_open done\n");/**/
2195     #endif
2196         return 0;
2197     } /* cy_open */
2198     
2199     
2200     
2201     /*
2202      * ---------------------------------------------------------------------
2203      * serial167_init() and friends
2204      *
2205      * serial167_init() is called at boot-time to initialize the serial driver.
2206      * ---------------------------------------------------------------------
2207      */
2208     
2209     /*
2210      * This routine prints out the appropriate serial driver version
2211      * number, and identifies which options were configured into this
2212      * driver.
2213      */
2214     static void
2215     show_version(void)
2216     {
2217         printk("MVME166/167 cd2401 driver\n");
2218     } /* show_version */
2219     
2220     /* initialize chips on card -- return number of valid
2221        chips (which is number of ports/4) */
2222     
2223     /*
2224      * This initialises the hardware to a reasonable state.  It should
2225      * probe the chip first so as to copy 166-Bug setup as a default for
2226      * port 0.  It initialises CMR to CyASYNC; that is never done again, so
2227      * as to limit the number of CyINIT_CHAN commands in normal running.
2228      *
2229      * ... I wonder what I should do if this fails ...
2230      */
2231     
2232     void
2233     mvme167_serial_console_setup(int cflag)
2234     {
2235     	volatile unsigned char* base_addr = (u_char *)BASE_ADDR;
2236     	int ch;
2237     	u_char spd;
2238     	u_char rcor, rbpr, badspeed = 0;
2239     	unsigned long flags;
2240     
2241     	save_flags(flags); cli();
2242     
2243     	/*
2244     	 * First probe channel zero of the chip, to see what speed has
2245     	 * been selected.
2246     	 */
2247     
2248     	base_addr[CyCAR] = 0;
2249     
2250     	rcor = base_addr[CyRCOR] << 5;
2251     	rbpr = base_addr[CyRBPR];
2252     
2253     	for (spd = 0; spd < sizeof(baud_bpr); spd++)
2254     		if (rbpr == baud_bpr[spd] && rcor == baud_co[spd])
2255     			break;
2256     	if (spd >= sizeof(baud_bpr)) {
2257     		spd = 14;	/* 19200 */
2258     		badspeed = 1;	/* Failed to identify speed */
2259     	}
2260     	initial_console_speed = spd;
2261     
2262     	/* OK, we have chosen a speed, now reset and reinitialise */
2263     
2264             my_udelay(20000L);	/* Allow time for any active o/p to complete */
2265             if(base_addr[CyCCR] != 0x00){
2266                 restore_flags(flags);
2267                 /* printk(" chip is never idle (CCR != 0)\n"); */
2268                 return;
2269             }
2270     
2271             base_addr[CyCCR] = CyCHIP_RESET;	/* Reset the chip */
2272             my_udelay(1000L);
2273     
2274             if(base_addr[CyGFRCR] == 0x00){
2275                 restore_flags(flags);
2276                 /* printk(" chip is not responding (GFRCR stayed 0)\n"); */
2277                 return;
2278             }
2279     
2280     	/*
2281     	 * System clock is 20Mhz, divided by 2048, so divide by 10 for a 1.0ms
2282     	 * tick
2283     	 */
2284     
2285     	base_addr[CyTPR] = 10;
2286     
2287     	base_addr[CyPILR1] = 0x01;    /* Interrupt level for modem change */
2288     	base_addr[CyPILR2] = 0x02;    /* Interrupt level for tx ints */
2289     	base_addr[CyPILR3] = 0x03;    /* Interrupt level for rx ints */
2290     
2291     	/*
2292     	 * Attempt to set up all channels to something reasonable, and
2293     	 * bang out a INIT_CHAN command.  We should then be able to limit
2294     	 * the ammount of fiddling we have to do in normal running.
2295     	 */
2296     
2297     	for (ch = 3; ch >= 0 ; ch--) {
2298     		base_addr[CyCAR] = (u_char)ch;
2299     		base_addr[CyIER] = 0;
2300     		base_addr[CyCMR] = CyASYNC;
2301     		base_addr[CyLICR] = (u_char)ch << 2;
2302     		base_addr[CyLIVR] = 0x5c;
2303     		base_addr[CyTCOR] = baud_co[spd];
2304     		base_addr[CyTBPR] = baud_bpr[spd];
2305     		base_addr[CyRCOR] = baud_co[spd] >> 5;
2306     		base_addr[CyRBPR] = baud_bpr[spd];
2307     		base_addr[CySCHR1] = 'Q' & 0x1f;
2308     		base_addr[CySCHR2] = 'X' & 0x1f;
2309     		base_addr[CySCRL] = 0;
2310     		base_addr[CySCRH] = 0;
2311     		base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
2312     		base_addr[CyCOR2] = 0;
2313     		base_addr[CyCOR3] = Cy_1_STOP;
2314     		base_addr[CyCOR4] = baud_cor4[spd];
2315     		base_addr[CyCOR5] = 0;
2316     		base_addr[CyCOR6] = 0;
2317     		base_addr[CyCOR7] = 0;
2318     		base_addr[CyRTPRL] = 2;
2319     		base_addr[CyRTPRH] = 0;
2320     	        base_addr[CyMSVR1] = 0;
2321     	        base_addr[CyMSVR2] = 0;
2322     		write_cy_cmd(base_addr,CyINIT_CHAN|CyDIS_RCVR|CyDIS_XMTR);
2323     	}
2324     
2325     	/*
2326     	 * Now do specials for channel zero....
2327     	 */
2328     
2329             base_addr[CyMSVR1] = CyRTS;
2330             base_addr[CyMSVR2] = CyDTR;
2331     	base_addr[CyIER] = CyRxData;
2332     	write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
2333     
2334     	restore_flags(flags);
2335     
2336     	my_udelay(20000L);	/* Let it all settle down */
2337     
2338             printk("CD2401 initialised,  chip is rev 0x%02x\n", base_addr[CyGFRCR]);
2339     	if (badspeed)
2340             	printk("  WARNING:  Failed to identify line speed, rcor=%02x,rbpr=%02x\n",
2341     					rcor >> 5, rbpr);
2342     } /* serial_console_init */
2343     
2344     /* The serial driver boot-time initialization code!
2345         Hardware I/O ports are mapped to character special devices on a
2346         first found, first allocated manner.  That is, this code searches
2347         for Cyclom cards in the system.  As each is found, it is probed
2348         to discover how many chips (and thus how many ports) are present.
2349         These ports are mapped to the tty ports 64 and upward in monotonic
2350         fashion.  If an 8-port card is replaced with a 16-port card, the
2351         port mapping on a following card will shift.
2352     
2353         This approach is different from what is used in the other serial
2354         device driver because the Cyclom is more properly a multiplexer,
2355         not just an aggregation of serial ports on one card.
2356     
2357         If there are more cards with more ports than have been statically
2358         allocated above, a warning is printed and the extra ports are ignored.
2359      */
2360     int
2361     serial167_init(void)
2362     {
2363       struct cyclades_port *info;
2364       int ret = 0;
2365       int good_ports = 0;
2366       int port_num = 0;
2367       int index;
2368       int DefSpeed;
2369     #ifdef notyet
2370       struct sigaction sa;
2371     #endif
2372     
2373         if (!(mvme16x_config &MVME16x_CONFIG_GOT_CD2401))
2374     	return 0;
2375     
2376     #if 0
2377     scrn[1] = '\0';
2378     #endif
2379     
2380         show_version();
2381     
2382         /* Has "console=0,9600n8" been used in bootinfo to change speed? */
2383         if (serial_console_cflag)
2384     	DefSpeed = serial_console_cflag & 0017;
2385         else {
2386     	DefSpeed = initial_console_speed;
2387     	serial_console_info = &cy_port[0];
2388     	serial_console_cflag = DefSpeed | CS8;
2389     #if 0
2390     	serial_console = 64; /*callout_driver.minor_start*/
2391     #endif
2392         }
2393     
2394         /* Initialize the tty_driver structure */
2395         
2396         memset(&cy_serial_driver, 0, sizeof(struct tty_driver));
2397         cy_serial_driver.magic = TTY_DRIVER_MAGIC;
2398         cy_serial_driver.name = "ttyS";
2399         cy_serial_driver.major = TTY_MAJOR;
2400         cy_serial_driver.minor_start = 64;
2401         cy_serial_driver.num = NR_PORTS;
2402         cy_serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2403         cy_serial_driver.subtype = SERIAL_TYPE_NORMAL;
2404         cy_serial_driver.init_termios = tty_std_termios;
2405         cy_serial_driver.init_termios.c_cflag =
2406     	    B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2407         cy_serial_driver.flags = TTY_DRIVER_REAL_RAW;
2408         cy_serial_driver.refcount = &serial_refcount;
2409         cy_serial_driver.table = serial_table;
2410         cy_serial_driver.termios = serial_termios;
2411         cy_serial_driver.termios_locked = serial_termios_locked;
2412         cy_serial_driver.open = cy_open;
2413         cy_serial_driver.close = cy_close;
2414         cy_serial_driver.write = cy_write;
2415         cy_serial_driver.put_char = cy_put_char;
2416         cy_serial_driver.flush_chars = cy_flush_chars;
2417         cy_serial_driver.write_room = cy_write_room;
2418         cy_serial_driver.chars_in_buffer = cy_chars_in_buffer;
2419         cy_serial_driver.flush_buffer = cy_flush_buffer;
2420         cy_serial_driver.ioctl = cy_ioctl;
2421         cy_serial_driver.throttle = cy_throttle;
2422         cy_serial_driver.unthrottle = cy_unthrottle;
2423         cy_serial_driver.set_termios = cy_set_termios;
2424         cy_serial_driver.stop = cy_stop;
2425         cy_serial_driver.start = cy_start;
2426         cy_serial_driver.hangup = cy_hangup;
2427     
2428         /*
2429          * The callout device is just like normal device except for
2430          * major number and the subtype code.
2431          */
2432         cy_callout_driver = cy_serial_driver;
2433         cy_callout_driver.name = "cua";
2434         cy_callout_driver.major = TTYAUX_MAJOR;
2435         cy_callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2436     
2437         ret = tty_register_driver(&cy_serial_driver);
2438         if (ret) {
2439     	    printk(KERN_ERR "Couldn't register MVME166/7 serial driver\n");
2440     	    return ret;
2441         }
2442         ret = tty_register_driver(&cy_callout_driver);
2443         if (ret) {
2444     	    printk(KERN_ERR "Couldn't register MVME166/7 callout driver\n");
2445     	    goto cleanup_serial_driver;
2446         }
2447     
2448         init_bh(CYCLADES_BH, do_cyclades_bh);
2449     
2450         port_num = 0;
2451         info = cy_port;
2452         for (index = 0; index < 1; index++) {
2453     
2454     	good_ports = 4;
2455     
2456     	if(port_num < NR_PORTS){
2457     	    while( good_ports-- && port_num < NR_PORTS){
2458     		/*** initialize port ***/
2459     		info->magic = CYCLADES_MAGIC;
2460     		info->type = PORT_CIRRUS;
2461     		info->card = index;
2462     		info->line = port_num;
2463     		info->flags = STD_COM_FLAGS;
2464     		info->tty = 0;
2465     		info->xmit_fifo_size = 12;
2466     		info->cor1 = CyPARITY_NONE|Cy_8_BITS;
2467     		info->cor2 = CyETC;
2468     		info->cor3 = Cy_1_STOP;
2469     		info->cor4 = 0x08; /* _very_ small receive threshold */
2470     		info->cor5 = 0;
2471     		info->cor6 = 0;
2472     		info->cor7 = 0;
2473     		info->tbpr = baud_bpr[DefSpeed]; /* Tx BPR */
2474     		info->tco = baud_co[DefSpeed]; /* Tx CO */
2475     		info->rbpr = baud_bpr[DefSpeed]; /* Rx BPR */
2476     		info->rco = baud_co[DefSpeed] >> 5; /* Rx CO */
2477     		info->close_delay = 0;
2478     		info->x_char = 0;
2479     		info->event = 0;
2480     		info->count = 0;
2481     #ifdef SERIAL_DEBUG_COUNT
2482         printk("cyc: %d: setting count to 0\n", __LINE__);
2483     #endif
2484     		info->blocked_open = 0;
2485     		info->default_threshold = 0;
2486     		info->default_timeout = 0;
2487     		info->tqueue.routine = do_softint;
2488     		info->tqueue.data = info;
2489     		info->callout_termios =cy_callout_driver.init_termios;
2490     		info->normal_termios = cy_serial_driver.init_termios;
2491     		init_waitqueue_head(&info->open_wait);
2492     		init_waitqueue_head(&info->close_wait);
2493     		/* info->session */
2494     		/* info->pgrp */
2495     /*** !!!!!!!! this may expose new bugs !!!!!!!!! *********/
2496     		info->read_status_mask = CyTIMEOUT| CySPECHAR| CyBREAK
2497                                            | CyPARITY| CyFRAME| CyOVERRUN;
2498     		/* info->timeout */
2499     
2500     		printk("ttyS%1d ", info->line);
2501     		port_num++;info++;
2502     		if(!(port_num & 7)){
2503     		    printk("\n               ");
2504     		}
2505     	    }
2506     	}
2507     	printk("\n");
2508         }
2509         while( port_num < NR_PORTS){
2510     	info->line = -1;
2511     	port_num++;info++;
2512         }
2513     #ifdef CONFIG_REMOTE_DEBUG
2514         debug_setup();
2515     #endif
2516         ret = request_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt, 0,
2517     				"cd2401_errors", cd2401_rxerr_interrupt);
2518         if (ret) {
2519     	    printk(KERN_ERR "Could't get cd2401_errors IRQ");
2520     	    goto cleanup_callout_driver;
2521         }
2522     
2523         ret = request_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt, 0,
2524     				"cd2401_modem", cd2401_modem_interrupt);
2525         if (ret) {
2526     	    printk(KERN_ERR "Could't get cd2401_modem IRQ");
2527     	    goto cleanup_irq_cd2401_errors;
2528         }
2529     
2530         ret = request_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt, 0,
2531     				"cd2401_txints", cd2401_tx_interrupt);
2532         if (ret) {
2533     	    printk(KERN_ERR "Could't get cd2401_txints IRQ");
2534     	    goto cleanup_irq_cd2401_modem;
2535         }
2536     
2537         ret = request_irq(MVME167_IRQ_SER_RX, cd2401_rx_interrupt, 0,
2538     				"cd2401_rxints", cd2401_rx_interrupt);
2539         if (ret) {
2540     	    printk(KERN_ERR "Could't get cd2401_rxints IRQ");
2541     	    goto cleanup_irq_cd2401_txints;
2542         }
2543     
2544         /* Now we have registered the interrupt handlers, allow the interrupts */
2545     
2546         pcc2chip[PccSCCMICR] = 0x15;		/* Serial ints are level 5 */
2547         pcc2chip[PccSCCTICR] = 0x15;
2548         pcc2chip[PccSCCRICR] = 0x15;
2549     
2550         pcc2chip[PccIMLR] = 3;			/* Allow PCC2 ints above 3!? */
2551     
2552         return 0;
2553     cleanup_irq_cd2401_txints:
2554         free_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt);
2555     cleanup_irq_cd2401_modem:
2556         free_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt);
2557     cleanup_irq_cd2401_errors:
2558         free_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt);
2559     cleanup_callout_driver:
2560         if (tty_unregister_driver(&cy_callout_driver))
2561     	    printk(KERN_ERR "Couldn't unregister MVME166/7 callout driver\n");
2562     cleanup_serial_driver:
2563         if (tty_unregister_driver(&cy_serial_driver))
2564     	    printk(KERN_ERR "Couldn't unregister MVME166/7 serial driver\n");
2565         return ret;
2566     } /* serial167_init */
2567     
2568     
2569     #ifdef CYCLOM_SHOW_STATUS
2570     static void
2571     show_status(int line_num)
2572     {
2573       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2574       int channel;
2575       struct cyclades_port * info;
2576       unsigned long flags;
2577     
2578         info = &cy_port[line_num];
2579         channel = info->line;
2580         printk("  channel %d\n", channel);/**/
2581     
2582         printk(" cy_port\n");
2583         printk("  card line flags = %d %d %x\n",
2584                      info->card, info->line, info->flags);
2585         printk("  *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
2586                      (long)info->tty, info->read_status_mask,
2587                      info->timeout, info->xmit_fifo_size);
2588         printk("  cor1,cor2,cor3,cor4,cor5,cor6,cor7 = %x %x %x %x %x %x %x\n",
2589                  info->cor1, info->cor2, info->cor3, info->cor4, info->cor5,
2590     			info->cor6, info->cor7);
2591         printk("  tbpr,tco,rbpr,rco = %d %d %d %d\n",
2592                  info->tbpr, info->tco, info->rbpr, info->rco);
2593         printk("  close_delay event count = %d %d %d\n",
2594                  info->close_delay, info->event, info->count);
2595         printk("  x_char blocked_open = %x %x\n",
2596                  info->x_char, info->blocked_open);
2597         printk("  session pgrp open_wait = %lx %lx %lx\n",
2598                  info->session, info->pgrp, (long)info->open_wait);
2599     
2600     
2601         save_flags(flags); cli();
2602     
2603     /* Global Registers */
2604     
2605     	printk(" CyGFRCR %x\n", base_addr[CyGFRCR]);
2606     	printk(" CyCAR %x\n", base_addr[CyCAR]);
2607     	printk(" CyRISR %x\n", base_addr[CyRISR]);
2608     	printk(" CyTISR %x\n", base_addr[CyTISR]);
2609     	printk(" CyMISR %x\n", base_addr[CyMISR]);
2610     	printk(" CyRIR %x\n", base_addr[CyRIR]);
2611     	printk(" CyTIR %x\n", base_addr[CyTIR]);
2612     	printk(" CyMIR %x\n", base_addr[CyMIR]);
2613     	printk(" CyTPR %x\n", base_addr[CyTPR]);
2614     
2615     	base_addr[CyCAR] = (u_char)channel;
2616     
2617     /* Virtual Registers */
2618     
2619     #if 0
2620     	printk(" CyRIVR %x\n", base_addr[CyRIVR]);
2621     	printk(" CyTIVR %x\n", base_addr[CyTIVR]);
2622     	printk(" CyMIVR %x\n", base_addr[CyMIVR]);
2623     	printk(" CyMISR %x\n", base_addr[CyMISR]);
2624     #endif
2625     
2626     /* Channel Registers */
2627     
2628     	printk(" CyCCR %x\n", base_addr[CyCCR]);
2629     	printk(" CyIER %x\n", base_addr[CyIER]);
2630     	printk(" CyCOR1 %x\n", base_addr[CyCOR1]);
2631     	printk(" CyCOR2 %x\n", base_addr[CyCOR2]);
2632     	printk(" CyCOR3 %x\n", base_addr[CyCOR3]);
2633     	printk(" CyCOR4 %x\n", base_addr[CyCOR4]);
2634     	printk(" CyCOR5 %x\n", base_addr[CyCOR5]);
2635     #if 0
2636     	printk(" CyCCSR %x\n", base_addr[CyCCSR]);
2637     	printk(" CyRDCR %x\n", base_addr[CyRDCR]);
2638     #endif
2639     	printk(" CySCHR1 %x\n", base_addr[CySCHR1]);
2640     	printk(" CySCHR2 %x\n", base_addr[CySCHR2]);
2641     #if 0
2642     	printk(" CySCHR3 %x\n", base_addr[CySCHR3]);
2643     	printk(" CySCHR4 %x\n", base_addr[CySCHR4]);
2644     	printk(" CySCRL %x\n", base_addr[CySCRL]);
2645     	printk(" CySCRH %x\n", base_addr[CySCRH]);
2646     	printk(" CyLNC %x\n", base_addr[CyLNC]);
2647     	printk(" CyMCOR1 %x\n", base_addr[CyMCOR1]);
2648     	printk(" CyMCOR2 %x\n", base_addr[CyMCOR2]);
2649     #endif
2650     	printk(" CyRTPRL %x\n", base_addr[CyRTPRL]);
2651     	printk(" CyRTPRH %x\n", base_addr[CyRTPRH]);
2652     	printk(" CyMSVR1 %x\n", base_addr[CyMSVR1]);
2653     	printk(" CyMSVR2 %x\n", base_addr[CyMSVR2]);
2654     	printk(" CyRBPR %x\n", base_addr[CyRBPR]);
2655     	printk(" CyRCOR %x\n", base_addr[CyRCOR]);
2656     	printk(" CyTBPR %x\n", base_addr[CyTBPR]);
2657     	printk(" CyTCOR %x\n", base_addr[CyTCOR]);
2658     
2659         restore_flags(flags);
2660     } /* show_status */
2661     #endif
2662     
2663     
2664     #if 0
2665     /* Dummy routine in mvme16x/config.c for now */
2666     
2667     /* Serial console setup. Called from linux/init/main.c */
2668     
2669     void console_setup(char *str, int *ints)
2670     {
2671     	char *s;
2672     	int baud, bits, parity;
2673     	int cflag = 0;
2674     
2675     	/* Sanity check. */
2676     	if (ints[0] > 3 || ints[1] > 3) return;
2677     
2678     	/* Get baud, bits and parity */
2679     	baud = 2400;
2680     	bits = 8;
2681     	parity = 'n';
2682     	if (ints[2]) baud = ints[2];
2683     	if ((s = strchr(str, ','))) {
2684     		do {
2685     			s++;
2686     		} while(*s >= '0' && *s <= '9');
2687     		if (*s) parity = *s++;
2688     		if (*s) bits   = *s - '0';
2689     	}
2690     
2691     	/* Now construct a cflag setting. */
2692     	switch(baud) {
2693     		case 1200:
2694     			cflag |= B1200;
2695     			break;
2696     		case 9600:
2697     			cflag |= B9600;
2698     			break;
2699     		case 19200:
2700     			cflag |= B19200;
2701     			break;
2702     		case 38400:
2703     			cflag |= B38400;
2704     			break;
2705     		case 2400:
2706     		default:
2707     			cflag |= B2400;
2708     			break;
2709     	}
2710     	switch(bits) {
2711     		case 7:
2712     			cflag |= CS7;
2713     			break;
2714     		default:
2715     		case 8:
2716     			cflag |= CS8;
2717     			break;
2718     	}
2719     	switch(parity) {
2720     		case 'o': case 'O':
2721     			cflag |= PARODD;
2722     			break;
2723     		case 'e': case 'E':
2724     			cflag |= PARENB;
2725     			break;
2726     	}
2727     
2728     	serial_console_info = &cy_port[ints[1]];
2729     	serial_console_cflag = cflag;
2730     	serial_console = ints[1] + 64; /*callout_driver.minor_start*/
2731     }
2732     #endif
2733     
2734     /*
2735      * The following is probably out of date for 2.1.x serial console stuff.
2736      *
2737      * The console is registered early on from arch/m68k/kernel/setup.c, and
2738      * it therefore relies on the chip being setup correctly by 166-Bug.  This
2739      * seems reasonable, as the serial port has been used to invoke the system
2740      * boot.  It also means that this function must not rely on any data
2741      * initialisation performed by serial167_init() etc.
2742      *
2743      * Of course, once the console has been registered, we had better ensure
2744      * that serial167_init() doesn't leave the chip non-functional.
2745      *
2746      * The console must be locked when we get here.
2747      */
2748     
2749     void serial167_console_write(struct console *co, const char *str, unsigned count)
2750     {
2751     	volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2752     	unsigned long flags;
2753     	volatile u_char sink;
2754     	u_char ier;
2755     	int port;
2756     	u_char do_lf = 0;
2757     	int i = 0;
2758     
2759     	save_flags(flags); cli();
2760     
2761     	/* Ensure transmitter is enabled! */
2762     
2763     	port = 0;
2764     	base_addr[CyCAR] = (u_char)port;
2765     	while (base_addr[CyCCR])
2766     		;
2767     	base_addr[CyCCR] = CyENB_XMTR;
2768     
2769     	ier = base_addr[CyIER];
2770     	base_addr[CyIER] = CyTxMpty;
2771     
2772     	while (1) {
2773     		if (pcc2chip[PccSCCTICR] & 0x20)
2774     		{
2775     			/* We have a Tx int. Acknowledge it */
2776     			sink = pcc2chip[PccTPIACKR];
2777     			if ((base_addr[CyLICR] >> 2) == port) {
2778     				if (i == count) {
2779     					/* Last char of string is now output */
2780     					base_addr[CyTEOIR] = CyNOTRANS;
2781     					break;
2782     				}
2783     				if (do_lf) {
2784     					base_addr[CyTDR] = '\n';
2785     					str++;
2786     					i++;
2787     					do_lf = 0;
2788     				}
2789     				else if (*str == '\n') {
2790     					base_addr[CyTDR] = '\r';
2791     					do_lf = 1;
2792     				}
2793     				else {
2794     					base_addr[CyTDR] = *str++;
2795     					i++;
2796     				}
2797     				base_addr[CyTEOIR] = 0;
2798     			}
2799     			else
2800     				base_addr[CyTEOIR] = CyNOTRANS;
2801     		}
2802     	}
2803     
2804     	base_addr[CyIER] = ier;
2805     
2806     	restore_flags(flags);
2807     }
2808     
2809     /* This is a hack; if there are multiple chars waiting in the chip we
2810      * discard all but the last one, and return that.  The cd2401 is not really
2811      * designed to be driven in polled mode.
2812      */
2813     
2814     int serial167_console_wait_key(struct console *co)
2815     {
2816     	volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2817     	unsigned long flags;
2818     	volatile u_char sink;
2819     	u_char ier;
2820     	int port;
2821     	int keypress = 0;
2822     
2823     	save_flags(flags); cli();
2824     
2825     	/* Ensure receiver is enabled! */
2826     
2827     	port = 0;
2828     	base_addr[CyCAR] = (u_char)port;
2829     	while (base_addr[CyCCR])
2830     		;
2831     	base_addr[CyCCR] = CyENB_RCVR;
2832     	ier = base_addr[CyIER];
2833     	base_addr[CyIER] = CyRxData;
2834     
2835     	while (!keypress) {
2836     		if (pcc2chip[PccSCCRICR] & 0x20)
2837     		{
2838     			/* We have an Rx int. Acknowledge it */
2839     			sink = pcc2chip[PccRPIACKR];
2840     			if ((base_addr[CyLICR] >> 2) == port) {
2841     				int cnt = base_addr[CyRFOC];
2842     				while (cnt-- > 0)
2843     				{
2844     					keypress = base_addr[CyRDR];
2845     				}
2846     				base_addr[CyREOIR] = 0;
2847     			}
2848     			else
2849     				base_addr[CyREOIR] = CyNOTRANS;
2850     		}
2851     	}
2852     
2853     	base_addr[CyIER] = ier;
2854     
2855     	restore_flags(flags);
2856     
2857     	return keypress;
2858     }
2859     
2860     
2861     static kdev_t serial167_console_device(struct console *c)
2862     {
2863     	return MKDEV(TTY_MAJOR, 64 + c->index);
2864     }
2865     
2866     
2867     static int __init serial167_console_setup(struct console *co, char *options)
2868     {
2869     	return 0;
2870     }
2871     
2872     
2873     static struct console sercons = {
2874     	name:		"ttyS",
2875     	write:		serial167_console_write,
2876     	device:		serial167_console_device,
2877     	wait_key:	serial167_console_wait_key,
2878     	setup:		serial167_console_setup,
2879     	flags:		CON_PRINTBUFFER,
2880     	index:		-1,
2881     };
2882     
2883     
2884     void __init serial167_console_init(void)
2885     {
2886     	if (vme_brdtype == VME_TYPE_MVME166 ||
2887     			vme_brdtype == VME_TYPE_MVME167 ||
2888     			vme_brdtype == VME_TYPE_MVME177) {
2889     		mvme167_serial_console_setup(0);
2890     		register_console(&sercons);
2891     	}
2892     }
2893     
2894     #ifdef CONFIG_REMOTE_DEBUG
2895     void putDebugChar (int c)
2896     {
2897     	volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2898     	unsigned long flags;
2899     	volatile u_char sink;
2900     	u_char ier;
2901     	int port;
2902     
2903     	save_flags(flags); cli();
2904     
2905     	/* Ensure transmitter is enabled! */
2906     
2907     	port = DEBUG_PORT;
2908     	base_addr[CyCAR] = (u_char)port;
2909     	while (base_addr[CyCCR])
2910     		;
2911     	base_addr[CyCCR] = CyENB_XMTR;
2912     
2913     	ier = base_addr[CyIER];
2914     	base_addr[CyIER] = CyTxMpty;
2915     
2916     	while (1) {
2917     		if (pcc2chip[PccSCCTICR] & 0x20)
2918     		{
2919     			/* We have a Tx int. Acknowledge it */
2920     			sink = pcc2chip[PccTPIACKR];
2921     			if ((base_addr[CyLICR] >> 2) == port) {
2922     				base_addr[CyTDR] = c;
2923     				base_addr[CyTEOIR] = 0;
2924     				break;
2925     			}
2926     			else
2927     				base_addr[CyTEOIR] = CyNOTRANS;
2928     		}
2929     	}
2930     
2931     	base_addr[CyIER] = ier;
2932     
2933     	restore_flags(flags);
2934     }
2935     
2936     int getDebugChar()
2937     {
2938     	volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2939     	unsigned long flags;
2940     	volatile u_char sink;
2941     	u_char ier;
2942     	int port;
2943     	int i, c;
2944     
2945     	i = debugiq.out;
2946     	if (i != debugiq.in) {
2947     		c = debugiq.buf[i];
2948     		if (++i == DEBUG_LEN)
2949     			i = 0;
2950     		debugiq.out = i;
2951     		return c;
2952     	}
2953     	/* OK, nothing in queue, wait in poll loop */
2954     
2955     	save_flags(flags); cli();
2956     
2957     	/* Ensure receiver is enabled! */
2958     
2959     	port = DEBUG_PORT;
2960     	base_addr[CyCAR] = (u_char)port;
2961     #if 0
2962     	while (base_addr[CyCCR])
2963     		;
2964     	base_addr[CyCCR] = CyENB_RCVR;
2965     #endif
2966     	ier = base_addr[CyIER];
2967     	base_addr[CyIER] = CyRxData;
2968     
2969     	while (1) {
2970     		if (pcc2chip[PccSCCRICR] & 0x20)
2971     		{
2972     			/* We have a Rx int. Acknowledge it */
2973     			sink = pcc2chip[PccRPIACKR];
2974     			if ((base_addr[CyLICR] >> 2) == port) {
2975     				int cnt = base_addr[CyRFOC];
2976     				while (cnt-- > 0)
2977     				{
2978     					c = base_addr[CyRDR];
2979     					if (c == 0)
2980     						printk ("!! debug char is null (cnt=%d) !!", cnt);
2981     					else
2982     						queueDebugChar (c);
2983     				}
2984     				base_addr[CyREOIR] = 0;
2985     				i = debugiq.out;
2986     				if (i == debugiq.in)
2987     					panic ("Debug input queue empty!");
2988     				c = debugiq.buf[i];
2989     				if (++i == DEBUG_LEN)
2990     					i = 0;
2991     				debugiq.out = i;
2992     				break;
2993     			}
2994     			else
2995     				base_addr[CyREOIR] = CyNOTRANS;
2996     		}
2997     	}
2998     
2999     	base_addr[CyIER] = ier;
3000     
3001     	restore_flags(flags);
3002     
3003     	return (c);
3004     }
3005     
3006     void queueDebugChar (int c)
3007     {
3008     	int i;
3009     
3010     	i = debugiq.in;
3011     	debugiq.buf[i] = c;
3012     	if (++i == DEBUG_LEN)
3013     		i = 0;
3014     	if (i != debugiq.out)
3015     		debugiq.in = i;
3016     }
3017     
3018     static void
3019     debug_setup()
3020     {
3021       unsigned long flags;
3022       volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
3023       int   i, cflag;
3024     
3025         cflag = B19200;
3026     
3027         save_flags(flags); cli();
3028     
3029         for (i = 0; i < 4; i++)
3030         {
3031     	base_addr[CyCAR] = i;
3032     	base_addr[CyLICR] = i << 2;
3033         }
3034     
3035         debugiq.in = debugiq.out = 0;
3036     
3037         base_addr[CyCAR] = DEBUG_PORT;
3038     
3039         /* baud rate */
3040         i = cflag & CBAUD;
3041     
3042         base_addr[CyIER] = 0;
3043     
3044         base_addr[CyCMR] = CyASYNC;
3045         base_addr[CyLICR] = DEBUG_PORT << 2;
3046         base_addr[CyLIVR] = 0x5c;
3047     
3048         /* tx and rx baud rate */
3049     
3050         base_addr[CyTCOR] = baud_co[i];
3051         base_addr[CyTBPR] = baud_bpr[i];
3052         base_addr[CyRCOR] = baud_co[i] >> 5;
3053         base_addr[CyRBPR] = baud_bpr[i];
3054     
3055         /* set line characteristics  according configuration */
3056     
3057         base_addr[CySCHR1] = 0;
3058         base_addr[CySCHR2] = 0;
3059         base_addr[CySCRL] = 0;
3060         base_addr[CySCRH] = 0;
3061         base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
3062         base_addr[CyCOR2] = 0;
3063         base_addr[CyCOR3] = Cy_1_STOP;
3064         base_addr[CyCOR4] = baud_cor4[i];
3065         base_addr[CyCOR5] = 0;
3066         base_addr[CyCOR6] = 0;
3067         base_addr[CyCOR7] = 0;
3068     
3069         write_cy_cmd(base_addr,CyINIT_CHAN);
3070         write_cy_cmd(base_addr,CyENB_RCVR);
3071     
3072         base_addr[CyCAR] = DEBUG_PORT; /* !!! Is this needed? */
3073     
3074         base_addr[CyRTPRL] = 2;
3075         base_addr[CyRTPRH] = 0;
3076     
3077         base_addr[CyMSVR1] = CyRTS;
3078         base_addr[CyMSVR2] = CyDTR;
3079     
3080         base_addr[CyIER] = CyRxData;
3081     
3082         restore_flags(flags);
3083     
3084     } /* debug_setup */
3085     
3086     #endif
3087     
3088     MODULE_LICENSE("GPL");
3089