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.
17 #include <hardware/sensors.h>
18 #include <utils/Log.h>
19 #include "enumeration.h"
21 #include "description.h"
26 /* This is the IIO Sensors HAL module entry points file */
28 static int init_count;
30 static int activate (__attribute__((unused)) struct sensors_poll_device_t* dev,
31 int handle, int enabled)
37 if (init_count == 0 || handle < 0 || handle >= sensor_count)
40 entry_ts = get_timestamp_thread();
43 * The Intel sensor hub seems to have trouble enabling sensors before
44 * a sampling rate has been configured, and setting the sampling rate
45 * after it's been enabled does not seem to revive affected sensors.
46 * The issue does not show up with an up to date ISH firmware but as the
47 * updater is a Windows only tool and is not widely available, implement
48 * a workaround for this behavior. We set the initial sampling rate to
49 * 10 events per second when the sensor is enabled for the first time.
52 if (enabled && sensor_get_quirks(handle) & QUIRK_INITIAL_RATE) {
53 ALOGI("Forcing initial sampling rate\n");
54 sensor_activate(handle, 1, 0);
55 sensor_set_delay(handle, 100000000); /* Start with 100 ms */
56 sensor_activate(handle, 0, 0);
58 /* Clear flag for this sensor as do this only once */
59 sensor[handle].quirks ^= QUIRK_INITIAL_RATE;
62 ret = sensor_activate(handle, enabled, 0);
64 elapsed_ms = (int) ((get_timestamp_thread() - entry_ts) / 1000000);
68 ALOGI("Activation of sensor %s took %d ms\n", sensor[handle].friendly_name, elapsed_ms);
70 ALOGI("Deactivation of sensor %s took %d ms\n", sensor[handle].friendly_name, elapsed_ms);
77 static int set_delay (__attribute__((unused)) struct sensors_poll_device_t* dev,
78 int handle, int64_t ns)
82 if (init_count == 0 || handle < 0 || handle >= sensor_count)
86 * If this sensor relies on other sensors, try to propagate the
87 * requested sampling rate to the base sensors.
90 for (i=0; i<sensor[handle].base_count; i++)
91 sensor_set_delay(sensor[handle].base[i], ns);
93 return sensor_set_delay(handle, ns);
97 static int poll (__attribute__((unused)) struct sensors_poll_device_t* dev,
98 sensors_event_t* data, int count)
100 if (init_count == 0 || !data || count < 1)
103 return sensor_poll(data, count);
107 static int batch (__attribute__((unused)) struct sensors_poll_device_1* dev,
108 int sensor_handle, __attribute__((unused)) int flags,
109 int64_t sampling_period_ns,
110 __attribute__((unused)) int64_t max_report_latency_ns)
112 return set_delay ((struct sensors_poll_device_t*)dev,
113 sensor_handle, sampling_period_ns);
116 static int flush (__attribute__((unused)) struct sensors_poll_device_1* dev,
119 return sensor_flush (handle);
123 static int close_module (__attribute__((unused)) hw_device_t *device)
130 if (init_count == 0) {
131 ALOGI("Closing IIO sensors HAL module\n");
132 delete_enumeration_data();
133 delete_control_data();
140 static int initialize_module(const struct hw_module_t *module, const char *id,
141 struct hw_device_t** device)
143 static struct sensors_poll_device_1 poll_device;
145 if (strcmp(id, SENSORS_HARDWARE_POLL))
148 poll_device.common.tag = HARDWARE_DEVICE_TAG;
149 poll_device.common.version = SENSORS_DEVICE_API_VERSION_1_3;
150 poll_device.common.module = (struct hw_module_t*) module;
151 poll_device.common.close = close_module;
153 poll_device.activate = activate;
154 poll_device.setDelay = set_delay;
155 poll_device.poll = poll;
156 poll_device.batch = batch;
157 poll_device.flush = flush;
159 *device = &poll_device.common;
161 if (init_count == 0) {
162 ALOGI("Initializing IIO sensors HAL module\n");
163 allocate_control_data();
172 static struct hw_module_methods_t module_methods = {
173 .open = initialize_module
177 /* Externally visible module descriptor */
178 struct sensors_module_t __attribute__ ((visibility ("default")))
179 HAL_MODULE_INFO_SYM = {
181 .tag = HARDWARE_MODULE_TAG,
184 .id = SENSORS_HARDWARE_MODULE_ID,
185 .name = "IIO sensors HAL",
187 .methods = &module_methods,
189 .get_sensors_list = get_sensors_list