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