]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
iio: trigger: implement devm_iio_trigger_alloc/devm_iio_triger_free
authorJacek Anaszewski <j.anaszewski@samsung.com>
Fri, 16 Aug 2013 13:11:00 +0000 (14:11 +0100)
committerJonathan Cameron <jic23@kernel.org>
Sat, 17 Aug 2013 18:34:52 +0000 (19:34 +0100)
Add a resource managed devm_iio_trigger_alloc()/devm_iio_triger_free()
to automatically clean up triggers allocated by IIO drivers, thus
leading to simplified IIO drivers code.

Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com>
Signed-off-by: Kyunmin Park <kyungmin.park@samsung.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Documentation/driver-model/devres.txt
drivers/iio/industrialio-trigger.c
include/linux/iio/iio.h

index 1d3233292989682a62252b206a7ff76e6f9d75cc..fb57d85e7316027f58c336a359e0b1eb6c320f64 100644 (file)
@@ -240,6 +240,8 @@ MEM
 IIO
   devm_iio_device_alloc()
   devm_iio_device_free()
+  devm_iio_trigger_alloc()
+  devm_iio_trigger_free()
 
 IO region
   devm_request_region()
index 0dd9bb8731301e755dc29ef2851371f70deb819d..bf5e70a32d3fb6011be6324c20ad45d4ba704064 100644 (file)
@@ -424,9 +424,8 @@ static void iio_trig_subirqunmask(struct irq_data *d)
        trig->subirqs[d->irq - trig->subirq_base].enabled = true;
 }
 
-struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
+static struct iio_trigger *viio_trigger_alloc(const char *fmt, va_list vargs)
 {
-       va_list vargs;
        struct iio_trigger *trig;
        trig = kzalloc(sizeof *trig, GFP_KERNEL);
        if (trig) {
@@ -444,9 +443,8 @@ struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
                        kfree(trig);
                        return NULL;
                }
-               va_start(vargs, fmt);
+
                trig->name = kvasprintf(GFP_KERNEL, fmt, vargs);
-               va_end(vargs);
                if (trig->name == NULL) {
                        irq_free_descs(trig->subirq_base,
                                       CONFIG_IIO_CONSUMERS_PER_TRIGGER);
@@ -467,6 +465,19 @@ struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
                }
                get_device(&trig->dev);
        }
+
+       return trig;
+}
+
+struct iio_trigger *iio_trigger_alloc(const char *fmt, ...)
+{
+       struct iio_trigger *trig;
+       va_list vargs;
+
+       va_start(vargs, fmt);
+       trig = viio_trigger_alloc(fmt, vargs);
+       va_end(vargs);
+
        return trig;
 }
 EXPORT_SYMBOL(iio_trigger_alloc);
@@ -478,6 +489,59 @@ void iio_trigger_free(struct iio_trigger *trig)
 }
 EXPORT_SYMBOL(iio_trigger_free);
 
+static void devm_iio_trigger_release(struct device *dev, void *res)
+{
+       iio_trigger_free(*(struct iio_trigger **)res);
+}
+
+static int devm_iio_trigger_match(struct device *dev, void *res, void *data)
+{
+       struct iio_trigger **r = res;
+
+       if (!r || !*r) {
+               WARN_ON(!r || !*r);
+               return 0;
+       }
+
+       return *r == data;
+}
+
+struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
+                                               const char *fmt, ...)
+{
+       struct iio_trigger **ptr, *trig;
+       va_list vargs;
+
+       ptr = devres_alloc(devm_iio_trigger_release, sizeof(*ptr),
+                          GFP_KERNEL);
+       if (!ptr)
+               return NULL;
+
+       /* use raw alloc_dr for kmalloc caller tracing */
+       va_start(vargs, fmt);
+       trig = viio_trigger_alloc(fmt, vargs);
+       va_end(vargs);
+       if (trig) {
+               *ptr = trig;
+               devres_add(dev, ptr);
+       } else {
+               devres_free(ptr);
+       }
+
+       return trig;
+}
+EXPORT_SYMBOL_GPL(devm_iio_trigger_alloc);
+
+void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
+{
+       int rc;
+
+       rc = devres_release(dev, devm_iio_trigger_release,
+                           devm_iio_trigger_match, iio_trig);
+       WARN_ON(rc);
+}
+EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
+
 void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
 {
        indio_dev->groups[indio_dev->groupcounter++] =
index 09ebe0a4d8e6a65d586efd893407208ac452662c..2103cc32a5fb9b7d701d428e2f49ac636bbc6376 100644 (file)
@@ -556,6 +556,35 @@ struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv);
  */
 void devm_iio_device_free(struct device *dev, struct iio_dev *indio_dev);
 
+/**
+ * devm_iio_trigger_alloc - Resource-managed iio_trigger_alloc()
+ * @dev:               Device to allocate iio_trigger for
+ * @fmt:               trigger name format. If it includes format
+ *                     specifiers, the additional arguments following
+ *                     format are formatted and inserted in the resulting
+ *                     string replacing their respective specifiers.
+ *
+ * Managed iio_trigger_alloc.  iio_trigger allocated with this function is
+ * automatically freed on driver detach.
+ *
+ * If an iio_trigger allocated with this function needs to be freed separately,
+ * devm_iio_trigger_free() must be used.
+ *
+ * RETURNS:
+ * Pointer to allocated iio_trigger on success, NULL on failure.
+ */
+struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
+                                               const char *fmt, ...);
+
+/**
+ * devm_iio_trigger_free - Resource-managed iio_trigger_free()
+ * @dev:               Device this iio_dev belongs to
+ * @iio_trig:          the iio_trigger associated with the device
+ *
+ * Free iio_trigger allocated with devm_iio_trigger_alloc().
+ */
+void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig);
+
 /**
  * iio_buffer_enabled() - helper function to test if the buffer is enabled
  * @indio_dev:         IIO device structure for device