Skip to content

Commit bb4bbbe

Browse files
William Breathitt Graygregkh
authored andcommitted
counter: Consolidate Counter extension sysfs attribute creation
Counter extensions are handled for the Device, Counts, and Signals. The code loops through each Counter extension and creates the expected sysfs attributes. This patch consolidates that code into functions to reduce redundancy and make the intention of the code clearer. Link: https://lore.kernel.org/r/6f2121cf52073028c119dbf981a8b72f3eb625d2.1664204990.git.william.gray@linaro.org/ Signed-off-by: William Breathitt Gray <[email protected]> Link: https://lore.kernel.org/r/0469c3ae3fbccbca908993c78d94f221761a6a3a.1664318353.git.william.gray@linaro.org Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 45d2918 commit bb4bbbe

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

drivers/counter/counter-sysfs.c

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,46 @@ static int counter_comp_id_attr_create(struct device *const dev,
592592
return 0;
593593
}
594594

595+
static int counter_ext_attrs_create(struct device *const dev,
596+
struct counter_attribute_group *const group,
597+
const struct counter_comp *const ext,
598+
const enum counter_scope scope,
599+
void *const parent, const size_t id)
600+
{
601+
int err;
602+
603+
/* Create main extension attribute */
604+
err = counter_attr_create(dev, group, ext, scope, parent);
605+
if (err < 0)
606+
return err;
607+
608+
/* Create extension id attribute */
609+
return counter_comp_id_attr_create(dev, group, ext->name, id);
610+
}
611+
612+
static int counter_sysfs_exts_add(struct device *const dev,
613+
struct counter_attribute_group *const group,
614+
const struct counter_comp *const exts,
615+
const size_t num_ext,
616+
const enum counter_scope scope,
617+
void *const parent)
618+
{
619+
size_t i;
620+
const struct counter_comp *ext;
621+
int err;
622+
623+
/* Create attributes for each extension */
624+
for (i = 0; i < num_ext; i++) {
625+
ext = &exts[i];
626+
err = counter_ext_attrs_create(dev, group, ext, scope, parent,
627+
i);
628+
if (err < 0)
629+
return err;
630+
}
631+
632+
return 0;
633+
}
634+
595635
static struct counter_comp counter_signal_comp = {
596636
.type = COUNTER_COMP_SIGNAL_LEVEL,
597637
.name = "signal",
@@ -605,8 +645,6 @@ static int counter_signal_attrs_create(struct counter_device *const counter,
605645
struct device *const dev = &counter->dev;
606646
int err;
607647
struct counter_comp comp;
608-
size_t i;
609-
struct counter_comp *ext;
610648

611649
/* Create main Signal attribute */
612650
comp = counter_signal_comp;
@@ -620,21 +658,9 @@ static int counter_signal_attrs_create(struct counter_device *const counter,
620658
if (err < 0)
621659
return err;
622660

623-
/* Create an attribute for each extension */
624-
for (i = 0; i < signal->num_ext; i++) {
625-
ext = &signal->ext[i];
626-
627-
err = counter_attr_create(dev, cattr_group, ext, scope, signal);
628-
if (err < 0)
629-
return err;
630-
631-
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
632-
i);
633-
if (err < 0)
634-
return err;
635-
}
636-
637-
return 0;
661+
/* Add Signal extensions */
662+
return counter_sysfs_exts_add(dev, cattr_group, signal->ext,
663+
signal->num_ext, scope, signal);
638664
}
639665

640666
static int counter_sysfs_signals_add(struct counter_device *const counter,
@@ -719,8 +745,6 @@ static int counter_count_attrs_create(struct counter_device *const counter,
719745
struct device *const dev = &counter->dev;
720746
int err;
721747
struct counter_comp comp;
722-
size_t i;
723-
struct counter_comp *ext;
724748

725749
/* Create main Count attribute */
726750
comp = counter_count_comp;
@@ -743,21 +767,9 @@ static int counter_count_attrs_create(struct counter_device *const counter,
743767
if (err < 0)
744768
return err;
745769

746-
/* Create an attribute for each extension */
747-
for (i = 0; i < count->num_ext; i++) {
748-
ext = &count->ext[i];
749-
750-
err = counter_attr_create(dev, cattr_group, ext, scope, count);
751-
if (err < 0)
752-
return err;
753-
754-
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
755-
i);
756-
if (err < 0)
757-
return err;
758-
}
759-
760-
return 0;
770+
/* Add Count extensions */
771+
return counter_sysfs_exts_add(dev, cattr_group, count->ext,
772+
count->num_ext, scope, count);
761773
}
762774

763775
static int counter_sysfs_counts_add(struct counter_device *const counter,
@@ -850,8 +862,6 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,
850862
const enum counter_scope scope = COUNTER_SCOPE_DEVICE;
851863
struct device *const dev = &counter->dev;
852864
int err;
853-
size_t i;
854-
struct counter_comp *ext;
855865

856866
/* Add Signals sysfs attributes */
857867
err = counter_sysfs_signals_add(counter, cattr_group);
@@ -888,19 +898,9 @@ static int counter_sysfs_attr_add(struct counter_device *const counter,
888898
if (err < 0)
889899
return err;
890900

891-
/* Create an attribute for each extension */
892-
for (i = 0; i < counter->num_ext; i++) {
893-
ext = &counter->ext[i];
894-
895-
err = counter_attr_create(dev, cattr_group, ext, scope, NULL);
896-
if (err < 0)
897-
return err;
898-
899-
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
900-
i);
901-
if (err < 0)
902-
return err;
903-
}
901+
/* Add device extensions */
902+
return counter_sysfs_exts_add(dev, cattr_group, counter->ext,
903+
counter->num_ext, scope, NULL);
904904

905905
return 0;
906906
}

0 commit comments

Comments
 (0)