include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / char / moxa.c
index f737fbb..107b0bd 100644 (file)
@@ -2,7 +2,8 @@
 /*
  *           moxa.c  -- MOXA Intellio family multiport serial driver.
  *
- *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com.tw).
+ *      Copyright (C) 1999-2000  Moxa Technologies (support@moxa.com).
+ *      Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  *
  *      This code is loosely based on the Linux serial driver, written by
  *      Linus Torvalds, Theodore T'so and others.
@@ -42,7 +43,7 @@
 #include <linux/pci.h>
 #include <linux/init.h>
 #include <linux/bitops.h>
-#include <linux/completion.h>
+#include <linux/slab.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
 
 #include "moxa.h"
 
-#define MOXA_VERSION           "5.1k"
+#define MOXA_VERSION           "6.0k"
 
 #define MOXA_FW_HDRLEN         32
 
 #define MOXAMAJOR              172
-#define MOXACUMAJOR            173
 
 #define MAX_BOARDS             4       /* Don't change this value */
 #define MAX_PORTS_PER_BOARD    32      /* Don't change this value */
 #define MAX_PORTS              (MAX_BOARDS * MAX_PORTS_PER_BOARD)
 
+#define MOXA_IS_320(brd) ((brd)->boardType == MOXA_BOARD_C320_ISA || \
+               (brd)->boardType == MOXA_BOARD_C320_PCI)
+
 /*
  *    Define the Moxa PCI vendor and device IDs.
  */
