]> git.karo-electronics.de Git - linux-beck.git/blobdiff - drivers/staging/comedi/drivers/amplc_pci224.c
Merge branch 'slab/common-for-cgroups' into slab/for-linus
[linux-beck.git] / drivers / staging / comedi / drivers / amplc_pci224.c
index b87e10ddf239b61af6ea8df7f5507aa34cc33fb3..8bf109e7bb05cef4c9c50669314289cde734c47c 100644 (file)
@@ -378,7 +378,6 @@ static const struct pci224_board pci224_boards[] = {
    several hardware drivers keep similar information in this structure,
    feel free to suggest moving the variable to the struct comedi_device struct.  */
 struct pci224_private {
-       struct pci_dev *pci_dev;        /* PCI device */
        const unsigned short *hwrange;
        unsigned long iobase1;
        unsigned long state;
@@ -1256,56 +1255,48 @@ static const struct pci224_board
  * This function looks for a PCI device matching the requested board name,
  * bus and slot.
  */
-static struct pci_dev *
-pci224_find_pci(struct comedi_device *dev, int bus, int slot)
+static struct pci_dev *pci224_find_pci_dev(struct comedi_device *dev,
+                                          struct comedi_devconfig *it)
 {
        const struct pci224_board *thisboard = comedi_board(dev);
        struct pci_dev *pci_dev = NULL;
+       int bus = it->options[0];
+       int slot = it->options[1];
 
-       /* Look for matching PCI device. */
-       for (pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON, PCI_ANY_ID, NULL);
-            pci_dev != NULL;
-            pci_dev = pci_get_device(PCI_VENDOR_ID_AMPLICON, PCI_ANY_ID,
-                                     pci_dev)) {
-               /* If bus/slot specified, check them. */
+       for_each_pci_dev(pci_dev) {
                if (bus || slot) {
-                       if (bus != pci_dev->bus->number
-                           || slot != PCI_SLOT(pci_dev->devfn))
+                       if (bus != pci_dev->bus->number ||
+                           slot != PCI_SLOT(pci_dev->devfn))
                                continue;
                }
+               if (pci_dev->vendor != PCI_VENDOR_ID_AMPLICON)
+                       continue;
+
                if (thisboard->model == any_model) {
                        /* Match any supported model. */
                        const struct pci224_board *board_ptr;
+
                        board_ptr = pci224_find_pci_board(pci_dev);
                        if (board_ptr == NULL)
                                continue;
                        /* Change board_ptr to matched board. */
                        dev->board_ptr = board_ptr;
-                       thisboard = comedi_board(dev);
                } else {
                        /* Match specific model name. */
                        if (thisboard->devid != pci_dev->device)
                                continue;
                }
-
-               /* Found a match. */
                return pci_dev;
        }
-       /* No match found. */
-       if (bus || slot) {
-               dev_err(dev->class_dev,
-                       "error! no %s found at pci %02x:%02x!\n",
-                       thisboard->name, bus, slot);
-       } else {
-               dev_err(dev->class_dev, "error! no %s found!\n",
-                       thisboard->name);
-       }
+       dev_err(dev->class_dev,
+               "No supported board found! (req. bus %d, slot %d)\n",
+               bus, slot);
        return NULL;
 }
 
 static void pci224_report_attach(struct comedi_device *dev, unsigned int irq)
 {
-       struct pci224_private *devpriv = dev->private;
+       struct pci_dev *pcidev = comedi_to_pci_dev(dev);
        char tmpbuf[30];
 
        if (irq)
@@ -1314,7 +1305,7 @@ static void pci224_report_attach(struct comedi_device *dev, unsigned int irq)
        else
                snprintf(tmpbuf, sizeof(tmpbuf), "no irq");
        dev_info(dev->class_dev, "%s (pci %s) (%s) attached\n",
-                dev->board_name, pci_name(devpriv->pci_dev), tmpbuf);
+                dev->board_name, pci_name(pcidev), tmpbuf);
 }
 
 /*
@@ -1330,7 +1321,8 @@ static int pci224_attach_common(struct comedi_device *dev,
        unsigned n;
        int ret;
 
-       devpriv->pci_dev = pci_dev;
+       comedi_set_hw_dev(dev, &pci_dev->dev);
+
        ret = comedi_pci_enable(pci_dev, DRIVER_NAME);
        if (ret < 0) {
                dev_err(dev->class_dev,
@@ -1474,21 +1466,18 @@ static int pci224_attach_common(struct comedi_device *dev,
 static int pci224_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 {
        struct pci_dev *pci_dev;
-       int bus, slot;
        int ret;
 
        dev_info(dev->class_dev, DRIVER_NAME ": attach\n");
 
-       bus = it->options[0];
-       slot = it->options[1];
        ret = alloc_private(dev, sizeof(struct pci224_private));
        if (ret < 0) {
                dev_err(dev->class_dev, "error! out of memory!\n");
                return ret;
        }
 
-       pci_dev = pci224_find_pci(dev, bus, slot);
-       if (pci_dev == NULL)
+       pci_dev = pci224_find_pci_dev(dev, it);
+       if (!pci_dev)
                return -EIO;
 
        return pci224_attach_common(dev, pci_dev, it->options);
@@ -1514,12 +1503,20 @@ pci224_attach_pci(struct comedi_device *dev, struct pci_dev *pci_dev)
                        DRIVER_NAME ": BUG! cannot determine board type!\n");
                return -EINVAL;
        }
+       /*
+        * Need to 'get' the PCI device to match the 'put' in pci224_detach().
+        * TODO: Remove the pci_dev_get() and matching pci_dev_put() once
+        * support for manual attachment of PCI devices via pci224_attach()
+        * has been removed.
+        */
+       pci_dev_get(pci_dev);
        return pci224_attach_common(dev, pci_dev, NULL);
 }
 
 static void pci224_detach(struct comedi_device *dev)
 {
        struct pci224_private *devpriv = dev->private;
+       struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 
        if (dev->irq)
                free_irq(dev->irq, dev);
@@ -1534,11 +1531,11 @@ static void pci224_detach(struct comedi_device *dev)
                kfree(devpriv->ao_readback);
                kfree(devpriv->ao_scan_vals);
                kfree(devpriv->ao_scan_order);
-               if (devpriv->pci_dev) {
-                       if (dev->iobase)
-                               comedi_pci_disable(devpriv->pci_dev);
-                       pci_dev_put(devpriv->pci_dev);
-               }
+       }
+       if (pcidev) {
+               if (dev->iobase)
+                       comedi_pci_disable(pcidev);
+               pci_dev_put(pcidev);
        }
 }