2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "Sensors"
19 #include <hardware/sensors.h>
28 #include <linux/input.h>
29 #include <linux/akm8973.h>
31 #include <utils/Atomic.h>
32 #include <utils/Log.h>
35 #include "SensorBase.h"
37 #include "SensorConfig.h"
38 #include "BoardConfig.h"
40 /*****************************************************************************/
42 /* The SENSORS Module */
43 static int open_sensors(const struct hw_module_t* module, const char* id,
44 struct hw_device_t** device);
47 static int sensors__get_sensors_list(struct sensors_module_t* module,
48 struct sensor_t const** list)
50 *list = BoardConfig::sensorList();
51 return BoardConfig::sensorListSize();
54 static struct hw_module_methods_t sensors_module_methods = {
58 struct sensors_module_t HAL_MODULE_INFO_SYM = {
60 tag: HARDWARE_MODULE_TAG,
63 id: SENSORS_HARDWARE_MODULE_ID,
64 name: "Samsung Sensor module",
65 author: "Samsung Electronic Company",
66 methods: &sensors_module_methods,
68 get_sensors_list: sensors__get_sensors_list,
71 struct sensors_poll_context_t {
72 struct sensors_poll_device_t device; // must be first
74 sensors_poll_context_t();
75 ~sensors_poll_context_t();
76 int activate(int handle, int enabled);
77 int setDelay(int handle, int64_t ns);
78 int pollEvents(sensors_event_t* data, int count);
81 static const size_t wake = numFds - 1;
82 static const char WAKE_MESSAGE = 'W';
83 struct pollfd mPollFds[numFds];
85 SensorBase* mSensors[numSensorDrivers];
88 /*****************************************************************************/
90 sensors_poll_context_t::sensors_poll_context_t()
92 BoardConfig::initSensors(mSensors);
94 for (int i=0; i < numSensorDrivers; i++) {
95 mPollFds[i].fd = mSensors[i]->fd();
96 mPollFds[i].events = POLLIN;
97 mPollFds[i].revents = 0;
101 int result = pipe(wakeFds);
102 LOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
103 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
104 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
105 mWritePipeFd = wakeFds[1];
107 mPollFds[wake].fd = wakeFds[0];
108 mPollFds[wake].events = POLLIN;
109 mPollFds[wake].revents = 0;
112 sensors_poll_context_t::~sensors_poll_context_t() {
113 for (int i=0 ; i<numSensorDrivers ; i++) {
116 close(mPollFds[wake].fd);
120 int sensors_poll_context_t::activate(int handle, int enabled) {
121 int index = BoardConfig::handleToDriver(handle);
122 if (index < 0) return index;
124 LOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
125 SensorBase* const s(mSensors[index]);
128 if (((err = s->open()) > 0) && (err = s->enable(1)) == 0) {
129 const char wakeMessage(WAKE_MESSAGE);
130 int result = write(mWritePipeFd, &wakeMessage, 1);
131 LOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
139 /* If the file descriptor is closed or opened the stored desciptor
140 should be updated. Even when not opened/closed the fd should
141 return correct value */
142 mPollFds[index].fd = s->fd();
146 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
148 int index = BoardConfig::handleToDriver(handle);
149 if (index < 0) return index;
150 return mSensors[index]->setDelay(ns);
153 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
159 // see if we have some leftover from the last poll()
160 for (int i=0 ; count && i<numSensorDrivers ; i++) {
161 SensorBase* const sensor(mSensors[i]);
162 if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
163 int nb = sensor->readEvents(data, count);
165 // no more data for this sensor
166 mPollFds[i].revents = 0;
169 LOGD("%s: handle:%d error:%d", __func__, i, nb);
180 // we still have some room, so try to see if we can get
181 // some events immediately or just wait if we don't have
182 // anything to return
183 n = poll(mPollFds, numFds, nbEvents ? 0 : POLL_TIMEOUT_MSEC);
185 LOGE("poll() failed (%s)", strerror(errno));
188 if (mPollFds[wake].revents & POLLIN) {
190 int result = read(mPollFds[wake].fd, &msg, 1);
191 LOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
192 LOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
193 mPollFds[wake].revents = 0;
196 // if we have events and space, go read them
197 } while (n && count);
202 /*****************************************************************************/
204 static int poll__close(struct hw_device_t *dev)
206 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
213 static int poll__activate(struct sensors_poll_device_t *dev,
214 int handle, int enabled) {
215 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
216 return ctx->activate(handle, enabled);
219 static int poll__setDelay(struct sensors_poll_device_t *dev,
220 int handle, int64_t ns) {
221 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
222 return ctx->setDelay(handle, ns);
225 static int poll__poll(struct sensors_poll_device_t *dev,
226 sensors_event_t* data, int count) {
227 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
228 return ctx->pollEvents(data, count);
231 /*****************************************************************************/
233 /** Open a new instance of a sensor device using name */
234 static int open_sensors(const struct hw_module_t* module, const char* id,
235 struct hw_device_t** device)
237 int status = -EINVAL;
238 sensors_poll_context_t *dev = new sensors_poll_context_t();
240 memset(&dev->device, 0, sizeof(sensors_poll_device_t));
242 dev->device.common.tag = HARDWARE_DEVICE_TAG;
243 dev->device.common.version = 0;
244 dev->device.common.module = const_cast<hw_module_t*>(module);
245 dev->device.common.close = poll__close;
246 dev->device.activate = poll__activate;
247 dev->device.setDelay = poll__setDelay;
248 dev->device.poll = poll__poll;
250 *device = &dev->device.common;