2  *          mxser.c  -- MOXA Smartio/Industio family multiport serial driver.
 
   4  *      Copyright (C) 1999-2006  Moxa Technologies (support@moxa.com.tw).
 
   5  *      Copyright (C) 2006-2007  Jiri Slaby <jirislaby@gmail.com>
 
   7  *      This code is loosely based on the 1.8 moxa driver which is based on
 
   8  *      Linux serial driver, written by Linus Torvalds, Theodore T'so and
 
  11  *      This program is free software; you can redistribute it and/or modify
 
  12  *      it under the terms of the GNU General Public License as published by
 
  13  *      the Free Software Foundation; either version 2 of the License, or
 
  14  *      (at your option) any later version.
 
  16  *      Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
 
  17  *      <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
 
  18  *      - Fixed x86_64 cleanness
 
  19  *      - Fixed sleep with spinlock held in mxser_send_break
 
  22 #include <linux/module.h>
 
  23 #include <linux/autoconf.h>
 
  24 #include <linux/errno.h>
 
  25 #include <linux/signal.h>
 
  26 #include <linux/sched.h>
 
  27 #include <linux/timer.h>
 
  28 #include <linux/interrupt.h>
 
  29 #include <linux/tty.h>
 
  30 #include <linux/tty_flip.h>
 
  31 #include <linux/serial.h>
 
  32 #include <linux/serial_reg.h>
 
  33 #include <linux/major.h>
 
  34 #include <linux/string.h>
 
  35 #include <linux/fcntl.h>
 
  36 #include <linux/ptrace.h>
 
  37 #include <linux/gfp.h>
 
  38 #include <linux/ioport.h>
 
  40 #include <linux/delay.h>
 
  41 #include <linux/pci.h>
 
  42 #include <linux/bitops.h>
 
  44 #include <asm/system.h>
 
  47 #include <asm/uaccess.h>
 
  49 #include "mxser_new.h"
 
  51 #define MXSER_VERSION   "2.0.2"         /* 1.10 */
 
  52 #define MXSERMAJOR       174
 
  53 #define MXSERCUMAJOR     175
 
  55 #define MXSER_BOARDS            4       /* Max. boards */
 
  56 #define MXSER_PORTS_PER_BOARD   8       /* Max. ports per board */
 
  57 #define MXSER_PORTS             (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
 
  58 #define MXSER_ISR_PASS_LIMIT    100
 
  60 #define MXSER_ERR_IOADDR        -1
 
  61 #define MXSER_ERR_IRQ           -2
 
  62 #define MXSER_ERR_IRQ_CONFLIT   -3
 
  63 #define MXSER_ERR_VECTOR        -4
 
  65 /*CheckIsMoxaMust return value*/
 
  66 #define MOXA_OTHER_UART         0x00
 
  67 #define MOXA_MUST_MU150_HWID    0x01
 
  68 #define MOXA_MUST_MU860_HWID    0x02
 
  70 #define WAKEUP_CHARS            256
 
  72 #define UART_MCR_AFE            0x20
 
  73 #define UART_LSR_SPECIAL        0x1E
 
  75 #define PCI_DEVICE_ID_CB108     0x1080
 
  76 #define PCI_DEVICE_ID_CB114     0x1142
 
  77 #define PCI_DEVICE_ID_CB134I    0x1341
 
  78 #define PCI_DEVICE_ID_CP138U    0x1380
 
  79 #define PCI_DEVICE_ID_POS104UL  0x1044
 
  82 #define C168_ASIC_ID    1
 
  83 #define C104_ASIC_ID    2
 
  84 #define C102_ASIC_ID    0xB
 
  85 #define CI132_ASIC_ID   4
 
  86 #define CI134_ASIC_ID   3
 
  87 #define CI104J_ASIC_ID  5
 
  89 #define MXSER_HIGHBAUD  1
 
  92 /* This is only for PCI */
 
 102 } Gpci_uart_info[] = {
 
 103         {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
 
 104         {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
 
 105         {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
 
 107 #define UART_INFO_NUM   ARRAY_SIZE(Gpci_uart_info)
 
 109 struct mxser_cardinfo {
 
 115 static const struct mxser_cardinfo mxser_cards[] = {
 
 116 /* 0*/  { 8, "C168 series", },
 
 117         { 4, "C104 series", },
 
 118         { 4, "CI-104J series", },
 
 119         { 8, "C168H/PCI series", },
 
 120         { 4, "C104H/PCI series", },
 
 121 /* 5*/  { 4, "C102 series", MXSER_HAS2 },       /* C102-ISA */
 
 122         { 4, "CI-132 series", MXSER_HAS2 },
 
 123         { 4, "CI-134 series", },
 
 124         { 2, "CP-132 series", },
 
 125         { 4, "CP-114 series", },
 
 126 /*10*/  { 4, "CT-114 series", },
 
 127         { 2, "CP-102 series", MXSER_HIGHBAUD },
 
 128         { 4, "CP-104U series", },
 
 129         { 8, "CP-168U series", },
 
 130         { 2, "CP-132U series", },
 
 131 /*15*/  { 4, "CP-134U series", },
 
 132         { 4, "CP-104JU series", },
 
 133         { 8, "Moxa UC7000 Serial", },           /* RC7000 */
 
 134         { 8, "CP-118U series", },
 
 135         { 2, "CP-102UL series", },
 
 136 /*20*/  { 2, "CP-102U series", },
 
 137         { 8, "CP-118EL series", },
 
 138         { 8, "CP-168EL series", },
 
 139         { 4, "CP-104EL series", },
 
 140         { 8, "CB-108 series", },
 
 141 /*25*/  { 4, "CB-114 series", },
 
 142         { 4, "CB-134I series", },
 
 143         { 8, "CP-138U series", },
 
 144         { 4, "POS-104UL series", }
 
 147 /* driver_data correspond to the lines in the structure above
 
 148    see also ISA probe function before you change something */
 
 149 static struct pci_device_id mxser_pcibrds[] = {
 
 150         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168),   .driver_data = 3 },
 
 151         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104),   .driver_data = 4 },
 
 152         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132),  .driver_data = 8 },
 
 153         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114),  .driver_data = 9 },
 
 154         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114),  .driver_data = 10 },
 
 155         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102),  .driver_data = 11 },
 
 156         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
 
 157         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
 
 158         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
 
 159         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
 
 160         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
 
 161         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
 
 162         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
 
 163         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
 
 164         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
 
 165         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
 
 166         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
 
 167         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
 
 168         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108),       .driver_data = 24 },
 
 169         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114),       .driver_data = 25 },
 
 170         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I),      .driver_data = 26 },
 
 171         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U),      .driver_data = 27 },
 
 172         { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL),    .driver_data = 28 },
 
 175 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
 
 177 static int mxvar_baud_table[] = {
 
 178         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
 
 179         4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
 
 181 static unsigned int mxvar_baud_table1[] = {
 
 182         0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
 
 183         B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
 
 185 #define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
 
 187 #define B_SPEC  B2000000
 
 189 static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
 
 190 static int ttymajor = MXSERMAJOR;
 
 191 static int calloutmajor = MXSERCUMAJOR;
 
 193 /* Variables for insmod */
 
 195 MODULE_AUTHOR("Casper Yang");
 
 196 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
 
 197 module_param_array(ioaddr, int, NULL, 0);
 
 198 module_param(ttymajor, int, 0);
 
 199 MODULE_LICENSE("GPL");
 
 203         unsigned long rxcnt[MXSER_PORTS];
 
 204         unsigned long txcnt[MXSER_PORTS];
 
 211         unsigned long up_rxcnt;
 
 212         unsigned long up_txcnt;
 
 214         unsigned char hold_reason;
 
 217 struct mxser_mon_ext {
 
 218         unsigned long rx_cnt[32];
 
 219         unsigned long tx_cnt[32];
 
 220         unsigned long up_rxcnt[32];
 
 221         unsigned long up_txcnt[32];
 
 222         int modem_status[32];
 
 236         struct mxser_board *board;
 
 237         struct tty_struct *tty;
 
 239         unsigned long ioaddr;
 
 240         unsigned long opmode_ioaddr;
 
 244         int rx_trigger;         /* Rx fifo trigger level */
 
 246         int baud_base;          /* max. speed */
 
 248         int type;               /* UART type */
 
 249         int flags;              /* defined in tty.h */
 
 252         int x_char;             /* xon/xoff character */
 
 253         int IER;                /* Interrupt Enable Register */
 
 254         int MCR;                /* Modem control register */
 
 256         unsigned char stop_rx;
 
 257         unsigned char ldisc_stop_rx;
 
 261         unsigned short closing_wait;
 
 262         unsigned char err_shadow;
 
 265         int count;              /* # of fd on device */
 
 266         int blocked_open;       /* # of blocked opens */
 
 267         struct async_icount icount; /* kernel counters for 4 input interrupts */
 
 270         int read_status_mask;
 
 271         int ignore_status_mask;
 
 273         unsigned char *xmit_buf;
 
 278         struct ktermios normal_termios;
 
 280         struct mxser_mon mon_data;
 
 283         wait_queue_head_t open_wait;
 
 284         wait_queue_head_t delta_msr_wait;
 
 290         const struct mxser_cardinfo *info;
 
 291         unsigned long vector;
 
 292         unsigned long vector_mask;
 
 297         struct mxser_port ports[MXSER_PORTS_PER_BOARD];
 
 300 struct mxser_mstatus {
 
 308 static struct mxser_mstatus GMStatus[MXSER_PORTS];
 
 310 static int mxserBoardCAP[MXSER_BOARDS] = {
 
 312         /*  0x180, 0x280, 0x200, 0x320 */
 
 315 static struct mxser_board mxser_boards[MXSER_BOARDS];
 
 316 static struct tty_driver *mxvar_sdriver;
 
 317 static struct mxser_log mxvar_log;
 
 318 static int mxvar_diagflag;
 
 319 static unsigned char mxser_msr[MXSER_PORTS + 1];
 
 320 static struct mxser_mon_ext mon_data_ext;
 
 321 static int mxser_set_baud_method[MXSER_PORTS + 1];
 
 324 static int __devinit CheckIsMoxaMust(int io)
 
 329         outb(0, io + UART_LCR);
 
 330         DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
 
 331         oldmcr = inb(io + UART_MCR);
 
 332         outb(0, io + UART_MCR);
 
 333         SET_MOXA_MUST_XON1_VALUE(io, 0x11);
 
 334         if ((hwid = inb(io + UART_MCR)) != 0) {
 
 335                 outb(oldmcr, io + UART_MCR);
 
 336                 return MOXA_OTHER_UART;
 
 339         GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
 
 340         for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
 
 341                 if (hwid == Gpci_uart_info[i].type)
 
 344         return MOXA_OTHER_UART;
 
 348 static void process_txrx_fifo(struct mxser_port *info)
 
 352         if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
 
 353                 info->rx_trigger = 1;
 
 354                 info->rx_high_water = 1;
 
 355                 info->rx_low_water = 1;
 
 356                 info->xmit_fifo_size = 1;
 
 358                 for (i = 0; i < UART_INFO_NUM; i++)
 
 359                         if (info->board->chip_flag == Gpci_uart_info[i].type) {
 
 360                                 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
 
 361                                 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
 
 362                                 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
 
 363                                 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
 
 368 static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
 
 370         unsigned char status = 0;
 
 372         status = inb(baseaddr + UART_MSR);
 
 374         mxser_msr[port] &= 0x0F;
 
 375         mxser_msr[port] |= status;
 
 376         status = mxser_msr[port];
 
 383 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
 
 384                 struct mxser_port *port)
 
 386         DECLARE_WAITQUEUE(wait, current);
 
 392          * If non-blocking mode is set, or the port is not enabled,
 
 393          * then make the check up front and then exit.
 
 395         if ((filp->f_flags & O_NONBLOCK) ||
 
 396                         test_bit(TTY_IO_ERROR, &tty->flags)) {
 
 397                 port->flags |= ASYNC_NORMAL_ACTIVE;
 
 401         if (tty->termios->c_cflag & CLOCAL)
 
 405          * Block waiting for the carrier detect and the line to become
 
 406          * free (i.e., not in use by the callout).  While we are in
 
 407          * this loop, port->count is dropped by one, so that
 
 408          * mxser_close() knows when to free things.  We restore it upon
 
 409          * exit, either normal or abnormal.
 
 412         add_wait_queue(&port->open_wait, &wait);
 
 414         spin_lock_irqsave(&port->slock, flags);
 
 415         if (!tty_hung_up_p(filp))
 
 417         spin_unlock_irqrestore(&port->slock, flags);
 
 418         port->blocked_open++;
 
 420                 spin_lock_irqsave(&port->slock, flags);
 
 421                 outb(inb(port->ioaddr + UART_MCR) |
 
 422                         UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
 
 423                 spin_unlock_irqrestore(&port->slock, flags);
 
 424                 set_current_state(TASK_INTERRUPTIBLE);
 
 425                 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
 
 426                         if (port->flags & ASYNC_HUP_NOTIFY)
 
 429                                 retval = -ERESTARTSYS;
 
 432                 if (!(port->flags & ASYNC_CLOSING) &&
 
 434                                 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
 
 436                 if (signal_pending(current)) {
 
 437                         retval = -ERESTARTSYS;
 
 442         set_current_state(TASK_RUNNING);
 
 443         remove_wait_queue(&port->open_wait, &wait);
 
 444         if (!tty_hung_up_p(filp))
 
 446         port->blocked_open--;
 
 449         port->flags |= ASYNC_NORMAL_ACTIVE;
 
 453 static int mxser_set_baud(struct mxser_port *info, long newspd)
 
 460         if (!info->tty || !info->tty->termios)
 
 466         if (newspd > info->max_baud)
 
 469         info->realbaud = newspd;
 
 470         for (i = 0; i < BAUD_TABLE_NO; i++)
 
 471                if (newspd == mxvar_baud_table[i])
 
 473         if (i == BAUD_TABLE_NO) {
 
 474                 quot = info->baud_base / info->speed;
 
 475                 if (info->speed <= 0 || info->speed > info->max_baud)
 
 479                         quot = (2 * info->baud_base / 269);
 
 481                         quot = info->baud_base / newspd;
 
 489         info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
 
 490         info->timeout += HZ / 50;       /* Add .02 seconds of slop */
 
 493                 info->MCR |= UART_MCR_DTR;
 
 494                 outb(info->MCR, info->ioaddr + UART_MCR);
 
 496                 info->MCR &= ~UART_MCR_DTR;
 
 497                 outb(info->MCR, info->ioaddr + UART_MCR);
 
 501         cval = inb(info->ioaddr + UART_LCR);
 
 503         outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR);    /* set DLAB */
 
 505         outb(quot & 0xff, info->ioaddr + UART_DLL);     /* LS of divisor */
 
 506         outb(quot >> 8, info->ioaddr + UART_DLM);       /* MS of divisor */
 
 507         outb(cval, info->ioaddr + UART_LCR);    /* reset DLAB */
 
 509         if (i == BAUD_TABLE_NO) {
 
 510                 quot = info->baud_base % info->speed;
 
 512                 if ((quot % info->speed) > (info->speed / 2)) {
 
 518                 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
 
 520                 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
 
 526  * This routine is called to set the UART divisor registers to match
 
 527  * the specified baud rate for a serial port.
 
 529 static int mxser_change_speed(struct mxser_port *info,
 
 530                 struct ktermios *old_termios)
 
 532         unsigned cflag, cval, fcr;
 
 534         unsigned char status;
 
 537         if (!info->tty || !info->tty->termios)
 
 539         cflag = info->tty->termios->c_cflag;
 
 543         if (mxser_set_baud_method[info->tty->index] == 0) {
 
 544                 if ((cflag & CBAUD) == B_SPEC)
 
 547                         baud = tty_get_baud_rate(info->tty);
 
 548                 mxser_set_baud(info, baud);
 
 551         /* byte size and parity */
 
 552         switch (cflag & CSIZE) {
 
 567                 break;          /* too keep GCC shut... */
 
 572                 cval |= UART_LCR_PARITY;
 
 573         if (!(cflag & PARODD))
 
 574                 cval |= UART_LCR_EPAR;
 
 576                 cval |= UART_LCR_SPAR;
 
 578         if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
 
 579                 if (info->board->chip_flag) {
 
 580                         fcr = UART_FCR_ENABLE_FIFO;
 
 581                         fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
 
 582                         SET_MOXA_MUST_FIFO_VALUE(info);
 
 586                 fcr = UART_FCR_ENABLE_FIFO;
 
 587                 if (info->board->chip_flag) {
 
 588                         fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
 
 589                         SET_MOXA_MUST_FIFO_VALUE(info);
 
 591                         switch (info->rx_trigger) {
 
 593                                 fcr |= UART_FCR_TRIGGER_1;
 
 596                                 fcr |= UART_FCR_TRIGGER_4;
 
 599                                 fcr |= UART_FCR_TRIGGER_8;
 
 602                                 fcr |= UART_FCR_TRIGGER_14;
 
 608         /* CTS flow control flag and modem status interrupts */
 
 609         info->IER &= ~UART_IER_MSI;
 
 610         info->MCR &= ~UART_MCR_AFE;
 
 611         if (cflag & CRTSCTS) {
 
 612                 info->flags |= ASYNC_CTS_FLOW;
 
 613                 info->IER |= UART_IER_MSI;
 
 614                 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
 
 615                         info->MCR |= UART_MCR_AFE;
 
 617                         status = inb(info->ioaddr + UART_MSR);
 
 618                         if (info->tty->hw_stopped) {
 
 619                                 if (status & UART_MSR_CTS) {
 
 620                                         info->tty->hw_stopped = 0;
 
 621                                         if (info->type != PORT_16550A &&
 
 622                                                         !info->board->chip_flag) {
 
 623                                                 outb(info->IER & ~UART_IER_THRI,
 
 626                                                 info->IER |= UART_IER_THRI;
 
 627                                                 outb(info->IER, info->ioaddr +
 
 630                                         tty_wakeup(info->tty);
 
 633                                 if (!(status & UART_MSR_CTS)) {
 
 634                                         info->tty->hw_stopped = 1;
 
 635                                         if ((info->type != PORT_16550A) &&
 
 636                                                         (!info->board->chip_flag)) {
 
 637                                                 info->IER &= ~UART_IER_THRI;
 
 638                                                 outb(info->IER, info->ioaddr +
 
 645                 info->flags &= ~ASYNC_CTS_FLOW;
 
 647         outb(info->MCR, info->ioaddr + UART_MCR);
 
 648         if (cflag & CLOCAL) {
 
 649                 info->flags &= ~ASYNC_CHECK_CD;
 
 651                 info->flags |= ASYNC_CHECK_CD;
 
 652                 info->IER |= UART_IER_MSI;
 
 654         outb(info->IER, info->ioaddr + UART_IER);
 
 657          * Set up parity check flag
 
 659         info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
 
 660         if (I_INPCK(info->tty))
 
 661                 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
 
 662         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
 
 663                 info->read_status_mask |= UART_LSR_BI;
 
 665         info->ignore_status_mask = 0;
 
 667         if (I_IGNBRK(info->tty)) {
 
 668                 info->ignore_status_mask |= UART_LSR_BI;
 
 669                 info->read_status_mask |= UART_LSR_BI;
 
 671                  * If we're ignore parity and break indicators, ignore
 
 672                  * overruns too.  (For real raw support).
 
 674                 if (I_IGNPAR(info->tty)) {
 
 675                         info->ignore_status_mask |=
 
 679                         info->read_status_mask |=
 
 685         if (info->board->chip_flag) {
 
 686                 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
 
 687                 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
 
 688                 if (I_IXON(info->tty)) {
 
 689                         ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
 691                         DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
 693                 if (I_IXOFF(info->tty)) {
 
 694                         ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
 696                         DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
 701         outb(fcr, info->ioaddr + UART_FCR);     /* set fcr */
 
 702         outb(cval, info->ioaddr + UART_LCR);
 
 707 static void mxser_check_modem_status(struct mxser_port *port, int status)
 
 709         /* update input line counters */
 
 710         if (status & UART_MSR_TERI)
 
 712         if (status & UART_MSR_DDSR)
 
 714         if (status & UART_MSR_DDCD)
 
 716         if (status & UART_MSR_DCTS)
 
 718         port->mon_data.modem_status = status;
 
 719         wake_up_interruptible(&port->delta_msr_wait);
 
 721         if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
 
 722                 if (status & UART_MSR_DCD)
 
 723                         wake_up_interruptible(&port->open_wait);
 
 726         if (port->flags & ASYNC_CTS_FLOW) {
 
 727                 if (port->tty->hw_stopped) {
 
 728                         if (status & UART_MSR_CTS) {
 
 729                                 port->tty->hw_stopped = 0;
 
 731                                 if ((port->type != PORT_16550A) &&
 
 732                                                 (!port->board->chip_flag)) {
 
 733                                         outb(port->IER & ~UART_IER_THRI,
 
 734                                                 port->ioaddr + UART_IER);
 
 735                                         port->IER |= UART_IER_THRI;
 
 736                                         outb(port->IER, port->ioaddr +
 
 739                                 tty_wakeup(port->tty);
 
 742                         if (!(status & UART_MSR_CTS)) {
 
 743                                 port->tty->hw_stopped = 1;
 
 744                                 if (port->type != PORT_16550A &&
 
 745                                                 !port->board->chip_flag) {
 
 746                                         port->IER &= ~UART_IER_THRI;
 
 747                                         outb(port->IER, port->ioaddr +
 
 755 static int mxser_startup(struct mxser_port *info)
 
 760         page = __get_free_page(GFP_KERNEL);
 
 764         spin_lock_irqsave(&info->slock, flags);
 
 766         if (info->flags & ASYNC_INITIALIZED) {
 
 768                 spin_unlock_irqrestore(&info->slock, flags);
 
 772         if (!info->ioaddr || !info->type) {
 
 774                         set_bit(TTY_IO_ERROR, &info->tty->flags);
 
 776                 spin_unlock_irqrestore(&info->slock, flags);
 
 782                 info->xmit_buf = (unsigned char *) page;
 
 785          * Clear the FIFO buffers and disable them
 
 786          * (they will be reenabled in mxser_change_speed())
 
 788         if (info->board->chip_flag)
 
 789                 outb((UART_FCR_CLEAR_RCVR |
 
 790                         UART_FCR_CLEAR_XMIT |
 
 791                         MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
 
 793                 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
 
 794                         info->ioaddr + UART_FCR);
 
 797          * At this point there's no way the LSR could still be 0xFF;
 
 798          * if it is, then bail out, because there's likely no UART
 
 801         if (inb(info->ioaddr + UART_LSR) == 0xff) {
 
 802                 spin_unlock_irqrestore(&info->slock, flags);
 
 803                 if (capable(CAP_SYS_ADMIN)) {
 
 805                                 set_bit(TTY_IO_ERROR, &info->tty->flags);
 
 812          * Clear the interrupt registers.
 
 814         (void) inb(info->ioaddr + UART_LSR);
 
 815         (void) inb(info->ioaddr + UART_RX);
 
 816         (void) inb(info->ioaddr + UART_IIR);
 
 817         (void) inb(info->ioaddr + UART_MSR);
 
 820          * Now, initialize the UART
 
 822         outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR);  /* reset DLAB */
 
 823         info->MCR = UART_MCR_DTR | UART_MCR_RTS;
 
 824         outb(info->MCR, info->ioaddr + UART_MCR);
 
 827          * Finally, enable interrupts
 
 829         info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
 
 831         if (info->board->chip_flag)
 
 832                 info->IER |= MOXA_MUST_IER_EGDAI;
 
 833         outb(info->IER, info->ioaddr + UART_IER);       /* enable interrupts */
 
 836          * And clear the interrupt registers again for luck.
 
 838         (void) inb(info->ioaddr + UART_LSR);
 
 839         (void) inb(info->ioaddr + UART_RX);
 
 840         (void) inb(info->ioaddr + UART_IIR);
 
 841         (void) inb(info->ioaddr + UART_MSR);
 
 844                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
 
 845         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
 
 848          * and set the speed of the serial port
 
 850         mxser_change_speed(info, NULL);
 
 851         info->flags |= ASYNC_INITIALIZED;
 
 852         spin_unlock_irqrestore(&info->slock, flags);
 
 858  * This routine will shutdown a serial port; interrupts maybe disabled, and
 
 859  * DTR is dropped if the hangup on close termio flag is on.
 
 861 static void mxser_shutdown(struct mxser_port *info)
 
 865         if (!(info->flags & ASYNC_INITIALIZED))
 
 868         spin_lock_irqsave(&info->slock, flags);
 
 871          * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
 
 872          * here so the queue might never be waken up
 
 874         wake_up_interruptible(&info->delta_msr_wait);
 
 877          * Free the IRQ, if necessary
 
 879         if (info->xmit_buf) {
 
 880                 free_page((unsigned long) info->xmit_buf);
 
 881                 info->xmit_buf = NULL;
 
 885         outb(0x00, info->ioaddr + UART_IER);
 
 887         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
 
 888                 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
 
 889         outb(info->MCR, info->ioaddr + UART_MCR);
 
 891         /* clear Rx/Tx FIFO's */
 
 892         if (info->board->chip_flag)
 
 893                 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
 
 894                                 MOXA_MUST_FCR_GDA_MODE_ENABLE,
 
 895                                 info->ioaddr + UART_FCR);
 
 897                 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
 
 898                         info->ioaddr + UART_FCR);
 
 900         /* read data port to reset things */
 
 901         (void) inb(info->ioaddr + UART_RX);
 
 904                 set_bit(TTY_IO_ERROR, &info->tty->flags);
 
 906         info->flags &= ~ASYNC_INITIALIZED;
 
 908         if (info->board->chip_flag)
 
 909                 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
 911         spin_unlock_irqrestore(&info->slock, flags);
 
 915  * This routine is called whenever a serial port is opened.  It
 
 916  * enables interrupts for a serial port, linking in its async structure into
 
 917  * the IRQ chain.   It also performs the serial-specific
 
 918  * initialization for the tty structure.
 
 920 static int mxser_open(struct tty_struct *tty, struct file *filp)
 
 922         struct mxser_port *info;
 
 927         if (line == MXSER_PORTS)
 
 929         if (line < 0 || line > MXSER_PORTS)
 
 931         info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
 
 935         tty->driver_data = info;
 
 938          * Start up serial port
 
 940         spin_lock_irqsave(&info->slock, flags);
 
 942         spin_unlock_irqrestore(&info->slock, flags);
 
 943         retval = mxser_startup(info);
 
 947         retval = mxser_block_til_ready(tty, filp, info);
 
 951         /* unmark here for very high baud rate (ex. 921600 bps) used */
 
 952         tty->low_latency = 1;
 
 957  * This routine is called when the serial port gets closed.  First, we
 
 958  * wait for the last remaining data to be sent.  Then, we unlink its
 
 959  * async structure from the interrupt chain if necessary, and we free
 
 960  * that IRQ if nothing is left in the chain.
 
 962 static void mxser_close(struct tty_struct *tty, struct file *filp)
 
 964         struct mxser_port *info = tty->driver_data;
 
 966         unsigned long timeout;
 
 969         if (tty->index == MXSER_PORTS)
 
 974         spin_lock_irqsave(&info->slock, flags);
 
 976         if (tty_hung_up_p(filp)) {
 
 977                 spin_unlock_irqrestore(&info->slock, flags);
 
 980         if ((tty->count == 1) && (info->count != 1)) {
 
 982                  * Uh, oh.  tty->count is 1, which means that the tty
 
 983                  * structure will be freed.  Info->count should always
 
 984                  * be one in these conditions.  If it's greater than
 
 985                  * one, we've got real problems, since it means the
 
 986                  * serial port won't be shutdown.
 
 988                 printk(KERN_ERR "mxser_close: bad serial port count; "
 
 989                         "tty->count is 1, info->count is %d\n", info->count);
 
 992         if (--info->count < 0) {
 
 993                 printk(KERN_ERR "mxser_close: bad serial port count for "
 
 994                         "ttys%d: %d\n", tty->index, info->count);
 
 998                 spin_unlock_irqrestore(&info->slock, flags);
 
1001         info->flags |= ASYNC_CLOSING;
 
1002         spin_unlock_irqrestore(&info->slock, flags);
 
1004          * Save the termios structure, since this port may have
 
1005          * separate termios for callout and dialin.
 
1007         if (info->flags & ASYNC_NORMAL_ACTIVE)
 
1008                 info->normal_termios = *tty->termios;
 
1010          * Now we wait for the transmit buffer to clear; and we notify
 
1011          * the line discipline to only process XON/XOFF characters.
 
1014         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
 
1015                 tty_wait_until_sent(tty, info->closing_wait);
 
1017          * At this point we stop accepting input.  To do this, we
 
1018          * disable the receive line status interrupts, and tell the
 
1019          * interrupt driver to stop checking the data ready bit in the
 
1020          * line status register.
 
1022         info->IER &= ~UART_IER_RLSI;
 
1023         if (info->board->chip_flag)
 
1024                 info->IER &= ~MOXA_MUST_RECV_ISR;
 
1026         if (info->flags & ASYNC_INITIALIZED) {
 
1027                 outb(info->IER, info->ioaddr + UART_IER);
 
1029                  * Before we drop DTR, make sure the UART transmitter
 
1030                  * has completely drained; this is especially
 
1031                  * important if there is a transmit FIFO!
 
1033                 timeout = jiffies + HZ;
 
1034                 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
 
1035                         schedule_timeout_interruptible(5);
 
1036                         if (time_after(jiffies, timeout))
 
1040         mxser_shutdown(info);
 
1042         if (tty->driver->flush_buffer)
 
1043                 tty->driver->flush_buffer(tty);
 
1045         tty_ldisc_flush(tty);
 
1050         if (info->blocked_open) {
 
1051                 if (info->close_delay)
 
1052                         schedule_timeout_interruptible(info->close_delay);
 
1053                 wake_up_interruptible(&info->open_wait);
 
1056         info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
 
1059 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
 
1062         struct mxser_port *info = tty->driver_data;
 
1063         unsigned long flags;
 
1065         if (!info->xmit_buf)
 
1069                 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
 
1070                                           SERIAL_XMIT_SIZE - info->xmit_head));
 
1074                 memcpy(info->xmit_buf + info->xmit_head, buf, c);
 
1075                 spin_lock_irqsave(&info->slock, flags);
 
1076                 info->xmit_head = (info->xmit_head + c) &
 
1077                                   (SERIAL_XMIT_SIZE - 1);
 
1078                 info->xmit_cnt += c;
 
1079                 spin_unlock_irqrestore(&info->slock, flags);
 
1086         if (info->xmit_cnt && !tty->stopped) {
 
1087                 if (!tty->hw_stopped ||
 
1088                                 (info->type == PORT_16550A) ||
 
1089                                 (info->board->chip_flag)) {
 
1090                         spin_lock_irqsave(&info->slock, flags);
 
1091                         outb(info->IER & ~UART_IER_THRI, info->ioaddr +
 
1093                         info->IER |= UART_IER_THRI;
 
1094                         outb(info->IER, info->ioaddr + UART_IER);
 
1095                         spin_unlock_irqrestore(&info->slock, flags);
 
1101 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
 
1103         struct mxser_port *info = tty->driver_data;
 
1104         unsigned long flags;
 
1106         if (!info->xmit_buf)
 
1109         if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
 
1112         spin_lock_irqsave(&info->slock, flags);
 
1113         info->xmit_buf[info->xmit_head++] = ch;
 
1114         info->xmit_head &= SERIAL_XMIT_SIZE - 1;
 
1116         spin_unlock_irqrestore(&info->slock, flags);
 
1117         if (!tty->stopped) {
 
1118                 if (!tty->hw_stopped ||
 
1119                                 (info->type == PORT_16550A) ||
 
1120                                 info->board->chip_flag) {
 
1121                         spin_lock_irqsave(&info->slock, flags);
 
1122                         outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
 
1123                         info->IER |= UART_IER_THRI;
 
1124                         outb(info->IER, info->ioaddr + UART_IER);
 
1125                         spin_unlock_irqrestore(&info->slock, flags);
 
1131 static void mxser_flush_chars(struct tty_struct *tty)
 
1133         struct mxser_port *info = tty->driver_data;
 
1134         unsigned long flags;
 
1136         if (info->xmit_cnt <= 0 ||
 
1140                          (info->type != PORT_16550A) &&
 
1141                          (!info->board->chip_flag)
 
1145         spin_lock_irqsave(&info->slock, flags);
 
1147         outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
 
1148         info->IER |= UART_IER_THRI;
 
1149         outb(info->IER, info->ioaddr + UART_IER);
 
1151         spin_unlock_irqrestore(&info->slock, flags);
 
1154 static int mxser_write_room(struct tty_struct *tty)
 
1156         struct mxser_port *info = tty->driver_data;
 
1159         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
 
1165 static int mxser_chars_in_buffer(struct tty_struct *tty)
 
1167         struct mxser_port *info = tty->driver_data;
 
1168         return info->xmit_cnt;
 
1171 static void mxser_flush_buffer(struct tty_struct *tty)
 
1173         struct mxser_port *info = tty->driver_data;
 
1175         unsigned long flags;
 
1178         spin_lock_irqsave(&info->slock, flags);
 
1179         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
 
1181         fcr = inb(info->ioaddr + UART_FCR);
 
1182         outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
 
1183                 info->ioaddr + UART_FCR);
 
1184         outb(fcr, info->ioaddr + UART_FCR);
 
1186         spin_unlock_irqrestore(&info->slock, flags);
 
1192  * ------------------------------------------------------------
 
1193  * friends of mxser_ioctl()
 
1194  * ------------------------------------------------------------
 
1196 static int mxser_get_serial_info(struct mxser_port *info,
 
1197                 struct serial_struct __user *retinfo)
 
1199         struct serial_struct tmp;
 
1203         memset(&tmp, 0, sizeof(tmp));
 
1204         tmp.type = info->type;
 
1205         tmp.line = info->tty->index;
 
1206         tmp.port = info->ioaddr;
 
1207         tmp.irq = info->board->irq;
 
1208         tmp.flags = info->flags;
 
1209         tmp.baud_base = info->baud_base;
 
1210         tmp.close_delay = info->close_delay;
 
1211         tmp.closing_wait = info->closing_wait;
 
1212         tmp.custom_divisor = info->custom_divisor;
 
1214         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
 
1219 static int mxser_set_serial_info(struct mxser_port *info,
 
1220                 struct serial_struct __user *new_info)
 
1222         struct serial_struct new_serial;
 
1223         unsigned long sl_flags;
 
1227         if (!new_info || !info->ioaddr)
 
1229         if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
 
1232         if ((new_serial.irq != info->board->irq) ||
 
1233                         (new_serial.port != info->ioaddr) ||
 
1234                         (new_serial.custom_divisor != info->custom_divisor) ||
 
1235                         (new_serial.baud_base != info->baud_base))
 
1238         flags = info->flags & ASYNC_SPD_MASK;
 
1240         if (!capable(CAP_SYS_ADMIN)) {
 
1241                 if ((new_serial.baud_base != info->baud_base) ||
 
1242                                 (new_serial.close_delay != info->close_delay) ||
 
1243                                 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
 
1245                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
 
1246                                 (new_serial.flags & ASYNC_USR_MASK));
 
1249                  * OK, past this point, all the error checking has been done.
 
1250                  * At this point, we start making changes.....
 
1252                 info->flags = ((info->flags & ~ASYNC_FLAGS) |
 
1253                                 (new_serial.flags & ASYNC_FLAGS));
 
1254                 info->close_delay = new_serial.close_delay * HZ / 100;
 
1255                 info->closing_wait = new_serial.closing_wait * HZ / 100;
 
1256                 info->tty->low_latency =
 
1257                                 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
 
1258                 info->tty->low_latency = 0;
 
1261         info->type = new_serial.type;
 
1263         process_txrx_fifo(info);
 
1265         if (info->flags & ASYNC_INITIALIZED) {
 
1266                 if (flags != (info->flags & ASYNC_SPD_MASK)) {
 
1267                         spin_lock_irqsave(&info->slock, sl_flags);
 
1268                         mxser_change_speed(info, NULL);
 
1269                         spin_unlock_irqrestore(&info->slock, sl_flags);
 
1272                 retval = mxser_startup(info);
 
1278  * mxser_get_lsr_info - get line status register info
 
1280  * Purpose: Let user call ioctl() to get info when the UART physically
 
1281  *          is emptied.  On bus types like RS485, the transmitter must
 
1282  *          release the bus after transmitting. This must be done when
 
1283  *          the transmit shift register is empty, not be done when the
 
1284  *          transmit holding register is empty.  This functionality
 
1285  *          allows an RS485 driver to be written in user space.
 
1287 static int mxser_get_lsr_info(struct mxser_port *info,
 
1288                 unsigned int __user *value)
 
1290         unsigned char status;
 
1291         unsigned int result;
 
1292         unsigned long flags;
 
1294         spin_lock_irqsave(&info->slock, flags);
 
1295         status = inb(info->ioaddr + UART_LSR);
 
1296         spin_unlock_irqrestore(&info->slock, flags);
 
1297         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
 
1298         return put_user(result, value);
 
1302  * This routine sends a break character out the serial port.
 
1304 static void mxser_send_break(struct mxser_port *info, int duration)
 
1306         unsigned long flags;
 
1310         set_current_state(TASK_INTERRUPTIBLE);
 
1311         spin_lock_irqsave(&info->slock, flags);
 
1312         outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
 
1313                 info->ioaddr + UART_LCR);
 
1314         spin_unlock_irqrestore(&info->slock, flags);
 
1315         schedule_timeout(duration);
 
1316         spin_lock_irqsave(&info->slock, flags);
 
1317         outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
 
1318                 info->ioaddr + UART_LCR);
 
1319         spin_unlock_irqrestore(&info->slock, flags);
 
1322 static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
 
1324         struct mxser_port *info = tty->driver_data;
 
1325         unsigned char control, status;
 
1326         unsigned long flags;
 
1329         if (tty->index == MXSER_PORTS)
 
1330                 return -ENOIOCTLCMD;
 
1331         if (test_bit(TTY_IO_ERROR, &tty->flags))
 
1334         control = info->MCR;
 
1336         spin_lock_irqsave(&info->slock, flags);
 
1337         status = inb(info->ioaddr + UART_MSR);
 
1338         if (status & UART_MSR_ANY_DELTA)
 
1339                 mxser_check_modem_status(info, status);
 
1340         spin_unlock_irqrestore(&info->slock, flags);
 
1341         return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
 
1342                     ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
 
1343                     ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
 
1344                     ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
 
1345                     ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
 
1346                     ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
 
1349 static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
 
1350                 unsigned int set, unsigned int clear)
 
1352         struct mxser_port *info = tty->driver_data;
 
1353         unsigned long flags;
 
1356         if (tty->index == MXSER_PORTS)
 
1357                 return -ENOIOCTLCMD;
 
1358         if (test_bit(TTY_IO_ERROR, &tty->flags))
 
1361         spin_lock_irqsave(&info->slock, flags);
 
1363         if (set & TIOCM_RTS)
 
1364                 info->MCR |= UART_MCR_RTS;
 
1365         if (set & TIOCM_DTR)
 
1366                 info->MCR |= UART_MCR_DTR;
 
1368         if (clear & TIOCM_RTS)
 
1369                 info->MCR &= ~UART_MCR_RTS;
 
1370         if (clear & TIOCM_DTR)
 
1371                 info->MCR &= ~UART_MCR_DTR;
 
1373         outb(info->MCR, info->ioaddr + UART_MCR);
 
1374         spin_unlock_irqrestore(&info->slock, flags);
 
1378 static int __init mxser_program_mode(int port)
 
1390         id = inb(port + 1) & 0x1F;
 
1391         if ((id != C168_ASIC_ID) &&
 
1392                         (id != C104_ASIC_ID) &&
 
1393                         (id != C102_ASIC_ID) &&
 
1394                         (id != CI132_ASIC_ID) &&
 
1395                         (id != CI134_ASIC_ID) &&
 
1396                         (id != CI104J_ASIC_ID))
 
1398         for (i = 0, j = 0; i < 4; i++) {
 
1402                 } else if ((j == 1) && (n == 1)) {
 
1413 static void __init mxser_normal_mode(int port)
 
1417         outb(0xA5, port + 1);
 
1418         outb(0x80, port + 3);
 
1419         outb(12, port + 0);     /* 9600 bps */
 
1421         outb(0x03, port + 3);   /* 8 data bits */
 
1422         outb(0x13, port + 4);   /* loop back mode */
 
1423         for (i = 0; i < 16; i++) {
 
1425                 if ((n & 0x61) == 0x60)
 
1430         outb(0x00, port + 4);
 
1433 #define CHIP_SK         0x01    /* Serial Data Clock  in Eprom */
 
1434 #define CHIP_DO         0x02    /* Serial Data Output in Eprom */
 
1435 #define CHIP_CS         0x04    /* Serial Chip Select in Eprom */
 
1436 #define CHIP_DI         0x08    /* Serial Data Input  in Eprom */
 
1437 #define EN_CCMD         0x000   /* Chip's command register     */
 
1438 #define EN0_RSARLO      0x008   /* Remote start address reg 0  */
 
1439 #define EN0_RSARHI      0x009   /* Remote start address reg 1  */
 
1440 #define EN0_RCNTLO      0x00A   /* Remote byte count reg WR    */
 
1441 #define EN0_RCNTHI      0x00B   /* Remote byte count reg WR    */
 
1442 #define EN0_DCFG        0x00E   /* Data configuration reg WR   */
 
1443 #define EN0_PORT        0x010   /* Rcv missed frame error counter RD */
 
1444 #define ENC_PAGE0       0x000   /* Select page 0 of chip registers   */
 
1445 #define ENC_PAGE3       0x0C0   /* Select page 3 of chip registers   */
 
1446 static int __init mxser_read_register(int port, unsigned short *regs)
 
1448         int i, k, value, id;
 
1451         id = mxser_program_mode(port);
 
1454         for (i = 0; i < 14; i++) {
 
1455                 k = (i & 0x3F) | 0x180;
 
1456                 for (j = 0x100; j > 0; j >>= 1) {
 
1457                         outb(CHIP_CS, port);
 
1459                                 outb(CHIP_CS | CHIP_DO, port);
 
1460                                 outb(CHIP_CS | CHIP_DO | CHIP_SK, port);        /* A? bit of read */
 
1462                                 outb(CHIP_CS, port);
 
1463                                 outb(CHIP_CS | CHIP_SK, port);  /* A? bit of read */
 
1468                 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
 
1469                         outb(CHIP_CS, port);
 
1470                         outb(CHIP_CS | CHIP_SK, port);
 
1471                         if (inb(port) & CHIP_DI)
 
1477         mxser_normal_mode(port);
 
1481 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
 
1483         struct mxser_port *port;
 
1489 /*              if (copy_to_user(argp, mxsercfg,
 
1490                                 sizeof(struct mxser_hwconf) * 4))
 
1494         case MOXA_GET_MAJOR:
 
1495                 if (copy_to_user(argp, &ttymajor, sizeof(int)))
 
1499         case MOXA_GET_CUMAJOR:
 
1500                 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
 
1504         case MOXA_CHKPORTENABLE:
 
1507                 for (i = 0; i < MXSER_BOARDS; i++)
 
1508                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
 
1509                                 if (mxser_boards[i].ports[j].ioaddr)
 
1512                 return put_user(result, (unsigned long __user *)argp);
 
1513         case MOXA_GETDATACOUNT:
 
1514                 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
 
1517         case MOXA_GETMSTATUS:
 
1518                 for (i = 0; i < MXSER_BOARDS; i++)
 
1519                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
 
1520                                 port = &mxser_boards[i].ports[j];
 
1523                                 if (!port->ioaddr) {
 
1524                                         GMStatus[i].dcd = 0;
 
1525                                         GMStatus[i].dsr = 0;
 
1526                                         GMStatus[i].cts = 0;
 
1530                                 if (!port->tty || !port->tty->termios)
 
1532                                                 port->normal_termios.c_cflag;
 
1535                                                 port->tty->termios->c_cflag;
 
1537                                 status = inb(port->ioaddr + UART_MSR);
 
1538                                 if (status & 0x80 /*UART_MSR_DCD */ )
 
1539                                         GMStatus[i].dcd = 1;
 
1541                                         GMStatus[i].dcd = 0;
 
1543                                 if (status & 0x20 /*UART_MSR_DSR */ )
 
1544                                         GMStatus[i].dsr = 1;
 
1546                                         GMStatus[i].dsr = 0;
 
1549                                 if (status & 0x10 /*UART_MSR_CTS */ )
 
1550                                         GMStatus[i].cts = 1;
 
1552                                         GMStatus[i].cts = 0;
 
1554                 if (copy_to_user(argp, GMStatus,
 
1555                                 sizeof(struct mxser_mstatus) * MXSER_PORTS))
 
1558         case MOXA_ASPP_MON_EXT: {
 
1560                 unsigned long opmode;
 
1561                 unsigned cflag, iflag;
 
1563                 for (i = 0; i < MXSER_BOARDS; i++)
 
1564                         for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
 
1565                                 port = &mxser_boards[i].ports[j];
 
1569                                 status = mxser_get_msr(port->ioaddr, 0, i);
 
1571                                 if (status & UART_MSR_TERI)
 
1573                                 if (status & UART_MSR_DDSR)
 
1575                                 if (status & UART_MSR_DDCD)
 
1577                                 if (status & UART_MSR_DCTS)
 
1580                                 port->mon_data.modem_status = status;
 
1581                                 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
 
1582                                 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
 
1583                                 mon_data_ext.up_rxcnt[i] =
 
1584                                         port->mon_data.up_rxcnt;
 
1585                                 mon_data_ext.up_txcnt[i] =
 
1586                                         port->mon_data.up_txcnt;
 
1587                                 mon_data_ext.modem_status[i] =
 
1588                                         port->mon_data.modem_status;
 
1589                                 mon_data_ext.baudrate[i] = port->realbaud;
 
1591                                 if (!port->tty || !port->tty->termios) {
 
1592                                         cflag = port->normal_termios.c_cflag;
 
1593                                         iflag = port->normal_termios.c_iflag;
 
1595                                         cflag = port->tty->termios->c_cflag;
 
1596                                         iflag = port->tty->termios->c_iflag;
 
1599                                 mon_data_ext.databits[i] = cflag & CSIZE;
 
1601                                 mon_data_ext.stopbits[i] = cflag & CSTOPB;
 
1603                                 mon_data_ext.parity[i] =
 
1604                                         cflag & (PARENB | PARODD | CMSPAR);
 
1606                                 mon_data_ext.flowctrl[i] = 0x00;
 
1608                                 if (cflag & CRTSCTS)
 
1609                                         mon_data_ext.flowctrl[i] |= 0x03;
 
1611                                 if (iflag & (IXON | IXOFF))
 
1612                                         mon_data_ext.flowctrl[i] |= 0x0C;
 
1614                                 if (port->type == PORT_16550A)
 
1615                                         mon_data_ext.fifo[i] = 1;
 
1617                                         mon_data_ext.fifo[i] = 0;
 
1621                                 opmode = inb(port->opmode_ioaddr) >> shiftbit;
 
1622                                 opmode &= OP_MODE_MASK;
 
1624                                 mon_data_ext.iftype[i] = opmode;
 
1627                         if (copy_to_user(argp, &mon_data_ext,
 
1628                                                 sizeof(mon_data_ext)))
 
1634                 return -ENOIOCTLCMD;
 
1639 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
 
1640                 unsigned int cmd, unsigned long arg)
 
1642         struct mxser_port *info = tty->driver_data;
 
1643         struct async_icount cprev, cnow;        /* kernel counter temps */
 
1644         struct serial_icounter_struct __user *p_cuser;
 
1645         unsigned long templ;
 
1646         unsigned long flags;
 
1648         void __user *argp = (void __user *)arg;
 
1651         if (tty->index == MXSER_PORTS)
 
1652                 return mxser_ioctl_special(cmd, argp);
 
1654         if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
 
1656                 unsigned long opmode;
 
1657                 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
 
1659                 unsigned char val, mask;
 
1662                 if (cmd == MOXA_SET_OP_MODE) {
 
1663                         if (get_user(opmode, (int __user *) argp))
 
1665                         if (opmode != RS232_MODE &&
 
1666                                         opmode != RS485_2WIRE_MODE &&
 
1667                                         opmode != RS422_MODE &&
 
1668                                         opmode != RS485_4WIRE_MODE)
 
1672                         val = inb(info->opmode_ioaddr);
 
1674                         val |= (opmode << shiftbit);
 
1675                         outb(val, info->opmode_ioaddr);
 
1678                         opmode = inb(info->opmode_ioaddr) >> shiftbit;
 
1679                         opmode &= OP_MODE_MASK;
 
1680                         if (copy_to_user(argp, &opmode, sizeof(int)))
 
1686         if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
 
1689                 if (get_user(speed, (int __user *)argp))
 
1691                 if (speed <= 0 || speed > info->max_baud)
 
1693                 if (!info->tty || !info->tty->termios || !info->ioaddr)
 
1695                 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
 
1696                 for (i = 0; i < BAUD_TABLE_NO; i++)
 
1697                         if (speed == mxvar_baud_table[i])
 
1699                 if (i == BAUD_TABLE_NO) {
 
1700                         info->tty->termios->c_cflag |= B_SPEC;
 
1701                 } else if (speed != 0)
 
1702                         info->tty->termios->c_cflag |= mxvar_baud_table1[i];
 
1704                 info->speed = speed;
 
1705                 spin_lock_irqsave(&info->slock, flags);
 
1706                 mxser_change_speed(info, NULL);
 
1707                 spin_unlock_irqrestore(&info->slock, flags);
 
1710         } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
 
1711                 if (copy_to_user(argp, &info->speed, sizeof(int)))
 
1716         if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
 
1717                         test_bit(TTY_IO_ERROR, &tty->flags))
 
1721         case TCSBRK:            /* SVID version: non-zero arg --> no break */
 
1722                 retval = tty_check_change(tty);
 
1725                 tty_wait_until_sent(tty, 0);
 
1727                         mxser_send_break(info, HZ / 4); /* 1/4 second */
 
1729         case TCSBRKP:           /* support for POSIX tcsendbreak() */
 
1730                 retval = tty_check_change(tty);
 
1733                 tty_wait_until_sent(tty, 0);
 
1734                 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
 
1737                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
 
1739                 if (get_user(templ, (unsigned long __user *) argp))
 
1742                 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
 
1745                 return mxser_get_serial_info(info, argp);
 
1747                 return mxser_set_serial_info(info, argp);
 
1748         case TIOCSERGETLSR:     /* Get line status register */
 
1749                 return mxser_get_lsr_info(info, argp);
 
1751                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
 
1752                  * - mask passed in arg for lines of interest
 
1753                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
 
1754                  * Caller should use TIOCGICOUNT to see which one it was
 
1757                 spin_lock_irqsave(&info->slock, flags);
 
1758                 cnow = info->icount;    /* note the counters on entry */
 
1759                 spin_unlock_irqrestore(&info->slock, flags);
 
1761                 wait_event_interruptible(info->delta_msr_wait, ({
 
1763                         spin_lock_irqsave(&info->slock, flags);
 
1764                         cnow = info->icount;    /* atomic copy */
 
1765                         spin_unlock_irqrestore(&info->slock, flags);
 
1767                         ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
 
1768                         ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
 
1769                         ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
 
1770                         ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
 
1774          * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
 
1775          * Return: write counters to the user passed counter struct
 
1776          * NB: both 1->0 and 0->1 transitions are counted except for
 
1777          *     RI where only 0->1 is counted.
 
1780                 spin_lock_irqsave(&info->slock, flags);
 
1781                 cnow = info->icount;
 
1782                 spin_unlock_irqrestore(&info->slock, flags);
 
1784                 if (put_user(cnow.frame, &p_cuser->frame))
 
1786                 if (put_user(cnow.brk, &p_cuser->brk))
 
1788                 if (put_user(cnow.overrun, &p_cuser->overrun))
 
1790                 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
 
1792                 if (put_user(cnow.parity, &p_cuser->parity))
 
1794                 if (put_user(cnow.rx, &p_cuser->rx))
 
1796                 if (put_user(cnow.tx, &p_cuser->tx))
 
1798                 put_user(cnow.cts, &p_cuser->cts);
 
1799                 put_user(cnow.dsr, &p_cuser->dsr);
 
1800                 put_user(cnow.rng, &p_cuser->rng);
 
1801                 put_user(cnow.dcd, &p_cuser->dcd);
 
1803         case MOXA_HighSpeedOn:
 
1804                 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
 
1805         case MOXA_SDS_RSTICOUNTER:
 
1806                 info->mon_data.rxcnt = 0;
 
1807                 info->mon_data.txcnt = 0;
 
1809         case MOXA_ASPP_SETBAUD:{
 
1811                 if (get_user(baud, (long __user *)argp))
 
1813                 spin_lock_irqsave(&info->slock, flags);
 
1814                 mxser_set_baud(info, baud);
 
1815                 spin_unlock_irqrestore(&info->slock, flags);
 
1818         case MOXA_ASPP_GETBAUD:
 
1819                 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
 
1824         case MOXA_ASPP_OQUEUE:{
 
1827                 len = mxser_chars_in_buffer(tty);
 
1829                 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
 
1831                 len += (lsr ? 0 : 1);
 
1833                 if (copy_to_user(argp, &len, sizeof(int)))
 
1838         case MOXA_ASPP_MON: {
 
1841                 status = mxser_get_msr(info->ioaddr, 1, tty->index);
 
1842                 mxser_check_modem_status(info, status);
 
1844                 mcr = inb(info->ioaddr + UART_MCR);
 
1845                 if (mcr & MOXA_MUST_MCR_XON_FLAG)
 
1846                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
 
1848                         info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
 
1850                 if (mcr & MOXA_MUST_MCR_TX_XON)
 
1851                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
 
1853                         info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
 
1855                 if (info->tty->hw_stopped)
 
1856                         info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
 
1858                         info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
 
1860                 if (copy_to_user(argp, &info->mon_data,
 
1861                                 sizeof(struct mxser_mon)))
 
1866         case MOXA_ASPP_LSTATUS: {
 
1867                 if (copy_to_user(argp, &info->err_shadow,
 
1868                                 sizeof(unsigned char)))
 
1871                 info->err_shadow = 0;
 
1874         case MOXA_SET_BAUD_METHOD: {
 
1877                 if (get_user(method, (int __user *)argp))
 
1879                 mxser_set_baud_method[tty->index] = method;
 
1880                 if (copy_to_user(argp, &method, sizeof(int)))
 
1886                 return -ENOIOCTLCMD;
 
1891 static void mxser_stoprx(struct tty_struct *tty)
 
1893         struct mxser_port *info = tty->driver_data;
 
1895         info->ldisc_stop_rx = 1;
 
1897                 if (info->board->chip_flag) {
 
1898                         info->IER &= ~MOXA_MUST_RECV_ISR;
 
1899                         outb(info->IER, info->ioaddr + UART_IER);
 
1901                         info->x_char = STOP_CHAR(tty);
 
1902                         outb(0, info->ioaddr + UART_IER);
 
1903                         info->IER |= UART_IER_THRI;
 
1904                         outb(info->IER, info->ioaddr + UART_IER);
 
1908         if (info->tty->termios->c_cflag & CRTSCTS) {
 
1909                 info->MCR &= ~UART_MCR_RTS;
 
1910                 outb(info->MCR, info->ioaddr + UART_MCR);
 
1915  * This routine is called by the upper-layer tty layer to signal that
 
1916  * incoming characters should be throttled.
 
1918 static void mxser_throttle(struct tty_struct *tty)
 
1923 static void mxser_unthrottle(struct tty_struct *tty)
 
1925         struct mxser_port *info = tty->driver_data;
 
1928         info->ldisc_stop_rx = 0;
 
1933                         if (info->board->chip_flag) {
 
1934                                 info->IER |= MOXA_MUST_RECV_ISR;
 
1935                                 outb(info->IER, info->ioaddr + UART_IER);
 
1937                                 info->x_char = START_CHAR(tty);
 
1938                                 outb(0, info->ioaddr + UART_IER);
 
1939                                 info->IER |= UART_IER_THRI;
 
1940                                 outb(info->IER, info->ioaddr + UART_IER);
 
1945         if (info->tty->termios->c_cflag & CRTSCTS) {
 
1946                 info->MCR |= UART_MCR_RTS;
 
1947                 outb(info->MCR, info->ioaddr + UART_MCR);
 
1952  * mxser_stop() and mxser_start()
 
1954  * This routines are called before setting or resetting tty->stopped.
 
1955  * They enable or disable transmitter interrupts, as necessary.
 
1957 static void mxser_stop(struct tty_struct *tty)
 
1959         struct mxser_port *info = tty->driver_data;
 
1960         unsigned long flags;
 
1962         spin_lock_irqsave(&info->slock, flags);
 
1963         if (info->IER & UART_IER_THRI) {
 
1964                 info->IER &= ~UART_IER_THRI;
 
1965                 outb(info->IER, info->ioaddr + UART_IER);
 
1967         spin_unlock_irqrestore(&info->slock, flags);
 
1970 static void mxser_start(struct tty_struct *tty)
 
1972         struct mxser_port *info = tty->driver_data;
 
1973         unsigned long flags;
 
1975         spin_lock_irqsave(&info->slock, flags);
 
1976         if (info->xmit_cnt && info->xmit_buf) {
 
1977                 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
 
1978                 info->IER |= UART_IER_THRI;
 
1979                 outb(info->IER, info->ioaddr + UART_IER);
 
1981         spin_unlock_irqrestore(&info->slock, flags);
 
1984 static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
 
1986         struct mxser_port *info = tty->driver_data;
 
1987         unsigned long flags;
 
1989         spin_lock_irqsave(&info->slock, flags);
 
1990         mxser_change_speed(info, old_termios);
 
1991         spin_unlock_irqrestore(&info->slock, flags);
 
1993         if ((old_termios->c_cflag & CRTSCTS) &&
 
1994                         !(tty->termios->c_cflag & CRTSCTS)) {
 
1995                 tty->hw_stopped = 0;
 
1999         /* Handle sw stopped */
 
2000         if ((old_termios->c_iflag & IXON) &&
 
2001                         !(tty->termios->c_iflag & IXON)) {
 
2004                 if (info->board->chip_flag) {
 
2005                         spin_lock_irqsave(&info->slock, flags);
 
2006                         DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
 
2007                         spin_unlock_irqrestore(&info->slock, flags);
 
2015  * mxser_wait_until_sent() --- wait until the transmitter is empty
 
2017 static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
 
2019         struct mxser_port *info = tty->driver_data;
 
2020         unsigned long orig_jiffies, char_time;
 
2023         if (info->type == PORT_UNKNOWN)
 
2026         if (info->xmit_fifo_size == 0)
 
2027                 return;         /* Just in case.... */
 
2029         orig_jiffies = jiffies;
 
2031          * Set the check interval to be 1/5 of the estimated time to
 
2032          * send a single character, and make it at least 1.  The check
 
2033          * interval should also be less than the timeout.
 
2035          * Note: we have to use pretty tight timings here to satisfy
 
2038         char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
 
2039         char_time = char_time / 5;
 
2042         if (timeout && timeout < char_time)
 
2043                 char_time = timeout;
 
2045          * If the transmitter hasn't cleared in twice the approximate
 
2046          * amount of time to send the entire FIFO, it probably won't
 
2047          * ever clear.  This assumes the UART isn't doing flow
 
2048          * control, which is currently the case.  Hence, if it ever
 
2049          * takes longer than info->timeout, this is probably due to a
 
2050          * UART bug of some kind.  So, we clamp the timeout parameter at
 
2053         if (!timeout || timeout > 2 * info->timeout)
 
2054                 timeout = 2 * info->timeout;
 
2055 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
 
2056         printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
 
2057                 timeout, char_time);
 
2058         printk("jiff=%lu...", jiffies);
 
2060         while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
 
2061 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
 
2062                 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
 
2064                 schedule_timeout_interruptible(char_time);
 
2065                 if (signal_pending(current))
 
2067                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
 
2070         set_current_state(TASK_RUNNING);
 
2072 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
 
2073         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
 
2078  * This routine is called by tty_hangup() when a hangup is signaled.
 
2080 static void mxser_hangup(struct tty_struct *tty)
 
2082         struct mxser_port *info = tty->driver_data;
 
2084         mxser_flush_buffer(tty);
 
2085         mxser_shutdown(info);
 
2088         info->flags &= ~ASYNC_NORMAL_ACTIVE;
 
2090         wake_up_interruptible(&info->open_wait);
 
2094  * mxser_rs_break() --- routine which turns the break handling on or off
 
2096 static void mxser_rs_break(struct tty_struct *tty, int break_state)
 
2098         struct mxser_port *info = tty->driver_data;
 
2099         unsigned long flags;
 
2101         spin_lock_irqsave(&info->slock, flags);
 
2102         if (break_state == -1)
 
2103                 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
 
2104                         info->ioaddr + UART_LCR);
 
2106                 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
 
2107                         info->ioaddr + UART_LCR);
 
2108         spin_unlock_irqrestore(&info->slock, flags);
 
2111 static void mxser_receive_chars(struct mxser_port *port, int *status)
 
2113         struct tty_struct *tty = port->tty;
 
2114         unsigned char ch, gdl;
 
2120         recv_room = tty->receive_room;
 
2121         if ((recv_room == 0) && (!port->ldisc_stop_rx))
 
2124         if (port->board->chip_flag != MOXA_OTHER_UART) {
 
2126                 if (*status & UART_LSR_SPECIAL)
 
2128                 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
 
2129                                 (*status & MOXA_MUST_LSR_RERR))
 
2131                 if (*status & MOXA_MUST_LSR_RERR)
 
2134                 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
 
2136                 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
 
2137                         gdl &= MOXA_MUST_GDL_MASK;
 
2138                 if (gdl >= recv_room) {
 
2139                         if (!port->ldisc_stop_rx)
 
2143                         ch = inb(port->ioaddr + UART_RX);
 
2144                         tty_insert_flip_char(tty, ch, 0);
 
2155                 ch = inb(port->ioaddr + UART_RX);
 
2156                 if (port->board->chip_flag && (*status & UART_LSR_OE))
 
2157                         outb(0x23, port->ioaddr + UART_FCR);
 
2158                 *status &= port->read_status_mask;
 
2159                 if (*status & port->ignore_status_mask) {
 
2160                         if (++ignored > 100)
 
2164                         if (*status & UART_LSR_SPECIAL) {
 
2165                                 if (*status & UART_LSR_BI) {
 
2169                                         if (port->flags & ASYNC_SAK)
 
2171                                 } else if (*status & UART_LSR_PE) {
 
2173                                         port->icount.parity++;
 
2174                                 } else if (*status & UART_LSR_FE) {
 
2176                                         port->icount.frame++;
 
2177                                 } else if (*status & UART_LSR_OE) {
 
2179                                         port->icount.overrun++;
 
2183                         tty_insert_flip_char(tty, ch, flag);
 
2185                         if (cnt >= recv_room) {
 
2186                                 if (!port->ldisc_stop_rx)
 
2193                 if (port->board->chip_flag)
 
2196                 *status = inb(port->ioaddr + UART_LSR);
 
2197         } while (*status & UART_LSR_DR);
 
2200         mxvar_log.rxcnt[port->tty->index] += cnt;
 
2201         port->mon_data.rxcnt += cnt;
 
2202         port->mon_data.up_rxcnt += cnt;
 
2205          * We are called from an interrupt context with &port->slock
 
2206          * being held. Drop it temporarily in order to prevent
 
2207          * recursive locking.
 
2209         spin_unlock(&port->slock);
 
2210         tty_flip_buffer_push(tty);
 
2211         spin_lock(&port->slock);
 
2214 static void mxser_transmit_chars(struct mxser_port *port)
 
2219                 outb(port->x_char, port->ioaddr + UART_TX);
 
2221                 mxvar_log.txcnt[port->tty->index]++;
 
2222                 port->mon_data.txcnt++;
 
2223                 port->mon_data.up_txcnt++;
 
2228         if (port->xmit_buf == 0)
 
2231         if ((port->xmit_cnt <= 0) || port->tty->stopped ||
 
2232                         (port->tty->hw_stopped &&
 
2233                         (port->type != PORT_16550A) &&
 
2234                         (!port->board->chip_flag))) {
 
2235                 port->IER &= ~UART_IER_THRI;
 
2236                 outb(port->IER, port->ioaddr + UART_IER);
 
2240         cnt = port->xmit_cnt;
 
2241         count = port->xmit_fifo_size;
 
2243                 outb(port->xmit_buf[port->xmit_tail++],
 
2244                         port->ioaddr + UART_TX);
 
2245                 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
 
2246                 if (--port->xmit_cnt <= 0)
 
2248         } while (--count > 0);
 
2249         mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
 
2251         port->mon_data.txcnt += (cnt - port->xmit_cnt);
 
2252         port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
 
2253         port->icount.tx += (cnt - port->xmit_cnt);
 
2255         if (port->xmit_cnt < WAKEUP_CHARS)
 
2256                 tty_wakeup(port->tty);
 
2258         if (port->xmit_cnt <= 0) {
 
2259                 port->IER &= ~UART_IER_THRI;
 
2260                 outb(port->IER, port->ioaddr + UART_IER);
 
2265  * This is the serial driver's generic interrupt routine
 
2267 static irqreturn_t mxser_interrupt(int irq, void *dev_id)
 
2270         struct mxser_board *brd = NULL;
 
2271         struct mxser_port *port;
 
2272         int max, irqbits, bits, msr;
 
2273         unsigned int int_cnt, pass_counter = 0;
 
2274         int handled = IRQ_NONE;
 
2276         for (i = 0; i < MXSER_BOARDS; i++)
 
2277                 if (dev_id == &mxser_boards[i]) {
 
2282         if (i == MXSER_BOARDS)
 
2286         max = brd->info->nports;
 
2287         while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
 
2288                 irqbits = inb(brd->vector) & brd->vector_mask;
 
2289                 if (irqbits == brd->vector_mask)
 
2292                 handled = IRQ_HANDLED;
 
2293                 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
 
2294                         if (irqbits == brd->vector_mask)
 
2298                         port = &brd->ports[i];
 
2301                         spin_lock(&port->slock);
 
2303                                 iir = inb(port->ioaddr + UART_IIR);
 
2304                                 if (iir & UART_IIR_NO_INT)
 
2306                                 iir &= MOXA_MUST_IIR_MASK;
 
2308                                                 (port->flags & ASYNC_CLOSING) ||
 
2310                                                         ASYNC_INITIALIZED)) {
 
2311                                         status = inb(port->ioaddr + UART_LSR);
 
2312                                         outb(0x27, port->ioaddr + UART_FCR);
 
2313                                         inb(port->ioaddr + UART_MSR);
 
2317                                 status = inb(port->ioaddr + UART_LSR);
 
2319                                 if (status & UART_LSR_PE)
 
2320                                         port->err_shadow |= NPPI_NOTIFY_PARITY;
 
2321                                 if (status & UART_LSR_FE)
 
2322                                         port->err_shadow |= NPPI_NOTIFY_FRAMING;
 
2323                                 if (status & UART_LSR_OE)
 
2325                                                 NPPI_NOTIFY_HW_OVERRUN;
 
2326                                 if (status & UART_LSR_BI)
 
2327                                         port->err_shadow |= NPPI_NOTIFY_BREAK;
 
2329                                 if (port->board->chip_flag) {
 
2330                                         if (iir == MOXA_MUST_IIR_GDA ||
 
2331                                             iir == MOXA_MUST_IIR_RDA ||
 
2332                                             iir == MOXA_MUST_IIR_RTO ||
 
2333                                             iir == MOXA_MUST_IIR_LSR)
 
2334                                                 mxser_receive_chars(port,
 
2338                                         status &= port->read_status_mask;
 
2339                                         if (status & UART_LSR_DR)
 
2340                                                 mxser_receive_chars(port,
 
2343                                 msr = inb(port->ioaddr + UART_MSR);
 
2344                                 if (msr & UART_MSR_ANY_DELTA)
 
2345                                         mxser_check_modem_status(port, msr);
 
2347                                 if (port->board->chip_flag) {
 
2348                                         if (iir == 0x02 && (status &
 
2350                                                 mxser_transmit_chars(port);
 
2352                                         if (status & UART_LSR_THRE)
 
2353                                                 mxser_transmit_chars(port);
 
2355                         } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
 
2356                         spin_unlock(&port->slock);
 
2364 static const struct tty_operations mxser_ops = {
 
2366         .close = mxser_close,
 
2367         .write = mxser_write,
 
2368         .put_char = mxser_put_char,
 
2369         .flush_chars = mxser_flush_chars,
 
2370         .write_room = mxser_write_room,
 
2371         .chars_in_buffer = mxser_chars_in_buffer,
 
2372         .flush_buffer = mxser_flush_buffer,
 
2373         .ioctl = mxser_ioctl,
 
2374         .throttle = mxser_throttle,
 
2375         .unthrottle = mxser_unthrottle,
 
2376         .set_termios = mxser_set_termios,
 
2378         .start = mxser_start,
 
2379         .hangup = mxser_hangup,
 
2380         .break_ctl = mxser_rs_break,
 
2381         .wait_until_sent = mxser_wait_until_sent,
 
2382         .tiocmget = mxser_tiocmget,
 
2383         .tiocmset = mxser_tiocmset,
 
2387  * The MOXA Smartio/Industio serial driver boot-time initialization code!
 
2390 static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
 
2394                 free_irq(brd->irq, brd);
 
2395         if (pdev != NULL) {     /* PCI */
 
2397                 pci_release_region(pdev, 2);
 
2398                 pci_release_region(pdev, 3);
 
2401                 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
 
2402                 release_region(brd->vector, 1);
 
2406 static int __devinit mxser_initbrd(struct mxser_board *brd,
 
2407                 struct pci_dev *pdev)
 
2409         struct mxser_port *info;
 
2413         printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
 
2415         for (i = 0; i < brd->info->nports; i++) {
 
2416                 info = &brd->ports[i];
 
2419                 info->ldisc_stop_rx = 0;
 
2421                 /* Enhance mode enabled here */
 
2422                 if (brd->chip_flag != MOXA_OTHER_UART)
 
2423                         ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
 
2425                 info->flags = ASYNC_SHARE_IRQ;
 
2426                 info->type = brd->uart_type;
 
2428                 process_txrx_fifo(info);
 
2430                 info->custom_divisor = info->baud_base * 16;
 
2431                 info->close_delay = 5 * HZ / 10;
 
2432                 info->closing_wait = 30 * HZ;
 
2433                 info->normal_termios = mxvar_sdriver->init_termios;
 
2434                 init_waitqueue_head(&info->open_wait);
 
2435                 init_waitqueue_head(&info->delta_msr_wait);
 
2437                 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
 
2438                 info->err_shadow = 0;
 
2439                 spin_lock_init(&info->slock);
 
2441                 /* before set INT ISR, disable all int */
 
2442                 outb(inb(info->ioaddr + UART_IER) & 0xf0,
 
2443                         info->ioaddr + UART_IER);
 
2446         retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
 
2449                 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
 
2450                         "conflict with another device.\n",
 
2451                         brd->info->name, brd->irq);
 
2452                 /* We hold resources, we need to release them. */
 
2453                 mxser_release_res(brd, pdev, 0);
 
2458 static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
 
2461         unsigned short regs[16], irq;
 
2462         unsigned char scratch, scratch2;
 
2464         brd->chip_flag = MOXA_OTHER_UART;
 
2466         id = mxser_read_register(cap, regs);
 
2469                 brd->info = &mxser_cards[0];
 
2472                 brd->info = &mxser_cards[1];
 
2474         case CI104J_ASIC_ID:
 
2475                 brd->info = &mxser_cards[2];
 
2478                 brd->info = &mxser_cards[5];
 
2481                 brd->info = &mxser_cards[6];
 
2484                 brd->info = &mxser_cards[7];
 
2491         /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
 
2492            Flag-hack checks if configuration should be read as 2-port here. */
 
2493         if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
 
2494                 irq = regs[9] & 0xF000;
 
2495                 irq = irq | (irq >> 4);
 
2496                 if (irq != (regs[9] & 0xFF00))
 
2497                         return MXSER_ERR_IRQ_CONFLIT;
 
2498         } else if (brd->info->nports == 4) {
 
2499                 irq = regs[9] & 0xF000;
 
2500                 irq = irq | (irq >> 4);
 
2501                 irq = irq | (irq >> 8);
 
2503                         return MXSER_ERR_IRQ_CONFLIT;
 
2504         } else if (brd->info->nports == 8) {
 
2505                 irq = regs[9] & 0xF000;
 
2506                 irq = irq | (irq >> 4);
 
2507                 irq = irq | (irq >> 8);
 
2508                 if ((irq != regs[9]) || (irq != regs[10]))
 
2509                         return MXSER_ERR_IRQ_CONFLIT;
 
2513                 return MXSER_ERR_IRQ;
 
2514         brd->irq = ((int)(irq & 0xF000) >> 12);
 
2515         for (i = 0; i < 8; i++)
 
2516                 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
 
2517         if ((regs[12] & 0x80) == 0)
 
2518                 return MXSER_ERR_VECTOR;
 
2519         brd->vector = (int)regs[11];    /* interrupt vector */
 
2521                 brd->vector_mask = 0x00FF;
 
2523                 brd->vector_mask = 0x000F;
 
2524         for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
 
2525                 if (regs[12] & bits) {
 
2526                         brd->ports[i].baud_base = 921600;
 
2527                         brd->ports[i].max_baud = 921600;
 
2529                         brd->ports[i].baud_base = 115200;
 
2530                         brd->ports[i].max_baud = 115200;
 
2533         scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
 
2534         outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
 
2535         outb(0, cap + UART_EFR);        /* EFR is the same as FCR */
 
2536         outb(scratch2, cap + UART_LCR);
 
2537         outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
 
2538         scratch = inb(cap + UART_IIR);
 
2541                 brd->uart_type = PORT_16550A;
 
2543                 brd->uart_type = PORT_16450;
 
2544         if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
 
2546                 return MXSER_ERR_IOADDR;
 
2547         if (!request_region(brd->vector, 1, "mxser(vector)")) {
 
2548                 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
 
2549                 return MXSER_ERR_VECTOR;
 
2551         return brd->info->nports;
 
2554 static int __devinit mxser_probe(struct pci_dev *pdev,
 
2555                 const struct pci_device_id *ent)
 
2558         struct mxser_board *brd;
 
2560         unsigned long ioaddress;
 
2561         int retval = -EINVAL;
 
2563         for (i = 0; i < MXSER_BOARDS; i++)
 
2564                 if (mxser_boards[i].info == NULL)
 
2567         if (i >= MXSER_BOARDS) {
 
2568                 printk(KERN_ERR "Too many Smartio/Industio family boards found "
 
2569                         "(maximum %d), board not configured\n", MXSER_BOARDS);
 
2573         brd = &mxser_boards[i];
 
2574         brd->idx = i * MXSER_PORTS_PER_BOARD;
 
2575         printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
 
2576                 mxser_cards[ent->driver_data].name,
 
2577                 pdev->bus->number, PCI_SLOT(pdev->devfn));
 
2579         retval = pci_enable_device(pdev);
 
2581                 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
 
2586         ioaddress = pci_resource_start(pdev, 2);
 
2587         retval = pci_request_region(pdev, 2, "mxser(IO)");
 
2591         brd->info = &mxser_cards[ent->driver_data];
 
2592         for (i = 0; i < brd->info->nports; i++)
 
2593                 brd->ports[i].ioaddr = ioaddress + 8 * i;
 
2596         ioaddress = pci_resource_start(pdev, 3);
 
2597         retval = pci_request_region(pdev, 3, "mxser(vector)");
 
2600         brd->vector = ioaddress;
 
2603         brd->irq = pdev->irq;
 
2605         brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
 
2606         brd->uart_type = PORT_16550A;
 
2607         brd->vector_mask = 0;
 
2609         for (i = 0; i < brd->info->nports; i++) {
 
2610                 for (j = 0; j < UART_INFO_NUM; j++) {
 
2611                         if (Gpci_uart_info[j].type == brd->chip_flag) {
 
2612                                 brd->ports[i].max_baud =
 
2613                                         Gpci_uart_info[j].max_baud;
 
2615                                 /* exception....CP-102 */
 
2616                                 if (brd->info->flags & MXSER_HIGHBAUD)
 
2617                                         brd->ports[i].max_baud = 921600;
 
2623         if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
 
2624                 for (i = 0; i < brd->info->nports; i++) {
 
2626                                 brd->ports[i].opmode_ioaddr = ioaddress + 4;
 
2628                                 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
 
2630                 outb(0, ioaddress + 4); /* default set to RS232 mode */
 
2631                 outb(0, ioaddress + 0x0c);      /* default set to RS232 mode */
 
2634         for (i = 0; i < brd->info->nports; i++) {
 
2635                 brd->vector_mask |= (1 << i);
 
2636                 brd->ports[i].baud_base = 921600;
 
2639         /* mxser_initbrd will hook ISR. */
 
2640         retval = mxser_initbrd(brd, pdev);
 
2644         for (i = 0; i < brd->info->nports; i++)
 
2645                 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
 
2647         pci_set_drvdata(pdev, brd);
 
2651         pci_release_region(pdev, 2);
 
2661 static void __devexit mxser_remove(struct pci_dev *pdev)
 
2663         struct mxser_board *brd = pci_get_drvdata(pdev);
 
2666         for (i = 0; i < brd->info->nports; i++)
 
2667                 tty_unregister_device(mxvar_sdriver, brd->idx + i);
 
2669         mxser_release_res(brd, pdev, 1);
 
2673 static struct pci_driver mxser_driver = {
 
2675         .id_table = mxser_pcibrds,
 
2676         .probe = mxser_probe,
 
2677         .remove = __devexit_p(mxser_remove)
 
2680 static int __init mxser_module_init(void)
 
2682         struct mxser_board *brd;
 
2684         unsigned int i, m, isaloop;
 
2687         pr_debug("Loading module mxser ...\n");
 
2689         mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
 
2693         printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
 
2696         /* Initialize the tty_driver structure */
 
2697         mxvar_sdriver->owner = THIS_MODULE;
 
2698         mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
 
2699         mxvar_sdriver->name = "ttyMI";
 
2700         mxvar_sdriver->major = ttymajor;
 
2701         mxvar_sdriver->minor_start = 0;
 
2702         mxvar_sdriver->num = MXSER_PORTS + 1;
 
2703         mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
 
2704         mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
 
2705         mxvar_sdriver->init_termios = tty_std_termios;
 
2706         mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
 
2707         mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
 
2708         tty_set_operations(mxvar_sdriver, &mxser_ops);
 
2710         retval = tty_register_driver(mxvar_sdriver);
 
2712                 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
 
2720         /* Start finding ISA boards here */
 
2721         for (isaloop = 0; isaloop < 2; isaloop++)
 
2722                 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
 
2724                                 cap = mxserBoardCAP[b]; /* predefined */
 
2726                                 cap = ioaddr[b]; /* module param */
 
2731                         brd = &mxser_boards[m];
 
2732                         retval = mxser_get_ISA_conf(cap, brd);
 
2735                                 printk(KERN_INFO "Found MOXA %s board "
 
2737                                         brd->info->name, ioaddr[b]);
 
2740                                 if (retval == MXSER_ERR_IRQ)
 
2741                                         printk(KERN_ERR "Invalid interrupt "
 
2742                                                 "number, board not "
 
2744                                 else if (retval == MXSER_ERR_IRQ_CONFLIT)
 
2745                                         printk(KERN_ERR "Invalid interrupt "
 
2746                                                 "number, board not "
 
2748                                 else if (retval == MXSER_ERR_VECTOR)
 
2749                                         printk(KERN_ERR "Invalid interrupt "
 
2750                                                 "vector, board not "
 
2752                                 else if (retval == MXSER_ERR_IOADDR)
 
2753                                         printk(KERN_ERR "Invalid I/O address, "
 
2754                                                 "board not configured\n");
 
2760                         /* mxser_initbrd will hook ISR. */
 
2761                         if (mxser_initbrd(brd, NULL) < 0) {
 
2766                         brd->idx = m * MXSER_PORTS_PER_BOARD;
 
2767                         for (i = 0; i < brd->info->nports; i++)
 
2768                                 tty_register_device(mxvar_sdriver, brd->idx + i,
 
2774         retval = pci_register_driver(&mxser_driver);
 
2776                 printk(KERN_ERR "Can't register pci driver\n");
 
2780                 } /* else: we have some ISA cards under control */
 
2783         pr_debug("Done.\n");
 
2787         tty_unregister_driver(mxvar_sdriver);
 
2789         put_tty_driver(mxvar_sdriver);
 
2793 static void __exit mxser_module_exit(void)
 
2797         pr_debug("Unloading module mxser ...\n");
 
2799         pci_unregister_driver(&mxser_driver);
 
2801         for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
 
2802                 if (mxser_boards[i].info != NULL)
 
2803                         for (j = 0; j < mxser_boards[i].info->nports; j++)
 
2804                                 tty_unregister_device(mxvar_sdriver,
 
2805                                                 mxser_boards[i].idx + j);
 
2806         tty_unregister_driver(mxvar_sdriver);
 
2807         put_tty_driver(mxvar_sdriver);
 
2809         for (i = 0; i < MXSER_BOARDS; i++)
 
2810                 if (mxser_boards[i].info != NULL)
 
2811                         mxser_release_res(&mxser_boards[i], NULL, 1);
 
2813         pr_debug("Done.\n");
 
2816 module_init(mxser_module_init);
 
2817 module_exit(mxser_module_exit);