[PATCH 02/13] staging: comedi: adv_pci1724: use auto_attach method

Ian Abbott abbotti at mev.co.uk
Tue Feb 19 13:09:09 UTC 2013


The old method of supporting auto-attachment of comedi PCI devices via
the struct comedi_driver's `attach()` method is no longer supported.
The struct comedi_driver needs to support an `auto_attach()` method to
do this.  Also, there is no need for this driver to support manual
attachment of devices, so just replace the `attach()` method with an
`auto_attach()` method implemented by `adv_pci1724_auto_attach()`.

As only a single PCI device is supported and `adv_pci1724_boards[]` does
not contain any useful information, just get rid of it.

Get rid of the `struct pci_dev *hw_dev` member of `struct
adv_pci1724_private` and use `comedi_to_pci_dev(dev)` to get a pointer
to the PCI device from the comedi device.  (The comedi core makes that
useable before it calls the comedi driver's `auto_attach()` method.)

Also rename the `detach()` function to `adv_pci1724_detach()`.  It no
longer needs to "put" the PCI device as the PCI device's reference count
is not incremented by `adv_pci1724_auto_attach()`.

Signed-off-by: Ian Abbott <abbotti at mev.co.uk>
Cc: Frank Mori Hess <fmh6jj at gmail.com>
---
 drivers/staging/comedi/drivers/adv_pci1724.c | 109 +++++++--------------------
 1 file changed, 26 insertions(+), 83 deletions(-)

diff --git a/drivers/staging/comedi/drivers/adv_pci1724.c b/drivers/staging/comedi/drivers/adv_pci1724.c
index 7ba8d5e..e9796f2 100644
--- a/drivers/staging/comedi/drivers/adv_pci1724.c
+++ b/drivers/staging/comedi/drivers/adv_pci1724.c
@@ -50,8 +50,10 @@ of adjusting the offset and gain calibration until the board outputs in
 the desired range.
 
 Configuration options:
-   [0] - PCI bus of device (optional)
-   [1] - PCI slot of device (optional)
+   None
+
+Manual configuration of comedi devices is not supported by this driver;
+supported PCI devices are configured as comedi devices automatically.
 
 */
 
@@ -118,11 +120,6 @@ enum board_id_contents {
 	BOARD_ID_MASK = 0xf
 };
 
-struct adv_pci1724_board {
-	const char *name;
-	int device_id;		/*  pci device id */
-};
-
 static const struct comedi_lrange ao_ranges_1724 = { 4,
 	{
 		BIP_RANGE(10),
@@ -132,13 +129,6 @@ static const struct comedi_lrange ao_ranges_1724 = { 4,
 	}
 };
 
-static const struct adv_pci1724_board adv_pci1724_boards[] = {
-	{
-	 .name = "pci-1724u",
-	 .device_id = 0x1724,
-	}
-};
-
 static DEFINE_PCI_DEVICE_TABLE(adv_pci1724_pci_table) = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_ADVANTECH, 0x1724) },
 	{ 0 }
@@ -146,15 +136,8 @@ static DEFINE_PCI_DEVICE_TABLE(adv_pci1724_pci_table) = {
 
 MODULE_DEVICE_TABLE(pci, adv_pci1724_pci_table);
 
-static inline struct adv_pci1724_board *board(const struct comedi_device *dev)
-{
-	return (struct adv_pci1724_board *)dev->board_ptr;
-}
-
 /* this structure is for data unique to this hardware driver. */
 struct adv_pci1724_private {
-
-	struct pci_dev *hw_dev;
 	resource_size_t main_iobase;
 	unsigned board_id; /* 4 bit number settable via dip switches on board */
 	int ao_value[NUM_AO_CHANNELS];
@@ -170,13 +153,14 @@ static inline struct adv_pci1724_private *priv(struct comedi_device *dev)
 	return dev->private;
 }
 
-static int attach(struct comedi_device *dev, struct comedi_devconfig *it);
-static void detach(struct comedi_device *dev);
+static int adv_pci1724_auto_attach(struct comedi_device *dev,
+				   unsigned long context_unused);
+static void adv_pci1724_detach(struct comedi_device *dev);
 static struct comedi_driver driver_adv_pci1724 = {
 	.driver_name = "adv_pci1724",
 	.module = THIS_MODULE,
-	.attach = attach,
-	.detach = detach,
+	.auto_attach = adv_pci1724_auto_attach,
+	.detach = adv_pci1724_detach,
 };
 
 static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
@@ -278,17 +262,13 @@ static int setup_subdevices(struct comedi_device *dev)
 	return 0;
 }
 
