diff options
author | Javi Ferrer <javi.f.o@gmail.com> | 2015-05-12 19:40:59 +0200 |
---|---|---|
committer | forkbomb <keepcalm444@gmail.com> | 2015-11-25 08:33:40 +1100 |
commit | 948970715990bde9caf07377f928530848d19a36 (patch) | |
tree | 8ed27e6116225749dab0ac6de29b640e9617bfab | |
parent | ef3bca4b0959df074d5b0867b95699c330b5779e (diff) | |
download | device_samsung_i9300-948970715990bde9caf07377f928530848d19a36.zip device_samsung_i9300-948970715990bde9caf07377f928530848d19a36.tar.gz device_samsung_i9300-948970715990bde9caf07377f928530848d19a36.tar.bz2 |
i9300: update libsensors from replicant
- Orientation sensor still broken
Change-Id: I7d20afbbc1f8b1ca5d046a38c0c3b7ce824c12cd
-rw-r--r-- | libsensors/Android.mk | 9 | ||||
-rw-r--r-- | libsensors/ak8975-reg.h (renamed from libsensors/akm8975-reg.h) | 0 | ||||
-rw-r--r-- | libsensors/ak8975.h (renamed from libsensors/akm8975.h) | 0 | ||||
-rw-r--r-- | libsensors/akm8975.c | 121 | ||||
-rw-r--r-- | libsensors/akmdfs/AKFS_Common.h | 4 | ||||
-rw-r--r-- | libsensors/cm36651_light.c | 43 | ||||
-rw-r--r-- | libsensors/cm36651_proximity.c | 26 | ||||
-rw-r--r-- | libsensors/exynos_sensors.c | 303 | ||||
-rw-r--r-- | libsensors/exynos_sensors.h | 101 | ||||
-rw-r--r-- | libsensors/input.c | 14 | ||||
-rw-r--r-- | libsensors/lps331ap.c | 53 | ||||
-rw-r--r-- | libsensors/lsm330dlc_acceleration.c | 79 | ||||
-rw-r--r-- | libsensors/lsm330dlc_gyroscope.c | 49 | ||||
-rw-r--r-- | libsensors/orientation.c | 77 | ||||
-rw-r--r-- | libsensors/smdk4x12_sensors.c | 299 | ||||
-rw-r--r-- | libsensors/smdk4x12_sensors.h | 105 |
16 files changed, 652 insertions, 631 deletions
diff --git a/libsensors/Android.mk b/libsensors/Android.mk index 41c93d0..2e8e871 100644 --- a/libsensors/Android.mk +++ b/libsensors/Android.mk @@ -1,5 +1,4 @@ -# -# Copyright (C) 2013 Paul Kocialkowski +# Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -13,15 +12,13 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -# LOCAL_PATH := $(call my-dir) -EXYNOS_SENSORS_PATH := $(LOCAL_PATH) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - exynos_sensors.c \ + smdk4x12_sensors.c \ input.c \ orientation.c \ akm8975.c \ @@ -35,7 +32,7 @@ LOCAL_SRC_FILES := \ cm36651_light.c \ lsm330dlc_acceleration.c \ lsm330dlc_gyroscope.c \ - lps331ap.c \ + lps331ap.c LOCAL_C_INCLUDES := \ $(LOCAL_PATH)/akmdfs \ diff --git a/libsensors/akm8975-reg.h b/libsensors/ak8975-reg.h index 1a78a27..1a78a27 100644 --- a/libsensors/akm8975-reg.h +++ b/libsensors/ak8975-reg.h diff --git a/libsensors/akm8975.h b/libsensors/ak8975.h index 79a5de4..79a5de4 100644 --- a/libsensors/akm8975.h +++ b/libsensors/ak8975.h diff --git a/libsensors/akm8975.c b/libsensors/akm8975.c index a8352ca..ef84e93 100644 --- a/libsensors/akm8975.c +++ b/libsensors/akm8975.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan * * This program is free software: you can redistribute it and/or modify @@ -21,20 +21,16 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> -#include <sys/types.h> -#include <linux/ioctl.h> -#include <linux/uinput.h> -#include <linux/input.h> #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" -#include "akm8975.h" -#include "akm8975-reg.h" +#include "smdk4x12_sensors.h" +#include "ak8975.h" +#include "ak8975-reg.h" #include <AKFS_Compass.h> #include <AKFS_FileIO.h> @@ -43,7 +39,7 @@ #define AKFS_PAT PAT3 struct akm8975_data { - struct exynos_sensors_handlers *orientation_sensor; + struct smdk4x12_sensors_handlers *orientation_sensor; AK8975PRMS akfs_params; sensors_vec_t magnetic; @@ -57,37 +53,37 @@ struct akm8975_data { int thread_continue; }; -int akfs_get_magnetic_field(struct akm8975_data *akm8975_data, short *mag_data) +int akfs_get_magnetic_field(struct akm8975_data *akm8975_data, short *magnetic_data) { AK8975PRMS *params; int rc; - if (akm8975_data == NULL || mag_data == NULL) + if (akm8975_data == NULL || magnetic_data == NULL) return -EINVAL; params = &akm8975_data->akfs_params; - /* Decomposition */ - /* Sensitivity adjustment, i.e. multiply ASA, is done in this function. */ - rc = AKFS_DecompAK8975(mag_data, 1, ¶ms->mi_asa, AKFS_HDATA_SIZE, params->mfv_hdata); + // Decomposition + // Sensitivity adjustment, i.e. multiply ASA, is done in this function. + rc = AKFS_DecompAK8975(magnetic_data, 1, ¶ms->mi_asa, AKFS_HDATA_SIZE, params->mfv_hdata); if (rc == AKFS_ERROR) { ALOGE("Failed to decomp!"); return -1; } - /* Adjust coordination */ + // Adjust coordination rc = AKFS_Rotate(params->m_hpat, ¶ms->mfv_hdata[0]); if (rc == AKFS_ERROR) { ALOGE("Failed to rotate!"); return -1; } - /* AOC for magnetometer */ - /* Offset estimation is done in this function */ + // AOC for magnetometer + // Offset estimation is done in this function AKFS_AOC(¶ms->m_aocv, params->mfv_hdata, ¶ms->mfv_ho); - /* Subtract offset */ - /* Then, a magnetic vector, the unit is uT, is stored in mfv_hvbuf. */ + // Subtract offset + // Then, a magnetic vector, the unit is uT, is stored in mfv_hvbuf. rc = AKFS_VbNorm(AKFS_HDATA_SIZE, params->mfv_hdata, 1, ¶ms->mfv_ho, ¶ms->mfv_hs, AK8975_HSENSE_TARGET, AKFS_HDATA_SIZE, params->mfv_hvbuf); @@ -96,7 +92,7 @@ int akfs_get_magnetic_field(struct akm8975_data *akm8975_data, short *mag_data) return -1; } - /* Averaging */ + // Averaging rc = AKFS_VbAve(AKFS_HDATA_SIZE, params->mfv_hvbuf, CSPEC_HNAVE_V, ¶ms->mfv_hvec); if (rc == AKFS_ERROR) { ALOGE("Failed to average!"); @@ -121,7 +117,7 @@ int akfs_init(struct akm8975_data *akm8975_data, char *asa, AKFS_PATNO pat) memset(params, 0, sizeof(AK8975PRMS)); - /* Sensitivity */ + // Sensitivity params->mfv_hs.u.x = AK8975_HSENSE_DEFAULT; params->mfv_hs.u.y = AK8975_HSENSE_DEFAULT; params->mfv_hs.u.z = AK8975_HSENSE_DEFAULT; @@ -129,18 +125,18 @@ int akfs_init(struct akm8975_data *akm8975_data, char *asa, AKFS_PATNO pat) params->mfv_as.u.y = AK8975_ASENSE_DEFAULT; params->mfv_as.u.z = AK8975_ASENSE_DEFAULT; - /* Initialize variables that initial value is not 0. */ + // Initialize variables that initial value is not 0. params->mi_hnaveV = CSPEC_HNAVE_V; params->mi_hnaveD = CSPEC_HNAVE_D; params->mi_anaveV = CSPEC_ANAVE_V; params->mi_anaveD = CSPEC_ANAVE_D; - /* Copy ASA values */ + // Copy ASA values params->mi_asa.u.x = asa[0]; params->mi_asa.u.y = asa[1]; params->mi_asa.u.z = asa[2]; - /* Copy layout pattern */ + // Copy layout pattern params->m_hpat = pat; return 0; @@ -148,12 +144,12 @@ int akfs_init(struct akm8975_data *akm8975_data, char *asa, AKFS_PATNO pat) void *akm8975_thread(void *thread_data) { - struct exynos_sensors_handlers *handlers = NULL; + struct smdk4x12_sensors_handlers *handlers = NULL; struct akm8975_data *data = NULL; struct input_event event; struct timeval time; char i2c_data[SENSOR_DATA_SIZE] = { 0 }; - short mag_data[3]; + short magnetic_data[3]; short mode; long int before, after; int diff; @@ -164,7 +160,7 @@ void *akm8975_thread(void *thread_data) if (thread_data == NULL) return NULL; - handlers = (struct exynos_sensors_handlers *) thread_data; + handlers = (struct smdk4x12_sensors_handlers *) thread_data; if (handlers->data == NULL) return NULL; @@ -211,11 +207,11 @@ void *akm8975_thread(void *thread_data) continue; } - mag_data[0] = (short) (i2c_data[2] << 8) | (i2c_data[1]); - mag_data[1] = (short) (i2c_data[4] << 8) | (i2c_data[3]); - mag_data[2] = (short) (i2c_data[6] << 8) | (i2c_data[5]); + magnetic_data[0] = (short) (i2c_data[2] << 8) | (i2c_data[1]); + magnetic_data[1] = (short) (i2c_data[4] << 8) | (i2c_data[3]); + magnetic_data[2] = (short) (i2c_data[6] << 8) | (i2c_data[5]); - rc = akfs_get_magnetic_field(data, (short *) &mag_data); + rc = akfs_get_magnetic_field(data, (short *) &magnetic_data); if (rc < 0) { ALOGE("%s: Unable to get AKFS magnetic field", __func__); continue; @@ -243,12 +239,12 @@ void *akm8975_thread(void *thread_data) return NULL; } -int akm8975_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int akm8975_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct akm8975_data *data = NULL; pthread_attr_t thread_attr; - char i2c_data[4] = { 0 }; + char i2c_data[RWBUF_SIZE] = { 0 }; short mode; int device_fd = -1; int uinput_fd = -1; @@ -258,7 +254,7 @@ int akm8975_init(struct exynos_sensors_handlers *handlers, ALOGD("%s(%p, %p)", __func__, handlers, device); - if (handlers == NULL) + if (handlers == NULL || device == NULL) return -EINVAL; data = (struct akm8975_data *) calloc(1, sizeof(struct akm8975_data)); @@ -295,7 +291,7 @@ int akm8975_init(struct exynos_sensors_handlers *handlers, i2c_data[1] = AK8975_FUSE_ASAY; rc = ioctl(device_fd, ECS_IOCTL_READ, &i2c_data); if (rc < 0) { - ALOGE("%s: Unable to set read akm8975 FUSE data", __func__); + ALOGE("%s: Unable to read akm8975 FUSE data", __func__); goto error; } @@ -312,7 +308,7 @@ int akm8975_init(struct exynos_sensors_handlers *handlers, i2c_data[1] = AK8975_REG_WIA; rc = ioctl(device_fd, ECS_IOCTL_READ, &i2c_data); if (rc < 0) { - ALOGE("%s: Unable to set read akm8975 FUSE data", __func__); + ALOGE("%s: Unable to read akm8975 FUSE data", __func__); goto error; } @@ -347,7 +343,7 @@ int akm8975_init(struct exynos_sensors_handlers *handlers, rc = pthread_create(&data->thread, &thread_attr, akm8975_thread, (void *) handlers); if (rc < 0) { - ALOGE("%s: Unable to create acceleration thread", __func__); + ALOGE("%s: Unable to create akm8975 thread", __func__); pthread_mutex_destroy(&data->mutex); goto error; } @@ -378,7 +374,7 @@ error: return -1; } -int akm8975_deinit(struct exynos_sensors_handlers *handlers) +int akm8975_deinit(struct smdk4x12_sensors_handlers *handlers) { struct akm8975_data *data = NULL; short mode; @@ -422,7 +418,7 @@ int akm8975_deinit(struct exynos_sensors_handlers *handlers) return 0; } -int akm8975_activate(struct exynos_sensors_handlers *handlers) +int akm8975_activate(struct smdk4x12_sensors_handlers *handlers) { struct akm8975_data *data; AK8975PRMS *akfs_params; @@ -436,20 +432,20 @@ int akm8975_activate(struct exynos_sensors_handlers *handlers) data = (struct akm8975_data *) handlers->data; akfs_params = &data->akfs_params; - /* Read setting files from a file */ + // Read settings from a file rc = AKFS_LoadParameters(akfs_params, AKFS_CONFIG_PATH); if (rc != AKM_SUCCESS) ALOGE("%s: Unable to read AKFS parameters", __func__); - /* Initialize buffer */ + // Initialize buffer AKFS_InitBuffer(AKFS_HDATA_SIZE, akfs_params->mfv_hdata); AKFS_InitBuffer(AKFS_HDATA_SIZE, akfs_params->mfv_hvbuf); AKFS_InitBuffer(AKFS_ADATA_SIZE, akfs_params->mfv_adata); AKFS_InitBuffer(AKFS_ADATA_SIZE, akfs_params->mfv_avbuf); - /* Initialize for AOC */ + // Initialize for AOC AKFS_InitAOC(&akfs_params->m_aocv); - /* Initialize magnetic status */ + // Initialize magnetic status akfs_params->mi_hstatus = 0; handlers->activated = 1; @@ -458,13 +454,15 @@ int akm8975_activate(struct exynos_sensors_handlers *handlers) return 0; } -int akm8975_deactivate(struct exynos_sensors_handlers *handlers) +int akm8975_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct akm8975_data *data; AK8975PRMS *akfs_params; int device_fd; short mode; + int empty; int rc; + int i; ALOGD("%s(%p)", __func__, handlers); @@ -476,12 +474,23 @@ int akm8975_deactivate(struct exynos_sensors_handlers *handlers) device_fd = data->device_fd; if (device_fd < 0) - return -EINVAL; + return -1; - /* Write setting files to a file */ - rc = AKFS_SaveParameters(akfs_params, AKFS_CONFIG_PATH); - if (rc != AKM_SUCCESS) - ALOGE("%s: Unable to write AKFS parameters", __func__); + empty = 1; + + for (i = 0; i < 3; i++) { + if (akfs_params->mfv_ho.v[i] != 0) { + empty = 0; + break; + } + } + + if (!empty) { + // Write settings to a file + rc = AKFS_SaveParameters(akfs_params, AKFS_CONFIG_PATH); + if (rc != AKM_SUCCESS) + ALOGE("%s: Unable to write AKFS parameters", __func__); + } mode = AK8975_MODE_POWER_DOWN; rc = ioctl(device_fd, ECS_IOCTL_SET_MODE, &mode); @@ -493,7 +502,7 @@ int akm8975_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int akm8975_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int akm8975_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { struct akm8975_data *data; @@ -514,7 +523,7 @@ float akm8975_convert(int value) return (float) value / 1000.0f; } -int akm8975_get_data(struct exynos_sensors_handlers *handlers, +int akm8975_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { struct akm8975_data *data; @@ -531,8 +540,9 @@ int akm8975_get_data(struct exynos_sensors_handlers *handlers, input_fd = handlers->poll_fd; if (input_fd < 0) - return -EINVAL; + return -1; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; @@ -570,8 +580,7 @@ int akm8975_get_data(struct exynos_sensors_handlers *handlers, return 0; } - -struct exynos_sensors_handlers akm8975 = { +struct smdk4x12_sensors_handlers akm8975 = { .name = "AKM8975", .handle = SENSOR_TYPE_MAGNETIC_FIELD, .init = akm8975_init, diff --git a/libsensors/akmdfs/AKFS_Common.h b/libsensors/akmdfs/AKFS_Common.h index 53eabf9..7780801 100644 --- a/libsensors/akmdfs/AKFS_Common.h +++ b/libsensors/akmdfs/AKFS_Common.h @@ -40,7 +40,7 @@ #include <string.h> /* memset */ #include <unistd.h> #include <stdarg.h> /* va_list */ -#include <utils/Log.h> /* LOGV */ +#include <utils/Log.h> /* ALOGV */ #include <errno.h> /* errno */ #endif @@ -118,7 +118,7 @@ /***** Log output ********************************************/ #ifdef AKM_LOG_ENABLE -#define AKM_LOG(format, ...) LOGD((format), ##__VA_ARGS__) +#define AKM_LOG(format, ...) ALOGD((format), ##__VA_ARGS__) #else #define AKM_LOG(format, ...) #endif diff --git a/libsensors/cm36651_light.c b/libsensors/cm36651_light.c index 389f5b6..bab689a 100644 --- a/libsensors/cm36651_light.c +++ b/libsensors/cm36651_light.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> +#include <math.h> #include <sys/types.h> #include <linux/ioctl.h> #include <linux/input.h> @@ -27,18 +28,18 @@ #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" struct cm36651_light_data { char path_enable[PATH_MAX]; char path_delay[PATH_MAX]; }; -int cm36651_light_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int cm36651_light_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct cm36651_light_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -85,7 +86,7 @@ error: return -1; } -int cm36651_light_deinit(struct exynos_sensors_handlers *handlers) +int cm36651_light_deinit(struct smdk4x12_sensors_handlers *handlers) { ALOGD("%s(%p)", __func__, handlers); @@ -103,8 +104,7 @@ int cm36651_light_deinit(struct exynos_sensors_handlers *handlers) return 0; } - -int cm36651_light_activate(struct exynos_sensors_handlers *handlers) +int cm36651_light_activate(struct smdk4x12_sensors_handlers *handlers) { struct cm36651_light_data *data; int rc; @@ -127,7 +127,7 @@ int cm36651_light_activate(struct exynos_sensors_handlers *handlers) return 0; } -int cm36651_light_deactivate(struct exynos_sensors_handlers *handlers) +int cm36651_light_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct cm36651_light_data *data; int rc; @@ -150,7 +150,7 @@ int cm36651_light_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int cm36651_light_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int cm36651_light_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { struct cm36651_light_data *data; int rc; @@ -171,19 +171,15 @@ int cm36651_light_set_delay(struct exynos_sensors_handlers *handlers, long int d return 0; } -float cm36651_light_convert(int red, int green, int blue, int white) +float cm36651_light_convert(int value) { - return (float) white * 1.7f - 0.5f; + return (float) value * 1.7f - 0.5f; } -int cm36651_light_get_data(struct exynos_sensors_handlers *handlers, +int cm36651_light_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { struct input_event input_event; - int red = 0; - int green = 0; - int blue = 0; - int white = 0; int input_fd; int rc; @@ -196,6 +192,7 @@ int cm36651_light_get_data(struct exynos_sensors_handlers *handlers, if (input_fd < 0) return -EINVAL; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; @@ -206,26 +203,18 @@ int cm36651_light_get_data(struct exynos_sensors_handlers *handlers, break; if (input_event.type == EV_REL) { - if (input_event.code == REL_X) - red = input_event.value; - if (input_event.code == REL_Y) - green = input_event.value; - if (input_event.code == REL_Z) - blue = input_event.value; if (input_event.code == REL_MISC) - white = input_event.value; + event->light = cm36651_light_convert(input_event.value); } else if (input_event.type == EV_SYN) { if (input_event.code == SYN_REPORT) event->timestamp = input_timestamp(&input_event); } } while (input_event.type != EV_SYN); - event->distance = cm36651_light_convert(red, green, blue, white); - return 0; } -struct exynos_sensors_handlers cm36651_light = { +struct smdk4x12_sensors_handlers cm36651_light = { .name = "CM36651 Light", .handle = SENSOR_TYPE_LIGHT, .init = cm36651_light_init, diff --git a/libsensors/cm36651_proximity.c b/libsensors/cm36651_proximity.c index 23349ce..b102f9a 100644 --- a/libsensors/cm36651_proximity.c +++ b/libsensors/cm36651_proximity.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -27,17 +27,17 @@ #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" struct cm36651_proximity_data { char path_enable[PATH_MAX]; }; -int cm36651_proximity_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int cm36651_proximity_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct cm36651_proximity_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -83,7 +83,7 @@ error: return -1; } -int cm36651_proximity_deinit(struct exynos_sensors_handlers *handlers) +int cm36651_proximity_deinit(struct smdk4x12_sensors_handlers *handlers) { ALOGD("%s(%p)", __func__, handlers); @@ -101,8 +101,7 @@ int cm36651_proximity_deinit(struct exynos_sensors_handlers *handlers) return 0; } - -int cm36651_proximity_activate(struct exynos_sensors_handlers *handlers) +int cm36651_proximity_activate(struct smdk4x12_sensors_handlers *handlers) { struct cm36651_proximity_data *data; int rc; @@ -125,7 +124,7 @@ int cm36651_proximity_activate(struct exynos_sensors_handlers *handlers) return 0; } -int cm36651_proximity_deactivate(struct exynos_sensors_handlers *handlers) +int cm36651_proximity_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct cm36651_proximity_data *data; int rc; @@ -148,7 +147,7 @@ int cm36651_proximity_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int cm36651_proximity_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int cm36651_proximity_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { ALOGD("%s(%p, %ld)", __func__, handlers, delay); @@ -157,10 +156,10 @@ int cm36651_proximity_set_delay(struct exynos_sensors_handlers *handlers, long i float cm36651_proximity_convert(int value) { - return (float) value * 8.0f; + return (float) value * 6.0f; } -int cm36651_proximity_get_data(struct exynos_sensors_handlers *handlers, +int cm36651_proximity_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { struct input_event input_event; @@ -176,6 +175,7 @@ int cm36651_proximity_get_data(struct exynos_sensors_handlers *handlers, if (input_fd < 0) return -EINVAL; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; @@ -197,7 +197,7 @@ int cm36651_proximity_get_data(struct exynos_sensors_handlers *handlers, return 0; } -struct exynos_sensors_handlers cm36651_proximity = { +struct smdk4x12_sensors_handlers cm36651_proximity = { .name = "CM36651 Proximity", .handle = SENSOR_TYPE_PROXIMITY, .init = cm36651_proximity_init, diff --git a/libsensors/exynos_sensors.c b/libsensors/exynos_sensors.c deleted file mode 100644 index e3d810f..0000000 --- a/libsensors/exynos_sensors.c +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (C) 2013 Paul Kocialkowski - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <stdlib.h> -#include <unistd.h> -#include <stdint.h> -#include <fcntl.h> -#include <errno.h> -#include <poll.h> - - - -#include <sys/select.h> - - - -#include <hardware/sensors.h> -#include <hardware/hardware.h> - -#define LOG_TAG "exynos_sensors" -#include <utils/Log.h> - -#include "exynos_sensors.h" - -/* - * Sensors list - */ - -struct sensor_t exynos_sensors[] = { - { "LSM330DLC Acceleration Sensor", "STMicroelectronics", 1, SENSOR_TYPE_ACCELEROMETER, - SENSOR_TYPE_ACCELEROMETER, 19.61f, 0.0096f, 0.23f, 10000, 0, 0, 0, 0, 0, - SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "AKM8975 Magnetic Sensor", "Asahi Kasei", 1, SENSOR_TYPE_MAGNETIC_FIELD, - SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, 0.06f, 6.8f, 10000, 0, 0, 0, 0, 0, - SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "Orientation Sensor", "Exynos Sensors", 1, SENSOR_TYPE_ORIENTATION, - SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 10000, 0, 0, 0, 0, 0, - SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "CM36651 Light Sensor", "Capella", 1, SENSOR_TYPE_LIGHT, - SENSOR_TYPE_LIGHT, 121240.0f, 1.0f, 0.2f, 0, 0, 0, 0, 0, 0, - SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "CM36651 Proximity Sensor", "Capella", 1, SENSOR_TYPE_PROXIMITY, - SENSOR_TYPE_PROXIMITY, 8.0f, 8.0f, 1.3f, 0, 0, 0, 0, 0, 0, - SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "LSM330DLC Gyroscope Sensor", "STMicroelectronics", 1, SENSOR_TYPE_GYROSCOPE, - SENSOR_TYPE_GYROSCOPE, 8.73f, 0.00031f, 6.1f, 5000, 0, 0, 0, 0, 0, - SENSOR_FLAG_ON_CHANGE_MODE, {}, }, - { "LPS331AP Pressure Sensor", "STMicroelectronics", 1, SENSOR_TYPE_PRESSURE, - SENSOR_TYPE_PRESSURE, 1260.0f, 0.00024f, 0.045f, 40000, 0, 0, 0, 0, 20000, - SENSOR_FLAG_CONTINUOUS_MODE, {}, }, -}; - -int exynos_sensors_count = sizeof(exynos_sensors) / sizeof(struct sensor_t); - -struct exynos_sensors_handlers *exynos_sensors_handlers[] = { - &lsm330dlc_acceleration, - &akm8975, - &orientation, - &cm36651_proximity, - &cm36651_light, - &lsm330dlc_gyroscope, - &lps331ap, -}; - -int exynos_sensors_handlers_count = sizeof(exynos_sensors_handlers) / - sizeof(struct exynos_sensors_handlers *); - -/* - * Exynos Sensors - */ - -int exynos_sensors_activate(struct sensors_poll_device_t *dev, int handle, int enabled) -{ - struct exynos_sensors_device *device; - int i; - - ALOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled); - - if (dev == NULL) - return -EINVAL; - - device = (struct exynos_sensors_device *) dev; - - if (device->handlers == NULL || device->handlers_count <= 0) - return -EINVAL; - - for (i = 0; i < device->handlers_count; i++) { - if (device->handlers[i] == NULL) - continue; - - if (device->handlers[i]->handle == handle) { - if (enabled && device->handlers[i]->activate != NULL) { - device->handlers[i]->needed |= EXYNOS_SENSORS_NEEDED_API; - if (device->handlers[i]->needed == EXYNOS_SENSORS_NEEDED_API) - return device->handlers[i]->activate(device->handlers[i]); - else - return 0; - } else if (!enabled && device->handlers[i]->deactivate != NULL) { - device->handlers[i]->needed &= ~EXYNOS_SENSORS_NEEDED_API; - if (device->handlers[i]->needed == 0) - return device->handlers[i]->deactivate(device->handlers[i]); - else - return 0; - } - } - } - - return -1; -} - -int exynos_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int64_t ns) -{ - struct exynos_sensors_device *device; - int i; - - ALOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns); - - if (dev == NULL) - return -EINVAL; - - device = (struct exynos_sensors_device *) dev; - - if (device->handlers == NULL || device->handlers_count <= 0) - return -EINVAL; - - for (i = 0; i < device->handlers_count; i++) { - if (device->handlers[i] == NULL) - continue; - - if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL) - return device->handlers[i]->set_delay(device->handlers[i], (long int) ns); - } - - return 0; -} - -int exynos_sensors_poll(struct sensors_poll_device_t *dev, - struct sensors_event_t* data, int count) -{ - struct exynos_sensors_device *device; - int i, j; - int c, n; - int poll_rc, rc; - -// ALOGD("%s(%p, %p, %d)", __func__, dev, data, count); - - if (dev == NULL) - return -EINVAL; - - device = (struct exynos_sensors_device *) dev; - - if (device->handlers == NULL || device->handlers_count <= 0 || - device->poll_fds == NULL || device->poll_fds_count <= 0) - return -EINVAL; - - n = 0; - - do { - poll_rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1); - if (poll_rc < 0) - return -1; - - for (i = 0; i < device->poll_fds_count; i++) { - if (!(device->poll_fds[i].revents & POLLIN)) - continue; - - for (j = 0; j < device->handlers_count; j++) { - if (device->handlers[j] == NULL || device->handlers[j]->poll_fd != device->poll_fds[i].fd || device->handlers[j]->get_data == NULL) - continue; - - rc = device->handlers[j]->get_data(device->handlers[j], &data[n]); - if (rc < 0) { - device->poll_fds[i].revents = 0; - poll_rc = -1; - } else { - n++; - count--; - } - } - } - } while ((poll_rc > 0 || n < 1) && count > 0); - - return n; -} - -/* - * Interface - */ - -int exynos_sensors_close(hw_device_t *device) -{ - struct exynos_sensors_device *exynos_sensors_device; - int i; - - ALOGD("%s(%p)", __func__, device); - - if (device == NULL) - return -EINVAL; - - exynos_sensors_device = (struct exynos_sensors_device *) device; - - if (exynos_sensors_device->poll_fds != NULL) - free(exynos_sensors_device->poll_fds); - - for (i = 0; i < exynos_sensors_device->handlers_count; i++) { - if (exynos_sensors_device->handlers[i] == NULL || exynos_sensors_device->handlers[i]->deinit == NULL) - continue; - - exynos_sensors_device->handlers[i]->deinit(exynos_sensors_device->handlers[i]); - } - - free(device); - - return 0; -} - -int exynos_sensors_open(const struct hw_module_t* module, const char *id, - struct hw_device_t** device) -{ - struct exynos_sensors_device *exynos_sensors_device; - int p, i; - - ALOGD("%s(%p, %s, %p)", __func__, module, id, device); - - if (module == NULL || device == NULL) - return -EINVAL; - - exynos_sensors_device = (struct exynos_sensors_device *) - calloc(1, sizeof(struct exynos_sensors_device)); - exynos_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG; - exynos_sensors_device->device.common.version = 0; - exynos_sensors_device->device.common.module = (struct hw_module_t *) module; - exynos_sensors_device->device.common.close = exynos_sensors_close; - exynos_sensors_device->device.activate = exynos_sensors_activate; - exynos_sensors_device->device.setDelay = exynos_sensors_set_delay; - exynos_sensors_device->device.poll = exynos_sensors_poll; - exynos_sensors_device->handlers = exynos_sensors_handlers; - exynos_sensors_device->handlers_count = exynos_sensors_handlers_count; - exynos_sensors_device->poll_fds = (struct pollfd *) - calloc(1, exynos_sensors_handlers_count * sizeof(struct pollfd)); - - p = 0; - for (i = 0; i < exynos_sensors_handlers_count; i++) { - if (exynos_sensors_handlers[i] == NULL || exynos_sensors_handlers[i]->init == NULL) - continue; - - exynos_sensors_handlers[i]->init(exynos_sensors_handlers[i], exynos_sensors_device); - if (exynos_sensors_handlers[i]->poll_fd >= 0) { - exynos_sensors_device->poll_fds[p].fd = exynos_sensors_handlers[i]->poll_fd; - exynos_sensors_device->poll_fds[p].events = POLLIN; - p++; - } - } - - exynos_sensors_device->poll_fds_count = p; - - *device = &(exynos_sensors_device->device.common); - - return 0; -} - -int exynos_sensors_get_sensors_list(struct sensors_module_t* module, - const struct sensor_t **sensors_p) -{ - ALOGD("%s(%p, %p)", __func__, module, sensors_p); - - if (sensors_p == NULL) - return -EINVAL; - - *sensors_p = exynos_sensors; - return exynos_sensors_count; -} - -struct hw_module_methods_t exynos_sensors_module_methods = { - .open = exynos_sensors_open, -}; - -struct sensors_module_t HAL_MODULE_INFO_SYM = { - .common = { - .tag = HARDWARE_MODULE_TAG, - .version_major = 1, - .version_minor = 0, - .id = SENSORS_HARDWARE_MODULE_ID, - .name = "Exynos Sensors", - .author = "Paul Kocialkowski", - .methods = &exynos_sensors_module_methods, - }, - .get_sensors_list = exynos_sensors_get_sensors_list, -}; diff --git a/libsensors/exynos_sensors.h b/libsensors/exynos_sensors.h deleted file mode 100644 index 65aed7f..0000000 --- a/libsensors/exynos_sensors.h +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2013 Paul Kocialkowski - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <stdint.h> -#include <poll.h> - -#include <linux/input.h> - -#include <hardware/sensors.h> -#include <hardware/hardware.h> - -#ifndef _EXYNOS_SENSORS_H_ -#define _EXYNOS_SENSORS_H_ - -#define EXYNOS_SENSORS_NEEDED_API (1 << 0) -#define EXYNOS_SENSORS_NEEDED_ORIENTATION (1 << 1) - -struct exynos_sensors_device; - -struct exynos_sensors_handlers { - char *name; - int handle; - - int (*init)(struct exynos_sensors_handlers *handlers, struct exynos_sensors_device *device); - int (*deinit)(struct exynos_sensors_handlers *handlers); - int (*activate)(struct exynos_sensors_handlers *handlers); - int (*deactivate)(struct exynos_sensors_handlers *handlers); - int (*set_delay)(struct exynos_sensors_handlers *handlers, long int delay); - int (*get_data)(struct exynos_sensors_handlers *handlers, struct sensors_event_t *event); - - int activated; - int needed; - int poll_fd; - - void *data; -}; - -struct exynos_sensors_device { - struct sensors_poll_device_t device; - - struct exynos_sensors_handlers **handlers; - int handlers_count; - - struct pollfd *poll_fds; - int poll_fds_count; -}; - -extern struct exynos_sensors_handlers *exynos_sensors_handlers[]; -extern int exynos_sensors_handlers_count; - -int exynos_sensors_activate(struct sensors_poll_device_t *dev, int handle, int enabled); -int exynos_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, int64_t ns); -int exynos_sensors_poll(struct sensors_poll_device_t *dev, - struct sensors_event_t* data, int count); - -/* - * Input - */ - -void input_event_set(struct input_event *event, int type, int code, int value); -long int timestamp(struct timeval *time); -long int input_timestamp(struct input_event *event); -int uinput_rel_create(const char *name); -void uinput_destroy(int uinput_fd); -int input_open(char *name); -int sysfs_path_prefix(char *name, char *path_prefix); -int sysfs_value_read(char *path); -int sysfs_value_write(char *path, int value); -int sysfs_string_read(char *path, char *buffer, int length); -int sysfs_string_write(char *path, char *buffer, int length); - -/* - * Sensors - */ - -int orientation_fill(struct exynos_sensors_handlers *handlers, - sensors_vec_t *acceleration, sensors_vec_t *magnetic); - -extern struct exynos_sensors_handlers lsm330dlc_acceleration; -extern struct exynos_sensors_handlers akm8975; -extern struct exynos_sensors_handlers orientation; -extern struct exynos_sensors_handlers cm36651_proximity; -extern struct exynos_sensors_handlers cm36651_light; -extern struct exynos_sensors_handlers lsm330dlc_gyroscope; -extern struct exynos_sensors_handlers lps331ap; - -#endif diff --git a/libsensors/input.c b/libsensors/input.c index f1e5283..5a58b45 100644 --- a/libsensors/input.c +++ b/libsensors/input.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,10 +25,10 @@ #include <linux/input.h> #include <linux/uinput.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" void input_event_set(struct input_event *event, int type, int code, int value) { @@ -276,12 +276,12 @@ complete: return rc; } -int sysfs_string_read(char *path, char *buffer, int length) +int sysfs_string_read(char *path, char *buffer, size_t length) { int fd = -1; int rc; - if (path == NULL || buffer == NULL || length <= 0) + if (path == NULL || buffer == NULL || length == 0) return -1; fd = open(path, O_RDONLY); @@ -305,12 +305,12 @@ complete: return rc; } -int sysfs_string_write(char *path, char *buffer, int length) +int sysfs_string_write(char *path, char *buffer, size_t length) { int fd = -1; int rc; - if (path == NULL || buffer == NULL || length <= 0) + if (path == NULL || buffer == NULL || length == 0) return -1; fd = open(path, O_WRONLY); diff --git a/libsensors/lps331ap.c b/libsensors/lps331ap.c index aed233d..5c3007b 100644 --- a/libsensors/lps331ap.c +++ b/libsensors/lps331ap.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,26 +20,26 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> +#include <math.h> #include <sys/types.h> #include <linux/ioctl.h> -#include <linux/uinput.h> #include <linux/input.h> #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" struct lps331ap_data { char path_enable[PATH_MAX]; char path_delay[PATH_MAX]; }; -int lps331ap_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int lps331ap_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct lps331ap_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -86,7 +86,7 @@ error: return -1; } -int lps331ap_deinit(struct exynos_sensors_handlers *handlers) +int lps331ap_deinit(struct smdk4x12_sensors_handlers *handlers) { ALOGD("%s(%p)", __func__, handlers); @@ -104,8 +104,7 @@ int lps331ap_deinit(struct exynos_sensors_handlers *handlers) return 0; } - -int lps331ap_activate(struct exynos_sensors_handlers *handlers) +int lps331ap_activate(struct smdk4x12_sensors_handlers *handlers) { struct lps331ap_data *data; int rc; @@ -128,7 +127,7 @@ int lps331ap_activate(struct exynos_sensors_handlers *handlers) return 0; } -int lps331ap_deactivate(struct exynos_sensors_handlers *handlers) +int lps331ap_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct lps331ap_data *data; int rc; @@ -151,9 +150,10 @@ int lps331ap_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int lps331ap_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int lps331ap_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { struct lps331ap_data *data; + int d; int rc; ALOGD("%s(%p, %ld)", __func__, handlers, delay); @@ -163,7 +163,12 @@ int lps331ap_set_delay(struct exynos_sensors_handlers *handlers, long int delay) data = (struct lps331ap_data *) handlers->data; - rc = sysfs_value_write(data->path_delay, (int) delay / 1000000); + if (delay < 10000000) + d = 10; + else + d = delay / 1000000; + + rc = sysfs_value_write(data->path_delay, d); if (rc < 0) { ALOGE("%s: Unable to write sysfs value", __func__); return -1; @@ -174,10 +179,10 @@ int lps331ap_set_delay(struct exynos_sensors_handlers *handlers, long int delay) float lps331ap_convert(int value) { - return (float) value / 4096.0f; + return value / 4096.0f; } -int lps331ap_get_data(struct exynos_sensors_handlers *handlers, +int lps331ap_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { struct input_event input_event; @@ -193,6 +198,7 @@ int lps331ap_get_data(struct exynos_sensors_handlers *handlers, if (input_fd < 0) return -EINVAL; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; @@ -203,18 +209,27 @@ int lps331ap_get_data(struct exynos_sensors_handlers *handlers, break; if (input_event.type == EV_REL) { - if (input_event.code == REL_X) - event->pressure = lps331ap_convert(input_event.value); + switch (input_event.code) { + case REL_X: + event->pressure = lps331ap_convert(input_event.value); + break; + default: + continue; + } } else if (input_event.type == EV_SYN) { - if (input_event.code == SYN_REPORT) + if (input_event.code == SYN_REPORT && event->pressure != 0) { event->timestamp = input_timestamp(&input_event); + break; + } else { + return -1; + } } - } while (input_event.type != EV_SYN); + } while (1); return 0; } -struct exynos_sensors_handlers lps331ap = { +struct smdk4x12_sensors_handlers lps331ap = { .name = "LPS331AP", .handle = SENSOR_TYPE_PRESSURE, .init = lps331ap_init, diff --git a/libsensors/lsm330dlc_acceleration.c b/libsensors/lsm330dlc_acceleration.c index 435faf8..4289eaf 100644 --- a/libsensors/lsm330dlc_acceleration.c +++ b/libsensors/lsm330dlc_acceleration.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,22 +20,18 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> -#include <sys/types.h> -#include <linux/ioctl.h> -#include <linux/uinput.h> -#include <linux/input.h> #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" #include "lsm330dlc_accel.h" struct lsm330dlc_acceleration_data { - struct exynos_sensors_handlers *orientation_sensor; + struct smdk4x12_sensors_handlers *orientation_sensor; long int delay; int device_fd; @@ -48,11 +44,11 @@ struct lsm330dlc_acceleration_data { void *lsm330dlc_acceleration_thread(void *thread_data) { - struct exynos_sensors_handlers *handlers = NULL; + struct smdk4x12_sensors_handlers *handlers = NULL; struct lsm330dlc_acceleration_data *data = NULL; - struct lsm330dlc_acc values; struct input_event event; struct timeval time; + struct lsm330dlc_acc acceleration_data; long int before, after; int diff; int device_fd; @@ -62,7 +58,7 @@ void *lsm330dlc_acceleration_thread(void *thread_data) if (thread_data == NULL) return NULL; - handlers = (struct exynos_sensors_handlers *) thread_data; + handlers = (struct smdk4x12_sensors_handlers *) thread_data; if (handlers->data == NULL) return NULL; @@ -85,22 +81,24 @@ void *lsm330dlc_acceleration_thread(void *thread_data) gettimeofday(&time, NULL); before = timestamp(&time); - memset(&values, 0, sizeof(values)); - rc = ioctl(device_fd, LSM330DLC_ACCEL_IOCTL_READ_XYZ, &values); + memset(&acceleration_data, 0, sizeof(acceleration_data)); + + rc = ioctl(device_fd, LSM330DLC_ACCEL_IOCTL_READ_XYZ, &acceleration_data); if (rc < 0) { - ALOGE("%s: Unable to set read XYZ", __func__); + ALOGE("%s: Unable to get lsm330dlc acceleration data", __func__); return NULL; } - input_event_set(&event, EV_REL, REL_X, (int) values.x); + input_event_set(&event, EV_REL, REL_X, (int) (acceleration_data.x * 1000)); write(uinput_fd, &event, sizeof(event)); - input_event_set(&event, EV_REL, REL_Y, (int) values.y); + input_event_set(&event, EV_REL, REL_Y, (int) (acceleration_data.y * 1000)); write(uinput_fd, &event, sizeof(event)); - input_event_set(&event, EV_REL, REL_Z, (int) values.z); + input_event_set(&event, EV_REL, REL_Z, (int) (acceleration_data.z * 1000)); write(uinput_fd, &event, sizeof(event)); input_event_set(&event, EV_SYN, 0, 0); write(uinput_fd, &event, sizeof(event)); +next: gettimeofday(&time, NULL); after = timestamp(&time); @@ -114,8 +112,8 @@ void *lsm330dlc_acceleration_thread(void *thread_data) return NULL; } -int lsm330dlc_acceleration_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int lsm330dlc_acceleration_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct lsm330dlc_acceleration_data *data = NULL; pthread_attr_t thread_attr; @@ -127,7 +125,7 @@ int lsm330dlc_acceleration_init(struct exynos_sensors_handlers *handlers, ALOGD("%s(%p, %p)", __func__, handlers, device); - if (handlers == NULL) + if (handlers == NULL || device == NULL) return -EINVAL; data = (struct lsm330dlc_acceleration_data *) calloc(1, sizeof(struct lsm330dlc_acceleration_data)); @@ -168,7 +166,7 @@ int lsm330dlc_acceleration_init(struct exynos_sensors_handlers *handlers, rc = pthread_create(&data->thread, &thread_attr, lsm330dlc_acceleration_thread, (void *) handlers); if (rc < 0) { - ALOGE("%s: Unable to create acceleration thread", __func__); + ALOGE("%s: Unable to create lsm330dlc acceleration thread", __func__); pthread_mutex_destroy(&data->mutex); goto error; } @@ -199,9 +197,9 @@ error: return -1; } -int lsm330dlc_acceleration_deinit(struct exynos_sensors_handlers *handlers) +int lsm330dlc_acceleration_deinit(struct smdk4x12_sensors_handlers *handlers) { - struct lsm330dlc_acceleration_data *data; + struct lsm330dlc_acceleration_data *data = NULL; ALOGD("%s(%p)", __func__, handlers); @@ -236,7 +234,7 @@ int lsm330dlc_acceleration_deinit(struct exynos_sensors_handlers *handlers) return 0; } -int lsm330dlc_acceleration_activate(struct exynos_sensors_handlers *handlers) +int lsm330dlc_acceleration_activate(struct smdk4x12_sensors_handlers *handlers) { struct lsm330dlc_acceleration_data *data; int device_fd; @@ -252,22 +250,22 @@ int lsm330dlc_acceleration_activate(struct exynos_sensors_handlers *handlers) device_fd = data->device_fd; if (device_fd < 0) - return -EINVAL; + return -1; enable = 1; rc = ioctl(device_fd, LSM330DLC_ACCEL_IOCTL_SET_ENABLE, &enable); if (rc < 0) { - ALOGE("%s: Unable to set enable", __func__); + ALOGE("%s: Unable to set lsm330dlc acceleration enable", __func__); return -1; } - + handlers->activated = 1; pthread_mutex_unlock(&data->mutex); return 0; } -int lsm330dlc_acceleration_deactivate(struct exynos_sensors_handlers *handlers) +int lsm330dlc_acceleration_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct lsm330dlc_acceleration_data *data; int device_fd; @@ -283,12 +281,12 @@ int lsm330dlc_acceleration_deactivate(struct exynos_sensors_handlers *handlers) device_fd = data->device_fd; if (device_fd < 0) - return -EINVAL; + return -1; enable = 0; rc = ioctl(device_fd, LSM330DLC_ACCEL_IOCTL_SET_ENABLE, &enable); if (rc < 0) { - ALOGE("%s: Unable to set enable", __func__); + ALOGE("%s: Unable to set lsm330dlc acceleration enable", __func__); return -1; } @@ -297,10 +295,10 @@ int lsm330dlc_acceleration_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int lsm330dlc_acceleration_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int lsm330dlc_acceleration_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { struct lsm330dlc_acceleration_data *data; - unsigned long long d; + int64_t d; int device_fd; int rc; @@ -313,12 +311,12 @@ int lsm330dlc_acceleration_set_delay(struct exynos_sensors_handlers *handlers, l device_fd = data->device_fd; if (device_fd < 0) - return -EINVAL; + return -1; - d = (unsigned long long) delay; + d = (int64_t) delay; rc = ioctl(device_fd, LSM330DLC_ACCEL_IOCTL_SET_DELAY, &d); if (rc < 0) { - ALOGE("%s: Unable to set delay", __func__); + ALOGE("%s: Unable to set lsm330dlc acceleration delay", __func__); return -1; } @@ -329,10 +327,10 @@ int lsm330dlc_acceleration_set_delay(struct exynos_sensors_handlers *handlers, l float lsm330dlc_acceleration_convert(int value) { - return (float) (value * GRAVITY_EARTH) / 1024.0f; + return (float) (value / 1000.f) * (GRAVITY_EARTH / 1024.0f); } -int lsm330dlc_acceleration_get_data(struct exynos_sensors_handlers *handlers, +int lsm330dlc_acceleration_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { struct lsm330dlc_acceleration_data *data; @@ -349,13 +347,14 @@ int lsm330dlc_acceleration_get_data(struct exynos_sensors_handlers *handlers, input_fd = handlers->poll_fd; if (input_fd < 0) - return -EINVAL; + return -1; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; - event->acceleration.status = SENSOR_STATUS_ACCURACY_MEDIUM; + event->magnetic.status = SENSOR_STATUS_ACCURACY_MEDIUM; do { rc = read(input_fd, &input_event, sizeof(input_event)); @@ -388,7 +387,7 @@ int lsm330dlc_acceleration_get_data(struct exynos_sensors_handlers *handlers, return 0; } -struct exynos_sensors_handlers lsm330dlc_acceleration = { +struct smdk4x12_sensors_handlers lsm330dlc_acceleration = { .name = "LSM330DLC Acceleration", .handle = SENSOR_TYPE_ACCELEROMETER, .init = lsm330dlc_acceleration_init, diff --git a/libsensors/lsm330dlc_gyroscope.c b/libsensors/lsm330dlc_gyroscope.c index a741a21..6a7f98d 100644 --- a/libsensors/lsm330dlc_gyroscope.c +++ b/libsensors/lsm330dlc_gyroscope.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,26 +20,28 @@ #include <stdint.h> #include <fcntl.h> #include <errno.h> +#include <math.h> #include <sys/types.h> #include <linux/ioctl.h> -#include <linux/uinput.h> #include <linux/input.h> #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" struct lsm330dlc_gyroscope_data { char path_enable[PATH_MAX]; char path_delay[PATH_MAX]; + + sensors_vec_t gyro; }; -int lsm330dlc_gyroscope_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int lsm330dlc_gyroscope_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct lsm330dlc_gyroscope_data *data = NULL; char path[PATH_MAX] = { 0 }; @@ -86,7 +88,7 @@ error: return -1; } -int lsm330dlc_gyroscope_deinit(struct exynos_sensors_handlers *handlers) +int lsm330dlc_gyroscope_deinit(struct smdk4x12_sensors_handlers *handlers) { ALOGD("%s(%p)", __func__, handlers); @@ -104,8 +106,7 @@ int lsm330dlc_gyroscope_deinit(struct exynos_sensors_handlers *handlers) return 0; } - -int lsm330dlc_gyroscope_activate(struct exynos_sensors_handlers *handlers) +int lsm330dlc_gyroscope_activate(struct smdk4x12_sensors_handlers *handlers) { struct lsm330dlc_gyroscope_data *data; int rc; @@ -128,7 +129,7 @@ int lsm330dlc_gyroscope_activate(struct exynos_sensors_handlers *handlers) return 0; } -int lsm330dlc_gyroscope_deactivate(struct exynos_sensors_handlers *handlers) +int lsm330dlc_gyroscope_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct lsm330dlc_gyroscope_data *data; int rc; @@ -151,7 +152,7 @@ int lsm330dlc_gyroscope_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int lsm330dlc_gyroscope_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int lsm330dlc_gyroscope_set_delay(struct smdk4x12_sensors_handlers *handlers, long int delay) { struct lsm330dlc_gyroscope_data *data; int rc; @@ -174,29 +175,37 @@ int lsm330dlc_gyroscope_set_delay(struct exynos_sensors_handlers *handlers, long float lsm330dlc_gyroscope_convert(int value) { - return ((float) value * 0.3054326f) / 1000.0f; + return value * (70.0f / 4000.0f) * (3.1415926535f / 180.0f); } -int lsm330dlc_gyroscope_get_data(struct exynos_sensors_handlers *handlers, +int lsm330dlc_gyroscope_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { + struct lsm330dlc_gyroscope_data *data; struct input_event input_event; int input_fd; int rc; // ALOGD("%s(%p, %p)", __func__, handlers, event); - if (handlers == NULL || event == NULL) + if (handlers == NULL || handlers->data == NULL || event == NULL) return -EINVAL; + data = (struct lsm330dlc_gyroscope_data *) handlers->data; + input_fd = handlers->poll_fd; if (input_fd < 0) return -EINVAL; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; + event->gyro.x = data->gyro.x; + event->gyro.y = data->gyro.y; + event->gyro.z = data->gyro.z; + do { rc = read(input_fd, &input_event, sizeof(input_event)); if (rc < (int) sizeof(input_event)) @@ -205,13 +214,13 @@ int lsm330dlc_gyroscope_get_data(struct exynos_sensors_handlers *handlers, if (input_event.type == EV_REL) { switch (input_event.code) { case REL_RX: - event->magnetic.x = lsm330dlc_gyroscope_convert(input_event.value); + event->gyro.x = lsm330dlc_gyroscope_convert(input_event.value); break; case REL_RY: - event->magnetic.y = lsm330dlc_gyroscope_convert(input_event.value); + event->gyro.y = lsm330dlc_gyroscope_convert(input_event.value); break; case REL_RZ: - event->magnetic.z = lsm330dlc_gyroscope_convert(input_event.value); + event->gyro.z = lsm330dlc_gyroscope_convert(input_event.value); break; default: continue; @@ -222,10 +231,14 @@ int lsm330dlc_gyroscope_get_data(struct exynos_sensors_handlers *handlers, } } while (input_event.type != EV_SYN); + data->gyro.x = event->gyro.x; + data->gyro.y = event->gyro.y; + data->gyro.z = event->gyro.z; + return 0; } -struct exynos_sensors_handlers lsm330dlc_gyroscope = { +struct smdk4x12_sensors_handlers lsm330dlc_gyroscope = { .name = "LSM330DLC Gyroscope", .handle = SENSOR_TYPE_GYROSCOPE, .init = lsm330dlc_gyroscope_init, diff --git a/libsensors/orientation.c b/libsensors/orientation.c index 954c2b9..e3529bd 100644 --- a/libsensors/orientation.c +++ b/libsensors/orientation.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Paul Kocialkowski + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,14 +28,14 @@ #include <hardware/sensors.h> #include <hardware/hardware.h> -#define LOG_TAG "exynos_sensors" +#define LOG_TAG "smdk4x12_sensors" #include <utils/Log.h> -#include "exynos_sensors.h" +#include "smdk4x12_sensors.h" struct orientation_data { - struct exynos_sensors_handlers *acceleration_sensor; - struct exynos_sensors_handlers *magnetic_sensor; + struct smdk4x12_sensors_handlers *acceleration_sensor; + struct smdk4x12_sensors_handlers *magnetic_sensor; sensors_vec_t orientation; sensors_vec_t acceleration; @@ -85,17 +85,17 @@ void orientation_calculate(sensors_vec_t *a, sensors_vec_t *m, sensors_vec_t *o) x = m->x * sinp * sinr + m->y * cosp + m->z * sinp * cosr; azimuth = atan2f(y, x); - o->x = rad2deg(azimuth); - o->y = rad2deg(pitch); - o->z = rad2deg(roll); + o->azimuth = rad2deg(azimuth); + o->pitch = rad2deg(pitch); + o->roll = rad2deg(roll); - if (o->x < 0) - o->x += 360.0f; + if (o->azimuth < 0) + o->azimuth += 360.0f; } void *orientation_thread(void *thread_data) { - struct exynos_sensors_handlers *handlers = NULL; + struct smdk4x12_sensors_handlers *handlers = NULL; struct orientation_data *data = NULL; struct input_event event; struct timeval time; @@ -106,7 +106,7 @@ void *orientation_thread(void *thread_data) if (thread_data == NULL) return NULL; - handlers = (struct exynos_sensors_handlers *) thread_data; + handlers = (struct smdk4x12_sensors_handlers *) thread_data; if (handlers->data == NULL) return NULL; @@ -127,11 +127,11 @@ void *orientation_thread(void *thread_data) orientation_calculate(&data->acceleration, &data->magnetic, &data->orientation); - input_event_set(&event, EV_REL, REL_X, (int) (data->orientation.x * 1000)); + input_event_set(&event, EV_REL, REL_X, (int) (data->orientation.azimuth * 1000)); write(uinput_fd, &event, sizeof(event)); - input_event_set(&event, EV_REL, REL_Y, (int) (data->orientation.y * 1000)); + input_event_set(&event, EV_REL, REL_Y, (int) (data->orientation.pitch * 1000)); write(uinput_fd, &event, sizeof(event)); - input_event_set(&event, EV_REL, REL_Z, (int) (data->orientation.z * 1000)); + input_event_set(&event, EV_REL, REL_Z, (int) (data->orientation.roll * 1000)); write(uinput_fd, &event, sizeof(event)); input_event_set(&event, EV_SYN, 0, 0); write(uinput_fd, &event, sizeof(event)); @@ -150,7 +150,7 @@ void *orientation_thread(void *thread_data) return NULL; } -int orientation_fill(struct exynos_sensors_handlers *handlers, +int orientation_fill(struct smdk4x12_sensors_handlers *handlers, sensors_vec_t *acceleration, sensors_vec_t *magnetic) { struct orientation_data *data; @@ -177,8 +177,8 @@ int orientation_fill(struct exynos_sensors_handlers *handlers, return 0; } -int orientation_init(struct exynos_sensors_handlers *handlers, - struct exynos_sensors_device *device) +int orientation_init(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device) { struct orientation_data *data = NULL; pthread_attr_t thread_attr; @@ -258,7 +258,7 @@ error: return -1; } -int orientation_deinit(struct exynos_sensors_handlers *handlers) +int orientation_deinit(struct smdk4x12_sensors_handlers *handlers) { struct orientation_data *data; @@ -291,7 +291,7 @@ int orientation_deinit(struct exynos_sensors_handlers *handlers) return 0; } -int orientation_activate(struct exynos_sensors_handlers *handlers) +int orientation_activate(struct smdk4x12_sensors_handlers *handlers) { struct orientation_data *data; @@ -305,12 +305,12 @@ int orientation_activate(struct exynos_sensors_handlers *handlers) if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) return -1; - data->acceleration_sensor->needed |= EXYNOS_SENSORS_NEEDED_ORIENTATION; - if (data->acceleration_sensor->needed == EXYNOS_SENSORS_NEEDED_ORIENTATION) + data->acceleration_sensor->needed |= SMDK4x12_SENSORS_NEEDED_ORIENTATION; + if (data->acceleration_sensor->needed == SMDK4x12_SENSORS_NEEDED_ORIENTATION) data->acceleration_sensor->activate(data->acceleration_sensor); - data->magnetic_sensor->needed |= EXYNOS_SENSORS_NEEDED_ORIENTATION; - if (data->magnetic_sensor->needed == EXYNOS_SENSORS_NEEDED_ORIENTATION) + data->magnetic_sensor->needed |= SMDK4x12_SENSORS_NEEDED_ORIENTATION; + if (data->magnetic_sensor->needed == SMDK4x12_SENSORS_NEEDED_ORIENTATION) data->magnetic_sensor->activate(data->magnetic_sensor); handlers->activated = 1; @@ -319,7 +319,7 @@ int orientation_activate(struct exynos_sensors_handlers *handlers) return 0; } -int orientation_deactivate(struct exynos_sensors_handlers *handlers) +int orientation_deactivate(struct smdk4x12_sensors_handlers *handlers) { struct orientation_data *data; @@ -333,11 +333,11 @@ int orientation_deactivate(struct exynos_sensors_handlers *handlers) if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) return -1; - data->acceleration_sensor->needed &= ~(EXYNOS_SENSORS_NEEDED_ORIENTATION); + data->acceleration_sensor->needed &= ~(SMDK4x12_SENSORS_NEEDED_ORIENTATION); if (data->acceleration_sensor->needed == 0) data->acceleration_sensor->deactivate(data->acceleration_sensor); - data->magnetic_sensor->needed &= ~(EXYNOS_SENSORS_NEEDED_ORIENTATION); + data->magnetic_sensor->needed &= ~(SMDK4x12_SENSORS_NEEDED_ORIENTATION); if (data->magnetic_sensor->needed == 0) data->magnetic_sensor->deactivate(data->magnetic_sensor); @@ -346,7 +346,8 @@ int orientation_deactivate(struct exynos_sensors_handlers *handlers) return 0; } -int orientation_set_delay(struct exynos_sensors_handlers *handlers, long int delay) +int orientation_set_delay(struct smdk4x12_sensors_handlers *handlers, + long int delay) { struct orientation_data *data; @@ -360,10 +361,10 @@ int orientation_set_delay(struct exynos_sensors_handlers *handlers, long int del if (data->acceleration_sensor == NULL || data->magnetic_sensor == NULL) return -1; - if (data->acceleration_sensor->needed == EXYNOS_SENSORS_NEEDED_ORIENTATION) + if (data->acceleration_sensor->needed == SMDK4x12_SENSORS_NEEDED_ORIENTATION) data->acceleration_sensor->set_delay(data->acceleration_sensor, delay); - if (data->magnetic_sensor->needed == EXYNOS_SENSORS_NEEDED_ORIENTATION) + if (data->magnetic_sensor->needed == SMDK4x12_SENSORS_NEEDED_ORIENTATION) data->magnetic_sensor->set_delay(data->magnetic_sensor, delay); data->delay = delay; @@ -376,25 +377,23 @@ float orientation_convert(int value) return (float) value / 1000.0f; } -int orientation_get_data(struct exynos_sensors_handlers *handlers, +int orientation_get_data(struct smdk4x12_sensors_handlers *handlers, struct sensors_event_t *event) { - struct orientation_data *data; struct input_event input_event; int input_fd = -1; int rc; // ALOGD("%s(%p, %p)", __func__, handlers, event); - if (handlers == NULL || handlers->data == NULL || event == NULL) + if (handlers == NULL || event == NULL) return -EINVAL; - data = (struct orientation_data *) handlers->data; - input_fd = handlers->poll_fd; if (input_fd < 0) return -EINVAL; + memset(event, 0, sizeof(struct sensors_event_t)); event->version = sizeof(struct sensors_event_t); event->sensor = handlers->handle; event->type = handlers->handle; @@ -409,13 +408,13 @@ int orientation_get_data(struct exynos_sensors_handlers *handlers, if (input_event.type == EV_REL) { switch (input_event.code) { case REL_X: - event->orientation.x = orientation_convert(input_event.value); + event->orientation.azimuth = orientation_convert(input_event.value); break; case REL_Y: - event->orientation.y = orientation_convert(input_event.value); + event->orientation.pitch = orientation_convert(input_event.value); break; case REL_Z: - event->orientation.z = orientation_convert(input_event.value); + event->orientation.roll = orientation_convert(input_event.value); break; default: continue; @@ -429,7 +428,7 @@ int orientation_get_data(struct exynos_sensors_handlers *handlers, return 0; } -struct exynos_sensors_handlers orientation = { +struct smdk4x12_sensors_handlers orientation = { .name = "Orientation", .handle = SENSOR_TYPE_ORIENTATION, .init = orientation_init, diff --git a/libsensors/smdk4x12_sensors.c b/libsensors/smdk4x12_sensors.c new file mode 100644 index 0000000..3cfdeaf --- /dev/null +++ b/libsensors/smdk4x12_sensors.c @@ -0,0 +1,299 @@ +/* + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> +#include <fcntl.h> +#include <errno.h> +#include <poll.h> +#include <sys/select.h> +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#define LOG_TAG "smdk4x12_sensors" +#include <utils/Log.h> + +#include "smdk4x12_sensors.h" + +/* + * Sensors list + */ + +struct sensor_t smdk4x12_sensors[] = { + { "LSM330DLC Acceleration Sensor", "STMicroelectronics", 1, SENSOR_TYPE_ACCELEROMETER, + SENSOR_TYPE_ACCELEROMETER, 2 * GRAVITY_EARTH, 0.0096f, 0.23f, 10000, 0, 0, SENSOR_STRING_TYPE_ACCELEROMETER, 0, 0, + SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "AKM8975 Magnetic Sensor", "Asahi Kasei", 1, SENSOR_TYPE_MAGNETIC_FIELD, + SENSOR_TYPE_MAGNETIC_FIELD, 2000.0f, 1.0f / 16, 6.8f, 10000, 0, 0, SENSOR_STRING_TYPE_MAGNETIC_FIELD, 0, 0, + SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "Orientation Sensor", "Exynos Sensors", 1, SENSOR_TYPE_ORIENTATION, + SENSOR_TYPE_ORIENTATION, 360.0f, 0.1f, 0.0f, 10000, 0, 0, SENSOR_STRING_TYPE_ORIENTATION, 0, 0, + SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "CM36651 Light Sensor", "Capella", 1, SENSOR_TYPE_LIGHT, + SENSOR_TYPE_LIGHT, 121240.0f, 1.0f, 0.2f, 0, 0, 0, SENSOR_STRING_TYPE_LIGHT, 0, 0, + SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "CM36651 Proximity Sensor", "Capella", 1, SENSOR_TYPE_PROXIMITY, + SENSOR_TYPE_PROXIMITY, 8.0f, 8.0f, 1.3f, 0, 0, 0, SENSOR_STRING_TYPE_PROXIMITY, 0, 0, + SENSOR_FLAG_WAKE_UP | SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "LSM330DLC Gyroscope Sensor", "STMicroelectronics", 1, SENSOR_TYPE_GYROSCOPE, + SENSOR_TYPE_GYROSCOPE, 500.0f * (3.1415926535f / 180.0f), (70.0f / 4000.0f) * (3.1415926535f / 180.0f), 6.1f, 5000, 0, 0, SENSOR_STRING_TYPE_GYROSCOPE, 0, 0, + SENSOR_FLAG_ON_CHANGE_MODE, {}, }, + { "LPS331AP Pressure Sensor", "STMicroelectronics", 1, SENSOR_TYPE_PRESSURE, + SENSOR_TYPE_PRESSURE, 1260.0f, 1.0f / 4096, 0.045f, 40000, 0, 0, SENSOR_STRING_TYPE_PRESSURE, 0, 20000, + SENSOR_FLAG_CONTINUOUS_MODE, {}, }, +}; + +int smdk4x12_sensors_count = sizeof(smdk4x12_sensors) / sizeof(struct sensor_t); + +struct smdk4x12_sensors_handlers *smdk4x12_sensors_handlers[] = { + &lsm330dlc_acceleration, + &akm8975, + &orientation, + &cm36651_proximity, + &cm36651_light, + &lsm330dlc_gyroscope, + &lps331ap, +}; + +int smdk4x12_sensors_handlers_count = sizeof(smdk4x12_sensors_handlers) / + sizeof(struct smdk4x12_sensors_handlers *); + +/* + * SMDK4x12 Sensors + */ + +int smdk4x12_sensors_activate(struct sensors_poll_device_t *dev, int handle, + int enabled) +{ + struct smdk4x12_sensors_device *device; + int i; + + ALOGD("%s(%p, %d, %d)", __func__, dev, handle, enabled); + + if (dev == NULL) + return -EINVAL; + + device = (struct smdk4x12_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0) + return -EINVAL; + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == handle) { + if (enabled && device->handlers[i]->activate != NULL) { + device->handlers[i]->needed |= SMDK4x12_SENSORS_NEEDED_API; + if (device->handlers[i]->needed == SMDK4x12_SENSORS_NEEDED_API) + return device->handlers[i]->activate(device->handlers[i]); + else + return 0; + } else if (!enabled && device->handlers[i]->deactivate != NULL) { + device->handlers[i]->needed &= ~SMDK4x12_SENSORS_NEEDED_API; + if (device->handlers[i]->needed == 0) + return device->handlers[i]->deactivate(device->handlers[i]); + else + return 0; + } + } + } + + return -1; +} + +int smdk4x12_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, + int64_t ns) +{ + struct smdk4x12_sensors_device *device; + int i; + + ALOGD("%s(%p, %d, %ld)", __func__, dev, handle, (long int) ns); + + if (dev == NULL) + return -EINVAL; + + device = (struct smdk4x12_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0) + return -EINVAL; + + for (i = 0; i < device->handlers_count; i++) { + if (device->handlers[i] == NULL) + continue; + + if (device->handlers[i]->handle == handle && device->handlers[i]->set_delay != NULL) + return device->handlers[i]->set_delay(device->handlers[i], (long int) ns); + } + + return 0; +} + +int smdk4x12_sensors_poll(struct sensors_poll_device_t *dev, + struct sensors_event_t* data, int count) +{ + struct smdk4x12_sensors_device *device; + int i, j; + int c, n; + int poll_rc, rc; + +// ALOGD("%s(%p, %p, %d)", __func__, dev, data, count); + + if (dev == NULL) + return -EINVAL; + + device = (struct smdk4x12_sensors_device *) dev; + + if (device->handlers == NULL || device->handlers_count <= 0 || + device->poll_fds == NULL || device->poll_fds_count <= 0) + return -EINVAL; + + n = 0; + + do { + poll_rc = poll(device->poll_fds, device->poll_fds_count, n > 0 ? 0 : -1); + if (poll_rc < 0) + return -1; + + for (i = 0; i < device->poll_fds_count; i++) { + if (!(device->poll_fds[i].revents & POLLIN)) + continue; + + for (j = 0; j < device->handlers_count; j++) { + if (device->handlers[j] == NULL || device->handlers[j]->poll_fd != device->poll_fds[i].fd || device->handlers[j]->get_data == NULL) + continue; + + rc = device->handlers[j]->get_data(device->handlers[j], &data[n]); + if (rc < 0) { + device->poll_fds[i].revents = 0; + poll_rc = -1; + } else { + n++; + count--; + } + } + } + } while ((poll_rc > 0 || n < 1) && count > 0); + + return n; +} + +/* + * Interface + */ + +int smdk4x12_sensors_close(hw_device_t *device) +{ + struct smdk4x12_sensors_device *smdk4x12_sensors_device; + int i; + + ALOGD("%s(%p)", __func__, device); + + if (device == NULL) + return -EINVAL; + + smdk4x12_sensors_device = (struct smdk4x12_sensors_device *) device; + + if (smdk4x12_sensors_device->poll_fds != NULL) + free(smdk4x12_sensors_device->poll_fds); + + for (i = 0; i < smdk4x12_sensors_device->handlers_count; i++) { + if (smdk4x12_sensors_device->handlers[i] == NULL || smdk4x12_sensors_device->handlers[i]->deinit == NULL) + continue; + + smdk4x12_sensors_device->handlers[i]->deinit(smdk4x12_sensors_device->handlers[i]); + } + + free(device); + + return 0; +} + +int smdk4x12_sensors_open(const struct hw_module_t* module, const char *id, + struct hw_device_t** device) +{ + struct smdk4x12_sensors_device *smdk4x12_sensors_device; + int p, i; + + ALOGD("%s(%p, %s, %p)", __func__, module, id, device); + + if (module == NULL || device == NULL) + return -EINVAL; + + smdk4x12_sensors_device = (struct smdk4x12_sensors_device *) + calloc(1, sizeof(struct smdk4x12_sensors_device)); + smdk4x12_sensors_device->device.common.tag = HARDWARE_DEVICE_TAG; + smdk4x12_sensors_device->device.common.version = 0; + smdk4x12_sensors_device->device.common.module = (struct hw_module_t *) module; + smdk4x12_sensors_device->device.common.close = smdk4x12_sensors_close; + smdk4x12_sensors_device->device.activate = smdk4x12_sensors_activate; + smdk4x12_sensors_device->device.setDelay = smdk4x12_sensors_set_delay; + smdk4x12_sensors_device->device.poll = smdk4x12_sensors_poll; + smdk4x12_sensors_device->handlers = smdk4x12_sensors_handlers; + smdk4x12_sensors_device->handlers_count = smdk4x12_sensors_handlers_count; + smdk4x12_sensors_device->poll_fds = (struct pollfd *) + calloc(1, smdk4x12_sensors_handlers_count * sizeof(struct pollfd)); + + p = 0; + for (i = 0; i < smdk4x12_sensors_handlers_count; i++) { + if (smdk4x12_sensors_handlers[i] == NULL || smdk4x12_sensors_handlers[i]->init == NULL) + continue; + + smdk4x12_sensors_handlers[i]->init(smdk4x12_sensors_handlers[i], smdk4x12_sensors_device); + if (smdk4x12_sensors_handlers[i]->poll_fd >= 0) { + smdk4x12_sensors_device->poll_fds[p].fd = smdk4x12_sensors_handlers[i]->poll_fd; + smdk4x12_sensors_device->poll_fds[p].events = POLLIN; + p++; + } + } + + smdk4x12_sensors_device->poll_fds_count = p; + + *device = &(smdk4x12_sensors_device->device.common); + + return 0; +} + +int smdk4x12_sensors_get_sensors_list(struct sensors_module_t* module, + const struct sensor_t **sensors_p) +{ + ALOGD("%s(%p, %p)", __func__, module, sensors_p); + + if (sensors_p == NULL) + return -EINVAL; + + *sensors_p = smdk4x12_sensors; + return smdk4x12_sensors_count; +} + +struct hw_module_methods_t smdk4x12_sensors_module_methods = { + .open = smdk4x12_sensors_open, +}; + +struct sensors_module_t HAL_MODULE_INFO_SYM = { + .common = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = SENSORS_HARDWARE_MODULE_ID, + .name = "SMDK4x12 Sensors", + .author = "Paul Kocialkowski", + .methods = &smdk4x12_sensors_module_methods, + }, + .get_sensors_list = smdk4x12_sensors_get_sensors_list, +}; diff --git a/libsensors/smdk4x12_sensors.h b/libsensors/smdk4x12_sensors.h new file mode 100644 index 0000000..984834a --- /dev/null +++ b/libsensors/smdk4x12_sensors.h @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2013 Paul Kocialkowski <contact@paulk.fr> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <stdint.h> +#include <poll.h> +#include <linux/input.h> + +#include <hardware/sensors.h> +#include <hardware/hardware.h> + +#ifndef _SMDK4x12_SENSORS_H_ +#define _SMDK4x12_SENSORS_H_ + +#define SMDK4x12_SENSORS_NEEDED_API (1 << 0) +#define SMDK4x12_SENSORS_NEEDED_ORIENTATION (1 << 1) + +struct smdk4x12_sensors_device; + +struct smdk4x12_sensors_handlers { + char *name; + int handle; + + int (*init)(struct smdk4x12_sensors_handlers *handlers, + struct smdk4x12_sensors_device *device); + int (*deinit)(struct smdk4x12_sensors_handlers *handlers); + int (*activate)(struct smdk4x12_sensors_handlers *handlers); + int (*deactivate)(struct smdk4x12_sensors_handlers *handlers); + int (*set_delay)(struct smdk4x12_sensors_handlers *handlers, + long int delay); + int (*get_data)(struct smdk4x12_sensors_handlers *handlers, + struct sensors_event_t *event); + + int activated; + int needed; + int poll_fd; + + void *data; +}; + +struct smdk4x12_sensors_device { + struct sensors_poll_device_t device; + + struct smdk4x12_sensors_handlers **handlers; + int handlers_count; + + struct pollfd *poll_fds; + int poll_fds_count; +}; + +extern struct smdk4x12_sensors_handlers *smdk4x12_sensors_handlers[]; +extern int smdk4x12_sensors_handlers_count; + +int smdk4x12_sensors_activate(struct sensors_poll_device_t *dev, int handle, + int enabled); +int smdk4x12_sensors_set_delay(struct sensors_poll_device_t *dev, int handle, + int64_t ns); +int smdk4x12_sensors_poll(struct sensors_poll_device_t *dev, + struct sensors_event_t* data, int count); + +/* + * Input + */ + +void input_event_set(struct input_event *event, int type, int code, int value); +long int timestamp(struct timeval *time); +long int input_timestamp(struct input_event *event); +int uinput_rel_create(const char *name); +void uinput_destroy(int uinput_fd); +int input_open(char *name); +int sysfs_path_prefix(char *name, char *path_prefix); +int sysfs_value_read(char *path); +int sysfs_value_write(char *path, int value); +int sysfs_string_read(char *path, char *buffer, size_t length); +int sysfs_string_write(char *path, char *buffer, size_t length); + +/* + * Sensors + */ + +int orientation_fill(struct smdk4x12_sensors_handlers *handlers, + sensors_vec_t *acceleration, sensors_vec_t *magnetic); + +extern struct smdk4x12_sensors_handlers lsm330dlc_acceleration; +extern struct smdk4x12_sensors_handlers akm8975; +extern struct smdk4x12_sensors_handlers orientation; +extern struct smdk4x12_sensors_handlers cm36651_proximity; +extern struct smdk4x12_sensors_handlers cm36651_light; +extern struct smdk4x12_sensors_handlers lsm330dlc_gyroscope; +extern struct smdk4x12_sensors_handlers lps331ap; + +#endif |