1*08609989SWilliam Breathitt Gray // SPDX-License-Identifier: GPL-2.0-only
2*08609989SWilliam Breathitt Gray /* Counter - example userspace application
3*08609989SWilliam Breathitt Gray *
4*08609989SWilliam Breathitt Gray * The userspace application opens /dev/counter0, configures the
5*08609989SWilliam Breathitt Gray * COUNTER_EVENT_INDEX event channel 0 to gather Count 0 count and Count
6*08609989SWilliam Breathitt Gray * 1 count, and prints out the data as it becomes available on the
7*08609989SWilliam Breathitt Gray * character device node.
8*08609989SWilliam Breathitt Gray *
9*08609989SWilliam Breathitt Gray * Copyright (C) 2021 William Breathitt Gray
10*08609989SWilliam Breathitt Gray */
11*08609989SWilliam Breathitt Gray #include <errno.h>
12*08609989SWilliam Breathitt Gray #include <fcntl.h>
13*08609989SWilliam Breathitt Gray #include <linux/counter.h>
14*08609989SWilliam Breathitt Gray #include <stdio.h>
15*08609989SWilliam Breathitt Gray #include <string.h>
16*08609989SWilliam Breathitt Gray #include <sys/ioctl.h>
17*08609989SWilliam Breathitt Gray #include <unistd.h>
18*08609989SWilliam Breathitt Gray
19*08609989SWilliam Breathitt Gray static struct counter_watch watches[2] = {
20*08609989SWilliam Breathitt Gray {
21*08609989SWilliam Breathitt Gray /* Component data: Count 0 count */
22*08609989SWilliam Breathitt Gray .component.type = COUNTER_COMPONENT_COUNT,
23*08609989SWilliam Breathitt Gray .component.scope = COUNTER_SCOPE_COUNT,
24*08609989SWilliam Breathitt Gray .component.parent = 0,
25*08609989SWilliam Breathitt Gray /* Event type: Index */
26*08609989SWilliam Breathitt Gray .event = COUNTER_EVENT_INDEX,
27*08609989SWilliam Breathitt Gray /* Device event channel 0 */
28*08609989SWilliam Breathitt Gray .channel = 0,
29*08609989SWilliam Breathitt Gray },
30*08609989SWilliam Breathitt Gray {
31*08609989SWilliam Breathitt Gray /* Component data: Count 1 count */
32*08609989SWilliam Breathitt Gray .component.type = COUNTER_COMPONENT_COUNT,
33*08609989SWilliam Breathitt Gray .component.scope = COUNTER_SCOPE_COUNT,
34*08609989SWilliam Breathitt Gray .component.parent = 1,
35*08609989SWilliam Breathitt Gray /* Event type: Index */
36*08609989SWilliam Breathitt Gray .event = COUNTER_EVENT_INDEX,
37*08609989SWilliam Breathitt Gray /* Device event channel 0 */
38*08609989SWilliam Breathitt Gray .channel = 0,
39*08609989SWilliam Breathitt Gray },
40*08609989SWilliam Breathitt Gray };
41*08609989SWilliam Breathitt Gray
main(void)42*08609989SWilliam Breathitt Gray int main(void)
43*08609989SWilliam Breathitt Gray {
44*08609989SWilliam Breathitt Gray int fd;
45*08609989SWilliam Breathitt Gray int ret;
46*08609989SWilliam Breathitt Gray int i;
47*08609989SWilliam Breathitt Gray struct counter_event event_data[2];
48*08609989SWilliam Breathitt Gray
49*08609989SWilliam Breathitt Gray fd = open("/dev/counter0", O_RDWR);
50*08609989SWilliam Breathitt Gray if (fd == -1) {
51*08609989SWilliam Breathitt Gray perror("Unable to open /dev/counter0");
52*08609989SWilliam Breathitt Gray return 1;
53*08609989SWilliam Breathitt Gray }
54*08609989SWilliam Breathitt Gray
55*08609989SWilliam Breathitt Gray for (i = 0; i < 2; i++) {
56*08609989SWilliam Breathitt Gray ret = ioctl(fd, COUNTER_ADD_WATCH_IOCTL, watches + i);
57*08609989SWilliam Breathitt Gray if (ret == -1) {
58*08609989SWilliam Breathitt Gray fprintf(stderr, "Error adding watches[%d]: %s\n", i,
59*08609989SWilliam Breathitt Gray strerror(errno));
60*08609989SWilliam Breathitt Gray return 1;
61*08609989SWilliam Breathitt Gray }
62*08609989SWilliam Breathitt Gray }
63*08609989SWilliam Breathitt Gray ret = ioctl(fd, COUNTER_ENABLE_EVENTS_IOCTL);
64*08609989SWilliam Breathitt Gray if (ret == -1) {
65*08609989SWilliam Breathitt Gray perror("Error enabling events");
66*08609989SWilliam Breathitt Gray return 1;
67*08609989SWilliam Breathitt Gray }
68*08609989SWilliam Breathitt Gray
69*08609989SWilliam Breathitt Gray for (;;) {
70*08609989SWilliam Breathitt Gray ret = read(fd, event_data, sizeof(event_data));
71*08609989SWilliam Breathitt Gray if (ret == -1) {
72*08609989SWilliam Breathitt Gray perror("Failed to read event data");
73*08609989SWilliam Breathitt Gray return 1;
74*08609989SWilliam Breathitt Gray }
75*08609989SWilliam Breathitt Gray
76*08609989SWilliam Breathitt Gray if (ret != sizeof(event_data)) {
77*08609989SWilliam Breathitt Gray fprintf(stderr, "Failed to read event data\n");
78*08609989SWilliam Breathitt Gray return -EIO;
79*08609989SWilliam Breathitt Gray }
80*08609989SWilliam Breathitt Gray
81*08609989SWilliam Breathitt Gray printf("Timestamp 0: %llu\tCount 0: %llu\n"
82*08609989SWilliam Breathitt Gray "Error Message 0: %s\n"
83*08609989SWilliam Breathitt Gray "Timestamp 1: %llu\tCount 1: %llu\n"
84*08609989SWilliam Breathitt Gray "Error Message 1: %s\n",
85*08609989SWilliam Breathitt Gray event_data[0].timestamp, event_data[0].value,
86*08609989SWilliam Breathitt Gray strerror(event_data[0].status),
87*08609989SWilliam Breathitt Gray event_data[1].timestamp, event_data[1].value,
88*08609989SWilliam Breathitt Gray strerror(event_data[1].status));
89*08609989SWilliam Breathitt Gray }
90*08609989SWilliam Breathitt Gray
91*08609989SWilliam Breathitt Gray return 0;
92*08609989SWilliam Breathitt Gray }
93