linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 65/90] staging: comedi: dt3000: cleanup "find pci device" code
@ 2012-07-19  1:57 H Hartley Sweeten
  2012-07-19 11:46 ` Ian Abbott
  0 siblings, 1 reply; 2+ messages in thread
From: H Hartley Sweeten @ 2012-07-19  1:57 UTC (permalink / raw)
  To: Linux Kernel; +Cc: devel, abbotti, gregkh

The "find pci device" code for this driver was split between
two functions which could cause the driver to walk the pci
bus multiple times while looking for a match.

Consolidate the functions into the format that is more
standard for the comedi pci drivers.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Cc: Ian Abbott <abbotti@mev.co.uk>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/staging/comedi/drivers/dt3000.c | 85 ++++++++++++---------------------
 1 file changed, 31 insertions(+), 54 deletions(-)

diff --git a/drivers/staging/comedi/drivers/dt3000.c b/drivers/staging/comedi/drivers/dt3000.c
index d1d99a3..10b43a2 100644
--- a/drivers/staging/comedi/drivers/dt3000.c
+++ b/drivers/staging/comedi/drivers/dt3000.c
@@ -159,7 +159,6 @@ static const struct dt3k_boardtype dt3k_boardtypes[] = {
 	 },
 };
 
-#define n_dt3k_boards sizeof(dt3k_boardtypes)/sizeof(struct dt3k_boardtype)
 #define this_board ((const struct dt3k_boardtype *)dev->board_ptr)
 
 #define DT3000_SIZE		(4*0x1000)
@@ -797,87 +796,65 @@ static int setup_pci(struct comedi_device *dev)
 	return 0;
 }
 
-static struct pci_dev *dt_pci_find_device(struct pci_dev *from, int *board)
+static struct pci_dev *dt3000_find_pci_dev(struct comedi_device *dev,
+					   struct comedi_devconfig *it)
 {
+	struct pci_dev *pcidev = NULL;
+	int bus = it->options[0];
+	int slot = it->options[1];
 	int i;
 
-	for (from = pci_get_device(PCI_VENDOR_ID_DT, PCI_ANY_ID, from);
-	     from != NULL;
-	     from = pci_get_device(PCI_VENDOR_ID_DT, PCI_ANY_ID, from)) {
-		for (i = 0; i < n_dt3k_boards; i++) {
-			if (from->device == dt3k_boardtypes[i].device_id) {
-				*board = i;
-				return from;
-			}
+	for_each_pci_dev(pcidev) {
+		if (bus || slot) {
+			if (bus != pcidev->bus->number ||
+			    slot != PCI_SLOT(pcidev->devfn))
+				continue;
 		}
-		printk
-		    ("unknown Data Translation PCI device found with device_id=0x%04x\n",
-		     from->device);
-	}
-	*board = -1;
-	return from;
-}
-
-static int dt_pci_probe(struct comedi_device *dev, int bus, int slot)
-{
-	int board;
-	int ret;
-	struct pci_dev *pcidev;
-
-	pcidev = NULL;
-	while ((pcidev = dt_pci_find_device(pcidev, &board)) != NULL) {
-		if ((bus == 0 && slot == 0) ||
-		    (pcidev->bus->number == bus &&
-		     PCI_SLOT(pcidev->devfn) == slot)) {
-			break;
+		if (pcidev->vendor != PCI_VENDOR_ID_DT)
+			continue;
+		for (i = 0; i < ARRAY_SIZE(dt3k_boardtypes); i++) {
+			if (dt3k_boardtypes[i].device_id != pcidev->device)
+				continue;
+			dev->board_ptr = dt3k_boardtypes + i;
+			return pcidev;
 		}
 	}
-	devpriv->pci_dev = pcidev;
-
-	if (board >= 0)
-		dev->board_ptr = dt3k_boardtypes + board;
-
-	if (!devpriv->pci_dev)
-		return 0;
-
-	ret = setup_pci(dev);
-	if (ret < 0)
-		return ret;
-
-	return 1;
+	dev_err(dev->class_dev,
+		"No supported board found! (req. bus %d, slot %d)\n",
+		bus, slot);
+	return NULL;
 }
 
 static int dt3000_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 {
+	struct pci_dev *pcidev;
 	struct comedi_subdevice *s;
-	int bus, slot;
 	int ret = 0;
 
 	dev_dbg(dev->class_dev, "dt3000:\n");
-	bus = it->options[0];
-	slot = it->options[1];
 
 	ret = alloc_private(dev, sizeof(struct dt3k_private));
 	if (ret < 0)
 		return ret;
 
-	ret = dt_pci_probe(dev, bus, slot);
+	pcidev = dt3000_find_pci_dev(dev, it);
+	if (!pcidev)
+		return -EIO;
+	devpriv->pci_dev = pcidev;
+
+	ret = setup_pci(dev);
 	if (ret < 0)
 		return ret;
-	if (ret == 0) {
-		dev_warn(dev->class_dev, "no DT board found\n");
-		return -ENODEV;
-	}
 
 	dev->board_name = this_board->name;
 
-	if (request_irq(devpriv->pci_dev->irq, dt3k_interrupt, IRQF_SHARED,
+	if (request_irq(pcidev->irq, dt3k_interrupt, IRQF_SHARED,
 			"dt3000", dev)) {
 		dev_err(dev->class_dev, "unable to allocate IRQ %u\n",
-			devpriv->pci_dev->irq);
+			pcidev->irq);
 		return -EINVAL;
 	}
-	dev->irq = devpriv->pci_dev->irq;
+	dev->irq = pcidev->irq;
 
 	ret = comedi_alloc_subdevices(dev, 4);
 	if (ret)
-- 
1.7.11


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 65/90] staging: comedi: dt3000: cleanup "find pci device" code
  2012-07-19  1:57 [PATCH 65/90] staging: comedi: dt3000: cleanup "find pci device" code H Hartley Sweeten
@ 2012-07-19 11:46 ` Ian Abbott
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Abbott @ 2012-07-19 11:46 UTC (permalink / raw)
  To: H Hartley Sweeten; +Cc: Linux Kernel, devel, Ian Abbott, gregkh

On 2012-07-19 02:57, H Hartley Sweeten wrote:
> The "find pci device" code for this driver was split between
> two functions which could cause the driver to walk the pci
> bus multiple times while looking for a match.

Actually the original version only walks through the PCI devices once 
(due to the `from` parameter in dt_pci_find_device()), but your version 
is cleaner.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti@mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-07-19 11:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-19  1:57 [PATCH 65/90] staging: comedi: dt3000: cleanup "find pci device" code H Hartley Sweeten
2012-07-19 11:46 ` Ian Abbott

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).