1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
|
// SPDX-License-Identifier: GPL-2.0
/*
* Generic Counter sysfs interface
* Copyright (C) 2020 William Breathitt Gray
*/
#include <linux/counter.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/gfp.h>
#include <linux/kernel.h>
#include <linux/kfifo.h>
#include <linux/kstrtox.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/types.h>
#include "counter-sysfs.h"
/**
* struct counter_attribute - Counter sysfs attribute
* @dev_attr: device attribute for sysfs
* @l: node to add Counter attribute to attribute group list
* @comp: Counter component callbacks and data
* @scope: Counter scope of the attribute
* @parent: pointer to the parent component
*/
struct counter_attribute {
struct device_attribute dev_attr;
struct list_head l;
struct counter_comp comp;
enum counter_scope scope;
void *parent;
};
#define to_counter_attribute(_dev_attr) \
container_of(_dev_attr, struct counter_attribute, dev_attr)
/**
* struct counter_attribute_group - container for attribute group
* @name: name of the attribute group
* @attr_list: list to keep track of created attributes
* @num_attr: number of attributes
*/
struct counter_attribute_group {
const char *name;
struct list_head attr_list;
size_t num_attr;
};
static const char *const counter_function_str[] = {
[COUNTER_FUNCTION_INCREASE] = "increase",
[COUNTER_FUNCTION_DECREASE] = "decrease",
[COUNTER_FUNCTION_PULSE_DIRECTION] = "pulse-direction",
[COUNTER_FUNCTION_QUADRATURE_X1_A] = "quadrature x1 a",
[COUNTER_FUNCTION_QUADRATURE_X1_B] = "quadrature x1 b",
[COUNTER_FUNCTION_QUADRATURE_X2_A] = "quadrature x2 a",
[COUNTER_FUNCTION_QUADRATURE_X2_B] = "quadrature x2 b",
[COUNTER_FUNCTION_QUADRATURE_X4] = "quadrature x4"
};
static const char *const counter_signal_value_str[] = {
[COUNTER_SIGNAL_LEVEL_LOW] = "low",
[COUNTER_SIGNAL_LEVEL_HIGH] = "high"
};
static const char *const counter_synapse_action_str[] = {
[COUNTER_SYNAPSE_ACTION_NONE] = "none",
[COUNTER_SYNAPSE_ACTION_RISING_EDGE] = "rising edge",
[COUNTER_SYNAPSE_ACTION_FALLING_EDGE] = "falling edge",
[COUNTER_SYNAPSE_ACTION_BOTH_EDGES] = "both edges"
};
static const char *const counter_count_direction_str[] = {
[COUNTER_COUNT_DIRECTION_FORWARD] = "forward",
[COUNTER_COUNT_DIRECTION_BACKWARD] = "backward"
};
static const char *const counter_count_mode_str[] = {
[COUNTER_COUNT_MODE_NORMAL] = "normal",
[COUNTER_COUNT_MODE_RANGE_LIMIT] = "range limit",
[COUNTER_COUNT_MODE_NON_RECYCLE] = "non-recycle",
[COUNTER_COUNT_MODE_MODULO_N] = "modulo-n"
};
static ssize_t counter_comp_u8_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
int err;
u8 data = 0;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u8_read(counter, &data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u8_read(counter, a->parent, &data);
break;
case COUNTER_SCOPE_COUNT:
err = a->comp.count_u8_read(counter, a->parent, &data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
if (a->comp.type == COUNTER_COMP_BOOL)
/* data should already be boolean but ensure just to be safe */
data = !!data;
return sysfs_emit(buf, "%u\n", (unsigned int)data);
}
static ssize_t counter_comp_u8_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
int err;
bool bool_data = 0;
u8 data = 0;
if (a->comp.type == COUNTER_COMP_BOOL) {
err = kstrtobool(buf, &bool_data);
data = bool_data;
} else
err = kstrtou8(buf, 0, &data);
if (err < 0)
return err;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u8_write(counter, data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u8_write(counter, a->parent, data);
break;
case COUNTER_SCOPE_COUNT:
err = a->comp.count_u8_write(counter, a->parent, data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
return len;
}
static ssize_t counter_comp_u32_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
const struct counter_available *const avail = a->comp.priv;
int err;
u32 data = 0;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u32_read(counter, &data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u32_read(counter, a->parent, &data);
break;
case COUNTER_SCOPE_COUNT:
if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION)
err = a->comp.action_read(counter, a->parent,
a->comp.priv, &data);
else
err = a->comp.count_u32_read(counter, a->parent, &data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
switch (a->comp.type) {
case COUNTER_COMP_FUNCTION:
return sysfs_emit(buf, "%s\n", counter_function_str[data]);
case COUNTER_COMP_SIGNAL_LEVEL:
return sysfs_emit(buf, "%s\n", counter_signal_value_str[data]);
case COUNTER_COMP_SYNAPSE_ACTION:
return sysfs_emit(buf, "%s\n", counter_synapse_action_str[data]);
case COUNTER_COMP_ENUM:
return sysfs_emit(buf, "%s\n", avail->strs[data]);
case COUNTER_COMP_COUNT_DIRECTION:
return sysfs_emit(buf, "%s\n", counter_count_direction_str[data]);
case COUNTER_COMP_COUNT_MODE:
return sysfs_emit(buf, "%s\n", counter_count_mode_str[data]);
default:
return sysfs_emit(buf, "%u\n", (unsigned int)data);
}
}
static int counter_find_enum(u32 *const enum_item, const u32 *const enums,
const size_t num_enums, const char *const buf,
const char *const string_array[])
{
size_t index;
for (index = 0; index < num_enums; index++) {
*enum_item = enums[index];
if (sysfs_streq(buf, string_array[*enum_item]))
return 0;
}
return -EINVAL;
}
static ssize_t counter_comp_u32_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
struct counter_count *const count = a->parent;
struct counter_synapse *const synapse = a->comp.priv;
const struct counter_available *const avail = a->comp.priv;
int err;
u32 data = 0;
switch (a->comp.type) {
case COUNTER_COMP_FUNCTION:
err = counter_find_enum(&data, count->functions_list,
count->num_functions, buf,
counter_function_str);
break;
case COUNTER_COMP_SYNAPSE_ACTION:
err = counter_find_enum(&data, synapse->actions_list,
synapse->num_actions, buf,
counter_synapse_action_str);
break;
case COUNTER_COMP_ENUM:
err = __sysfs_match_string(avail->strs, avail->num_items, buf);
data = err;
break;
case COUNTER_COMP_COUNT_MODE:
err = counter_find_enum(&data, avail->enums, avail->num_items,
buf, counter_count_mode_str);
break;
default:
err = kstrtou32(buf, 0, &data);
break;
}
if (err < 0)
return err;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u32_write(counter, data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u32_write(counter, a->parent, data);
break;
case COUNTER_SCOPE_COUNT:
if (a->comp.type == COUNTER_COMP_SYNAPSE_ACTION)
err = a->comp.action_write(counter, count, synapse,
data);
else
err = a->comp.count_u32_write(counter, count, data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
return len;
}
static ssize_t counter_comp_u64_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
int err;
u64 data = 0;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u64_read(counter, &data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u64_read(counter, a->parent, &data);
break;
case COUNTER_SCOPE_COUNT:
err = a->comp.count_u64_read(counter, a->parent, &data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
return sysfs_emit(buf, "%llu\n", (unsigned long long)data);
}
static ssize_t counter_comp_u64_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t len)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
struct counter_device *const counter = dev_get_drvdata(dev);
int err;
u64 data = 0;
err = kstrtou64(buf, 0, &data);
if (err < 0)
return err;
switch (a->scope) {
case COUNTER_SCOPE_DEVICE:
err = a->comp.device_u64_write(counter, data);
break;
case COUNTER_SCOPE_SIGNAL:
err = a->comp.signal_u64_write(counter, a->parent, data);
break;
case COUNTER_SCOPE_COUNT:
err = a->comp.count_u64_write(counter, a->parent, data);
break;
default:
return -EINVAL;
}
if (err < 0)
return err;
return len;
}
static ssize_t enums_available_show(const u32 *const enums,
const size_t num_enums,
const char *const strs[], char *buf)
{
size_t len = 0;
size_t index;
for (index = 0; index < num_enums; index++)
len += sysfs_emit_at(buf, len, "%s\n", strs[enums[index]]);
return len;
}
static ssize_t strs_available_show(const struct counter_available *const avail,
char *buf)
{
size_t len = 0;
size_t index;
for (index = 0; index < avail->num_items; index++)
len += sysfs_emit_at(buf, len, "%s\n", avail->strs[index]);
return len;
}
static ssize_t counter_comp_available_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
const struct counter_attribute *const a = to_counter_attribute(attr);
const struct counter_count *const count = a->parent;
const struct counter_synapse *const synapse = a->comp.priv;
const struct counter_available *const avail = a->comp.priv;
switch (a->comp.type) {
case COUNTER_COMP_FUNCTION:
return enums_available_show(count->functions_list,
count->num_functions,
counter_function_str, buf);
case COUNTER_COMP_SYNAPSE_ACTION:
return enums_available_show(synapse->actions_list,
synapse->num_actions,
counter_synapse_action_str, buf);
case COUNTER_COMP_ENUM:
return strs_available_show(avail, buf);
case COUNTER_COMP_COUNT_MODE:
return enums_available_show(avail->enums, avail->num_items,
counter_count_mode_str, buf);
default:
return -EINVAL;
}
}
static int counter_avail_attr_create(struct device *const dev,
struct counter_attribute_group *const group,
const struct counter_comp *const comp, void *const parent)
{
struct counter_attribute *counter_attr;
struct device_attribute *dev_attr;
counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
if (!counter_attr)
return -ENOMEM;
/* Configure Counter attribute */
counter_attr->comp.type = comp->type;
counter_attr->comp.priv = comp->priv;
counter_attr->parent = parent;
/* Initialize sysfs attribute */
dev_attr = &counter_attr->dev_attr;
sysfs_attr_init(&dev_attr->attr);
/* Configure device attribute */
dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL, "%s_available",
comp->name);
if (!dev_attr->attr.name)
return -ENOMEM;
dev_attr->attr.mode = 0444;
dev_attr->show = counter_comp_available_show;
/* Store list node */
list_add(&counter_attr->l, &group->attr_list);
group->num_attr++;
return 0;
}
static int counter_attr_create(struct device *const dev,
struct counter_attribute_group *const group,
const struct counter_comp *const comp,
const enum counter_scope scope,
void *const parent)
{
struct counter_attribute *counter_attr;
struct device_attribute *dev_attr;
counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
if (!counter_attr)
return -ENOMEM;
/* Configure Counter attribute */
counter_attr->comp = *comp;
counter_attr->scope = scope;
counter_attr->parent = parent;
/* Configure device attribute */
dev_attr = &counter_attr->dev_attr;
sysfs_attr_init(&dev_attr->attr);
dev_attr->attr.name = comp->name;
switch (comp->type) {
case COUNTER_COMP_U8:
case COUNTER_COMP_BOOL:
if (comp->device_u8_read) {
dev_attr->attr.mode |= 0444;
dev_attr->show = counter_comp_u8_show;
}
if (comp->device_u8_write) {
dev_attr->attr.mode |= 0200;
dev_attr->store = counter_comp_u8_store;
}
break;
case COUNTER_COMP_SIGNAL_LEVEL:
case COUNTER_COMP_FUNCTION:
case COUNTER_COMP_SYNAPSE_ACTION:
case COUNTER_COMP_ENUM:
case COUNTER_COMP_COUNT_DIRECTION:
case COUNTER_COMP_COUNT_MODE:
if (comp->device_u32_read) {
dev_attr->attr.mode |= 0444;
dev_attr->show = counter_comp_u32_show;
}
if (comp->device_u32_write) {
dev_attr->attr.mode |= 0200;
dev_attr->store = counter_comp_u32_store;
}
break;
case COUNTER_COMP_U64:
if (comp->device_u64_read) {
dev_attr->attr.mode |= 0444;
dev_attr->show = counter_comp_u64_show;
}
if (comp->device_u64_write) {
dev_attr->attr.mode |= 0200;
dev_attr->store = counter_comp_u64_store;
}
break;
default:
return -EINVAL;
}
/* Store list node */
list_add(&counter_attr->l, &group->attr_list);
group->num_attr++;
/* Create "*_available" attribute if needed */
switch (comp->type) {
case COUNTER_COMP_FUNCTION:
case COUNTER_COMP_SYNAPSE_ACTION:
case COUNTER_COMP_ENUM:
case COUNTER_COMP_COUNT_MODE:
return counter_avail_attr_create(dev, group, comp, parent);
default:
return 0;
}
}
static ssize_t counter_comp_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sysfs_emit(buf, "%s\n", to_counter_attribute(attr)->comp.name);
}
static int counter_name_attr_create(struct device *const dev,
struct counter_attribute_group *const group,
const char *const name)
{
struct counter_attribute *counter_attr;
counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
if (!counter_attr)
return -ENOMEM;
/* Configure Counter attribute */
counter_attr->comp.name = name;
/* Configure device attribute */
sysfs_attr_init(&counter_attr->dev_attr.attr);
counter_attr->dev_attr.attr.name = "name";
counter_attr->dev_attr.attr.mode = 0444;
counter_attr->dev_attr.show = counter_comp_name_show;
/* Store list node */
list_add(&counter_attr->l, &group->attr_list);
group->num_attr++;
return 0;
}
static ssize_t counter_comp_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
const size_t id = (size_t)to_counter_attribute(attr)->comp.priv;
return sysfs_emit(buf, "%zu\n", id);
}
static int counter_comp_id_attr_create(struct device *const dev,
struct counter_attribute_group *const group,
const char *name, const size_t id)
{
struct counter_attribute *counter_attr;
/* Allocate Counter attribute */
counter_attr = devm_kzalloc(dev, sizeof(*counter_attr), GFP_KERNEL);
if (!counter_attr)
return -ENOMEM;
/* Generate component ID name */
name = devm_kasprintf(dev, GFP_KERNEL, "%s_component_id", name);
if (!name)
return -ENOMEM;
/* Configure Counter attribute */
counter_attr->comp.priv = (void *)id;
/* Configure device attribute */
sysfs_attr_init(&counter_attr->dev_attr.attr);
counter_attr->dev_attr.attr.name = name;
counter_attr->dev_attr.attr.mode = 0444;
counter_attr->dev_attr.show = counter_comp_id_show;
/* Store list node */
list_add(&counter_attr->l, &group->attr_list);
group->num_attr++;
return 0;
}
static struct counter_comp counter_signal_comp = {
.type = COUNTER_COMP_SIGNAL_LEVEL,
.name = "signal",
};
static int counter_signal_attrs_create(struct counter_device *const counter,
struct counter_attribute_group *const cattr_group,
struct counter_signal *const signal)
{
const enum counter_scope scope = COUNTER_SCOPE_SIGNAL;
struct device *const dev = &counter->dev;
int err;
struct counter_comp comp;
size_t i;
struct counter_comp *ext;
/* Create main Signal attribute */
comp = counter_signal_comp;
comp.signal_u32_read = counter->ops->signal_read;
err = counter_attr_create(dev, cattr_group, &comp, scope, signal);
if (err < 0)
return err;
/* Create Signal name attribute */
err = counter_name_attr_create(dev, cattr_group, signal->name);
if (err < 0)
return err;
/* Create an attribute for each extension */
for (i = 0; i < signal->num_ext; i++) {
ext = &signal->ext[i];
err = counter_attr_create(dev, cattr_group, ext, scope, signal);
if (err < 0)
return err;
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
i);
if (err < 0)
return err;
}
return 0;
}
static int counter_sysfs_signals_add(struct counter_device *const counter,
struct counter_attribute_group *const groups)
{
size_t i;
int err;
/* Add each Signal */
for (i = 0; i < counter->num_signals; i++) {
/* Generate Signal attribute directory name */
groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL,
"signal%zu", i);
if (!groups[i].name)
return -ENOMEM;
/* Create all attributes associated with Signal */
err = counter_signal_attrs_create(counter, groups + i,
counter->signals + i);
if (err < 0)
return err;
}
return 0;
}
static int counter_sysfs_synapses_add(struct counter_device *const counter,
struct counter_attribute_group *const group,
struct counter_count *const count)
{
size_t i;
/* Add each Synapse */
for (i = 0; i < count->num_synapses; i++) {
struct device *const dev = &counter->dev;
struct counter_synapse *synapse;
size_t id;
struct counter_comp comp;
int err;
synapse = count->synapses + i;
/* Generate Synapse action name */
id = synapse->signal - counter->signals;
comp.name = devm_kasprintf(dev, GFP_KERNEL, "signal%zu_action",
id);
if (!comp.name)
return -ENOMEM;
/* Create action attribute */
comp.type = COUNTER_COMP_SYNAPSE_ACTION;
comp.action_read = counter->ops->action_read;
comp.action_write = counter->ops->action_write;
comp.priv = synapse;
err = counter_attr_create(dev, group, &comp,
COUNTER_SCOPE_COUNT, count);
if (err < 0)
return err;
/* Create Synapse component ID attribute */
err = counter_comp_id_attr_create(dev, group, comp.name, i);
if (err < 0)
return err;
}
return 0;
}
static struct counter_comp counter_count_comp =
COUNTER_COMP_COUNT_U64("count", NULL, NULL);
static struct counter_comp counter_function_comp = {
.type = COUNTER_COMP_FUNCTION,
.name = "function",
};
static int counter_count_attrs_create(struct counter_device *const counter,
struct counter_attribute_group *const cattr_group,
struct counter_count *const count)
{
const enum counter_scope scope = COUNTER_SCOPE_COUNT;
struct device *const dev = &counter->dev;
int err;
struct counter_comp comp;
size_t i;
struct counter_comp *ext;
/* Create main Count attribute */
comp = counter_count_comp;
comp.count_u64_read = counter->ops->count_read;
comp.count_u64_write = counter->ops->count_write;
err = counter_attr_create(dev, cattr_group, &comp, scope, count);
if (err < 0)
return err;
/* Create Count name attribute */
err = counter_name_attr_create(dev, cattr_group, count->name);
if (err < 0)
return err;
/* Create Count function attribute */
comp = counter_function_comp;
comp.count_u32_read = counter->ops->function_read;
comp.count_u32_write = counter->ops->function_write;
err = counter_attr_create(dev, cattr_group, &comp, scope, count);
if (err < 0)
return err;
/* Create an attribute for each extension */
for (i = 0; i < count->num_ext; i++) {
ext = &count->ext[i];
err = counter_attr_create(dev, cattr_group, ext, scope, count);
if (err < 0)
return err;
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
i);
if (err < 0)
return err;
}
return 0;
}
static int counter_sysfs_counts_add(struct counter_device *const counter,
struct counter_attribute_group *const groups)
{
size_t i;
struct counter_count *count;
int err;
/* Add each Count */
for (i = 0; i < counter->num_counts; i++) {
count = counter->counts + i;
/* Generate Count attribute directory name */
groups[i].name = devm_kasprintf(&counter->dev, GFP_KERNEL,
"count%zu", i);
if (!groups[i].name)
return -ENOMEM;
/* Add sysfs attributes of the Synapses */
err = counter_sysfs_synapses_add(counter, groups + i, count);
if (err < 0)
return err;
/* Create all attributes associated with Count */
err = counter_count_attrs_create(counter, groups + i, count);
if (err < 0)
return err;
}
return 0;
}
static int counter_num_signals_read(struct counter_device *counter, u8 *val)
{
*val = counter->num_signals;
return 0;
}
static int counter_num_counts_read(struct counter_device *counter, u8 *val)
{
*val = counter->num_counts;
return 0;
}
static int counter_events_queue_size_read(struct counter_device *counter,
u64 *val)
{
*val = kfifo_size(&counter->events);
return 0;
}
static int counter_events_queue_size_write(struct counter_device *counter,
u64 val)
{
DECLARE_KFIFO_PTR(events, struct counter_event);
int err;
unsigned long flags;
/* Allocate new events queue */
err = kfifo_alloc(&events, val, GFP_KERNEL);
if (err)
return err;
/* Swap in new events queue */
mutex_lock(&counter->events_out_lock);
spin_lock_irqsave(&counter->events_in_lock, flags);
kfifo_free(&counter->events);
counter->events.kfifo = events.kfifo;
spin_unlock_irqrestore(&counter->events_in_lock, flags);
mutex_unlock(&counter->events_out_lock);
return 0;
}
static struct counter_comp counter_num_signals_comp =
COUNTER_COMP_DEVICE_U8("num_signals", counter_num_signals_read, NULL);
static struct counter_comp counter_num_counts_comp =
COUNTER_COMP_DEVICE_U8("num_counts", counter_num_counts_read, NULL);
static struct counter_comp counter_events_queue_size_comp =
COUNTER_COMP_DEVICE_U64("events_queue_size",
counter_events_queue_size_read,
counter_events_queue_size_write);
static int counter_sysfs_attr_add(struct counter_device *const counter,
struct counter_attribute_group *cattr_group)
{
const enum counter_scope scope = COUNTER_SCOPE_DEVICE;
struct device *const dev = &counter->dev;
int err;
size_t i;
struct counter_comp *ext;
/* Add Signals sysfs attributes */
err = counter_sysfs_signals_add(counter, cattr_group);
if (err < 0)
return err;
cattr_group += counter->num_signals;
/* Add Counts sysfs attributes */
err = counter_sysfs_counts_add(counter, cattr_group);
if (err < 0)
return err;
cattr_group += counter->num_counts;
/* Create name attribute */
err = counter_name_attr_create(dev, cattr_group, counter->name);
if (err < 0)
return err;
/* Create num_signals attribute */
err = counter_attr_create(dev, cattr_group, &counter_num_signals_comp,
scope, NULL);
if (err < 0)
return err;
/* Create num_counts attribute */
err = counter_attr_create(dev, cattr_group, &counter_num_counts_comp,
scope, NULL);
if (err < 0)
return err;
/* Create events_queue_size attribute */
err = counter_attr_create(dev, cattr_group,
&counter_events_queue_size_comp, scope, NULL);
if (err < 0)
return err;
/* Create an attribute for each extension */
for (i = 0; i < counter->num_ext; i++) {
ext = &counter->ext[i];
err = counter_attr_create(dev, cattr_group, ext, scope, NULL);
if (err < 0)
return err;
err = counter_comp_id_attr_create(dev, cattr_group, ext->name,
i);
if (err < 0)
return err;
}
return 0;
}
/**
* counter_sysfs_add - Adds Counter sysfs attributes to the device structure
* @counter: Pointer to the Counter device structure
*
* Counter sysfs attributes are created and added to the respective device
* structure for later registration to the system. Resource-managed memory
* allocation is performed by this function, and this memory should be freed
* when no longer needed (automatically by a device_unregister call, or
* manually by a devres_release_all call).
*/
int counter_sysfs_add(struct counter_device *const counter)
{
struct device *const dev = &counter->dev;
const size_t num_groups = counter->num_signals + counter->num_counts + 1;
struct counter_attribute_group *cattr_groups;
size_t i, j;
int err;
struct attribute_group *groups;
struct counter_attribute *p;
/* Allocate space for attribute groups (signals, counts, and ext) */
cattr_groups = devm_kcalloc(dev, num_groups, sizeof(*cattr_groups),
GFP_KERNEL);
if (!cattr_groups)
return -ENOMEM;
/* Initialize attribute lists */
for (i = 0; i < num_groups; i++)
INIT_LIST_HEAD(&cattr_groups[i].attr_list);
/* Add Counter device sysfs attributes */
err = counter_sysfs_attr_add(counter, cattr_groups);
if (err < 0)
return err;
/* Allocate attribute group pointers for association with device */
dev->groups = devm_kcalloc(dev, num_groups + 1, sizeof(*dev->groups),
GFP_KERNEL);
if (!dev->groups)
return -ENOMEM;
/* Allocate space for attribute groups */
groups = devm_kcalloc(dev, num_groups, sizeof(*groups), GFP_KERNEL);
if (!groups)
return -ENOMEM;
/* Prepare each group of attributes for association */
for (i = 0; i < num_groups; i++) {
groups[i].name = cattr_groups[i].name;
/* Allocate space for attribute pointers */
groups[i].attrs = devm_kcalloc(dev,
cattr_groups[i].num_attr + 1,
sizeof(*groups[i].attrs),
GFP_KERNEL);
if (!groups[i].attrs)
return -ENOMEM;
/* Add attribute pointers to attribute group */
j = 0;
list_for_each_entry(p, &cattr_groups[i].attr_list, l)
groups[i].attrs[j++] = &p->dev_attr.attr;
/* Associate attribute group */
dev->groups[i] = &groups[i];
}
return 0;
}
|