OSDN Git Service

Fix type of recently introduced semi arbitrated sampling rate
[android-x86/hardware-intel-libsensors.git] / common.h
1 /*
2  * Copyright (C) 2014-2015 Intel Corporation.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #define MAX_DEVICES     9       /* Check iio devices 0 to MAX_DEVICES-1 */
9 #define MAX_SENSORS     12      /* We can handle as many sensors */
10 #define MAX_CHANNELS    4       /* We can handle as many channels per sensor */
11 #define MAX_EVENTS      2       /* We can handle as many events per channel */
12 #define MAX_TRIGGERS    8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
13
14 #define DEV_FILE_PATH           "/dev/iio:device%d"
15 #define BASE_PATH               "/sys/bus/iio/devices/iio:device%d/"
16 #define TRIGGER_FILE_PATH       "/sys/bus/iio/devices/trigger%d/name"
17
18 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
19 #define ENABLE_PATH             BASE_PATH "buffer/enable"
20 #define NAME_PATH               BASE_PATH "name"
21 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
22 #define EVENTS_PATH             BASE_PATH "events/"
23 #define SENSOR_ENABLE_PATH      BASE_PATH "in_%s_en"
24 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
25 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
26 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
27 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
28 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
29 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
30 #define SENSOR_CALIB_BIAS_PATH  BASE_PATH "in_%s_calibbias"
31
32 #define PROP_BASE               "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
33
34 #define MAX_TYPE_SPEC_LEN       32      /* Channel type spec len; ex: "le:u10/16>>0" */
35 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
36 #define MAX_DEVICE_REPORT_SIZE  32      /* iio device scan buffer size */
37
38 #define MAX_NAME_SIZE           32
39
40 #define MAX_SENSOR_BASES        3       /* Max number of base sensors a sensor can rely on */
41
42 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
43 #define REPORTING_MODE(x)       ((x) & 0x06)
44
45 #define FILTER_TYPE_NONE                0
46 #define FILTER_TYPE_MOVING_AVERAGE      1
47 #define FILTER_TYPE_MEDIAN              2
48
49 #define MODE_AUTO       0 /* autodetect */
50 #define MODE_POLL       1
51 #define MODE_TRIGGER    2
52 #define MODE_EVENT      3
53
54
55 /* Couple of temporary defines until we get a suitable linux/iio/events.h include */
56
57 struct iio_event_data {
58          __u64   id;
59          __s64   timestamp;
60 };
61
62 #define IIO_GET_EVENT_FD_IOCTL _IOR('i', 0x90, int)
63
64 #define IIO_EVENT_CODE_EXTRACT_TYPE(mask) ((mask >> 56) & 0xFF)
65 #define IIO_EVENT_CODE_EXTRACT_DIR(mask) ((mask >> 48) & 0xCF)
66 #define IIO_EVENT_CODE_EXTRACT_CHAN_TYPE(mask) ((mask >> 32) & 0xFF)
67 #define IIO_EVENT_CODE_EXTRACT_MODIFIER(mask) ((mask >> 40) & 0xFF)
68
69 /* Couple of temporary defines until we get a suitable linux/iio/types.h include */
70 enum iio_chan_type {
71         IIO_VOLTAGE,
72         IIO_CURRENT,
73         IIO_POWER,
74         IIO_ACCEL,
75         IIO_ANGL_VEL,
76         IIO_MAGN,
77         IIO_LIGHT,
78         IIO_INTENSITY,
79         IIO_PROXIMITY,
80         IIO_TEMP,
81         IIO_INCLI,
82         IIO_ROT,
83         IIO_ANGL,
84         IIO_TIMESTAMP,
85         IIO_CAPACITANCE,
86         IIO_ALTVOLTAGE,
87         IIO_CCT,
88         IIO_PRESSURE,
89         IIO_HUMIDITYRELATIVE,
90         IIO_ACTIVITY,
91         IIO_STEPS,
92         IIO_CALORIES,
93         IIO_DISTANCE,
94         IIO_SPEED,
95 };
96
97 enum iio_modifier {
98         IIO_NO_MOD,
99         IIO_MOD_X,
100         IIO_MOD_Y,
101         IIO_MOD_Z,
102         IIO_MOD_X_AND_Y,
103         IIO_MOD_X_AND_Z,
104         IIO_MOD_Y_AND_Z,
105         IIO_MOD_X_AND_Y_AND_Z,
106         IIO_MOD_X_OR_Y,
107         IIO_MOD_X_OR_Z,
108         IIO_MOD_Y_OR_Z,
109         IIO_MOD_X_OR_Y_OR_Z,
110         IIO_MOD_LIGHT_BOTH,
111         IIO_MOD_LIGHT_IR,
112         IIO_MOD_ROOT_SUM_SQUARED_X_Y,
113         IIO_MOD_SUM_SQUARED_X_Y_Z,
114         IIO_MOD_LIGHT_CLEAR,
115         IIO_MOD_LIGHT_RED,
116         IIO_MOD_LIGHT_GREEN,
117         IIO_MOD_LIGHT_BLUE,
118         IIO_MOD_QUATERNION,
119         IIO_MOD_TEMP_AMBIENT,
120         IIO_MOD_TEMP_OBJECT,
121         IIO_MOD_NORTH_MAGN,
122         IIO_MOD_NORTH_TRUE,
123         IIO_MOD_NORTH_MAGN_TILT_COMP,
124         IIO_MOD_NORTH_TRUE_TILT_COMP,
125         IIO_MOD_RUNNING,
126         IIO_MOD_JOGGING,
127         IIO_MOD_WALKING,
128         IIO_MOD_STILL,
129 };
130
131 enum iio_event_type {
132         IIO_EV_TYPE_THRESH,
133         IIO_EV_TYPE_MAG,
134         IIO_EV_TYPE_ROC,
135         IIO_EV_TYPE_THRESH_ADAPTIVE,
136         IIO_EV_TYPE_MAG_ADAPTIVE,
137         IIO_EV_TYPE_INSTANCE,
138 };
139
140 enum iio_event_info {
141         IIO_EV_INFO_ENABLE,
142         IIO_EV_INFO_VALUE,
143         IIO_EV_INFO_HYSTERESIS,
144         IIO_EV_INFO_PERIOD,
145 };
146
147 enum iio_event_direction {
148         IIO_EV_DIR_EITHER,
149         IIO_EV_DIR_RISING,
150         IIO_EV_DIR_FALLING,
151         IIO_EV_DIR_NONE,
152 };
153
154
155 typedef struct
156 {
157         const char *type; /* event type; e.g: transition */
158         const char *dir;  /* event direction; e.g: rising */
159
160         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX/events/ */
161         const char *ev_en_path;
162         const char *ev_value_path;
163 }
164 event_descriptor_t;
165
166
167 typedef struct
168 {
169         const char *name;       /* channel name ; ex: x */
170
171         /* sysfs entries located under scan_elements */
172         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
173         const char *type_path;  /* _type sysfs file name  */
174         const char *index_path; /* _index sysfs file name */
175
176         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
177         const char *raw_path;   /* _raw sysfs file name  */
178         const char *input_path; /* _input sysfs file name */
179         const char *scale_path; /* _scale sysfs file name */
180
181         const int num_events;
182         event_descriptor_t event[MAX_EVENTS];
183 }
184 channel_descriptor_t;
185
186
187 typedef struct
188 {
189         const char *tag;        /* Prefix such as "accel", "gyro", "temp"... */
190         const int type;         /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
191         const int num_channels; /* Expected iio channels for this sensor */
192         const int is_virtual;   /* Is the sensor virtual or not */
193         channel_descriptor_t channel[MAX_CHANNELS];
194 }
195 sensor_catalog_entry_t;
196
197
198 typedef struct
199 {
200         char sign;
201         char endianness;
202         short realbits;
203         short storagebits;
204         short shift;
205 }
206 datum_info_t;
207
208
209 typedef struct
210 {
211         int offset;     /* Offset in bytes within the iio character device report */
212         int size;       /* Field size in bytes */
213         float scale;    /* Scale for each channel */
214         char type_spec[MAX_TYPE_SPEC_LEN];      /* From driver; ex: le:u10/16>>0 */
215         datum_info_t type_info;                 /* Decoded contents of type spec */
216         float opt_scale; /*
217                           * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
218                           * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
219                           */
220         int raw_path_present;   /* Flag signalling the presence of in_<sens>_<axis>_raw file */
221         int input_path_present; /* Flag signalling the presence of in_<sens>_input file */
222 }
223 channel_info_t;
224
225
226 typedef struct
227 {
228         /* Conversion function called once per channel */
229         float (*transform) (int s, int c, unsigned char* sample_data);
230
231         /* Function called once per sample */
232         int (*finalize) (int s, sensors_event_t* data);
233 }
234 sample_ops_t;
235
236
237 /*
238  * Whenever we have sensor data recorded for a sensor in the associated
239  * sensor cell, its report_pending field is set to a non-zero value
240  * indicating how we got this data.
241  */
242 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
243 #define DATA_SYSFS      2       /* Through polling                      */
244 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
245
246
247 typedef struct
248 {
249         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
250         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
251         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
252         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
253         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
254         float max_range;
255         float resolution;
256         float power;
257
258         /*
259          * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer,
260          * or something else (typically NULL or a pointer to some static "\n". This is used to determine if the conditions are met to switch from
261          * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device.
262          */
263         const char* selected_trigger;
264
265         float offset;           /* (cooked = raw + offset) * scale                      */
266         float scale;            /* default:1. when set to 0, use channel specific value */
267         float illumincalib;     /* to set the calibration for the ALS                   */
268
269         float requested_rate;   /* requested events / second                            */
270         float sampling_rate;    /* setup events / second                                */
271
272         float min_supported_rate;
273         float max_supported_rate;
274
275         int dev_num;            /* Associated iio dev num, ex: 3 for /dev/iio:device3   */
276
277         int catalog_index;      /* Associated entry within the sensor_catalog array     */
278         int type;               /* Sensor type, such as SENSOR_TYPE_GYROSCOPE           */
279
280         int num_channels;       /* Actual channel count ; 0 for poll mode sensors       */
281
282         int mode;       /* Usage mode, ex: poll, trigger ... */
283
284         /*
285          * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
286          * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
287          * should be zero for disabled channels. The type field indicates how a specific channel data item is structured.
288          */
289         channel_info_t channel[MAX_CHANNELS];
290
291         /*
292          * This flag is set if we acquired data from the sensor but did not forward it to upper layers (i.e. Android) yet. If so, report_buffer
293          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout.
294          */
295         int report_pending;
296
297         /* This flag is set if we have a meta data event pending */
298         int meta_data_pending;
299
300         /*
301          * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that
302          * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android.
303          */
304         int64_t report_ts;
305
306         /* Buffer containing the last generated sensor report for this sensor */
307         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
308
309         /* Whether or not the above buffer contains data from a device report */
310         int report_initialized;
311
312         /* Channel and sample finalization callbacks for this sensor */
313         sample_ops_t ops;
314
315         int cal_level; /* 0 means not calibrated */
316
317         /*
318          * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level.
319          */
320         int max_cal_level;
321
322         void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
323
324         void* filter;   /* Filtering data for noisy sensors */
325         int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
326
327         /* Previously reported value, for on-change sensors */
328         union {
329                 float data;
330                 uint64_t data64;
331         } prev_val;
332         /*
333          * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
334          * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
335          * their data through a fd that we can add to our poll fd set.
336          */
337         int thread_data_fd[2];
338         pthread_t acquisition_thread;
339
340         int base_count; /* How many base sensors is the sensor depending on */
341         int base[MAX_SENSOR_BASES];
342
343         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
344
345         /* Note: we may have to explicitely serialize access to some fields */
346
347         int is_virtual;                 /* Composite sensor, exposed from data acquired through other sensors */
348
349         uint32_t ref_count;             /* Dependency count - for a real sensor how many active virtual sensors are depending on it */
350
351         uint32_t directly_enabled;      /* Flag showing if a sensor was enabled directly by Android */
352
353         /*
354          * Current sample for a virtual sensor - when a report is ready we'll keep the data here until it's finally processed. Can be modified for
355          * more than one at a later time.
356          */
357         sensors_event_t sample;
358
359         /*
360          * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of this array are used in the finalization stage to swap sample fields
361          * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for
362          * a sample containing 4 fields.
363          */
364         unsigned char order[MAX_CHANNELS];
365
366         /*
367          * 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
368          * events before filtering kicks in. We can also use it for statistics.
369          */
370         uint64_t event_count;
371
372         /* Some polled sensors need to be first enabled so that they start
373          * computing a set of values in hardware (e.g step counter). Enabling
374          * is done through a sysfs attribute in_<tag>_en
375          */
376         int needs_enable;
377
378         float semi_arbitrated_rate;     /* Arbitrated sampling rate before we considered other sensors co-located on the same iio device */
379 }
380 sensor_info_t;
381
382
383 /* Reference a few commonly used variables... */
384 extern int                      sensor_count;
385 extern struct sensor_t          sensor_desc[MAX_SENSORS];
386 extern sensor_info_t            sensor[MAX_SENSORS];
387 extern sensor_catalog_entry_t   sensor_catalog[];
388 extern unsigned int             catalog_size;
389
390 /* Needed both in sensors and activity HALs */
391 void check_trig_sensors (int i, char *sysfs_file, char map[catalog_size]);
392 void check_poll_sensors (int i, char *sysfs_file, char map[catalog_size]);
393 void check_event_sensors (int i, char *sysfs_file, char map[catalog_size]);
394 void discover_sensors(int dev_num, char *sysfs_base_path, char map[catalog_size],
395                       void (*discover_sensor)(int, char*, char*));
396
397 /*
398  * Macros associating iio sysfs entries to to sensor types ; see
399  * linux/kernel/drivers/iio/industrialio-core.c and
400  * hardware/libhardware/include/hardware/sensor.h
401  */
402
403 #define DECLARE_VOID_CHANNEL(tag)       \
404                         tag,    \
405                         "",     \
406                         "",     \
407                         "",     \
408                         "",     \
409                         "",     \
410                         "",     \
411
412 #define DECLARE_CHANNEL(tag, spacer, name)              \
413                         name,                           \
414                         "in_"tag spacer name"_en",      \
415                         "in_"tag spacer name"_type",    \
416                         "in_"tag spacer name"_index",   \
417                         "in_"tag spacer name"_raw",     \
418                         "in_"tag spacer name"_input",   \
419                         "in_"tag spacer name"_scale",   \
420                         0, {{0}},
421
422 #define DECLARE_NAMED_CHANNEL(tag, name)        DECLARE_CHANNEL(tag, "_", name)
423
424 #define DECLARE_GENERIC_CHANNEL(tag)            DECLARE_CHANNEL(tag, "", "")
425
426 #define DECLARE_EVENT(tag, spacer1, name, spacer2, type, spacer3, dir)          \
427                       type, dir,                                                \
428                       "in_"tag spacer1 name spacer2 type spacer3 dir"_en",      \
429                       "in_"tag spacer1 name spacer2 type spacer3 dir"_value",   \
430
431 #define DECLARE_GENERIC_EVENT(tag, name, type, dir) \
432                 DECLARE_EVENT(tag, "_", name, "_", type, "_", dir)
433 #define DECLARE_NAMED_EVENT(tag, name) \
434                 DECLARE_EVENT(tag, "_", name, "","","","")
435
436 #endif