pcmcia: pcmcia_config_loop() ConfigIndex unification
[linux-2.6.git] / drivers / net / wireless / orinoco_cs.c
index 405b7ba..67a172d 100644 (file)
@@ -13,7 +13,6 @@
 #define DRIVER_NAME "orinoco_cs"
 #define PFX DRIVER_NAME ": "
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -63,7 +62,7 @@ struct orinoco_pccard {
 /* Function prototypes                                             */
 /********************************************************************/
 
-static void orinoco_cs_config(struct pcmcia_device *link);
+static int orinoco_cs_config(struct pcmcia_device *link);
 static void orinoco_cs_release(struct pcmcia_device *link);
 static void orinoco_cs_detach(struct pcmcia_device *p_dev);
 
@@ -104,7 +103,7 @@ orinoco_cs_hard_reset(struct orinoco_private *priv)
  * configure the card at this point -- we wait until we receive a card
  * insertion event.  */
 static int
-orinoco_cs_attach(struct pcmcia_device *link)
+orinoco_cs_probe(struct pcmcia_device *link)
 {
        struct net_device *dev;
        struct orinoco_private *priv;
@@ -134,10 +133,7 @@ orinoco_cs_attach(struct pcmcia_device *link)
        link->conf.Attributes = 0;
        link->conf.IntType = INT_MEMORY_AND_IO;
 
-       link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
-       orinoco_cs_config(link);
-
-       return 0;
+       return orinoco_cs_config(link);
 }                              /* orinoco_cs_attach */
 
 /*
@@ -150,15 +146,11 @@ static void orinoco_cs_detach(struct pcmcia_device *link)
 {
        struct net_device *dev = link->priv;
 
-       if (link->state & DEV_CONFIG)
-               orinoco_cs_release(link);
-
-       DEBUG(0, PFX "detach: link=%p link->dev_node=%p\n", link, link->dev_node);
-       if (link->dev_node) {
-               DEBUG(0, PFX "About to unregister net device %p\n",
-                     dev);
+       if (link->dev_node)
                unregister_netdev(dev);
-       }
+
+       orinoco_cs_release(link);
+
        free_orinocodev(dev);
 }                              /* orinoco_cs_detach */
 
@@ -172,44 +164,95 @@ static void orinoco_cs_detach(struct pcmcia_device *link)
                last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; \
        } while (0)
 