@@ -128,26 +131,17 @@ struct moxaq_str {
 };
 
 struct moxa_port {
+       struct tty_port port;
        struct moxa_board_conf *board;
+       void __iomem *tableAddr;
+
        int type;
-       int close_delay;
-       int count;
-       int blocked_open;
-       int asyncflags;
-       unsigned long statusflags;
-       struct tty_struct *tty;
        int cflag;
-       wait_queue_head_t open_wait;
-       struct completion close_wait;
-
-       struct timer_list emptyTimer;
-
-       char lineCtrl;
-       void __iomem *tableAddr;
-       char DCDState;
-       char lowChkFlag;
+       unsigned long statusflags;
 
-       ushort breakCnt;
+       u8 DCDState;            /* Protected by the port lock */
+       u8 lineCtrl;
+       u8 lowChkFlag;
 };
 
 struct mon_str {
@@ -157,10 +151,9 @@ struct mon_str {
 };
 
 /* statusflags */
-#define TXSTOPPED      0x1
-#define LOWWAIT        0x2
-#define EMPTYWAIT      0x4
-#define THROTTLE       0x8
+#define TXSTOPPED      1
+#define LOWWAIT        2
+#define EMPTYWAIT      3
 
 #define SERIAL_DO_RESTART
 
@@ -169,24 +162,28 @@ struct mon_str {
 static int ttymajor = MOXAMAJOR;
 static struct mon_str moxaLog;
 static unsigned int moxaFuncTout = HZ / 2;
-/* Variables for insmod */
-#ifdef MODULE
+static unsigned int moxaLowWaterChk;
+static DEFINE_MUTEX(moxa_openlock);
+static DEFINE_SPINLOCK(moxa_lock);
+
 static unsigned long baseaddr[MAX_BOARDS];
 static unsigned int type[MAX_BOARDS];
 static unsigned int numports[MAX_BOARDS];
-#endif
 
 MODULE_AUTHOR("William Chen");
 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
 MODULE_LICENSE("GPL");
-#ifdef MODULE
+MODULE_FIRMWARE("c218tunx.cod");
+MODULE_FIRMWARE("cp204unx.cod");
+MODULE_FIRMWARE("c320tunx.cod");
+
 module_param_array(type, uint, NULL, 0);
 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
 module_param_array(baseaddr, ulong, NULL, 0);
 MODULE_PARM_DESC(baseaddr, "base address");
 module_param_array(numports, uint, NULL, 0);
 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
-#endif
+
 module_param(ttymajor, int, 0);
 
 /*
@@ -198,10 +195,6 @@ static int moxa_write(struct tty_struct *, const unsigned char *, int);
 static int moxa_write_room(struct tty_struct *);
 static void moxa_flush_buffer(struct tty_struct *);
 static int moxa_chars_in_buffer(struct tty_struct *);
-static void moxa_flush_chars(struct tty_struct *);
-static void moxa_put_char(struct tty_struct *, unsigned char);
-static void moxa_throttle(struct tty_struct *);
-static void moxa_unthrottle(struct tty_struct *);
 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
 static void moxa_stop(struct tty_struct *);
 static void moxa_start(struct tty_struct *);
@@ -211,16 +204,12 @@ static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
                         unsigned int set, unsigned int clear);
 static void moxa_poll(unsigned long);
 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
-static int moxa_block_till_ready(struct tty_struct *, struct file *,
-                           struct moxa_port *);
-static void moxa_setup_empty_event(struct tty_struct *);
-static void moxa_check_xmit_empty(unsigned long);
-static void moxa_shut_down(struct moxa_port *);
-static void moxa_receive_data(struct moxa_port *);
+static void moxa_shutdown(struct tty_port *);
+static int moxa_carrier_raised(struct tty_port *);
+static void moxa_dtr_rts(struct tty_port *, int);
 /*
  * moxa board interface functions:
  */
-static int MoxaDriverPoll(void);
 static void MoxaPortEnable(struct moxa_port *);
 static void MoxaPortDisable(struct moxa_port *);
 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
@@ -228,17 +217,14 @@ static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
 static int MoxaPortLineStatus(struct moxa_port *);
-static int MoxaPortDCDChange(struct moxa_port *);
-static int MoxaPortDCDON(struct moxa_port *);
 static void MoxaPortFlushData(struct moxa_port *, int);
-static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
-static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
+static int MoxaPortWriteData(struct tty_struct *, const unsigned char *, int);
+static int MoxaPortReadData(struct moxa_port *);
 static int MoxaPortTxQueue(struct moxa_port *);
 static int MoxaPortRxQueue(struct moxa_port *);
 static int MoxaPortTxFree(struct moxa_port *);
 static void MoxaPortTxDisable(struct moxa_port *);
 static void MoxaPortTxEnable(struct moxa_port *);
-static int MoxaPortResetBrkCnt(struct moxa_port *);
 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
 static void MoxaSetFifo(struct moxa_port *port, int enable);
@@ -247,6 +233,8 @@ static void MoxaSetFifo(struct moxa_port *port, int enable);
  * I/O functions
  */
 
+static DEFINE_SPINLOCK(moxafunc_lock);
+
 static void moxa_wait_finish(void __iomem *ofsAddr)
 {
        unsigned long end = jiffies + moxaFuncTout;
@@ -258,11 +246,41 @@ static void moxa_wait_finish(void __iomem *ofsAddr)
                printk(KERN_WARNING "moxa function expired\n");
 }
 
-static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
+static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
+{
+        unsigned long flags;
+        spin_lock_irqsave(&moxafunc_lock, flags);
+       writew(arg, ofsAddr + FuncArg);
+       writew(cmd, ofsAddr + FuncCode);
+       moxa_wait_finish(ofsAddr);
+       spin_unlock_irqrestore(&moxafunc_lock, flags);
+}
+
+static int moxafuncret(void __iomem *ofsAddr, u16 cmd, u16 arg)
 {
+        unsigned long flags;
+        u16 ret;
+        spin_lock_irqsave(&moxafunc_lock, flags);
        writew(arg, ofsAddr + FuncArg);
        writew(cmd, ofsAddr + FuncCode);
        moxa_wait_finish(ofsAddr);
+       ret = readw(ofsAddr + FuncArg);
+       spin_unlock_irqrestore(&moxafunc_lock, flags);
+       return ret;
+}
+
+static void moxa_low_water_check(void __iomem *ofsAddr)
+{
+       u16 rptr, wptr, mask, len;
+
+       if (readb(ofsAddr + FlagStat) & Xoff_state) {
+               rptr = readw(ofsAddr + RXrptr);
+               wptr = readw(ofsAddr + RXwptr);
+               mask = readw(ofsAddr + RX_mask);
+               len = (wptr - rptr) & mask;
+               if (len <= Low_water)
+                       moxafunc(ofsAddr, FC_SendXon, 0);
+       }
 }
 
 /*
@@ -274,7 +292,7 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
 {
        struct moxa_port *ch = tty->driver_data;
        void __user *argp = (void __user *)arg;
-       int status;
+       int status, ret = 0;
 
        if (tty->index == MAX_PORTS) {
                if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
@@ -286,11 +304,12 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
        switch (cmd) {
        case MOXA_GETDATACOUNT:
                moxaLog.tick = jiffies;
-               return copy_to_user(argp, &moxaLog, sizeof(moxaLog)) ?
-                       -EFAULT : 0;
+               if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
+                       ret = -EFAULT;
+               break;
        case MOXA_FLUSH_QUEUE:
                MoxaPortFlushData(ch, arg);
-               return 0;
+               break;
        case MOXA_GET_IOQUEUE: {
                struct moxaq_str __user *argm = argp;
                struct moxaq_str tmp;
@@ -301,21 +320,25 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
                        p = moxa_boards[i].ports;
                        for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
                                memset(&tmp, 0, sizeof(tmp));
+                               spin_lock_bh(&moxa_lock);
                                if (moxa_boards[i].ready) {
                                        tmp.inq = MoxaPortRxQueue(p);
                                        tmp.outq = MoxaPortTxQueue(p);
                                }
+                               spin_unlock_bh(&moxa_lock);
                                if (copy_to_user(argm, &tmp, sizeof(tmp)))
                                        return -EFAULT;
                        }
                }
-               return 0;
+               break;
        } case MOXA_GET_OQUEUE:
                status = MoxaPortTxQueue(ch);
-               return put_user(status, (unsigned long __user *)argp);
+               ret = put_user(status, (unsigned long __user *)argp);
+               break;
        case MOXA_GET_IQUEUE:
                status = MoxaPortRxQueue(ch);
-               return put_user(status, (unsigned long __user *)argp);
+               ret = put_user(status, (unsigned long __user *)argp);
+               break;
        case MOXA_GETMSTATUS: {
                struct mxser_mstatus __user *argm = argp;
                struct mxser_mstatus tmp;
@@ -325,11 +348,17 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
                for (i = 0; i < MAX_BOARDS; i++) {
                        p = moxa_boards[i].ports;
                        for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
+                               struct tty_struct *ttyp;
                                memset(&tmp, 0, sizeof(tmp));
-                               if (!moxa_boards[i].ready)
+                               spin_lock_bh(&moxa_lock);
+                               if (!moxa_boards[i].ready) {
+                                       spin_unlock_bh(&moxa_lock);
                                        goto copy;
+                                }
 
                                status = MoxaPortLineStatus(p);
+                               spin_unlock_bh(&moxa_lock);
+
                                if (status & 1)
                                        tmp.cts = 1;
                                if (status & 2)
@@ -337,31 +366,42 @@ static int moxa_ioctl(struct tty_struct *tty, struct file *file,
                                if (status & 4)
                                        tmp.dcd = 1;
 
-                               if (!p->tty || !p->tty->termios)
+                               ttyp = tty_port_tty_get(&p->port);
+                               if (!ttyp || !ttyp->termios)
                                        tmp.cflag = p->cflag;
                                else
-                                       tmp.cflag = p->tty->termios->c_cflag;
+                                       tmp.cflag = ttyp->termios->c_cflag;
+                               tty_kref_put(tty);
 copy:
                                if (copy_to_user(argm, &tmp, sizeof(tmp)))
                                        return -EFAULT;
                        }
                }
-               return 0;
+               break;
        }
        case TIOCGSERIAL:
-               return moxa_get_serial_info(ch, argp);
+               mutex_lock(&ch->port.mutex);
+               ret = moxa_get_serial_info(ch, argp);
+               mutex_unlock(&ch->port.mutex);
+               break;
        case TIOCSSERIAL:
-               return moxa_set_serial_info(ch, argp);
+               mutex_lock(&ch->port.mutex);
+               ret = moxa_set_serial_info(ch, argp);
+               mutex_unlock(&ch->port.mutex);
+               break;
+       default:
+               ret = -ENOIOCTLCMD;
        }
-       return -ENOIOCTLCMD;
+       return ret;
 }
 
-static void moxa_break_ctl(struct tty_struct *tty, int state)
+static int moxa_break_ctl(struct tty_struct *tty, int state)
 {
        struct moxa_port *port = tty->driver_data;
 
        moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
                        Magic_code);
+       return 0;
 }
 
 static const struct tty_operations moxa_ops = {
@@ -371,11 +411,7 @@ static const struct tty_operations moxa_ops = {
        .write_room = moxa_write_room,
        .flush_buffer = moxa_flush_buffer,
        .chars_in_buffer = moxa_chars_in_buffer,
-       .flush_chars = moxa_flush_chars,
-       .put_char = moxa_put_char,
        .ioctl = moxa_ioctl,
-       .throttle = moxa_throttle,
-       .unthrottle = moxa_unthrottle,
        .set_termios = moxa_set_termios,
        .stop = moxa_stop,
        .start = moxa_start,
@@ -385,9 +421,14 @@ static const struct tty_operations moxa_ops = {
        .tiocmset = moxa_tiocmset,
 };
 
+static const struct tty_port_operations moxa_port_ops = {
+       .carrier_raised = moxa_carrier_raised,
+       .dtr_rts = moxa_dtr_rts,
+       .shutdown = moxa_shutdown,
+};
+
 static struct tty_driver *moxaDriver;
 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
-static DEFINE_SPINLOCK(moxa_lock);
 
 /*
  * HW init
@@ -457,7 +498,7 @@ static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
                        goto err;
                tmp = readw(baseAddr + C320_status);
                if (tmp != STS_init) {
-                       printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
+                       printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
                                        "module not found\n");
                        return -EIO;
                }
@@ -466,7 +507,7 @@ static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
 
        return 0;
 err:
-       printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
+       printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
        return -EIO;
 }
 
@@ -476,7 +517,7 @@ static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
        void __iomem *baseAddr = brd->basemem;
 
        if (len < 7168) {
-               printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
+               printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
                return -EINVAL;
        }
 
@@ -493,14 +534,12 @@ static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
                size_t len)
 {
        void __iomem *baseAddr = brd->basemem;
-       const u16 *uptr = ptr;
+       const __le16 *uptr = ptr;
        size_t wlen, len2, j;
        unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
-       unsigned int i, retry, c320;
+       unsigned int i, retry;
        u16 usum, keycode;
 
-       c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
-                       brd->boardType == MOXA_BOARD_C320_ISA;
        keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
                                C218_KeyCode;
 
@@ -570,7 +609,7 @@ static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
        if (readw(baseAddr + Magic_no) != Magic_code)
                return -EIO;
 
-       if (c320) {
+       if (MOXA_IS_320(brd)) {
                if (brd->busType == MOXA_BUS_TYPE_PCI) {        /* ASIC board */
                        writew(0x3800, baseAddr + TMS320_PORT1);
                        writew(0x3900, baseAddr + TMS320_PORT2);
@@ -591,7 +630,7 @@ static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
        if (readw(baseAddr + Magic_no) != Magic_code)
                return -EIO;
 
-       if (c320) {
+       if (MOXA_IS_320(brd)) {
                j = readw(baseAddr + Module_cnt);
                if (j <= 0)
                        return -EIO;
@@ -621,7 +660,7 @@ static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
        int retval, i;
 
        if (len % 2) {
-               printk(KERN_ERR "moxa: bios length is not even\n");
+               printk(KERN_ERR "MOXA: bios length is not even\n");
                return -EINVAL;
        }
 
@@ -703,7 +742,7 @@ static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
 
 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
 {
-       void *ptr = fw->data;
+       const void *ptr = fw->data;
        char rsn[64];
        u16 lens[5];
        size_t len;
@@ -716,7 +755,7 @@ static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
                u8 model;       /* C218T=1, C320T=2, CP204=3 */
                u8 reserved2[8];
                __le16 len[5];
-       } *hdr = ptr;
+       } const *hdr = ptr;
 
        BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
 
@@ -743,7 +782,7 @@ static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
                lens[a] = le16_to_cpu(hdr->len[a]);
                if (lens[a] && len + lens[a] <= fw->size &&
                                moxa_check_fw(&fw->data[len]))
-                       printk(KERN_WARNING "moxa firmware: unexpected input "
+                       printk(KERN_WARNING "MOXA firmware: unexpected input "
                                "at offset %u, but going on\n", (u32)len);
                if (!lens[a] && a < lencnt) {
                        sprintf(rsn, "too few entries in fw file");
@@ -807,14 +846,10 @@ static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
        }
 
        for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
+               tty_port_init(&p->port);
+               p->port.ops = &moxa_port_ops;
                p->type = PORT_16550A;
-               p->close_delay = 5 * HZ / 10;
                p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
-               init_waitqueue_head(&p->open_wait);
-               init_completion(&p->close_wait);
-
-               setup_timer(&p->emptyTimer, moxa_check_xmit_empty,
-                               (unsigned long)p);
        }
 
        switch (brd->boardType) {
@@ -832,7 +867,10 @@ static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
 
        ret = request_firmware(&fw, file, dev);
        if (ret) {
-               printk(KERN_ERR "request_firmware failed\n");
+               printk(KERN_ERR "MOXA: request_firmware failed. Make sure "
+                               "you've placed '%s' file into your firmware "
+                               "loader directory (e.g. /lib/firmware)\n",
+                               file);
                goto err_free;
        }
 
@@ -843,10 +881,11 @@ static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
        if (ret)
                goto err_free;
 
+       spin_lock_bh(&moxa_lock);
        brd->ready = 1;
-
        if (!timer_pending(&moxaTimer))
                mod_timer(&moxaTimer, jiffies + HZ / 50);
+       spin_unlock_bh(&moxa_lock);
 
        return 0;
 err_free:
@@ -857,11 +896,34 @@ err:
 
 static void moxa_board_deinit(struct moxa_board_conf *brd)
 {
-       unsigned int i;
+       unsigned int a, opened;
 
+       mutex_lock(&moxa_openlock);
+       spin_lock_bh(&moxa_lock);
        brd->ready = 0;
-       for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
-               del_timer_sync(&brd->ports[i].emptyTimer);
+       spin_unlock_bh(&moxa_lock);
+
+       /* pci hot-un-plug support */
+       for (a = 0; a < brd->numPorts; a++)
+               if (brd->ports[a].port.flags & ASYNC_INITIALIZED) {
+                       struct tty_struct *tty = tty_port_tty_get(
+                                               &brd->ports[a].port);
+                       if (tty) {
+                               tty_hangup(tty);
+                               tty_kref_put(tty);
+                       }
+               }
+       while (1) {
+               opened = 0;
+               for (a = 0; a < brd->numPorts; a++)
+                       if (brd->ports[a].port.flags & ASYNC_INITIALIZED)
+                               opened++;
+               mutex_unlock(&moxa_openlock);
+               if (!opened)
+                       break;
+               msleep(50);
+               mutex_lock(&moxa_openlock);
+       }
 
        iounmap(brd->basemem);
        brd->basemem = NULL;
@@ -902,7 +964,7 @@ static int __devinit moxa_pci_probe(struct pci_dev *pdev,
                goto err;
        }
 
-       board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
+       board->basemem = ioremap_nocache(pci_resource_start(pdev, 2), 0x4000);
        if (board->basemem == NULL) {
                dev_err(&pdev->dev, "can't remap io space 2\n");
                goto err_reg;
@@ -930,7 +992,10 @@ static int __devinit moxa_pci_probe(struct pci_dev *pdev,
 
        pci_set_drvdata(pdev, board);
 
-       return (0);
+       dev_info(&pdev->dev, "board '%s' ready (%u ports, firmware loaded)\n",
+                       moxa_brdname[board_type - 1], board->numPorts);
+
+       return 0;
 err_base:
        iounmap(board->basemem);
        board->basemem = NULL;
@@ -961,6 +1026,8 @@ static int __init moxa_init(void)
 {
        unsigned int isabrds = 0;
        int retval = 0;
+       struct moxa_board_conf *brd = moxa_boards;
+       unsigned int i;
 
        printk(KERN_INFO "MOXA Intellio family driver version %s\n",
                        MOXA_VERSION);
@@ -981,19 +1048,14 @@ static int __init moxa_init(void)
        moxaDriver->flags = TTY_DRIVER_REAL_RAW;
        tty_set_operations(moxaDriver, &moxa_ops);
 
-       pr_debug("Moxa tty devices major number = %d\n", ttymajor);
-
        if (tty_register_driver(moxaDriver)) {
-               printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
+               printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
                put_tty_driver(moxaDriver);
                return -1;
        }
 
        /* Find the boards defined from module args. */
-#ifdef MODULE
-       {
-       struct moxa_board_conf *brd = moxa_boards;
-       unsigned int i;
+
        for (i = 0; i < MAX_BOARDS; i++) {
                if (!baseaddr[i])
                        break;
@@ -1006,9 +1068,9 @@ static int __init moxa_init(void)
                        brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
                                        numports[i];
                        brd->busType = MOXA_BUS_TYPE_ISA;
-                       brd->basemem = ioremap(baseaddr[i], 0x4000);
+                       brd->basemem = ioremap_nocache(baseaddr[i], 0x4000);
                        if (!brd->basemem) {
-                               printk(KERN_ERR "moxa: can't remap %lx\n",
+                               printk(KERN_ERR "MOXA: can't remap %lx\n",
                                                baseaddr[i]);
                                continue;
                        }
@@ -1018,17 +1080,19 @@ static int __init moxa_init(void)
                                continue;
                        }
 
+                       printk(KERN_INFO "MOXA isa board found at 0x%.8lu and "
+                                       "ready (%u ports, firmware loaded)\n",
+                                       baseaddr[i], brd->numPorts);
+
                        brd++;
                        isabrds++;
                }
        }
-       }
-#endif
 
 #ifdef CONFIG_PCI
        retval = pci_register_driver(&moxa_pci_driver);
        if (retval) {
-               printk(KERN_ERR "Can't register moxa pci driver!\n");
+               printk(KERN_ERR "Can't register MOXA pci driver!\n");
                if (isabrds)
                        retval = 0;
        }
@@ -1039,14 +1103,7 @@ static int __init moxa_init(void)
 
 static void __exit moxa_exit(void)
 {
-       int i;
-
-       del_timer_sync(&moxaTimer);
-
-       if (tty_unregister_driver(moxaDriver))
-               printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
-                               "serial driver\n");
-       put_tty_driver(moxaDriver);
+       unsigned int i;
 
 #ifdef CONFIG_PCI
        pci_unregister_driver(&moxa_pci_driver);
@@ -1055,11 +1112,44 @@ static void __exit moxa_exit(void)
        for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
                if (moxa_boards[i].ready)
                        moxa_board_deinit(&moxa_boards[i]);
+
+       del_timer_sync(&moxaTimer);
+
+       if (tty_unregister_driver(moxaDriver))
+               printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
+                               "serial driver\n");
+       put_tty_driver(moxaDriver);
 }
 
 module_init(moxa_init);
 module_exit(moxa_exit);
 
+static void moxa_shutdown(struct tty_port *port)
+{
+       struct moxa_port *ch = container_of(port, struct moxa_port, port);
+        MoxaPortDisable(ch);
+       MoxaPortFlushData(ch, 2);
+       clear_bit(ASYNCB_NORMAL_ACTIVE, &port->flags);
+}
+
+static int moxa_carrier_raised(struct tty_port *port)
+{
+       struct moxa_port *ch = container_of(port, struct moxa_port, port);
+       int dcd;
+
+       spin_lock_irq(&port->lock);
+       dcd = ch->DCDState;
+       spin_unlock_irq(&port->lock);
+       return dcd;
+}
+
+static void moxa_dtr_rts(struct tty_port *port, int onoff)
+{
+       struct moxa_port *ch = container_of(port, struct moxa_port, port);
+       MoxaPortLineCtrl(ch, onoff, onoff);
+}
+
+
 static int moxa_open(struct tty_struct *tty, struct file *filp)
 {
        struct moxa_board_conf *brd;
@@ -1071,111 +1161,63 @@ static int moxa_open(struct tty_struct *tty, struct file *filp)
        if (port == MAX_PORTS) {
                return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
        }
+       if (mutex_lock_interruptible(&moxa_openlock))
+               return -ERESTARTSYS;
        brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
-       if (!brd->ready)
+       if (!brd->ready) {
+               mutex_unlock(&moxa_openlock);
                return -ENODEV;
+       }
+
+       if (port % MAX_PORTS_PER_BOARD >= brd->numPorts) {
+               mutex_unlock(&moxa_openlock);
+               return -ENODEV;
+       }
 
        ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
-       ch->count++;
+       ch->port.count++;
        tty->driver_data = ch;
-       ch->tty = tty;
-       if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
+       tty_port_tty_set(&ch->port, tty);
+       mutex_lock(&ch->port.mutex);
+       if (!(ch->port.flags & ASYNC_INITIALIZED)) {
                ch->statusflags = 0;
                moxa_set_tty_param(tty, tty->termios);
                MoxaPortLineCtrl(ch, 1, 1);
                MoxaPortEnable(ch);
-               ch->asyncflags |= ASYNC_INITIALIZED;
+               MoxaSetFifo(ch, ch->type == PORT_16550A);
+               ch->port.flags |= ASYNC_INITIALIZED;
        }
-       retval = moxa_block_till_ready(tty, filp, ch);
-
-       moxa_unthrottle(tty);
+       mutex_unlock(&ch->port.mutex);
+       mutex_unlock(&moxa_openlock);
 
-       if (ch->type == PORT_16550A) {
-               MoxaSetFifo(ch, 1);
-       } else {
-               MoxaSetFifo(ch, 0);
-       }
-
-       return (retval);
+       retval = tty_port_block_til_ready(&ch->port, tty, filp);
+       if (retval == 0)
+               set_bit(ASYNCB_NORMAL_ACTIVE, &ch->port.flags);
+       return retval;
 }
 
 static void moxa_close(struct tty_struct *tty, struct file *filp)
 {
-       struct moxa_port *ch;
-       int port;
-
-       port = tty->index;
-       if (port == MAX_PORTS) {
-               return;
-       }
-       if (tty->driver_data == NULL) {
-               return;
-       }
-       if (tty_hung_up_p(filp)) {
-               return;
-       }
-       ch = (struct moxa_port *) tty->driver_data;
-
-       if ((tty->count == 1) && (ch->count != 1)) {
-               printk(KERN_WARNING "moxa_close: bad serial port count; "
-                       "tty->count is 1, ch->count is %d\n", ch->count);
-               ch->count = 1;
-       }
-       if (--ch->count < 0) {
-               printk(KERN_WARNING "moxa_close: bad serial port count, "
-                       "device=%s\n", tty->name);
-               ch->count = 0;
-       }
-       if (ch->count) {
-               return;
-       }
-       ch->asyncflags |= ASYNC_CLOSING;
-
+       struct moxa_port *ch = tty->driver_data;
        ch->cflag = tty->termios->c_cflag;
-       if (ch->asyncflags & ASYNC_INITIALIZED) {
-               moxa_setup_empty_event(tty);
-               tty_wait_until_sent(tty, 30 * HZ);      /* 30 seconds timeout */
-               del_timer_sync(&ch->emptyTimer);
-       }
-       moxa_shut_down(ch);
-       MoxaPortFlushData(ch, 2);
-
-       if (tty->driver->flush_buffer)
-               tty->driver->flush_buffer(tty);
-       tty_ldisc_flush(tty);
-                       
-       tty->closing = 0;
-       ch->tty = NULL;
-       if (ch->blocked_open) {
-               if (ch->close_delay) {
-                       msleep_interruptible(jiffies_to_msecs(ch->close_delay));
-               }
-               wake_up_interruptible(&ch->open_wait);
-       }
-       ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
-       complete_all(&ch->close_wait);
+       tty_port_close(&ch->port, tty, filp);
 }
 
 static int moxa_write(struct tty_struct *tty,
                      const unsigned char *buf, int count)
 {
        struct moxa_port *ch = tty->driver_data;
-       unsigned long flags;
        int len;
 
        if (ch == NULL)
                return 0;
 
-       spin_lock_irqsave(&moxa_lock, flags);
-       len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
-       spin_unlock_irqrestore(&moxa_lock, flags);
+       spin_lock_bh(&moxa_lock);
+       len = MoxaPortWriteData(tty, buf, count);
+       spin_unlock_bh(&moxa_lock);
 
-       /*********************************************
-       if ( !(ch->statusflags & LOWWAIT) &&
-            ((len != count) || (MoxaPortTxFree(port) <= 100)) )
-       ************************************************/
-       ch->statusflags |= LOWWAIT;
-       return (len);
+       set_bit(LOWWAIT, &ch->statusflags);
+       return len;
 }
 
 static int moxa_write_room(struct tty_struct *tty)
@@ -1183,10 +1225,10 @@ static int moxa_write_room(struct tty_struct *tty)
        struct moxa_port *ch;
 
        if (tty->stopped)
-               return (0);
+               return 0;
        ch = tty->driver_data;
        if (ch == NULL)
-               return (0);
+               return 0;
        return MoxaPortTxFree(ch);
 }
 
@@ -1205,48 +1247,14 @@ static int moxa_chars_in_buffer(struct tty_struct *tty)
        struct moxa_port *ch = tty->driver_data;
        int chars;
 
-       /*
-        * Sigh...I have to check if driver_data is NULL here, because
-        * if an open() fails, the TTY subsystem eventually calls
-        * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
-        * routine.  And since the open() failed, we return 0 here.  TDJ
-        */
-       if (ch == NULL)
-               return (0);
        chars = MoxaPortTxQueue(ch);
-       if (chars) {
+       if (chars)
                /*
                 * Make it possible to wakeup anything waiting for output
                 * in tty_ioctl.c, etc.
                 */
-               if (!(ch->statusflags & EMPTYWAIT))
-                       moxa_setup_empty_event(tty);
-       }
-       return (chars);
-}
-
-static void moxa_flush_chars(struct tty_struct *tty)
-{
-       /*
-        * Don't think I need this, because this is called to empty the TX
-        * buffer for the 16450, 16550, etc.
-        */
-}
-
-static void moxa_put_char(struct tty_struct *tty, unsigned char c)
-{
-       struct moxa_port *ch = tty->driver_data;
-       unsigned long flags;
-
-       if (ch == NULL)
-               return;
-       spin_lock_irqsave(&moxa_lock, flags);
-       MoxaPortWriteData(ch, &c, 1);
-       spin_unlock_irqrestore(&moxa_lock, flags);
-       /************************************************
-       if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
-       *************************************************/
-       ch->statusflags |= LOWWAIT;
+               set_bit(EMPTYWAIT, &ch->statusflags);
+       return chars;
 }
 
 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
@@ -1254,9 +1262,6 @@ static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
        struct moxa_port *ch = tty->driver_data;
        int flag = 0, dtr, rts;
 
-       if (!ch)
-               return -EINVAL;
-
        MoxaPortGetLineOut(ch, &dtr, &rts);
        if (dtr)
                flag |= TIOCM_DTR;
@@ -1275,13 +1280,17 @@ static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
                         unsigned int set, unsigned int clear)
 {
-       struct moxa_port *ch = tty->driver_data;
+       struct moxa_port *ch;
        int port;
        int dtr, rts;
 
        port = tty->index;
-       if (!ch)
+       mutex_lock(&moxa_openlock);
+       ch = tty->driver_data;
+       if (!ch) {
+               mutex_unlock(&moxa_openlock);
                return -EINVAL;
+       }
 
        MoxaPortGetLineOut(ch, &dtr, &rts);
        if (set & TIOCM_RTS)
@@ -1293,50 +1302,36 @@ static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
        if (clear & TIOCM_DTR)
                dtr = 0;
        MoxaPortLineCtrl(ch, dtr, rts);
+       mutex_unlock(&moxa_openlock);
        return 0;
 }
 
-static void moxa_throttle(struct tty_struct *tty)
-{
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
-
-       ch->statusflags |= THROTTLE;
-}
-
-static void moxa_unthrottle(struct tty_struct *tty)
-{
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
-
-       ch->statusflags &= ~THROTTLE;
-}
-
 static void moxa_set_termios(struct tty_struct *tty,
-                            struct ktermios *old_termios)
+               struct ktermios *old_termios)
 {
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
+       struct moxa_port *ch = tty->driver_data;
 
        if (ch == NULL)
                return;
        moxa_set_tty_param(tty, old_termios);
-       if (!(old_termios->c_cflag & CLOCAL) &&
-           (tty->termios->c_cflag & CLOCAL))
-               wake_up_interruptible(&ch->open_wait);
+       if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
+               wake_up_interruptible(&ch->port.open_wait);
 }
 
 static void moxa_stop(struct tty_struct *tty)
 {
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
+       struct moxa_port *ch = tty->driver_data;
 
        if (ch == NULL)
                return;
        MoxaPortTxDisable(ch);
-       ch->statusflags |= TXSTOPPED;
+       set_bit(TXSTOPPED, &ch->statusflags);
 }
 
 
 static void moxa_start(struct tty_struct *tty)
 {
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
+       struct moxa_port *ch = tty->driver_data;
 
        if (ch == NULL)
                return;
@@ -1345,91 +1340,141 @@ static void moxa_start(struct tty_struct *tty)
                return;
 
        MoxaPortTxEnable(ch);
-       ch->statusflags &= ~TXSTOPPED;
+       clear_bit(TXSTOPPED, &ch->statusflags);
 }
 
 static void moxa_hangup(struct tty_struct *tty)
 {
-       struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
-
-       moxa_flush_buffer(tty);
-       moxa_shut_down(ch);
-       ch->count = 0;
-       ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
-       ch->tty = NULL;
-       wake_up_interruptible(&ch->open_wait);
+       struct moxa_port *ch = tty->driver_data;
+       tty_port_hangup(&ch->port);
 }
 
-static void moxa_poll(unsigned long ignored)
+static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
 {
-       struct moxa_port *ch;
        struct tty_struct *tty;
-       unsigned int card;
-       int i;
+       unsigned long flags;
+       dcd = !!dcd;
 
-       del_timer(&moxaTimer);
+       spin_lock_irqsave(&p->port.lock, flags);
+       if (dcd != p->DCDState) {
+               p->DCDState = dcd;
+               spin_unlock_irqrestore(&p->port.lock, flags);
+               tty = tty_port_tty_get(&p->port);
+               if (tty && C_CLOCAL(tty) && !dcd)
+                       tty_hangup(tty);
+               tty_kref_put(tty);
+       }
+       else
+               spin_unlock_irqrestore(&p->port.lock, flags);
+}
 
-       if (MoxaDriverPoll() < 0) {
-               mod_timer(&moxaTimer, jiffies + HZ / 50);
-               return;
+static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
+               u16 __iomem *ip)
+{
+       struct tty_struct *tty = tty_port_tty_get(&p->port);
+       void __iomem *ofsAddr;
+       unsigned int inited = p->port.flags & ASYNC_INITIALIZED;
+       u16 intr;
+
+       if (tty) {
+               if (test_bit(EMPTYWAIT, &p->statusflags) &&
+                               MoxaPortTxQueue(p) == 0) {
+                       clear_bit(EMPTYWAIT, &p->statusflags);
+                       tty_wakeup(tty);
+               }
+               if (test_bit(LOWWAIT, &p->statusflags) && !tty->stopped &&
+                               MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
+                       clear_bit(LOWWAIT, &p->statusflags);
+                       tty_wakeup(tty);
+               }
+
+               if (inited && !test_bit(TTY_THROTTLED, &tty->flags) &&
+                               MoxaPortRxQueue(p) > 0) { /* RX */
+                       MoxaPortReadData(p);
+                       tty_schedule_flip(tty);
+               }
+       } else {
+               clear_bit(EMPTYWAIT, &p->statusflags);
+               MoxaPortFlushData(p, 0); /* flush RX */
+       }
+
+       if (!handle) /* nothing else to do */
+               goto put;
+
+       intr = readw(ip); /* port irq status */
+       if (intr == 0)
+               goto put;
+
+       writew(0, ip); /* ACK port */
+       ofsAddr = p->tableAddr;
+       if (intr & IntrTx) /* disable tx intr */
+               writew(readw(ofsAddr + HostStat) & ~WakeupTx,
+                               ofsAddr + HostStat);
+
+       if (!inited)
+               goto put;
+
+       if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
+               tty_insert_flip_char(tty, 0, TTY_BREAK);
+               tty_schedule_flip(tty);
        }
 
+       if (intr & IntrLine)
+               moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
+put:
+       tty_kref_put(tty);
+
+       return 0;
+}
+
+static void moxa_poll(unsigned long ignored)
+{
+       struct moxa_board_conf *brd;
+       u16 __iomem *ip;
+       unsigned int card, port, served = 0;
+
+       spin_lock(&moxa_lock);
        for (card = 0; card < MAX_BOARDS; card++) {
-               if (!moxa_boards[card].ready)
+               brd = &moxa_boards[card];
+               if (!brd->ready)
                        continue;
-               ch = moxa_boards[card].ports;
-               for (i = 0; i < moxa_boards[card].numPorts; i++, ch++) {
-                       if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
-                               continue;
-                       if (!(ch->statusflags & THROTTLE) &&
-                           (MoxaPortRxQueue(ch) > 0))
-                               moxa_receive_data(ch);
-                       tty = ch->tty;
-                       if (tty == NULL)
-                               continue;
-                       if (ch->statusflags & LOWWAIT) {
-                               if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
-                                       if (!tty->stopped) {
-                                               ch->statusflags &= ~LOWWAIT;
-                                               tty_wakeup(tty);
-                                       }
-                               }
-                       }
-                       if (!I_IGNBRK(tty) && (MoxaPortResetBrkCnt(ch) > 0)) {
-                               tty_insert_flip_char(tty, 0, TTY_BREAK);
-                               tty_schedule_flip(tty);
-                       }
-                       if (MoxaPortDCDChange(ch)) {
-                               if (ch->asyncflags & ASYNC_CHECK_CD) {
-                                       if (MoxaPortDCDON(ch))
-                                               wake_up_interruptible(&ch->open_wait);
-                                       else {
-                                               tty_hangup(tty);
-                                               wake_up_interruptible(&ch->open_wait);
-                                               ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
-                                       }
+
+               served++;
+
+               ip = NULL;
+               if (readb(brd->intPend) == 0xff)
+                       ip = brd->intTable + readb(brd->intNdx);
+
+               for (port = 0; port < brd->numPorts; port++)
+                       moxa_poll_port(&brd->ports[port], !!ip, ip + port);
+
+               if (ip)
+                       writeb(0, brd->intPend); /* ACK */
+
+               if (moxaLowWaterChk) {
+                       struct moxa_port *p = brd->ports;
+                       for (port = 0; port < brd->numPorts; port++, p++)
+                               if (p->lowChkFlag) {
+                                       p->lowChkFlag = 0;
+                                       moxa_low_water_check(p->tableAddr);
                                }
-                       }
                }
        }
+       moxaLowWaterChk = 0;
 
-       mod_timer(&moxaTimer, jiffies + HZ / 50);
+       if (served)
+               mod_timer(&moxaTimer, jiffies + HZ / 50);
+       spin_unlock(&moxa_lock);
 }
 
 /******************************************************************************/
 
 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
 {
-       register struct ktermios *ts;
-       struct moxa_port *ch;
+       register struct ktermios *ts = tty->termios;
+       struct moxa_port *ch = tty->driver_data;
        int rts, cts, txflow, rxflow, xany, baud;
 
-       ch = (struct moxa_port *) tty->driver_data;
-       ts = tty->termios;
-       if (ts->c_cflag & CLOCAL)
-               ch->asyncflags &= ~ASYNC_CHECK_CD;
-       else
-               ch->asyncflags |= ASYNC_CHECK_CD;
        rts = cts = txflow = rxflow = xany = 0;
        if (ts->c_cflag & CRTSCTS)
                rts = cts = 1;
@@ -1450,182 +1495,14 @@ static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_term
        tty_encode_baud_rate(tty, baud, baud);
 }
 
-static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
-                           struct moxa_port *ch)
-{
-       DECLARE_WAITQUEUE(wait,current);
-       unsigned long flags;
-       int retval;
-       int do_clocal = C_CLOCAL(tty);
-
-       /*
-        * If the device is in the middle of being closed, then block
-        * until it's done, and then try again.
-        */
-       if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
-               if (ch->asyncflags & ASYNC_CLOSING)
-                       wait_for_completion_interruptible(&ch->close_wait);
-#ifdef SERIAL_DO_RESTART
-               if (ch->asyncflags & ASYNC_HUP_NOTIFY)
-                       return (-EAGAIN);
-               else
-                       return (-ERESTARTSYS);
-#else
-               return (-EAGAIN);
-#endif
-       }
-       /*
-        * If non-blocking mode is set, then make the check up front
-        * and then exit.
-        */
-       if (filp->f_flags & O_NONBLOCK) {
-               ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
-               return (0);
-       }
-       /*
-        * Block waiting for the carrier detect and the line to become free
-        */
-       retval = 0;
-       add_wait_queue(&ch->open_wait, &wait);
-       pr_debug("block_til_ready before block: ttys%d, count = %d\n",
-               tty->index, ch->count);
-       spin_lock_irqsave(&moxa_lock, flags);
-       if (!tty_hung_up_p(filp))
-               ch->count--;
-       ch->blocked_open++;
-       spin_unlock_irqrestore(&moxa_lock, flags);
-
-       while (1) {
-               set_current_state(TASK_INTERRUPTIBLE);
-               if (tty_hung_up_p(filp) ||
-                   !(ch->asyncflags & ASYNC_INITIALIZED)) {
-#ifdef SERIAL_DO_RESTART
-                       if (ch->asyncflags & ASYNC_HUP_NOTIFY)
-                               retval = -EAGAIN;
-                       else
-                               retval = -ERESTARTSYS;
-#else
-                       retval = -EAGAIN;
-#endif
-                       break;
-               }
-               if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
-                                               MoxaPortDCDON(ch)))
-                       break;
-
-               if (signal_pending(current)) {
-                       retval = -ERESTARTSYS;
-                       break;
-               }
-               schedule();
-       }
-       set_current_state(TASK_RUNNING);
-       remove_wait_queue(&ch->open_wait, &wait);
-
-       spin_lock_irqsave(&moxa_lock, flags);
-       if (!tty_hung_up_p(filp))
-               ch->count++;
-       ch->blocked_open--;
-       spin_unlock_irqrestore(&moxa_lock, flags);
-       pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
-               tty->index, ch->count);
-       if (retval)
-               return (retval);
-       /* FIXME: review to see if we need to use set_bit on these */
-       ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
-       return 0;
-}
-
-static void moxa_setup_empty_event(struct tty_struct *tty)
-{
-       struct moxa_port *ch = tty->driver_data;
-       unsigned long flags;
-
-       spin_lock_irqsave(&moxa_lock, flags);
-       ch->statusflags |= EMPTYWAIT;
-       mod_timer(&ch->emptyTimer, jiffies + HZ);
-       spin_unlock_irqrestore(&moxa_lock, flags);
-}
-
-static void moxa_check_xmit_empty(unsigned long data)
-{
-       struct moxa_port *ch;
-
-       ch = (struct moxa_port *) data;
-       if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
-               if (MoxaPortTxQueue(ch) == 0) {
-                       ch->statusflags &= ~EMPTYWAIT;
-                       tty_wakeup(ch->tty);
-                       return;
-               }
-               mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
-       } else
-               ch->statusflags &= ~EMPTYWAIT;
-}
-
-static void moxa_shut_down(struct moxa_port *ch)
-{
-       struct tty_struct *tp;
-
-       if (!(ch->asyncflags & ASYNC_INITIALIZED))
-               return;
-
-       tp = ch->tty;
-
-       MoxaPortDisable(ch);
-
-       /*
-        * If we're a modem control device and HUPCL is on, drop RTS & DTR.
-        */
-       if (tp->termios->c_cflag & HUPCL)
-               MoxaPortLineCtrl(ch, 0, 0);
-
-       ch->asyncflags &= ~ASYNC_INITIALIZED;
-}
-
-static void moxa_receive_data(struct moxa_port *ch)
-{
-       struct tty_struct *tp;
-       struct ktermios *ts;
-       unsigned long flags;
-
-       ts = NULL;
-       tp = ch->tty;
-       if (tp)
-               ts = tp->termios;
-       /**************************************************
-       if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
-       *****************************************************/
-       if (!tp || !ts) {
-               MoxaPortFlushData(ch, 0);
-               return;
-       }
-       spin_lock_irqsave(&moxa_lock, flags);
-       MoxaPortReadData(ch, tp);
-       spin_unlock_irqrestore(&moxa_lock, flags);
-       tty_schedule_flip(tp);
-}
-
-/*
- *    Query
- */
-
-#define        DCD_changed     0x01
-#define        DCD_oldstate    0x80
-
-static int moxaLowWaterChk;
-
-static void moxa_low_water_check(void __iomem *);
-
 /*****************************************************************************
  *     Driver level functions:                                              *
- *     3. MoxaDriverPoll(void);                                             *
  *****************************************************************************/
 
 static void MoxaPortFlushData(struct moxa_port *port, int mode)
 {
        void __iomem *ofsAddr;
-       if ((mode < 0) || (mode > 2))
+       if (mode < 0 || mode > 2)
                return;
        ofsAddr = port->tableAddr;
        moxafunc(ofsAddr, FC_FlushQueue, mode);
@@ -1635,90 +1512,6 @@ static void MoxaPortFlushData(struct moxa_port *port, int mode)
        }
 }
 
