OSDN Git Service

Add quirk for passing timestamps
[android-x86/hardware-intel-libsensors.git] / common.h
index 1a0c9f5..a7a77d8 100644 (file)
--- a/common.h
+++ b/common.h
@@ -1,13 +1,14 @@
 /*
- * Copyright (C) 2014 Intel Corporation.
+ * Copyright (C) 2014-2015 Intel Corporation.
  */
 
 #ifndef __COMMON_H__
 #define __COMMON_H__
 
-#define MAX_DEVICES    8       /* Check iio devices 0 to MAX_DEVICES-1 */
-#define MAX_SENSORS    10      /* We can handle as many sensors */
+#define MAX_DEVICES    9       /* Check iio devices 0 to MAX_DEVICES-1 */
+#define MAX_SENSORS    12      /* We can handle as many sensors */
 #define MAX_CHANNELS   4       /* We can handle as many channels per sensor */
+#define MAX_EVENTS     2       /* We can handle as many events per channel */
 #define MAX_TRIGGERS   8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
 
 #define DEV_FILE_PATH          "/dev/iio:device%d"
 #define ENABLE_PATH            BASE_PATH "buffer/enable"
 #define NAME_PATH              BASE_PATH "name"
 #define TRIGGER_PATH           BASE_PATH "trigger/current_trigger"
+#define EVENTS_PATH            BASE_PATH "events/"
+#define SENSOR_ENABLE_PATH     BASE_PATH "in_%s_en"
 #define SENSOR_OFFSET_PATH     BASE_PATH "in_%s_offset"
 #define SENSOR_SCALE_PATH      BASE_PATH "in_%s_scale"
 #define SENSOR_SAMPLING_PATH   BASE_PATH "in_%s_sampling_frequency"
 #define DEVICE_SAMPLING_PATH   BASE_PATH "sampling_frequency"
 #define DEVICE_AVAIL_FREQ_PATH BASE_PATH "sampling_frequency_available"
 #define ILLUMINATION_CALIBPATH BASE_PATH "in_illuminance_calibscale"
+#define SENSOR_CALIB_BIAS_PATH BASE_PATH "in_%s_calibbias"
 
 #define PROP_BASE              "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
 
 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
 #define REPORTING_MODE(x)      ((x) & 0x06)
 
