OSDN Git Service

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