-static int attach(struct comedi_device *dev, struct comedi_devconfig *it)
+static int adv_pci1724_auto_attach(struct comedi_device *dev,
+				   unsigned long context_unused)
 {
-	struct pci_dev *pcidev = NULL;
+	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 	int i;
 	int retval;
 
-	pr_info("comedi%d: adv_pci1724\n", dev->minor);
-
-	/*
-	 * Allocate the private structure area.
-	 */
 	dev->private = kzalloc(sizeof(struct adv_pci1724_private), GFP_KERNEL);
 	if (!dev->private)
 		return -ENOMEM;
@@ -300,73 +280,36 @@ static int attach(struct comedi_device *dev, struct comedi_devconfig *it)
 		priv(dev)->gain_value[i] = -1;
 	}
 
-	/*
-	 * Probe the device to determine what device in the series it is.
-	 */
-	for_each_pci_dev(pcidev) {
-		/*  is it not a computer boards card? */
-		if (pcidev->vendor != PCI_VENDOR_ID_ADVANTECH)
-			continue;
-		/*  loop through cards supported by this driver */
-		for (i = 0; i < ARRAY_SIZE(adv_pci1724_boards); ++i) {
-			if (adv_pci1724_boards[i].device_id != pcidev->device)
-				continue;
-			/*  was a particular bus/slot requested? */
-			if (it->options[0] || it->options[1]) {
-				/*  are we on the wrong bus/slot? */
-				if (pcidev->bus->number != it->options[0] ||
-				    PCI_SLOT(pcidev->devfn) != it->options[1]) {
-					continue;
-				}
-			}
-			priv(dev)->hw_dev = pcidev;
-			dev->board_ptr = adv_pci1724_boards + i;
-			break;
-		}
-		if (dev->board_ptr)
-			break;
-	}
-
-	if (dev->board_ptr == NULL) {
-		pr_info("No supported Advantech 1724 card found\n");
-		return -EIO;
-	}
-
-	pr_info("Found %s on bus %i, slot %i\n", board(dev)->name,
-		pcidev->bus->number, PCI_SLOT(pcidev->devfn));
+	dev->board_name = dev->driver->driver_name;
 
-	if (comedi_pci_enable(pcidev, driver_adv_pci1724.driver_name)) {
-		pr_warn(" failed to enable PCI device and request regions\n");
-		return -EIO;
-	}
-
-	/* Initialize dev->board_name */
-	dev->board_name = board(dev)->name;
+	retval = comedi_pci_enable(pcidev, dev->board_name);
+	if (retval)
+		return retval;
 
-	priv(dev)->main_iobase =
-	    pci_resource_start(pcidev, 2);
+	priv(dev)->main_iobase = pci_resource_start(pcidev, 2);
 
 	priv(dev)->board_id = inl(priv(dev)->main_iobase + BOARD_ID_REG) &
 			      BOARD_ID_MASK;
-	pr_info("board id: %d\n", priv(dev)->board_id);
 
 	retval = setup_subdevices(dev);
 	if (retval < 0)
 		return retval;
 
+	dev_info(dev->class_dev, "%s (pci %s) attached, board id: %u\n",
+		 dev->board_name, pci_name(pcidev), priv(dev)->board_id);
 	return 0;
 }
 
-static void detach(struct comedi_device *dev)
+static void adv_pci1724_detach(struct comedi_device *dev)
 {
-	pr_info("comedi%d: adv_pci1724: remove\n", dev->minor);
+	struct pci_dev *pcidev = comedi_to_pci_dev(dev);
 
 	if (priv(dev)) {
-		if (priv(dev)->hw_dev) {
-			if (priv(dev)->main_iobase)
-				comedi_pci_disable(priv(dev)->hw_dev);
-
-			pci_dev_put(priv(dev)->hw_dev);
+		if (pcidev) {
+			if (priv(dev)->main_iobase) {
+				comedi_pci_disable(pcidev);
+				dev_info(dev->class_dev, "detached\n");
+			}
 		}
 	}
 }
-- 
1.8.1.2




More information about the devel mailing list