-static void
+struct orinoco_cs_config_data {
+       cistpl_cftable_entry_t dflt;
+       config_info_t conf;
+};
+
+static int orinoco_cs_config_check(struct pcmcia_device *p_dev,
+                                  cistpl_cftable_entry_t *cfg,
+                                  void *priv_data)
+{
+       struct orinoco_cs_config_data *cfg_mem = priv_data;
+
+       if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
+               cfg_mem->dflt = *cfg;
+       if (cfg->index == 0)
+               goto next_entry;
+
+       /* Use power settings for Vcc and Vpp if present */
+       /* Note that the CIS values need to be rescaled */
+       if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
+               if (cfg_mem->conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
+                       DEBUG(2, "spectrum_cs_config: Vcc mismatch (cfg_mem->conf.Vcc = %d, CIS = %d)\n",  cfg_mem->conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
+                       if (!ignore_cis_vcc)
+                               goto next_entry;
+               }
+       } else if (cfg_mem->dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
+               if (cfg_mem->conf.Vcc != cfg_mem->dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
+                       DEBUG(2, "spectrum_cs_config: Vcc mismatch (cfg_mem->conf.Vcc = %d, CIS = %d)\n",  cfg_mem->conf.Vcc, cfg_mem->dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
+                       if (!ignore_cis_vcc)
+                               goto next_entry;
+               }
+       }
+
+       if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
+               p_dev->conf.Vpp =
+                       cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
+       else if (cfg_mem->dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
+               p_dev->conf.Vpp =
+                       cfg_mem->dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
+
+       /* Do we need to allocate an interrupt? */
+       p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
+
+       /* IO window settings */
+       p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
+       if ((cfg->io.nwin > 0) || (cfg_mem->dflt.io.nwin > 0)) {
+               cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &cfg_mem->dflt.io;
+               p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
+               if (!(io->flags & CISTPL_IO_8BIT))
+                       p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
+               if (!(io->flags & CISTPL_IO_16BIT))
+                       p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
+               p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
+               p_dev->io.BasePort1 = io->win[0].base;
+               p_dev->io.NumPorts1 = io->win[0].len;
+               if (io->nwin > 1) {
+                       p_dev->io.Attributes2 = p_dev->io.Attributes1;
+                       p_dev->io.BasePort2 = io->win[1].base;
+                       p_dev->io.NumPorts2 = io->win[1].len;
+               }
+
+               /* This reserves IO space but doesn't actually enable it */
+               if (pcmcia_request_io(p_dev, &p_dev->io) != 0)
+                       goto next_entry;
+       }
+       return 0;
+
+next_entry:
+       pcmcia_disable_device(p_dev);
+       return -ENODEV;
+};
+
+static int
 orinoco_cs_config(struct pcmcia_device *link)
 {
+       struct orinoco_cs_config_data *cfg_mem;
        struct net_device *dev = link->priv;
        struct orinoco_private *priv = netdev_priv(dev);
        struct orinoco_pccard *card = priv->card;
        hermes_t *hw = &priv->hw;
        int last_fn, last_ret;
-       u_char buf[64];
-       config_info_t conf;
-       cisinfo_t info;
-       tuple_t tuple;
-       cisparse_t parse;
        void __iomem *mem;
 
-       CS_CHECK(ValidateCIS, pcmcia_validate_cis(link, &info));
-
-       /*
-        * This reads the card's CONFIG tuple to find its
-        * configuration registers.
-        */
-       tuple.DesiredTuple = CISTPL_CONFIG;
-       tuple.Attributes = 0;
-       tuple.TupleData = buf;
-       tuple.TupleDataMax = sizeof(buf);
-       tuple.TupleOffset = 0;
-       CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
-       CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
-       CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
-       link->conf.ConfigBase = parse.config.base;
-       link->conf.Present = parse.config.rmask[0];
-
-       /* Configure card */
-       link->state |= DEV_CONFIG;
+       cfg_mem = kzalloc(sizeof(struct orinoco_cs_config_data), GFP_KERNEL);
+       if (!cfg_mem)
+               return -ENOMEM;
 
        /* Look up the current Vcc */
        CS_CHECK(GetConfigurationInfo,
-                pcmcia_get_configuration_info(link, &conf));
+                pcmcia_get_configuration_info(link, &cfg_mem->conf));
 
        /*
         * In this loop, we scan the CIS for configuration table
@@ -225,96 +268,14 @@ orinoco_cs_config(struct pcmcia_device *link)
         * and most client drivers will only use the CIS to fill in
         * implementation-defined details.
         */
-       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
-       CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
-       while (1) {
-               cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
-               cistpl_cftable_entry_t dflt = { .index = 0 };
-
-               if ( (pcmcia_get_tuple_data(link, &tuple) != 0)
-                   || (pcmcia_parse_tuple(link, &tuple, &parse) != 0))
-                       goto next_entry;
-
-               if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
-                       dflt = *cfg;
-               if (cfg->index == 0)
-                       goto next_entry;
-               link->conf.ConfigIndex = cfg->index;
-
-               /* Does this card need audio output? */
-               if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
-                       link->conf.Attributes |= CONF_ENABLE_SPKR;
-                       link->conf.Status = CCSR_AUDIO_ENA;
-               }
-
-               /* Use power settings for Vcc and Vpp if present */
-               /* Note that the CIS values need to be rescaled */
-               if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
-                       if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
-                               DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, cfg CIS = %d)\n",  conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
-                               if (!ignore_cis_vcc)
-                                       goto next_entry;
-                       }
-               } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
-                       if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
-                               DEBUG(2, "orinoco_cs_config: Vcc mismatch (conf.Vcc = %d, dflt CIS = %d)\n",  conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
-                               if(!ignore_cis_vcc)
-                                       goto next_entry;
-                       }
-               }
-
-               if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
-                       link->conf.Vpp =
-                           cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
-               else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
-                       link->conf.Vpp =
-                           dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
-               
-               /* Do we need to allocate an interrupt? */
-               link->conf.Attributes |= CONF_ENABLE_IRQ;
-
-               /* IO window settings */
-               link->io.NumPorts1 = link->io.NumPorts2 = 0;
-               if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
-                       cistpl_io_t *io =
-                           (cfg->io.nwin) ? &cfg->io : &dflt.io;
-                       link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
-                       if (!(io->flags & CISTPL_IO_8BIT))
-                               link->io.Attributes1 =
-                                   IO_DATA_PATH_WIDTH_16;
-                       if (!(io->flags & CISTPL_IO_16BIT))
-                               link->io.Attributes1 =
-                                   IO_DATA_PATH_WIDTH_8;
-                       link->io.IOAddrLines =
-                           io->flags & CISTPL_IO_LINES_MASK;
-                       link->io.BasePort1 = io->win[0].base;
-                       link->io.NumPorts1 = io->win[0].len;
-                       if (io->nwin > 1) {
-                               link->io.Attributes2 =
-                                   link->io.Attributes1;
-                               link->io.BasePort2 = io->win[1].base;
-                               link->io.NumPorts2 = io->win[1].len;
-                       }
-
-                       /* This reserves IO space but doesn't actually enable it */
-                       if (pcmcia_request_io(link, &link->io) != 0)
-                               goto next_entry;
-               }
-
-
-               /* If we got this far, we're cool! */
-
-               break;
-               
-       next_entry:
-               pcmcia_disable_device(link);
-               last_ret = pcmcia_get_next_tuple(link, &tuple);
-               if (last_ret  == CS_NO_MORE_ITEMS) {
+       last_ret = pcmcia_loop_config(link, orinoco_cs_config_check, cfg_mem);
+       if (last_ret) {
+               if (!ignore_cis_vcc)
                        printk(KERN_ERR PFX "GetNextTuple(): No matching "
                               "CIS configuration.  Maybe you need the "
                               "ignore_cis_vcc=1 parameter.\n");
-                       goto cs_failed;
-               }
+               cs_error(link, RequestIO, last_ret);
+               goto failed;
        }
 
        /*
@@ -344,7 +305,6 @@ orinoco_cs_config(struct pcmcia_device *link)
        /* Ok, we have the configuration, prepare to register the netdev */
        dev->base_addr = link->io.BasePort1;
        dev->irq = link->irq.AssignedIRQ;
