OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into gmin...
[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 (__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         if (enabled && sensor_get_quirks(handle) & QUIRK_INITIAL_RATE) {
31                 ALOGI("Forcing initial sampling rate\n");
32                 sensor_activate(handle, 1);
33                 sensor_set_delay(handle, 100000000L);   /* Start with 100 ms */
34                 sensor_activate(handle, 0);
35
36                 /* Clear flag for this sensor as do this only once */
37                 sensor_info[handle].quirks ^= QUIRK_INITIAL_RATE;
38         }
39
40         return sensor_activate(handle, enabled);
41 }
42
43
44 static int set_delay (__attribute__((unused)) struct sensors_poll_device_t* dev,
45                       int handle, int64_t ns)
46 {
47         if (init_count == 0 || handle < 0 || handle >= sensor_count)
48                 return -EINVAL;
49
50         return sensor_set_delay(handle, ns);
51 }
52
53
54 static int poll (__attribute__((unused)) struct sensors_poll_device_t* dev,
55                  sensors_event_t* data, int count)
56 {
57         if (init_count == 0 || !data || count < 1)
58                 return -EINVAL;
59
60         return sensor_poll(data, count);
61 }
62
63
64 static int batch (__attribute__((unused)) struct sensors_poll_device_1* dev,
65                   int sensor_handle, __attribute__((unused)) int flags,
66                   int64_t sampling_period_ns,
67                   __attribute__((unused)) int64_t max_report_latency_ns)
68 {
69         return set_delay ((struct sensors_poll_device_t*)dev,
70                 sensor_handle, sampling_period_ns);
71 }
72
73 static int flush (__attribute__((unused)) struct sensors_poll_device_1* dev,
74                   int handle)
75 {
76         return sensor_flush (handle);
77 }
78
79
80 static int close_module (__attribute__((unused)) hw_device_t *device)
81 {
82         if (init_count == 0)
83                 return -EINVAL;
84
85         init_count--;
86
87         if (init_count == 0) {
88                 ALOGI("Closing IIO sensors HAL module\n");
89                 delete_enumeration_data();
90                 delete_control_data();
91         }
92
93         return 0;
94 }
95
96
97 static int initialize_module(const struct hw_module_t *module, const char *id,
98                                 struct hw_device_t** device)
99 {
100         static struct sensors_poll_device_1 poll_device;
101
102         if (strcmp(id, SENSORS_HARDWARE_POLL))
103                 return -EINVAL;
104
105         poll_device.common.tag          = HARDWARE_DEVICE_TAG;
106         poll_device.common.version      = SENSORS_DEVICE_API_VERSION_1_3;
107         poll_device.common.module       = (struct hw_module_t*) module;
108         poll_device.common.close        = close_module;
109
110         poll_device.activate            = activate;
111         poll_device.setDelay            = set_delay;
112         poll_device.poll                = poll;
113         poll_device.batch               = batch;
114         poll_device.flush               = flush;
115
116         *device = &poll_device.common;
117
118         if (init_count == 0) {
119                 ALOGI("Initializing IIO sensors HAL module\n");
120                 allocate_control_data();
121                 enumerate_sensors();
122         }
123
124         init_count++;
125         return 0;
126 }
127
128
129 static struct hw_module_methods_t module_methods = {
130         .open = initialize_module
131 };
132
133
134 /* Externally visible module descriptor */
135 struct sensors_module_t __attribute__ ((visibility ("default")))
136         HAL_MODULE_INFO_SYM = {
137                 .common = {
138                         .tag = HARDWARE_MODULE_TAG,
139                         .version_major = 1,
140                         .version_minor = 3,
141                         .id = SENSORS_HARDWARE_MODULE_ID,
142                         .name = "IIO sensors HAL",
143                         .author = "Intel",
144                         .methods = &module_methods,
145                 },
146                 .get_sensors_list = get_sensors_list
147 };
148