1 // SPDX-License-Identifier: GPL-2.0
3 * Generic Counter sysfs interface
4 * Copyright (C) 2020 William Breathitt Gray
6 #include <linux/counter.h>
7 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/kfifo.h>
12 #include <linux/kstrtox.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/sysfs.h>
18 #include <linux/types.h>
20 #include "counter-sysfs.h"
22 static inline struct counter_device *counter_from_dev(struct device *dev)
24 return container_of(dev, struct counter_device, dev);
28 * struct counter_attribute - Counter sysfs attribute
29 * @dev_attr: device attribute for sysfs
30 * @l: node to add Counter attribute to attribute group list
31 * @comp: Counter component callbacks and data
32 * @scope: Counter scope of the attribute
33 * @parent: pointer to the parent component
35 struct counter_attribute {
36 struct device_attribute dev_attr;
39 struct counter_comp comp;
40 enum counter_scope scope;
44 #define to_counter_attribute(_dev_attr) \
45 container_of(_dev_attr, struct counter_attribute, dev_attr)
48 * struct counter_attribute_group - container for attribute group
49 * @name: name of the attribute group
50 * @attr_list: list to keep track of created attributes
51 * @num_attr: number of attributes
53 struct counter_attribute_group {
55 struct list_head attr_list;
59 static const char *const counter_function_str[] = {
60 [COUNTER_FUNCTION_INCREASE] = "increase",
61 [COUNTER_FUNCTION_DECREASE] = "decrease",
62 [COUNTER_FUNCTION_PULSE_DIRECTION] = "pulse-direction",
63 [COUNTER_FUNCTION_QUADRATURE_X1_A] = "quadrature x1 a",
64 [COUNTER_FUNCTION_QUADRATURE_X1_B] = "quadrature x1 b",
65 [COUNTER_FUNCTION_QUADRATURE_X2_A] = "quadrature x2 a",
66 [COUNTER_FUNCTION_QUADRATURE_X2_B] = "quadrature x2 b",
67 [COUNTER_FUNCTION_QUADRATURE_X4] = "quadrature x4"
70 static const char *const counter_signal_value_str[] = {
71 [COUNTER_SIGNAL_LEVEL_LOW] = "low",
72 [COUNTER_SIGNAL_LEVEL_HIGH] = "high"
75 static const char *const counter_synapse_action_str[] = {
76 [COUNTER_SYNAPSE_ACTION_NONE] = "none",
77 [COUNTER_SYNAPSE_ACTION_RISING_EDGE] = "rising edge",
78 [COUNTER_SYNAPSE_ACTION_FALLING_EDGE] = "falling edge",
79 [COUNTER_SYNAPSE_ACTION_BOTH_EDGES] = "both edges"
82 static const char *const counter_count_direction_str[] = {
83 [COUNTER_COUNT_DIRECTION_FORWARD] = "forward",
84 [COUNTER_COUNT_DIRECTION_BACKWARD] = "backward"
87 static const char *const counter_count_mode_str[] = {
88 [COUNTER_COUNT_MODE_NORMAL] = "normal",
89 [COUNTER_COUNT_MODE_RANGE_LIMIT] = "range limit",
90 [COUNTER_COUNT_MODE_NON_RECYCLE] = "non-recycle",
91 [COUNTER_COUNT_MODE_MODULO_N] = "modulo-n"
94 static ssize_t counter_comp_u8_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
97 const struct counter_attribute *const a = to_counter_attribute(attr);
98 struct counter_device *const counter = counter_from_dev(dev);
103 case COUNTER_SCOPE_DEVICE:
104 err = a->comp.device_u8_read(counter, &data);
106 case COUNTER_SCOPE_SIGNAL:
107 err = a->comp.signal_u8_read(counter, a->parent, &data);
109 case COUNTER_SCOPE_COUNT:
110 err = a->comp.count_u8_read(counter, a->parent, &data);
118 if (a->comp.type == COUNTER_COMP_BOOL)
119 /* data should already be boolean but ensure just to be safe */
122 return sysfs_emit(buf, "%u\n", (unsigned int)data);
125 static ssize_t counter_comp_u8_store(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t len)
129 const struct counter_attribute *const a = to_counter_attribute(attr);
130 struct counter_device *const counter = counter_from_dev(dev);
135 if (a->comp.type == COUNTER_COMP_BOOL) {
136 err = kstrtobool(buf, &bool_data);
139 err = kstrtou8(buf, 0, &data);
144 case COUNTER_SCOPE_DEVICE:
145 err = a->comp.device_u8_write(counter, data);
147 case COUNTER_SCOPE_SIGNAL:
148 err = a->comp.signal_u8_write(counter, a->parent, data);
150 case COUNTER_SCOPE_COUNT:
151 err = a->comp.count_u8_write(counter, a->parent, data);
162 static ssize_t counter_comp_u32_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
165 const struct counter_attribute *const a = to_counter_attribute(attr);
166 struct counter_device *const counter = counter_from_dev(dev);
167 const struct counter_available *const avail = a->comp.priv;
172 case COUNTER_SCOPE_DEVICE:
173 err = a->comp.device_u32_read(counter, &data);
175 case COUNTER_SCOPE_SIGNAL:
176 err = a->comp.signal_u32_read(counter, a->parent, &data);
178 case COUNTER_SCOPE_COUNT:
179 if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION)
180 err = a->comp.action_read(counter, a->parent,
181 a->comp.priv, &data);
183 err = a->comp.count_u32_read(counter, a->parent, &data);
191 switch (a->comp.type) {
192 case COUNTER_COMP_FUNCTION:
193 return sysfs_emit(buf, "%s\n", counter_function_str[data]);
194 case COUNTER_COMP_SIGNAL_LEVEL:
195 return sysfs_emit(buf, "%s\n", counter_signal_value_str[data]);
196 case COUNTER_COMP_SYNAPSE_ACTION:
197 return sysfs_emit(buf, "%s\n", counter_synapse_action_str[data]);
198 case COUNTER_COMP_ENUM:
199 return sysfs_emit(buf, "%s\n", avail->strs[data]);
200 case COUNTER_COMP_COUNT_DIRECTION:
201 return sysfs_emit(buf, "%s\n", counter_count_direction_str[data]);
202 case COUNTER_COMP_COUNT_MODE:
203 return sysfs_emit(buf, "%s\n", counter_count_mode_str[data]);
205 return sysfs_emit(buf, "%u\n", (unsigned int)data);
209 static int counter_find_enum(u32 *const enum_item, const u32 *const enums,
210 const size_t num_enums, const char *const buf,
211 const char *const string_array[])
215 for (index = 0; index < num_enums; index++) {
216 *enum_item = enums[index];
217 if (sysfs_streq(buf, string_array[*enum_item]))
224 static ssize_t counter_comp_u32_store(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf, size_t len)
228 const struct counter_attribute *const a = to_counter_attribute(attr);
229 struct counter_device *const counter = counter_from_dev(dev);
230 struct counter_count *const count = a->parent;
231 struct counter_synapse *const synapse = a->comp.priv;
232 const struct counter_available *const avail = a->comp.priv;
236 switch (a->comp.type) {
237 case COUNTER_COMP_FUNCTION:
238 err = counter_find_enum(&data, count->functions_list,
239 count->num_functions, buf,
240 counter_function_str);
242 case COUNTER_COMP_SYNAPSE_ACTION:
243 err = counter_find_enum(&data, synapse->actions_list,
244 synapse->num_actions, buf,
245 counter_synapse_action_str);
247 case COUNTER_COMP_ENUM:
248 err = __sysfs_match_string(avail->strs, avail->num_items, buf);
251 case COUNTER_COMP_COUNT_MODE:
252 err = counter_find_enum(&data, avail->enums, avail->num_items,
253 buf, counter_count_mode_str);
256 err = kstrtou32(buf, 0, &data);
263 case COUNTER_SCOPE_DEVICE:
264 err = a->comp.device_u32_write(counter, data);
266 case COUNTER_SCOPE_SIGNAL:
267 err = a->comp.signal_u32_write(counter, a->parent, data);
269 case COUNTER_SCOPE_COUNT:
270 if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION)
271 err = a->comp.action_write(counter, count, synapse,
274 err = a->comp.count_u32_write(counter, count, data);
285 static ssize_t counter_comp_u64_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 const struct counter_attribute *const a = to_counter_attribute(attr);
289 struct counter_device *const counter = counter_from_dev(dev);
294 case COUNTER_SCOPE_DEVICE:
295 err = a->comp.device_u64_read(counter, &data);
297 case COUNTER_SCOPE_SIGNAL:
298 err = a->comp.signal_u64_read(counter, a->parent, &data);
300 case COUNTER_SCOPE_COUNT:
301 err = a->comp.count_u64_read(counter, a->parent, &data);
309 return sysfs_emit(buf, "%llu\n", (unsigned long long)data);
312 static ssize_t counter_comp_u64_store(struct device *dev,
313 struct device_attribute *attr,
314 const char *buf, size_t len)
316 const struct counter_attribute *const a = to_counter_attribute(attr);
317 struct counter_device *const counter = counter_from_dev(dev);
321 err = kstrtou64(buf, 0, &data);
326 case COUNTER_SCOPE_DEVICE:
327 err = a->comp.device_u64_write(counter, data);
329 case COUNTER_SCOPE_SIGNAL:
330 err = a->comp.signal_u64_write(counter, a->parent, data);
332 case COUNTER_SCOPE_COUNT:
333 err = a->comp.count_u64_write(counter, a->parent, data);
344 static ssize_t enums_available_show(const u32 *const enums,
345 const size_t num_enums,
346 const char *const strs[], char *buf)
351 for (index = 0; index < num_enums; index++)
352 len += sysfs_emit_at(buf, len, "%s\n", strs[enums[index]]);
357 static ssize_t strs_available_show(const struct counter_available *const avail,
363 for (index = 0; index < avail->num_items; index++)
364 len += sysfs_emit_at(buf, len, "%s\n", avail->strs[index]);
369 static ssize_t counter_comp_available_show(struct device *dev,
370 struct device_attribute *attr,
373 const struct counter_attribute *const a = to_counter_attribute(attr);
374 const struct counter_count *const count = a->parent;
375 const struct counter_synapse *const synapse = a->comp.priv;
376 const struct counter_available *const avail = a->comp.priv;
378 switch (a->comp.type) {
379 case COUNTER_COMP_FUNCTION:
380 return enums_available_show(count->functions_list,
381 count->num_functions,
382 counter_function_str, buf);
383 case COUNTER_COMP_SYNAPSE_ACTION:
384 return enums_available_show(synapse->actions_list,
385 synapse->num_actions,
386 counter_synapse_action_str, buf);
387 case COUNTER_COMP_ENUM:
388 return strs_available_show(avail, buf);
389 case COUNTER_COMP_COUNT_MODE:
390 return enums_available_show(avail->enums, avail->num_items,
391 counter_count_mode_str, buf);
397 static int counter_avail_attr_create(struct device *const dev,
398 struct counter_attribute_group *const group,
399 const struct counter_comp *const comp, void *const parent)
401 struct counter_attribute *counter_attr;
402 struct device_attribute *dev_attr;
404 counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
408 /* Configure Counter attribute */
409 counter_attr->comp.type = comp->type;
410 counter_attr->comp.priv = comp->priv;
411 counter_attr->parent = parent;
413 /* Initialize sysfs attribute */
414 dev_attr = &counter_attr->dev_attr;
415 sysfs_attr_init(&dev_attr->attr);
417 /* Configure device attribute */
418 dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL, "%s_available",
420 if (!dev_attr->attr.name)
422 dev_attr->attr.mode = 0444;
423 dev_attr->show = counter_comp_available_show;
425 /* Store list node */
426 list_add(&counter_attr->l, &group->attr_list);
432 static int counter_attr_create(struct device *const dev,
433 struct counter_attribute_group *const group,
434 const struct counter_comp *const comp,
435 const enum counter_scope scope,
438 struct counter_attribute *counter_attr;
439 struct device_attribute *dev_attr;
441 counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
445 /* Configure Counter attribute */
446 counter_attr->comp = *comp;
447 counter_attr->scope = scope;
448 counter_attr->parent = parent;
450 /* Configure device attribute */
451 dev_attr = &counter_attr->dev_attr;
452 sysfs_attr_init(&dev_attr->attr);
453 dev_attr->attr.name = comp->name;
454 switch (comp->type) {
455 case COUNTER_COMP_U8:
456 case COUNTER_COMP_BOOL:
457 if (comp->device_u8_read) {
458 dev_attr->attr.mode |= 0444;
459 dev_attr->show = counter_comp_u8_show;
461 if (comp->device_u8_write) {
462 dev_attr->attr.mode |= 0200;
463 dev_attr->store = counter_comp_u8_store;
466 case COUNTER_COMP_SIGNAL_LEVEL:
467 case COUNTER_COMP_FUNCTION:
468 case COUNTER_COMP_SYNAPSE_ACTION:
469 case COUNTER_COMP_ENUM:
470 case COUNTER_COMP_COUNT_DIRECTION:
471 case COUNTER_COMP_COUNT_MODE:
472 if (comp->device_u32_read) {
473 dev_attr->attr.mode |= 0444;
474 dev_attr->show = counter_comp_u32_show;
476 if (comp->device_u32_write) {
477 dev_attr->attr.mode |= 0200;
478 dev_attr->store = counter_comp_u32_store;
481 case COUNTER_COMP_U64:
482 if (comp->device_u64_read) {
483 dev_attr->attr.mode |= 0444;
484 dev_attr->show = counter_comp_u64_show;
486 if (comp->device_u64_write) {
487 dev_attr->attr.mode |= 0200;
488 dev_attr->store = counter_comp_u64_store;
495 /* Store list node */
496 list_add(&counter_attr->l, &group->attr_list);
499 /* Create "*_available" attribute if needed */
500 switch (comp->type) {
501 case COUNTER_COMP_FUNCTION:
502 case COUNTER_COMP_SYNAPSE_ACTION:
503 case COUNTER_COMP_ENUM:
504 case COUNTER_COMP_COUNT_MODE:
505 return counter_avail_attr_create(dev, group, comp, parent);
511 static ssize_t counter_comp_name_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 return sysfs_emit(buf, "%s\n", to_counter_attribute(attr)->comp.name);
517 static int counter_name_attr_create(struct device *const dev,
518 struct counter_attribute_group *const group,
519 const char *const name)
521 struct counter_attribute *counter_attr;
523 counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
527 /* Configure Counter attribute */
528 counter_attr->comp.name = name;
530 /* Configure device attribute */
531 sysfs_attr_init(&counter_attr->dev_attr.attr);
532 counter_attr->dev_attr.attr.name = "name";
533 counter_attr->dev_attr.attr.mode = 0444;
534 counter_attr->dev_attr.show = counter_comp_name_show;
536 /* Store list node */
537 list_add(&counter_attr->l, &group->attr_list);
543 static ssize_t counter_comp_id_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 const size_t id = (size_t)to_counter_attribute(attr)->comp.priv;
548 return sysfs_emit(buf, "%zu\n", id);
551 static int counter_comp_id_attr_create(struct device *const dev,
552 struct counter_attribute_group *const group,
553 const char *name, const size_t id)
555 struct counter_attribute *counter_attr;
557 /* Allocate Counter attribute */
558 counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
562 /* Generate component ID name */
563 name = devm_kasprintf(dev, GFP_KERNEL, "%s_component_id", name);
567 /* Configure Counter attribute */
568 counter_attr->comp.priv = (void *)id;
570 /* Configure device attribute */
571 sysfs_attr_init(&counter_attr->dev_attr.attr);
572 counter_attr->dev_attr.attr.name = name;
573 counter_attr->dev_attr.attr.mode = 0444;
574 counter_attr->dev_attr.show = counter_comp_id_show;
576 /* Store list node */
577 list_add(&counter_attr->l, &group->attr_list);
583 static struct counter_comp counter_signal_comp = {
584 .type = COUNTER_COMP_SIGNAL_LEVEL,
588 static int counter_signal_attrs_create(struct counter_device *const counter,
589 struct counter_attribute_group *const cattr_group,
590 struct counter_signal *const signal)
592 const enum counter_scope scope = COUNTER_SCOPE_SIGNAL;
593 struct device *const dev = &counter->dev;
595 struct counter_comp comp;
597 struct counter_comp *ext;
599 /* Create main Signal attribute */
600 comp = counter_signal_comp;
601 comp.signal_u32_read = counter->ops->signal_read;
602 err = counter_attr_create(dev, cattr_group, &comp, scope, signal);
606 /* Create Signal name attribute */
607 err = counter_name_attr_create(dev, cattr_group, signal->name);
611 /* Create an attribute for each extension */
612 for (i = 0; i < signal->num_ext; i++) {
613 ext = &signal->ext[i];
615 err = counter_attr_create(dev, cattr_group, ext, scope, signal);
619 err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
628 static int counter_sysfs_signals_add(struct counter_device *const counter,
629 struct counter_attribute_group *const groups)
634 /* Add each Signal */
635 for (i = 0; i < counter->num_signals; i++) {
636 /* Generate Signal attribute directory name */
637 groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL,
642 /* Create all attributes associated with Signal */
643 err = counter_signal_attrs_create(counter, groups + i,
644 counter->signals + i);
652 static int counter_sysfs_synapses_add(struct counter_device *const counter,
653 struct counter_attribute_group *const group,
654 struct counter_count *const count)
658 /* Add each Synapse */
659 for (i = 0; i < count->num_synapses; i++) {
660 struct device *const dev = &counter->dev;
661 struct counter_synapse *synapse;
663 struct counter_comp comp;
666 synapse = count->synapses + i;
668 /* Generate Synapse action name */
669 id = synapse->signal - counter->signals;
670 comp.name = devm_kasprintf(dev, GFP_KERNEL, "signal%zu_action",
675 /* Create action attribute */
676 comp.type = COUNTER_COMP_SYNAPSE_ACTION;
677 comp.action_read = counter->ops->action_read;
678 comp.action_write = counter->ops->action_write;
680 err = counter_attr_create(dev, group, &comp,
681 COUNTER_SCOPE_COUNT, count);
685 /* Create Synapse component ID attribute */
686 err = counter_comp_id_attr_create(dev, group, comp.name, i);
694 static struct counter_comp counter_count_comp =
695 COUNTER_COMP_COUNT_U64("count", NULL, NULL);
697 static struct counter_comp counter_function_comp = {
698 .type = COUNTER_COMP_FUNCTION,
702 static int counter_count_attrs_create(struct counter_device *const counter,
703 struct counter_attribute_group *const cattr_group,
704 struct counter_count *const count)
706 const enum counter_scope scope = COUNTER_SCOPE_COUNT;
707 struct device *const dev = &counter->dev;
709 struct counter_comp comp;
711 struct counter_comp *ext;
713 /* Create main Count attribute */
714 comp = counter_count_comp;
715 comp.count_u64_read = counter->ops->count_read;
716 comp.count_u64_write = counter->ops->count_write;
717 err = counter_attr_create(dev, cattr_group, &comp, scope, count);
721 /* Create Count name attribute */
722 err = counter_name_attr_create(dev, cattr_group, count->name);
726 /* Create Count function attribute */
727 comp = counter_function_comp;
728 comp.count_u32_read = counter->ops->function_read;
729 comp.count_u32_write = counter->ops->function_write;
730 err = counter_attr_create(dev, cattr_group, &comp, scope, count);
734 /* Create an attribute for each extension */
735 for (i = 0; i < count->num_ext; i++) {
736 ext = &count->ext[i];
738 err = counter_attr_create(dev, cattr_group, ext, scope, count);
742 err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
751 static int counter_sysfs_counts_add(struct counter_device *const counter,
752 struct counter_attribute_group *const groups)
755 struct counter_count *count;
759 for (i = 0; i < counter->num_counts; i++) {
760 count = counter->counts + i;
762 /* Generate Count attribute directory name */
763 groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL,
768 /* Add sysfs attributes of the Synapses */
769 err = counter_sysfs_synapses_add(counter, groups + i, count);
773 /* Create all attributes associated with Count */
774 err = counter_count_attrs_create(counter, groups + i, count);
782 static int counter_num_signals_read(struct counter_device *counter, u8 *val)
784 *val = counter->num_signals;
788 static int counter_num_counts_read(struct counter_device *counter, u8 *val)
790 *val = counter->num_counts;
794 static int counter_events_queue_size_read(struct counter_device *counter,
797 *val = kfifo_size(&counter->events);
801 static int counter_events_queue_size_write(struct counter_device *counter,
804 DECLARE_KFIFO_PTR(events, struct counter_event);
808 /* Allocate new events queue */
809 err = kfifo_alloc(&events, val, GFP_KERNEL);
813 /* Swap in new events queue */
814 mutex_lock(&counter->events_out_lock);
815 spin_lock_irqsave(&counter->events_in_lock, flags);
816 kfifo_free(&counter->events);
817 counter->events.kfifo = events.kfifo;
818 spin_unlock_irqrestore(&counter->events_in_lock, flags);
819 mutex_unlock(&counter->events_out_lock);
824 static struct counter_comp counter_num_signals_comp =
825 COUNTER_COMP_DEVICE_U8("num_signals", counter_num_signals_read, NULL);
827 static struct counter_comp counter_num_counts_comp =
828 COUNTER_COMP_DEVICE_U8("num_counts", counter_num_counts_read, NULL);
830 static struct counter_comp counter_events_queue_size_comp =
831 COUNTER_COMP_DEVICE_U64("events_queue_size",
832 counter_events_queue_size_read,
833 counter_events_queue_size_write);
835 static int counter_sysfs_attr_add(struct counter_device *const counter,
836 struct counter_attribute_group *cattr_group)
838 const enum counter_scope scope = COUNTER_SCOPE_DEVICE;
839 struct device *const dev = &counter->dev;
842 struct counter_comp *ext;
844 /* Add Signals sysfs attributes */
845 err = counter_sysfs_signals_add(counter, cattr_group);
848 cattr_group += counter->num_signals;
850 /* Add Counts sysfs attributes */
851 err = counter_sysfs_counts_add(counter, cattr_group);
854 cattr_group += counter->num_counts;
856 /* Create name attribute */
857 err = counter_name_attr_create(dev, cattr_group, counter->name);
861 /* Create num_signals attribute */
862 err = counter_attr_create(dev, cattr_group, &counter_num_signals_comp,
867 /* Create num_counts attribute */
868 err = counter_attr_create(dev, cattr_group, &counter_num_counts_comp,
873 /* Create events_queue_size attribute */
874 err = counter_attr_create(dev, cattr_group,
875 &counter_events_queue_size_comp, scope, NULL);
879 /* Create an attribute for each extension */
880 for (i = 0; i < counter->num_ext; i++) {
881 ext = &counter->ext[i];
883 err = counter_attr_create(dev, cattr_group, ext, scope, NULL);
887 err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
897 * counter_sysfs_add - Adds Counter sysfs attributes to the device structure
898 * @counter: Pointer to the Counter device structure
900 * Counter sysfs attributes are created and added to the respective device
901 * structure for later registration to the system. Resource-managed memory
902 * allocation is performed by this function, and this memory should be freed
903 * when no longer needed (automatically by a device_unregister call, or
904 * manually by a devres_release_all call).
906 int counter_sysfs_add(struct counter_device *const counter)
908 struct device *const dev = &counter->dev;
909 const size_t num_groups = counter->num_signals + counter->num_counts + 1;
910 struct counter_attribute_group *cattr_groups;
913 struct attribute_group *groups;
914 struct counter_attribute *p;
916 /* Allocate space for attribute groups (signals, counts, and ext) */
917 cattr_groups = devm_kcalloc(dev, num_groups, sizeof(*cattr_groups),
922 /* Initialize attribute lists */
923 for (i = 0; i < num_groups; i++)
924 INIT_LIST_HEAD(&cattr_groups[i].attr_list);
926 /* Add Counter device sysfs attributes */
927 err = counter_sysfs_attr_add(counter, cattr_groups);
931 /* Allocate attribute group pointers for association with device */
932 dev->groups = devm_kcalloc(dev, num_groups + 1, sizeof(*dev->groups),
937 /* Allocate space for attribute groups */
938 groups = devm_kcalloc(dev, num_groups, sizeof(*groups), GFP_KERNEL);
942 /* Prepare each group of attributes for association */
943 for (i = 0; i < num_groups; i++) {
944 groups[i].name = cattr_groups[i].name;
946 /* Allocate space for attribute pointers */
947 groups[i].attrs = devm_kcalloc(dev,
948 cattr_groups[i].num_attr + 1,
949 sizeof(*groups[i].attrs),
951 if (!groups[i].attrs)
954 /* Add attribute pointers to attribute group */
956 list_for_each_entry(p, &cattr_groups[i].attr_list, l)
957 groups[i].attrs[j++] = &p->dev_attr.attr;
959 /* Associate attribute group */
960 dev->groups[i] = &groups[i];