OSDN Git Service

Distribute under Apache License v2.0
[android-x86/hardware-intel-libsensors.git] / entry.c
1 /*
2 // Copyright (c) 2015 Intel Corporation
3 //
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
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16
17 #include <hardware/sensors.h>
18 #include <utils/Log.h>
19 #include "enumeration.h"
20 #include "control.h"
21 #include "description.h"
22 #include "utils.h"
23
24 /* This is the IIO Sensors HAL module entry points file */
25
26 static int init_count;
27
28 static int activate (__attribute__((unused)) struct sensors_poll_device_t* dev,
29                      int handle, int enabled)
30 {
31         int64_t entry_ts;
32         int ret;
33         int elapsed_ms;
34
35         if (init_count == 0 || handle < 0 || handle >= sensor_count)
36                 return -EINVAL;
37
38         entry_ts = get_timestamp_thread();
39
40         /*
41          * The Intel sensor hub seems to have trouble enabling sensors before
42          * a sampling rate has been configured, and setting the sampling rate
43          * after it's been enabled does not seem to revive affected sensors.
44          * The issue does not show up with an up to date ISH firmware but as the
45          * updater is a Windows only tool and is not widely available, implement
46          * a workaround for this behavior. We set the initial sampling rate to
47          * 10 events per second when the sensor is enabled for the first time.
48          */
49
50         if (enabled && sensor_get_quirks(handle) & QUIRK_INITIAL_RATE) {
51                 ALOGI("Forcing initial sampling rate\n");
52                 sensor_activate(handle, 1, 0);
53                 sensor_set_delay(handle, 100000000);    /* Start with 100 ms */
54                 sensor_activate(handle, 0, 0);
55
56                 /* Clear flag for this sensor as do this only once */
57                 sensor[handle].quirks ^= QUIRK_INITIAL_RATE;
58         }
59
60         ret = sensor_activate(handle, enabled, 0);
61
62         elapsed_ms = (int) ((get_timestamp_thread() - entry_ts) / 1000000);
63
64         if (elapsed_ms) {
65                 if (enabled)
66                         ALOGI("Activation of sensor %s took %d ms\n", sensor[handle].friendly_name, elapsed_ms);
67                 else
68                         ALOGI("Deactivation of sensor %s took %d ms\n", sensor[handle].friendly_name, elapsed_ms);
69         }
70
71         return ret;
72 }
73
74
75 static int set_delay (__attribute__((unused)) struct sensors_poll_device_t* dev,
76                       int handle, int64_t ns)
77 {
78         int i;
79
80         if (init_count == 0 || handle < 0 || handle >= sensor_count)
81                 return -EINVAL;
82
83         /*
84          * If this sensor relies on other sensors, try to propagate the
85          * requested sampling rate to the base sensors.
86          */
87
88         for (i=0; i<sensor[handle].base_count; i++)
89                 sensor_set_delay(sensor[handle].base[i], ns);
90
91         return sensor_set_delay(handle, ns);
92 }
93
94
95 static int poll (__attribute__((unused)) struct sensors_poll_device_t* dev,
96                  sensors_event_t* data, int count)
97 {
98         if (init_count == 0 || !data || count < 1)
99                 return -EINVAL;
100
101         return sensor_poll(data, count);
102 }
103
104
105 static int batch (__attribute__((unused)) struct sensors_poll_device_1* dev,
106                   int sensor_handle, __attribute__((unused)) int flags,
107                   int64_t sampling_period_ns,
108                   __attribute__((unused)) int64_t max_report_latency_ns)
109 {
110         return set_delay ((struct sensors_poll_device_t*)dev,
111                 sensor_handle, sampling_period_ns);
112 }
113
114 static int flush (__attribute__((unused)) struct sensors_poll_device_1* dev,
115                   int handle)
116 {
117         return sensor_flush (handle);
118 }
119
120
121 static int close_module (__attribute__((unused)) hw_device_t *device)
122 {
123         if (init_count == 0)
124                 return -EINVAL;
125
126         init_count--;
127
128         if (init_count == 0) {
129                 ALOGI("Closing IIO sensors HAL module\n");
130                 delete_enumeration_data();
131                 delete_control_data();
132         }
133
134         return 0;
135 }
136
137
138 static int initialize_module(const struct hw_module_t *module, const char *id,
139                                 struct hw_device_t** device)
140 {
141         static struct sensors_poll_device_1 poll_device;
142
143         if (strcmp(id, SENSORS_HARDWARE_POLL))
144                 return -EINVAL;
145
146         poll_device.common.tag          = HARDWARE_DEVICE_TAG;
147         poll_device.common.version      = SENSORS_DEVICE_API_VERSION_1_3;
148         poll_device.common.module       = (struct hw_module_t*) module;
149         poll_device.common.close        = close_module;
150
151         poll_device.activate            = activate;
152         poll_device.setDelay            = set_delay;
153         poll_device.poll                = poll;
154         poll_device.batch               = batch;
155         poll_device.flush               = flush;
156
157         *device = &poll_device.common;
158
159         if (init_count == 0) {
160                 ALOGI("Initializing IIO sensors HAL module\n");
161                 allocate_control_data();
162                 enumerate_sensors();
163         }
164
165         init_count++;
166         return 0;
167 }
168
169
170 static struct hw_module_methods_t module_methods = {
171         .open = initialize_module
172 };
173
174
175 /* Externally visible module descriptor */
176 struct sensors_module_t __attribute__ ((visibility ("default")))
177         HAL_MODULE_INFO_SYM = {
178                 .common = {
179                         .tag = HARDWARE_MODULE_TAG,
180                         .version_major = 1,
181                         .version_minor = 3,
182                         .id = SENSORS_HARDWARE_MODULE_ID,
183                         .name = "IIO sensors HAL",
184                         .author = "Intel",
185                         .methods = &module_methods,
186                 },
187                 .get_sensors_list = get_sensors_list
188 };
189