[PATCH] staging: comedi: adl_pci9111: factor out the find PCI device code

From: H Hartley Sweeten
Date: Wed May 23 2012 - 19:06:40 EST


Factor out the code that finds a matching PCI device from attach
function.

Signed-off-by: H Hartley Sweeten <hsweeten@xxxxxxxxxxxxxxxxxxx>
Cc: Ian Abbott <abbotti@xxxxxxxxx>
Cc: Mori Hess <fmhess@xxxxxxxxxxxxxxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>

---

diff --git a/drivers/staging/comedi/drivers/adl_pci9111.c b/drivers/staging/comedi/drivers/adl_pci9111.c
index ccfb1a5..d150383 100644
--- a/drivers/staging/comedi/drivers/adl_pci9111.c
+++ b/drivers/staging/comedi/drivers/adl_pci9111.c
@@ -1210,17 +1210,47 @@ static int pci9111_reset(struct comedi_device *dev)
return 0;
}

-/* Attach */
-/* - Register PCI device */
-/* - Declare device driver capability */
+static struct pci_dev *pci9111_find_pci(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_each_pci_dev(pcidev) {
+ if (pcidev->vendor != PCI_VENDOR_ID_ADLINK)
+ continue;
+ for (i = 0; i < pci9111_board_nbr; i++) {
+ if (pcidev->device != pci9111_boards[i].device_id)
+ continue;
+ if (bus || slot) {
+ /* requested particular bus/slot */
+ if (pcidev->bus->number != bus ||
+ PCI_SLOT(pcidev->devfn) != slot)
+ continue;
+ }
+ dev->board_ptr = pci9111_boards + i;
+ printk(KERN_ERR
+ "comedi%d: found %s (b:s:f=%d:%d:%d), irq=%d\n",
+ dev->minor, pci9111_boards[i].name,
+ pcidev->bus->number, PCI_SLOT(pcidev->devfn),
+ PCI_FUNC(pcidev->devfn), pcidev->irq);
+ return pcidev;
+ }
+ }
+ printk(KERN_ERR
+ "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
+ dev->minor, bus, slot);
+ return NULL;
+}

static int pci9111_attach(struct comedi_device *dev,
struct comedi_devconfig *it)
{
struct comedi_subdevice *subdevice;
unsigned long io_base, io_range, lcr_io_base, lcr_io_range;
- struct pci_dev *pci_device = NULL;
- int error, i;
+ int error;
const struct pci9111_board *board;

if (alloc_private(dev, sizeof(struct pci9111_private_data)) < 0)
@@ -1230,65 +1260,25 @@ static int pci9111_attach(struct comedi_device *dev,
printk(KERN_ERR "comedi%d: " PCI9111_DRIVER_NAME " driver\n",
dev->minor);

- for_each_pci_dev(pci_device) {
- if (pci_device->vendor == PCI_VENDOR_ID_ADLINK) {
- for (i = 0; i < pci9111_board_nbr; i++) {
- if (pci9111_boards[i].device_id ==
- pci_device->device) {
- /* was a particular bus/slot
- * requested? */
- if ((it->options[0] != 0)
- || (it->options[1] != 0)) {
- /* are we on the wrong
- * bus/slot? */
- if (pci_device->bus->number !=
- it->options[0]
- ||
- PCI_SLOT(pci_device->devfn)
- != it->options[1]) {
- continue;
- }
- }
-
- dev->board_ptr = pci9111_boards + i;
- board =
- (struct pci9111_board *)
- dev->board_ptr;
- dev_private->pci_device = pci_device;
- goto found;
- }
- }
- }
- }
-
- printk(KERN_ERR
- "comedi%d: no supported board found! (req. bus/slot : %d/%d)\n",
- dev->minor, it->options[0], it->options[1]);
- return -EIO;
-
-found:
-
- printk(KERN_ERR "comedi%d: found %s (b:s:f=%d:%d:%d) , irq=%d\n",
- dev->minor,
- pci9111_boards[i].name,
- pci_device->bus->number,
- PCI_SLOT(pci_device->devfn),
- PCI_FUNC(pci_device->devfn), pci_device->irq);
+ dev_private->pci_device = pci9111_find_pci(dev, it);
+ if (!dev_private->pci_device)
+ return -EIO;
+ board = (struct pci9111_board *)dev->board_ptr;

/* TODO: Warn about non-tested boards. */

/* Read local configuration register base address
* [PCI_BASE_ADDRESS #1]. */

- lcr_io_base = pci_resource_start(pci_device, 1);
- lcr_io_range = pci_resource_len(pci_device, 1);
+ lcr_io_base = pci_resource_start(dev_private->pci_device, 1);
+ lcr_io_range = pci_resource_len(dev_private->pci_device, 1);

printk
("comedi%d: local configuration registers at address 0x%4lx [0x%4lx]\n",
dev->minor, lcr_io_base, lcr_io_range);

/* Enable PCI device and request regions */
- if (comedi_pci_enable(pci_device, PCI9111_DRIVER_NAME) < 0) {
+ if (comedi_pci_enable(dev_private->pci_device, PCI9111_DRIVER_NAME) < 0) {
printk
("comedi%d: Failed to enable PCI device and request regions\n",
dev->minor);
@@ -1296,8 +1286,8 @@ found:
}
/* Read PCI6308 register base address [PCI_BASE_ADDRESS #2]. */

- io_base = pci_resource_start(pci_device, 2);
- io_range = pci_resource_len(pci_device, 2);
+ io_base = pci_resource_start(dev_private->pci_device, 2);
+ io_range = pci_resource_len(dev_private->pci_device, 2);

printk(KERN_ERR "comedi%d: 6503 registers at address 0x%4lx [0x%4lx]\n",
dev->minor, io_base, io_range);
@@ -1314,16 +1304,17 @@ found:
/* Irq setup */

dev->irq = 0;
- if (pci_device->irq > 0) {
- if (request_irq(pci_device->irq, pci9111_interrupt,
+ if (dev_private->pci_device->irq > 0) {
+ dev->irq = dev_private->pci_device->irq;
+
+ if (request_irq(dev->irq, pci9111_interrupt,
IRQF_SHARED, PCI9111_DRIVER_NAME, dev) != 0) {
printk(KERN_ERR
"comedi%d: unable to allocate irq %u\n",
- dev->minor, pci_device->irq);
+ dev->minor, dev->irq);
return -EINVAL;
}
}
- dev->irq = pci_device->irq;

/* TODO: Add external multiplexer setup (according to option[2]). */

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/