OSDN Git Service

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