Don't unconditionally set in_sync on newly added device in raid5_reshape
[safe/jmp/linux-2.6] / drivers / char / cyclades.c
index 47cd7b8..4254457 100644 (file)
@@ -90,7 +90,6 @@
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 
-static void cy_throttle(struct tty_struct *tty);
 static void cy_send_xchar(struct tty_struct *tty, char ch);
 
 #ifndef SERIAL_XMIT_SIZE
@@ -298,6 +297,20 @@ static void cyz_rx_restart(unsigned long);
 static struct timer_list cyz_rx_full_timer[NR_PORTS];
 #endif                         /* CONFIG_CYZ_INTR */
 
+static inline void cyy_writeb(struct cyclades_port *port, u32 reg, u8 val)
+{
+       struct cyclades_card *card = port->card;
+
+       cy_writeb(port->u.cyy.base_addr + (reg << card->bus_index), val);
+}
+
+static inline u8 cyy_readb(struct cyclades_port *port, u32 reg)
+{
+       struct cyclades_card *card = port->card;
+
+       return readb(port->u.cyy.base_addr + (reg << card->bus_index));
+}
+
 static inline bool cy_is_Z(struct cyclades_card *card)
 {
        return card->num_chips == (unsigned int)-1;
@@ -350,13 +363,14 @@ static inline int serial_paranoia_check(struct cyclades_port *info,
 
    This function is only called from inside spinlock-protected code.
  */
-static int cyy_issue_cmd(void __iomem *base_addr, u_char cmd, int index)
+static int __cyy_issue_cmd(void __iomem *base_addr, u8 cmd, int index)
 {
+       void __iomem *ccr = base_addr + (CyCCR << index);
        unsigned int i;
 
        /* Check to see that the previous command has completed */
        for (i = 0; i < 100; i++) {
-               if (readb(base_addr + (CyCCR << index)) == 0)
+               if (readb(ccr) == 0)
                        break;
                udelay(10L);
        }
@@ -366,10 +380,16 @@ static int cyy_issue_cmd(void __iomem *base_addr, u_char cmd, int index)
                return -1;
 
        /* Issue the new command */
-       cy_writeb(base_addr + (CyCCR << index), cmd);
+       cy_writeb(ccr, cmd);
 
        return 0;
-}                              /* cyy_issue_cmd */
+}
+
+static inline int cyy_issue_cmd(struct cyclades_port *port, u8 cmd)
+{
+       return __cyy_issue_cmd(port->u.cyy.base_addr, cmd,
+                       port->card->bus_index);
+}
 
 #ifdef CONFIG_ISA
 /* ISA interrupt detection code */
@@ -394,7 +414,7 @@ static unsigned detect_isa_irq(void __iomem *address)
        /* Enable the Tx interrupts on the CD1400 */
        local_irq_save(flags);
        cy_writeb(address + (CyCAR << index), 0);
-       cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
+       __cyy_issue_cmd(address, CyCHAN_CTL | CyENB_XMTR, index);
 
        cy_writeb(address + (CyCAR << index), 0);
        cy_writeb(address + (CySRER << index),
@@ -428,7 +448,7 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
        struct cyclades_port *info;
        struct tty_struct *tty;
        int len, index = cinfo->bus_index;
-       u8 save_xir, channel, save_car, data, char_count;
+       u8 ivr, save_xir, channel, save_car, data, char_count;
 
 #ifdef CY_DEBUG_INTERRUPTS
        printk(KERN_DEBUG "cyy_interrupt: rcvd intr, chip %d\n", chip);
@@ -437,26 +457,25 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
        save_xir = readb(base_addr + (CyRIR << index));
        channel = save_xir & CyIRChannel;
        info = &cinfo->ports[channel + chip * 4];
-       save_car = readb(base_addr + (CyCAR << index));
-       cy_writeb(base_addr + (CyCAR << index), save_xir);
+       save_car = cyy_readb(info, CyCAR);
+       cyy_writeb(info, CyCAR, save_xir);
+       ivr = cyy_readb(info, CyRIVR) & CyIVRMask;
 
        tty = tty_port_tty_get(&info->port);
        /* if there is nowhere to put the data, discard it */
        if (tty == NULL) {
-               if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
-                               CyIVRRxEx) {    /* exception */
-                       data = readb(base_addr + (CyRDSR << index));
+               if (ivr == CyIVRRxEx) { /* exception */
+                       data = cyy_readb(info, CyRDSR);
                } else {        /* normal character reception */
-                       char_count = readb(base_addr + (CyRDCR << index));
+                       char_count = cyy_readb(info, CyRDCR);
                        while (char_count--)
-                               data = readb(base_addr + (CyRDSR << index));
+                               data = cyy_readb(info, CyRDSR);
                }
                goto end;
        }
        /* there is an open port for this data */
-       if ((readb(base_addr + (CyRIVR << index)) & CyIVRMask) ==
-                       CyIVRRxEx) {    /* exception */
-               data = readb(base_addr + (CyRDSR << index));
+       if (ivr == CyIVRRxEx) { /* exception */
+               data = cyy_readb(info, CyRDSR);
 
                /* For statistics only */
                if (data & CyBREAK)
@@ -477,22 +496,22 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
                        if (data & info->read_status_mask) {
                                if (data & CyBREAK) {
                                        tty_insert_flip_char(tty,
-                                               readb(base_addr + (CyRDSR <<
-                                                       index)), TTY_BREAK);
+                                               cyy_readb(info, CyRDSR),
+                                               TTY_BREAK);
                                        info->icount.rx++;
                                        if (info->port.flags & ASYNC_SAK)
                                                do_SAK(tty);
                                } else if (data & CyFRAME) {
                                        tty_insert_flip_char(tty,
-                                               readb(base_addr + (CyRDSR <<
-                                                       index)), TTY_FRAME);
+                                               cyy_readb(info, CyRDSR),
+                                               TTY_FRAME);
                                        info->icount.rx++;
                                        info->idle_stats.frame_errs++;
                                } else if (data & CyPARITY) {
                                        /* Pieces of seven... */
                                        tty_insert_flip_char(tty,
-                                               readb(base_addr + (CyRDSR <<
-                                                       index)), TTY_PARITY);
+                                               cyy_readb(info, CyRDSR),
+                                               TTY_PARITY);
                                        info->icount.rx++;
                                        info->idle_stats.parity_errs++;
                                } else if (data & CyOVERRUN) {
@@ -504,8 +523,8 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
                                           the next incoming character.
                                         */
                                        tty_insert_flip_char(tty,
-                                               readb(base_addr + (CyRDSR <<
-                                                       index)), TTY_FRAME);
+                                               cyy_readb(info, CyRDSR),
+                                               TTY_FRAME);
                                        info->icount.rx++;
                                        info->idle_stats.overruns++;
                                /* These two conditions may imply */
@@ -529,7 +548,7 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
                }
        } else {        /* normal character reception */
                /* load # chars available from the chip */
-               char_count = readb(base_addr + (CyRDCR << index));
+               char_count = cyy_readb(info, CyRDCR);
 
 #ifdef CY_ENABLE_MONITORING
                ++info->mon.int_count;
@@ -540,7 +559,7 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
 #endif
                len = tty_buffer_request_room(tty, char_count);
                while (len--) {
-                       data = readb(base_addr + (CyRDSR << index));
+                       data = cyy_readb(info, CyRDSR);
                        tty_insert_flip_char(tty, data, TTY_NORMAL);
                        info->idle_stats.recv_bytes++;
                        info->icount.rx++;
@@ -554,8 +573,8 @@ static void cyy_chip_rx(struct cyclades_card *cinfo, int chip,
        tty_kref_put(tty);
 end:
        /* end of service */
-       cy_writeb(base_addr + (CyRIR << index), save_xir & 0x3f);
-       cy_writeb(base_addr + (CyCAR << index), save_car);
+       cyy_writeb(info, CyRIR, save_xir & 0x3f);
+       cyy_writeb(info, CyCAR, save_car);
 }
 
 static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
@@ -588,8 +607,7 @@ static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
        info = &cinfo->ports[channel + chip * 4];
        tty = tty_port_tty_get(&info->port);
        if (tty == NULL) {
-               cy_writeb(base_addr + (CySRER << index),
-                         readb(base_addr + (CySRER << index)) & ~CyTxRdy);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
                goto end;
        }
 
@@ -598,7 +616,7 @@ static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
 
        if (info->x_char) {     /* send special char */
                outch = info->x_char;
-               cy_writeb(base_addr + (CyTDR << index), outch);
+               cyy_writeb(info, CyTDR, outch);
                char_count--;
                info->icount.tx++;
                info->x_char = 0;
@@ -606,14 +624,14 @@ static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
 
        if (info->breakon || info->breakoff) {
                if (info->breakon) {
-                       cy_writeb(base_addr + (CyTDR << index), 0);
-                       cy_writeb(base_addr + (CyTDR << index), 0x81);
+                       cyy_writeb(info, CyTDR, 0);
+                       cyy_writeb(info, CyTDR, 0x81);
                        info->breakon = 0;
                        char_count -= 2;
                }
                if (info->breakoff) {
-                       cy_writeb(base_addr + (CyTDR << index), 0);
-                       cy_writeb(base_addr + (CyTDR << index), 0x83);
+                       cyy_writeb(info, CyTDR, 0);
+                       cyy_writeb(info, CyTDR, 0x83);
                        info->breakoff = 0;
                        char_count -= 2;
                }
@@ -621,27 +639,23 @@ static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
 
        while (char_count-- > 0) {
                if (!info->xmit_cnt) {
-                       if (readb(base_addr + (CySRER << index)) & CyTxMpty) {
-                               cy_writeb(base_addr + (CySRER << index),
-                                       readb(base_addr + (CySRER << index)) &
-                                               ~CyTxMpty);
+                       if (cyy_readb(info, CySRER) & CyTxMpty) {
+                               cyy_writeb(info, CySRER,
+                                       cyy_readb(info, CySRER) & ~CyTxMpty);
                        } else {
-                               cy_writeb(base_addr + (CySRER << index),
-                                       (readb(base_addr + (CySRER << index)) &
-                                               ~CyTxRdy) | CyTxMpty);
+                               cyy_writeb(info, CySRER, CyTxMpty |
+                                       (cyy_readb(info, CySRER) & ~CyTxRdy));
                        }
                        goto done;
                }
                if (info->port.xmit_buf == NULL) {
-                       cy_writeb(base_addr + (CySRER << index),
-                               readb(base_addr + (CySRER << index)) &
-                                       ~CyTxRdy);
+                       cyy_writeb(info, CySRER,
+                               cyy_readb(info, CySRER) & ~CyTxRdy);
                        goto done;
                }
                if (tty->stopped || tty->hw_stopped) {
-                       cy_writeb(base_addr + (CySRER << index),
-                               readb(base_addr + (CySRER << index)) &
-                                       ~CyTxRdy);
+                       cyy_writeb(info, CySRER,
+                               cyy_readb(info, CySRER) & ~CyTxRdy);
                        goto done;
                }
                /* Because the Embedded Transmit Commands have been enabled,
@@ -658,15 +672,15 @@ static void cyy_chip_tx(struct cyclades_card *cinfo, unsigned int chip,
                        info->xmit_cnt--;
                        info->xmit_tail = (info->xmit_tail + 1) &
                                        (SERIAL_XMIT_SIZE - 1);
-                       cy_writeb(base_addr + (CyTDR << index), outch);
+                       cyy_writeb(info, CyTDR, outch);
                        info->icount.tx++;
                } else {
                        if (char_count > 1) {
                                info->xmit_cnt--;
                                info->xmit_tail = (info->xmit_tail + 1) &
                                        (SERIAL_XMIT_SIZE - 1);
-                               cy_writeb(base_addr + (CyTDR << index), outch);
-                               cy_writeb(base_addr + (CyTDR << index), 0);
+                               cyy_writeb(info, CyTDR, outch);
+                               cyy_writeb(info, CyTDR, 0);
                                info->icount.tx++;
                                char_count--;
                        }
@@ -678,8 +692,8 @@ done:
        tty_kref_put(tty);
 end:
        /* end of service */
-       cy_writeb(base_addr + (CyTIR << index), save_xir & 0x3f);
-       cy_writeb(base_addr + (CyCAR << index), save_car);
+       cyy_writeb(info, CyTIR, save_xir & 0x3f);
+       cyy_writeb(info, CyCAR, save_car);
 }
 
 static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
@@ -694,11 +708,11 @@ static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
        save_xir = readb(base_addr + (CyMIR << index));
        channel = save_xir & CyIRChannel;
        info = &cinfo->ports[channel + chip * 4];
-       save_car = readb(base_addr + (CyCAR << index));
-       cy_writeb(base_addr + (CyCAR << index), save_xir);
+       save_car = cyy_readb(info, CyCAR);
+       cyy_writeb(info, CyCAR, save_xir);
 
-       mdm_change = readb(base_addr + (CyMISR << index));
-       mdm_status = readb(base_addr + (CyMSVR1 << index));
+       mdm_change = cyy_readb(info, CyMISR);
+       mdm_status = cyy_readb(info, CyMSVR1);
 
        tty = tty_port_tty_get(&info->port);
        if (!tty)
@@ -715,7 +729,7 @@ static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
                if (mdm_change & CyRI)
                        info->icount.rng++;
 
-               wake_up_interruptible(&info->delta_msr_wait);
+               wake_up_interruptible(&info->port.delta_msr_wait);
        }
 
        if ((mdm_change & CyDCD) && (info->port.flags & ASYNC_CHECK_CD)) {
@@ -730,9 +744,8 @@ static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
                                /* cy_start isn't used
                                   because... !!! */
                                tty->hw_stopped = 0;
-                               cy_writeb(base_addr + (CySRER << index),
-                                       readb(base_addr + (CySRER << index)) |
-                                               CyTxRdy);
+                               cyy_writeb(info, CySRER,
+                                       cyy_readb(info, CySRER) | CyTxRdy);
                                tty_wakeup(tty);
                        }
                } else {
@@ -740,9 +753,8 @@ static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
                                /* cy_stop isn't used
                                   because ... !!! */
                                tty->hw_stopped = 1;
-                               cy_writeb(base_addr + (CySRER << index),
-                                       readb(base_addr + (CySRER << index)) &
-                                               ~CyTxRdy);
+                               cyy_writeb(info, CySRER,
+                                       cyy_readb(info, CySRER) & ~CyTxRdy);
                        }
                }
        }
@@ -753,8 +765,8 @@ static void cyy_chip_modem(struct cyclades_card *cinfo, int chip,
        tty_kref_put(tty);
 end:
        /* end of service */
-       cy_writeb(base_addr + (CyMIR << index), save_xir & 0x3f);
-       cy_writeb(base_addr + (CyCAR << index), save_car);
+       cyy_writeb(info, CyMIR, save_xir & 0x3f);
+       cyy_writeb(info, CyCAR, save_car);
 }
 
 /* The real interrupt service routine is called
@@ -829,58 +841,48 @@ static void cyy_change_rts_dtr(struct cyclades_port *info, unsigned int set,
                unsigned int clear)
 {
        struct cyclades_card *card = info->card;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel = info->line - card->first_line;
+       u32 rts, dtr, msvrr, msvrd;
 
-       channel = info->line - card->first_line;
-       chip = channel >> 2;
        channel &= 0x03;
-       index = card->bus_index;
-       base_addr = card->base_addr + (cy_chip_offset[chip] << index);
 
+       if (info->rtsdtr_inv) {
+               msvrr = CyMSVR2;
+               msvrd = CyMSVR1;
+               rts = CyDTR;
+               dtr = CyRTS;
+       } else {
+               msvrr = CyMSVR1;
+               msvrd = CyMSVR2;
+               rts = CyRTS;
+               dtr = CyDTR;
+       }
        if (set & TIOCM_RTS) {
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               if (info->rtsdtr_inv) {
-                       cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
-               } else {
-                       cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
-               }
+               cyy_writeb(info, CyCAR, channel);
+               cyy_writeb(info, msvrr, rts);
        }
        if (clear & TIOCM_RTS) {
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               if (info->rtsdtr_inv) {
-                       cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
-               } else {
-                       cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
-               }
+               cyy_writeb(info, CyCAR, channel);
+               cyy_writeb(info, msvrr, ~rts);
        }
        if (set & TIOCM_DTR) {
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               if (info->rtsdtr_inv) {
-                       cy_writeb(base_addr + (CyMSVR1 << index), CyRTS);
-               } else {
-                       cy_writeb(base_addr + (CyMSVR2 << index), CyDTR);
-               }
+               cyy_writeb(info, CyCAR, channel);
+               cyy_writeb(info, msvrd, dtr);
 #ifdef CY_DEBUG_DTR
                printk(KERN_DEBUG "cyc:set_modem_info raising DTR\n");
                printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
-                       readb(base_addr + (CyMSVR1 << index)),
-                       readb(base_addr + (CyMSVR2 << index)));
+                       cyy_readb(info, CyMSVR1),
+                       cyy_readb(info, CyMSVR2));
 #endif
        }
        if (clear & TIOCM_DTR) {
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               if (info->rtsdtr_inv) {
-                       cy_writeb(base_addr + (CyMSVR1 << index), ~CyRTS);
-               } else {
-                       cy_writeb(base_addr + (CyMSVR2 << index), ~CyDTR);
-               }
-
+               cyy_writeb(info, CyCAR, channel);
+               cyy_writeb(info, msvrd, ~dtr);
 #ifdef CY_DEBUG_DTR
                printk(KERN_DEBUG "cyc:set_modem_info dropping DTR\n");
                printk(KERN_DEBUG "     status: 0x%x, 0x%x\n",
-                       readb(base_addr + (CyMSVR1 << index)),
-                       readb(base_addr + (CyMSVR2 << index)));
+                       cyy_readb(info, CyMSVR1),
+                       cyy_readb(info, CyMSVR2));
 #endif
        }
 }
@@ -1195,7 +1197,7 @@ static void cyz_handle_cmd(struct cyclades_card *cinfo)
                        break;
                }
                if (delta_count)
-                       wake_up_interruptible(&info->delta_msr_wait);
+                       wake_up_interruptible(&info->port.delta_msr_wait);
                if (special_count)
                        tty_schedule_flip(tty);
                tty_kref_put(tty);
@@ -1295,7 +1297,6 @@ static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
        struct cyclades_card *card;
        unsigned long flags;
        int retval = 0;
-       void __iomem *base_addr;
        int channel;
        unsigned long page;
 
@@ -1326,31 +1327,21 @@ static int cy_startup(struct cyclades_port *info, struct tty_struct *tty)
        cy_set_line_char(info, tty);
 
        if (!cy_is_Z(card)) {
-               int chip = channel >> 2;
-               int index = card->bus_index;
                channel &= 0x03;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
 
-#ifdef CY_DEBUG_OPEN
-               printk(KERN_DEBUG "cyc startup card %d, chip %d, channel %d, "
-                               "base_addr %p\n",
-                               card, chip, channel, base_addr);
-#endif
                spin_lock_irqsave(&card->card_lock, flags);
 
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
+               cyy_writeb(info, CyCAR, channel);
 
-               cy_writeb(base_addr + (CyRTPR << index),
+               cyy_writeb(info, CyRTPR,
                        (info->default_timeout ? info->default_timeout : 0x02));
                /* 10ms rx timeout */
 
-               cyy_issue_cmd(base_addr, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR,
-                               index);
+               cyy_issue_cmd(info, CyCHAN_CTL | CyENB_RCVR | CyENB_XMTR);
 
                cyy_change_rts_dtr(info, TIOCM_RTS | TIOCM_DTR, 0);
 
-               cy_writeb(base_addr + (CySRER << index),
-                       readb(base_addr + (CySRER << index)) | CyRxData);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyRxData);
        } else {
                struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
 
@@ -1428,23 +1419,14 @@ errout:
 
 static void start_xmit(struct cyclades_port *info)
 {
-       struct cyclades_card *card;
+       struct cyclades_card *card = info->card;
        unsigned long flags;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel = info->line - card->first_line;
 
-       card = info->card;
-       channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
                spin_lock_irqsave(&card->card_lock, flags);
-               cy_writeb(base_addr + (CyCAR << index), channel);
-               cy_writeb(base_addr + (CySRER << index),
-                       readb(base_addr + (CySRER << index)) | CyTxRdy);
+               cyy_writeb(info, CyCAR, channel & 0x03);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
                spin_unlock_irqrestore(&card->card_lock, flags);
        } else {
 #ifdef CONFIG_CYZ_INTR
@@ -1471,8 +1453,7 @@ static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
 {
        struct cyclades_card *card;
        unsigned long flags;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel;
 
        if (!(info->port.flags & ASYNC_INITIALIZED))
                return;
@@ -1480,21 +1461,10 @@ static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
        card = info->card;
        channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
-#ifdef CY_DEBUG_OPEN
-               printk(KERN_DEBUG "cyc shutdown Y card %d, chip %d, "
-                               "channel %d, base_addr %p\n",
-                               card, chip, channel, base_addr);
-#endif
-
                spin_lock_irqsave(&card->card_lock, flags);
 
                /* Clear delta_msr_wait queue to avoid mem leaks. */
-               wake_up_interruptible(&info->delta_msr_wait);
+               wake_up_interruptible(&info->port.delta_msr_wait);
 
                if (info->port.xmit_buf) {
                        unsigned char *temp;
@@ -1505,7 +1475,7 @@ static void cy_shutdown(struct cyclades_port *info, struct tty_struct *tty)
                if (tty->termios->c_cflag & HUPCL)
                        cyy_change_rts_dtr(info, 0, TIOCM_RTS | TIOCM_DTR);
 
-               cyy_issue_cmd(base_addr, CyCHAN_CTL | CyDIS_RCVR, index);
+               cyy_issue_cmd(info, CyCHAN_CTL | CyDIS_RCVR);
                /* it may be appropriate to clear _XMIT at
                   some later date (after testing)!!! */
 
@@ -1682,8 +1652,6 @@ static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
 {
        struct cyclades_card *card;
        struct cyclades_port *info = tty->driver_data;
-       void __iomem *base_addr;
-       int chip, channel, index;
        unsigned long orig_jiffies;
        int char_time;
 
@@ -1727,13 +1695,8 @@ static void cy_wait_until_sent(struct tty_struct *tty, int timeout)
                timeout, char_time, jiffies);
 #endif
        card = info->card;
-       channel = (info->line) - (card->first_line);
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-               while (readb(base_addr + (CySRER << index)) & CyTxRdy) {
+               while (cyy_readb(info, CySRER) & CyTxRdy) {
 #ifdef CY_DEBUG_WAIT_UNTIL_SENT
                        printk(KERN_DEBUG "Not clean (jiff=%lu)...", jiffies);
 #endif
@@ -1787,40 +1750,27 @@ static void cy_flush_buffer(struct tty_struct *tty)
 }                              /* cy_flush_buffer */
 
 
-/*
- * This routine is called when a particular tty device is closed.
- */
-static void cy_close(struct tty_struct *tty, struct file *filp)
+static void cy_do_close(struct tty_port *port)
 {
-       struct cyclades_port *info = tty->driver_data;
+       struct cyclades_port *info = container_of(port, struct cyclades_port,
+                                                               port);
        struct cyclades_card *card;
        unsigned long flags;
-
-       if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
-               return;
+       int channel;
 
        card = info->card;
-
-       if (!tty_port_close_start(&info->port, tty, filp))
-               return;
-
+       channel = info->line - card->first_line;
        spin_lock_irqsave(&card->card_lock, flags);
 
        if (!cy_is_Z(card)) {
-               int channel = info->line - card->first_line;
-               int index = card->bus_index;
-               void __iomem *base_addr = card->base_addr +
-                       (cy_chip_offset[channel >> 2] << index);
                /* Stop accepting input */
-               channel &= 0x03;
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               cy_writeb(base_addr + (CySRER << index),
-                         readb(base_addr + (CySRER << index)) & ~CyRxData);
+               cyy_writeb(info, CyCAR, channel & 0x03);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyRxData);
                if (info->port.flags & ASYNC_INITIALIZED) {
                        /* Waiting for on-board buffers to be empty before
                           closing the port */
                        spin_unlock_irqrestore(&card->card_lock, flags);
-                       cy_wait_until_sent(tty, info->timeout);
+                       cy_wait_until_sent(port->tty, info->timeout);
                        spin_lock_irqsave(&card->card_lock, flags);
                }
        } else {
@@ -1828,7 +1778,6 @@ static void cy_close(struct tty_struct *tty, struct file *filp)
                /* Waiting for on-board buffers to be empty before closing
                   the port */
                struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
-               int channel = info->line - card->first_line;
                int retval;
 
                if (readl(&ch_ctrl->flow_status) != C_FS_TXIDLE) {
@@ -1843,14 +1792,19 @@ static void cy_close(struct tty_struct *tty, struct file *filp)
                }
 #endif
        }
-
        spin_unlock_irqrestore(&card->card_lock, flags);
-       cy_shutdown(info, tty);
-       cy_flush_buffer(tty);
-
-       tty_port_tty_set(&info->port, NULL);
+       cy_shutdown(info, port->tty);
+}
 
-       tty_port_close_end(&info->port, tty);
+/*
+ * This routine is called when a particular tty device is closed.
+ */
+static void cy_close(struct tty_struct *tty, struct file *filp)
+{
+       struct cyclades_port *info = tty->driver_data;
+       if (!info || serial_paranoia_check(info, tty->name, "cy_close"))
+               return;
+       tty_port_close(&info->port, tty, filp);
 }                              /* cy_close */
 
 /* This routine gets called when tty_write has put something into
@@ -2069,8 +2023,7 @@ static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
 {
        struct cyclades_card *card;
        unsigned long flags;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel;
        unsigned cflag, iflag;
        int baud, baud_rate = 0;
        int i;
@@ -2100,8 +2053,7 @@ static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
        channel = info->line - card->first_line;
 
        if (!cy_is_Z(card)) {
-
-               index = card->bus_index;
+               u32 cflags;
 
                /* baud rate */
                baud = tty_get_baud_rate(tty);
@@ -2213,71 +2165,49 @@ static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
            cable.  Contact Marcio Saito for details.
         ***********************************************/
 
-               chip = channel >> 2;
                channel &= 0x03;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
 
                spin_lock_irqsave(&card->card_lock, flags);
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
+               cyy_writeb(info, CyCAR, channel);
 
                /* tx and rx baud rate */
 
-               cy_writeb(base_addr + (CyTCOR << index), info->tco);
-               cy_writeb(base_addr + (CyTBPR << index), info->tbpr);
-               cy_writeb(base_addr + (CyRCOR << index), info->rco);
-               cy_writeb(base_addr + (CyRBPR << index), info->rbpr);
+               cyy_writeb(info, CyTCOR, info->tco);
+               cyy_writeb(info, CyTBPR, info->tbpr);
+               cyy_writeb(info, CyRCOR, info->rco);
+               cyy_writeb(info, CyRBPR, info->rbpr);
 
                /* set line characteristics  according configuration */
 
-               cy_writeb(base_addr + (CySCHR1 << index), START_CHAR(tty));
-               cy_writeb(base_addr + (CySCHR2 << index), STOP_CHAR(tty));
-               cy_writeb(base_addr + (CyCOR1 << index), info->cor1);
-               cy_writeb(base_addr + (CyCOR2 << index), info->cor2);
-               cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
-               cy_writeb(base_addr + (CyCOR4 << index), info->cor4);
-               cy_writeb(base_addr + (CyCOR5 << index), info->cor5);
+               cyy_writeb(info, CySCHR1, START_CHAR(tty));
+               cyy_writeb(info, CySCHR2, STOP_CHAR(tty));
+               cyy_writeb(info, CyCOR1, info->cor1);
+               cyy_writeb(info, CyCOR2, info->cor2);
+               cyy_writeb(info, CyCOR3, info->cor3);
+               cyy_writeb(info, CyCOR4, info->cor4);
+               cyy_writeb(info, CyCOR5, info->cor5);
 
-               cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
-                               CyCOR3ch, index);
+               cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR1ch | CyCOR2ch |
+                               CyCOR3ch);
 
                /* !!! Is this needed? */
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               cy_writeb(base_addr + (CyRTPR << index),
+               cyy_writeb(info, CyCAR, channel);
+               cyy_writeb(info, CyRTPR,
                        (info->default_timeout ? info->default_timeout : 0x02));
                /* 10ms rx timeout */
 
-               if (C_CLOCAL(tty)) {
-                       /* without modem intr */
-                       cy_writeb(base_addr + (CySRER << index),
-                               readb(base_addr + (CySRER << index)) | CyMdmCh);
-                       /* act on 1->0 modem transitions */
-                       if ((cflag & CRTSCTS) && info->rflow) {
-                               cy_writeb(base_addr + (CyMCOR1 << index),
-                                         (CyCTS | rflow_thr[i]));
-                       } else {
-                               cy_writeb(base_addr + (CyMCOR1 << index),
-                                         CyCTS);
-                       }
-                       /* act on 0->1 modem transitions */
-                       cy_writeb(base_addr + (CyMCOR2 << index), CyCTS);
-               } else {
-                       /* without modem intr */
-                       cy_writeb(base_addr + (CySRER << index),
-                                 readb(base_addr +
-                                          (CySRER << index)) | CyMdmCh);
-                       /* act on 1->0 modem transitions */
-                       if ((cflag & CRTSCTS) && info->rflow) {
-                               cy_writeb(base_addr + (CyMCOR1 << index),
-                                         (CyDSR | CyCTS | CyRI | CyDCD |
-                                          rflow_thr[i]));
-                       } else {
-                               cy_writeb(base_addr + (CyMCOR1 << index),
-                                         CyDSR | CyCTS | CyRI | CyDCD);
-                       }
-                       /* act on 0->1 modem transitions */
-                       cy_writeb(base_addr + (CyMCOR2 << index),
-                                 CyDSR | CyCTS | CyRI | CyDCD);
-               }
+               cflags = CyCTS;
+               if (!C_CLOCAL(tty))
+                       cflags |= CyDSR | CyRI | CyDCD;
+               /* without modem intr */
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyMdmCh);
+               /* act on 1->0 modem transitions */
+               if ((cflag & CRTSCTS) && info->rflow)
+                       cyy_writeb(info, CyMCOR1, cflags | rflow_thr[i]);
+               else
+                       cyy_writeb(info, CyMCOR1, cflags);
+               /* act on 0->1 modem transitions */
+               cyy_writeb(info, CyMCOR2, cflags);
 
                if (i == 0)     /* baud rate is zero, turn off line */
                        cyy_change_rts_dtr(info, 0, TIOCM_DTR);
@@ -2411,29 +2341,25 @@ static void cy_set_line_char(struct cyclades_port *info, struct tty_struct *tty)
        }
 }                              /* set_line_char */
 
