1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 #define LOG_TAG "Sensors" 18 19 #include <hardware/sensors.h> 20 #include <fcntl.h> 21 #include <errno.h> 22 #include <dirent.h> 23 #include <math.h> 24 #include <poll.h> 25 #include <pthread.h> 26 #include <stdlib.h> 27 28 #include <linux/input.h> 29 30 #include <utils/Atomic.h> 31 #include <utils/Log.h> 32 33 #include "sensors.h" 34 35 #if defined SENSORHAL_ACC_ADXL346 36 #include "AdxlSensor.h" 37 #elif defined SENSORHAL_ACC_KXTF9 38 #include "KionixSensor.h" 39 #else 40 #error "Sensor configuration ERROR: No sensor is defined." 41 #endif 42 43 #include "AkmSensor.h" 44 45 /*****************************************************************************/ 46 47 #define DELAY_OUT_TIME 0x7FFFFFFF 48 49 #define LIGHT_SENSOR_POLLTIME 2000000000 50 51 52 #define SENSORS_ACCELERATION (1<<ID_A) 53 #define SENSORS_MAGNETIC_FIELD (1<<ID_M) 54 #define SENSORS_ORIENTATION (1<<ID_O) 55 56 #define SENSORS_ACCELERATION_HANDLE 0 57 #define SENSORS_MAGNETIC_FIELD_HANDLE 1 58 #define SENSORS_ORIENTATION_HANDLE 2 59 60 /*****************************************************************************/ 61 62 /* The SENSORS Module */ 63 static const struct sensor_t sSensorList[] = { 64 { "AK8975 3-axis Magnetic field sensor", 65 "Asahi Kasei Microdevices", 66 1, 67 SENSORS_MAGNETIC_FIELD_HANDLE, 68 SENSOR_TYPE_MAGNETIC_FIELD, 1228.8f, 69 CONVERT_M, 0.35f, 10000, 0, 0, 0, 0, 0, 0, { } }, 70 #ifdef SENSORHAL_ACC_ADXL346 71 { "Analog Devices ADXL345/6 3-axis Accelerometer", 72 "ADI", 73 1, SENSORS_ACCELERATION_HANDLE, 74 SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 16.0f), 75 (GRAVITY_EARTH * 16.0f) / 4096.0f, 0.145f, 10000, 0, 0, 0, 0, 0, 0, { } }, 76 { "AK8975 Orientation sensor", 77 "Asahi Kasei Microdevices", 78 1, SENSORS_ORIENTATION_HANDLE, 79 SENSOR_TYPE_ORIENTATION, 360.0f, 80 CONVERT_O, 0.495f, 10000, 0, 0, 0, 0, 0, 0, { } } 81 #endif 82 #ifdef SENSORHAL_ACC_KXTF9 83 { "Kionix KXTF9 3-axis Accelerometer", 84 "Kionix", 85 1, SENSORS_ACCELERATION_HANDLE, 86 SENSOR_TYPE_ACCELEROMETER, (GRAVITY_EARTH * 2.0f), 87 (GRAVITY_EARTH) / 1024.0f, 0.7f, 10000, 0, 0, 0, 0, 0, 0, { } }, 88 { "AK8975 Orientation sensor", 89 "Asahi Kasei Microdevices", 90 1, SENSORS_ORIENTATION_HANDLE, 91 SENSOR_TYPE_ORIENTATION, 360.0f, 92 CONVERT_O, 1.05f, 10000, 0, 0, 0, 0, 0, 0, { } } 93 #endif 94 }; 95 96 97 static int open_sensors(const struct hw_module_t* module, const char* id, 98 struct hw_device_t** device); 99 100 static int sensors__get_sensors_list(struct sensors_module_t* module, 101 struct sensor_t const** list) 102 { 103 *list = sSensorList; 104 return ARRAY_SIZE(sSensorList); 105 } 106 107 static struct hw_module_methods_t sensors_module_methods = { 108 .open = open_sensors 109 }; 110 111 struct sensors_module_t HAL_MODULE_INFO_SYM = { 112 .common = { 113 .tag = HARDWARE_MODULE_TAG, 114 .version_major = 1, 115 .version_minor = 0, 116 .id = SENSORS_HARDWARE_MODULE_ID, 117 .name = "AKM Sensor module", 118 .author = "Asahi Kasei Microdevices", 119 .methods = &sensors_module_methods, 120 }, 121 .get_sensors_list = sensors__get_sensors_list, 122 }; 123 124 struct sensors_poll_context_t { 125 struct sensors_poll_device_t device; // must be first 126 127 sensors_poll_context_t(); 128 ~sensors_poll_context_t(); 129 int activate(int handle, int enabled); 130 int setDelay(int handle, int64_t ns); 131 int setDelay_sub(int handle, int64_t ns); 132 int pollEvents(sensors_event_t* data, int count); 133 134 private: 135 enum { 136 acc = 0, 137 akm = 1, 138 numSensorDrivers, 139 numFds, 140 }; 141 142 static const size_t wake = numFds - 1; 143 static const char WAKE_MESSAGE = 'W'; 144 struct pollfd mPollFds[numFds]; 145 int mWritePipeFd; 146 SensorBase* mSensors[numSensorDrivers]; 147 148 /* These function will be different depends on 149 * which sensor is implemented in AKMD program. 150 */ 151 int handleToDriver(int handle); 152 int proxy_enable(int handle, int enabled); 153 int proxy_setDelay(int handle, int64_t ns); 154 }; 155 156 /*****************************************************************************/ 157 158 sensors_poll_context_t::sensors_poll_context_t() 159 { 160 #ifdef SENSORHAL_ACC_ADXL346 161 mSensors[acc] = new AdxlSensor(); 162 #endif 163 #ifdef SENSORHAL_ACC_KXTF9 164 mSensors[acc] = new KionixSensor(); 165 #endif 166 mPollFds[acc].fd = mSensors[acc]->getFd(); 167 mPollFds[acc].events = POLLIN; 168 mPollFds[acc].revents = 0; 169 170 mSensors[akm] = new AkmSensor(); 171 mPollFds[akm].fd = mSensors[akm]->getFd(); 172 mPollFds[akm].events = POLLIN; 173 mPollFds[akm].revents = 0; 174 175 int wakeFds[2]; 176 int result = pipe(wakeFds); 177 ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno)); 178 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK); 179 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK); 180 mWritePipeFd = wakeFds[1]; 181 182 mPollFds[wake].fd = wakeFds[0]; 183 mPollFds[wake].events = POLLIN; 184 mPollFds[wake].revents = 0; 185 } 186 187 sensors_poll_context_t::~sensors_poll_context_t() { 188 for (int i=0 ; i<numSensorDrivers ; i++) { 189 delete mSensors[i]; 190 } 191 close(mPollFds[wake].fd); 192 close(mWritePipeFd); 193 } 194 195 int sensors_poll_context_t::handleToDriver(int handle) { 196 switch (handle) { 197 case ID_A: 198 return acc; 199 case ID_M: 200 case ID_O: 201 return akm; 202 } 203 return -EINVAL; 204 } 205 206 int sensors_poll_context_t::activate(int handle, int enabled) { 207 int drv = handleToDriver(handle); 208 int err; 209 210 switch (handle) { 211 case ID_A: 212 case ID_M: 213 /* No dependencies */ 214 break; 215 216 case ID_O: 217 /* These sensors depend on ID_A and ID_M */ 218 mSensors[handleToDriver(ID_A)]->setEnable(ID_A, enabled); 219 mSensors[handleToDriver(ID_M)]->setEnable(ID_M, enabled); 220 break; 221 222 default: 223 return -EINVAL; 224 } 225 err = mSensors[drv]->setEnable(handle, enabled); 226 227 if (enabled && !err) { 228 const char wakeMessage(WAKE_MESSAGE); 229 int result = write(mWritePipeFd, &wakeMessage, 1); 230 ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno)); 231 } 232 return err; 233 } 234 235 int sensors_poll_context_t::setDelay(int handle, int64_t ns) { 236 switch (handle) { 237 case ID_A: 238 case ID_M: 239 /* No dependencies */ 240 break; 241 242 case ID_O: 243 /* These sensors depend on ID_A and ID_M */ 244 setDelay_sub(ID_A, ns); 245 setDelay_sub(ID_M, ns); 246 break; 247 248 default: 249 return -EINVAL; 250 } 251 return setDelay_sub(handle, ns); 252 } 253 254 int sensors_poll_context_t::setDelay_sub(int handle, int64_t ns) { 255 int drv = handleToDriver(handle); 256 int en = mSensors[drv]->getEnable(handle); 257 int64_t cur = mSensors[drv]->getDelay(handle); 258 int err = 0; 259 260 if (en <= 1) { 261 /* no dependencies */ 262 if (cur != ns) { 263 err = mSensors[drv]->setDelay(handle, ns); 264 } 265 } else { 266 /* has dependencies, choose shorter interval */ 267 if (cur > ns) { 268 err = mSensors[drv]->setDelay(handle, ns); 269 } 270 } 271 return err; 272 } 273 274 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count) 275 { 276 int nbEvents = 0; 277 int n = 0; 278 279 do { 280 // see if we have some leftover from the last poll() 281 for (int i=0 ; count && i<numSensorDrivers ; i++) { 282 SensorBase* const sensor(mSensors[i]); 283 if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) { 284 int nb = sensor->readEvents(data, count); 285 if (nb < count) { 286 // no more data for this sensor 287 mPollFds[i].revents = 0; 288 } 289 if ((0 != nb) && (acc == i)) { 290 ((AkmSensor*)(mSensors[akm]))->setAccel(&data[nb-1]); 291 } 292 count -= nb; 293 nbEvents += nb; 294 data += nb; 295 } 296 } 297 298 if (count) { 299 // we still have some room, so try to see if we can get 300 // some events immediately or just wait if we don't have 301 // anything to return 302 n = poll(mPollFds, numFds, nbEvents ? 0 : -1); 303 if (n<0) { 304 ALOGE("poll() failed (%s)", strerror(errno)); 305 return -errno; 306 } 307 if (mPollFds[wake].revents & POLLIN) { 308 char msg; 309 int result = read(mPollFds[wake].fd, &msg, 1); 310 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno)); 311 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg)); 312 mPollFds[wake].revents = 0; 313 } 314 } 315 // if we have events and space, go read them 316 } while (n && count); 317 318 return nbEvents; 319 } 320 321 /*****************************************************************************/ 322 323 static int poll__close(struct hw_device_t *dev) 324 { 325 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev; 326 if (ctx) { 327 delete ctx; 328 } 329 return 0; 330 } 331 332 static int poll__activate(struct sensors_poll_device_t *dev, 333 int handle, int enabled) { 334 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev; 335 return ctx->activate(handle, enabled); 336 } 337 338 static int poll__setDelay(struct sensors_poll_device_t *dev, 339 int handle, int64_t ns) { 340 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev; 341 return ctx->setDelay(handle, ns); 342 } 343 344 static int poll__poll(struct sensors_poll_device_t *dev, 345 sensors_event_t* data, int count) { 346 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev; 347 return ctx->pollEvents(data, count); 348 } 349 350 /*****************************************************************************/ 351 352 /** Open a new instance of a sensor device using name */ 353 static int open_sensors(const struct hw_module_t* module, const char* id, 354 struct hw_device_t** device) 355 { 356 int status = -EINVAL; 357 sensors_poll_context_t *dev = new sensors_poll_context_t(); 358 359 memset(&dev->device, 0, sizeof(sensors_poll_device_t)); 360 361 dev->device.common.tag = HARDWARE_DEVICE_TAG; 362 dev->device.common.version = 0; 363 dev->device.common.module = const_cast<hw_module_t*>(module); 364 dev->device.common.close = poll__close; 365 dev->device.activate = poll__activate; 366 dev->device.setDelay = poll__setDelay; 367 dev->device.poll = poll__poll; 368 369 *device = &dev->device.common; 370 status = 0; 371 372 return status; 373 } 374 375