-static int MoxaDriverPoll(void)
-{
-       struct moxa_board_conf *brd;
-       struct moxa_port *p;
-       void __iomem *ofsAddr;
-       void __iomem *ip;
-       unsigned int port, ports, card;
-       ushort temp;
-
-       for (card = 0; card < MAX_BOARDS; card++) {
-               brd = &moxa_boards[card];
-               if (brd->ready == 0)
-                       continue;
-               if ((ports = brd->numPorts) == 0)
-                       continue;
-               if (readb(brd->intPend) == 0xff) {
-                       ip = brd->intTable + readb(brd->intNdx);
-                       p = brd->ports;
-                       ports <<= 1;
-                       for (port = 0; port < ports; port += 2, p++) {
-                               temp = readw(ip + port);
-                               if (temp == 0)
-                                       continue;
-
-                               writew(0, ip + port);
-                               ofsAddr = p->tableAddr;
-                               if (temp & IntrTx)
-                                       writew(readw(ofsAddr + HostStat) &
-                                               ~WakeupTx, ofsAddr + HostStat);
-                               if (temp & IntrBreak)
-                                       p->breakCnt++;
-
-                               if (temp & IntrLine) {
-                                       if (readb(ofsAddr + FlagStat) & DCD_state) {
-                                               if ((p->DCDState & DCD_oldstate) == 0)
-                                                       p->DCDState = (DCD_oldstate |
-                                                                          DCD_changed);
-                                       } else {
-                                               if (p->DCDState & DCD_oldstate)
-                                                       p->DCDState = DCD_changed;
-                                       }
-                               }
-                       }
-                       writeb(0, brd->intPend);
-               }
-               if (moxaLowWaterChk) {
-                       p = brd->ports;
-                       for (port = 0; port < ports; port++, p++) {
-                               if (p->lowChkFlag) {
-                                       p->lowChkFlag = 0;
-                                       ofsAddr = p->tableAddr;
-                                       moxa_low_water_check(ofsAddr);
-                               }
-                       }
-               }
-       }
-       moxaLowWaterChk = 0;
-
-       return 0;
-}
-
-/*****************************************************************************
- *     Port level functions:                                                *
- *     2.  MoxaPortEnable(int port);                                        *
- *     3.  MoxaPortDisable(int port);                                       *
- *     4.  MoxaPortGetMaxBaud(int port);                                    *
- *     6.  MoxaPortSetBaud(int port, long baud);                            *
- *     8.  MoxaPortSetTermio(int port, unsigned char *termio);              *
- *     9.  MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);      *
- *     10. MoxaPortLineCtrl(int port, int dtrState, int rtsState);          *
- *     11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany);    *
- *     12. MoxaPortLineStatus(int port);                                    *
- *     13. MoxaPortDCDChange(int port);                                     *
- *     14. MoxaPortDCDON(int port);                                         *
- *     15. MoxaPortFlushData(int port, int mode);                           *
- *     16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
- *     17. MoxaPortReadData(int port, struct tty_struct *tty);              *
- *     20. MoxaPortTxQueue(int port);                                       *
- *     21. MoxaPortTxFree(int port);                                        *
- *     22. MoxaPortRxQueue(int port);                                       *
- *     24. MoxaPortTxDisable(int port);                                     *
- *     25. MoxaPortTxEnable(int port);                                      *
- *     27. MoxaPortResetBrkCnt(int port);                                   *
- *****************************************************************************/
 /*
  *    Moxa Port Number Description:
  *
@@ -1755,14 +1548,6 @@ static int MoxaDriverPoll(void)
  *                      -ENOIOCTLCMD
  *
  *
- *      Function 3:     Moxa driver polling process routine.
- *      Syntax:
- *      int  MoxaDriverPoll(void);
- *
- *           return:    0       ; polling O.K.
- *                      -1      : no any Moxa card.             
- *
- *
  *      Function 6:     Enable this port to start Tx/Rx data.
  *      Syntax:
  *      void MoxaPortEnable(int port);
@@ -1775,18 +1560,9 @@ static int MoxaDriverPoll(void)
  *           int port           : port number (0 - 127)
  *
  *
- *      Function 8:     Get the maximun available baud rate of this port.
- *      Syntax:
- *      long MoxaPortGetMaxBaud(int port);
- *           int port           : port number (0 - 127)
- *
- *           return:    0       : this port is invalid
- *                      38400/57600/115200 bps
- *
- *
  *      Function 10:    Setting baud rate of this port.
  *      Syntax:
- *      long MoxaPortSetBaud(int port, long baud);
+ *      speed_t MoxaPortSetBaud(int port, speed_t baud);
  *           int port           : port number (0 - 127)
  *           long baud          : baud rate (50 - 115200)
  *
@@ -1853,25 +1629,6 @@ static int MoxaDriverPoll(void)
  *                      Bit 2 - DCD state (0: off, 1: on)
  *
  *
- *      Function 17:    Check the DCD state has changed since the last read
- *                      of this function.
- *      Syntax:
- *      int  MoxaPortDCDChange(int port);
- *           int port           : port number (0 - 127)
- *
- *           return:    0       : no changed
- *                      1       : DCD has changed
- *
- *
- *      Function 18:    Check ths current DCD state is ON or not.
- *      Syntax:
- *      int  MoxaPortDCDON(int port);
- *           int port           : port number (0 - 127)
- *
- *           return:    0       : DCD off
- *                      1       : DCD on
- *
- *
  *      Function 19:    Flush the Rx/Tx buffer data of this port.
  *      Syntax:
  *      void MoxaPortFlushData(int port, int mode);
@@ -1950,17 +1707,15 @@ static int MoxaDriverPoll(void)
 static void MoxaPortEnable(struct moxa_port *port)
 {
        void __iomem *ofsAddr;
-       short lowwater = 512;
+       u16 lowwater = 512;
 
        ofsAddr = port->tableAddr;
        writew(lowwater, ofsAddr + Low_water);
-       port->breakCnt = 0;
-       if (port->board->boardType == MOXA_BOARD_C320_ISA ||
-           port->board->boardType == MOXA_BOARD_C320_PCI) {
+       if (MOXA_IS_320(port->board))
                moxafunc(ofsAddr, FC_SetBreakIrq, 0);
-       } else {
-               writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
-       }
+       else
+               writew(readw(ofsAddr + HostStat) | WakeupBreak,
+                               ofsAddr + HostStat);
 
        moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
        moxafunc(ofsAddr, FC_FlushQueue, 2);
@@ -1979,37 +1734,22 @@ static void MoxaPortDisable(struct moxa_port *port)
        moxafunc(ofsAddr, FC_DisableCH, Magic_code);
 }
 
-static long MoxaPortGetMaxBaud(struct moxa_port *port)
-{
-       if (port->board->boardType == MOXA_BOARD_C320_ISA ||
-                       port->board->boardType == MOXA_BOARD_C320_PCI)
-               return (460800L);
-       else
-               return (921600L);
-}
-
-
-static long MoxaPortSetBaud(struct moxa_port *port, long baud)
+static speed_t MoxaPortSetBaud(struct moxa_port *port, speed_t baud)
 {
-       void __iomem *ofsAddr;
-       long max, clock;
-       unsigned int val;
+       void __iomem *ofsAddr = port->tableAddr;
+       unsigned int clock, val;
+       speed_t max;
 
-       if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
-               return (0);
-       ofsAddr = port->tableAddr;
+       max = MOXA_IS_320(port->board) ? 460800 : 921600;
+       if (baud < 50)
+               return 0;
        if (baud > max)
                baud = max;
-       if (max == 38400L)
-               clock = 614400L;        /* for 9.8304 Mhz : max. 38400 bps */
-       else if (max == 57600L)
-               clock = 691200L;        /* for 11.0592 Mhz : max. 57600 bps */
-       else
-               clock = 921600L;        /* for 14.7456 Mhz : max. 115200 bps */
+       clock = 921600;
        val = clock / baud;
        moxafunc(ofsAddr, FC_SetBaud, val);
        baud = clock / val;