-static int
-get_serial_info(struct cyclades_port *info,
+static int cy_get_serial_info(struct cyclades_port *info,
                struct serial_struct __user *retinfo)
 {
-       struct serial_struct tmp;
        struct cyclades_card *cinfo = info->card;
-
-       if (!retinfo)
-               return -EFAULT;
-       memset(&tmp, 0, sizeof(tmp));
-       tmp.type = info->type;
-       tmp.line = info->line;
-       tmp.port = (info->card - cy_card) * 0x100 + info->line -
-               cinfo->first_line;
-       tmp.irq = cinfo->irq;
-       tmp.flags = info->port.flags;
-       tmp.close_delay = info->port.close_delay;
-       tmp.closing_wait = info->port.closing_wait;
-       tmp.baud_base = info->baud;
-       tmp.custom_divisor = info->custom_divisor;
-       tmp.hub6 = 0;           /*!!! */
+       struct serial_struct tmp = {
+               .type = info->type,
+               .line = info->line,
+               .port = (info->card - cy_card) * 0x100 + info->line -
+                       cinfo->first_line,
+               .irq = cinfo->irq,
+               .flags = info->port.flags,
+               .close_delay = info->port.close_delay,
+               .closing_wait = info->port.closing_wait,
+               .baud_base = info->baud,
+               .custom_divisor = info->custom_divisor,
+               .hub6 = 0,              /*!!! */
+       };
        return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
-}                              /* get_serial_info */
+}
 
 static int
 cy_set_serial_info(struct cyclades_port *info, struct tty_struct *tty,
@@ -2491,24 +2417,14 @@ check_and_exit:
  */
 static int get_lsr_info(struct cyclades_port *info, unsigned int __user *value)
 {
-       struct cyclades_card *card;
-       int chip, channel, index;
-       unsigned char status;
+       struct cyclades_card *card = info->card;
        unsigned int result;
        unsigned long flags;
-       void __iomem *base_addr;
+       u8 status;
 
-       card = info->card;
-       channel = (info->line) - (card->first_line);
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
                spin_lock_irqsave(&card->card_lock, flags);
-               status = readb(base_addr + (CySRER << index)) &
-                               (CyTxRdy | CyTxMpty);
+               status = cyy_readb(info, CySRER) & (CyTxRdy | CyTxMpty);
                spin_unlock_irqrestore(&card->card_lock, flags);
                result = (status ? 0 : TIOCSER_TEMT);
        } else {
@@ -2522,30 +2438,23 @@ static int cy_tiocmget(struct tty_struct *tty, struct file *file)
 {
        struct cyclades_port *info = tty->driver_data;
        struct cyclades_card *card;
-       int chip, channel, index;
-       void __iomem *base_addr;
-       unsigned long flags;
-       unsigned char status;
-       unsigned long lstatus;
-       unsigned int result;
+       int result;
 
        if (serial_paranoia_check(info, tty->name, __func__))
                return -ENODEV;
 
-       lock_kernel();
-
        card = info->card;
-       channel = info->line - card->first_line;
+
+       lock_kernel();
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
+               unsigned long flags;
+               int channel = info->line - card->first_line;
+               u8 status;
 
                spin_lock_irqsave(&card->card_lock, flags);
-               cy_writeb(base_addr + (CyCAR << index), (u_char) channel);
-               status = readb(base_addr + (CyMSVR1 << index));
-               status |= readb(base_addr + (CyMSVR2 << index));
+               cyy_writeb(info, CyCAR, channel & 0x03);
+               status = cyy_readb(info, CyMSVR1);
+               status |= cyy_readb(info, CyMSVR2);
                spin_unlock_irqrestore(&card->card_lock, flags);
 
                if (info->rtsdtr_inv) {
@@ -2560,21 +2469,22 @@ static int cy_tiocmget(struct tty_struct *tty, struct file *file)
                        ((status & CyDSR) ? TIOCM_DSR : 0) |
                        ((status & CyCTS) ? TIOCM_CTS : 0);
        } else {
-               if (cyz_is_loaded(card)) {
-                       lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
-                       result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
-                               ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
-                               ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
-                               ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
-                               ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
-                               ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
-               } else {
-                       result = 0;
-                       unlock_kernel();
-                       return -ENODEV;
+               u32 lstatus;
+
+               if (!cyz_is_loaded(card)) {
+                       result = -ENODEV;
+                       goto end;
                }
 
+               lstatus = readl(&info->u.cyz.ch_ctrl->rs_status);
+               result = ((lstatus & C_RS_RTS) ? TIOCM_RTS : 0) |
+                       ((lstatus & C_RS_DTR) ? TIOCM_DTR : 0) |
+                       ((lstatus & C_RS_DCD) ? TIOCM_CAR : 0) |
+                       ((lstatus & C_RS_RI) ? TIOCM_RNG : 0) |
+                       ((lstatus & C_RS_DSR) ? TIOCM_DSR : 0) |
+                       ((lstatus & C_RS_CTS) ? TIOCM_CTS : 0);
        }
+end:
        unlock_kernel();
        return result;
 }                              /* cy_tiomget */
@@ -2586,68 +2496,52 @@ cy_tiocmset(struct tty_struct *tty, struct file *file,
        struct cyclades_port *info = tty->driver_data;
        struct cyclades_card *card;
        unsigned long flags;
-       int channel, retval;
 
        if (serial_paranoia_check(info, tty->name, __func__))
                return -ENODEV;
 
        card = info->card;
-       channel = (info->line) - (card->first_line);
        if (!cy_is_Z(card)) {
                spin_lock_irqsave(&card->card_lock, flags);
                cyy_change_rts_dtr(info, set, clear);
                spin_unlock_irqrestore(&card->card_lock, flags);
        } else {
-               if (cyz_is_loaded(card)) {
-                       struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
-
-                       if (set & TIOCM_RTS) {
-                               spin_lock_irqsave(&card->card_lock, flags);
-                               cy_writel(&ch_ctrl->rs_control,
-                                       readl(&ch_ctrl->rs_control) | C_RS_RTS);
-                               spin_unlock_irqrestore(&card->card_lock, flags);
-                       }
-                       if (clear & TIOCM_RTS) {
-                               spin_lock_irqsave(&card->card_lock, flags);
-                               cy_writel(&ch_ctrl->rs_control,
-                                       readl(&ch_ctrl->rs_control) &
-                                       ~C_RS_RTS);
-                               spin_unlock_irqrestore(&card->card_lock, flags);
-                       }
-                       if (set & TIOCM_DTR) {
-                               spin_lock_irqsave(&card->card_lock, flags);
-                               cy_writel(&ch_ctrl->rs_control,
-                                       readl(&ch_ctrl->rs_control) | C_RS_DTR);
+               struct CH_CTRL __iomem *ch_ctrl = info->u.cyz.ch_ctrl;
+               int retval, channel = info->line - card->first_line;
+               u32 rs;
+
+               if (!cyz_is_loaded(card))
+                       return -ENODEV;
+
+               spin_lock_irqsave(&card->card_lock, flags);
+               rs = readl(&ch_ctrl->rs_control);
+               if (set & TIOCM_RTS)
+                       rs |= C_RS_RTS;
+               if (clear & TIOCM_RTS)
+                       rs &= ~C_RS_RTS;
+               if (set & TIOCM_DTR) {
+                       rs |= C_RS_DTR;
 #ifdef CY_DEBUG_DTR
-                               printk(KERN_DEBUG "cyc:set_modem_info raising "
-                                       "Z DTR\n");
+                       printk(KERN_DEBUG "cyc:set_modem_info raising Z DTR\n");
 #endif
-                               spin_unlock_irqrestore(&card->card_lock, flags);
-                       }
-                       if (clear & TIOCM_DTR) {
-                               spin_lock_irqsave(&card->card_lock, flags);
-                               cy_writel(&ch_ctrl->rs_control,
-                                       readl(&ch_ctrl->rs_control) &
-                                       ~C_RS_DTR);
+               }
+               if (clear & TIOCM_DTR) {
+                       rs &= ~C_RS_DTR;
 #ifdef CY_DEBUG_DTR
-                               printk(KERN_DEBUG "cyc:set_modem_info clearing "
-                                       "Z DTR\n");
+                       printk(KERN_DEBUG "cyc:set_modem_info clearing "
+                               "Z DTR\n");
 #endif
-                               spin_unlock_irqrestore(&card->card_lock, flags);
-                       }
-               } else {
-                       return -ENODEV;
                }
-               spin_lock_irqsave(&card->card_lock, flags);
+               cy_writel(&ch_ctrl->rs_control, rs);
                retval = cyz_issue_cmd(card, channel, C_CM_IOCTLM, 0L);
+               spin_unlock_irqrestore(&card->card_lock, flags);
                if (retval != 0) {
                        printk(KERN_ERR "cyc:set_modem_info retval on ttyC%d "
                                "was %x\n", info->line, retval);
                }
-               spin_unlock_irqrestore(&card->card_lock, flags);
        }
        return 0;
-}                              /* cy_tiocmset */
+}
 
 /*
  * cy_break() --- routine which turns the break handling on or off
@@ -2712,40 +2606,18 @@ static int cy_break(struct tty_struct *tty, int break_state)
        return retval;
 }                              /* cy_break */
 
-static int get_mon_info(struct cyclades_port *info,
-                               struct cyclades_monitor __user *mon)
-{
-       if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
-               return -EFAULT;
-       info->mon.int_count = 0;
-       info->mon.char_count = 0;
-       info->mon.char_max = 0;
-       info->mon.char_last = 0;
-       return 0;
-}                              /* get_mon_info */
-
 static int set_threshold(struct cyclades_port *info, unsigned long value)
 {
-       struct cyclades_card *card;
-       void __iomem *base_addr;
-       int channel, chip, index;
+       struct cyclades_card *card = info->card;
        unsigned long flags;
 
-       card = info->card;
-       channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr =
-                   card->base_addr + (cy_chip_offset[chip] << index);
-
                info->cor3 &= ~CyREC_FIFO;
                info->cor3 |= value & CyREC_FIFO;
 
                spin_lock_irqsave(&card->card_lock, flags);
-               cy_writeb(base_addr + (CyCOR3 << index), info->cor3);
-               cyy_issue_cmd(base_addr, CyCOR_CHANGE | CyCOR3ch, index);
+               cyy_writeb(info, CyCOR3, info->cor3);
+               cyy_issue_cmd(info, CyCOR_CHANGE | CyCOR3ch);
                spin_unlock_irqrestore(&card->card_lock, flags);
        }
        return 0;
@@ -2754,55 +2626,23 @@ static int set_threshold(struct cyclades_port *info, unsigned long value)
 static int get_threshold(struct cyclades_port *info,
                                                unsigned long __user *value)
 {
-       struct cyclades_card *card;
-       void __iomem *base_addr;
-       int channel, chip, index;
-       unsigned long tmp;
+       struct cyclades_card *card = info->card;
 
-       card = info->card;
-       channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
-               tmp = readb(base_addr + (CyCOR3 << index)) & CyREC_FIFO;
+               u8 tmp = cyy_readb(info, CyCOR3) & CyREC_FIFO;
                return put_user(tmp, value);
        }
        return 0;
 }                              /* get_threshold */
 
-static int set_default_threshold(struct cyclades_port *info,
-                                                       unsigned long value)
-{
-       info->default_threshold = value & 0x0f;
-       return 0;
-}                              /* set_default_threshold */
-
-static int get_default_threshold(struct cyclades_port *info,
-                                               unsigned long __user *value)
-{
-       return put_user(info->default_threshold, value);
-}                              /* get_default_threshold */
-
 static int set_timeout(struct cyclades_port *info, unsigned long value)
 {
-       struct cyclades_card *card;
-       void __iomem *base_addr;
-       int channel, chip, index;
+       struct cyclades_card *card = info->card;
        unsigned long flags;
 
-       card = info->card;
-       channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
                spin_lock_irqsave(&card->card_lock, flags);
-               cy_writeb(base_addr + (CyRTPR << index), value & 0xff);
+               cyy_writeb(info, CyRTPR, value & 0xff);
                spin_unlock_irqrestore(&card->card_lock, flags);
        }
        return 0;
@@ -2811,36 +2651,35 @@ static int set_timeout(struct cyclades_port *info, unsigned long value)
 static int get_timeout(struct cyclades_port *info,
                                                unsigned long __user *value)
 {
-       struct cyclades_card *card;
-       void __iomem *base_addr;
-       int channel, chip, index;
-       unsigned long tmp;
+       struct cyclades_card *card = info->card;
 
-       card = info->card;
-       channel = info->line - card->first_line;
        if (!cy_is_Z(card)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               index = card->bus_index;
-               base_addr = card->base_addr + (cy_chip_offset[chip] << index);
-
-               tmp = readb(base_addr + (CyRTPR << index));
+               u8 tmp = cyy_readb(info, CyRTPR);
                return put_user(tmp, value);
        }
        return 0;
 }                              /* get_timeout */
 
-static int set_default_timeout(struct cyclades_port *info, unsigned long value)
+static int cy_cflags_changed(struct cyclades_port *info, unsigned long arg,
+               struct cyclades_icount *cprev)
 {
-       info->default_timeout = value & 0xff;
-       return 0;
-}                              /* set_default_timeout */
+       struct cyclades_icount cnow;
+       unsigned long flags;
+       int ret;
 
-static int get_default_timeout(struct cyclades_port *info,
-                                       unsigned long __user *value)
-{
-       return put_user(info->default_timeout, value);
-}                              /* get_default_timeout */
+       spin_lock_irqsave(&info->card->card_lock, flags);
+       cnow = info->icount;    /* atomic copy */
+       spin_unlock_irqrestore(&info->card->card_lock, flags);
+
+       ret =   ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
+               ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
+               ((arg & TIOCM_CD)  && (cnow.dcd != cprev->dcd)) ||
+               ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
+
+       *cprev = cnow;
+
+       return ret;
+}
 
 /*
  * This routine allows the tty driver to implement device-
@@ -2852,8 +2691,7 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
         unsigned int cmd, unsigned long arg)
 {
        struct cyclades_port *info = tty->driver_data;
-       struct cyclades_icount cprev, cnow;     /* kernel counter temps */
-       struct serial_icounter_struct __user *p_cuser;  /* user space */
+       struct cyclades_icount cnow;    /* kernel counter temps */
        int ret_val = 0;
        unsigned long flags;
        void __user *argp = (void __user *)arg;
@@ -2869,7 +2707,11 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
 
        switch (cmd) {
        case CYGETMON:
-               ret_val = get_mon_info(info, argp);
+               if (copy_to_user(argp, &info->mon, sizeof(info->mon))) {
+                       ret_val = -EFAULT;
+                       break;
+               }
+               memset(&info->mon, 0, sizeof(info->mon));
                break;
        case CYGETTHRESH:
                ret_val = get_threshold(info, argp);
@@ -2878,10 +2720,11 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
                ret_val = set_threshold(info, arg);
                break;
        case CYGETDEFTHRESH:
-               ret_val = get_default_threshold(info, argp);
+               ret_val = put_user(info->default_threshold,
+                               (unsigned long __user *)argp);
                break;
        case CYSETDEFTHRESH:
-               ret_val = set_default_threshold(info, arg);
+               info->default_threshold = arg & 0x0f;
                break;
        case CYGETTIMEOUT:
                ret_val = get_timeout(info, argp);
@@ -2890,21 +2733,20 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
                ret_val = set_timeout(info, arg);
                break;
        case CYGETDEFTIMEOUT:
-               ret_val = get_default_timeout(info, argp);
+               ret_val = put_user(info->default_timeout,
+                               (unsigned long __user *)argp);
                break;
        case CYSETDEFTIMEOUT:
-               ret_val = set_default_timeout(info, arg);
+               info->default_timeout = arg & 0xff;
                break;
        case CYSETRFLOW:
                info->rflow = (int)arg;
-               ret_val = 0;
                break;
        case CYGETRFLOW:
                ret_val = info->rflow;
                break;
        case CYSETRTSDTR_INV:
                info->rtsdtr_inv = (int)arg;
-               ret_val = 0;
                break;
        case CYGETRTSDTR_INV:
                ret_val = info->rtsdtr_inv;
@@ -2915,7 +2757,6 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
 #ifndef CONFIG_CYZ_INTR
        case CYZSETPOLLCYCLE:
                cyz_polling_cycle = (arg * HZ) / 1000;
-               ret_val = 0;
                break;
        case CYZGETPOLLCYCLE:
                ret_val = (cyz_polling_cycle * 1000) / HZ;
@@ -2923,13 +2764,12 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
 #endif                         /* CONFIG_CYZ_INTR */
        case CYSETWAIT:
                info->port.closing_wait = (unsigned short)arg * HZ / 100;
-               ret_val = 0;
                break;
        case CYGETWAIT:
                ret_val = info->port.closing_wait / (HZ / 100);
                break;
        case TIOCGSERIAL:
-               ret_val = get_serial_info(info, argp);
+               ret_val = cy_get_serial_info(info, argp);
                break;
        case TIOCSSERIAL:
                ret_val = cy_set_serial_info(info, tty, argp);
@@ -2948,17 +2788,8 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
                /* note the counters on entry */
                cnow = info->icount;
                spin_unlock_irqrestore(&info->card->card_lock, flags);
-               ret_val = wait_event_interruptible(info->delta_msr_wait, ({
-                       cprev = cnow;
-                       spin_lock_irqsave(&info->card->card_lock, flags);
-                       cnow = info->icount;    /* atomic copy */
-                       spin_unlock_irqrestore(&info->card->card_lock, flags);
-
-                       ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
-                       ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
-                       ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
-                       ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
-               }));
+               ret_val = wait_event_interruptible(info->port.delta_msr_wait,
+                               cy_cflags_changed(info, arg, &cnow));
                break;
 
                /*
@@ -2967,46 +2798,29 @@ cy_ioctl(struct tty_struct *tty, struct file *file,
                 * NB: both 1->0 and 0->1 transitions are counted except for
                 *     RI where only 0->1 is counted.
                 */
-       case TIOCGICOUNT:
+       case TIOCGICOUNT: {
+               struct serial_icounter_struct sic = { };
+
                spin_lock_irqsave(&info->card->card_lock, flags);
                cnow = info->icount;
                spin_unlock_irqrestore(&info->card->card_lock, flags);
-               p_cuser = argp;
-               ret_val = put_user(cnow.cts, &p_cuser->cts);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.dsr, &p_cuser->dsr);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.rng, &p_cuser->rng);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.dcd, &p_cuser->dcd);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.rx, &p_cuser->rx);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.tx, &p_cuser->tx);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.frame, &p_cuser->frame);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.overrun, &p_cuser->overrun);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.parity, &p_cuser->parity);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.brk, &p_cuser->brk);
-               if (ret_val)
-                       break;
-               ret_val = put_user(cnow.buf_overrun, &p_cuser->buf_overrun);
-               if (ret_val)
-                       break;
-               ret_val = 0;
+
+               sic.cts = cnow.cts;
+               sic.dsr = cnow.dsr;
+               sic.rng = cnow.rng;
+               sic.dcd = cnow.dcd;
+               sic.rx = cnow.rx;
+               sic.tx = cnow.tx;
+               sic.frame = cnow.frame;
+               sic.overrun = cnow.overrun;
+               sic.parity = cnow.parity;
+               sic.brk = cnow.brk;
+               sic.buf_overrun = cnow.buf_overrun;
+
+               if (copy_to_user(argp, &sic, sizeof(sic)))
+                       ret_val = -EFAULT;
                break;