+#define FILTER_TYPE_NONE               0
+#define FILTER_TYPE_MOVING_AVERAGE     1
+#define FILTER_TYPE_MEDIAN             2
+
+#define MODE_AUTO      0 /* autodetect */
+#define MODE_POLL      1
+#define MODE_TRIGGER   2
+#define MODE_EVENT     3
+
+
+/* Couple of temporary defines until we get a suitable linux/iio/events.h include */
+
+struct iio_event_data {
+         __u64   id;
+         __s64   timestamp;
+};
+
+#define IIO_GET_EVENT_FD_IOCTL _IOR('i', 0x90, int)
+
+#define IIO_EVENT_CODE_EXTRACT_TYPE(mask) ((mask >> 56) & 0xFF)
+#define IIO_EVENT_CODE_EXTRACT_DIR(mask) ((mask >> 48) & 0xCF)
+#define IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(mask) ((mask >> 32) & 0xFF)
+#define IIO_EVENT_CODE_EXTRACT_MODIFIER(mask) ((mask >> 40) & 0xFF)
+
+/* Couple of temporary defines until we get a suitable linux/iio/types.h include */
+enum iio_chan_type {
+       IIO_VOLTAGE,
+       IIO_CURRENT,
+       IIO_POWER,
+       IIO_ACCEL,
+       IIO_ANGL_VEL,
+       IIO_MAGN,
+       IIO_LIGHT,
+       IIO_INTENSITY,
+       IIO_PROXIMITY,
+       IIO_TEMP,
+       IIO_INCLI,
+       IIO_ROT,
+       IIO_ANGL,
+       IIO_TIMESTAMP,
+       IIO_CAPACITANCE,
+       IIO_ALTVOLTAGE,
+       IIO_CCT,
+       IIO_PRESSURE,
+       IIO_HUMIDITYRELATIVE,
+       IIO_ACTIVITY,
+       IIO_STEPS,
+       IIO_CALORIES,
+       IIO_DISTANCE,
+       IIO_SPEED,
+};
+
+enum iio_modifier {
+       IIO_NO_MOD,
+       IIO_MOD_X,
+       IIO_MOD_Y,
+       IIO_MOD_Z,
+       IIO_MOD_X_AND_Y,
+       IIO_MOD_X_AND_Z,
+       IIO_MOD_Y_AND_Z,
+       IIO_MOD_X_AND_Y_AND_Z,
+       IIO_MOD_X_OR_Y,
+       IIO_MOD_X_OR_Z,
+       IIO_MOD_Y_OR_Z,
+       IIO_MOD_X_OR_Y_OR_Z,
+       IIO_MOD_LIGHT_BOTH,
+       IIO_MOD_LIGHT_IR,
+       IIO_MOD_ROOT_SUM_SQUARED_X_Y,
+       IIO_MOD_SUM_SQUARED_X_Y_Z,
+       IIO_MOD_LIGHT_CLEAR,
+       IIO_MOD_LIGHT_RED,
+       IIO_MOD_LIGHT_GREEN,
+       IIO_MOD_LIGHT_BLUE,
+       IIO_MOD_QUATERNION,
+       IIO_MOD_TEMP_AMBIENT,
+       IIO_MOD_TEMP_OBJECT,
+       IIO_MOD_NORTH_MAGN,
+       IIO_MOD_NORTH_TRUE,
+       IIO_MOD_NORTH_MAGN_TILT_COMP,
+       IIO_MOD_NORTH_TRUE_TILT_COMP,
+       IIO_MOD_RUNNING,
+       IIO_MOD_JOGGING,
+       IIO_MOD_WALKING,
+       IIO_MOD_STILL,
+};
+
+enum iio_event_type {
+       IIO_EV_TYPE_THRESH,
+       IIO_EV_TYPE_MAG,
+       IIO_EV_TYPE_ROC,
+       IIO_EV_TYPE_THRESH_ADAPTIVE,
+       IIO_EV_TYPE_MAG_ADAPTIVE,
+       IIO_EV_TYPE_INSTANCE,
+};
+
+enum iio_event_info {
+       IIO_EV_INFO_ENABLE,
+       IIO_EV_INFO_VALUE,
+       IIO_EV_INFO_HYSTERESIS,
+       IIO_EV_INFO_PERIOD,
+};
+
+enum iio_event_direction {
+       IIO_EV_DIR_EITHER,
+       IIO_EV_DIR_RISING,
+       IIO_EV_DIR_FALLING,
+       IIO_EV_DIR_NONE,
+};
+
+
+typedef struct
+{
+       const char *type; /* event type; e.g: transition */
+       const char *dir;  /* event direction; e.g: rising */
+
+       /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX/events/ */
+       const char *ev_en_path;
+       const char *ev_value_path;
+}
+event_descriptor_t;
+
 
 typedef struct
 {
@@ -52,6 +177,9 @@ typedef struct
        const char *raw_path;   /* _raw sysfs file name  */
        const char *input_path; /* _input sysfs file name */
        const char *scale_path; /* _scale sysfs file name */
+
+       const int num_events;
+       event_descriptor_t event[MAX_EVENTS];
 }
 channel_descriptor_t;
 
@@ -82,13 +210,15 @@ typedef struct
 {
        int offset;     /* Offset in bytes within the iio character device report */
        int size;       /* Field size in bytes */
-       float scale;    /* scale for each channel */
+       float scale;    /* Scale for each channel */
        char type_spec[MAX_TYPE_SPEC_LEN];      /* From driver; ex: le:u10/16>>0 */
        datum_info_t type_info;                 /* Decoded contents of type spec */
        float opt_scale; /*
                          * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
                          * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
                          */
+       int raw_path_present;   /* Flag signalling the presence of in_<sens>_<axis>_raw file */
+       int input_path_present; /* Flag signalling the presence of in_<sens>_input file */
 }
 channel_info_t;
 
@@ -149,6 +279,8 @@ typedef struct
 
        int num_channels;       /* Actual channel count ; 0 for poll mode sensors       */
 
