These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / tools / iio / iio_event_monitor.c
index 427c271..d51eb04 100644 (file)
@@ -13,7 +13,6 @@
  *
  * Usage:
  *     iio_event_monitor <device_name>
- *
  */
 
 #include <unistd.h>
@@ -51,6 +50,9 @@ static const char * const iio_chan_type_name_spec[] = {
        [IIO_HUMIDITYRELATIVE] = "humidityrelative",
        [IIO_ACTIVITY] = "activity",
        [IIO_STEPS] = "steps",
+       [IIO_ENERGY] = "energy",
+       [IIO_DISTANCE] = "distance",
+       [IIO_VELOCITY] = "velocity",
 };
 
 static const char * const iio_ev_type_text[] = {
@@ -99,6 +101,7 @@ static const char * const iio_modifier_names[] = {
        [IIO_MOD_JOGGING] = "jogging",
        [IIO_MOD_WALKING] = "walking",
        [IIO_MOD_STILL] = "still",
+       [IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z] = "sqrt(x^2+y^2+z^2)",
 };
 
 static bool event_is_known(struct iio_event_data *event)
@@ -130,6 +133,9 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_HUMIDITYRELATIVE:
        case IIO_ACTIVITY:
        case IIO_STEPS:
+       case IIO_ENERGY:
+       case IIO_DISTANCE:
+       case IIO_VELOCITY:
                break;
        default:
                return false;
@@ -167,6 +173,7 @@ static bool event_is_known(struct iio_event_data *event)
        case IIO_MOD_JOGGING:
        case IIO_MOD_WALKING:
        case IIO_MOD_STILL:
+       case IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z:
                break;
        default:
                return false;
@@ -208,31 +215,29 @@ static void print_event(struct iio_event_data *event)
        bool diff = IIO_EVENT_CODE_EXTRACT_DIFF(event->id);
 
        if (!event_is_known(event)) {
-               printf("Unknown event: time: %lld, id: %llx\n",
-                               event->timestamp, event->id);
+               fprintf(stderr, "Unknown event: time: %lld, id: %llx\n",
+                       event->timestamp, event->id);
+
                return;
        }
 
-       printf("Event: time: %lld, ", event->timestamp);
+       printf("Event: time: %lld, type: %s", event->timestamp,
+              iio_chan_type_name_spec[type]);
 
-       if (mod != IIO_NO_MOD) {
-               printf("type: %s(%s), ",
-                       iio_chan_type_name_spec[type],
-                       iio_modifier_names[mod]);
-       } else {
-               printf("type: %s, ",
-                       iio_chan_type_name_spec[type]);
-       }
+       if (mod != IIO_NO_MOD)
+               printf("(%s)", iio_modifier_names[mod]);
 
-       if (diff && chan >= 0 && chan2 >= 0)
-               printf("channel: %d-%d, ", chan, chan2);
-       else if (chan >= 0)
-               printf("channel: %d, ", chan);
+       if (chan >= 0) {
+               printf(", channel: %d", chan);
+               if (diff && chan2 >= 0)
+                       printf("-%d", chan2);
+       }
 
-       printf("evtype: %s", iio_ev_type_text[ev_type]);
+       printf("evtype: %s", iio_ev_type_text[ev_type]);
 
        if (dir != IIO_EV_DIR_NONE)
                printf(", direction: %s", iio_ev_dir_text[dir]);
+
        printf("\n");
 }
 
@@ -246,7 +251,7 @@ int main(int argc, char **argv)
        int fd, event_fd;
 
        if (argc <= 1) {
-               printf("Usage: %s <device_name>\n", argv[0]);
+               fprintf(stderr, "Usage: %s <device_name>\n", argv[0]);
                return -1;
        }
 
@@ -255,31 +260,42 @@ int main(int argc, char **argv)
        dev_num = find_type_by_name(device_name, "iio:device");
        if (dev_num >= 0) {
                printf("Found IIO device with name %s with device number %d\n",
-                       device_name, dev_num);
+                      device_name, dev_num);
                ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num);
-               if (ret < 0) {
-                       ret = -ENOMEM;
-                       goto error_ret;
-               }
+               if (ret < 0)
+                       return -ENOMEM;
        } else {
-               /* If we can't find a IIO device by name assume device_name is a
-                  IIO chrdev */
+               /*
+                * If we can't find an IIO device by name assume device_name is
+                * an IIO chrdev
+                */
                chrdev_name = strdup(device_name);
+               if (!chrdev_name)
+                       return -ENOMEM;
        }
 
        fd = open(chrdev_name, 0);
        if (fd == -1) {
-               fprintf(stdout, "Failed to open %s\n", chrdev_name);
                ret = -errno;
+               fprintf(stderr, "Failed to open %s\n", chrdev_name);
                goto error_free_chrdev_name;
        }
 
        ret = ioctl(fd, IIO_GET_EVENT_FD_IOCTL, &event_fd);
+       if (ret == -1 || event_fd == -1) {
+               ret = -errno;
+               if (ret == -ENODEV)
+                       fprintf(stderr,
+                               "This device does not support events\n");
+               else
+                       fprintf(stderr, "Failed to retrieve event fd\n");
+               if (close(fd) == -1)
+                       perror("Failed to close character device file");
 
-       close(fd);
+               goto error_free_chrdev_name;
+       }
 
-       if (ret == -1 || event_fd == -1) {
-               fprintf(stdout, "Failed to retrieve event fd\n");
+       if (close(fd) == -1)  {
                ret = -errno;
                goto error_free_chrdev_name;
        }
@@ -288,21 +304,29 @@ int main(int argc, char **argv)
                ret = read(event_fd, &event, sizeof(event));
                if (ret == -1) {
                        if (errno == EAGAIN) {
-                               printf("nothing available\n");
+                               fprintf(stderr, "nothing available\n");
                                continue;
                        } else {
-                               perror("Failed to read event from device");
                                ret = -errno;
+                               perror("Failed to read event from device");
                                break;
                        }
                }
 
+               if (ret != sizeof(event)) {
+                       fprintf(stderr, "Reading event failed!\n");
+                       ret = -EIO;
+                       break;
+               }
+
                print_event(&event);
        }
 
-       close(event_fd);
+       if (close(event_fd) == -1)
+               perror("Failed to close event file");
+
 error_free_chrdev_name:
        free(chrdev_name);
-error_ret:
+
        return ret;
 }