[PATCH 01/12] staging: comedi: drivers: introduce comedi_legacy_detach()

H Hartley Sweeten hsweeten at visionengravers.com
Wed Apr 17 18:16:50 UTC 2013


This function is intended to be used by the comedi legacy (ISA) drivers
either directly as the (*detach) function or as a helper in the drivers
private (*detach) function.

Modify the __comedi_request_region() helper so that it stores the first
struct resource returned by request_region() in the comedi_device.

The comedi_legacy_detach() function will then automatically release the
region during the detach of the driver.

Signed-off-by: H Hartley Sweeten <hsweeten at visionengravers.com>
Cc: Ian Abbott <abbotti at mev.co.uk>
Cc: Greg Kroah-Hartman <gregkh at linuxfoundation.org>
---
 drivers/staging/comedi/comedidev.h |  2 ++
 drivers/staging/comedi/drivers.c   | 29 ++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/comedi/comedidev.h b/drivers/staging/comedi/comedidev.h
index ce509d0..48dfe16 100644
--- a/drivers/staging/comedi/comedidev.h
+++ b/drivers/staging/comedi/comedidev.h
@@ -215,6 +215,7 @@ struct comedi_device {
 	struct comedi_subdevice *subdevices;
 
 	/* dumb */
+	struct resource *res;
 	unsigned long iobase;
 	unsigned int irq;
 
@@ -354,6 +355,7 @@ int __comedi_request_region(struct comedi_device *,
 			    unsigned long start, unsigned long len);
 int comedi_request_region(struct comedi_device *,
 			  unsigned long start, unsigned long len);
+void comedi_legacy_detach(struct comedi_device *);
 
 int comedi_auto_config(struct device *, struct comedi_driver *,
 		       unsigned long context);
diff --git a/drivers/staging/comedi/drivers.c b/drivers/staging/comedi/drivers.c
index 2a28f64..cb5f0d5 100644
--- a/drivers/staging/comedi/drivers.c
+++ b/drivers/staging/comedi/drivers.c
@@ -121,6 +121,7 @@ static void cleanup_device(struct comedi_device *dev)
 	dev->driver = NULL;
 	dev->board_name = NULL;
 	dev->board_ptr = NULL;
+	dev->res = NULL;
 	dev->iobase = 0;
 	dev->ioenabled = false;
 	dev->irq = 0;
@@ -358,6 +359,8 @@ static void comedi_report_boards(struct comedi_driver *driv)
 int __comedi_request_region(struct comedi_device *dev,
 			    unsigned long start, unsigned long len)
 {
+	struct resource *res;
+
 	if (!start) {
 		dev_warn(dev->class_dev,
 			 "%s: a I/O base address must be specified\n",
@@ -365,12 +368,23 @@ int __comedi_request_region(struct comedi_device *dev,
 		return -EINVAL;
 	}
 
-	if (!request_region(start, len, dev->board_name)) {
+	res = request_region(start, len, dev->board_name);
+	if (!res) {
 		dev_warn(dev->class_dev, "%s: I/O port conflict (%#lx,%lu)\n",
 			 dev->board_name, start, len);
 		return -EIO;
 	}
 
+	/*
+	 * Save the resource of the first region requested by a driver.
+	 * comedi_legacy_detach() will automatically release that region.
+	 *
+	 * If a driver request additional regions, it must release those
+	 * regions during the (*detach).
+	 */
+	if (!dev->res)
+		dev->res = res;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(__comedi_request_region);
@@ -394,6 +408,19 @@ int comedi_request_region(struct comedi_device *dev,
 }
 EXPORT_SYMBOL_GPL(comedi_request_region);
 
+/**
+ * comedi_legacy_detach() - A generic (*detach) function for legacy drivers.
+ * @dev: comedi_device struct
+ */
+void comedi_legacy_detach(struct comedi_device *dev)
+{
+	if (dev->res) {
+		release_region(dev->res->start, resource_size(dev->res));
+		dev->res = NULL;
+	}
+}
+EXPORT_SYMBOL_GPL(comedi_legacy_detach);
+
 int comedi_device_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 {
 	struct comedi_driver *driv;
-- 
1.8.1.4




More information about the devel mailing list