Merge branch 'gma500-fixes' of git://github.com/patjak/drm-gma500 into drm-fixes
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / Documentation / iio_event_monitor.c
1 /* Industrialio event test code.
2  *
3  * Copyright (c) 2011-2012 Lars-Peter Clausen <lars@metafoo.de>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is primarily intended as an example application.
10  * Reads the current buffer setup from sysfs and starts a short capture
11  * from the specified device, pretty printing the result after appropriate
12  * conversion.
13  *
14  * Usage:
15  *      iio_event_monitor <device_name>
16  *
17  */
18
19 #define _GNU_SOURCE
20
21 #include <unistd.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <poll.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include "iio_utils.h"
30 #include <linux/iio/events.h>
31
32 static const char * const iio_chan_type_name_spec[] = {
33         [IIO_VOLTAGE] = "voltage",
34         [IIO_CURRENT] = "current",
35         [IIO_POWER] = "power",
36         [IIO_ACCEL] = "accel",
37         [IIO_ANGL_VEL] = "anglvel",
38         [IIO_MAGN] = "magn",
39         [IIO_LIGHT] = "illuminance",
40         [IIO_INTENSITY] = "intensity",
41         [IIO_PROXIMITY] = "proximity",
42         [IIO_TEMP] = "temp",
43         [IIO_INCLI] = "incli",
44         [IIO_ROT] = "rot",
45         [IIO_ANGL] = "angl",
46         [IIO_TIMESTAMP] = "timestamp",
47         [IIO_CAPACITANCE] = "capacitance",
48         [IIO_ALTVOLTAGE] = "altvoltage",
49 };
50
51 static const char * const iio_ev_type_text[] = {
52         [IIO_EV_TYPE_THRESH] = "thresh",
53         [IIO_EV_TYPE_MAG] = "mag",
54         [IIO_EV_TYPE_ROC] = "roc",
55         [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
56         [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
57 };
58
59 static const char * const iio_ev_dir_text[] = {
60         [IIO_EV_DIR_EITHER] = "either",
61         [IIO_EV_DIR_RISING] = "rising",
62         [IIO_EV_DIR_FALLING] = "falling"
63 };
64
65 static const char * const iio_modifier_names[] = {
66         [IIO_MOD_X] = "x",
67         [IIO_MOD_Y] = "y",
68         [IIO_MOD_Z] = "z",
69         [IIO_MOD_LIGHT_BOTH] = "both",
70         [IIO_MOD_LIGHT_IR] = "ir",
71         [IIO_MOD_ROOT_SUM_SQUARED_X_Y] = "sqrt(x^2+y^2)",
72         [IIO_MOD_SUM_SQUARED_X_Y_Z] = "x^2+y^2+z^2",
73         [IIO_MOD_LIGHT_CLEAR] = "clear",
74         [IIO_MOD_LIGHT_RED] = "red",
75         [IIO_MOD_LIGHT_GREEN] = "green",
76         [IIO_MOD_LIGHT_BLUE] = "blue",
77 };
78
79 static bool event_is_known(struct iio_event_data *event)
80 {
81         enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
82         enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
83         enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
84         enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
85
86         switch (type) {
87         case IIO_VOLTAGE:
88         case IIO_CURRENT:
89         case IIO_POWER:
90         case IIO_ACCEL:
91         case IIO_ANGL_VEL:
92         case IIO_MAGN:
93         case IIO_LIGHT:
94         case IIO_INTENSITY:
95         case IIO_PROXIMITY:
96         case IIO_TEMP:
97         case IIO_INCLI:
98         case IIO_ROT:
99         case IIO_ANGL:
100         case IIO_TIMESTAMP:
101         case IIO_CAPACITANCE:
102         case IIO_ALTVOLTAGE:
103                 break;
104         default:
105                 return false;
106         }
107
108         switch (mod) {
109         case IIO_NO_MOD:
110         case IIO_MOD_X:
111         case IIO_MOD_Y:
112         case IIO_MOD_Z:
113         case IIO_MOD_LIGHT_BOTH:
114         case IIO_MOD_LIGHT_IR:
115         case IIO_MOD_ROOT_SUM_SQUARED_X_Y:
116         case IIO_MOD_SUM_SQUARED_X_Y_Z:
117         case IIO_MOD_LIGHT_CLEAR:
118         case IIO_MOD_LIGHT_RED:
119         case IIO_MOD_LIGHT_GREEN:
120         case IIO_MOD_LIGHT_BLUE:
121                 break;
122         default:
123                 return false;
124         }
125
126         switch (ev_type) {
127         case IIO_EV_TYPE_THRESH:
128         case IIO_EV_TYPE_MAG:
129         case IIO_EV_TYPE_ROC:
130         case IIO_EV_TYPE_THRESH_ADAPTIVE:
131         case IIO_EV_TYPE_MAG_ADAPTIVE:
132                 break;
133         default:
134                 return false;
135         }
136
137         switch (dir) {
138         case IIO_EV_DIR_EITHER:
139         case IIO_EV_DIR_RISING:
140         case IIO_EV_DIR_FALLING:
141                 break;
142         default:
143                 return false;
144         }
145
146         return true;
147 }
148
149 static void print_event(struct iio_event_data *event)
150 {
151         enum iio_chan_type type = IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(event->id);
152         enum iio_modifier mod = IIO_EVENT_CODE_EXTRACT_MODIFIER(event->id);
153         enum iio_event_type ev_type = IIO_EVENT_CODE_EXTRACT_TYPE(event->id);
154         enum iio_event_direction dir = IIO_EVENT_CODE_EXTRACT_DIR(event->id);
155         int chan = IIO_EVENT_CODE_EXTRACT_CHAN(event->id);
156         int chan2 = IIO_EVENT_CODE_EXTRACT_CHAN2(event->id);
157         bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
158
159         if (!event_is_known(event)) {
160                 printf("Unknown event: time: %lld, id: %llx\n",
161                                 event->timestamp, event->id);
162                 return;
163         }
164
165         printf("Event: time: %lld, ", event->timestamp);
166
167         if (mod != IIO_NO_MOD) {
168                 printf("type: %s(%s), ",
169                         iio_chan_type_name_spec[type],
170                         iio_modifier_names[mod]);
171         } else {
172                 printf("type: %s, ",
173                         iio_chan_type_name_spec[type]);
174         }
175
176         if (diff && chan >= 0 && chan2 >= 0)
177                 printf("channel: %d-%d, ", chan, chan2);
178         else if (chan >= 0)
179                 printf("channel: %d, ", chan);
180
181         printf("evtype: %s, direction: %s\n",
182                 iio_ev_type_text[ev_type],
183                 iio_ev_dir_text[dir]);
184 }
185
186 int main(int argc, char **argv)
187 {
188         struct iio_event_data event;
189         const char *device_name;
190         char *chrdev_name;
191         int ret;
192         int dev_num;
193         int fd, event_fd;
194
195         if (argc <= 1) {
196                 printf("Usage: %s <device_name>\n", argv[0]);
197                 return -1;
198         }
199
200         device_name = argv[1];
201
202         dev_num = find_type_by_name(device_name, "iio:device");
203         if (dev_num >= 0) {
204                 printf("Found IIO device with name %s with device number %d\n",
205                         device_name, dev_num);
206                 ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
207                 if (ret < 0) {
208                         ret = -ENOMEM;
209                         goto error_ret;
210                 }
211         } else {
212                 /* If we can't find a IIO device by name assume device_name is a
213                    IIO chrdev */
214                 chrdev_name = strdup(device_name);
215         }
216
217         fd = open(chrdev_name, 0);
218         if (fd == -1) {
219                 fprintf(stdout, "Failed to open %s\n", chrdev_name);
220                 ret = -errno;
221                 goto error_free_chrdev_name;
222         }
223
224         ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
225
226         close(fd);
227
228         if (ret == -1 || event_fd == -1) {
229                 fprintf(stdout, "Failed to retrieve event fd\n");
230                 ret = -errno;
231                 goto error_free_chrdev_name;
232         }
233
234         while (true) {
235                 ret = read(event_fd, &event, sizeof(event));
236                 if (ret == -1) {
237                         if (errno == EAGAIN) {
238                                 printf("nothing available\n");
239                                 continue;
240                         } else {
241                                 perror("Failed to read event from device");
242                                 ret = -errno;
243                                 break;
244                         }
245                 }
246
247                 print_event(&event);
248         }
249
250         close(event_fd);
251 error_free_chrdev_name:
252         free(chrdev_name);
253 error_ret:
254         return ret;
255 }