-       return (baud);
+       return baud;
 }
 
 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
@@ -2048,40 +1788,39 @@ static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
        } else
                mode |= MX_PARNONE;
 
-       moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
+       moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
+
+       if (MOXA_IS_320(port->board) && baud >= 921600)
+               return -1;
 
-       if (port->board->boardType == MOXA_BOARD_C320_ISA ||
-                       port->board->boardType == MOXA_BOARD_C320_PCI) {
-               if (baud >= 921600L)
-                       return (-1);
-       }
        baud = MoxaPortSetBaud(port, baud);
 
        if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
+               spin_lock_irq(&moxafunc_lock);
                writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
                writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
                writeb(FC_SetXonXoff, ofsAddr + FuncCode);
                moxa_wait_finish(ofsAddr);
+               spin_unlock_irq(&moxafunc_lock);
 
        }
-       return (baud);
+       return baud;
 }
 
 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
                int *rtsState)
 {
-
        if (dtrState)
                *dtrState = !!(port->lineCtrl & DTR_ON);
        if (rtsState)
                *rtsState = !!(port->lineCtrl & RTS_ON);
 
-       return (0);
+       return 0;
 }
 
 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
 {
-       int mode = 0;
+       u8 mode = 0;
 
        if (dtr)
                mode |= DTR_ON;
@@ -2115,56 +1854,26 @@ static int MoxaPortLineStatus(struct moxa_port *port)
        int val;
 
        ofsAddr = port->tableAddr;
-       if (port->board->boardType == MOXA_BOARD_C320_ISA ||
-                       port->board->boardType == MOXA_BOARD_C320_PCI) {
-               moxafunc(ofsAddr, FC_LineStatus, 0);
-               val = readw(ofsAddr + FuncArg);
-       } else {
+       if (MOXA_IS_320(port->board))
+               val = moxafuncret(ofsAddr, FC_LineStatus, 0);
+       else
                val = readw(ofsAddr + FlagStat) >> 4;
-       }
        val &= 0x0B;
-       if (val & 8) {
+       if (val & 8)
                val |= 4;
-               if ((port->DCDState & DCD_oldstate) == 0)
-                       port->DCDState = (DCD_oldstate | DCD_changed);
-       } else {
-               if (port->DCDState & DCD_oldstate)
-                       port->DCDState = DCD_changed;
-       }
+       moxa_new_dcdstate(port, val & 8);
        val &= 7;
-       return (val);
-}
-
-static int MoxaPortDCDChange(struct moxa_port *port)
-{
-       int n;
-
-       n = port->DCDState;
-       port->DCDState &= ~DCD_changed;
-       n &= DCD_changed;
-       return (n);
+       return val;
 }
 
