2 // Copyright (c) 2015 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
22 #define MAX_DEVICES 9 /* Check iio devices 0 to MAX_DEVICES-1 */
23 #define MAX_SENSORS 12 /* We can handle as many sensors */
24 #define MAX_CHANNELS 4 /* We can handle as many channels per sensor */
25 #define MAX_EVENTS 2 /* We can handle as many events per channel */
26 #define MAX_TRIGGERS 8 /* Check for triggers 0 to MAX_TRIGGERS-1 */
28 #define DEV_FILE_PATH "/dev/iio:device%d"
29 #define BASE_PATH "/sys/bus/iio/devices/iio:device%d/"
30 #define TRIGGER_FILE_PATH "/sys/bus/iio/devices/trigger%d/name"
31 #define IIO_DEVICES "/sys/bus/iio/devices/"
33 #define CHANNEL_PATH BASE_PATH "scan_elements/"
34 #define ENABLE_PATH BASE_PATH "buffer/enable"
35 #define BUFFER_LENGTH_PATH BASE_PATH "buffer/length"
36 #define NAME_PATH BASE_PATH "name"
37 #define TRIGGER_PATH BASE_PATH "trigger/current_trigger"
38 #define EVENTS_PATH BASE_PATH "events/"
39 #define SENSOR_ENABLE_PATH BASE_PATH "in_%s_en"
40 #define SENSOR_OFFSET_PATH BASE_PATH "in_%s_offset"
41 #define SENSOR_SCALE_PATH BASE_PATH "in_%s_scale"
42 #define SENSOR_SAMPLING_PATH BASE_PATH "in_%s_sampling_frequency"
43 #define DEVICE_SAMPLING_PATH BASE_PATH "sampling_frequency"
44 #define DEVICE_AVAIL_FREQ_PATH BASE_PATH "sampling_frequency_available"
45 #define ILLUMINATION_CALIBPATH BASE_PATH "in_illuminance_calibscale"
46 #define SENSOR_CALIB_BIAS_PATH BASE_PATH "in_%s_calibbias"
47 #define MOUNTING_MATRIX_PATH BASE_PATH "mounting_matrix"
49 #define CONFIGFS_TRIGGER_PATH "/sys/kernel/config/iio/triggers/"
51 #define PROP_BASE "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
53 #define MAX_TYPE_SPEC_LEN 32 /* Channel type spec len; ex: "le:u10/16>>0" */
54 #define MAX_SENSOR_REPORT_SIZE 32 /* Sensor report buffer size */
55 #define MAX_DEVICE_REPORT_SIZE 32 /* iio device scan buffer size */
57 #define MAX_NAME_SIZE 64
59 #define MAX_SENSOR_BASES 3 /* Max number of base sensors a sensor can rely on */
61 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
62 #define REPORTING_MODE(x) ((x) & 0x06)
64 #define FILTER_TYPE_NONE 0
65 #define FILTER_TYPE_MOVING_AVERAGE 1
66 #define FILTER_TYPE_MEDIAN 2
68 #define MODE_AUTO 0 /* autodetect */
70 #define MODE_TRIGGER 2
76 const char *type; /* event type; e.g: transition */
77 const char *dir; /* event direction; e.g: rising */
79 /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX/events/ */
80 const char *ev_en_path;
81 const char *ev_value_path;
88 const char *name; /* channel name ; ex: x */
90 /* sysfs entries located under scan_elements */
91 const char *en_path; /* Enabled sysfs file name ; ex: "in_temp_en" */
92 const char *type_path; /* _type sysfs file name */
93 const char *index_path; /* _index sysfs file name */
95 /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
96 const char *raw_path; /* _raw sysfs file name */
97 const char *input_path; /* _input sysfs file name */
98 const char *scale_path; /* _scale sysfs file name */
100 const int num_events;
101 event_descriptor_t event[MAX_EVENTS];
103 channel_descriptor_t;
108 const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
109 const char *shorthand;
110 const int type; /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
111 const int num_channels; /* Expected iio channels for this sensor */
112 const int is_virtual; /* Is the sensor virtual or not */
113 channel_descriptor_t channel[MAX_CHANNELS];
115 sensor_catalog_entry_t;
131 int offset; /* Offset in bytes within the iio character device report */
132 int size; /* Field size in bytes */
133 float scale; /* Scale for each channel */
134 char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
135 datum_info_t type_info; /* Decoded contents of type spec */
137 * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
138 * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
140 int raw_path_present; /* Flag signalling the presence of in_<sens>_<axis>_raw file */
141 int input_path_present; /* Flag signalling the presence of in_<sens>_input file */
148 /* Conversion function called once per channel */
149 float (*transform) (int s, int c, unsigned char* sample_data);
151 /* Function called once per sample */
152 int (*finalize) (int s, sensors_event_t* data);
158 * Whenever we have sensor data recorded for a sensor in the associated
159 * sensor cell, its report_pending field is set to a non-zero value
160 * indicating how we got this data.
162 #define DATA_TRIGGER 1 /* From /dev/iio:device fd */
163 #define DATA_SYSFS 2 /* Through polling */
164 #define DATA_DUPLICATE 3 /* Duplicate of triggered motion sample */
169 char friendly_name[MAX_NAME_SIZE]; /* ex: Accelerometer */
170 char internal_name[MAX_NAME_SIZE]; /* ex: accel_3d */
171 char vendor_name[MAX_NAME_SIZE]; /* ex: Intel */
172 char init_trigger_name[MAX_NAME_SIZE]; /* ex: accel-name-dev1 */
173 char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
174 char hrtimer_trigger_name[MAX_NAME_SIZE]; /*ex: accel-hr-dev1 */
175 int trigger_nr; /* trigger number associated with this device */
181 * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer,
182 * 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
183 * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device.
185 const char* selected_trigger;
187 float offset; /* (cooked = raw + offset) * scale */
188 float scale; /* default:1. when set to 0, use channel specific value */
189 float illumincalib; /* to set the calibration for the ALS */
191 float requested_rate; /* requested events / second */
192 float sampling_rate; /* setup events / second */
194 float min_supported_rate;
195 float max_supported_rate;
197 int dev_num; /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
199 int catalog_index; /* Associated entry within the sensor_catalog array */
200 int type; /* Sensor type, such as SENSOR_TYPE_GYROSCOPE */
202 int num_channels; /* Actual channel count ; 0 for poll mode sensors */
204 int mode; /* Usage mode, ex: poll, trigger ... */
207 * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
208 * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
209 * should be zero for disabled channels. The type field indicates how a specific channel data item is structured.
211 channel_info_t channel[MAX_CHANNELS];
214 * 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
215 * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout.
219 /* This flag is set if we have a meta data event pending */
220 int meta_data_pending;
223 * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that
224 * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android.
228 /* Buffer containing the last generated sensor report for this sensor */
229 unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
231 /* Whether or not the above buffer contains data from a device report */
232 int report_initialized;
234 /* Channel and sample finalization callbacks for this sensor */
237 int cal_level; /* 0 means not calibrated */
240 * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level.
244 void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
246 void* filter; /* Filtering data for noisy sensors */
247 int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
249 /* Previously reported value, for on-change sensors */
255 * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
256 * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
257 * their data through a fd that we can add to our poll fd set.
259 int thread_data_fd[2];
260 pthread_t acquisition_thread;
262 int base_count; /* How many base sensors is the sensor depending on */
263 int base[MAX_SENSOR_BASES];
265 uint32_t quirks; /* Bit mask expressing the need for special tweaks */
267 /* Note: we may have to explicitely serialize access to some fields */
269 int is_virtual; /* Composite sensor, exposed from data acquired through other sensors */
271 uint32_t ref_count; /* Dependency count - for a real sensor how many active virtual sensors are depending on it */
273 uint32_t directly_enabled; /* Flag showing if a sensor was enabled directly by Android */
276 * 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
277 * more than one at a later time.
279 sensors_event_t sample;
283 * 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
284 * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for
285 * a sample containing 4 fields.
287 unsigned char order[MAX_CHANNELS];
290 * 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
291 * into account the way the sensor has been mounted on the PCB.
293 float mounting_matrix[9];
295 /** Count of available frequencies */
296 int avail_freqs_count;
298 /** Array of available frequencies */
302 * 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
303 * events before filtering kicks in. We can also use it for statistics.
305 uint64_t event_count;
307 /* Some polled sensors need to be first enabled so that they start
308 * computing a set of values in hardware (e.g step counter). Enabling
309 * is done through a sysfs attribute in_<tag>_en
313 float semi_arbitrated_rate; /* Arbitrated sampling rate before we considered other sensors co-located on the same iio device */
318 /* Reference a few commonly used variables... */
319 extern int sensor_count;
320 extern struct sensor_t sensor_desc[MAX_SENSORS];
321 extern sensor_info_t sensor[MAX_SENSORS];
322 extern sensor_catalog_entry_t sensor_catalog[];
323 extern unsigned int catalog_size;
325 /* Needed both in sensors and activity HALs */
326 void check_trig_sensors (int i, char *sysfs_file, char map[catalog_size]);
327 void check_poll_sensors (int i, char *sysfs_file, char map[catalog_size]);
328 void check_event_sensors (int i, char *sysfs_file, char map[catalog_size]);
329 void discover_sensors(int dev_num, char *sysfs_base_path, char map[catalog_size],
330 void (*discover_sensor)(int, char*, char*));
333 * Macros associating iio sysfs entries to to sensor types ; see
334 * linux/kernel/drivers/iio/industrialio-core.c and
335 * hardware/libhardware/include/hardware/sensor.h
338 #define DECLARE_VOID_CHANNEL(tag) \
347 #define DECLARE_CHANNEL(tag, spacer, name) \
349 "in_"tag spacer name"_en", \
350 "in_"tag spacer name"_type", \
351 "in_"tag spacer name"_index", \
352 "in_"tag spacer name"_raw", \
353 "in_"tag spacer name"_input", \
354 "in_"tag spacer name"_scale", \
357 #define DECLARE_NAMED_CHANNEL(tag, name) DECLARE_CHANNEL(tag, "_", name)
359 #define DECLARE_GENERIC_CHANNEL(tag) DECLARE_CHANNEL(tag, "", "")
361 #define DECLARE_EVENT(tag, spacer1, name, spacer2, type, spacer3, dir) \
363 "in_"tag spacer1 name spacer2 type spacer3 dir"_en", \
364 "in_"tag spacer1 name spacer2 type spacer3 dir"_value", \
366 #define DECLARE_GENERIC_EVENT(tag, name, type, dir) \
367 DECLARE_EVENT(tag, "_", name, "_", type, "_", dir)
368 #define DECLARE_NAMED_EVENT(tag, name) \
369 DECLARE_EVENT(tag, "_", name, "","","","")