Merge branch 'pxa-trizeps' into pxa-machines
[safe/jmp/linux-2.6] / drivers / pcmcia / pcmcia_ioctl.c
index 56b625d..419f97f 100644 (file)
@@ -18,7 +18,6 @@
  */
 
 
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/proc_fs.h>
 #include <linux/poll.h>
 #include <linux/pci.h>
+#include <linux/smp_lock.h>
 #include <linux/workqueue.h>
 
-#define IN_CARD_SERVICES
 #include <pcmcia/cs_types.h>
 #include <pcmcia/cs.h>
 #include <pcmcia/cistpl.h>
+#include <pcmcia/cisreg.h>
 #include <pcmcia/ds.h>
 #include <pcmcia/ss.h>
 
@@ -60,7 +60,6 @@ typedef struct user_info_t {
 
 #ifdef DEBUG
 extern int ds_pc_debug;
-#define cs_socket_name(skt)    ((skt)->dev.class_id)
 
 #define ds_dbg(lvl, fmt, arg...) do {          \
        if (ds_pc_debug >= lvl)                         \
@@ -129,14 +128,165 @@ static int proc_read_drivers(char *buf, char **start, off_t pos,
                             int count, int *eof, void *data)
 {
        char *p = buf;
+       int rc;
 
-       bus_for_each_drv(&pcmcia_bus_type, NULL,
-                        (void *) &p, proc_read_drivers_callback);
+       rc = bus_for_each_drv(&pcmcia_bus_type, NULL,
+                             (void *) &p, proc_read_drivers_callback);
+       if (rc < 0)
+               return rc;
 
        return (p - buf);
 }
 #endif
 
+
+#ifdef CONFIG_PCMCIA_PROBE
+
+static int adjust_irq(struct pcmcia_socket *s, adjust_t *adj)
+{
+       int irq;
+       u32 mask;
+
+       irq = adj->resource.irq.IRQ;
+       if ((irq < 0) || (irq > 15))
+               return CS_BAD_IRQ;
+
+       if (adj->Action != REMOVE_MANAGED_RESOURCE)
+               return 0;
+
+       mask = 1 << irq;
+
+       if (!(s->irq_mask & mask))
+               return 0;
+
+       s->irq_mask &= ~mask;
+
+       return 0;
+}
+
+#else
+
+static inline int adjust_irq(struct pcmcia_socket *s, adjust_t *adj) {
+       return CS_SUCCESS;
+}
+
+#endif
+
+static int pcmcia_adjust_resource_info(adjust_t *adj)
+{
+       struct pcmcia_socket *s;
+       int ret = CS_UNSUPPORTED_FUNCTION;
+       unsigned long flags;
+
+       down_read(&pcmcia_socket_list_rwsem);
+       list_for_each_entry(s, &pcmcia_socket_list, socket_list) {
+
+               if (adj->Resource == RES_IRQ)
+                       ret = adjust_irq(s, adj);
+
+               else if (s->resource_ops->add_io) {
+                       unsigned long begin, end;
+
+                       /* you can't use the old interface if the new
+                        * one was used before */
+                       spin_lock_irqsave(&s->lock, flags);
+                       if ((s->resource_setup_new) &&
+                           !(s->resource_setup_old)) {
+                               spin_unlock_irqrestore(&s->lock, flags);
+                               continue;
+                       } else if (!(s->resource_setup_old))
+                               s->resource_setup_old = 1;
+                       spin_unlock_irqrestore(&s->lock, flags);
+
+                       switch (adj->Resource) {
+                       case RES_MEMORY_RANGE:
+                               begin = adj->resource.memory.Base;
+                               end = adj->resource.memory.Base + adj->resource.memory.Size - 1;
+                               if (s->resource_ops->add_mem)
+                                       ret =s->resource_ops->add_mem(s, adj->Action, begin, end);
+                       case RES_IO_RANGE:
+                               begin = adj->resource.io.BasePort;
+                               end = adj->resource.io.BasePort + adj->resource.io.NumPorts - 1;
+                               if (s->resource_ops->add_io)
+                                       ret = s->resource_ops->add_io(s, adj->Action, begin, end);
+                       }
+                       if (!ret) {
+                               /* as there's no way we know this is the
+                                * last call to adjust_resource_info, we
+                                * always need to assume this is the latest
+                                * one... */
+                               spin_lock_irqsave(&s->lock, flags);
+                               s->resource_setup_done = 1;
+                               spin_unlock_irqrestore(&s->lock, flags);
+                       }
+               }
+       }
+       up_read(&pcmcia_socket_list_rwsem);
+
+       return (ret);
+}
+
+/** pccard_get_status
+ *
+ * Get the current socket state bits.  We don't support the latched
+ * SocketState yet: I haven't seen any point for it.
+ */
+
+static int pccard_get_status(struct pcmcia_socket *s,
+                            struct pcmcia_device *p_dev,
+                            cs_status_t *status)
+{
+       config_t *c;
+       int val;
+
+       s->ops->get_status(s, &val);
+       status->CardState = status->SocketState = 0;
+       status->CardState |= (val & SS_DETECT) ? CS_EVENT_CARD_DETECT : 0;
+       status->CardState |= (val & SS_CARDBUS) ? CS_EVENT_CB_DETECT : 0;
+       status->CardState |= (val & SS_3VCARD) ? CS_EVENT_3VCARD : 0;
+       status->CardState |= (val & SS_XVCARD) ? CS_EVENT_XVCARD : 0;
+       if (s->state & SOCKET_SUSPEND)
+               status->CardState |= CS_EVENT_PM_SUSPEND;
+       if (!(s->state & SOCKET_PRESENT))
+               return CS_NO_CARD;
+
+       c = (p_dev) ? p_dev->function_config : NULL;
+
+       if ((c != NULL) && (c->state & CONFIG_LOCKED) &&
+           (c->IntType & (INT_MEMORY_AND_IO | INT_ZOOMED_VIDEO))) {
+               u_char reg;
+               if (c->CardValues & PRESENT_PIN_REPLACE) {
+                       pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_PRR)>>1, 1, &reg);
+                       status->CardState |=
+                               (reg & PRR_WP_STATUS) ? CS_EVENT_WRITE_PROTECT : 0;
+                       status->CardState |=
+                               (reg & PRR_READY_STATUS) ? CS_EVENT_READY_CHANGE : 0;
+                       status->CardState |=
+                               (reg & PRR_BVD2_STATUS) ? CS_EVENT_BATTERY_LOW : 0;
+                       status->CardState |=
+                               (reg & PRR_BVD1_STATUS) ? CS_EVENT_BATTERY_DEAD : 0;
+               } else {
+                       /* No PRR?  Then assume we're always ready */
+                       status->CardState |= CS_EVENT_READY_CHANGE;
+               }
+               if (c->CardValues & PRESENT_EXT_STATUS) {
+                       pcmcia_read_cis_mem(s, 1, (c->ConfigBase+CISREG_ESR)>>1, 1, &reg);
+                       status->CardState |=
+                               (reg & ESR_REQ_ATTN) ? CS_EVENT_REQUEST_ATTENTION : 0;
+               }
+               return CS_SUCCESS;
+       }
+       status->CardState |=
+               (val & SS_WRPROT) ? CS_EVENT_WRITE_PROTECT : 0;
+       status->CardState |=
+               (val & SS_BATDEAD) ? CS_EVENT_BATTERY_DEAD : 0;
+       status->CardState |=
+               (val & SS_BATWARN) ? CS_EVENT_BATTERY_LOW : 0;
+       status->CardState |=
+               (val & SS_READY) ? CS_EVENT_READY_CHANGE : 0;
+       return CS_SUCCESS;
+} /* pccard_get_status */
+
 /*======================================================================
 
     These manage a ring buffer of events pending for one user process
@@ -230,7 +380,7 @@ static int bind_request(struct pcmcia_socket *s, bind_info_t *bind_info)
                                         * by userspace before, we need to
                                         * return the "instance". */
                                        spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
