OSDN Git Service

add module for HDAPS accelerometer found in many IBM Thinkpads
[android-x86/hardware-libsensors.git] / hdaps.c
1 /**
2  * HDAPS accelerometer sensor
3  *
4  * Copyright (C) 2011 The Android-x86 Open Source Project
5  *
6  * by Stefan Seidel <stefans@android-x86.org>
7  *
8  * Licensed under GPLv2 or later
9  *
10  **/
11
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <dirent.h>
20 #include <math.h>
21 #include <poll.h>
22 #include <pthread.h>
23 #include <linux/input.h>
24
25 #include <cutils/atomic.h>
26 #include <cutils/log.h>
27 #include <hardware/sensors.h>
28
29 //#define DEBUG_SENSOR          1
30
31 #define CONVERT                 (GRAVITY_EARTH / 156.0f)
32 #define SENSOR_NAME             "hdaps"
33 #define INPUT_DIR               "/dev/input"
34 #define ARRAY_SIZE(a)           (sizeof(a) / sizeof(a[0]))
35 #define ID_ACCELERATION         (SENSORS_HANDLE_BASE + 0)
36
37 #define AMIN(a,b)               (((a)<(fabs(b)))?(a):(b))
38 #define SQUARE(x)               ((x)*(x))
39 #define COS_ASIN(m,x)           (sqrt(SQUARE(m)-SQUARE(AMIN(m,x))))
40 #define COS_ASIN_2D(m,x,y)      (COS_ASIN(m,x)*COS_ASIN(m,y)/(m))
41
42 static unsigned int forced_delay = 0;
43
44 struct sensors_poll_context_t {
45         struct sensors_poll_device_t device;
46         int fd;
47 };
48
49 static int common__close(struct hw_device_t *dev) {
50         struct sensors_poll_context_t *ctx = (struct sensors_poll_context_t *) dev;
51         if (ctx) {
52                 free(ctx);
53         }
54
55         return 0;
56 }
57
58 static int device__activate(struct sensors_poll_device_t *dev, int handle,
59                 int enabled) {
60
61         return 0;
62 }
63
64 static int device__set_delay(struct sensors_poll_device_t *device, int handle,
65                 int64_t ns) {
66         forced_delay = ns / 1000;
67         return 0;
68
69 }
70
71 static int device__poll(struct sensors_poll_device_t *device,
72                 sensors_event_t* data, int count) {
73
74         struct input_event event;
75         int ret;
76         struct sensors_poll_context_t *dev =
77                         (struct sensors_poll_context_t *) device;
78
79         if (dev->fd < 0)
80                 return 0;
81
82         while (1) {
83
84                 ret = read(dev->fd, &event, sizeof(event));
85
86 #ifdef DEBUG_SENSOR
87                 LOGD("hdaps event %d - %d - %d\n", event.type, event.code, event.value);
88 #endif
89                 if (event.type == EV_ABS) {
90                         switch (event.code) {
91                         // Even though this mapping results in wrong results with some apps,
92                         // it just means that these apps are broken, i.e. they rely on the
93                         // fact that phone have portrait displays. Laptops/tablets however
94                         // have landscape displays, and the axes are relative to the default
95                         // screen orientation, not relative to portrait orientation.
96                         // See the nVidia Tegra accelerometer docs if you want to know for sure.
97                         case ABS_X:
98                                 data->acceleration.x = (float) event.value * CONVERT;
99                                 break;
100                         case ABS_Y:
101                                 // we invert the y-axis here because we assume the laptop is
102                                 // in tablet mode (and thus the display is rotated 180 degrees.
103                                 data->acceleration.y = -(float) event.value * CONVERT;
104                                 break;
105                         }
106                 } else if (event.type == EV_SYN) {
107                         data->timestamp = (int64_t) ((int64_t) event.time.tv_sec
108                                         * 1000000000 + (int64_t) event.time.tv_usec * 1000);
109                         // hdaps doesn't have z-axis, so simulate it by rotation matrix solution
110                         data ->acceleration.z = COS_ASIN_2D(GRAVITY_EARTH, data->acceleration.x, data->acceleration.y);
111                         data->sensor = ID_ACCELERATION;
112                         data->type = SENSOR_TYPE_ACCELEROMETER;
113                         data->acceleration.status = SENSOR_STATUS_ACCURACY_HIGH;
114                         // spare the CPU if desired
115                         if (forced_delay)
116                                 usleep(forced_delay);
117                         return 1;
118                 }
119         }
120
121         return -errno;
122 }
123
124 static int open_input_device(void) {
125         char *filename;
126         int fd;
127         DIR *dir;
128         struct dirent *de;
129         char name[80];
130         char devname[256];
131         dir = opendir(INPUT_DIR);
132         if (dir == NULL)
133                 return -1;
134
135         strcpy(devname, INPUT_DIR);
136         filename = devname + strlen(devname);
137         *filename++ = '/';
138
139         while ((de = readdir(dir))) {
140                 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || (de->d_name[1] == '.' && de->d_name[2] == '\0')))
141                         continue;
142                 strcpy(filename, de->d_name);
143                 fd = open(devname, O_RDONLY);
144                 if (fd < 0) {
145                         continue;
146                 }
147
148                 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
149                         name[0] = '\0';
150                 }
151
152                 if (!strcmp(name, SENSOR_NAME)) {
153 #ifdef DEBUG_SENSOR
154                         LOGI("devname is %s \n", devname);
155 #endif
156                 } else {
157                         close(fd);
158                         continue;
159                 }
160                 closedir(dir);
161
162                 return fd;
163
164         }
165         closedir(dir);
166
167         return -1;
168 }
169
170 static const struct sensor_t sSensorList[] = {
171         {       .name = "HDAPS accelerometer",
172                 .vendor = "Linux kernel",
173                 .version = 1,
174                 .handle = ID_ACCELERATION,
175                 .type = SENSOR_TYPE_ACCELEROMETER,
176                 .maxRange = (GRAVITY_EARTH * 6.0f),
177                 .resolution = (GRAVITY_EARTH * 6.0f) / 1024.0f,
178                 .power = 0.84f,
179                 .reserved = {},
180         },
181 };
182
183 static int open_sensors(const struct hw_module_t* module, const char* name,
184                 struct hw_device_t** device);
185
186 static int sensors__get_sensors_list(struct sensors_module_t* module,
187                 struct sensor_t const** list) {
188         *list = sSensorList;
189
190         return ARRAY_SIZE(sSensorList);
191 }
192
193 static struct hw_module_methods_t sensors_module_methods = {
194                 .open = open_sensors };
195
196 const struct sensors_module_t HAL_MODULE_INFO_SYM = {
197         .common = {
198                 .tag = HARDWARE_MODULE_TAG,
199                 .version_major = 1,
200                 .version_minor = 1,
201                 .id = SENSORS_HARDWARE_MODULE_ID,
202                 .name = "hdaps accelerometer sensor",
203                 .author = "Stefan Seidel",
204                 .methods = &sensors_module_methods,
205                 .dso = NULL,
206                 .reserved = {},
207         },
208         .get_sensors_list = sensors__get_sensors_list
209 };
210
211 static int open_sensors(const struct hw_module_t* module, const char* name,
212                 struct hw_device_t** device) {
213         int status = -EINVAL;
214
215         struct sensors_poll_context_t *dev = malloc(
216                         sizeof(struct sensors_poll_context_t));
217         memset(&dev->device, 0, sizeof(struct sensors_poll_device_t));
218
219         dev->device.common.tag = HARDWARE_DEVICE_TAG;
220         dev->device.common.version = 0;
221         dev->device.common.module = (struct hw_module_t*) module;
222         dev->device.common.close = common__close;
223         dev->device.activate = device__activate;
224         dev->device.setDelay = device__set_delay;
225         dev->device.poll = device__poll;
226
227         if ((dev->fd = open_input_device()) < 0) {
228                 LOGE("g sensor get class path error \n");
229         } else {
230                 *device = &dev->device.common;
231                 status = 0;
232         }
233
234         return status;
235 }