OSDN Git Service

Ignore avail freqs when the hrtimer trigger is selected
[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 #define IIO_DEVICES             "/sys/bus/iio/devices/"
18
19 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
20 #define ENABLE_PATH             BASE_PATH "buffer/enable"
21 #define BUFFER_LENGTH_PATH      BASE_PATH "buffer/length"
22 #define NAME_PATH               BASE_PATH "name"
23 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
24 #define EVENTS_PATH             BASE_PATH "events/"
25 #define SENSOR_ENABLE_PATH      BASE_PATH "in_%s_en"
26 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
27 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
28 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
29 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
30 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
31 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
32 #define SENSOR_CALIB_BIAS_PATH  BASE_PATH "in_%s_calibbias"
33 #define MOUNTING_MATRIX_PATH    BASE_PATH "mounting_matrix"
34
35 #define CONFIGFS_TRIGGER_PATH   "/sys/kernel/config/iio/triggers/"
36
37 #define PROP_BASE               "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
38
39 #define MAX_TYPE_SPEC_LEN       32      /* Channel type spec len; ex: "le:u10/16>>0" */
40 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
41 #define MAX_DEVICE_REPORT_SIZE  32      /* iio device scan buffer size */
42
43 #define MAX_NAME_SIZE           32
44
45 #define MAX_SENSOR_BASES        3       /* Max number of base sensors a sensor can rely on */
46
47 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
48 #define REPORTING_MODE(x)       ((x) & 0x06)
49
50 #define FILTER_TYPE_NONE                0
51 #define FILTER_TYPE_MOVING_AVERAGE      1
52 #define FILTER_TYPE_MEDIAN              2
53
54 #define MODE_AUTO       0 /* autodetect */
55 #define MODE_POLL       1
56 #define MODE_TRIGGER    2
57 #define MODE_EVENT      3
58
59
60 typedef struct
61 {
62         const char *type; /* event type; e.g: transition */
63         const char *dir;  /* event direction; e.g: rising */
64
65         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX/events/ */
66         const char *ev_en_path;
67         const char *ev_value_path;
68 }
69 event_descriptor_t;
70
71
72 typedef struct
73 {
74         const char *name;       /* channel name ; ex: x */
75
76         /* sysfs entries located under scan_elements */
77         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
78         const char *type_path;  /* _type sysfs file name  */
79         const char *index_path; /* _index sysfs file name */
80
81         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
82         const char *raw_path;   /* _raw sysfs file name  */
83         const char *input_path; /* _input sysfs file name */
84         const char *scale_path; /* _scale sysfs file name */
85
86         const int num_events;
87         event_descriptor_t event[MAX_EVENTS];
88 }
89 channel_descriptor_t;
90
91
92 typedef struct
93 {
94         const char *tag;        /* Prefix such as "accel", "gyro", "temp"... */
95         const char *shorthand;
96         const int type;         /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
97         const int num_channels; /* Expected iio channels for this sensor */
98         const int is_virtual;   /* Is the sensor virtual or not */
99         channel_descriptor_t channel[MAX_CHANNELS];
100 }
101 sensor_catalog_entry_t;
102
103
104 typedef struct
105 {
106         char sign;
107         char endianness;
108         short realbits;
109         short storagebits;
110         short shift;
111 }
112 datum_info_t;
113
114
115 typedef struct
116 {
117         int offset;     /* Offset in bytes within the iio character device report */
118         int size;       /* Field size in bytes */
119         float scale;    /* Scale for each channel */
120         char type_spec[MAX_TYPE_SPEC_LEN];      /* From driver; ex: le:u10/16>>0 */
121         datum_info_t type_info;                 /* Decoded contents of type spec */
122         float opt_scale; /*
123                           * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
124                           * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
125                           */
126         int raw_path_present;   /* Flag signalling the presence of in_<sens>_<axis>_raw file */
127         int input_path_present; /* Flag signalling the presence of in_<sens>_input file */
128 }
129 channel_info_t;
130
131
132 typedef struct
133 {
134         /* Conversion function called once per channel */
135         float (*transform) (int s, int c, unsigned char* sample_data);
136
137         /* Function called once per sample */
138         int (*finalize) (int s, sensors_event_t* data);
139 }
140 sample_ops_t;
141
142
143 /*
144  * Whenever we have sensor data recorded for a sensor in the associated
145  * sensor cell, its report_pending field is set to a non-zero value
146  * indicating how we got this data.
147  */
148 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
149 #define DATA_SYSFS      2       /* Through polling                      */
150 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
151
152
153 typedef struct
154 {
155         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
156         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
157         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
158         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
159         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
160         char hrtimer_trigger_name[MAX_NAME_SIZE]; /*ex: accel-hr-dev1 */
161         int trigger_nr; /* trigger number associated with this device */
162         float max_range;
163         float resolution;
164         float power;
165
166         /*
167          * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer,
168          * 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
169          * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device.
170          */
171         const char* selected_trigger;
172
173         float offset;           /* (cooked = raw + offset) * scale                      */
174         float scale;            /* default:1. when set to 0, use channel specific value */
175         float illumincalib;     /* to set the calibration for the ALS                   */
176
177         float requested_rate;   /* requested events / second                            */
178         float sampling_rate;    /* setup events / second                                */
179
180         float min_supported_rate;
181         float max_supported_rate;
182
183         int dev_num;            /* Associated iio dev num, ex: 3 for /dev/iio:device3   */
184
185         int catalog_index;      /* Associated entry within the sensor_catalog array     */
186         int type;               /* Sensor type, such as SENSOR_TYPE_GYROSCOPE           */
187
188         int num_channels;       /* Actual channel count ; 0 for poll mode sensors       */
189
190         int mode;       /* Usage mode, ex: poll, trigger ... */
191
192         /*
193          * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
194          * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
195          * should be zero for disabled channels. The type field indicates how a specific channel data item is structured.
196          */
197         channel_info_t channel[MAX_CHANNELS];
198
199         /*
200          * 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
201          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout.
202          */
203         int report_pending;
204
205         /* This flag is set if we have a meta data event pending */
206         int meta_data_pending;
207
208         /*
209          * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that
210          * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android.
211          */
212         int64_t report_ts;
213
214         /* Buffer containing the last generated sensor report for this sensor */
215         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
216
217         /* Whether or not the above buffer contains data from a device report */
218         int report_initialized;
219
220         /* Channel and sample finalization callbacks for this sensor */
221         sample_ops_t ops;
222
223         int cal_level; /* 0 means not calibrated */
224
225         /*
226          * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level.
227          */
228         int max_cal_level;
229
230         void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
231
232         void* filter;   /* Filtering data for noisy sensors */
233         int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
234
235         /* Previously reported value, for on-change sensors */
236         union {
237                 float data;
238                 uint64_t data64;
239         } prev_val;
240         /*
241          * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
242          * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
243          * their data through a fd that we can add to our poll fd set.
244          */
245         int thread_data_fd[2];
246         pthread_t acquisition_thread;
247
248         int base_count; /* How many base sensors is the sensor depending on */
249         int base[MAX_SENSOR_BASES];
250
251         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
252
253         /* Note: we may have to explicitely serialize access to some fields */
254
255         int is_virtual;                 /* Composite sensor, exposed from data acquired through other sensors */
256
257         uint32_t ref_count;             /* Dependency count - for a real sensor how many active virtual sensors are depending on it */
258
259         uint32_t directly_enabled;      /* Flag showing if a sensor was enabled directly by Android */
260
261         /*
262          * 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
263          * more than one at a later time.
264          */
265         sensors_event_t sample;
266         uint64_t event_id;
267
268         /*
269          * 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
270          * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for
271          * a sample containing 4 fields.
272          */
273         unsigned char order[MAX_CHANNELS];
274
275         /*
276          * If the QUIRK_MOUNTING_MATRIX bit is set in quirks, the contents of this matrix is used to correct the sample values so that it takes
277          * into account the way the sensor has been mounted on the PCB.
278          */
279         float mounting_matrix[9];
280
281         /** Count of available frequencies */
282         int avail_freqs_count;
283
284         /** Array of available frequencies */
285         float* avail_freqs;
286
287         /*
288          * 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
289          * events before filtering kicks in. We can also use it for statistics.
290          */
291         uint64_t event_count;
292
293         /* Some polled sensors need to be first enabled so that they start
294          * computing a set of values in hardware (e.g step counter). Enabling
295          * is done through a sysfs attribute in_<tag>_en
296          */
297         int needs_enable;
298
299         float semi_arbitrated_rate;     /* Arbitrated sampling rate before we considered other sensors co-located on the same iio device */
300 }
301 sensor_info_t;
302
303
304 /* Reference a few commonly used variables... */
305 extern int                      sensor_count;
306 extern struct sensor_t          sensor_desc[MAX_SENSORS];
307 extern sensor_info_t            sensor[MAX_SENSORS];
308 extern sensor_catalog_entry_t   sensor_catalog[];
309 extern unsigned int             catalog_size;
310
311 /* Needed both in sensors and activity HALs */
312 void check_trig_sensors (int i, char *sysfs_file, char map[catalog_size]);
313 void check_poll_sensors (int i, char *sysfs_file, char map[catalog_size]);
314 void check_event_sensors (int i, char *sysfs_file, char map[catalog_size]);
315 void discover_sensors(int dev_num, char *sysfs_base_path, char map[catalog_size],
316                       void (*discover_sensor)(int, char*, char*));
317
318 /*
319  * Macros associating iio sysfs entries to to sensor types ; see
320  * linux/kernel/drivers/iio/industrialio-core.c and
321  * hardware/libhardware/include/hardware/sensor.h
322  */
323
324 #define DECLARE_VOID_CHANNEL(tag)       \
325                         tag,    \
326                         "",     \
327                         "",     \
328                         "",     \
329                         "",     \
330                         "",     \
331                         "",     \
332
333 #define DECLARE_CHANNEL(tag, spacer, name)              \
334                         name,                           \
335                         "in_"tag spacer name"_en",      \
336                         "in_"tag spacer name"_type",    \
337                         "in_"tag spacer name"_index",   \
338                         "in_"tag spacer name"_raw",     \
339                         "in_"tag spacer name"_input",   \
340                         "in_"tag spacer name"_scale",   \
341                         0, {{0}},
342
343 #define DECLARE_NAMED_CHANNEL(tag, name)        DECLARE_CHANNEL(tag, "_", name)
344
345 #define DECLARE_GENERIC_CHANNEL(tag)            DECLARE_CHANNEL(tag, "", "")
346
347 #define DECLARE_EVENT(tag, spacer1, name, spacer2, type, spacer3, dir)          \
348                       type, dir,                                                \
349                       "in_"tag spacer1 name spacer2 type spacer3 dir"_en",      \
350                       "in_"tag spacer1 name spacer2 type spacer3 dir"_value",   \
351
352 #define DECLARE_GENERIC_EVENT(tag, name, type, dir) \
353                 DECLARE_EVENT(tag, "_", name, "_", type, "_", dir)
354 #define DECLARE_NAMED_EVENT(tag, name) \
355                 DECLARE_EVENT(tag, "_", name, "","","","")
356
357 #endif