Merge branch 'topic/core-cleanup' into for-linus
[safe/jmp/linux-2.6] / drivers / char / pcmcia / cm4000_cs.c
index 31c8a21..90b199f 100644 (file)
   * All rights reserved. Licensed under dual BSD/GPL license.
   */
 
-/* #define PCMCIA_DEBUG 6 */
-
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/init.h>
 #include <linux/fs.h>
 #include <linux/delay.h>
-#include <asm/uaccess.h>
-#include <asm/io.h>
+#include <linux/bitrev.h>
+#include <linux/smp_lock.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
 
 #include <pcmcia/cs_types.h>
 #include <pcmcia/cs.h>
 
 /* #define ATR_CSUM */
 
-#ifdef PCMCIA_DEBUG
-#define reader_to_dev(x)       (&handle_to_dev(x->p_dev->handle))
-static int pc_debug = PCMCIA_DEBUG;
-module_param(pc_debug, int, 0600);
-#define DEBUGP(n, rdr, x, args...) do {                                \
-       if (pc_debug >= (n))                                            \
-               dev_printk(KERN_DEBUG, reader_to_dev(rdr), "%s:" x,     \
-                          __FUNCTION__ , ## args);                     \
+#define reader_to_dev(x)       (&x->p_dev->dev)
+
+/* n (debug level) is ignored */
+/* additional debug output may be enabled by re-compiling with
+ * CM4000_DEBUG set */
+/* #define CM4000_DEBUG */
+#define DEBUGP(n, rdr, x, args...) do {                \
+               dev_dbg(reader_to_dev(rdr), "%s:" x,    \
+                          __func__ , ## args);         \
        } while (0)
-#else
-#define DEBUGP(n, rdr, x, args...)
-#endif
+
 static char *version = "cm4000_cs.c v2.4.0gm6 - All bugs added by Harald Welte";
 
 #define        T_1SEC          (HZ)
@@ -172,14 +171,13 @@ static unsigned char fi_di_table[10][14] = {
 /* 9 */ {0x09,0x19,0x29,0x39,0x49,0x59,0x69,0x11,0x11,0x99,0xA9,0xB9,0xC9,0xD9}
 };
 
-#ifndef PCMCIA_DEBUG
+#ifndef CM4000_DEBUG
 #define        xoutb   outb
 #define        xinb    inb
 #else
 static inline void xoutb(unsigned char val, unsigned short port)
 {
-       if (pc_debug >= 7)
-               printk(KERN_DEBUG "outb(val=%.2x,port=%.4x)\n", val, port);
+       pr_debug("outb(val=%.2x,port=%.4x)\n", val, port);
        outb(val, port);
 }
 static inline unsigned char xinb(unsigned short port)
@@ -187,48 +185,23 @@ static inline unsigned char xinb(unsigned short port)
        unsigned char val;
 
        val = inb(port);
-       if (pc_debug >= 7)
-               printk(KERN_DEBUG "%.2x=inb(%.4x)\n", val, port);
+       pr_debug("%.2x=inb(%.4x)\n", val, port);
 
        return val;
 }
 #endif
 
-#define        b_0000  15
-#define        b_0001  14
-#define        b_0010  13
-#define        b_0011  12
-#define        b_0100  11
-#define        b_0101  10
-#define        b_0110  9
-#define        b_0111  8
-#define        b_1000  7
-#define        b_1001  6
-#define        b_1010  5
-#define        b_1011  4
-#define        b_1100  3
-#define        b_1101  2
-#define        b_1110  1
-#define        b_1111  0
-
-static unsigned char irtab[16] = {
-       b_0000, b_1000, b_0100, b_1100,
-       b_0010, b_1010, b_0110, b_1110,
-       b_0001, b_1001, b_0101, b_1101,
-       b_0011, b_1011, b_0111, b_1111
-};
+static inline unsigned char invert_revert(unsigned char ch)
+{
+       return bitrev8(~ch);
+}
 
 static void str_invert_revert(unsigned char *b, int len)
 {
        int i;
 
        for (i = 0; i < len; i++)
-               b[i] = (irtab[b[i] & 0x0f] << 4) | irtab[b[i] >> 4];
-}
-
-static unsigned char invert_revert(unsigned char ch)
-{
-       return (irtab[ch & 0x0f] << 4) | irtab[ch >> 4];
+               b[i] = invert_revert(b[i]);
 }
 
 #define        ATRLENCK(dev,pos) \