-                                       bind_info->instance = p_dev->instance;
+                                       bind_info->instance = p_dev;
                                        ret = -EBUSY;
                                        goto err_put_module;
                                } else {
@@ -269,9 +419,11 @@ rescan:
        /*
         * Prevent this racing with a card insertion.
         */
-       down(&s->skt_sem);
-       bus_rescan_devices(&pcmcia_bus_type);
-       up(&s->skt_sem);
+       mutex_lock(&s->skt_mutex);
+       ret = bus_rescan_devices(&pcmcia_bus_type);
+       mutex_unlock(&s->skt_mutex);
+       if (ret)
+               goto err_put_module;
 
        /* check whether the driver indeed matched. I don't care if this
         * is racy or not, because it can only happen on cardmgr access
@@ -305,6 +457,7 @@ static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int
 {
        dev_node_t *node;
        struct pcmcia_device *p_dev;
+       struct pcmcia_driver *p_drv;
        unsigned long flags;
        int ret = 0;
 
@@ -359,16 +512,16 @@ static int get_device_info(struct pcmcia_socket *s, bind_info_t *bind_info, int
  found:
        spin_unlock_irqrestore(&pcmcia_dev_list_lock, flags);
 
-       if ((!p_dev->instance) ||
-           (p_dev->instance->state & DEV_CONFIG_PENDING)) {
+       p_drv = to_pcmcia_drv(p_dev->dev.driver);
+       if (p_drv && !p_dev->_locked) {
                ret = -EAGAIN;
                goto err_put;
        }
 
        if (first)
-               node = p_dev->instance->dev;
+               node = p_dev->dev_node;
        else
-               for (node = p_dev->instance->dev; node; node = node->next)
+               for (node = p_dev->dev_node; node; node = node->next)
                        if (node == bind_info->next)
                                break;
        if (!node) {
@@ -393,20 +546,27 @@ static int ds_open(struct inode *inode, struct file *file)
     struct pcmcia_socket *s;
     user_info_t *user;
     static int warning_printed = 0;
+    int ret = 0;
 
     ds_dbg(0, "ds_open(socket %d)\n", i);
 
+    lock_kernel();
     s = pcmcia_get_socket_by_nr(i);
-    if (!s)
-           return -ENODEV;
+    if (!s) {
+           ret = -ENODEV;
+           goto out;
+    }
     s = pcmcia_get_socket(s);
-    if (!s)
-           return -ENODEV;
+    if (!s) {
+           ret = -ENODEV;
+           goto out;
+    }
 
     if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
            if (s->pcmcia_state.busy) {
                    pcmcia_put_socket(s);
-                   return -EBUSY;
+                   ret = -EBUSY;
+                   goto out;
            }
        else
            s->pcmcia_state.busy = 1;
@@ -415,7 +575,8 @@ static int ds_open(struct inode *inode, struct file *file)
     user = kmalloc(sizeof(user_info_t), GFP_KERNEL);
     if (!user) {
            pcmcia_put_socket(s);
-           return -ENOMEM;
+           ret = -ENOMEM;
+           goto out;
     }
     user->event_tail = user->event_head = 0;
     user->next = s->user;
@@ -426,7 +587,7 @@ static int ds_open(struct inode *inode, struct file *file)
 
     if (!warning_printed) {
            printk(KERN_INFO "pcmcia: Detected deprecated PCMCIA ioctl "
-                       "usage.\n");
+                       "usage from process: %s.\n", current->comm);
            printk(KERN_INFO "pcmcia: This interface will soon be removed from "
                        "the kernel; please expect breakage unless you upgrade "
                        "to new tools.\n");
@@ -437,7 +598,9 @@ static int ds_open(struct inode *inode, struct file *file)
 
     if (s->pcmcia_state.present)
        queue_event(user, CS_EVENT_CARD_INSERTION);
-    return 0;
+out:
+    unlock_kernel();
+    return ret;
 } /* ds_open */
 
 /*====================================================================*/
@@ -481,7 +644,7 @@ static ssize_t ds_read(struct file *file, char __user *buf,
     user_info_t *user;
     int ret;
 
-    ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_dentry->d_inode));
+    ds_dbg(2, "ds_read(socket %d)\n", iminor(file->f_path.dentry->d_inode));
 
     if (count < 4)
        return -EINVAL;
