summaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2013-10-07 07:50:00 -0400
committerJonathan Cameron <jic23@kernel.org>2013-10-12 07:17:34 -0400
commit84088ebd14aebf1b8499409a037094b9b88e2796 (patch)
treea239085ff20650319449d811f5e55207ea890df3 /drivers/iio
parenta95194569f697a6cc10d00f9b9b3d21b0b820520 (diff)
iio: Add a helper to free a list of IIO device attributes
We have the same code to free a IIO device attribute list in multiple place. This patch adds a new helper function to take care of this and replaces the custom instances with a call to the helper function. Note that we do not need to call list_del() for each of the list items since we will never look at any of the list items nor the list itself again. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/iio_core.h1
-rw-r--r--drivers/iio/industrialio-buffer.c21
-rw-r--r--drivers/iio/industrialio-core.c34
-rw-r--r--drivers/iio/industrialio-event.c15
4 files changed, 23 insertions, 48 deletions
diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 7512cf728ee6..9939917033ca 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -33,6 +33,7 @@ int __iio_add_chan_devattr(const char *postfix,
33 enum iio_shared_by shared_by, 33 enum iio_shared_by shared_by,
34 struct device *dev, 34 struct device *dev,
35 struct list_head *attr_list); 35 struct list_head *attr_list);
36void iio_free_chan_devattr_list(struct list_head *attr_list);
36 37
37/* Event interface flags */ 38/* Event interface flags */
38#define IIO_BUSY_BIT_POS 1 39#define IIO_BUSY_BIT_POS 1
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index d6a5455ae51a..d12b384d94bf 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -274,23 +274,6 @@ error_ret:
274 return ret; 274 return ret;
275} 275}
276 276
277static void iio_buffer_remove_and_free_scan_dev_attr(struct iio_dev *indio_dev,
278 struct iio_dev_attr *p)
279{
280 kfree(p->dev_attr.attr.name);
281 kfree(p);
282}
283
284static void __iio_buffer_attr_cleanup(struct iio_dev *indio_dev)
285{
286 struct iio_dev_attr *p, *n;
287 struct iio_buffer *buffer = indio_dev->buffer;
288
289 list_for_each_entry_safe(p, n,
290 &buffer->scan_el_dev_attr_list, l)
291 iio_buffer_remove_and_free_scan_dev_attr(indio_dev, p);
292}
293
294static const char * const iio_scan_elements_group_name = "scan_elements"; 277static const char * const iio_scan_elements_group_name = "scan_elements";
295 278
296int iio_buffer_register(struct iio_dev *indio_dev, 279int iio_buffer_register(struct iio_dev *indio_dev,
@@ -367,7 +350,7 @@ int iio_buffer_register(struct iio_dev *indio_dev,
367error_free_scan_mask: 350error_free_scan_mask:
368 kfree(buffer->scan_mask); 351 kfree(buffer->scan_mask);
369error_cleanup_dynamic: 352error_cleanup_dynamic:
370 __iio_buffer_attr_cleanup(indio_dev); 353 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
371 354
372 return ret; 355 return ret;
373} 356}
@@ -377,7 +360,7 @@ void iio_buffer_unregister(struct iio_dev *indio_dev)
377{ 360{
378 kfree(indio_dev->buffer->scan_mask); 361 kfree(indio_dev->buffer->scan_mask);
379 kfree(indio_dev->buffer->scan_el_group.attrs); 362 kfree(indio_dev->buffer->scan_el_group.attrs);
380 __iio_buffer_attr_cleanup(indio_dev); 363 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
381} 364}
382EXPORT_SYMBOL(iio_buffer_unregister); 365EXPORT_SYMBOL(iio_buffer_unregister);
383 366
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index dc24a9b3d325..572982fe3155 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -794,11 +794,22 @@ static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
794 return attrcount; 794 return attrcount;
795} 795}
796 796
797static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev, 797/**
798 struct iio_dev_attr *p) 798 * iio_free_chan_devattr_list() - Free a list of IIO device attributes
799 * @attr_list: List of IIO device attributes
800 *
801 * This function frees the memory allocated for each of the IIO device
802 * attributes in the list. Note: if you want to reuse the list after calling
803 * this function you have to reinitialize it using INIT_LIST_HEAD().
804 */
805void iio_free_chan_devattr_list(struct list_head *attr_list)
799{ 806{
800 kfree(p->dev_attr.attr.name); 807 struct iio_dev_attr *p, *n;
801 kfree(p); 808
809 list_for_each_entry_safe(p, n, attr_list, l) {
810 kfree(p->dev_attr.attr.name);
811 kfree(p);
812 }
802} 813}
803 814
804static ssize_t iio_show_dev_name(struct device *dev, 815static ssize_t iio_show_dev_name(struct device *dev,
@@ -814,7 +825,7 @@ static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
814static int iio_device_register_sysfs(struct iio_dev *indio_dev) 825static int iio_device_register_sysfs(struct iio_dev *indio_dev)
815{ 826{
816 int i, ret = 0, attrcount, attrn, attrcount_orig = 0; 827 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
817 struct iio_dev_attr *p, *n; 828 struct iio_dev_attr *p;
818 struct attribute **attr; 829 struct attribute **attr;
819 830
820 /* First count elements in any existing group */ 831 /* First count elements in any existing group */
@@ -867,11 +878,7 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
867 return 0; 878 return 0;
868 879
869error_clear_attrs: 880error_clear_attrs:
870 list_for_each_entry_safe(p, n, 881 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
871 &indio_dev->channel_attr_list, l) {
872 list_del(&p->l);
873 iio_device_remove_and_free_read_attr(indio_dev, p);
874 }
875 882
876 return ret; 883 return ret;
877} 884}
@@ -879,12 +886,7 @@ error_clear_attrs:
879static void iio_device_unregister_sysfs(struct iio_dev *indio_dev) 886static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
880{ 887{
881 888
882 struct iio_dev_attr *p, *n; 889 iio_free_chan_devattr_list(&indio_dev->channel_attr_list);
883
884 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
885 list_del(&p->l);
886 iio_device_remove_and_free_read_attr(indio_dev, p);
887 }
888 kfree(indio_dev->chan_attr_group.attrs); 890 kfree(indio_dev->chan_attr_group.attrs);
889} 891}
890 892
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index d251f30fb739..4a3fd5acda94 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -350,17 +350,6 @@ error_ret:
350 return ret; 350 return ret;
351} 351}
352 352
353static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
354{
355 struct iio_dev_attr *p, *n;
356 list_for_each_entry_safe(p, n,
357 &indio_dev->event_interface->
358 dev_attr_list, l) {
359 kfree(p->dev_attr.attr.name);
360 kfree(p);
361 }
362}
363
364static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev) 353static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
365{ 354{
366 int j, ret, attrcount = 0; 355 int j, ret, attrcount = 0;
@@ -452,7 +441,7 @@ int iio_device_register_eventset(struct iio_dev *indio_dev)
452 return 0; 441 return 0;
453 442
454error_free_setup_event_lines: 443error_free_setup_event_lines:
455 __iio_remove_event_config_attrs(indio_dev); 444 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
456 kfree(indio_dev->event_interface); 445 kfree(indio_dev->event_interface);
457error_ret: 446error_ret:
458 447
@@ -477,7 +466,7 @@ void iio_device_unregister_eventset(struct iio_dev *indio_dev)
477{ 466{
478 if (indio_dev->event_interface == NULL) 467 if (indio_dev->event_interface == NULL)
479 return; 468 return;
480 __iio_remove_event_config_attrs(indio_dev); 469 iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
481 kfree(indio_dev->event_interface->group.attrs); 470 kfree(indio_dev->event_interface->group.attrs);
482 kfree(indio_dev->event_interface); 471 kfree(indio_dev->event_interface);
483} 472}