@@ -331,7 +304,8 @@ static unsigned int calc_baudv(unsigned char fidi)
        return (wcrcf / wbrcf);
 }
 
-static unsigned short io_read_num_rec_bytes(ioaddr_t iobase, unsigned short *s)
+static unsigned short io_read_num_rec_bytes(unsigned int iobase,
+                                           unsigned short *s)
 {
        unsigned short tmp;
 
@@ -449,7 +423,7 @@ static struct card_fixup card_fixups[] = {
 static void set_cardparameter(struct cm4000_dev *dev)
 {
        int i;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
        u_int8_t stopbits = 0x02; /* ISO default */
 
        DEBUGP(3, dev, "-> set_cardparameter\n");
@@ -482,7 +456,7 @@ static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
        unsigned short num_bytes_read;
        unsigned char pts_reply[4];
        ssize_t rc;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
 
        rc = 0;
 
@@ -535,12 +509,10 @@ static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
        for (i = 0; i < 4; i++) {
                xoutb(i, REG_BUF_ADDR(iobase));
                xoutb(dev->pts[i], REG_BUF_DATA(iobase));       /* buf data */
-#ifdef PCMCIA_DEBUG
-               if (pc_debug >= 5)
-                       printk("0x%.2x ", dev->pts[i]);
+#ifdef CM4000_DEBUG
+               pr_debug("0x%.2x ", dev->pts[i]);
        }
-       if (pc_debug >= 5)
-               printk("\n");
+       pr_debug("\n");
 #else
        }
 #endif
@@ -600,14 +572,13 @@ static int set_protocol(struct cm4000_dev *dev, struct ptsreq *ptsreq)
                pts_reply[i] = inb(REG_BUF_DATA(iobase));
        }
 
-#ifdef PCMCIA_DEBUG
+#ifdef CM4000_DEBUG
        DEBUGP(2, dev, "PTSreply: ");
        for (i = 0; i < num_bytes_read; i++) {
-               if (pc_debug >= 5)
-                       printk("0x%.2x ", pts_reply[i]);
+               pr_debug("0x%.2x ", pts_reply[i]);
        }
-       printk("\n");
-#endif /* PCMCIA_DEBUG */
+       pr_debug("\n");
+#endif /* CM4000_DEBUG */
 
        DEBUGP(5, dev, "Clear Tactive in Flags1\n");
        xoutb(0x20, REG_FLAGS1(iobase));
@@ -633,7 +604,7 @@ exit_setprotocol:
        return rc;
 }
 
-static int io_detect_cm4000(ioaddr_t iobase, struct cm4000_dev *dev)
+static int io_detect_cm4000(unsigned int iobase, struct cm4000_dev *dev)
 {
 
        /* note: statemachine is assumed to be reset */
@@ -676,7 +647,7 @@ static void terminate_monitor(struct cm4000_dev *dev)
 
        DEBUGP(5, dev, "Delete timer\n");
        del_timer_sync(&dev->timer);
-#ifdef PCMCIA_DEBUG
+#ifdef CM4000_DEBUG
        dev->monitor_running = 0;
 #endif
 
@@ -694,7 +665,7 @@ static void terminate_monitor(struct cm4000_dev *dev)
 static void monitor_card(unsigned long p)
 {
        struct cm4000_dev *dev = (struct cm4000_dev *) p;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
        unsigned short s;
        struct ptsreq ptsreq;
        int i, atrc;
@@ -919,7 +890,7 @@ static void monitor_card(unsigned long p)
                                DEBUGP(4, dev, "ATR checksum (0x%.2x, should "
                                       "be zero) failed\n", dev->atr_csum);
                        }
-#ifdef PCMCIA_DEBUG
+#ifdef CM4000_DEBUG
                        else if (test_bit(IS_BAD_LENGTH, &dev->flags)) {
                                DEBUGP(4, dev, "ATR length error\n");
                        } else {
@@ -946,8 +917,7 @@ release_io:
 
 return_with_timer:
        DEBUGP(7, dev, "<- monitor_card (returns with timer)\n");
-       dev->timer.expires = jiffies + dev->mdelay;
-       add_timer(&dev->timer);
+       mod_timer(&dev->timer, jiffies + dev->mdelay);
        clear_bit(LOCK_MONITOR, &dev->flags);
 }
 
@@ -957,7 +927,7 @@ static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
                        loff_t *ppos)
 {
        struct cm4000_dev *dev = filp->private_data;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
        ssize_t rc;
        int i, j, k;
 
@@ -1039,7 +1009,7 @@ static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
                }
        }
 
