OSDN Git Service

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