-       SET_MODULE_OWNER(dev);
        card->node.major = card->node.minor = 0;
 
        SET_NETDEV_DEV(dev, &handle_to_dev(link));
@@ -360,30 +320,22 @@ orinoco_cs_config(struct pcmcia_device *link)
        link->dev_node = &card->node; /* link->dev_node being non-NULL is also
                                     used to indicate that the
                                     net_device has been registered */
-       link->state &= ~DEV_CONFIG_PENDING;
 
        /* Finally, report what we've done */
-       printk(KERN_DEBUG "%s: index 0x%02x: ",
-              dev->name, link->conf.ConfigIndex);
-       if (link->conf.Vpp)
-               printk(", Vpp %d.%d", link->conf.Vpp / 10,
-                      link->conf.Vpp % 10);
-       printk(", irq %d", link->irq.AssignedIRQ);
-       if (link->io.NumPorts1)
-               printk(", io 0x%04x-0x%04x", link->io.BasePort1,
-                      link->io.BasePort1 + link->io.NumPorts1 - 1);
-       if (link->io.NumPorts2)
-               printk(" & 0x%04x-0x%04x", link->io.BasePort2,
-                      link->io.BasePort2 + link->io.NumPorts2 - 1);
-       printk("\n");
-
-       return;
+       printk(KERN_DEBUG "%s: " DRIVER_NAME " at %s, irq %d, io "
+              "0x%04x-0x%04x\n", dev->name, dev->dev.parent->bus_id,
+              link->irq.AssignedIRQ, link->io.BasePort1,
+              link->io.BasePort1 + link->io.NumPorts1 - 1);
+       kfree(cfg_mem);
+       return 0;
 
  cs_failed:
        cs_error(link, last_fn, last_ret);
 
  failed:
        orinoco_cs_release(link);
