include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / char / mxser.c
index 8beef50..95c9f54 100644 (file)
@@ -14,7 +14,8 @@
  *      (at your option) any later version.
  *
  *     Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
- *     <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
+ *     <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
+ *     www.moxa.com.
  *     - Fixed x86_64 cleanness
  */
 
 #include <linux/string.h>
 #include <linux/fcntl.h>
 #include <linux/ptrace.h>
-#include <linux/gfp.h>
 #include <linux/ioport.h>
 #include <linux/mm.h>
 #include <linux/delay.h>
 #include <linux/pci.h>
 #include <linux/bitops.h>
+#include <linux/slab.h>
 
 #include <asm/system.h>
 #include <asm/io.h>
@@ -46,7 +47,7 @@
 
 #include "mxser.h"
 
-#define        MXSER_VERSION   "2.0.4"         /* 1.12 */
+#define        MXSER_VERSION   "2.0.5"         /* 1.14 */
 #define        MXSERMAJOR       174
 
 #define MXSER_BOARDS           4       /* Max. boards */
@@ -67,6 +68,7 @@
 #define PCI_DEVICE_ID_POS104UL 0x1044
 #define PCI_DEVICE_ID_CB108    0x1080
 #define PCI_DEVICE_ID_CP102UF  0x1023
+#define PCI_DEVICE_ID_CP112UL  0x1120
 #define PCI_DEVICE_ID_CB114    0x1142
 #define PCI_DEVICE_ID_CP114UL  0x1143
 #define PCI_DEVICE_ID_CB134I   0x1341
@@ -137,7 +139,8 @@ static const struct mxser_cardinfo mxser_cards[] = {
        { "CP-138U series",     8, },
        { "POS-104UL series",   4, },
        { "CP-114UL series",    4, },
-/*30*/ { "CP-102UF series",    2, }
+/*30*/ { "CP-102UF series",    2, },
+       { "CP-112UL series",    2, },
 };
 
 /* driver_data correspond to the lines in the structure above
@@ -168,6 +171,7 @@ static struct pci_device_id mxser_pcibrds[] = {
        { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL),    .driver_data = 28 },
        { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL),     .driver_data = 29 },
        { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF),     .driver_data = 30 },
+       { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP112UL),     .driver_data = 31 },
        { }
 };
 MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
@@ -256,7 +260,6 @@ struct mxser_port {
        struct mxser_mon mon_data;
 
        spinlock_t slock;
-       wait_queue_head_t delta_msr_wait;
 };
 
 struct mxser_board {
@@ -540,74 +543,25 @@ static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
        return status;
 }
 
-static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
-               struct mxser_port *port)
+static int mxser_carrier_raised(struct tty_port *port)
 {
-       DECLARE_WAITQUEUE(wait, current);
-       int retval;
-       int do_clocal = 0;
-       unsigned long flags;
-
-       /*
-        * If non-blocking mode is set, or the port is not enabled,
-        * then make the check up front and then exit.
-        */
-       if ((filp->f_flags & O_NONBLOCK) ||
-                       test_bit(TTY_IO_ERROR, &tty->flags)) {
-               port->port.flags |= ASYNC_NORMAL_ACTIVE;
-               return 0;
-       }
+       struct mxser_port *mp = container_of(port, struct mxser_port, port);
+       return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
+}
 
