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

1     /*****************************************************************************/
2     /*
3      *          mxser.c  -- MOXA Smartio family multiport serial driver.
4      *
5      *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com.tw).
6      *
7      *      This code is loosely based on the Linux serial driver, written by
8      *      Linus Torvalds, Theodore T'so and others.
9      *
10      *      This program is free software; you can redistribute it and/or modify
11      *      it under the terms of the GNU General Public License as published by
12      *      the Free Software Foundation; either version 2 of the License, or
13      *      (at your option) any later version.
14      *
15      *      This program is distributed in the hope that it will be useful,
16      *      but WITHOUT ANY WARRANTY; without even the implied warranty of
17      *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18      *      GNU General Public License for more details.
19      *
20      *      You should have received a copy of the GNU General Public License
21      *      along with this program; if not, write to the Free Software
22      *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23      */
24     
25     /*
26      *    MOXA Smartio Family Serial Driver
27      *
28      *      Copyright (C) 1999,2000  Moxa Technologies Co., LTD.
29      *
30      *      for             : LINUX 2.0.X, 2.2.X, 2.4.X
31      *      date            : 2001/05/01
32      *      version         : 1.2 
33      *      
34      *    Fixes for C104H/PCI by Tim Hockin <thockin@sun.com>
35      */
36     
37     #include <linux/config.h>
38     #include <linux/module.h>
39     #include <linux/version.h>
40     #include <linux/errno.h>
41     #include <linux/signal.h>
42     #include <linux/sched.h>
43     #include <linux/timer.h>
44     #include <linux/interrupt.h>
45     #include <linux/tty.h>
46     #include <linux/tty_flip.h>
47     #include <linux/serial.h>
48     #include <linux/serial_reg.h>
49     #include <linux/major.h>
50     #include <linux/string.h>
51     #include <linux/fcntl.h>
52     #include <linux/ptrace.h>
53     #include <linux/ioport.h>
54     #include <linux/mm.h>
55     #include <linux/smp_lock.h>
56     #include <linux/pci.h>
57     
58     #include <asm/system.h>
59     #include <asm/io.h>
60     #include <asm/irq.h>
61     #include <asm/segment.h>
62     #include <asm/bitops.h>
63     #include <asm/uaccess.h>
64     
65     #define		MXSER_VERSION			"1.2"
66     
67     #define		MXSERMAJOR	 	174
68     #define		MXSERCUMAJOR		175
69     
70     
71     #define	MXSER_EVENT_TXLOW	 1
72     #define	MXSER_EVENT_HANGUP	 2
73     
74     
75     #define 	SERIAL_DO_RESTART
76     
77     #define 	MXSER_BOARDS		4	/* Max. boards */
78     #define 	MXSER_PORTS		32	/* Max. ports */
79     #define 	MXSER_PORTS_PER_BOARD	8	/* Max. ports per board */
80     #define 	MXSER_ISR_PASS_LIMIT	256
81     
82     #define		MXSER_ERR_IOADDR	-1
83     #define		MXSER_ERR_IRQ		-2
84     #define		MXSER_ERR_IRQ_CONFLIT	-3
85     #define		MXSER_ERR_VECTOR	-4
86     
87     #define 	SERIAL_TYPE_NORMAL	1
88     #define 	SERIAL_TYPE_CALLOUT	2
89     
90     #define 	WAKEUP_CHARS		256
91     
92     #define 	UART_MCR_AFE		0x20
93     #define 	UART_LSR_SPECIAL	0x1E
94     
95     #define PORTNO(x)		(MINOR((x)->device) - (x)->driver.minor_start)
96     
97     #define RELEVANT_IFLAG(iflag)	(iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
98     
99     #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
100     
101     #ifndef MIN
102     #define MIN(a,b)	((a) < (b) ? (a) : (b))
103     #endif
104     
105     /*
106      *    Define the Moxa PCI vendor and device IDs.
107      */
108     
109     #ifndef	PCI_VENDOR_ID_MOXA
110     #define	PCI_VENDOR_ID_MOXA	0x1393
111     #endif
112     #ifndef PCI_DEVICE_ID_C168
113     #define PCI_DEVICE_ID_C168	0x1680
114     #endif
115     #ifndef PCI_DEVICE_ID_C104
116     #define PCI_DEVICE_ID_C104	0x1040
117     #endif
118     
119     #define C168_ASIC_ID    1
120     #define C104_ASIC_ID    2
121     #define CI104J_ASIC_ID  5
122     
123     enum {
124     	MXSER_BOARD_C168_ISA = 0,
125     	MXSER_BOARD_C104_ISA,
126     	MXSER_BOARD_CI104J,
127     	MXSER_BOARD_C168_PCI,
128     	MXSER_BOARD_C104_PCI,
129     };
130     
131     static char *mxser_brdname[] =
132     {
133     	"C168 series",
134     	"C104 series",
135     	"CI-104J series",
136     	"C168H/PCI series",
137     	"C104H/PCI series",
138     };
139     
140     static int mxser_numports[] =
141     {
142     	8,
143     	4,
144     	4,
145     	8,
146     	4,
147     };
148     
149     /*
150      *    MOXA ioctls
151      */
152     #define 	MOXA		0x400
153     #define 	MOXA_GETDATACOUNT     (MOXA + 23)
154     #define		MOXA_GET_CONF         (MOXA + 35)
155     #define 	MOXA_DIAGNOSE         (MOXA + 50)
156     #define 	MOXA_CHKPORTENABLE    (MOXA + 60)
157     #define 	MOXA_HighSpeedOn      (MOXA + 61)
158     #define         MOXA_GET_MAJOR        (MOXA + 63)
159     #define         MOXA_GET_CUMAJOR      (MOXA + 64)
160     #define         MOXA_GETMSTATUS       (MOXA + 65)
161     
162     typedef struct {
163     	unsigned short vendor_id;
164     	unsigned short device_id;
165     	unsigned short board_type;
166     } mxser_pciinfo;
167     
168     static mxser_pciinfo mxser_pcibrds[] =
169     {
170     	{PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, MXSER_BOARD_C168_PCI},
171     	{PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, MXSER_BOARD_C104_PCI},
172     };
173     
174     static int ioaddr[MXSER_BOARDS];
175     static int ttymajor = MXSERMAJOR;
176     static int calloutmajor = MXSERCUMAJOR;
177     static int verbose;
178     
179     /* Variables for insmod */
180     
181     MODULE_AUTHOR("William Chen");
182     MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
183     MODULE_LICENSE("GPL");
184     MODULE_PARM(ioaddr, "1-4i");
185     MODULE_PARM(ttymajor, "i");
186     MODULE_PARM(calloutmajor, "i");
187     MODULE_PARM(verbose, "i");
188     
189     EXPORT_NO_SYMBOLS;
190     
191     struct mxser_hwconf {
192     	int board_type;
193     	int ports;
194     	int irq;
195     	int vector;
196     	int vector_mask;
197     	int uart_type;
198     	int ioaddr[MXSER_PORTS_PER_BOARD];
199     	int baud_base[MXSER_PORTS_PER_BOARD];
200     	struct pci_dev *pdev;
201     };
202     
203     struct mxser_struct {
204     	int port;
205     	int base;		/* port base address */
206     	int irq;		/* port using irq no. */
207     	int vector;		/* port irq vector */
208     	int vectormask;		/* port vector mask */
209     	int rx_trigger;		/* Rx fifo trigger level */
210     	int baud_base;		/* max. speed */
211     	int flags;		/* defined in tty.h */
212     	int type;		/* UART type */
213     	struct tty_struct *tty;
214     	int read_status_mask;
215     	int ignore_status_mask;
216     	int xmit_fifo_size;
217     	int custom_divisor;
218     	int x_char;		/* xon/xoff character */
219     	int close_delay;
220     	unsigned short closing_wait;
221     	int IER;		/* Interrupt Enable Register */
222     	int MCR;		/* Modem control register */
223     	unsigned long event;
224     	int count;		/* # of fd on device */
225     	int blocked_open;	/* # of blocked opens */
226     	long session;		/* Session of opening process */
227     	long pgrp;		/* pgrp of opening process */
228     	unsigned char *xmit_buf;
229     	int xmit_head;
230     	int xmit_tail;
231     	int xmit_cnt;
232     	struct tq_struct tqueue;
233     	struct termios normal_termios;
234     	struct termios callout_termios;
235     	wait_queue_head_t open_wait;
236     	wait_queue_head_t close_wait;
237     	wait_queue_head_t delta_msr_wait;
238     	struct async_icount icount;	/* kernel counters for the 4 input interrupts */
239     };
240     
241     struct mxser_log {
242     	int tick;
243     	int rxcnt[MXSER_PORTS];
244     	int txcnt[MXSER_PORTS];
245     };
246     
247     struct mxser_mstatus {
248     	tcflag_t cflag;
249     	int cts;
250     	int dsr;
251     	int ri;
252     	int dcd;
253     };
254     
255     static struct mxser_mstatus GMStatus[MXSER_PORTS];
256     
257     static int mxserBoardCAP[MXSER_BOARDS] =
258     {
259     	0, 0, 0, 0
260            /*  0x180, 0x280, 0x200, 0x320   */
261     };
262     
263     
264     static struct tty_driver mxvar_sdriver, mxvar_cdriver;
265     static int mxvar_refcount;
266     static struct mxser_struct mxvar_table[MXSER_PORTS];
267     static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
268     static struct termios *mxvar_termios[MXSER_PORTS + 1];
269     static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
270     static struct mxser_log mxvar_log;
271     static int mxvar_diagflag;
272     /*
273      * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
274      * to lock it in case the memcpy_fromfs blocks while swapping in a page,
275      * and some other program tries to do a serial write at the same time.
276      * Since the lock will only come under contention when the system is
277      * swapping and available memory is low, it makes sense to share one
278      * buffer across all the serial ports, since it significantly saves
279      * memory if large numbers of serial ports are open.
280      */
281     static unsigned char *mxvar_tmp_buf;
282     static struct semaphore mxvar_tmp_buf_sem;
283     
284     /*
285      * This is used to figure out the divisor speeds and the timeouts
286      */
287     static int mxvar_baud_table[] =
288     {
289     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
290     	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
291     
292     struct mxser_hwconf mxsercfg[MXSER_BOARDS];
293     
294     /*
295      * static functions:
296      */
297     
298     #ifdef MODULE
299     int init_module(void);
300     void cleanup_module(void);
301     #endif
302     
303     static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
304     int mxser_init(void);
305     static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
306     static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
307     static void mxser_do_softint(void *);
308     static int mxser_open(struct tty_struct *, struct file *);
309     static void mxser_close(struct tty_struct *, struct file *);
310     static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
311     static int mxser_write_room(struct tty_struct *);
312     static void mxser_flush_buffer(struct tty_struct *);
313     static int mxser_chars_in_buffer(struct tty_struct *);
314     static void mxser_flush_chars(struct tty_struct *);
315     static void mxser_put_char(struct tty_struct *, unsigned char);
316     static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
317     static int mxser_ioctl_special(unsigned int, unsigned long);
318     static void mxser_throttle(struct tty_struct *);
319     static void mxser_unthrottle(struct tty_struct *);
320     static void mxser_set_termios(struct tty_struct *, struct termios *);
321     static void mxser_stop(struct tty_struct *);
322     static void mxser_start(struct tty_struct *);
323     static void mxser_hangup(struct tty_struct *);
324     static void mxser_interrupt(int, void *, struct pt_regs *);
325     static inline void mxser_receive_chars(struct mxser_struct *, int *);
326     static inline void mxser_transmit_chars(struct mxser_struct *);
327     static inline void mxser_check_modem_status(struct mxser_struct *, int);
328     static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
329     static int mxser_startup(struct mxser_struct *);
330     static void mxser_shutdown(struct mxser_struct *);
331     static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
332     static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
333     static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
334     static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
335     static void mxser_send_break(struct mxser_struct *, int);
336     static int mxser_get_modem_info(struct mxser_struct *, unsigned int *);
337     static int mxser_set_modem_info(struct mxser_struct *, unsigned int, unsigned int *);
338     
339     /*
340      * The MOXA C168/C104 serial driver boot-time initialization code!
341      */
342     
343     
344     #ifdef MODULE
345     int init_module(void)
346     {
347     	int ret;
348     
349     	if (verbose)
350     		printk("Loading module mxser ...\n");
351     	ret = mxser_init();
352     	if (verbose)
353     		printk("Done.\n");
354     	return (ret);
355     }
356     
357     void cleanup_module(void)
358     {
359     	int i, err = 0;
360     
361     
362     	if (verbose)
363     		printk("Unloading module mxser ...\n");
364     	if ((err |= tty_unregister_driver(&mxvar_cdriver)))
365     		printk("Couldn't unregister MOXA Smartio family callout driver\n");
366     	if ((err |= tty_unregister_driver(&mxvar_sdriver)))
367     		printk("Couldn't unregister MOXA Smartio family serial driver\n");
368     
369     	for (i = 0; i < MXSER_BOARDS; i++) {
370     		if (mxsercfg[i].board_type == -1)
371     			continue;
372     		else {
373     			free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
374     		}
375     	}
376     
377     	if (verbose)
378     		printk("Done.\n");
379     
380     }
381     #endif
382     
383     
384     int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
385     {
386     	struct mxser_struct *info;
387     	unsigned long flags;
388     	int retval;
389     	int i, n;
390     
391     	init_MUTEX(&mxvar_tmp_buf_sem);
392     	
393     	n = board * MXSER_PORTS_PER_BOARD;
394     	info = &mxvar_table[n];
395     	for (i = 0; i < hwconf->ports; i++, n++, info++) {
396     		if (verbose) {
397     			printk("        ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
398     			if (hwconf->baud_base[i] == 115200)
399     				printk(" max. baud rate up to 115200 bps.\n");
400     			else
401     				printk(" max. baud rate up to 921600 bps.\n");
402     		}
403     		info->port = n;
404     		info->base = hwconf->ioaddr[i];
405     		info->irq = hwconf->irq;
406     		info->vector = hwconf->vector;
407     		info->vectormask = hwconf->vector_mask;
408     		info->rx_trigger = 14;
409     		info->baud_base = hwconf->baud_base[i];
410     		info->flags = ASYNC_SHARE_IRQ;
411     		info->type = hwconf->uart_type;
412     		if ((info->type == PORT_16450) || (info->type == PORT_8250))
413     			info->xmit_fifo_size = 1;
414     		else
415     			info->xmit_fifo_size = 16;
416     		info->custom_divisor = hwconf->baud_base[i] * 16;
417     		info->close_delay = 5 * HZ / 10;
418     		info->closing_wait = 30 * HZ;
419     		info->tqueue.routine = mxser_do_softint;
420     		info->tqueue.data = info;
421     		info->callout_termios = mxvar_cdriver.init_termios;
422     		info->normal_termios = mxvar_sdriver.init_termios;
423     		init_waitqueue_head(&info->open_wait);
424     		init_waitqueue_head(&info->close_wait);
425     		init_waitqueue_head(&info->delta_msr_wait);
426     	}
427     
428     	/*
429     	 * Allocate the IRQ if necessary
430     	 */
431     	save_flags(flags);
432     
433     	n = board * MXSER_PORTS_PER_BOARD;
434     	info = &mxvar_table[n];
435     
436     	cli();
437     	retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
438     			     "mxser", info);
439     	if (retval) {
440     		restore_flags(flags);
441     		printk("Board %d: %s", board, mxser_brdname[hwconf->board_type]);
442     		printk("  Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
443     		return (retval);
444     	}
445     	restore_flags(flags);
446     
447     	return 0;
448     }
449     
450     
451     static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
452     {
453     	mxsercfg[board] = *hwconf;
454     }
455     
456     static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
457     {
458     	int i;
459     	unsigned int ioaddress;
460     
461     	hwconf->board_type = board_type;
462     	hwconf->ports = mxser_numports[board_type];
463     	ioaddress = pci_resource_start (pdev, 2);
464     	for (i = 0; i < hwconf->ports; i++)
465     		hwconf->ioaddr[i] = ioaddress + 8 * i;
466     
467     	ioaddress = pci_resource_start (pdev, 3);
468     	hwconf->vector = ioaddress;
469     
470     	hwconf->irq = pdev->irq;
471     
472     	hwconf->uart_type = PORT_16550A;
473     	hwconf->vector_mask = 0;
474     	for (i = 0; i < hwconf->ports; i++) {
475     		hwconf->vector_mask |= (1 << i);
476     		hwconf->baud_base[i] = 921600;
477     	}
478     	return (0);
479     }
480     
481     int mxser_init(void)
482     {
483     	int i, m, retval, b;
484     	int n, index;
485     	int ret1, ret2;
486     	struct mxser_hwconf hwconf;
487     
488     	printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
489     
490     	/* Initialize the tty_driver structure */
491     
492     	memset(&mxvar_sdriver, 0, sizeof(struct tty_driver));
493     	mxvar_sdriver.magic = TTY_DRIVER_MAGIC;
494     	mxvar_sdriver.name = "ttyM";
495     	mxvar_sdriver.major = ttymajor;
496     	mxvar_sdriver.minor_start = 0;
497     	mxvar_sdriver.num = MXSER_PORTS + 1;
498     	mxvar_sdriver.type = TTY_DRIVER_TYPE_SERIAL;
499     	mxvar_sdriver.subtype = SERIAL_TYPE_NORMAL;
500     	mxvar_sdriver.init_termios = tty_std_termios;
501     	mxvar_sdriver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
502     	mxvar_sdriver.flags = TTY_DRIVER_REAL_RAW;
503     	mxvar_sdriver.refcount = &mxvar_refcount;
504     	mxvar_sdriver.table = mxvar_tty;
505     	mxvar_sdriver.termios = mxvar_termios;
506     	mxvar_sdriver.termios_locked = mxvar_termios_locked;
507     
508     	mxvar_sdriver.open = mxser_open;
509     	mxvar_sdriver.close = mxser_close;
510     	mxvar_sdriver.write = mxser_write;
511     	mxvar_sdriver.put_char = mxser_put_char;
512     	mxvar_sdriver.flush_chars = mxser_flush_chars;
513     	mxvar_sdriver.write_room = mxser_write_room;
514     	mxvar_sdriver.chars_in_buffer = mxser_chars_in_buffer;
515     	mxvar_sdriver.flush_buffer = mxser_flush_buffer;
516     	mxvar_sdriver.ioctl = mxser_ioctl;
517     	mxvar_sdriver.throttle = mxser_throttle;
518     	mxvar_sdriver.unthrottle = mxser_unthrottle;
519     	mxvar_sdriver.set_termios = mxser_set_termios;
520     	mxvar_sdriver.stop = mxser_stop;
521     	mxvar_sdriver.start = mxser_start;
522     	mxvar_sdriver.hangup = mxser_hangup;
523     
524     	/*
525     	 * The callout device is just like normal device except for
526     	 * major number and the subtype code.
527     	 */
528     	mxvar_cdriver = mxvar_sdriver;
529     	mxvar_cdriver.name = "cum";
530     	mxvar_cdriver.major = calloutmajor;
531     	mxvar_cdriver.subtype = SERIAL_TYPE_CALLOUT;
532     
533     	printk("Tty devices major number = %d, callout devices major number = %d\n", ttymajor, calloutmajor);
534     
535     	mxvar_diagflag = 0;
536     	memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
537     	memset(&mxvar_log, 0, sizeof(struct mxser_log));
538     
539     
540     	m = 0;
541     	/* Start finding ISA boards here */
542     	for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
543     		int cap;
544     		if (!(cap = mxserBoardCAP[b]))
545     			continue;
546     
547     		retval = mxser_get_ISA_conf(cap, &hwconf);
548     
549     		if (retval != 0)
550     			printk("Found MOXA %s board (CAP=0x%x)\n",
551     			       mxser_brdname[hwconf.board_type],
552     			       ioaddr[b]);
553     
554     		if (retval <= 0) {
555     			if (retval == MXSER_ERR_IRQ)
556     				printk("Invalid interrupt number,board not configured\n");
557     			else if (retval == MXSER_ERR_IRQ_CONFLIT)
558     				printk("Invalid interrupt number,board not configured\n");
559     			else if (retval == MXSER_ERR_VECTOR)
560     				printk("Invalid interrupt vector,board not configured\n");
561     			else if (retval == MXSER_ERR_IOADDR)
562     				printk("Invalid I/O address,board not configured\n");
563     
564     			continue;
565     		}
566     		hwconf.pdev = NULL;
567     
568     		if (mxser_initbrd(m, &hwconf) < 0)
569     			continue;
570     
571     		mxser_getcfg(m, &hwconf);
572     
573     		m++;
574     	}
575     
576     	/* Start finding ISA boards from module arg */
577     	for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
578     		int cap;
579     		if (!(cap = ioaddr[b]))
580     			continue;
581     
582     		retval = mxser_get_ISA_conf(cap, &hwconf);
583     
584     		if (retval != 0)
585     			printk("Found MOXA %s board (CAP=0x%x)\n",
586     			       mxser_brdname[hwconf.board_type],
587     			       ioaddr[b]);
588     
589     		if (retval <= 0) {
590     			if (retval == MXSER_ERR_IRQ)
591     				printk("Invalid interrupt number,board not configured\n");
592     			else if (retval == MXSER_ERR_IRQ_CONFLIT)
593     				printk("Invalid interrupt number,board not configured\n");
594     			else if (retval == MXSER_ERR_VECTOR)
595     				printk("Invalid interrupt vector,board not configured\n");
596     			else if (retval == MXSER_ERR_IOADDR)
597     				printk("Invalid I/O address,board not configured\n");
598     
599     			continue;
600     		}
601     		hwconf.pdev = NULL;
602     
603     		if (mxser_initbrd(m, &hwconf) < 0)
604     			continue;
605     
606     		mxser_getcfg(m, &hwconf);
607     
608     		m++;
609     	}
610     
611     	/* start finding PCI board here */
612     
613     #ifdef CONFIG_PCI
614     	{
615     		struct pci_dev *pdev = NULL;
616     
617     		n = sizeof(mxser_pcibrds) / sizeof(mxser_pciinfo);
618     		index = 0;
619     		for (b = 0; b < n; b++) {
620     			pdev = pci_find_device(mxser_pcibrds[b].vendor_id,
621     					       mxser_pcibrds[b].device_id, pdev);
622     			if (!pdev || pci_enable_device(pdev))
623     				continue;
624     			hwconf.pdev = pdev;
625     			printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
626     				mxser_brdname[mxser_pcibrds[b].board_type],
627     				pdev->bus->number, PCI_SLOT(pdev->devfn));
628     			if (m >= MXSER_BOARDS) {
629     				printk("Too many Smartio family boards found (maximum %d),board not configured\n", MXSER_BOARDS);
630     			} else {
631     				retval = mxser_get_PCI_conf(pdev,
632     				   mxser_pcibrds[b].board_type, &hwconf);
633     				if (retval < 0) {
634     					if (retval == MXSER_ERR_IRQ)
635     						printk("Invalid interrupt number,board not configured\n");
636     					else if (retval == MXSER_ERR_IRQ_CONFLIT)
637     						printk("Invalid interrupt number,board not configured\n");
638     					else if (retval == MXSER_ERR_VECTOR)
639     						printk("Invalid interrupt vector,board not configured\n");
640     					else if (retval == MXSER_ERR_IOADDR)
641     						printk("Invalid I/O address,board not configured\n");
642     					continue;
643     
644     				}
645     				if (mxser_initbrd(m, &hwconf) < 0)
646     					continue;
647     				mxser_getcfg(m, &hwconf);
648     				m++;
649     
650     			}
651     
652     		}
653     	}
654     #endif
655     
656     	for (i = m; i < MXSER_BOARDS; i++) {
657     		mxsercfg[i].board_type = -1;
658     	}
659     
660     
661     	ret1 = 0;
662     	ret2 = 0;
663     	if (!(ret1 = tty_register_driver(&mxvar_sdriver))) {
664     		if (!(ret2 = tty_register_driver(&mxvar_cdriver))) {
665     			return 0;
666     		} else {
667     			tty_unregister_driver(&mxvar_sdriver);
668     			printk("Couldn't install MOXA Smartio family callout driver !\n");
669     		}
670     	} else
671     		printk("Couldn't install MOXA Smartio family driver !\n");
672     
673     
674     	if (ret1 || ret2) {
675     		for (i = 0; i < MXSER_BOARDS; i++) {
676     			if (mxsercfg[i].board_type == -1)
677     				continue;
678     			else {
679     				free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
680     			}
681     		}
682     		return -1;
683     	}
684     	return (0);
685     }
686     
687     static void mxser_do_softint(void *private_)
688     {
689     	struct mxser_struct *info = (struct mxser_struct *) private_;
690     	struct tty_struct *tty;
691     
692     	tty = info->tty;
693     	if (tty) {
694     		if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
695     			if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
696     			    tty->ldisc.write_wakeup)
697     				(tty->ldisc.write_wakeup) (tty);
698     			wake_up_interruptible(&tty->write_wait);
699     		}
700     		if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
701     			tty_hangup(tty);	/* FIXME: module removal race here - AKPM */
702     		}
703     	}
704     	MOD_DEC_USE_COUNT;
705     }
706     
707     /*
708      * This routine is called whenever a serial port is opened.  It
709      * enables interrupts for a serial port, linking in its async structure into
710      * the IRQ chain.   It also performs the serial-specific
711      * initialization for the tty structure.
712      */
713     
714     static int mxser_open(struct tty_struct *tty, struct file *filp)
715     {
716     	struct mxser_struct *info;
717     	int retval, line;
718     	unsigned long page;
719     
720     	line = PORTNO(tty);
721     	if (line == MXSER_PORTS)
722     		return (0);
723     	if ((line < 0) || (line > MXSER_PORTS))
724     		return (-ENODEV);
725     	info = mxvar_table + line;
726     	if (!info->base)
727     		return (-ENODEV);
728     
729     	info->count++;
730     	tty->driver_data = info;
731     	info->tty = tty;
732     
733     	if (!mxvar_tmp_buf) {
734     		page = get_free_page(GFP_KERNEL);
735     		if (!page)
736     			return (-ENOMEM);
737     		if (mxvar_tmp_buf)
738     			free_page(page);
739     		else
740     			mxvar_tmp_buf = (unsigned char *) page;
741     	}
742     	/*
743     	 * Start up serial port
744     	 */
745     	retval = mxser_startup(info);
746     	if (retval)
747     		return (retval);
748     
749     	retval = mxser_block_til_ready(tty, filp, info);
750     	if (retval)
751     		return (retval);
752     
753     	if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
754     		if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
755     			*tty->termios = info->normal_termios;
756     		else
757     			*tty->termios = info->callout_termios;
758     		mxser_change_speed(info, 0);
759     	}
760     	info->session = current->session;
761     	info->pgrp = current->pgrp;
762     
763     	MOD_INC_USE_COUNT;
764     
765     	return (0);
766     }
767     
768     /*
769      * This routine is called when the serial port gets closed.  First, we
770      * wait for the last remaining data to be sent.  Then, we unlink its
771      * async structure from the interrupt chain if necessary, and we free
772      * that IRQ if nothing is left in the chain.
773      */
774     
775     static void mxser_close(struct tty_struct *tty, struct file *filp)
776     {
777     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
778     	unsigned long flags;
779     	unsigned long timeout;
780     
781     	if (PORTNO(tty) == MXSER_PORTS)
782     		return;
783     	if (!info)
784     		return;
785     
786     	save_flags(flags);
787     	cli();
788     
789     	if (tty_hung_up_p(filp)) {
790     		restore_flags(flags);
791     		MOD_DEC_USE_COUNT;
792     		return;
793     	}
794     	if ((tty->count == 1) && (info->count != 1)) {
795     		/*
796     		 * Uh, oh.        tty->count is 1, which means that the tty
797     		 * structure will be freed.  Info->count should always
798     		 * be one in these conditions.  If it's greater than
799     		 * one, we've got real problems, since it means the
800     		 * serial port won't be shutdown.
801     		 */
802     		printk("mxser_close: bad serial port count; tty->count is 1, "
803     		       "info->count is %d\n", info->count);
804     		info->count = 1;
805     	}
806     	if (--info->count < 0) {
807     		printk("mxser_close: bad serial port count for ttys%d: %d\n",
808     		       info->port, info->count);
809     		info->count = 0;
810     	}
811     	if (info->count) {
812     		restore_flags(flags);
813     		MOD_DEC_USE_COUNT;
814     		return;
815     	}
816     	info->flags |= ASYNC_CLOSING;
817     	/*
818     	 * Save the termios structure, since this port may have
819     	 * separate termios for callout and dialin.
820     	 */
821     	if (info->flags & ASYNC_NORMAL_ACTIVE)
822     		info->normal_termios = *tty->termios;
823     	if (info->flags & ASYNC_CALLOUT_ACTIVE)
824     		info->callout_termios = *tty->termios;
825     	/*
826     	 * Now we wait for the transmit buffer to clear; and we notify
827     	 * the line discipline to only process XON/XOFF characters.
828     	 */
829     	tty->closing = 1;
830     	if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
831     		tty_wait_until_sent(tty, info->closing_wait);
832     	/*
833     	 * At this point we stop accepting input.  To do this, we
834     	 * disable the receive line status interrupts, and tell the
835     	 * interrupt driver to stop checking the data ready bit in the
836     	 * line status register.
837     	 */
838     	info->IER &= ~UART_IER_RLSI;
839     	/* by William
840     	   info->read_status_mask &= ~UART_LSR_DR;
841     	 */
842     	if (info->flags & ASYNC_INITIALIZED) {
843     		outb(info->IER, info->base + UART_IER);
844     		/*
845     		 * Before we drop DTR, make sure the UART transmitter
846     		 * has completely drained; this is especially
847     		 * important if there is a transmit FIFO!
848     		 */
849     		timeout = jiffies + HZ;
850     		while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
851     			current->state = TASK_INTERRUPTIBLE;
852     			schedule_timeout(5);
853     			if (jiffies > timeout)
854     				break;
855     		}
856     	}
857     	mxser_shutdown(info);
858     	if (tty->driver.flush_buffer)
859     		tty->driver.flush_buffer(tty);
860     	if (tty->ldisc.flush_buffer)
861     		tty->ldisc.flush_buffer(tty);
862     	tty->closing = 0;
863     	info->event = 0;
864     	info->tty = 0;
865     	if (info->blocked_open) {
866     		if (info->close_delay) {
867     			current->state = TASK_INTERRUPTIBLE;
868     			schedule_timeout(info->close_delay);
869     		}
870     		wake_up_interruptible(&info->open_wait);
871     	}
872     	info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
873     			 ASYNC_CLOSING);
874     	wake_up_interruptible(&info->close_wait);
875     	restore_flags(flags);
876     
877     	MOD_DEC_USE_COUNT;
878     }
879     
880     static int mxser_write(struct tty_struct *tty, int from_user,
881     		       const unsigned char *buf, int count)
882     {
883     	int c, total = 0;
884     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
885     	unsigned long flags;
886     
887     	if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
888     		return (0);
889     
890     	save_flags(flags);
891     	if (from_user) {
892     		down(&mxvar_tmp_buf_sem);
893     		while (1) {
894     			c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
895     					   SERIAL_XMIT_SIZE - info->xmit_head));
896     			if (c <= 0)
897     				break;
898     
899     			c -= copy_from_user(mxvar_tmp_buf, buf, c);
900     			if (!c) {
901     				if (!total)
902     					total = -EFAULT;
903     				break;
904     			}
905     
906     			cli();
907     			c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
908     				       SERIAL_XMIT_SIZE - info->xmit_head));
909     			memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
910     			info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
911     			info->xmit_cnt += c;
912     			restore_flags(flags);
913     
914     			buf += c;
915     			count -= c;
916     			total += c;
917     		}
918     		up(&mxvar_tmp_buf_sem);
919     	} else {
920     		while (1) {
921     			cli();
922     			c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
923     					   SERIAL_XMIT_SIZE - info->xmit_head));
924     			if (c <= 0) {
925     				restore_flags(flags);
926     				break;
927     			}
928     
929     			memcpy(info->xmit_buf + info->xmit_head, buf, c);
930     			info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
931     			info->xmit_cnt += c;
932     			restore_flags(flags);
933     
934     			buf += c;
935     			count -= c;
936     			total += c;
937     		}
938     	}
939     
940     	cli();
941     	if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
942     	    !(info->IER & UART_IER_THRI)) {
943     		info->IER |= UART_IER_THRI;
944     		outb(info->IER, info->base + UART_IER);
945     	}
946     	restore_flags(flags);
947     	return (total);
948     }
949     
950     static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
951     {
952     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
953     	unsigned long flags;
954     
955     	if (!tty || !info->xmit_buf)
956     		return;
957     
958     	save_flags(flags);
959     	cli();
960     	if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
961     		restore_flags(flags);
962     		return;
963     	}
964     	info->xmit_buf[info->xmit_head++] = ch;
965     	info->xmit_head &= SERIAL_XMIT_SIZE - 1;
966     	info->xmit_cnt++;
967     	/********************************************** why ??? ***********
968     	if ( !tty->stopped && !tty->hw_stopped &&
969     	     !(info->IER & UART_IER_THRI) ) {
970     	    info->IER |= UART_IER_THRI;
971     	    outb(info->IER, info->base + UART_IER);
972     	}
973     	*****************************************************************/
974     	restore_flags(flags);
975     }
976     
977     static void mxser_flush_chars(struct tty_struct *tty)
978     {
979     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
980     	unsigned long flags;
981     
982     	if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
983     	    !info->xmit_buf)
984     		return;
985     
986     	save_flags(flags);
987     	cli();
988     	info->IER |= UART_IER_THRI;
989     	outb(info->IER, info->base + UART_IER);
990     	restore_flags(flags);
991     }
992     
993     static int mxser_write_room(struct tty_struct *tty)
994     {
995     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
996     	int ret;
997     
998     	ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
999     	if (ret < 0)
1000     		ret = 0;
1001     	return (ret);
1002     }
1003     
1004     static int mxser_chars_in_buffer(struct tty_struct *tty)
1005     {
1006     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1007     
1008     	return (info->xmit_cnt);
1009     }
1010     
1011     static void mxser_flush_buffer(struct tty_struct *tty)
1012     {
1013     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1014     	unsigned long flags;
1015     
1016     	save_flags(flags);
1017     	cli();
1018     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1019     	restore_flags(flags);
1020     	wake_up_interruptible(&tty->write_wait);
1021     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1022     	    tty->ldisc.write_wakeup)
1023     		(tty->ldisc.write_wakeup) (tty);
1024     }
1025     
1026     static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1027     		       unsigned int cmd, unsigned long arg)
1028     {
1029     	unsigned long flags;
1030     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1031     	int retval;
1032     	struct async_icount cprev, cnow;	/* kernel counter temps */
1033     	struct serial_icounter_struct *p_cuser;		/* user space */
1034     	unsigned long templ;
1035     
1036     	if (PORTNO(tty) == MXSER_PORTS)
1037     		return (mxser_ioctl_special(cmd, arg));
1038     	if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
1039     	    (cmd != TIOCGICOUNT)) {
1040     		if (tty->flags & (1 << TTY_IO_ERROR))
1041     			return (-EIO);
1042     	}
1043     	switch (cmd) {
1044     	case TCSBRK:		/* SVID version: non-zero arg --> no break */
1045     		retval = tty_check_change(tty);
1046     		if (retval)
1047     			return (retval);
1048     		tty_wait_until_sent(tty, 0);
1049     		if (!arg)
1050     			mxser_send_break(info, HZ / 4);		/* 1/4 second */
1051     		return (0);
1052     	case TCSBRKP:		/* support for POSIX tcsendbreak() */
1053     		retval = tty_check_change(tty);
1054     		if (retval)
1055     			return (retval);
1056     		tty_wait_until_sent(tty, 0);
1057     		mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1058     		return (0);
1059     	case TIOCGSOFTCAR:
1060     		return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1061     	case TIOCSSOFTCAR:
1062     		if(get_user(templ, (unsigned long *) arg))
1063     			return -EFAULT;
1064     		arg = templ;
1065     		tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1066     					 (arg ? CLOCAL : 0));
1067     		return (0);
1068     	case TIOCMGET:
1069     		return (mxser_get_modem_info(info, (unsigned int *) arg));
1070     	case TIOCMBIS:
1071     	case TIOCMBIC:
1072     	case TIOCMSET:
1073     		return (mxser_set_modem_info(info, cmd, (unsigned int *) arg));
1074     	case TIOCGSERIAL:
1075     		return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1076     	case TIOCSSERIAL:
1077     		return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1078     	case TIOCSERGETLSR:	/* Get line status register */
1079     		return (mxser_get_lsr_info(info, (unsigned int *) arg));
1080     		/*
1081     		 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1082     		 * - mask passed in arg for lines of interest
1083     		 *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1084     		 * Caller should use TIOCGICOUNT to see which one it was
1085     		 */
1086     	case TIOCMIWAIT:
1087     		save_flags(flags);
1088     		cli();
1089     		cprev = info->icount;	/* note the counters on entry */
1090     		restore_flags(flags);
1091     		while (1) {
1092     			interruptible_sleep_on(&info->delta_msr_wait);
1093     			/* see if a signal did it */
1094     			if (signal_pending(current))
1095     				return (-ERESTARTSYS);
1096     			save_flags(flags);
1097     			cli();
1098     			cnow = info->icount;	/* atomic copy */
1099     			restore_flags(flags);
1100     			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1101     			  cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1102     				return (-EIO);	/* no change => error */
1103     			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1104     			((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1105     			 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1106     			((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1107     				return (0);
1108     			}
1109     			cprev = cnow;
1110     		}
1111     		/* NOTREACHED */
1112     		/*
1113     		 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1114     		 * Return: write counters to the user passed counter struct
1115     		 * NB: both 1->0 and 0->1 transitions are counted except for
1116     		 *     RI where only 0->1 is counted.
1117     		 */
1118     	case TIOCGICOUNT:
1119     		save_flags(flags);
1120     		cli();
1121     		cnow = info->icount;
1122     		restore_flags(flags);
1123     		p_cuser = (struct serial_icounter_struct *) arg;
1124     		if(put_user(cnow.cts, &p_cuser->cts))
1125     			return -EFAULT;
1126     		if(put_user(cnow.dsr, &p_cuser->dsr))
1127     			return -EFAULT;
1128     		if(put_user(cnow.rng, &p_cuser->rng))
1129     			return -EFAULT;
1130     		return put_user(cnow.dcd, &p_cuser->dcd);
1131     	case MOXA_HighSpeedOn:
1132     		return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1133     	default:
1134     		return (-ENOIOCTLCMD);
1135     	}
1136     	return (0);
1137     }
1138     
1139     static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1140     {
1141     	int i, result, status;
1142     
1143     	switch (cmd) {
1144     	case MOXA_GET_CONF:
1145     		if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1146     			     sizeof(struct mxser_hwconf) * 4))
1147     			     	return -EFAULT;
1148     		return 0;
1149     	case MOXA_GET_MAJOR:
1150     		if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1151     			return -EFAULT;
1152     		return 0;
1153     
1154     	case MOXA_GET_CUMAJOR:
1155     		if(copy_to_user((int *) arg, &calloutmajor, sizeof(int)))
1156     			return -EFAULT;
1157     		return 0;
1158     
1159     	case MOXA_CHKPORTENABLE:
1160     		result = 0;
1161     		for (i = 0; i < MXSER_PORTS; i++) {
1162     			if (mxvar_table[i].base)
1163     				result |= (1 << i);
1164     		}
1165     		return put_user(result, (unsigned long *) arg);
1166     	case MOXA_GETDATACOUNT:
1167     		if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1168     			return -EFAULT;
1169     		return (0);
1170     	case MOXA_GETMSTATUS:
1171     		for (i = 0; i < MXSER_PORTS; i++) {
1172     			GMStatus[i].ri = 0;
1173     			if (!mxvar_table[i].base) {
1174     				GMStatus[i].dcd = 0;
1175     				GMStatus[i].dsr = 0;
1176     				GMStatus[i].cts = 0;
1177     				continue;
1178     			}
1179     			if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1180     				GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1181     			else
1182     				GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1183     
1184     			status = inb(mxvar_table[i].base + UART_MSR);
1185     			if (status & 0x80 /*UART_MSR_DCD */ )
1186     				GMStatus[i].dcd = 1;
1187     			else
1188     				GMStatus[i].dcd = 0;
1189     
1190     			if (status & 0x20 /*UART_MSR_DSR */ )
1191     				GMStatus[i].dsr = 1;
1192     			else
1193     				GMStatus[i].dsr = 0;
1194     
1195     
1196     			if (status & 0x10 /*UART_MSR_CTS */ )
1197     				GMStatus[i].cts = 1;
1198     			else
1199     				GMStatus[i].cts = 0;
1200     		}
1201     		if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1202     			     sizeof(struct mxser_mstatus) * MXSER_PORTS))
1203     			return -EFAULT;
1204     		return 0;
1205     	default:
1206     		return (-ENOIOCTLCMD);
1207     	}
1208     	return (0);
1209     }
1210     
1211     /*
1212      * This routine is called by the upper-layer tty layer to signal that
1213      * incoming characters should be throttled.
1214      */
1215     static void mxser_throttle(struct tty_struct *tty)
1216     {
1217     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1218     	unsigned long flags;
1219     
1220     	if (I_IXOFF(tty)) {
1221     		info->x_char = STOP_CHAR(tty);
1222     		save_flags(flags);
1223     		cli();
1224     		outb(info->IER, 0);
1225     		info->IER |= UART_IER_THRI;
1226     		outb(info->IER, info->base + UART_IER);		/* force Tx interrupt */
1227     		restore_flags(flags);
1228     	}
1229     	if (info->tty->termios->c_cflag & CRTSCTS) {
1230     		info->MCR &= ~UART_MCR_RTS;
1231     		save_flags(flags);
1232     		cli();
1233     		outb(info->MCR, info->base + UART_MCR);
1234     		restore_flags(flags);
1235     	}
1236     }
1237     
1238     static void mxser_unthrottle(struct tty_struct *tty)
1239     {
1240     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1241     	unsigned long flags;
1242     
1243     	if (I_IXOFF(tty)) {
1244     		if (info->x_char)
1245     			info->x_char = 0;
1246     		else {
1247     			info->x_char = START_CHAR(tty);
1248     			save_flags(flags);
1249     			cli();
1250     			outb(info->IER, 0);
1251     			info->IER |= UART_IER_THRI;	/* force Tx interrupt */
1252     			outb(info->IER, info->base + UART_IER);
1253     			restore_flags(flags);
1254     		}
1255     	}
1256     	if (info->tty->termios->c_cflag & CRTSCTS) {
1257     		info->MCR |= UART_MCR_RTS;
1258     		save_flags(flags);
1259     		cli();
1260     		outb(info->MCR, info->base + UART_MCR);
1261     		restore_flags(flags);
1262     	}
1263     }
1264     
1265     static void mxser_set_termios(struct tty_struct *tty,
1266     			      struct termios *old_termios)
1267     {
1268     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1269     
1270     /* 8-2-99 by William
1271        if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1272        (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1273        RELEVANT_IFLAG(old_termios->c_iflag)) )
1274        return;
1275     
1276        mxser_change_speed(info, old_termios);
1277     
1278        if ( (old_termios->c_cflag & CRTSCTS) &&
1279        !(tty->termios->c_cflag & CRTSCTS) ) {
1280        tty->hw_stopped = 0;
1281        mxser_start(tty);
1282        }
1283      */
1284     	if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1285     	    (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1286     	     RELEVANT_IFLAG(old_termios->c_iflag))) {
1287     
1288     		mxser_change_speed(info, old_termios);
1289     
1290     		if ((old_termios->c_cflag & CRTSCTS) &&
1291     		    !(tty->termios->c_cflag & CRTSCTS)) {
1292     			tty->hw_stopped = 0;
1293     			mxser_start(tty);
1294     		}
1295     	}
1296     /* Handle sw stopped */
1297     	if ((old_termios->c_iflag & IXON) &&
1298     	    !(tty->termios->c_iflag & IXON)) {
1299     		tty->stopped = 0;
1300     		mxser_start(tty);
1301     	}
1302     }
1303     
1304     /*
1305      * mxser_stop() and mxser_start()
1306      *
1307      * This routines are called before setting or resetting tty->stopped.
1308      * They enable or disable transmitter interrupts, as necessary.
1309      */
1310     static void mxser_stop(struct tty_struct *tty)
1311     {
1312     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1313     	unsigned long flags;
1314     
1315     	save_flags(flags);
1316     	cli();
1317     	if (info->IER & UART_IER_THRI) {
1318     		info->IER &= ~UART_IER_THRI;
1319     		outb(info->IER, info->base + UART_IER);
1320     	}
1321     	restore_flags(flags);
1322     }
1323     
1324     static void mxser_start(struct tty_struct *tty)
1325     {
1326     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1327     	unsigned long flags;
1328     
1329     	save_flags(flags);
1330     	cli();
1331     	if (info->xmit_cnt && info->xmit_buf &&
1332     	    !(info->IER & UART_IER_THRI)) {
1333     		info->IER |= UART_IER_THRI;
1334     		outb(info->IER, info->base + UART_IER);
1335     	}
1336     	restore_flags(flags);
1337     }
1338     
1339     /*
1340      * This routine is called by tty_hangup() when a hangup is signaled.
1341      */
1342     void mxser_hangup(struct tty_struct *tty)
1343     {
1344     	struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1345     
1346     	mxser_flush_buffer(tty);
1347     	mxser_shutdown(info);
1348     	info->event = 0;
1349     	info->count = 0;
1350     	info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1351     	info->tty = 0;
1352     	wake_up_interruptible(&info->open_wait);
1353     }
1354     
1355     /*
1356      * This is the serial driver's generic interrupt routine
1357      */
1358     static void mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1359     {
1360     	int status, i;
1361     	struct mxser_struct *info;
1362     	struct mxser_struct *port;
1363     	int max, irqbits, bits, msr;
1364     	int pass_counter = 0;
1365     
1366     	port = 0;
1367     	for (i = 0; i < MXSER_BOARDS; i++) {
1368     		if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1369     			port = dev_id;
1370     			break;
1371     		}
1372     	}
1373     
1374     	if (i == MXSER_BOARDS)
1375     		return;
1376     	if (port == 0)
1377     		return;
1378     	max = mxser_numports[mxsercfg[i].board_type];
1379     
1380     	while (1) {
1381     		irqbits = inb(port->vector) & port->vectormask;
1382     		if (irqbits == port->vectormask)
1383     			break;
1384     		for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1385     			if (irqbits == port->vectormask)
1386     				break;
1387     			if (bits & irqbits)
1388     				continue;
1389     			info = port + i;
1390     			if (!info->tty ||
1391     			  (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1392     				continue;
1393     			status = inb(info->base + UART_LSR) & info->read_status_mask;
1394     			if (status & UART_LSR_DR)
1395     				mxser_receive_chars(info, &status);
1396     			msr = inb(info->base + UART_MSR);
1397     			if (msr & UART_MSR_ANY_DELTA)
1398     				mxser_check_modem_status(info, msr);
1399     			if (status & UART_LSR_THRE) {
1400     /* 8-2-99 by William
1401        if ( info->x_char || (info->xmit_cnt > 0) )
1402      */
1403     				mxser_transmit_chars(info);
1404     			}
1405     		}
1406     		if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1407     #if 0
1408     			printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1409     #endif
1410     			break;	/* Prevent infinite loops */
1411     		}
1412     	}
1413     }
1414     
1415     static inline void mxser_receive_chars(struct mxser_struct *info,
1416     					 int *status)
1417     {
1418     	struct tty_struct *tty = info->tty;
1419     	unsigned char ch;
1420     	int ignored = 0;
1421     	int cnt = 0;
1422     
1423     	do {
1424     		ch = inb(info->base + UART_RX);
1425     		if (*status & info->ignore_status_mask) {
1426     			if (++ignored > 100)
1427     				break;
1428     		} else {
1429     			if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1430     				break;
1431     			tty->flip.count++;
1432     			if (*status & UART_LSR_SPECIAL) {
1433     				if (*status & UART_LSR_BI) {
1434     					*tty->flip.flag_buf_ptr++ = TTY_BREAK;
1435     					if (info->flags & ASYNC_SAK)
1436     						do_SAK(tty);
1437     				} else if (*status & UART_LSR_PE) {
1438     					*tty->flip.flag_buf_ptr++ = TTY_PARITY;
1439     				} else if (*status & UART_LSR_FE) {
1440     					*tty->flip.flag_buf_ptr++ = TTY_FRAME;
1441     				} else if (*status & UART_LSR_OE) {
1442     					*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1443     				} else
1444     					*tty->flip.flag_buf_ptr++ = 0;
1445     			} else
1446     				*tty->flip.flag_buf_ptr++ = 0;
1447     			*tty->flip.char_buf_ptr++ = ch;
1448     			cnt++;
1449     		}
1450     		*status = inb(info->base + UART_LSR) & info->read_status_mask;
1451     	} while (*status & UART_LSR_DR);
1452     	mxvar_log.rxcnt[info->port] += cnt;
1453     	queue_task(&tty->flip.tqueue, &tq_timer);
1454     
1455     }
1456     
1457     static inline void mxser_transmit_chars(struct mxser_struct *info)
1458     {
1459     	int count, cnt;
1460     
1461     	if (info->x_char) {
1462     		outb(info->x_char, info->base + UART_TX);
1463     		info->x_char = 0;
1464     		mxvar_log.txcnt[info->port]++;
1465     		return;
1466     	}
1467     	if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1468     	    info->tty->hw_stopped) {
1469     		info->IER &= ~UART_IER_THRI;
1470     		outb(info->IER, info->base + UART_IER);
1471     		return;
1472     	}
1473     	cnt = info->xmit_cnt;
1474     	count = info->xmit_fifo_size;
1475     	do {
1476     		outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1477     		info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1478     		if (--info->xmit_cnt <= 0)
1479     			break;
1480     	} while (--count > 0);
1481     	mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1482     
1483     	if (info->xmit_cnt < WAKEUP_CHARS) {
1484     		set_bit(MXSER_EVENT_TXLOW, &info->event);
1485     		MOD_INC_USE_COUNT;
1486     		if (schedule_task(&info->tqueue) == 0)
1487     		    MOD_DEC_USE_COUNT;
1488     	}
1489     	if (info->xmit_cnt <= 0) {
1490     		info->IER &= ~UART_IER_THRI;
1491     		outb(info->IER, info->base + UART_IER);
1492     	}
1493     }
1494     
1495     static inline void mxser_check_modem_status(struct mxser_struct *info,
1496     					      int status)
1497     {
1498     
1499     	/* update input line counters */
1500     	if (status & UART_MSR_TERI)
1501     		info->icount.rng++;
1502     	if (status & UART_MSR_DDSR)
1503     		info->icount.dsr++;
1504     	if (status & UART_MSR_DDCD)
1505     		info->icount.dcd++;
1506     	if (status & UART_MSR_DCTS)
1507     		info->icount.cts++;
1508     	wake_up_interruptible(&info->delta_msr_wait);
1509     
1510     	if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1511     		if (status & UART_MSR_DCD)
1512     			wake_up_interruptible(&info->open_wait);
1513     		else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1514     			   (info->flags & ASYNC_CALLOUT_NOHUP)))
1515     			set_bit(MXSER_EVENT_HANGUP, &info->event);
1516     		MOD_INC_USE_COUNT;
1517     		if (schedule_task(&info->tqueue) == 0)
1518     		    MOD_DEC_USE_COUNT;
1519     	}
1520     	if (info->flags & ASYNC_CTS_FLOW) {
1521     		if (info->tty->hw_stopped) {
1522     			if (status & UART_MSR_CTS) {
1523     				info->tty->hw_stopped = 0;
1524     				info->IER |= UART_IER_THRI;
1525     				outb(info->IER, info->base + UART_IER);
1526     
1527     				set_bit(MXSER_EVENT_TXLOW, &info->event);
1528     				MOD_INC_USE_COUNT;
1529     				if (schedule_task(&info->tqueue) == 0)
1530     					MOD_DEC_USE_COUNT;
1531     			}
1532     		} else {
1533     			if (!(status & UART_MSR_CTS)) {
1534     				info->tty->hw_stopped = 1;
1535     				info->IER &= ~UART_IER_THRI;
1536     				outb(info->IER, info->base + UART_IER);
1537     			}
1538     		}
1539     	}
1540     }
1541     
1542     static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1543     				 struct mxser_struct *info)
1544     {
1545     	DECLARE_WAITQUEUE(wait, current);
1546     	unsigned long flags;
1547     	int retval;
1548     	int do_clocal = 0;
1549     
1550     	/*
1551     	 * If the device is in the middle of being closed, then block
1552     	 * until it's done, and then try again.
1553     	 */
1554     	if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1555     		if (info->flags & ASYNC_CLOSING)
1556     			interruptible_sleep_on(&info->close_wait);
1557     #ifdef SERIAL_DO_RESTART
1558     		if (info->flags & ASYNC_HUP_NOTIFY)
1559     			return (-EAGAIN);
1560     		else
1561     			return (-ERESTARTSYS);
1562     #else
1563     		return (-EAGAIN);
1564     #endif
1565     	}
1566     	/*
1567     	 * If this is a callout device, then just make sure the normal
1568     	 * device isn't being used.
1569     	 */
1570     	if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1571     		if (info->flags & ASYNC_NORMAL_ACTIVE)
1572     			return (-EBUSY);
1573     		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1574     		    (info->flags & ASYNC_SESSION_LOCKOUT) &&
1575     		    (info->session != current->session))
1576     			return (-EBUSY);
1577     		if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1578     		    (info->flags & ASYNC_PGRP_LOCKOUT) &&
1579     		    (info->pgrp != current->pgrp))
1580     			return (-EBUSY);
1581     		info->flags |= ASYNC_CALLOUT_ACTIVE;
1582     		return (0);
1583     	}
1584     	/*
1585     	 * If non-blocking mode is set, or the port is not enabled,
1586     	 * then make the check up front and then exit.
1587     	 */
1588     	if ((filp->f_flags & O_NONBLOCK) ||
1589     	    (tty->flags & (1 << TTY_IO_ERROR))) {
1590     		if (info->flags & ASYNC_CALLOUT_ACTIVE)
1591     			return (-EBUSY);
1592     		info->flags |= ASYNC_NORMAL_ACTIVE;
1593     		return (0);
1594     	}
1595     	if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1596     		if (info->normal_termios.c_cflag & CLOCAL)
1597     			do_clocal = 1;
1598     	} else {
1599     		if (tty->termios->c_cflag & CLOCAL)
1600     			do_clocal = 1;
1601     	}
1602     
1603     	/*
1604     	 * Block waiting for the carrier detect and the line to become
1605     	 * free (i.e., not in use by the callout).  While we are in
1606     	 * this loop, info->count is dropped by one, so that
1607     	 * mxser_close() knows when to free things.  We restore it upon
1608     	 * exit, either normal or abnormal.
1609     	 */
1610     	retval = 0;
1611     	add_wait_queue(&info->open_wait, &wait);
1612     	save_flags(flags);
1613     	cli();
1614     	if (!tty_hung_up_p(filp))
1615     		info->count--;
1616     	restore_flags(flags);
1617     	info->blocked_open++;
1618     	while (1) {
1619     		save_flags(flags);
1620     		cli();
1621     		if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1622     			outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1623     			     info->base + UART_MCR);
1624     		restore_flags(flags);
1625     		current->state = TASK_INTERRUPTIBLE;
1626     		if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1627     #ifdef SERIAL_DO_RESTART
1628     			if (info->flags & ASYNC_HUP_NOTIFY)
1629     				retval = -EAGAIN;
1630     			else
1631     				retval = -ERESTARTSYS;
1632     #else
1633     			retval = -EAGAIN;
1634     #endif
1635     			break;
1636     		}
1637     		if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1638     		    !(info->flags & ASYNC_CLOSING) &&
1639     		    (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1640     			break;
1641     		if (signal_pending(current)) {
1642     			retval = -ERESTARTSYS;
1643     			break;
1644     		}
1645     		schedule();
1646     	}
1647     	current->state = TASK_RUNNING;
1648     	remove_wait_queue(&info->open_wait, &wait);
1649     	if (!tty_hung_up_p(filp))
1650     		info->count++;
1651     	info->blocked_open--;
1652     	if (retval)
1653     		return (retval);
1654     	info->flags |= ASYNC_NORMAL_ACTIVE;
1655     	return (0);
1656     }
1657     
1658     static int mxser_startup(struct mxser_struct *info)
1659     {
1660     	unsigned long flags;
1661     	unsigned long page;
1662     
1663     	page = get_free_page(GFP_KERNEL);
1664     	if (!page)
1665     		return (-ENOMEM);
1666     
1667     	save_flags(flags);
1668     	cli();
1669     
1670     	if (info->flags & ASYNC_INITIALIZED) {
1671     		free_page(page);
1672     		restore_flags(flags);
1673     		return (0);
1674     	}
1675     	if (!info->base || !info->type) {
1676     		if (info->tty)
1677     			set_bit(TTY_IO_ERROR, &info->tty->flags);
1678     		free_page(page);
1679     		restore_flags(flags);
1680     		return (0);
1681     	}
1682     	if (info->xmit_buf)
1683     		free_page(page);
1684     	else
1685     		info->xmit_buf = (unsigned char *) page;
1686     
1687     	/*
1688     	 * Clear the FIFO buffers and disable them
1689     	 * (they will be reenabled in mxser_change_speed())
1690     	 */
1691     	if (info->xmit_fifo_size == 16)
1692     		outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1693     		     info->base + UART_FCR);
1694     
1695     	/*
1696     	 * At this point there's no way the LSR could still be 0xFF;
1697     	 * if it is, then bail out, because there's likely no UART
1698     	 * here.
1699     	 */
1700     	if (inb(info->base + UART_LSR) == 0xff) {
1701     		restore_flags(flags);
1702     		if (capable(CAP_SYS_ADMIN)) {
1703     			if (info->tty)
1704     				set_bit(TTY_IO_ERROR, &info->tty->flags);
1705     			return (0);
1706     		} else
1707     			return (-ENODEV);
1708     	}
1709     	/*
1710     	 * Clear the interrupt registers.
1711     	 */
1712     	(void) inb(info->base + UART_LSR);
1713     	(void) inb(info->base + UART_RX);
1714     	(void) inb(info->base + UART_IIR);
1715     	(void) inb(info->base + UART_MSR);
1716     
1717     	/*
1718     	 * Now, initialize the UART
1719     	 */
1720     	outb(UART_LCR_WLEN8, info->base + UART_LCR);	/* reset DLAB */
1721     	info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1722     	outb(info->MCR, info->base + UART_MCR);
1723     
1724     	/*
1725     	 * Finally, enable interrupts
1726     	 */
1727     	info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1728     	outb(info->IER, info->base + UART_IER);		/* enable interrupts */
1729     
1730     	/*
1731     	 * And clear the interrupt registers again for luck.
1732     	 */
1733     	(void) inb(info->base + UART_LSR);
1734     	(void) inb(info->base + UART_RX);
1735     	(void) inb(info->base + UART_IIR);
1736     	(void) inb(info->base + UART_MSR);
1737     
1738     	if (info->tty)
1739     		test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1740     
1741     	info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1742     
1743     	/*
1744     	 * and set the speed of the serial port
1745     	 */
1746     	mxser_change_speed(info, 0);
1747     
1748     	info->flags |= ASYNC_INITIALIZED;
1749     	restore_flags(flags);
1750     	return (0);
1751     }
1752     
1753     /*
1754      * This routine will shutdown a serial port; interrupts maybe disabled, and
1755      * DTR is dropped if the hangup on close termio flag is on.
1756      */
1757     static void mxser_shutdown(struct mxser_struct *info)
1758     {
1759     	unsigned long flags;
1760     
1761     	if (!(info->flags & ASYNC_INITIALIZED))
1762     		return;
1763     
1764     	save_flags(flags);
1765     	cli();			/* Disable interrupts */
1766     
1767     	/*
1768     	 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1769     	 * here so the queue might never be waken up
1770     	 */
1771     	wake_up_interruptible(&info->delta_msr_wait);
1772     
1773     	/*
1774     	 * Free the IRQ, if necessary
1775     	 */
1776     	if (info->xmit_buf) {
1777     		free_page((unsigned long) info->xmit_buf);
1778     		info->xmit_buf = 0;
1779     	}
1780     	info->IER = 0;
1781     	outb(0x00, info->base + UART_IER);	/* disable all intrs */
1782     
1783     	if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1784     		info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1785     	outb(info->MCR, info->base + UART_MCR);
1786     
1787     	/* clear Rx/Tx FIFO's */
1788     	outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1789     	/* read data port to reset things */
1790     	(void) inb(info->base + UART_RX);
1791     
1792     	if (info->tty)
1793     		set_bit(TTY_IO_ERROR, &info->tty->flags);
1794     
1795     	info->flags &= ~ASYNC_INITIALIZED;
1796     	restore_flags(flags);
1797     }
1798     
1799     /*
1800      * This routine is called to set the UART divisor registers to match
1801      * the specified baud rate for a serial port.
1802      */
1803     static int mxser_change_speed(struct mxser_struct *info,
1804     			      struct termios *old_termios)
1805     {
1806     	int quot = 0;
1807     	unsigned cflag, cval, fcr;
1808     	int i;
1809     	int ret = 0;
1810     	unsigned long flags;
1811     
1812     	if (!info->tty || !info->tty->termios)
1813     		return ret;
1814     	cflag = info->tty->termios->c_cflag;
1815     	if (!(info->base))
1816     		return ret;
1817     
1818     #ifndef B921600
1819     #define B921600 (B460800 +1)
1820     #endif
1821     	switch (cflag & (CBAUD | CBAUDEX)) {
1822     	case B921600:
1823     		i = 20;
1824     		break;
1825     	case B460800:
1826     		i = 19;
1827     		break;
1828     	case B230400:
1829     		i = 18;
1830     		break;
1831     	case B115200:
1832     		i = 17;
1833     		break;
1834     	case B57600:
1835     		i = 16;
1836     		break;
1837     	case B38400:
1838     		i = 15;
1839     		break;
1840     	case B19200:
1841     		i = 14;
1842     		break;
1843     	case B9600:
1844     		i = 13;
1845     		break;
1846     	case B4800:
1847     		i = 12;
1848     		break;
1849     	case B2400:
1850     		i = 11;
1851     		break;
1852     	case B1800:
1853     		i = 10;
1854     		break;
1855     	case B1200:
1856     		i = 9;
1857     		break;
1858     	case B600:
1859     		i = 8;
1860     		break;
1861     	case B300:
1862     		i = 7;
1863     		break;
1864     	case B200:
1865     		i = 6;
1866     		break;
1867     	case B150:
1868     		i = 5;
1869     		break;
1870     	case B134:
1871     		i = 4;
1872     		break;
1873     	case B110:
1874     		i = 3;
1875     		break;
1876     	case B75:
1877     		i = 2;
1878     		break;
1879     	case B50:
1880     		i = 1;
1881     		break;
1882     	default:
1883     		i = 0;
1884     		break;
1885     	}
1886     
1887     	if (i == 15) {
1888     		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1889     			i = 16;	/* 57600 bps */
1890     		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1891     			i = 17;	/* 115200 bps */
1892     
1893     #ifdef ASYNC_SPD_SHI
1894     		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1895     			i = 18;
1896     #endif
1897     
1898     #ifdef ASYNC_SPD_WARP
1899     		if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1900     			i = 19;
1901     #endif
1902     	}
1903     	if (mxvar_baud_table[i] == 134) {
1904     		quot = (2 * info->baud_base / 269);
1905     	} else if (mxvar_baud_table[i]) {
1906     		quot = info->baud_base / mxvar_baud_table[i];
1907     		if (!quot && old_termios) {
1908     			/* re-calculate */
1909     			info->tty->termios->c_cflag &= ~CBAUD;
1910     			info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1911     			switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1912     			case B921600:
1913     				i = 20;
1914     				break;
1915     			case B460800:
1916     				i = 19;
1917     				break;
1918     			case B230400:
1919     				i = 18;
1920     				break;
1921     			case B115200:
1922     				i = 17;
1923     				break;
1924     			case B57600:
1925     				i = 16;
1926     				break;
1927     			case B38400:
1928     				i = 15;
1929     				break;
1930     			case B19200:
1931     				i = 14;
1932     				break;
1933     			case B9600:
1934     				i = 13;
1935     				break;
1936     			case B4800:
1937     				i = 12;
1938     				break;
1939     			case B2400:
1940     				i = 11;
1941     				break;
1942     			case B1800:
1943     				i = 10;
1944     				break;
1945     			case B1200:
1946     				i = 9;
1947     				break;
1948     			case B600:
1949     				i = 8;
1950     				break;
1951     			case B300:
1952     				i = 7;
1953     				break;
1954     			case B200:
1955     				i = 6;
1956     				break;
1957     			case B150:
1958     				i = 5;
1959     				break;
1960     			case B134:
1961     				i = 4;
1962     				break;
1963     			case B110:
1964     				i = 3;
1965     				break;
1966     			case B75:
1967     				i = 2;
1968     				break;
1969     			case B50:
1970     				i = 1;
1971     				break;
1972     			default:
1973     				i = 0;
1974     				break;
1975     			}
1976     			if (i == 15) {
1977     				if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1978     					i = 16;		/* 57600 bps */
1979     				if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1980     					i = 17;		/* 115200 bps */
1981     #ifdef ASYNC_SPD_SHI
1982     				if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1983     					i = 18;
1984     #endif
1985     #ifdef ASYNC_SPD_WARP
1986     				if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1987     					i = 19;
1988     #endif
1989     			}
1990     			if (mxvar_baud_table[i] == 134) {
1991     				quot = (2 * info->baud_base / 269);
1992     			} else if (mxvar_baud_table[i]) {
1993     				quot = info->baud_base / mxvar_baud_table[i];
1994     				if (quot == 0)
1995     					quot = 1;
1996     			} else {
1997     				quot = 0;
1998     			}
1999     		} else if (quot == 0)
2000     			quot = 1;
2001     	} else {
2002     		quot = 0;
2003     	}
2004     
2005     	if (quot) {
2006     		info->MCR |= UART_MCR_DTR;
2007     		save_flags(flags);
2008     		cli();
2009     		outb(info->MCR, info->base + UART_MCR);
2010     		restore_flags(flags);
2011     	} else {
2012     		info->MCR &= ~UART_MCR_DTR;
2013     		save_flags(flags);
2014     		cli();
2015     		outb(info->MCR, info->base + UART_MCR);
2016     		restore_flags(flags);
2017     		return ret;
2018     	}
2019     	/* byte size and parity */
2020     	switch (cflag & CSIZE) {
2021     	case CS5:
2022     		cval = 0x00;
2023     		break;
2024     	case CS6:
2025     		cval = 0x01;
2026     		break;
2027     	case CS7:
2028     		cval = 0x02;
2029     		break;
2030     	case CS8:
2031     		cval = 0x03;
2032     		break;
2033     	default:
2034     		cval = 0x00;
2035     		break;		/* too keep GCC shut... */
2036     	}
2037     	if (cflag & CSTOPB)
2038     		cval |= 0x04;
2039     	if (cflag & PARENB)
2040     		cval |= UART_LCR_PARITY;
2041     	if (!(cflag & PARODD))
2042     		cval |= UART_LCR_EPAR;
2043     	if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2044     		fcr = 0;
2045     	} else {
2046     		fcr = UART_FCR_ENABLE_FIFO;
2047     		switch (info->rx_trigger) {
2048     		case 1:
2049     			fcr |= UART_FCR_TRIGGER_1;
2050     			break;
2051     		case 4:
2052     			fcr |= UART_FCR_TRIGGER_4;
2053     			break;
2054     		case 8:
2055     			fcr |= UART_FCR_TRIGGER_8;
2056     			break;
2057     		default:
2058     			fcr |= UART_FCR_TRIGGER_14;
2059     		}
2060     	}
2061     
2062     	/* CTS flow control flag and modem status interrupts */
2063     	info->IER &= ~UART_IER_MSI;
2064     	info->MCR &= ~UART_MCR_AFE;
2065     	if (cflag & CRTSCTS) {
2066     		info->flags |= ASYNC_CTS_FLOW;
2067     		info->IER |= UART_IER_MSI;
2068     		if (info->type == PORT_16550A)
2069     			info->MCR |= UART_MCR_AFE;
2070     	} else {
2071     		info->flags &= ~ASYNC_CTS_FLOW;
2072     	}
2073     	outb(info->MCR, info->base + UART_MCR);
2074     	if (cflag & CLOCAL)
2075     		info->flags &= ~ASYNC_CHECK_CD;
2076     	else {
2077     		info->flags |= ASYNC_CHECK_CD;
2078     		info->IER |= UART_IER_MSI;
2079     	}
2080     	outb(info->IER, info->base + UART_IER);
2081     
2082     	/*
2083     	 * Set up parity check flag
2084     	 */
2085     	info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2086     	if (I_INPCK(info->tty))
2087     		info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2088     	if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2089     		info->read_status_mask |= UART_LSR_BI;
2090     
2091     	info->ignore_status_mask = 0;
2092     #if 0
2093     	/* This should be safe, but for some broken bits of hardware... */
2094     	if (I_IGNPAR(info->tty)) {
2095     		info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2096     		info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2097     	}
2098     #endif
2099     	if (I_IGNBRK(info->tty)) {
2100     		info->ignore_status_mask |= UART_LSR_BI;
2101     		info->read_status_mask |= UART_LSR_BI;
2102     		/*
2103     		 * If we're ignore parity and break indicators, ignore
2104     		 * overruns too.  (For real raw support).
2105     		 */
2106     		if (I_IGNPAR(info->tty)) {
2107     			info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2108     			info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2109     		}
2110     	}
2111     	save_flags(flags);
2112     	cli();
2113     	outb(cval | UART_LCR_DLAB, info->base + UART_LCR);	/* set DLAB */
2114     	outb(quot & 0xff, info->base + UART_DLL);	/* LS of divisor */
2115     	outb(quot >> 8, info->base + UART_DLM);		/* MS of divisor */
2116     	outb(cval, info->base + UART_LCR);	/* reset DLAB */
2117     	outb(fcr, info->base + UART_FCR);	/* set fcr */
2118     	restore_flags(flags);
2119     
2120     	return ret;
2121     }
2122     
2123     /*
2124      * ------------------------------------------------------------
2125      * friends of mxser_ioctl()
2126      * ------------------------------------------------------------
2127      */
2128     static int mxser_get_serial_info(struct mxser_struct *info,
2129     				 struct serial_struct *retinfo)
2130     {
2131     	struct serial_struct tmp;
2132     
2133     	if (!retinfo)
2134     		return (-EFAULT);
2135     	memset(&tmp, 0, sizeof(tmp));
2136     	tmp.type = info->type;
2137     	tmp.line = info->port;
2138     	tmp.port = info->base;
2139     	tmp.irq = info->irq;
2140     	tmp.flags = info->flags;
2141     	tmp.baud_base = info->baud_base;
2142     	tmp.close_delay = info->close_delay;
2143     	tmp.closing_wait = info->closing_wait;
2144     	tmp.custom_divisor = info->custom_divisor;
2145     	tmp.hub6 = 0;
2146     	copy_to_user(retinfo, &tmp, sizeof(*retinfo));
2147     	return (0);
2148     }
2149     
2150     static int mxser_set_serial_info(struct mxser_struct *info,
2151     				 struct serial_struct *new_info)
2152     {
2153     	struct serial_struct new_serial;
2154     	unsigned int flags;
2155     	int retval = 0;
2156     
2157     	if (!new_info || !info->base)
2158     		return (-EFAULT);
2159     	copy_from_user(&new_serial, new_info, sizeof(new_serial));
2160     
2161     	if ((new_serial.irq != info->irq) ||
2162     	    (new_serial.port != info->base) ||
2163     	    (new_serial.type != info->type) ||
2164     	    (new_serial.custom_divisor != info->custom_divisor) ||
2165     	    (new_serial.baud_base != info->baud_base))
2166     		return (-EPERM);
2167     
2168     	flags = info->flags & ASYNC_SPD_MASK;
2169     
2170     	if (!suser()) {
2171     		if ((new_serial.baud_base != info->baud_base) ||
2172     		    (new_serial.close_delay != info->close_delay) ||
2173     		    ((new_serial.flags & ~ASYNC_USR_MASK) !=
2174     		     (info->flags & ~ASYNC_USR_MASK)))
2175     			return (-EPERM);
2176     		info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2177     			       (new_serial.flags & ASYNC_USR_MASK));
2178     	} else {
2179     		/*
2180     		 * OK, past this point, all the error checking has been done.
2181     		 * At this point, we start making changes.....
2182     		 */
2183     		info->flags = ((info->flags & ~ASYNC_FLAGS) |
2184     			       (new_serial.flags & ASYNC_FLAGS));
2185     		info->close_delay = new_serial.close_delay * HZ / 100;
2186     		info->closing_wait = new_serial.closing_wait * HZ / 100;
2187     	}
2188     
2189     	if (info->flags & ASYNC_INITIALIZED) {
2190     		if (flags != (info->flags & ASYNC_SPD_MASK)) {
2191     			mxser_change_speed(info, 0);
2192     		}
2193     	} else
2194     		retval = mxser_startup(info);
2195     	return (retval);
2196     }
2197     
2198     /*
2199      * mxser_get_lsr_info - get line status register info
2200      *
2201      * Purpose: Let user call ioctl() to get info when the UART physically
2202      *          is emptied.  On bus types like RS485, the transmitter must
2203      *          release the bus after transmitting. This must be done when
2204      *          the transmit shift register is empty, not be done when the
2205      *          transmit holding register is empty.  This functionality
2206      *          allows an RS485 driver to be written in user space.
2207      */
2208     static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2209     {
2210     	unsigned char status;
2211     	unsigned int result;
2212     	unsigned long flags;
2213     
2214     	save_flags(flags);
2215     	cli();
2216     	status = inb(info->base + UART_LSR);
2217     	restore_flags(flags);
2218     	result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2219     	return put_user(result, value);
2220     }
2221     
2222     /*
2223      * This routine sends a break character out the serial port.
2224      */
2225     static void mxser_send_break(struct mxser_struct *info, int duration)
2226     {
2227     	unsigned long flags;
2228     	if (!info->base)
2229     		return;
2230     	current->state = TASK_INTERRUPTIBLE;
2231     	save_flags(flags);
2232     	cli();
2233     	outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2234     	schedule_timeout(duration);
2235     	outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2236     	restore_flags(flags);
2237     }
2238     
2239     static int mxser_get_modem_info(struct mxser_struct *info,
2240     				unsigned int *value)
2241     {
2242     	unsigned char control, status;
2243     	unsigned int result;
2244     	unsigned long flags;
2245     
2246     	control = info->MCR;
2247     	save_flags(flags);
2248     	cli();
2249     	status = inb(info->base + UART_MSR);
2250     	if (status & UART_MSR_ANY_DELTA)
2251     		mxser_check_modem_status(info, status);
2252     	restore_flags(flags);
2253     	result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2254     	    ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2255     	    ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2256     	    ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2257     	    ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2258     	    ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2259     	return put_user(result, value);
2260     }
2261     
2262     static int mxser_set_modem_info(struct mxser_struct *info, unsigned int cmd,
2263     				unsigned int *value)
2264     {
2265     	unsigned int arg;
2266     	unsigned long flags;
2267     
2268     	if(get_user(arg, value))
2269     		return -EFAULT;
2270     	switch (cmd) {
2271     	case TIOCMBIS:
2272     		if (arg & TIOCM_RTS)
2273     			info->MCR |= UART_MCR_RTS;
2274     		if (arg & TIOCM_DTR)
2275     			info->MCR |= UART_MCR_DTR;
2276     		break;
2277     	case TIOCMBIC:
2278     		if (arg & TIOCM_RTS)
2279     			info->MCR &= ~UART_MCR_RTS;
2280     		if (arg & TIOCM_DTR)
2281     			info->MCR &= ~UART_MCR_DTR;
2282     		break;
2283     	case TIOCMSET:
2284     		info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR)) |
2285     			     ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0) |
2286     			     ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
2287     		break;
2288     	default:
2289     		return (-EINVAL);
2290     	}
2291     	save_flags(flags);
2292     	cli();
2293     	outb(info->MCR, info->base + UART_MCR);
2294     	restore_flags(flags);
2295     	return (0);
2296     }
2297     
2298     static int mxser_read_register(int, unsigned short *);
2299     static int mxser_program_mode(int);
2300     static void mxser_normal_mode(int);
2301     
2302     static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2303     {
2304     	int id, i, bits;
2305     	unsigned short regs[16], irq;
2306     	unsigned char scratch, scratch2;
2307     
2308     	id = mxser_read_register(cap, regs);
2309     	if (id == C168_ASIC_ID)
2310     		hwconf->board_type = MXSER_BOARD_C168_ISA;
2311     	else if (id == C104_ASIC_ID)
2312     		hwconf->board_type = MXSER_BOARD_C104_ISA;
2313     	else if (id == CI104J_ASIC_ID)
2314     		hwconf->board_type = MXSER_BOARD_CI104J;
2315     	else
2316     		return (0);
2317     	irq = regs[9] & 0x0F;
2318     	irq = irq | (irq << 4);
2319     	irq = irq | (irq << 8);
2320     	if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2321     		return (MXSER_ERR_IRQ_CONFLIT);
2322     	}
2323     	if (!irq) {
2324     		return (MXSER_ERR_IRQ);
2325     	}
2326     	for (i = 0; i < 8; i++)
2327     		hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2328     	hwconf->irq = (int) (irq & 0x0F);
2329     	if ((regs[12] & 0x80) == 0) {
2330     		return (MXSER_ERR_VECTOR);
2331     	}
2332     	hwconf->vector = (int) regs[11];	/* interrupt vector */
2333     	if (id == 1)
2334     		hwconf->vector_mask = 0x00FF;
2335     	else
2336     		hwconf->vector_mask = 0x000F;
2337     	for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2338     		if (regs[12] & bits)
2339     			hwconf->baud_base[i] = 921600;
2340     		else
2341     			hwconf->baud_base[i] = 115200;
2342     	}
2343     	scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2344     	outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2345     	outb(0, cap + UART_EFR);	/* EFR is the same as FCR */
2346     	outb(scratch2, cap + UART_LCR);
2347     	outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2348     	scratch = inb(cap + UART_IIR);
2349     	if (scratch & 0xC0)
2350     		hwconf->uart_type = PORT_16550A;
2351     	else
2352     		hwconf->uart_type = PORT_16450;
2353     	if (id == 1)
2354     		hwconf->ports = 8;
2355     	else
2356     		hwconf->ports = 4;
2357     	return (hwconf->ports);
2358     }
2359     
2360     #define CHIP_SK 	0x01	/* Serial Data Clock  in Eprom */
2361     #define CHIP_DO 	0x02	/* Serial Data Output in Eprom */
2362     #define CHIP_CS 	0x04	/* Serial Chip Select in Eprom */
2363     #define CHIP_DI 	0x08	/* Serial Data Input  in Eprom */
2364     #define EN_CCMD 	0x000	/* Chip's command register     */
2365     #define EN0_RSARLO	0x008	/* Remote start address reg 0  */
2366     #define EN0_RSARHI	0x009	/* Remote start address reg 1  */
2367     #define EN0_RCNTLO	0x00A	/* Remote byte count reg WR    */
2368     #define EN0_RCNTHI	0x00B	/* Remote byte count reg WR    */
2369     #define EN0_DCFG	0x00E	/* Data configuration reg WR   */
2370     #define EN0_PORT	0x010	/* Rcv missed frame error counter RD */
2371     #define ENC_PAGE0	0x000	/* Select page 0 of chip registers   */
2372     #define ENC_PAGE3	0x0C0	/* Select page 3 of chip registers   */
2373     static int mxser_read_register(int port, unsigned short *regs)
2374     {
2375     	int i, k, value, id;
2376     	unsigned int j;
2377     
2378     	id = mxser_program_mode(port);
2379     	if (id < 0)
2380     		return (id);
2381     	for (i = 0; i < 14; i++) {
2382     		k = (i & 0x3F) | 0x180;
2383     		for (j = 0x100; j > 0; j >>= 1) {
2384     			outb(CHIP_CS, port);
2385     			if (k & j) {
2386     				outb(CHIP_CS | CHIP_DO, port);
2387     				outb(CHIP_CS | CHIP_DO | CHIP_SK, port);	/* A? bit of read */
2388     			} else {
2389     				outb(CHIP_CS, port);
2390     				outb(CHIP_CS | CHIP_SK, port);	/* A? bit of read */
2391     			}
2392     		}
2393     		(void) inb(port);
2394     		value = 0;
2395     		for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2396     			outb(CHIP_CS, port);
2397     			outb(CHIP_CS | CHIP_SK, port);
2398     			if (inb(port) & CHIP_DI)
2399     				value |= j;
2400     		}
2401     		regs[i] = value;
2402     		outb(0, port);
2403     	}
2404     	mxser_normal_mode(port);
2405     	return (id);
2406     }
2407     
2408     static int mxser_program_mode(int port)
2409     {
2410     	int id, i, j, n;
2411     	unsigned long flags;
2412     
2413     	save_flags(flags);
2414     	cli();
2415     	outb(0, port);
2416     	outb(0, port);
2417     	outb(0, port);
2418     	(void) inb(port);
2419     	(void) inb(port);
2420     	outb(0, port);
2421     	(void) inb(port);
2422     	restore_flags(flags);
2423     	id = inb(port + 1) & 0x1F;
2424     	if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID))
2425     		return (-1);
2426     	for (i = 0, j = 0; i < 4; i++) {
2427     		n = inb(port + 2);
2428     		if (n == 'M') {
2429     			j = 1;
2430     		} else if ((j == 1) && (n == 1)) {
2431     			j = 2;
2432     			break;
2433     		} else
2434     			j = 0;
2435     	}
2436     	if (j != 2)
2437     		id = -2;
2438     	return (id);
2439     }
2440     
2441     static void mxser_normal_mode(int port)
2442     {
2443     	int i, n;
2444     
2445     	outb(0xA5, port + 1);
2446     	outb(0x80, port + 3);
2447     	outb(12, port + 0);	/* 9600 bps */
2448     	outb(0, port + 1);
2449     	outb(0x03, port + 3);	/* 8 data bits */
2450     	outb(0x13, port + 4);	/* loop back mode */
2451     	for (i = 0; i < 16; i++) {
2452     		n = inb(port + 5);
2453     		if ((n & 0x61) == 0x60)
2454     			break;
2455     		if ((n & 1) == 1)
2456     			(void) inb(port);
2457     	}
2458     	outb(0x00, port + 4);
2459     }
2460