-static int MoxaPortDCDON(struct moxa_port *port)
+static int MoxaPortWriteData(struct tty_struct *tty,
+               const unsigned char *buffer, int len)
 {
-       int n;
-
-       if (port->DCDState & DCD_oldstate)
-               n = 1;
-       else
-               n = 0;
-       return (n);
-}
-
-static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
-               int len)
-{
-       int c, total, i;
-       ushort tail;
-       int cnt;
-       ushort head, tx_mask, spage, epage;
-       ushort pageno, pageofs, bufhead;
+       struct moxa_port *port = tty->driver_data;
        void __iomem *baseAddr, *ofsAddr, *ofs;
+       unsigned int c, total;
+       u16 head, tail, tx_mask, spage, epage;
+       u16 pageno, pageofs, bufhead;
 
        ofsAddr = port->tableAddr;
        baseAddr = port->board->basemem;
@@ -2173,11 +1882,10 @@ static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
        epage = readw(ofsAddr + EndPage_txb);
        tail = readw(ofsAddr + TXwptr);
        head = readw(ofsAddr + TXrptr);
-       c = (head > tail) ? (head - tail - 1)
-           : (head - tail + tx_mask);
+       c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
        if (c > len)
                c = len;
-       moxaLog.txcnt[port->tty->index] += c;
+       moxaLog.txcnt[port->port.tty->index] += c;
        total = c;
        if (spage == epage) {
                bufhead = readw(ofsAddr + Ofs_txb);
@@ -2189,45 +1897,42 @@ static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
                                len = tx_mask + 1 - tail;
                        len = (c > len) ? len : c;
                        ofs = baseAddr + DynPage_addr + bufhead + tail;
-                       for (i = 0; i < len; i++)
-                               writeb(*buffer++, ofs + i);
+                       memcpy_toio(ofs, buffer, len);
+                       buffer += len;
                        tail = (tail + len) & tx_mask;
                        c -= len;
                }
-               writew(tail, ofsAddr + TXwptr);
        } else {
-               len = c;
                pageno = spage + (tail >> 13);
                pageofs = tail & Page_mask;
-               do {
-                       cnt = Page_size - pageofs;
-                       if (cnt > c)
-                               cnt = c;
-                       c -= cnt;
+               while (c > 0) {
+                       len = Page_size - pageofs;
+                       if (len > c)
+                               len = c;
                        writeb(pageno, baseAddr + Control_reg);
                        ofs = baseAddr + DynPage_addr + pageofs;
-                       for (i = 0; i < cnt; i++)
-                               writeb(*buffer++, ofs + i);
-                       if (c == 0) {
-                               writew((tail + len) & tx_mask, ofsAddr + TXwptr);
-                               break;
-                       }
+                       memcpy_toio(ofs, buffer, len);
+                       buffer += len;
                        if (++pageno == epage)
                                pageno = spage;
                        pageofs = 0;
-               } while (1);
+                       c -= len;
+               }
+               tail = (tail + total) & tx_mask;
        }