+       }
        default:
                ret_val = -ENOIOCTLCMD;
        }
@@ -3167,8 +2981,7 @@ static void cy_stop(struct tty_struct *tty)
 {
        struct cyclades_card *cinfo;
        struct cyclades_port *info = tty->driver_data;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel;
        unsigned long flags;
 
 #ifdef CY_DEBUG_OTHER
@@ -3181,16 +2994,9 @@ static void cy_stop(struct tty_struct *tty)
        cinfo = info->card;
        channel = info->line - cinfo->first_line;
        if (!cy_is_Z(cinfo)) {
-               index = cinfo->bus_index;
-               chip = channel >> 2;
-               channel &= 0x03;
-               base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
-
                spin_lock_irqsave(&cinfo->card_lock, flags);
-               cy_writeb(base_addr + (CyCAR << index),
-                       (u_char)(channel & 0x0003)); /* index channel */
-               cy_writeb(base_addr + (CySRER << index),
-                         readb(base_addr + (CySRER << index)) & ~CyTxRdy);
+               cyy_writeb(info, CyCAR, channel & 0x03);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) & ~CyTxRdy);
                spin_unlock_irqrestore(&cinfo->card_lock, flags);
        }
 }                              /* cy_stop */
@@ -3199,8 +3005,7 @@ static void cy_start(struct tty_struct *tty)
 {
        struct cyclades_card *cinfo;
        struct cyclades_port *info = tty->driver_data;
-       void __iomem *base_addr;
-       int chip, channel, index;
+       int channel;
        unsigned long flags;
 
 #ifdef CY_DEBUG_OTHER
@@ -3212,17 +3017,10 @@ static void cy_start(struct tty_struct *tty)
 
        cinfo = info->card;
        channel = info->line - cinfo->first_line;
-       index = cinfo->bus_index;
        if (!cy_is_Z(cinfo)) {
-               chip = channel >> 2;
-               channel &= 0x03;
-               base_addr = cinfo->base_addr + (cy_chip_offset[chip] << index);
-
                spin_lock_irqsave(&cinfo->card_lock, flags);
-               cy_writeb(base_addr + (CyCAR << index),
-                       (u_char) (channel & 0x0003));   /* index channel */
-               cy_writeb(base_addr + (CySRER << index),
-                         readb(base_addr + (CySRER << index)) | CyTxRdy);
+               cyy_writeb(info, CyCAR, channel & 0x03);
+               cyy_writeb(info, CySRER, cyy_readb(info, CySRER) | CyTxRdy);
                spin_unlock_irqrestore(&cinfo->card_lock, flags);
        }
 }                              /* cy_start */
