Merge branch 'topic/core-cleanup' into for-linus
[safe/jmp/linux-2.6] / drivers / char / pcmcia / cm4000_cs.c
index 59ca351..90b199f 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>
 
 /* #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,     \
-                          __func__ , ## 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)
@@ -174,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)
@@ -189,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;
 }
@@ -514,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
@@ -579,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));
@@ -655,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
 
@@ -898,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 {
@@ -1017,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;
@@ -1034,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);
@@ -1055,7 +1049,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;
@@ -1415,7 +1409,7 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
        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",
@@ -1423,9 +1417,9 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                [_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;
@@ -1439,7 +1433,7 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                DEBUGP(4, dev, "CMM_ABSENT flag set\n");
                goto out;
        }
-       rc = EINVAL;
+       rc = -EINVAL;
 
        if (_IOC_TYPE(cmd) != CM_IOC_MAGIC) {
                DEBUGP(4, dev, "ioctype mismatch\n");
@@ -1523,7 +1517,7 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                }
        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");
@@ -1575,7 +1569,8 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
                clear_bit(LOCK_IO, &dev->flags);
                wake_up_interruptible(&dev->ioq);
 
-               return 0;
+               rc = 0;
+               break;
        case CM_IOCSPTS:
                {
                        struct ptsreq krnptsreq;
@@ -1624,18 +1619,9 @@ static long cmm_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 
                }
                break;
-#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)))
-                               rc = -EFAULT;
-                       else if (old_pc_debug != pc_debug)
-                               DEBUGP(0, dev, "Changed debug log level "
-                                      "to %i\n", pc_debug);
-               }
+#ifdef CM4000_DEBUG
+       case CM_IOSDBGLVL:
+               rc = -ENOTTY;
                break;
 #endif
        default:
@@ -1759,65 +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.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);
@@ -1896,7 +1857,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;
 }