+       writew(tail, ofsAddr + TXwptr);
        writeb(1, ofsAddr + CD180TXirq);        /* start to send */
-       return (total);
+       return total;
 }
 
-static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
+static int MoxaPortReadData(struct moxa_port *port)
 {
-       register ushort head, pageofs;
-       int i, count, cnt, len, total, remain;
-       ushort tail, rx_mask, spage, epage;
-       ushort pageno, bufhead;
+       struct tty_struct *tty = port->port.tty;
+       unsigned char *dst;
        void __iomem *baseAddr, *ofsAddr, *ofs;
+       unsigned int count, len, total;
+       u16 tail, rx_mask, spage, epage;
+       u16 pageno, pageofs, bufhead, head;
 
        ofsAddr = port->tableAddr;
        baseAddr = port->board->basemem;
@@ -2236,101 +1941,84 @@ static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
        rx_mask = readw(ofsAddr + RX_mask);
        spage = readw(ofsAddr + Page_rxb);
        epage = readw(ofsAddr + EndPage_rxb);
-       count = (tail >= head) ? (tail - head)
-           : (tail - head + rx_mask + 1);
+       count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
        if (count == 0)
                return 0;
 
        total = count;
-       remain = count - total;
-       moxaLog.rxcnt[port->tty->index] += total;
-       count = total;
+       moxaLog.rxcnt[tty->index] += total;
        if (spage == epage) {
                bufhead = readw(ofsAddr + Ofs_rxb);
                writew(spage, baseAddr + Control_reg);
                while (count > 0) {
-                       if (tail >= head)
-                               len = tail - head;
-                       else
-                               len = rx_mask + 1 - head;
-                       len = (count > len) ? len : count;
                        ofs = baseAddr + DynPage_addr + bufhead + head;
-                       for (i = 0; i < len; i++)
-                               tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
+                       len = (tail >= head) ? (tail - head) :
+                                       (rx_mask + 1 - head);
+                       len = tty_prepare_flip_string(tty, &dst,
+                                       min(len, count));
+                       memcpy_fromio(dst, ofs, len);
                        head = (head + len) & rx_mask;
                        count -= len;
                }
-               writew(head, ofsAddr + RXrptr);
        } else {
-               len = count;
                pageno = spage + (head >> 13);
                pageofs = head & Page_mask;
-               do {
-                       cnt = Page_size - pageofs;
-                       if (cnt > count)
-                               cnt = count;
-                       count -= cnt;
+               while (count > 0) {
                        writew(pageno, baseAddr + Control_reg);
                        ofs = baseAddr + DynPage_addr + pageofs;
-                       for (i = 0; i < cnt; i++)
-                               tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
-                       if (count == 0) {
-                               writew((head + len) & rx_mask, ofsAddr + RXrptr);
-                               break;
-                       }
-                       if (++pageno == epage)
+                       len = tty_prepare_flip_string(tty, &dst,
+                                       min(Page_size - pageofs, count));
+                       memcpy_fromio(dst, ofs, len);
+
+                       count -= len;
+                       pageofs = (pageofs + len) & Page_mask;
+                       if (pageofs == 0 && ++pageno == epage)
                                pageno = spage;
-                       pageofs = 0;
-               } while (1);
+               }
+               head = (head + total) & rx_mask;
        }
-       if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
+       writew(head, ofsAddr + RXrptr);
+       if (readb(ofsAddr + FlagStat) & Xoff_state) {
                moxaLowWaterChk = 1;
                port->lowChkFlag = 1;
        }
-       return (total);
+       return total;
 }
 
 
 static int MoxaPortTxQueue(struct moxa_port *port)
 {
        void __iomem *ofsAddr = port->tableAddr;
-       ushort rptr, wptr, mask;
-       int len;
+       u16 rptr, wptr, mask;
 
        rptr = readw(ofsAddr + TXrptr);
        wptr = readw(ofsAddr + TXwptr);
        mask = readw(ofsAddr + TX_mask);
-       len = (wptr - rptr) & mask;
-       return (len);
+       return (wptr - rptr) & mask;
 }
 
 static int MoxaPortTxFree(struct moxa_port *port)
 {
        void __iomem *ofsAddr = port->tableAddr;
-       ushort rptr, wptr, mask;
-       int len;
+       u16 rptr, wptr, mask;
 
        rptr = readw(ofsAddr + TXrptr);
        wptr = readw(ofsAddr + TXwptr);
        mask = readw(ofsAddr + TX_mask);
-       len = mask - ((wptr - rptr) & mask);
-       return (len);
+       return mask - ((wptr - rptr) & mask);
 }
 
 static int MoxaPortRxQueue(struct moxa_port *port)
 {
        void __iomem *ofsAddr = port->tableAddr;
-       ushort rptr, wptr, mask;
-       int len;
+       u16 rptr, wptr, mask;
 
        rptr = readw(ofsAddr + RXrptr);
        wptr = readw(ofsAddr + RXwptr);
        mask = readw(ofsAddr + RX_mask);
-       len = (wptr - rptr) & mask;
-       return (len);
+       return (wptr - rptr) & mask;
 }
 