+       int mode;       /* Usage mode, ex: poll, trigger ... */
+
        /*
         * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
         * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
@@ -189,11 +321,14 @@ typedef struct
 
        void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
 
-       /* Filtering data for noisy sensors */
-       void* filter;
-
-       float prev_val; /* Previously reported value, for on-change sensors */
+       void* filter;   /* Filtering data for noisy sensors */
+       int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
 
+       /* Previously reported value, for on-change sensors */
+       union {
+               float data;
+               uint64_t data64;
+       } prev_val;
        /*
         * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
         * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
@@ -228,18 +363,19 @@ typedef struct
         */
        unsigned char order[MAX_CHANNELS];
 
-       /* A few variables used for data filtering */
-       float *history;         /* Working buffer containing recorded samples */
-       float *history_sum;     /* The current sum of the history elements    */
-       int history_size;       /* Number of recorded samples                 */
-       int history_entries;    /* How many of these are initialized          */
-       int history_index;      /* Index of sample to evict next time         */
-
        /*
         * Event counter - will be used to check if we have a significant sample for noisy sensors. We want to make sure we do not send any wrong
         * events before filtering kicks in. We can also use it for statistics.
         */
        uint64_t event_count;
+
+       /* Some polled sensors need to be first enabled so that they start
+        * computing a set of values in hardware (e.g step counter). Enabling
+        * is done through a sysfs attribute in_<tag>_en
+        */
+       int needs_enable;
+
+       float semi_arbitrated_rate;     /* Arbitrated sampling rate before we considered other sensors co-located on the same iio device */
 }
 sensor_info_t;
 
@@ -249,5 +385,52 @@ extern int                 sensor_count;
 extern struct sensor_t         sensor_desc[MAX_SENSORS];
 extern sensor_info_t           sensor[MAX_SENSORS];
 extern sensor_catalog_entry_t  sensor_catalog[];
+extern unsigned int            catalog_size;
+
+/* Needed both in sensors and activity HALs */
+void check_trig_sensors (int i, char *sysfs_file, char map[catalog_size]);
+void check_poll_sensors (int i, char *sysfs_file, char map[catalog_size]);
+void check_event_sensors (int i, char *sysfs_file, char map[catalog_size]);
+void discover_sensors(int dev_num, char *sysfs_base_path, char map[catalog_size],
+                     void (*discover_sensor)(int, char*, char*));
+
+/*
+ * Macros associating iio sysfs entries to to sensor types ; see
+ * linux/kernel/drivers/iio/industrialio-core.c and
+ * hardware/libhardware/include/hardware/sensor.h
+ */
+
+#define DECLARE_VOID_CHANNEL(tag)      \
+                       tag,    \
+                       "",     \
+                       "",     \
+                       "",     \
+                       "",     \
+                       "",     \
+                       "",     \
+
+#define DECLARE_CHANNEL(tag, spacer, name)             \
+                       name,                           \
+                       "in_"tag spacer name"_en",      \
+                       "in_"tag spacer name"_type",    \
+                       "in_"tag spacer name"_index",   \
+                       "in_"tag spacer name"_raw",     \
+                       "in_"tag spacer name"_input",   \
+                       "in_"tag spacer name"_scale",   \
+                       0, {{0}},
+
+#define DECLARE_NAMED_CHANNEL(tag, name)       DECLARE_CHANNEL(tag, "_", name)
+
+#define DECLARE_GENERIC_CHANNEL(tag)           DECLARE_CHANNEL(tag, "", "")
+
+#define DECLARE_EVENT(tag, spacer1, name, spacer2, type, spacer3, dir)         \
+                     type, dir,                                                \
+                     "in_"tag spacer1 name spacer2 type spacer3 dir"_en",      \
+                     "in_"tag spacer1 name spacer2 type spacer3 dir"_value",   \
+
+#define DECLARE_GENERIC_EVENT(tag, name, type, dir) \
+               DECLARE_EVENT(tag, "_", name, "_", type, "_", dir)
+#define DECLARE_NAMED_EVENT(tag, name) \
+               DECLARE_EVENT(tag, "_", name, "","","","")
 
 #endif