forked from sonyxperiadev/vendor-sony-oss-fingerprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
165 lines (135 loc) · 3.51 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "common.h"
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#if PLATFORM_SDK_VERSION >= 28
#include <bits/epoll_event.h>
#endif
#include <sys/epoll.h>
#define LOG_TAG "FPC COMMON"
#include <log/log.h>
#define EVENT_COUNT 2
err_t fpc_event_create(fpc_event_t *event, int event_fd)
{
int fd = 0, rc;
event->event_fd = event_fd;
fd = open("/dev/fingerprint", O_RDWR);
if (fd < 0) {
ALOGE("Error opening FPC device");
return -1;
}
event->dev_fd = fd;
fd = epoll_create1(0);
if (fd < 0) {
ALOGE("Error creating epoll fd");
return -1;
}
event->epoll_fd = fd;
struct epoll_event ev = {
.data.fd = event_fd,
.events = EPOLLIN,
};
rc = epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (rc) {
ALOGE("Failed to add event_fd to epoll: %d", rc);
return -1;
}
ev = (struct epoll_event){
.data.fd = event->dev_fd,
.events = EPOLLIN,
};
rc = epoll_ctl(event->epoll_fd, EPOLL_CTL_ADD, ev.data.fd, &ev);
if (rc) {
ALOGE("Failed to add event->dev_fd to epoll: %d", rc);
return -1;
}
return 0;
}
err_t fpc_event_destroy(fpc_event_t *event)
{
event->event_fd = -1;
close(event->dev_fd);
event->dev_fd = -1;
close(event->epoll_fd);
event->epoll_fd = -1;
return 0;
}
err_t fpc_set_power(const fpc_event_t *event, int poweron)
{
int ret = -1;
ret = ioctl(event->dev_fd, FPC_IOCWPREPARE, poweron);
if (ret < 0) {
ALOGE("Failed preparing FPC device (%d) %s", ret, strerror(errno));
return -1;
}
return 1;
}
err_t fpc_get_power(const fpc_event_t *event)
{
int ret = -1;
uint32_t reply = -1;
ret = ioctl(event->dev_fd, FPC_IOCRPREPARE, &reply);
if (ret < 0) {
ALOGE("Failed reading device power state (%d) %s", ret, strerror(errno));
return -1;
}
if (reply > 1)
return -1;
return reply;
}
err_t fpc_poll_event(const fpc_event_t *event)
{
int cnt;
struct epoll_event events[EVENT_COUNT];
cnt = epoll_wait(event->epoll_fd, events, EVENT_COUNT, -1);
if (cnt < 0) {
ALOGE("Failed waiting for epoll: %d", cnt);
return FPC_EVENT_ERROR;
}
if (!cnt) {
ALOGE("Epoll timed out despite infinite blocking!");
return FPC_EVENT_TIMEOUT;
}
for (int i = 0; i < cnt; ++i)
if (events[i].data.fd == event->event_fd && events[i].events | EPOLLIN) {
ALOGD("Waking up from eventfd");
return FPC_EVENT_EVENTFD;
}
// Only other event source is the fingerprint.
ALOGD("Waking up from finger event");
return FPC_EVENT_FINGER;
}
/**
* Checks if an event (request to switch to a different state) is available.
*
* Does not return true on (spurious) hardware/irq raise.
*/
err_t is_event_available(const fpc_event_t *event)
{
int cnt;
struct pollfd pfd = {
.fd = event->event_fd,
.events = POLLIN,
};
// 0 = do not block at all:
cnt = poll(&pfd, 1, 0);
if (cnt < 0) {
ALOGE("Failed waiting for epoll: %d", cnt);
return cnt;
}
return cnt > 0;
}
err_t fpc_keep_awake(const fpc_event_t *event, int awake, unsigned int timeout)
{
struct {
int awake;
unsigned int timeout;
} args = {awake, timeout};
int rc = ioctl(event->dev_fd, FPC_IOCWAWAKE, &args);
if (rc)
ALOGE("%s failed: %d", __func__, rc);
return rc;
}