-
 static void MoxaPortTxDisable(struct moxa_port *port)
 {
        moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
@@ -2341,70 +2029,47 @@ static void MoxaPortTxEnable(struct moxa_port *port)
        moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
 }
 
-
-static int MoxaPortResetBrkCnt(struct moxa_port *port)
-{
-       ushort cnt;
-       cnt = port->breakCnt;
-       port->breakCnt = 0;
-       return (cnt);
-}
-
 static int moxa_get_serial_info(struct moxa_port *info,
-                               struct serial_struct __user *retinfo)
+               struct serial_struct __user *retinfo)
 {
-       struct serial_struct tmp;
-
-       memset(&tmp, 0, sizeof(tmp));
-       tmp.type = info->type;
-       tmp.line = info->tty->index;
-       tmp.port = 0;
-       tmp.irq = 0;
-       tmp.flags = info->asyncflags;
-       tmp.baud_base = 921600;
-       tmp.close_delay = info->close_delay;
-       tmp.custom_divisor = 0;
-       tmp.hub6 = 0;
-       if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
-               return -EFAULT;
-       return (0);
+       struct serial_struct tmp = {
+               .type = info->type,
+               .line = info->port.tty->index,
+               .flags = info->port.flags,
+               .baud_base = 921600,
+               .close_delay = info->port.close_delay
+       };
+       return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
 }
 
 
 static int moxa_set_serial_info(struct moxa_port *info,
-                               struct serial_struct __user *new_info)
+               struct serial_struct __user *new_info)
 {
        struct serial_struct new_serial;
 
-       if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
+       if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
                return -EFAULT;
 
-       if ((new_serial.irq != 0) ||
-           (new_serial.port != 0) ||
-//           (new_serial.type != info->type) ||
-           (new_serial.custom_divisor != 0) ||
-           (new_serial.baud_base != 921600))
-               return (-EPERM);
+       if (new_serial.irq != 0 || new_serial.port != 0 ||
+                       new_serial.custom_divisor != 0 ||
+                       new_serial.baud_base != 921600)
+               return -EPERM;
 
        if (!capable(CAP_SYS_ADMIN)) {
                if (((new_serial.flags & ~ASYNC_USR_MASK) !=
-                    (info->asyncflags & ~ASYNC_USR_MASK)))
-                       return (-EPERM);
-       } else {
-               info->close_delay = new_serial.close_delay * HZ / 100;
-       }
+                    (info->port.flags & ~ASYNC_USR_MASK)))
+                       return -EPERM;
+       } else
+               info->port.close_delay = new_serial.close_delay * HZ / 100;
 
        new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