+       kfree(cfg_mem);
+       return -ENODEV;
 }                              /* orinoco_cs_config */
 
 /*
@@ -417,23 +369,21 @@ static int orinoco_cs_suspend(struct pcmcia_device *link)
        int err = 0;
        unsigned long flags;
 
-       if (link->state & DEV_CONFIG) {
-               /* This is probably racy, but I can't think of
-                  a better way, short of rewriting the PCMCIA
-                  layer to not suck :-( */
-               if (! test_bit(0, &card->hard_reset_in_progress)) {
-                       spin_lock_irqsave(&priv->lock, flags);
+       /* This is probably racy, but I can't think of
+          a better way, short of rewriting the PCMCIA
+          layer to not suck :-( */
+       if (! test_bit(0, &card->hard_reset_in_progress)) {
+               spin_lock_irqsave(&priv->lock, flags);
 
-                       err = __orinoco_down(dev);
-                       if (err)
-                               printk(KERN_WARNING "%s: Error %d downing interface\n",
-                                      dev->name, err);
+               err = __orinoco_down(dev);
+               if (err)
+                       printk(KERN_WARNING "%s: Error %d downing interface\n",
+                              dev->name, err);
 
-                       netif_device_detach(dev);
-                       priv->hw_unavailable++;
+               netif_device_detach(dev);
+               priv->hw_unavailable++;
 
-                       spin_unlock_irqrestore(&priv->lock, flags);
-               }
+               spin_unlock_irqrestore(&priv->lock, flags);
        }
 
        return 0;
@@ -445,34 +395,31 @@ static int orinoco_cs_resume(struct pcmcia_device *link)
        struct orinoco_private *priv = netdev_priv(dev);
        struct orinoco_pccard *card = priv->card;
        int err = 0;
-       unsigned long flags;
 
-       if (link->state & DEV_CONFIG) {
-               if (! test_bit(0, &card->hard_reset_in_progress)) {
-                       err = orinoco_reinit_firmware(dev);
-                       if (err) {
-                               printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
-                                      dev->name, err);
-                               return -EIO;
-                       }
-
-                       spin_lock_irqsave(&priv->lock, flags);
+       if (! test_bit(0, &card->hard_reset_in_progress)) {
+               err = orinoco_reinit_firmware(dev);
+               if (err) {
+                       printk(KERN_ERR "%s: Error %d re-initializing firmware\n",
+                              dev->name, err);
+                       return -EIO;
+               }
 
-                       netif_device_attach(dev);
-                       priv->hw_unavailable--;
+               spin_lock(&priv->lock);
 
-                       if (priv->open && ! priv->hw_unavailable) {
-                               err = __orinoco_up(dev);
-                               if (err)
-                                       printk(KERN_ERR "%s: Error %d restarting card\n",
-                                              dev->name, err);
-                       }
+               netif_device_attach(dev);
+               priv->hw_unavailable--;
 
-                       spin_unlock_irqrestore(&priv->lock, flags);
+               if (priv->open && ! priv->hw_unavailable) {
+                       err = __orinoco_up(dev);
+                       if (err)
+                               printk(KERN_ERR "%s: Error %d restarting card\n",
+                                      dev->name, err);
                }
+
+               spin_unlock(&priv->lock);
        }
 
-       return 0;
+       return err;
 }
 
 
@@ -507,6 +454,7 @@ static struct pcmcia_device_id orinoco_cs_ids[] = {
        PCMCIA_DEVICE_MANF_CARD(0x028a, 0x0673), /* Linksys WCF12 Wireless CompactFlash Card */
        PCMCIA_DEVICE_MANF_CARD(0x02aa, 0x0002), /* ASUS SpaceLink WL-100 */
        PCMCIA_DEVICE_MANF_CARD(0x02ac, 0x0002), /* SpeedStream SS1021 Wireless Adapter */
+       PCMCIA_DEVICE_MANF_CARD(0x02ac, 0x3021), /* SpeedStream Wireless Adapter */
        PCMCIA_DEVICE_MANF_CARD(0x14ea, 0xb001), /* PLANEX RoadLannerWave GW-NS11H */
        PCMCIA_DEVICE_MANF_CARD(0x50c2, 0x7300), /* Airvast WN-100 */
        PCMCIA_DEVICE_MANF_CARD(0x9005, 0x0021), /* Adaptec Ultra Wireless ANW-8030 */
@@ -576,7 +524,7 @@ static struct pcmcia_driver orinoco_driver = {
        .drv            = {
                .name   = DRIVER_NAME,
        },
-       .probe          = orinoco_cs_attach,
+       .probe          = orinoco_cs_probe,
        .remove         = orinoco_cs_detach,
        .id_table       = orinoco_cs_ids,
        .suspend        = orinoco_cs_suspend,