OSDN Git Service

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