@@ -506,7 +669,7 @@ static ssize_t ds_read(struct file *file, char __user *buf,
 static ssize_t ds_write(struct file *file, const char __user *buf,
                        size_t count, loff_t *ppos)
 {
-    ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_dentry->d_inode));
+    ds_dbg(2, "ds_write(socket %d)\n", iminor(file->f_path.dentry->d_inode));
 
     if (count != 4)
        return -EINVAL;
@@ -524,7 +687,7 @@ static u_int ds_poll(struct file *file, poll_table *wait)
     struct pcmcia_socket *s;
     user_info_t *user;
 
-    ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_dentry->d_inode));
+    ds_dbg(2, "ds_poll(socket %d)\n", iminor(file->f_path.dentry->d_inode));
 
     user = file->private_data;
     if (CHECK_USER(user))
@@ -542,8 +705,6 @@ static u_int ds_poll(struct file *file, poll_table *wait)
 
 /*====================================================================*/
 
-extern int pcmcia_adjust_resource_info(adjust_t *adj);
-
 static int ds_ioctl(struct inode * inode, struct file * file,
                    u_int cmd, u_long arg)
 {
@@ -589,7 +750,12 @@ static int ds_ioctl(struct inode * inode, struct file * file,
 
     err = ret = 0;
 
-    if (cmd & IOC_IN) __copy_from_user((char *)buf, uarg, size);
+    if (cmd & IOC_IN) {
+       if (__copy_from_user((char *)buf, uarg, size)) {
+           err = -EFAULT;
+           goto free_out;
+       }
+    }
 
     switch (cmd) {
     case DS_ADJUST_RESOURCE_INFO:
@@ -606,9 +772,9 @@ static int ds_ioctl(struct inode * inode, struct file * file,
        }
        break;
     case DS_GET_FIRST_TUPLE:
-       down(&s->skt_sem);
+       mutex_lock(&s->skt_mutex);
        pcmcia_validate_mem(s);
-       up(&s->skt_sem);
+       mutex_unlock(&s->skt_mutex);
        ret = pccard_get_first_tuple(s, BIND_FN_ALL, &buf->tuple);
        break;
     case DS_GET_NEXT_TUPLE:
@@ -637,10 +803,10 @@ static int ds_ioctl(struct inode * inode, struct file * file,
            }
            break;
     case DS_VALIDATE_CIS:
-       down(&s->skt_sem);
+       mutex_lock(&s->skt_mutex);
        pcmcia_validate_mem(s);
-       up(&s->skt_sem);
-       ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo);
+       mutex_unlock(&s->skt_mutex);
+       ret = pccard_validate_cis(s, BIND_FN_ALL, &buf->cisinfo.Chains);
        break;
     case DS_SUSPEND_CARD:
        ret = pcmcia_suspend_card(s);
@@ -665,9 +831,10 @@ static int ds_ioctl(struct inode * inode, struct file * file,
        if (!(buf->conf_reg.Function &&
             (buf->conf_reg.Function >= s->functions))) {
                struct pcmcia_device *p_dev = get_pcmcia_device(s, buf->conf_reg.Function);
-               if (p_dev)
+               if (p_dev) {
                        ret = pcmcia_access_configuration_register(p_dev, &buf->conf_reg);
-               pcmcia_put_dev(p_dev);
+                       pcmcia_put_dev(p_dev);
+               }
        }
        break;
     case DS_GET_FIRST_REGION:
@@ -755,7 +922,7 @@ free_out:
 
 /*====================================================================*/
 
-static struct file_operations ds_fops = {
+static const struct file_operations ds_fops = {
        .owner          = THIS_MODULE,
        .open           = ds_open,
        .release        = ds_release,
@@ -777,7 +944,7 @@ void __init pcmcia_setup_ioctl(void) {
                major_dev = i;
 
 #ifdef CONFIG_PROC_FS
-       proc_pccard = proc_mkdir("pccard", proc_bus);
+       proc_pccard = proc_mkdir("bus/pccard", NULL);
        if (proc_pccard)
                create_proc_read_entry("drivers",0,proc_pccard,proc_read_drivers,NULL);
 #endif
@@ -788,7 +955,7 @@ void __exit pcmcia_cleanup_ioctl(void) {
 #ifdef CONFIG_PROC_FS
        if (proc_pccard) {
                remove_proc_entry("drivers", proc_pccard);
-               remove_proc_entry("pccard", proc_bus);
+               remove_proc_entry("bus/pccard", NULL);
        }
 #endif
        if (major_dev != -1)