-       if (dev->proto == 0 && count > dev->rlen - dev->rpos) {
+       if (dev->proto == 0 && count > dev->rlen - dev->rpos && i) {
                DEBUGP(4, dev, "T=0 and count > buffer\n");
                dev->rbuf[i] = dev->rbuf[i - 1];
                dev->rbuf[i - 1] = dev->procbyte;
@@ -1056,14 +1026,16 @@ static ssize_t cmm_read(struct file *filp, __user char *buf, size_t count,
 
        xoutb(0, REG_FLAGS1(iobase));   /* clear detectCMM */
        /* last check before exit */
-       if (!io_detect_cm4000(iobase, dev))
-               count = -ENODEV;
+       if (!io_detect_cm4000(iobase, dev)) {
+               rc = -ENODEV;
+               goto release_io;
+       }
 
        if (test_bit(IS_INVREV, &dev->flags) && count > 0)
                str_invert_revert(dev->rbuf, count);
 
        if (copy_to_user(buf, dev->rbuf, count))
-               return -EFAULT;
+               rc = -EFAULT;
 
 release_io:
        clear_bit(LOCK_IO, &dev->flags);
@@ -1077,8 +1049,8 @@ release_io:
 static ssize_t cmm_write(struct file *filp, const char __user *buf,
                         size_t count, loff_t *ppos)
 {
-       struct cm4000_dev *dev = (struct cm4000_dev *) filp->private_data;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       struct cm4000_dev *dev = filp->private_data;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
        unsigned short s;
        unsigned char tmp;
        unsigned char infolen;
@@ -1115,7 +1087,7 @@ static ssize_t cmm_write(struct file *filp, const char __user *buf,
        /*
         * wait for atr to become valid.
         * note: it is important to lock this code. if we dont, the monitor
-        * could be run between test_bit and the the call the sleep on the
+        * could be run between test_bit and the call to sleep on the
         * atr-queue.  if *then* the monitor detects atr valid, it will wake up
         * any process on the atr-queue, *but* since we have been interrupted,
         * we do not yet sleep on this queue. this would result in a missed
@@ -1406,12 +1378,9 @@ static void start_monitor(struct cm4000_dev *dev)
        DEBUGP(3, dev, "-> start_monitor\n");
        if (!dev->monitor_running) {
                DEBUGP(5, dev, "create, init and add timer\n");
-               init_timer(&dev->timer);
+               setup_timer(&dev->timer, monitor_card, (unsigned long)dev);
                dev->monitor_running = 1;
-               dev->timer.expires = jiffies;
-               dev->timer.data = (unsigned long) dev;
-               dev->timer.function = monitor_card;
-               add_timer(&dev->timer);
+               mod_timer(&dev->timer, jiffies);
        } else
                DEBUGP(5, dev, "monitor already running\n");
        DEBUGP(3, dev, "<- start_monitor\n");
@@ -1431,16 +1400,16 @@ static void stop_monitor(struct cm4000_dev *dev)
        DEBUGP(3, dev, "<- stop_monitor\n");
 }
 
-static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
-                    unsigned long arg)
+static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        struct cm4000_dev *dev = filp->private_data;
-       ioaddr_t iobase = dev->p_dev->io.BasePort1;
+       unsigned int iobase = dev->p_dev->io.BasePort1;
+       struct inode *inode = filp->f_path.dentry->d_inode;
        struct pcmcia_device *link;
        int size;
        int rc;
        void __user *argp = (void __user *)arg;
-#ifdef PCMCIA_DEBUG
+#ifdef CM4000_DEBUG
        char *ioctl_names[CM_IOC_MAXNR + 1] = {
                [_IOC_NR(CM_IOCGSTATUS)] "CM_IOCGSTATUS",
                [_IOC_NR(CM_IOCGATR)] "CM_IOCGATR",
@@ -1448,42 +1417,46 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                [_IOC_NR(CM_IOCSPTS)] "CM_IOCSPTS",
                [_IOC_NR(CM_IOSDBGLVL)] "CM4000_DBGLVL",
        };
-#endif
        DEBUGP(3, dev, "cmm_ioctl(device=%d.%d) %s\n", imajor(inode),
               iminor(inode), ioctl_names[_IOC_NR(cmd)]);
+#endif
 
+       lock_kernel();
+       rc = -ENODEV;
        link = dev_table[iminor(inode)];
        if (!pcmcia_dev_present(link)) {
                DEBUGP(4, dev, "DEV_OK false\n");
-               return -ENODEV;
+               goto out;
        }
 
        if (test_bit(IS_CMM_ABSENT, &dev->flags)) {
                DEBUGP(4, dev, "CMM_ABSENT flag set\n");
-               return -ENODEV;
+               goto out;
        }
+       rc = -EINVAL;
 
        if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
                DEBUGP(4, dev, "ioctype mismatch\n");
-               return -EINVAL;
+               goto out;
        }
        if (_IOC_NR(cmd) > CM_IOC_MAXNR) {
                DEBUGP(4, dev, "iocnr mismatch\n");
-               return -EINVAL;
+               goto out;
        }
        size = _IOC_SIZE(cmd);
-       rc = 0;
+       rc = -EFAULT;
        DEBUGP(4, dev, "iocdir=%.4x iocr=%.4x iocw=%.4x iocsize=%d cmd=%.4x\n",
              _IOC_DIR(cmd), _IOC_READ, _IOC_WRITE, size, cmd);
 
        if (_IOC_DIR(cmd) & _IOC_READ) {
                if (!access_ok(VERIFY_WRITE, argp, size))
-                       return -EFAULT;
+                       goto out;
        }
        if (_IOC_DIR(cmd) & _IOC_WRITE) {
                if (!access_ok(VERIFY_READ, argp, size))
-                       return -EFAULT;
+                       goto out;
        }
+       rc = 0;
 
        switch (cmd) {
        case CM_IOCGSTATUS:
@@ -1503,9 +1476,9 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                        if (test_bit(IS_BAD_CARD, &dev->flags))
                                status |= CM_BAD_CARD;
                        if (copy_to_user(argp, &status, sizeof(int)))
-                               return -EFAULT;
+                               rc = -EFAULT;
                }
-               return 0;
+               break;
        case CM_IOCGATR:
                DEBUGP(4, dev, "... in CM_IOCGATR\n");
                {
@@ -1518,29 +1491,33 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
                                  != 0)))) {
                                if (filp->f_flags & O_NONBLOCK)
-                                       return -EAGAIN;
-                               return -ERESTARTSYS;
+                                       rc = -EAGAIN;
+                               else
+                                       rc = -ERESTARTSYS;
+                               break;
                        }
 
+                       rc = -EFAULT;
                        if (test_bit(IS_ATR_VALID, &dev->flags) == 0) {
                                tmp = -1;
                                if (copy_to_user(&(atreq->atr_len), &tmp,
                                                 sizeof(int)))
-                                       return -EFAULT;
+                                       break;
                        } else {
                                if (copy_to_user(atreq->atr, dev->atr,
                                                 dev->atr_len))
-                                       return -EFAULT;
+                                       break;
 
                                tmp = dev->atr_len;
                                if (copy_to_user(&(atreq->atr_len), &tmp, sizeof(int)))
-                                       return -EFAULT;
+                                       break;
                        }
-                       return 0;
+                       rc = 0;
+                       break;
                }
        case CM_IOCARDOFF:
 
-#ifdef PCMCIA_DEBUG
+#ifdef CM4000_DEBUG
                DEBUGP(4, dev, "... in CM_IOCARDOFF\n");
                if (dev->flags0 & 0x01) {
                        DEBUGP(4, dev, "    Card inserted\n");
@@ -1564,8 +1541,10 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
                                  == 0)))) {
                                if (filp->f_flags & O_NONBLOCK)
-                                       return -EAGAIN;
-                               return -ERESTARTSYS;
+                                       rc = -EAGAIN;
+                               else
+                                       rc = -ERESTARTSYS;
+                               break;
                        }
                        /* Set Flags0 = 0x42 */
                        DEBUGP(4, dev, "Set Flags0=0x42 \n");
@@ -1580,22 +1559,27 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                              || (test_bit(IS_ATR_VALID, (void *)&dev->flags) !=
                                  0)))) {
                                if (filp->f_flags & O_NONBLOCK)
-                                       return -EAGAIN;
-                               return -ERESTARTSYS;
+                                       rc = -EAGAIN;
+                               else
+                                       rc = -ERESTARTSYS;
+                               break;
                        }
                }
                /* release lock */
                clear_bit(LOCK_IO, &dev->flags);
                wake_up_interruptible(&dev->ioq);
 
-               return 0;
+               rc = 0;
+               break;
        case CM_IOCSPTS:
                {
                        struct ptsreq krnptsreq;
 
                        if (copy_from_user(&krnptsreq, argp,
-                                          sizeof(struct ptsreq)))
-                               return -EFAULT;
+                                          sizeof(struct ptsreq))) {
+                               rc = -EFAULT;
+                               break;
+                       }
 
                        rc = 0;
                        DEBUGP(4, dev, "... in CM_IOCSPTS\n");
