2 * Copyright (C) 2014 Intel Corporation.
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 */
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"
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"
28 #define PROP_BASE "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
30 #define MAX_TYPE_SPEC_LEN 32 /* Channel type spec len; ex: "le:u10/16>>0" */
31 #define MAX_SENSOR_REPORT_SIZE 32 /* Sensor report buffer size */
32 #define MAX_DEVICE_REPORT_SIZE 32 /* iio device scan buffer size */
34 #define MAX_NAME_SIZE 32
36 #define MAX_SENSOR_BASES 3 /* Max number of base sensors a sensor can rely on */
38 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
39 #define REPORTING_MODE(x) ((x) & 0x06)
42 typedef uint64_t flag_t;
43 typedef int64_t max_delay_t;
45 typedef uint32_t flag_t;
46 typedef int32_t max_delay_t;
49 struct channel_descriptor_t
51 const char *name; /* channel name ; ex: x */
53 /* sysfs entries located under scan_elements */
54 const char *en_path; /* Enabled sysfs file name ; ex: "in_temp_en" */
55 const char *type_path; /* _type sysfs file name */
56 const char *index_path; /* _index sysfs file name */
58 /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
59 const char *raw_path; /* _raw sysfs file name */
60 const char *input_path; /* _input sysfs file name */
61 const char *scale_path; /* _scale sysfs file name */
64 struct sensor_catalog_entry_t
66 const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
67 const int type; /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
68 const int num_channels; /* Expected iio channels for this sensor */
69 const int is_virtual; /* Is the sensor virtual or not */
70 struct channel_descriptor_t channel[MAX_CHANNELS];
84 int offset; /* Offset in bytes within the iio character device report */
85 int size; /* Field size in bytes */
86 float scale; /* scale for each channel */
87 char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
88 struct datum_info_t type_info; /* Decoded contents of type spec */
89 float opt_scale; /* Optional correction scale read from a property such
90 * as iio.accel.x.scale, allowing late compensation of
91 * problems such as misconfigured axes ; set to 1 by
92 * default. Applied at the end of the scaling process.
98 /* Conversion function called once per channel */
99 float (*transform)(int s, int c, unsigned char* sample_data);
101 /* Function called once per sample */
102 int (*finalize)(int s, struct sensors_event_t* data);
106 * Whenever we have sensor data recorded for a sensor in the associated
107 * sensor cell, its report_pending field is set to a non-zero value
108 * indicating how we got this data.
110 #define DATA_TRIGGER 1 /* From /dev/iio:device fd */
111 #define DATA_SYSFS 2 /* Through polling */
112 #define DATA_DUPLICATE 3 /* Duplicate of triggered motion sample */
116 char friendly_name[MAX_NAME_SIZE]; /* ex: Accelerometer */
117 char internal_name[MAX_NAME_SIZE]; /* ex: accel_3d */
118 char vendor_name[MAX_NAME_SIZE]; /* ex: Intel */
119 char init_trigger_name[MAX_NAME_SIZE]; /* ex: accel-name-dev1 */
120 char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
126 * Currently active trigger - either a pointer to the initial (default)
127 * trigger name buffer, or a pointer to the motion trigger name buffer,
128 * or something else (typically NULL or a pointer to some static "\n".
129 * This is used to determine if the conditions are met to switch from
130 * the default trigger to the motion trigger for a sensor, or rather for
131 * the interrupt-driven sensors associated to a given iio device.
133 const char* selected_trigger;
135 float offset; /* (cooked = raw + offset) * scale */
136 float scale; /*default:1. when set to 0, use channel specific value*/
137 float illumincalib; /* to set the calibration for the ALS */
139 float requested_rate; /* requested events / second */
140 float sampling_rate; /* setup events / second */
142 int dev_num; /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
144 int catalog_index;/* Associated entry within the sensor_catalog array */
145 int type; /* Sensor type, such as SENSOR_TYPE_GYROSCOPE */
147 int num_channels; /* Actual channel count ; 0 for poll mode sensors */
150 * The array below indicates where to gather report data for this
151 * sensor inside the reports that we read from the iio character device.
152 * It is updated whenever channels are enabled or disabled on the same
153 * device. Channel size indicates the size in bytes of fields, and
154 * should be zero for disabled channels. The type field indicates how a
155 * specific channel data item is structured.
157 struct channel_info_t channel[MAX_CHANNELS];
160 * This flag is set if we acquired data from the sensor but did not
161 * forward it to upper layers (i.e. Android) yet. If so, report_buffer
162 * contains that data. Valid values are 0: empty, 1: normal, 2: repeat
163 * of last acquired value after timeout.
168 * This flag is set if we have a meta data event pending
170 int meta_data_pending;
173 * Timestamp closely matching the date of sampling, preferably retrieved
174 * from a iio channel alongside sample data. Value zero indicates that
175 * we couldn't get such a closely correlated timestamp, and that one
176 * has to be generated before the report gets sent up to Android.
180 /* Buffer containing the last generated sensor report for this sensor */
181 unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
183 /* Whether or not the above buffer contains data from a device report */
184 int report_initialized;
186 /* Channel and sample finalization callbacks for this sensor */
187 struct sample_ops_t ops;
189 int cal_level; /* 0 means not calibrated */
192 * Depending on the sensor, calibration may take too much time at
193 * higher levels. Allow optional capping to a certain level.
197 void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
199 /* Filtering data for noisy sensors */
202 float prev_val; /* Previously reported value, for on-change sensors */
205 * Certain sensors expose their readings through sysfs files that have
206 * a long response time (100-200 ms for ALS). Rather than block our
207 * global control loop for several hundred ms each second, offload those
208 * lengthy blocking reads to dedicated threads, which will then report
209 * their data through a fd that we can add to our poll fd set.
211 int thread_data_fd[2];
212 pthread_t acquisition_thread;
214 /* How many base sensors is the sensor depending on */
216 int base[MAX_SENSOR_BASES];
218 uint32_t quirks; /* Bit mask expressing the need for special tweaks */
220 /* Note: we may have to explicitely serialize access to some fields */
224 * Dependency count - for a real sensor how many active virtual sensors are
230 * Flag showing if a sensor was enabled directly by Android
232 uint32_t directly_enabled;
235 * Current sample for a virtual sensor - when a report is ready we'll
236 * keep the data here until it's finally processed. Can be modified for
237 * more than one at a later time.
239 struct sensors_event_t sample;
242 * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
243 * this array are used in the finalization stage to swap sample fields
244 * before transmitting them to Android ; they form a mapping between
245 * the indices of the input and output arrays: ex: 0123 is identity for
246 * a sample containing 4 fields.
248 unsigned char order[MAX_CHANNELS];
250 /* A few variables used for data filtering */
251 float *history; /* Working buffer containing recorded samples */
252 float *history_sum; /* The current sum of the history elements */
253 int history_size; /* Number of recorded samples */
254 int history_entries; /* How many of these are initialized */
255 int history_index; /* Index of sample to evict next time */
258 * Event counter - will be used to check if we have a significant sample
259 * for noisy sensors. We want to make sure we do not send any wrong
260 * events before filtering kicks in. We can also use it for statistics.
262 uint64_t event_count;
265 /* Reference a few commonly used variables... */
266 extern int sensor_count;
267 extern struct sensor_t sensor_desc[MAX_SENSORS];
268 extern struct sensor_info_t sensor[MAX_SENSORS];
269 extern struct sensor_catalog_entry_t sensor_catalog[];