-       if (tty->termios->c_cflag & CLOCAL)
-               do_clocal = 1;
+static void mxser_dtr_rts(struct tty_port *port, int on)
+{
+       struct mxser_port *mp = container_of(port, struct mxser_port, port);
+       unsigned long flags;
 
-       /*
-        * Block waiting for the carrier detect and the line to become
-        * free (i.e., not in use by the callout).  While we are in
-        * this loop, port->port.count is dropped by one, so that
-        * mxser_close() knows when to free things.  We restore it upon
-        * exit, either normal or abnormal.
-        */
-       retval = 0;
-       add_wait_queue(&port->port.open_wait, &wait);
-
-       spin_lock_irqsave(&port->slock, flags);
-       if (!tty_hung_up_p(filp))
-               port->port.count--;
-       spin_unlock_irqrestore(&port->slock, flags);
-       port->port.blocked_open++;
-       while (1) {
-               spin_lock_irqsave(&port->slock, flags);
-               outb(inb(port->ioaddr + UART_MCR) |
-                       UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
-               spin_unlock_irqrestore(&port->slock, flags);
-               set_current_state(TASK_INTERRUPTIBLE);
-               if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
-                       if (port->port.flags & ASYNC_HUP_NOTIFY)
-                               retval = -EAGAIN;
-                       else
-                               retval = -ERESTARTSYS;
-                       break;
-               }
-               if (!(port->port.flags & ASYNC_CLOSING) &&
-                               (do_clocal ||
-                               (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
-                       break;
-               if (signal_pending(current)) {
-                       retval = -ERESTARTSYS;
-                       break;
-               }
-               schedule();
-       }
-       set_current_state(TASK_RUNNING);
-       remove_wait_queue(&port->port.open_wait, &wait);
-       if (!tty_hung_up_p(filp))
-               port->port.count++;
-       port->port.blocked_open--;
-       if (retval)
-               return retval;
-       port->port.flags |= ASYNC_NORMAL_ACTIVE;
-       return 0;
+       spin_lock_irqsave(&mp->slock, flags);
+       if (on)
+               outb(inb(mp->ioaddr + UART_MCR) |
+                       UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
+       else
+               outb(inb(mp->ioaddr + UART_MCR)&~(UART_MCR_DTR | UART_MCR_RTS),
+                       mp->ioaddr + UART_MCR);
+       spin_unlock_irqrestore(&mp->slock, flags);
 }
 
 static int mxser_set_baud(struct tty_struct *tty, long newspd)
@@ -865,14 +819,13 @@ static void mxser_check_modem_status(struct tty_struct *tty,
        if (status & UART_MSR_DCTS)
                port->icount.cts++;
        port->mon_data.modem_status = status;
-       wake_up_interruptible(&port->delta_msr_wait);
+       wake_up_interruptible(&port->port.delta_msr_wait);
 
        if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
                if (status & UART_MSR_DCD)
                        wake_up_interruptible(&port->port.open_wait);
        }
 
-       tty = tty_port_tty_get(&port->port);
        if (port->port.flags & ASYNC_CTS_FLOW) {
                if (tty->hw_stopped) {
                        if (status & UART_MSR_CTS) {
@@ -902,9 +855,9 @@ static void mxser_check_modem_status(struct tty_struct *tty,
        }
 }
 
-static int mxser_startup(struct tty_struct *tty)
+static int mxser_activate(struct tty_port *port, struct tty_struct *tty)
 {
-       struct mxser_port *info = tty->driver_data;
+       struct mxser_port *info = container_of(port, struct mxser_port, port);
        unsigned long page;
        unsigned long flags;
 
@@ -914,22 +867,13 @@ static int mxser_startup(struct tty_struct *tty)
 
        spin_lock_irqsave(&info->slock, flags);
 
-       if (info->port.flags & ASYNC_INITIALIZED) {
-               free_page(page);
-               spin_unlock_irqrestore(&info->slock, flags);
-               return 0;
-       }
-
        if (!info->ioaddr || !info->type) {
                set_bit(TTY_IO_ERROR, &tty->flags);
                free_page(page);
                spin_unlock_irqrestore(&info->slock, flags);
                return 0;
        }
-       if (info->port.xmit_buf)
-               free_page(page);
-       else
-               info->port.xmit_buf = (unsigned char *) page;
+       info->port.xmit_buf = (unsigned char *) page;
 
        /*
         * Clear the FIFO buffers and disable them
@@ -951,8 +895,7 @@ static int mxser_startup(struct tty_struct *tty)
        if (inb(info->ioaddr + UART_LSR) == 0xff) {
                spin_unlock_irqrestore(&info->slock, flags);
                if (capable(CAP_SYS_ADMIN)) {
-                       if (tty)
-                               set_bit(TTY_IO_ERROR, &tty->flags);
+                       set_bit(TTY_IO_ERROR, &tty->flags);
                        return 0;
                } else
                        return -ENODEV;
@@ -997,34 +940,29 @@ static int mxser_startup(struct tty_struct *tty)
         * and set the speed of the serial port
         */
        mxser_change_speed(tty, NULL);
-       info->port.flags |= ASYNC_INITIALIZED;
        spin_unlock_irqrestore(&info->slock, flags);
 
        return 0;
 }
 
 /*
- * This routine will shutdown a serial port; interrupts maybe disabled, and
- * DTR is dropped if the hangup on close termio flag is on.
+ * This routine will shutdown a serial port
  */
-static void mxser_shutdown(struct tty_struct *tty)
+static void mxser_shutdown_port(struct tty_port *port)
 {
-       struct mxser_port *info = tty->driver_data;
+       struct mxser_port *info = container_of(port, struct mxser_port, port);
        unsigned long flags;
 
-       if (!(info->port.flags & ASYNC_INITIALIZED))
-               return;
-
        spin_lock_irqsave(&info->slock, flags);
 
        /*
         * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
         * here so the queue might never be waken up
         */
-       wake_up_interruptible(&info->delta_msr_wait);
+       wake_up_interruptible(&info->port.delta_msr_wait);
 
        /*
-        * Free the IRQ, if necessary
+        * Free the xmit buffer, if necessary
         */
        if (info->port.xmit_buf) {
                free_page((unsigned long) info->port.xmit_buf);
@@ -1034,10 +972,6 @@ static void mxser_shutdown(struct tty_struct *tty)
        info->IER = 0;
        outb(0x00, info->ioaddr + UART_IER);
 
-       if (tty->termios->c_cflag & HUPCL)
-               info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
-       outb(info->MCR, info->ioaddr + UART_MCR);
-
        /* clear Rx/Tx FIFO's */
        if (info->board->chip_flag)
                outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
@@ -1050,9 +984,6 @@ static void mxser_shutdown(struct tty_struct *tty)
        /* read data port to reset things */
        (void) inb(info->ioaddr + UART_RX);
 
-       set_bit(TTY_IO_ERROR, &tty->flags);
-
-       info->port.flags &= ~ASYNC_INITIALIZED;
 
        if (info->board->chip_flag)
                SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
@@ -1069,8 +1000,7 @@ static void mxser_shutdown(struct tty_struct *tty)
 static int mxser_open(struct tty_struct *tty, struct file *filp)
 {
        struct mxser_port *info;
-       unsigned long flags;
-       int retval, line;
+       int line;
 
        line = tty->index;
        if (line == MXSER_PORTS)
@@ -1081,25 +1011,7 @@ static int mxser_open(struct tty_struct *tty, struct file *filp)
        if (!info->ioaddr)
                return -ENODEV;
 
-       tty->driver_data = info;
-       tty_port_tty_set(&info->port, tty);
-       /*
-        * Start up serial port
-        */
-       spin_lock_irqsave(&info->slock, flags);
-       info->port.count++;
-       spin_unlock_irqrestore(&info->slock, flags);
-       retval = mxser_startup(tty);
-       if (retval)
-               return retval;
-
-       retval = mxser_block_til_ready(tty, filp, info);
-       if (retval)
-               return retval;
-
-       /* unmark here for very high baud rate (ex. 921600 bps) used */
-       tty->low_latency = 1;
-       return 0;
+       return tty_port_open(&info->port, tty, filp);
 }
 
 static void mxser_flush_buffer(struct tty_struct *tty)
@@ -1123,66 +1035,10 @@ static void mxser_flush_buffer(struct tty_struct *tty)
 }
 
 
-/*
- * This routine is called when the serial port gets closed.  First, we
- * wait for the last remaining data to be sent.  Then, we unlink its
- * async structure from the interrupt chain if necessary, and we free
- * that IRQ if nothing is left in the chain.
- */
-static void mxser_close(struct tty_struct *tty, struct file *filp)
+static void mxser_close_port(struct tty_port *port)
 {
-       struct mxser_port *info = tty->driver_data;
-
+       struct mxser_port *info = container_of(port, struct mxser_port, port);
        unsigned long timeout;
-       unsigned long flags;
-
-       if (tty->index == MXSER_PORTS)
-               return;
-       if (!info)
-               return;
-
-       spin_lock_irqsave(&info->slock, flags);
-
-       if (tty_hung_up_p(filp)) {
-               spin_unlock_irqrestore(&info->slock, flags);
-               return;
-       }
-       if ((tty->count == 1) && (info->port.count != 1)) {
-               /*
-                * Uh, oh.  tty->count is 1, which means that the tty
-                * structure will be freed.  Info->port.count should always
-                * be one in these conditions.  If it's greater than
-                * one, we've got real problems, since it means the
-                * serial port won't be shutdown.
-                */
-               printk(KERN_ERR "mxser_close: bad serial port count; "
-                       "tty->count is 1, info->port.count is %d\n", info->port.count);
-               info->port.count = 1;
-       }
-       if (--info->port.count < 0) {
-               printk(KERN_ERR "mxser_close: bad serial port count for "
-                       "ttys%d: %d\n", tty->index, info->port.count);
-               info->port.count = 0;
-       }
-       if (info->port.count) {
-               spin_unlock_irqrestore(&info->slock, flags);
-               return;
-       }
-       info->port.flags |= ASYNC_CLOSING;
-       spin_unlock_irqrestore(&info->slock, flags);
-       /*
-        * Save the termios structure, since this port may have
-        * separate termios for callout and dialin.
-        */
-       if (info->port.flags & ASYNC_NORMAL_ACTIVE)
-               info->normal_termios = *tty->termios;
-       /*
-        * Now we wait for the transmit buffer to clear; and we notify
-        * the line discipline to only process XON/XOFF characters.
-        */
-       tty->closing = 1;
-       if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
-               tty_wait_until_sent(tty, info->port.closing_wait);
        /*
         * At this point we stop accepting input.  To do this, we
         * disable the receive line status interrupts, and tell the
@@ -1193,34 +1049,45 @@ static void mxser_close(struct tty_struct *tty, struct file *filp)
        if (info->board->chip_flag)
                info->IER &= ~MOXA_MUST_RECV_ISR;
 
-       if (info->port.flags & ASYNC_INITIALIZED) {
-               outb(info->IER, info->ioaddr + UART_IER);
-               /*
-                * Before we drop DTR, make sure the UART transmitter
-                * has completely drained; this is especially
-                * important if there is a transmit FIFO!
-                */
-               timeout = jiffies + HZ;
-               while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
-                       schedule_timeout_interruptible(5);
-                       if (time_after(jiffies, timeout))
-                               break;
-               }
+       outb(info->IER, info->ioaddr + UART_IER);
+       /*
+        * Before we drop DTR, make sure the UART transmitter
+        * has completely drained; this is especially
+        * important if there is a transmit FIFO!
+        */
+       timeout = jiffies + HZ;
+       while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
+               schedule_timeout_interruptible(5);
+               if (time_after(jiffies, timeout))
+                       break;
        }
-       mxser_shutdown(tty);
-
-       mxser_flush_buffer(tty);
-       tty_ldisc_flush(tty);
+}
 
-       tty->closing = 0;
-       tty_port_tty_set(&info->port, NULL);
-       if (info->port.blocked_open) {
-               if (info->port.close_delay)
-                       schedule_timeout_interruptible(info->port.close_delay);
-               wake_up_interruptible(&info->port.open_wait);
-       }
+/*
+ * This routine is called when the serial port gets closed.  First, we
+ * wait for the last remaining data to be sent.  Then, we unlink its
+ * async structure from the interrupt chain if necessary, and we free
+ * that IRQ if nothing is left in the chain.
+ */
+static void mxser_close(struct tty_struct *tty, struct file *filp)
+{
+       struct mxser_port *info = tty->driver_data;
+       struct tty_port *port = &info->port;
 
-       info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
+       if (tty->index == MXSER_PORTS)
+               return;
+       if (tty_port_close_start(port, tty, filp) == 0)
+               return;
+       mutex_lock(&port->mutex);
+       mxser_close_port(port);
+       mxser_flush_buffer(tty);
+       mxser_shutdown_port(port);
+       clear_bit(ASYNCB_INITIALIZED, &port->flags);
+       mutex_unlock(&port->mutex);
+       /* Right now the tty_port set is done outside of the close_end helper
+          as we don't yet have everyone using refcounts */     
+       tty_port_close_end(port, tty);
+       tty_port_tty_set(port, NULL);
 }
 
 static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
@@ -1360,6 +1227,7 @@ static int mxser_set_serial_info(struct tty_struct *tty,
                struct serial_struct __user *new_info)
 {
        struct mxser_port *info = tty->driver_data;
+       struct tty_port *port = &info->port;
        struct serial_struct new_serial;
        speed_t baud;
        unsigned long sl_flags;
@@ -1375,7 +1243,7 @@ static int mxser_set_serial_info(struct tty_struct *tty,
                        new_serial.port != info->ioaddr)
                return -EINVAL;
 
-       flags = info->port.flags & ASYNC_SPD_MASK;
+       flags = port->flags & ASYNC_SPD_MASK;
 
        if (!capable(CAP_SYS_ADMIN)) {
                if ((new_serial.baud_base != info->baud_base) ||
@@ -1389,16 +1257,17 @@ static int mxser_set_serial_info(struct tty_struct *tty,
                 * OK, past this point, all the error checking has been done.
                 * At this point, we start making changes.....
                 */
-               info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
+               port->flags = ((port->flags & ~ASYNC_FLAGS) |
                                (new_serial.flags & ASYNC_FLAGS));
-               info->port.close_delay = new_serial.close_delay * HZ / 100;
-               info->port.closing_wait = new_serial.closing_wait * HZ / 100;
-               tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
-                                                               ? 1 : 0;
-               if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
+               port->close_delay = new_serial.close_delay * HZ / 100;
+               port->closing_wait = new_serial.closing_wait * HZ / 100;
+               tty->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
+               if ((port->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
                                (new_serial.baud_base != info->baud_base ||
                                new_serial.custom_divisor !=
                                info->custom_divisor)) {
+                       if (new_serial.custom_divisor == 0)
+                               return -EINVAL;
                        baud = new_serial.baud_base / new_serial.custom_divisor;
                        tty_encode_baud_rate(tty, baud, baud);
                }
@@ -1408,15 +1277,17 @@ static int mxser_set_serial_info(struct tty_struct *tty,
 
        process_txrx_fifo(info);
 
-       if (info->port.flags & ASYNC_INITIALIZED) {
-               if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
+       if (test_bit(ASYNCB_INITIALIZED, &port->flags)) {
+               if (flags != (port->flags & ASYNC_SPD_MASK)) {
                        spin_lock_irqsave(&info->slock, sl_flags);
                        mxser_change_speed(tty, NULL);
                        spin_unlock_irqrestore(&info->slock, sl_flags);
                }
-       } else
-               retval = mxser_startup(tty);
-
+       } else {
+               retval = mxser_activate(port, tty);
+               if (retval == 0)
+                       set_bit(ASYNCB_INITIALIZED, &port->flags);
+       }
        return retval;
 }
 
@@ -1605,7 +1476,8 @@ static int __init mxser_read_register(int port, unsigned short *regs)
 
 static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
 {
-       struct mxser_port *port;
+       struct mxser_port *ip;
+       struct tty_port *port;
        struct tty_struct *tty;
        int result, status;
        unsigned int i, j;
@@ -1621,38 +1493,39 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
 
        case MOXA_CHKPORTENABLE:
                result = 0;
-               lock_kernel();
                for (i = 0; i < MXSER_BOARDS; i++)
                        for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
                                if (mxser_boards[i].ports[j].ioaddr)
                                        result |= (1 << i);
-               unlock_kernel();
                return put_user(result, (unsigned long __user *)argp);
        case MOXA_GETDATACOUNT:
-               lock_kernel();
+               /* The receive side is locked by port->slock but it isn't
+                  clear that an exact snapshot is worth copying here */
                if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
                        ret = -EFAULT;
-               unlock_kernel();
                return ret;
        case MOXA_GETMSTATUS: {
                struct mxser_mstatus ms, __user *msu = argp;
-               lock_kernel();
                for (i = 0; i < MXSER_BOARDS; i++)
                        for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
-                               port = &mxser_boards[i].ports[j];
+                               ip = &mxser_boards[i].ports[j];
+                               port = &ip->port;
                                memset(&ms, 0, sizeof(ms));
 
-                               if (!port->ioaddr)
+                               mutex_lock(&port->mutex);
+                               if (!ip->ioaddr)
                                        goto copy;
                                
-                               tty = tty_port_tty_get(&port->port);
+                               tty = tty_port_tty_get(port);
 
                                if (!tty || !tty->termios)
-                                       ms.cflag = port->normal_termios.c_cflag;
+                                       ms.cflag = ip->normal_termios.c_cflag;
                                else
                                        ms.cflag = tty->termios->c_cflag;
                                tty_kref_put(tty);
-                               status = inb(port->ioaddr + UART_MSR);
+                               spin_lock_irq(&ip->slock);
+                               status = inb(ip->ioaddr + UART_MSR);
+                               spin_unlock_irq(&ip->slock);
                                if (status & UART_MSR_DCD)
                                        ms.dcd = 1;
                                if (status & UART_MSR_DSR)
@@ -1660,13 +1533,11 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
                                if (status & UART_MSR_CTS)
                                        ms.cts = 1;
                        copy:
-                               if (copy_to_user(msu, &ms, sizeof(ms))) {
-                                       unlock_kernel();
+                               mutex_unlock(&port->mutex);
+                               if (copy_to_user(msu, &ms, sizeof(ms)))
                                        return -EFAULT;
-                               }
                                msu++;
                        }
-               unlock_kernel();
                return 0;
        }
        case MOXA_ASPP_MON_EXT: {
@@ -1678,41 +1549,48 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
                if (!me)
                        return -ENOMEM;
 
-               lock_kernel();
                for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
                        for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
                                if (p >= ARRAY_SIZE(me->rx_cnt)) {
                                        i = MXSER_BOARDS;
                                        break;
                                }
-                               port = &mxser_boards[i].ports[j];
-                               if (!port->ioaddr)
+                               ip = &mxser_boards[i].ports[j];
+                               port = &ip->port;
+
+                               mutex_lock(&port->mutex);
+                               if (!ip->ioaddr) {
+                                       mutex_unlock(&port->mutex);
                                        continue;
+                               }
 
-                               status = mxser_get_msr(port->ioaddr, 0, p);
+                               spin_lock_irq(&ip->slock);
+                               status = mxser_get_msr(ip->ioaddr, 0, p);
 
                                if (status & UART_MSR_TERI)
-                                       port->icount.rng++;
+                                       ip->icount.rng++;
                                if (status & UART_MSR_DDSR)
-                                       port->icount.dsr++;
+                                       ip->icount.dsr++;
                                if (status & UART_MSR_DDCD)
-                                       port->icount.dcd++;
+                                       ip->icount.dcd++;
                                if (status & UART_MSR_DCTS)
-                                       port->icount.cts++;
+                                       ip->icount.cts++;
 
-                               port->mon_data.modem_status = status;
-                               me->rx_cnt[p] = port->mon_data.rxcnt;
-                               me->tx_cnt[p] = port->mon_data.txcnt;
-                               me->up_rxcnt[p] = port->mon_data.up_rxcnt;
-                               me->up_txcnt[p] = port->mon_data.up_txcnt;
+                               ip->mon_data.modem_status = status;
+                               me->rx_cnt[p] = ip->mon_data.rxcnt;
+                               me->tx_cnt[p] = ip->mon_data.txcnt;
+                               me->up_rxcnt[p] = ip->mon_data.up_rxcnt;
+                               me->up_txcnt[p] = ip->mon_data.up_txcnt;
                                me->modem_status[p] =
-                                       port->mon_data.modem_status;
-                               tty = tty_port_tty_get(&port->port);
+                                       ip->mon_data.modem_status;
+                               spin_unlock_irq(&ip->slock);
+
+                               tty = tty_port_tty_get(&ip->port);
 
                                if (!tty || !tty->termios) {
-                                       cflag = port->normal_termios.c_cflag;
-                                       iflag = port->normal_termios.c_iflag;
-                                       me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
+                                       cflag = ip->normal_termios.c_cflag;
+                                       iflag = ip->normal_termios.c_iflag;
+                                       me->baudrate[p] = tty_termios_baud_rate(&ip->normal_termios);
                                } else {
                                        cflag = tty->termios->c_cflag;
                                        iflag = tty->termios->c_iflag;
@@ -1731,16 +1609,15 @@ static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
                                if (iflag & (IXON | IXOFF))
                                        me->flowctrl[p] |= 0x0C;
 
-                               if (port->type == PORT_16550A)
+                               if (ip->type == PORT_16550A)
                                        me->fifo[p] = 1;
 
-                               opmode = inb(port->opmode_ioaddr) >>
-                                               ((p % 4) * 2);
+                               opmode = inb(ip->opmode_ioaddr)>>((p % 4) * 2);
                                opmode &= OP_MODE_MASK;
                                me->iftype[p] = opmode;
+                               mutex_unlock(&port->mutex);
                        }
                }
-               unlock_kernel();
                if (copy_to_user(argp, me, sizeof(*me)))
                        ret = -EFAULT;
                kfree(me);
@@ -1777,6 +1654,7 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
                unsigned int cmd, unsigned long arg)
 {
        struct mxser_port *info = tty->driver_data;
+       struct tty_port *port = &info->port;
        struct async_icount cnow;
        unsigned long flags;
        void __user *argp = (void __user *)arg;
@@ -1801,20 +1679,20 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
                                        opmode != RS422_MODE &&
                                        opmode != RS485_4WIRE_MODE)
                                return -EFAULT;
-                       lock_kernel();
                        mask = ModeMask[p];
                        shiftbit = p * 2;
+                       spin_lock_irq(&info->slock);
                        val = inb(info->opmode_ioaddr);
                        val &= mask;
                        val |= (opmode << shiftbit);
                        outb(val, info->opmode_ioaddr);
-                       unlock_kernel();
+                       spin_unlock_irq(&info->slock);
                } else {
-                       lock_kernel();
                        shiftbit = p * 2;
+                       spin_lock_irq(&info->slock);
                        opmode = inb(info->opmode_ioaddr) >> shiftbit;
+                       spin_unlock_irq(&info->slock);
                        opmode &= OP_MODE_MASK;
-                       unlock_kernel();
                        if (put_user(opmode, (int __user *)argp))
                                return -EFAULT;
                }
@@ -1827,14 +1705,14 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
 
        switch (cmd) {
        case TIOCGSERIAL:
-               lock_kernel();
+               mutex_lock(&port->mutex);
                retval = mxser_get_serial_info(tty, argp);
-               unlock_kernel();
+               mutex_unlock(&port->mutex);
                return retval;
        case TIOCSSERIAL:
-               lock_kernel();
+               mutex_lock(&port->mutex);
                retval = mxser_set_serial_info(tty, argp);
-               unlock_kernel();
+               mutex_unlock(&port->mutex);
                return retval;
        case TIOCSERGETLSR:     /* Get line status register */
                return  mxser_get_lsr_info(info, argp);
@@ -1849,7 +1727,7 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
                cnow = info->icount;    /* note the counters on entry */
                spin_unlock_irqrestore(&info->slock, flags);
 
-               return wait_event_interruptible(info->delta_msr_wait,
+               return wait_event_interruptible(info->port.delta_msr_wait,
                                mxser_cflags_changed(info, arg, &cnow));
        /*
         * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
@@ -1880,31 +1758,33 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
        case MOXA_HighSpeedOn:
                return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
        case MOXA_SDS_RSTICOUNTER:
-               lock_kernel();
+               spin_lock_irq(&info->slock);
                info->mon_data.rxcnt = 0;
                info->mon_data.txcnt = 0;
-               unlock_kernel();
+               spin_unlock_irq(&info->slock);
                return 0;
 
        case MOXA_ASPP_OQUEUE:{
                int len, lsr;
 
-               lock_kernel();
                len = mxser_chars_in_buffer(tty);
-               lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
+               spin_lock(&info->slock);
+               lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_THRE;
+               spin_unlock_irq(&info->slock);
                len += (lsr ? 0 : 1);
-               unlock_kernel();
 
                return put_user(len, (int __user *)argp);
        }
        case MOXA_ASPP_MON: {
                int mcr, status;
 
-               lock_kernel();
+               spin_lock(&info->slock);
                status = mxser_get_msr(info->ioaddr, 1, tty->index);
                mxser_check_modem_status(tty, info, status);
 
                mcr = inb(info->ioaddr + UART_MCR);
+               spin_unlock(&info->slock);
+
                if (mcr & MOXA_MUST_MCR_XON_FLAG)
                        info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
                else
@@ -1919,7 +1799,7 @@ static int mxser_ioctl(struct tty_struct *tty, struct file *file,
                        info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
                else
                        info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
-               unlock_kernel();
+
                if (copy_to_user(argp, &info->mon_data,
                                sizeof(struct mxser_mon)))
                        return -EFAULT;
@@ -2078,6 +1958,7 @@ static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
 {
        struct mxser_port *info = tty->driver_data;
        unsigned long orig_jiffies, char_time;
+       unsigned long flags;
        int lsr;
 
        if (info->type == PORT_UNKNOWN)
@@ -2117,19 +1998,21 @@ static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
                timeout, char_time);
        printk("jiff=%lu...", jiffies);
 #endif
-       lock_kernel();
+       spin_lock_irqsave(&info->slock, flags);
        while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
                printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
 #endif
+               spin_unlock_irqrestore(&info->slock, flags);
                schedule_timeout_interruptible(char_time);
+               spin_lock_irqsave(&info->slock, flags);
                if (signal_pending(current))
                        break;
                if (timeout && time_after(jiffies, orig_jiffies + timeout))
                        break;
        }
+       spin_unlock_irqrestore(&info->slock, flags);
        set_current_state(TASK_RUNNING);
-       unlock_kernel();
 
 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
        printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
@@ -2144,11 +2027,7 @@ static void mxser_hangup(struct tty_struct *tty)
        struct mxser_port *info = tty->driver_data;
 
        mxser_flush_buffer(tty);
-       mxser_shutdown(tty);
-       info->port.count = 0;
-       info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
-       tty_port_tty_set(&info->port, NULL);
-       wake_up_interruptible(&info->port.open_wait);
+       tty_port_hangup(&info->port);
 }
 
 /*
@@ -2448,6 +2327,13 @@ static const struct tty_operations mxser_ops = {
        .tiocmset = mxser_tiocmset,
 };
 
+struct tty_port_operations mxser_port_ops = {
+       .carrier_raised = mxser_carrier_raised,
+       .dtr_rts = mxser_dtr_rts,
+       .activate = mxser_activate,
+       .shutdown = mxser_shutdown_port,
+};
+
 /*
  * The MOXA Smartio/Industio serial driver boot-time initialization code!
  */
@@ -2481,6 +2367,7 @@ static int __devinit mxser_initbrd(struct mxser_board *brd,
        for (i = 0; i < brd->info->nports; i++) {
                info = &brd->ports[i];
                tty_port_init(&info->port);
+               info->port.ops = &mxser_port_ops;
                info->board = brd;
                info->stop_rx = 0;
                info->ldisc_stop_rx = 0;
@@ -2498,7 +2385,6 @@ static int __devinit mxser_initbrd(struct mxser_board *brd,
                info->port.close_delay = 5 * HZ / 10;
                info->port.closing_wait = 30 * HZ;
                info->normal_termios = mxvar_sdriver->init_termios;
-               init_waitqueue_head(&info->delta_msr_wait);
                memset(&info->mon_data, 0, sizeof(struct mxser_mon));
                info->err_shadow = 0;
                spin_lock_init(&info->slock);
@@ -2799,7 +2685,7 @@ static int __init mxser_module_init(void)
                        continue;
 
                brd = &mxser_boards[m];
-               retval = mxser_get_ISA_conf(!ioaddr[b], brd);
+               retval = mxser_get_ISA_conf(ioaddr[b], brd);
                if (retval <= 0) {
                        brd->info = NULL;
                        continue;