@@ -1606,8 +1590,10 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                              || (test_bit(IS_ATR_PRESENT, (void *)&dev->flags)
                                  != 0)))) {
                                if (filp->f_flags & O_NONBLOCK)
-                                       return -EAGAIN;
-                               return -ERESTARTSYS;
+                                       rc = -EAGAIN;
+                               else
+                                       rc = -ERESTARTSYS;
+                               break;
                        }
                        /* get IO lock */
                        if (wait_event_interruptible
@@ -1616,8 +1602,10 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                              || (test_and_set_bit(LOCK_IO, (void *)&dev->flags)
                                  == 0)))) {
                                if (filp->f_flags & O_NONBLOCK)
-                                       return -EAGAIN;
-                               return -ERESTARTSYS;
+                                       rc = -EAGAIN;
+                               else
+                                       rc = -ERESTARTSYS;
+                               break;
                        }
 
                        if ((rc = set_protocol(dev, &krnptsreq)) != 0) {
@@ -1630,43 +1618,42 @@ static int cmm_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
                        wake_up_interruptible(&dev->ioq);
 
                }
-               return rc;
-#ifdef PCMCIA_DEBUG
-       case CM_IOSDBGLVL:      /* set debug log level */
-               {
-                       int old_pc_debug = 0;
-
-                       old_pc_debug = pc_debug;
-                       if (copy_from_user(&pc_debug, argp, sizeof(int)))
-                               return -EFAULT;
-
-                       if (old_pc_debug != pc_debug)
-                               DEBUGP(0, dev, "Changed debug log level "
-                                      "to %i\n", pc_debug);
-               }
-               return rc;
+               break;
+#ifdef CM4000_DEBUG
+       case CM_IOSDBGLVL:
+               rc = -ENOTTY;
+               break;
 #endif
        default:
                DEBUGP(4, dev, "... in default (unknown IOCTL code)\n");
