]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/char/pcmcia/cm4040_cs.c
pcmcia: introduce autoconfiguration feature
[linux-2.6.git] / drivers / char / pcmcia / cm4040_cs.c
index 55cf4be42976913547663b6b15b41b196d9c5800..acf88d5c72b2bf2168e6b770dceed8bd07bb9970 100644 (file)
@@ -17,8 +17,6 @@
  * All rights reserved, Dual BSD/GPL Licensed.
  */
 
-/* #define PCMCIA_DEBUG 6 */
-
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/fs.h>
 #include <linux/delay.h>
 #include <linux/poll.h>
+#include <linux/smp_lock.h>
 #include <linux/wait.h>
 #include <asm/uaccess.h>
 #include <asm/io.h>
 
-#include <pcmcia/cs_types.h>
-#include <pcmcia/cs.h>
 #include <pcmcia/cistpl.h>
 #include <pcmcia/cisreg.h>
 #include <pcmcia/ciscode.h>
 #include "cm4040_cs.h"
 
 
-#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
+ * CM4040_DEBUG set */
+/* #define CM4040_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 =
 "OMNIKEY CardMan 4040 v1.1.0gm5 - All bugs added by Harald Welte";
@@ -75,7 +70,6 @@ static struct class *cmx_class;
 
 struct reader_dev {
        struct pcmcia_device    *p_dev;
-       dev_node_t              node;
        wait_queue_head_t       devq;
        wait_queue_head_t       poll_wait;
        wait_queue_head_t       read_wait;
@@ -89,14 +83,13 @@ struct reader_dev {
 
 static struct pcmcia_device *dev_table[CM_MAX_DEV];
 
-#ifndef PCMCIA_DEBUG
+#ifndef CM4040_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);
 }
 
@@ -105,8 +98,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;
 }
 #endif
@@ -116,7 +108,7 @@ static inline unsigned char xinb(unsigned short port)
 static void cm4040_do_poll(unsigned long dummy)
 {
        struct reader_dev *dev = (struct reader_dev *) dummy;
-       unsigned int obs = xinb(dev->p_dev->io.BasePort1
+       unsigned int obs = xinb(dev->p_dev->resource[0]->start
                                + REG_OFFSET_BUFFER_STATUS);
 
        if ((obs & BSR_BULK_IN_FULL)) {
@@ -147,7 +139,7 @@ static void cm4040_stop_poll(struct reader_dev *dev)
 static int wait_for_bulk_out_ready(struct reader_dev *dev)
 {
        int i, rc;
-       int iobase = dev->p_dev->io.BasePort1;
+       int iobase = dev->p_dev->resource[0]->start;
 
        for (i = 0; i < POLL_LOOP_COUNT; i++) {
                if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
@@ -177,7 +169,7 @@ static int wait_for_bulk_out_ready(struct reader_dev *dev)
 /* Write to Sync Control Register */
 static int write_sync_reg(unsigned char val, struct reader_dev *dev)
 {
-       int iobase = dev->p_dev->io.BasePort1;
+       int iobase = dev->p_dev->resource[0]->start;
        int rc;
 
        rc = wait_for_bulk_out_ready(dev);
@@ -195,7 +187,7 @@ static int write_sync_reg(unsigned char val, struct reader_dev *dev)
 static int wait_for_bulk_in_ready(struct reader_dev *dev)
 {
        int i, rc;
-       int iobase = dev->p_dev->io.BasePort1;
+       int iobase = dev->p_dev->resource[0]->start;
 
        for (i = 0; i < POLL_LOOP_COUNT; i++) {
                if ((xinb(iobase + REG_OFFSET_BUFFER_STATUS)
@@ -225,7 +217,7 @@ static ssize_t cm4040_read(struct file *filp, char __user *buf,
                        size_t count, loff_t *ppos)
 {
        struct reader_dev *dev = filp->private_data;
-       int iobase = dev->p_dev->io.BasePort1;
+       int iobase = dev->p_dev->resource[0]->start;
        size_t bytes_to_read;
        unsigned long i;
        size_t min_bytes_to_read;
@@ -259,22 +251,22 @@ static ssize_t cm4040_read(struct file *filp, char __user *buf,
                        return -EIO;
                }
                dev->r_buf[i] = xinb(iobase + REG_OFFSET_BULK_IN);
-#ifdef PCMCIA_DEBUG
-               if (pc_debug >= 6)
-                       printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
+#ifdef CM4040_DEBUG
+               pr_debug("%lu:%2x ", i, dev->r_buf[i]);
        }
-       printk("\n");
+       pr_debug("\n");
 #else
        }
 #endif
 
        bytes_to_read = 5 + le32_to_cpu(*(__le32 *)&dev->r_buf[1]);
 
-       DEBUGP(6, dev, "BytesToRead=%lu\n", bytes_to_read);
+       DEBUGP(6, dev, "BytesToRead=%zu\n", bytes_to_read);
 
        min_bytes_to_read = min(count, bytes_to_read + 5);
+       min_bytes_to_read = min_t(size_t, min_bytes_to_read, READ_WRITE_BUFFER_SIZE);
 
-       DEBUGP(6, dev, "Min=%lu\n", min_bytes_to_read);
+       DEBUGP(6, dev, "Min=%zu\n", min_bytes_to_read);
 
        for (i = 0; i < (min_bytes_to_read-5); i++) {
                rc = wait_for_bulk_in_ready(dev);
@@ -286,11 +278,10 @@ static ssize_t cm4040_read(struct file *filp, char __user *buf,
                        return -EIO;
                }
                dev->r_buf[i+5] = xinb(iobase + REG_OFFSET_BULK_IN);
-#ifdef PCMCIA_DEBUG
-               if (pc_debug >= 6)
-                       printk(KERN_DEBUG "%lu:%2x ", i, dev->r_buf[i]);
+#ifdef CM4040_DEBUG
+               pr_debug("%lu:%2x ", i, dev->r_buf[i]);
        }
-       printk("\n");
+       pr_debug("\n");
 #else
        }
 #endif
@@ -328,7 +319,7 @@ static ssize_t cm4040_write(struct file *filp, const char __user *buf,
                         size_t count, loff_t *ppos)
 {
        struct reader_dev *dev = filp->private_data;
-       int iobase = dev->p_dev->io.BasePort1;
+       int iobase = dev->p_dev->resource[0]->start;
        ssize_t rc;
        int i;
        unsigned int bytes_to_write;
@@ -340,7 +331,7 @@ static ssize_t cm4040_write(struct file *filp, const char __user *buf,
                return 0;
        }
 
-       if (count < 5) {
+       if ((count < 5) || (count > READ_WRITE_BUFFER_SIZE)) {
                DEBUGP(2, dev, "<- cm4040_write buffersize=%Zd < 5\n", count);
                return -EIO;
        }
@@ -447,23 +438,30 @@ static int cm4040_open(struct inode *inode, struct file *filp)
        struct reader_dev *dev;
        struct pcmcia_device *link;
        int minor = iminor(inode);
+       int ret;
 
        if (minor >= CM_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;
 
        if (filp->f_flags & O_NONBLOCK) {
                DEBUGP(4, dev, "filep->f_flags O_NONBLOCK set\n");
-               return -EAGAIN;
+               ret = -EAGAIN;
+               goto out;
        }
 
        link->open = 1;
@@ -472,7 +470,10 @@ static int cm4040_open(struct inode *inode, struct file *filp)
        mod_timer(&dev->poll_timer, jiffies + POLL_PERIOD);
 
        DEBUGP(2, dev, "<- cm4040_open (successfully)\n");
-       return nonseekable_open(inode, filp);
+       ret = nonseekable_open(inode, filp);
+out:
+       unlock_kernel();
+       return ret;
 }
 
 static int cm4040_close(struct inode *inode, struct file *filp)
@@ -514,103 +515,52 @@ static void cm4040_reader_release(struct pcmcia_device *link)
        return;
 }
 
-static int reader_config(struct pcmcia_device *link, int devno)
+static int cm4040_config_check(struct pcmcia_device *p_dev,
+                              cistpl_cftable_entry_t *cfg,
+                              cistpl_cftable_entry_t *dflt,
+                              void *priv_data)
 {
-       struct reader_dev *dev;
-       tuple_t tuple;
-       cisparse_t parse;
-       u_char buf[64];
-       int fail_fn, fail_rc;
        int rc;
+       if (!cfg->io.nwin)
+               return -ENODEV;
 
-       tuple.DesiredTuple = CISTPL_CONFIG;
-       tuple.Attributes = 0;
-       tuple.TupleData = buf;
-       tuple.TupleDataMax = sizeof(buf);
-       tuple.TupleOffset = 0;
+       /* Get the IOaddr */
+       p_dev->resource[0]->start = cfg->io.win[0].base;
+       p_dev->resource[0]->end = cfg->io.win[0].len;
+       p_dev->resource[0]->flags |= pcmcia_io_cfg_data_width(cfg->io.flags);
+       p_dev->io_lines = cfg->io.flags & CISTPL_IO_LINES_MASK;
+       rc = pcmcia_request_io(p_dev);
 
-       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;
-       }
+       dev_printk(KERN_INFO, &p_dev->dev,
+                  "pcmcia_request_io returned 0x%x\n", rc);
+       return rc;
+}
 
-       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;
-
-               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);
-
-               dev_printk(KERN_INFO, &handle_to_dev(link), "foo");
-               if (rc == CS_SUCCESS)
-                       break;
-               else
-                       dev_printk(KERN_INFO, &handle_to_dev(link),
-                                  "pcmcia_request_io failed 0x%x\n", rc);
-       }
-       if (rc != CS_SUCCESS)
-               goto cs_release;
 
-       link->conf.IntType = 00000002;
+static int reader_config(struct pcmcia_device *link, int devno)
+{
+       struct reader_dev *dev;
+       int fail_rc;
 
-       if ((fail_rc = pcmcia_request_configuration(link,&link->conf))
-                                                               !=CS_SUCCESS) {
-               fail_fn = RequestConfiguration;
-               dev_printk(KERN_INFO, &handle_to_dev(link),
-                          "pcmcia_request_configuration failed 0x%x\n",
+       if (pcmcia_loop_config(link, cm4040_config_check, NULL))
+               goto cs_release;
+
+       fail_rc = pcmcia_enable_device(link);
+       if (fail_rc != 0) {
+               dev_printk(KERN_INFO, &link->dev,
+                          "pcmcia_enable_device failed 0x%x\n",
                           fail_rc);
                goto cs_release;
        }
 
        dev = link->priv;
-       sprintf(dev->node.dev_name, DEVICE_NAME "%d", devno);
-       dev->node.major = major;
-       dev->node.minor = devno;
-       dev->node.next = &dev->node;
 
-       DEBUGP(2, dev, "device " DEVICE_NAME "%d at 0x%.4x-0x%.4x\n", devno,
-             link->io.BasePort1, link->io.BasePort1+link->io.NumPorts1);
+       DEBUGP(2, dev, "device " DEVICE_NAME "%d at %pR\n", devno,
+             link->resource[0]);
        DEBUGP(2, dev, "<- reader_config (succ)\n");
 
        return 0;
 
-cs_failed:
-       cs_error(link, fail_fn, fail_rc);
 cs_release:
        reader_release(link);
        return -ENODEV;
@@ -618,7 +568,7 @@ cs_release:
 
 static void reader_release(struct pcmcia_device *link)
 {
-       cm4040_reader_release(link->priv);
+       cm4040_reader_release(link);
        pcmcia_disable_device(link);
 }
 
@@ -645,22 +595,22 @@ static int reader_probe(struct pcmcia_device *link)
        link->priv = dev;
        dev->p_dev = link;
 
-       link->conf.IntType = INT_MEMORY_AND_IO;
        dev_table[i] = link;
 
        init_waitqueue_head(&dev->devq);
        init_waitqueue_head(&dev->poll_wait);
        init_waitqueue_head(&dev->read_wait);
        init_waitqueue_head(&dev->write_wait);
-       init_timer(&dev->poll_timer);
-       dev->poll_timer.function = &cm4040_do_poll;
+       setup_timer(&dev->poll_timer, cm4040_do_poll, 0);
 
        ret = reader_config(link, i);
-       if (ret)
+       if (ret) {
+               dev_table[i] = NULL;
+               kfree(dev);
                return ret;
+       }
 
-       class_device_create(cmx_class, NULL, MKDEV(major, i), NULL,
-                           "cmx%d", i);
+       device_create(cmx_class, NULL, MKDEV(major, i), NULL, "cmx%d", i);
 
        return 0;
 }
@@ -683,7 +633,7 @@ static void reader_detach(struct pcmcia_device *link)
        dev_table[devno] = NULL;
        kfree(dev);
 
-       class_device_destroy(cmx_class, MKDEV(major, devno));
+       device_destroy(cmx_class, MKDEV(major, devno));
 
        return;
 }
@@ -721,19 +671,21 @@ static int __init cm4040_init(void)
 
        printk(KERN_INFO "%s\n", version);
        cmx_class = class_create(THIS_MODULE, "cardman_4040");
-       if (!cmx_class)
-               return -1;
+       if (IS_ERR(cmx_class))
+               return PTR_ERR(cmx_class);
 
        major = register_chrdev(0, DEVICE_NAME, &reader_fops);
        if (major < 0) {
                printk(KERN_WARNING MODULE_NAME
                        ": could not get major number\n");
-               return -1;
+               class_destroy(cmx_class);
+               return major;
        }
 
        rc = pcmcia_register_driver(&reader_driver);
        if (rc < 0) {
                unregister_chrdev(major, DEVICE_NAME);
+               class_destroy(cmx_class);
                return rc;
        }