-       new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
+       new_serial.flags |= (info->port.flags & ASYNC_FLAGS);
 
-       if (new_serial.type == PORT_16550A) {
-               MoxaSetFifo(info, 1);
-       } else {
-               MoxaSetFifo(info, 0);
-       }
+       MoxaSetFifo(info, new_serial.type == PORT_16550A);
 
        info->type = new_serial.type;
-       return (0);
+       return 0;
 }
 
 
@@ -2413,21 +2078,6 @@ static int moxa_set_serial_info(struct moxa_port *info,
  *     Static local functions:                                              *
  *****************************************************************************/
 
-static void moxa_low_water_check(void __iomem *ofsAddr)
-{
-       int len;
-       ushort rptr, wptr, mask;
-
-       if (readb(ofsAddr + FlagStat) & Xoff_state) {
-               rptr = readw(ofsAddr + RXrptr);
-               wptr = readw(ofsAddr + RXwptr);
-               mask = readw(ofsAddr + RX_mask);
-               len = (wptr - rptr) & mask;
-               if (len <= Low_water)
-                       moxafunc(ofsAddr, FC_SendXon, 0);
-       }
-}
-
 static void MoxaSetFifo(struct moxa_port *port, int enable)
 {
        void __iomem *ofsAddr = port->tableAddr;