OSDN Git Service

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