OSDN Git Service

42cbe769b7711da3b455ebf1f72ef38b83f9abcf
[android-x86/hardware-intel-libsensors.git] / common.h
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #define MAX_DEVICES     8       /* Check iio devices 0 to MAX_DEVICES-1 */
9 #define MAX_SENSORS     10      /* We can handle as many sensors */
10 #define MAX_CHANNELS    4       /* We can handle as many channels per sensor */
11 #define MAX_TRIGGERS    8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
12
13 #define DEV_FILE_PATH           "/dev/iio:device%d"
14 #define BASE_PATH               "/sys/bus/iio/devices/iio:device%d/"
15 #define TRIGGER_FILE_PATH       "/sys/bus/iio/devices/trigger%d/name"
16
17 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
18 #define ENABLE_PATH             BASE_PATH "buffer/enable"
19 #define NAME_PATH               BASE_PATH "name"
20 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
21 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
22 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
23 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
24 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
25 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
26 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
27 #define SENSOR_CALIB_BIAS_PATH  BASE_PATH "in_%s_calibbias"
28
29 #define PROP_BASE               "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
30
31 #define MAX_TYPE_SPEC_LEN       32      /* Channel type spec len; ex: "le:u10/16>>0" */
32 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
33 #define MAX_DEVICE_REPORT_SIZE  32      /* iio device scan buffer size */
34
35 #define MAX_NAME_SIZE           32
36
37 #define MAX_SENSOR_BASES        3       /* Max number of base sensors a sensor can rely on */
38
39 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
40 #define REPORTING_MODE(x)       ((x) & 0x06)
41
42 #define FILTER_TYPE_NONE                0
43 #define FILTER_TYPE_MOVING_AVERAGE      1
44 #define FILTER_TYPE_MEDIAN              2
45
46 #define MODE_AUTO       0 /* autodetect */
47 #define MODE_POLL       1
48 #define MODE_TRIGGER    2
49
50 typedef struct
51 {
52         const char *name;       /* channel name ; ex: x */
53
54         /* sysfs entries located under scan_elements */
55         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
56         const char *type_path;  /* _type sysfs file name  */
57         const char *index_path; /* _index sysfs file name */
58
59         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
60         const char *raw_path;   /* _raw sysfs file name  */
61         const char *input_path; /* _input sysfs file name */
62         const char *scale_path; /* _scale sysfs file name */
63 }
64 channel_descriptor_t;
65
66
67 typedef struct
68 {
69         const char *tag;        /* Prefix such as "accel", "gyro", "temp"... */
70         const int type;         /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
71         const int num_channels; /* Expected iio channels for this sensor */
72         const int is_virtual;   /* Is the sensor virtual or not */
73         channel_descriptor_t channel[MAX_CHANNELS];
74 }
75 sensor_catalog_entry_t;
76
77
78 typedef struct
79 {
80         char sign;
81         char endianness;
82         short realbits;
83         short storagebits;
84         short shift;
85 }
86 datum_info_t;
87
88
89 typedef struct
90 {
91         int offset;     /* Offset in bytes within the iio character device report */
92         int size;       /* Field size in bytes */
93         float scale;    /* Scale for each channel */
94         char type_spec[MAX_TYPE_SPEC_LEN];      /* From driver; ex: le:u10/16>>0 */
95         datum_info_t type_info;                 /* Decoded contents of type spec */
96         float opt_scale; /*
97                           * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
98                           * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
99                           */
100 }
101 channel_info_t;
102
103
104 typedef struct
105 {
106         /* Conversion function called once per channel */
107         float (*transform) (int s, int c, unsigned char* sample_data);
108
109         /* Function called once per sample */
110         int (*finalize) (int s, sensors_event_t* data);
111 }
112 sample_ops_t;
113
114
115 /*
116  * Whenever we have sensor data recorded for a sensor in the associated
117  * sensor cell, its report_pending field is set to a non-zero value
118  * indicating how we got this data.
119  */
120 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
121 #define DATA_SYSFS      2       /* Through polling                      */
122 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
123
124
125 typedef struct
126 {
127         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
128         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
129         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
130         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
131         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
132         float max_range;
133         float resolution;
134         float power;
135
136         /*
137          * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer,
138          * 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
139          * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device.
140          */
141         const char* selected_trigger;
142
143         float offset;           /* (cooked = raw + offset) * scale                      */
144         float scale;            /* default:1. when set to 0, use channel specific value */
145         float illumincalib;     /* to set the calibration for the ALS                   */
146
147         float requested_rate;   /* requested events / second                            */
148         float sampling_rate;    /* setup events / second                                */
149
150         float min_supported_rate;
151         float max_supported_rate;
152
153         int dev_num;            /* Associated iio dev num, ex: 3 for /dev/iio:device3   */
154
155         int catalog_index;      /* Associated entry within the sensor_catalog array     */
156         int type;               /* Sensor type, such as SENSOR_TYPE_GYROSCOPE           */
157
158         int num_channels;       /* Actual channel count ; 0 for poll mode sensors       */
159
160         int mode;       /* Usage mode, ex: poll, trigger ... */
161
162         /*
163          * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
164          * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
165          * should be zero for disabled channels. The type field indicates how a specific channel data item is structured.
166          */
167         channel_info_t channel[MAX_CHANNELS];
168
169         /*
170          * 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
171          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout.
172          */
173         int report_pending;
174
175         /* This flag is set if we have a meta data event pending */
176         int meta_data_pending;
177
178         /*
179          * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that
180          * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android.
181          */
182         int64_t report_ts;
183
184         /* Buffer containing the last generated sensor report for this sensor */
185         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
186
187         /* Whether or not the above buffer contains data from a device report */
188         int report_initialized;
189
190         /* Channel and sample finalization callbacks for this sensor */
191         sample_ops_t ops;
192
193         int cal_level; /* 0 means not calibrated */
194
195         /*
196          * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level.
197          */
198         int max_cal_level;
199
200         void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
201
202         void* filter;   /* Filtering data for noisy sensors */
203         int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
204
205         float prev_val; /* Previously reported value, for on-change sensors */
206
207         /*
208          * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
209          * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
210          * their data through a fd that we can add to our poll fd set.
211          */
212         int thread_data_fd[2];
213         pthread_t acquisition_thread;
214
215         int base_count; /* How many base sensors is the sensor depending on */
216         int base[MAX_SENSOR_BASES];
217
218         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
219
220         /* Note: we may have to explicitely serialize access to some fields */
221
222         int is_virtual;                 /* Composite sensor, exposed from data acquired through other sensors */
223
224         uint32_t ref_count;             /* Dependency count - for a real sensor how many active virtual sensors are depending on it */
225
226         uint32_t directly_enabled;      /* Flag showing if a sensor was enabled directly by Android */
227
228         /*
229          * 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
230          * more than one at a later time.
231          */
232         sensors_event_t sample;
233
234         /*
235          * 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
236          * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for
237          * a sample containing 4 fields.
238          */
239         unsigned char order[MAX_CHANNELS];
240
241         /*
242          * 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
243          * events before filtering kicks in. We can also use it for statistics.
244          */
245         uint64_t event_count;
246 }
247 sensor_info_t;
248
249
250 /* Reference a few commonly used variables... */
251 extern int                      sensor_count;
252 extern struct sensor_t          sensor_desc[MAX_SENSORS];
253 extern sensor_info_t            sensor[MAX_SENSORS];
254 extern sensor_catalog_entry_t   sensor_catalog[];
255
256 #endif