@@ -3251,18 +3049,13 @@ static int cyy_carrier_raised(struct tty_port *port)
        struct cyclades_port *info = container_of(port, struct cyclades_port,
                        port);
        struct cyclades_card *cinfo = info->card;
-       void __iomem *base = cinfo->base_addr;
        unsigned long flags;
        int channel = info->line - cinfo->first_line;
-       int chip = channel >> 2, index = cinfo->bus_index;
        u32 cd;
 
-       channel &= 0x03;
-       base += cy_chip_offset[chip] << index;
-
        spin_lock_irqsave(&cinfo->card_lock, flags);
-       cy_writeb(base + (CyCAR << index), (u8)channel);
-       cd = readb(base + (CyMSVR1 << index)) & CyDCD;
+       cyy_writeb(info, CyCAR, channel & 0x03);
+       cd = cyy_readb(info, CyMSVR1) & CyDCD;
        spin_unlock_irqrestore(&cinfo->card_lock, flags);
 
        return cd;
@@ -3316,11 +3109,13 @@ static void cyz_dtr_rts(struct tty_port *port, int raise)
 static const struct tty_port_operations cyy_port_ops = {
        .carrier_raised = cyy_carrier_raised,
        .dtr_rts = cyy_dtr_rts,
+       .shutdown = cy_do_close,
 };
 
 static const struct tty_port_operations cyz_port_ops = {
        .carrier_raised = cyz_carrier_raised,
        .dtr_rts = cyz_dtr_rts,
+       .shutdown = cy_do_close,
 };
 
 /*
@@ -3358,7 +3153,6 @@ static int __devinit cy_init_card(struct cyclades_card *cinfo)
                info->port.close_delay = 5 * HZ / 10;
                info->port.flags = STD_COM_FLAGS;
                init_completion(&info->shutdown_wait);
-               init_waitqueue_head(&info->delta_msr_wait);
 
                if (cy_is_Z(cinfo)) {
                        struct FIRM_ID *firm_id = cinfo->base_addr + ID_ADDRESS;
@@ -3392,9 +3186,9 @@ static int __devinit cy_init_card(struct cyclades_card *cinfo)
                        info->cor3 = 0x08;      /* _very_ small rcv threshold */
 
                        chip_number = channel / CyPORTS_PER_CHIP;
-                       info->chip_rev = readb(cinfo->base_addr +
-                                     (cy_chip_offset[chip_number] << index) +
-                                     (CyGFRCR << index));
+                       info->u.cyy.base_addr = cinfo->base_addr +
+                               (cy_chip_offset[chip_number] << index);
+                       info->chip_rev = cyy_readb(info, CyGFRCR);
 
                        if (info->chip_rev >= CD1400_REV_J) {
                                /* It is a CD1400 rev. J or later */
@@ -3560,7 +3354,7 @@ static int __init cy_detect_isa(void)
                        continue;
                }
 #ifdef MODULE
-               if (isparam && irq[i])
+               if (isparam && i < NR_CARDS && irq[i])
                        cy_isa_irq = irq[i];
                else
 #endif