OSDN Git Service

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