-               return -EINVAL;
+               rc = -ENOTTY;
        }
+out:
+       unlock_kernel();
+       return rc;
 }
 
 static int cmm_open(struct inode *inode, struct file *filp)
 {
        struct cm4000_dev *dev;
        struct pcmcia_device *link;
-       int rc, minor = iminor(inode);
+       int minor = iminor(inode);
+       int ret;
 
        if (minor >= CM4000_MAX_DEV)
                return -ENODEV;
 
+       lock_kernel();
        link = dev_table[minor];
-       if (link == NULL || !pcmcia_dev_present(link))
-               return -ENODEV;
+       if (link == NULL || !pcmcia_dev_present(link)) {
+               ret = -ENODEV;
+               goto out;
+       }
 
-       if (link->open)
-               return -EBUSY;
+       if (link->open) {
+               ret = -EBUSY;
+               goto out;
+       }
 
        dev = link->priv;
        filp->private_data = dev;
@@ -1686,8 +1673,10 @@ static int cmm_open(struct inode *inode, struct file *filp)
         * vaild = block until valid (or card
         * inserted)
         */
-       if (filp->f_flags & O_NONBLOCK)
-               return -EAGAIN;
+       if (filp->f_flags & O_NONBLOCK) {
+               ret = -EAGAIN;
+               goto out;
+       }
 
        dev->mdelay = T_50MSEC;
 
@@ -1695,10 +1684,12 @@ static int cmm_open(struct inode *inode, struct file *filp)
        start_monitor(dev);
 
        link->open = 1;         /* only one open per device */
-       rc = 0;
 
        DEBUGP(2, dev, "<- cmm_open\n");
-       return nonseekable_open(inode, filp);
+       ret = nonseekable_open(inode, filp);
+out:
+       unlock_kernel();
+       return ret;
 }
 
 static int cmm_close(struct inode *inode, struct file *filp)
