Merge branch 'master' into for-2.6.35
[safe/jmp/linux-2.6] / drivers / pcmcia / ds.c
index 253d9ac..508f94a 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/firmware.h>
 #include <linux/kref.h>
 #include <linux/dma-mapping.h>
+#include <linux/slab.h>
 
 #include <pcmcia/cs_types.h>
 #include <pcmcia/cs.h>
@@ -237,30 +238,21 @@ static void pcmcia_release_function(struct kref *ref)
 static void pcmcia_release_dev(struct device *dev)
 {
        struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
+       int i;
        dev_dbg(dev, "releasing device\n");
        pcmcia_put_socket(p_dev->socket);
+       for (i = 0; i < 4; i++)
+               kfree(p_dev->prod_id[i]);
        kfree(p_dev->devname);
        kref_put(&p_dev->function_config->ref, pcmcia_release_function);
        kfree(p_dev);
 }
 
-static void pcmcia_add_device_later(struct pcmcia_socket *s, int mfc)
-{
-       if (!s->pcmcia_state.device_add_pending) {
-               dev_dbg(&s->dev, "scheduling to add %s secondary"
-                      " device to %d\n", mfc ? "mfc" : "pfc", s->sock);
-               s->pcmcia_state.device_add_pending = 1;
-               s->pcmcia_state.mfc_pfc = mfc;
-               schedule_work(&s->device_add);
-       }
-       return;
-}
 
 static int pcmcia_device_probe(struct device *dev)
 {
        struct pcmcia_device *p_dev;
        struct pcmcia_driver *p_drv;
-       struct pcmcia_device_id *did;
        struct pcmcia_socket *s;
        cistpl_config_t cis_config;
        int ret = 0;
@@ -273,18 +265,6 @@ static int pcmcia_device_probe(struct device *dev)
        p_drv = to_pcmcia_drv(dev->driver);
        s = p_dev->socket;
 
-       /* The PCMCIA code passes the match data in via dev_set_drvdata(dev)
-        * which is an ugly hack. Once the driver probe is called it may
-        * and often will overwrite the match data so we must save it first
-        *
-        * handle pseudo multifunction devices:
-        * there are at most two pseudo multifunction devices.
-        * if we're matching against the first, schedule a
-        * call which will then check whether there are two
-        * pseudo devices, and if not, add the second one.
-        */
-       did = dev_get_drvdata(&p_dev->dev);
-
        dev_dbg(dev, "trying to bind to %s\n", p_drv->drv.name);
 
        if ((!p_drv->probe) || (!p_dev->function_config) ||
@@ -314,9 +294,9 @@ static int pcmcia_device_probe(struct device *dev)
        }
 
        mutex_lock(&s->ops_mutex);
-       if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
+       if ((s->pcmcia_state.has_pfc) &&
            (p_dev->socket->device_count == 1) && (p_dev->device_no == 0))
-               pcmcia_add_device_later(p_dev->socket, 0);
+               pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
        mutex_unlock(&s->ops_mutex);
 
 put_module:
@@ -355,7 +335,6 @@ static void pcmcia_card_remove(struct pcmcia_socket *s, struct pcmcia_device *le
 
                mutex_lock(&s->ops_mutex);
                list_del(&p_dev->socket_device_list);
-               p_dev->_removed = 1;
                mutex_unlock(&s->ops_mutex);
 
                dev_dbg(&p_dev->dev, "unregistering device\n");
@@ -369,7 +348,6 @@ static int pcmcia_device_remove(struct device *dev)
 {
        struct pcmcia_device *p_dev;
        struct pcmcia_driver *p_drv;
-       struct pcmcia_device_id *did;
        int i;
 
        p_dev = to_pcmcia_dev(dev);
@@ -381,8 +359,7 @@ static int pcmcia_device_remove(struct device *dev)
         * pseudo multi-function card, we need to unbind
         * all devices
         */
-       did = dev_get_drvdata(&p_dev->dev);
-       if (did && (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) &&
+       if ((p_dev->socket->pcmcia_state.has_pfc) &&
            (p_dev->socket->device_count > 0) &&
            (p_dev->device_no == 0))
                pcmcia_card_remove(p_dev->socket, p_dev);
@@ -432,16 +409,20 @@ static int pcmcia_device_query(struct pcmcia_device *p_dev)
 
        if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL,
                               CISTPL_MANFID, &manf_id)) {
+               mutex_lock(&p_dev->socket->ops_mutex);
                p_dev->manf_id = manf_id.manf;
                p_dev->card_id = manf_id.card;
                p_dev->has_manf_id = 1;
                p_dev->has_card_id = 1;
+               mutex_unlock(&p_dev->socket->ops_mutex);
        }
 
        if (!pccard_read_tuple(p_dev->socket, p_dev->func,
                               CISTPL_FUNCID, &func_id)) {
+               mutex_lock(&p_dev->socket->ops_mutex);
                p_dev->func_id = func_id.func;
                p_dev->has_func_id = 1;
+               mutex_unlock(&p_dev->socket->ops_mutex);
        } else {
                /* rule of thumb: cards with no FUNCID, but with
                 * common memory device geometry information, are
@@ -458,17 +439,21 @@ static int pcmcia_device_query(struct pcmcia_device *p_dev)
                        dev_dbg(&p_dev->dev,
                                   "mem device geometry probably means "
                                   "FUNCID_MEMORY\n");
+                       mutex_lock(&p_dev->socket->ops_mutex);
                        p_dev->func_id = CISTPL_FUNCID_MEMORY;
                        p_dev->has_func_id = 1;
+                       mutex_unlock(&p_dev->socket->ops_mutex);
                }
                kfree(devgeo);
        }
 
        if (!pccard_read_tuple(p_dev->socket, BIND_FN_ALL, CISTPL_VERS_1,
                               vers1)) {
+               mutex_lock(&p_dev->socket->ops_mutex);
                for (i = 0; i < min_t(unsigned int, 4, vers1->ns); i++) {
                        char *tmp;
                        unsigned int length;
+                       char *new;
 
                        tmp = vers1->str + vers1->ofs[i];
 
@@ -476,14 +461,17 @@ static int pcmcia_device_query(struct pcmcia_device *p_dev)
                        if ((length < 2) || (length > 255))
                                continue;
 
-                       p_dev->prod_id[i] = kmalloc(sizeof(char) * length,
-                                                   GFP_KERNEL);
-                       if (!p_dev->prod_id[i])
+                       new = kmalloc(sizeof(char) * length, GFP_KERNEL);
+                       if (!new)
                                continue;
 
-                       p_dev->prod_id[i] = strncpy(p_dev->prod_id[i],
-                                                   tmp, length);
+                       new = strncpy(new, tmp, length);
+
+                       tmp = p_dev->prod_id[i];
+                       p_dev->prod_id[i] = new;
+                       kfree(tmp);
                }
+               mutex_unlock(&p_dev->socket->ops_mutex);
        }
 
        kfree(vers1);
@@ -503,6 +491,7 @@ static DEFINE_MUTEX(device_add_lock);
 struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int function)
 {
        struct pcmcia_device *p_dev, *tmp_dev;
+       int i;
 
        s = pcmcia_get_socket(s);
        if (!s)
@@ -520,7 +509,11 @@ struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int fu
        p_dev->device_no = (s->device_count++);
        mutex_unlock(&s->ops_mutex);
 
-       /* max of 4 devices per card */
+       /* max of 2 PFC devices */
+       if ((p_dev->device_no >= 2) && (function == 0))
+               goto err_free;
+
+       /* max of 4 devices overall */
        if (p_dev->device_no >= 4)
                goto err_free;
 
@@ -593,6 +586,8 @@ struct pcmcia_device *pcmcia_device_add(struct pcmcia_socket *s, unsigned int fu
        s->device_count--;
        mutex_unlock(&s->ops_mutex);
 
+       for (i = 0; i < 4; i++)
+               kfree(p_dev->prod_id[i]);
        kfree(p_dev->devname);
        kfree(p_dev);
  err_put:
@@ -607,19 +602,23 @@ static int pcmcia_card_add(struct pcmcia_socket *s)
 {
        cistpl_longlink_mfc_t mfc;
        unsigned int no_funcs, i, no_chains;
-       int ret = 0;
+       int ret = -EAGAIN;
 
+       mutex_lock(&s->ops_mutex);
        if (!(s->resource_setup_done)) {
                dev_dbg(&s->dev,
                           "no resources available, delaying card_add\n");
+               mutex_unlock(&s->ops_mutex);
                return -EAGAIN; /* try again, but later... */
        }
 
        if (pcmcia_validate_mem(s)) {
                dev_dbg(&s->dev, "validating mem resources failed, "
                       "delaying card_add\n");
+               mutex_unlock(&s->ops_mutex);
                return -EAGAIN; /* try again, but later... */
        }
+       mutex_unlock(&s->ops_mutex);
 
        ret = pccard_validate_cis(s, &no_chains);
        if (ret || !no_chains) {
@@ -640,23 +639,7 @@ static int pcmcia_card_add(struct pcmcia_socket *s)
 }
 
 
-static void pcmcia_delayed_add_device(struct work_struct *work)
-{
-       struct pcmcia_socket *s =
-               container_of(work, struct pcmcia_socket, device_add);
-       u8 mfc_pfc;
-
-       mutex_lock(&s->ops_mutex);
-       mfc_pfc = s->pcmcia_state.mfc_pfc;
-       s->pcmcia_state.device_add_pending = 0;
-       s->pcmcia_state.mfc_pfc = 0;
-       mutex_unlock(&s->ops_mutex);
-
-       dev_dbg(&s->dev, "adding additional device to %d\n", s->sock);
-       pcmcia_device_add(s, mfc_pfc);
-}
-
-static int pcmcia_requery(struct device *dev, void * _data)
+static int pcmcia_requery_callback(struct device *dev, void * _data)
 {
        struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
        if (!p_dev->dev.driver) {
@@ -667,44 +650,58 @@ static int pcmcia_requery(struct device *dev, void * _data)
        return 0;
 }
 
-static void pcmcia_bus_rescan(struct pcmcia_socket *skt, int new_cis)
-{
-       int no_devices = 0;
-       int ret = 0;
-
-       /* must be called with skt_mutex held */
-       dev_dbg(&skt->dev, "re-scanning socket %d\n", skt->sock);
-
-       mutex_lock(&skt->ops_mutex);
-       if (list_empty(&skt->devices_list))
-               no_devices = 1;
-       mutex_unlock(&skt->ops_mutex);
 
-       /* If this is because of a CIS override, start over */
-       if (new_cis && !no_devices)
-               pcmcia_card_remove(skt, NULL);
+static void pcmcia_requery(struct pcmcia_socket *s)
+{
+       int has_pfc;
 
-       /* if no devices were added for this socket yet because of
-        * missing resource information or other trouble, we need to
-        * do this now. */
-       if (no_devices || new_cis) {
-               ret = pcmcia_card_add(skt);
-               if (ret)
-                       return;
+       if (s->functions == 0) {
+               pcmcia_card_add(s);
+               return;
        }
 
        /* some device information might have changed because of a CIS
         * update or because we can finally read it correctly... so
         * determine it again, overwriting old values if necessary. */
-       bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery);
+       bus_for_each_dev(&pcmcia_bus_type, NULL, NULL, pcmcia_requery_callback);
+
+       /* if the CIS changed, we need to check whether the number of
+        * functions changed. */
+       if (s->fake_cis) {
+               int old_funcs, new_funcs;
+               cistpl_longlink_mfc_t mfc;
+
+               /* does this cis override add or remove functions? */
+               old_funcs = s->functions;
+
+               if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
+                                       &mfc))
+                       new_funcs = mfc.nfn;
+               else
+                       new_funcs = 1;
+               if (old_funcs != new_funcs) {
+                       /* we need to re-start */
+                       pcmcia_card_remove(s, NULL);
+                       pcmcia_card_add(s);
+               }
+       }
+
+       /* If the PCMCIA device consists of two pseudo devices,
+        * call pcmcia_device_add() -- which will fail if both
+        * devices are already registered. */
+       mutex_lock(&s->ops_mutex);
+       has_pfc = s->pcmcia_state.has_pfc;
+       mutex_unlock(&s->ops_mutex);
+       if (has_pfc)
+               pcmcia_device_add(s, 0);
 
        /* we re-scan all devices, not just the ones connected to this
         * socket. This does not matter, though. */
-       ret = bus_rescan_devices(&pcmcia_bus_type);
-       if (ret)
-               printk(KERN_INFO "pcmcia: bus_rescan_devices failed\n");
+       if (bus_rescan_devices(&pcmcia_bus_type))
+               dev_warn(&s->dev, "rescanning the bus failed\n");
 }
 
+
 #ifdef CONFIG_PCMCIA_LOAD_CIS
 
 /**
@@ -721,9 +718,8 @@ static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
        struct pcmcia_socket *s = dev->socket;
        const struct firmware *fw;
        int ret = -ENOMEM;
-       int no_funcs;
-       int old_funcs;
        cistpl_longlink_mfc_t mfc;
+       int old_funcs, new_funcs = 1;
 
        if (!filename)
                return -EINVAL;
@@ -746,26 +742,20 @@ static int pcmcia_load_firmware(struct pcmcia_device *dev, char * filename)
                        goto release;
                }
 
+               /* we need to re-start if the number of functions changed */
+               old_funcs = s->functions;
+               if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC,
+                                       &mfc))
+                       new_funcs = mfc.nfn;
+
+               if (old_funcs != new_funcs)
+                       ret = -EBUSY;
 
                /* update information */
                pcmcia_device_query(dev);
 
-               /* does this cis override add or remove functions? */
-               old_funcs = s->functions;
-
-               if (!pccard_read_tuple(s, BIND_FN_ALL, CISTPL_LONGLINK_MFC, &mfc))
-                       no_funcs = mfc.nfn;
-               else
-                       no_funcs = 1;
-               s->functions = no_funcs;
-
-               if (old_funcs > no_funcs)
-                       pcmcia_card_remove(s, dev);
-               else if (no_funcs > old_funcs) {
-                       mutex_lock(&s->ops_mutex);
-                       pcmcia_add_device_later(s, 1);
-                       mutex_unlock(&s->ops_mutex);
-               }
+               /* requery (as number of functions might have changed) */
+               pcmcia_parse_uevents(s, PCMCIA_UEVENT_REQUERY);
        }
  release:
        release_firmware(fw);
@@ -830,6 +820,10 @@ static inline int pcmcia_devmatch(struct pcmcia_device *dev,
        }
 
        if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO) {
+               dev_dbg(&dev->dev, "this is a pseudo-multi-function device\n");
+               mutex_lock(&dev->socket->ops_mutex);
+               dev->socket->pcmcia_state.has_pfc = 1;
+               mutex_unlock(&dev->socket->ops_mutex);
                if (dev->device_no != did->device_no)
                        return 0;
        }
@@ -842,7 +836,7 @@ static inline int pcmcia_devmatch(struct pcmcia_device *dev,
 
                /* if this is a pseudo-multi-function device,
                 * we need explicit matches */
-               if (did->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO)
+               if (dev->socket->pcmcia_state.has_pfc)
                        return 0;
                if (dev->device_no)
                        return 0;
@@ -865,10 +859,8 @@ static inline int pcmcia_devmatch(struct pcmcia_device *dev,
        if (did->match_flags & PCMCIA_DEV_ID_MATCH_FAKE_CIS) {
                dev_dbg(&dev->dev, "device needs a fake CIS\n");
                if (!dev->socket->fake_cis)
-                       pcmcia_load_firmware(dev, did->cisfile);
-
-               if (!dev->socket->fake_cis)
-                       return 0;
+                       if (pcmcia_load_firmware(dev, did->cisfile))
+                               return 0;
        }
 
        if (did->match_flags & PCMCIA_DEV_ID_MATCH_ANONYMOUS) {
@@ -880,8 +872,6 @@ static inline int pcmcia_devmatch(struct pcmcia_device *dev,
                        return 0;
        }
 
-       dev_set_drvdata(&dev->dev, did);
-
        return 1;
 }
 
@@ -985,9 +975,9 @@ static int runtime_suspend(struct device *dev)
 {
        int rc;
 
-       down(&dev->sem);
+       device_lock(dev);
        rc = pcmcia_dev_suspend(dev, PMSG_SUSPEND);
-       up(&dev->sem);
+       device_unlock(dev);
        return rc;
 }
 
@@ -995,9 +985,9 @@ static int runtime_resume(struct device *dev)
 {
        int rc;
 
-       down(&dev->sem);
+       device_lock(dev);
        rc = pcmcia_dev_resume(dev);
-       up(&dev->sem);
+       device_unlock(dev);
        return rc;
 }
 
@@ -1081,7 +1071,6 @@ static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t count)
 {
        struct pcmcia_device *p_dev = to_pcmcia_dev(dev);
-       int ret;
 
        if (!count)
                return -EINVAL;
@@ -1089,11 +1078,7 @@ static ssize_t pcmcia_store_allow_func_id_match(struct device *dev,
        mutex_lock(&p_dev->socket->ops_mutex);
        p_dev->allow_func_id_match = 1;
        mutex_unlock(&p_dev->socket->ops_mutex);
-
-       ret = bus_rescan_devices(&pcmcia_bus_type);
-       if (ret)
-               printk(KERN_INFO "pcmcia: bus_rescan_devices failed after "
-                      "allowing func_id matches\n");
+       pcmcia_parse_uevents(p_dev->socket, PCMCIA_UEVENT_REQUERY);
 
        return count;
 }
@@ -1268,9 +1253,7 @@ static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
 
        switch (event) {
        case CS_EVENT_CARD_REMOVAL:
-               mutex_lock(&s->ops_mutex);
-               s->pcmcia_state.present = 0;
-               mutex_unlock(&s->ops_mutex);
+               atomic_set(&skt->present, 0);
                pcmcia_card_remove(skt, NULL);
                handle_event(skt, event);
                mutex_lock(&s->ops_mutex);
@@ -1279,8 +1262,9 @@ static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
                break;
 
        case CS_EVENT_CARD_INSERTION:
+               atomic_set(&skt->present, 1);
                mutex_lock(&s->ops_mutex);
-               s->pcmcia_state.present = 1;
+               s->pcmcia_state.has_pfc = 0;
                destroy_cis_cache(s); /* to be on the safe side... */
                mutex_unlock(&s->ops_mutex);
                pcmcia_card_add(skt);
@@ -1320,7 +1304,13 @@ static int ds_event(struct pcmcia_socket *skt, event_t event, int priority)
     return 0;
 } /* ds_event */
 
-
+/*
+ * NOTE: This is racy. There's no guarantee the card will still be
+ * physically present, even if the call to this function returns
+ * non-NULL. Furthermore, the device driver most likely is unbound
+ * almost immediately, so the timeframe where pcmcia_dev_present
+ * returns NULL is probably really really small.
+ */
 struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
 {
        struct pcmcia_device *p_dev;
@@ -1330,22 +1320,9 @@ struct pcmcia_device *pcmcia_dev_present(struct pcmcia_device *_p_dev)
        if (!p_dev)
                return NULL;
 
-       mutex_lock(&p_dev->socket->ops_mutex);
-       if (!p_dev->socket->pcmcia_state.present)
-               goto out;
-
-       if (p_dev->socket->pcmcia_state.dead)
-               goto out;
-
-       if (p_dev->_removed)
-               goto out;
-
-       if (p_dev->suspended)
-               goto out;
+       if (atomic_read(&p_dev->socket->present) != 0)
+               ret = p_dev;
 
-       ret = p_dev;
- out:
-       mutex_unlock(&p_dev->socket->ops_mutex);
        pcmcia_put_dev(p_dev);
        return ret;
 }
@@ -1355,7 +1332,7 @@ EXPORT_SYMBOL(pcmcia_dev_present);
 static struct pcmcia_callback pcmcia_bus_callback = {
        .owner = THIS_MODULE,
        .event = ds_event,
-       .requery = pcmcia_bus_rescan,
+       .requery = pcmcia_requery,
        .validate = pccard_validate_cis,
        .suspend = pcmcia_bus_suspend,
        .resume = pcmcia_bus_resume,
@@ -1374,12 +1351,6 @@ static int __devinit pcmcia_bus_add_socket(struct device *dev,
                return -ENODEV;
        }
 
-       /*
-        * Ugly. But we want to wait for the socket threads to have started up.
-        * We really should let the drivers themselves drive some of this..
-        */
-       msleep(250);
-
        ret = sysfs_create_bin_file(&dev->kobj, &pccard_cis_attr);
        if (ret) {
                dev_printk(KERN_ERR, dev, "PCMCIA registration failed\n");
@@ -1391,7 +1362,6 @@ static int __devinit pcmcia_bus_add_socket(struct device *dev,
        init_waitqueue_head(&socket->queue);
 #endif
        INIT_LIST_HEAD(&socket->devices_list);
-       INIT_WORK(&socket->device_add, pcmcia_delayed_add_device);
        memset(&socket->pcmcia_state, 0, sizeof(u8));
        socket->device_count = 0;
 
@@ -1402,6 +1372,8 @@ static int __devinit pcmcia_bus_add_socket(struct device *dev,
                return ret;
        }
 
+       atomic_set(&socket->present, 0);
+
        return 0;
 }
 
@@ -1413,10 +1385,6 @@ static void pcmcia_bus_remove_socket(struct device *dev,
        if (!socket)
                return;
 
-       mutex_lock(&socket->ops_mutex);
-       socket->pcmcia_state.dead = 1;
-       mutex_unlock(&socket->ops_mutex);
-
        pccard_register_pcmcia(socket, NULL);
 
        /* unregister any unbound devices */