OSDN Git Service

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