@@ -1754,83 +1745,40 @@ static void cmm_cm4000_release(struct pcmcia_device * link)
 
 /*==== Interface to PCMCIA Layer =======================================*/
 
+static int cm4000_config_check(struct pcmcia_device *p_dev,
+                              cistpl_cftable_entry_t *cfg,
+                              cistpl_cftable_entry_t *dflt,
+                              unsigned int vcc,
+                              void *priv_data)
+{
+       if (!cfg->io.nwin)
+               return -ENODEV;
+
+       /* Get the IOaddr */
+       p_dev->io.BasePort1 = cfg->io.win[0].base;
+       p_dev->io.NumPorts1 = cfg->io.win[0].len;
+       p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
+       if (!(cfg->io.flags & CISTPL_IO_8BIT))
+               p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
+       if (!(cfg->io.flags & CISTPL_IO_16BIT))
+               p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
+       p_dev->io.IOAddrLines = cfg->io.flags & CISTPL_IO_LINES_MASK;
+
+       return pcmcia_request_io(p_dev, &p_dev->io);
+}
+
 static int cm4000_config(struct pcmcia_device * link, int devno)
 {
        struct cm4000_dev *dev;
-       tuple_t tuple;
-       cisparse_t parse;
-       u_char buf[64];
-       int fail_fn, fail_rc;
-       int rc;
 
        /* read the config-tuples */
-       tuple.DesiredTuple = CISTPL_CONFIG;
-       tuple.Attributes = 0;
-       tuple.TupleData = buf;
-       tuple.TupleDataMax = sizeof(buf);
-       tuple.TupleOffset = 0;
-
-       if ((fail_rc = pcmcia_get_first_tuple(link, &tuple)) != CS_SUCCESS) {
-               fail_fn = GetFirstTuple;
-               goto cs_failed;
-       }
-       if ((fail_rc = pcmcia_get_tuple_data(link, &tuple)) != CS_SUCCESS) {
-               fail_fn = GetTupleData;
-               goto cs_failed;
-       }
-       if ((fail_rc =
-            pcmcia_parse_tuple(link, &tuple, &parse)) != CS_SUCCESS) {
-               fail_fn = ParseTuple;
-               goto cs_failed;
-       }
-
-       link->conf.ConfigBase = parse.config.base;
-       link->conf.Present = parse.config.rmask[0];
-
-       link->io.BasePort2 = 0;
-       link->io.NumPorts2 = 0;
-       link->io.Attributes2 = 0;
-       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
-       for (rc = pcmcia_get_first_tuple(link, &tuple);
-            rc == CS_SUCCESS; rc = pcmcia_get_next_tuple(link, &tuple)) {
-
-               rc = pcmcia_get_tuple_data(link, &tuple);
-               if (rc != CS_SUCCESS)
-                       continue;
-               rc = pcmcia_parse_tuple(link, &tuple, &parse);
-               if (rc != CS_SUCCESS)
-                       continue;
-
-               link->conf.ConfigIndex = parse.cftable_entry.index;
-
-               if (!parse.cftable_entry.io.nwin)
-                       continue;
-
-               /* Get the IOaddr */
-               link->io.BasePort1 = parse.cftable_entry.io.win[0].base;
-               link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
-               link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
-               if (!(parse.cftable_entry.io.flags & CISTPL_IO_8BIT))
-                       link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
-               if (!(parse.cftable_entry.io.flags & CISTPL_IO_16BIT))
-                       link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
-               link->io.IOAddrLines = parse.cftable_entry.io.flags
-                   & CISTPL_IO_LINES_MASK;
-
-               rc = pcmcia_request_io(link, &link->io);
-               if (rc == CS_SUCCESS)
-                       break;  /* we are done */
-       }
-       if (rc != CS_SUCCESS)
+       if (pcmcia_loop_config(link, cm4000_config_check, NULL))
                goto cs_release;
 
        link->conf.IntType = 00000002;
 
-       if ((fail_rc =
-            pcmcia_request_configuration(link, &link->conf)) != CS_SUCCESS) {
-               fail_fn = RequestConfiguration;
+       if (pcmcia_request_configuration(link, &link->conf))
                goto cs_release;
-       }
 
        dev = link->priv;
        sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
@@ -1841,8 +1789,6 @@ static int cm4000_config(struct pcmcia_device * link, int devno)
 
        return 0;
 
-cs_failed:
-       cs_error(link, fail_fn, fail_rc);
 cs_release:
        cm4000_release(link);
        return -ENODEV;
@@ -1871,7 +1817,7 @@ static int cm4000_resume(struct pcmcia_device *link)
 
 static void cm4000_release(struct pcmcia_device *link)
 {
-       cmm_cm4000_release(link->priv); /* delay release until device closed */
+       cmm_cm4000_release(link);       /* delay release until device closed */
        pcmcia_disable_device(link);
 }
 
@@ -1905,11 +1851,13 @@ static int cm4000_probe(struct pcmcia_device *link)
        init_waitqueue_head(&dev->readq);
 
        ret = cm4000_config(link, i);
-       if (ret)
+       if (ret) {
+               dev_table[i] = NULL;
+               kfree(dev);
                return ret;
+       }
 
-       class_device_create(cmm_class, NULL, MKDEV(major, i), NULL,
-                           "cmm%d", i);
+       device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
 
        return 0;
 }
