include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / char / pcmcia / cm4000_cs.c
index 454d732..c9bc896 100644 (file)
@@ -23,8 +23,6 @@
   * 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>
@@ -32,8 +30,9 @@
 #include <linux/fs.h>
 #include <linux/delay.h>
 #include <linux/bitrev.h>
-#include <asm/uaccess.h>
-#include <asm/io.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))
-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)
@@ -173,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)
@@ -188,8 +185,7 @@ 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;
 }
@@ -513,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
@@ -578,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));
@@ -654,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
 
@@ -897,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 {
@@ -1016,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;
@@ -1054,7 +1047,7 @@ 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;
+       struct cm4000_dev *dev = filp->private_data;
        unsigned int iobase = dev->p_dev->io.BasePort1;
        unsigned short s;
        unsigned char tmp;
@@ -1405,16 +1398,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;
        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",
@@ -1422,42 +1415,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:
@@ -1477,9 +1474,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");
                {
@@ -1492,29 +1489,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");
@@ -1538,8 +1539,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");
@@ -1554,22 +1557,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");
@@ -1580,8 +1588,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
@@ -1590,8 +1600,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) {
@@ -1604,26 +1616,19 @@ 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)
@@ -1631,16 +1636,22 @@ static int cmm_open(struct inode *inode, struct file *filp)
        struct cm4000_dev *dev;
        struct pcmcia_device *link;
        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;
@@ -1660,8 +1671,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;
 
@@ -1671,7 +1684,10 @@ static int cmm_open(struct inode *inode, struct file *filp)
        link->open = 1;         /* only one open per device */
 
        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)
@@ -1727,65 +1743,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.Attributes = 0;
-       tuple.TupleData = buf;
-       tuple.TupleDataMax = sizeof(buf);
-       tuple.TupleOffset = 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);
@@ -1864,7 +1855,7 @@ static int cm4000_probe(struct pcmcia_device *link)
                return ret;
        }
 
-       device_create(cmm_class, NULL, MKDEV(major, i), "cmm%d", i);
+       device_create(cmm_class, NULL, MKDEV(major, i), NULL, "cmm%d", i);
 
        return 0;
 }
@@ -1897,7 +1888,7 @@ 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,
 };