OSDN Git Service

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