~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/char/mxser.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.