@@ -1931,18 +1879,18 @@ static void cm4000_detach(struct pcmcia_device *link)
        cm4000_release(link);
 
        dev_table[devno] = NULL;
-       kfree(dev);
+       kfree(dev);
 
-       class_device_destroy(cmm_class, MKDEV(major, devno));
+       device_destroy(cmm_class, MKDEV(major, devno));
 
        return;
 }
 
-static struct file_operations cm4000_fops = {
+static const struct file_operations cm4000_fops = {
        .owner  = THIS_MODULE,
        .read   = cmm_read,
        .write  = cmm_write,
-       .ioctl  = cmm_ioctl,
+       .unlocked_ioctl = cmm_ioctl,
        .open   = cmm_open,
        .release= cmm_close,
 };
@@ -1973,19 +1921,21 @@ static int __init cmm_init(void)
        printk(KERN_INFO "%s\n", version);
 
        cmm_class = class_create(THIS_MODULE, "cardman_4000");
-       if (!cmm_class)
-               return -1;
+       if (IS_ERR(cmm_class))
+               return PTR_ERR(cmm_class);
 
        major = register_chrdev(0, DEVICE_NAME, &cm4000_fops);
        if (major < 0) {
                printk(KERN_WARNING MODULE_NAME
                        ": could not get major number\n");
-               return -1;
+               class_destroy(cmm_class);
+               return major;
        }
 
        rc = pcmcia_register_driver(&cm4000_driver);
        if (rc < 0) {
                unregister_chrdev(major, DEVICE_NAME);
+               class_destroy(cmm_class);
                return rc;
        }