platform_device_xiaomi_sm84.../udfps/UdfpsHandler.cpp
Arian 6cfbe86b6d sm8450-common: udfps: Improve fod press poll logging
Change-Id: I9999722660f3d56519463c8e3762b9f48ef0208b
2024-03-19 23:22:37 +01:00

188 lines
5 KiB
C++

/*
* Copyright (C) 2022 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#define LOG_TAG "UdfpsHandler.xiaomi_sm8450"
#include <android-base/logging.h>
#include <android-base/unique_fd.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <fstream>
#include <thread>
#include <display/drm/mi_disp.h>
#include "UdfpsHandler.h"
#include "xiaomi_touch.h"
#define COMMAND_NIT 10
#define PARAM_NIT_FOD 1
#define PARAM_NIT_NONE 0
#define COMMAND_FOD_PRESS_STATUS 1
#define PARAM_FOD_PRESSED 1
#define PARAM_FOD_RELEASED 0
#define FOD_STATUS_OFF 0
#define FOD_STATUS_ON 1
#define TOUCH_DEV_PATH "/dev/xiaomi-touch"
#define TOUCH_MAGIC 'T'
#define TOUCH_IOC_SET_CUR_VALUE _IO(TOUCH_MAGIC, SET_CUR_VALUE)
#define TOUCH_IOC_GET_CUR_VALUE _IO(TOUCH_MAGIC, GET_CUR_VALUE)
#define DISP_FEATURE_PATH "/dev/mi_display/disp_feature"
#define FOD_PRESS_STATUS_PATH "/sys/class/touch/touch_dev/fod_press_status"
namespace {
static bool readBool(int fd) {
char c;
int rc;
rc = lseek(fd, 0, SEEK_SET);
if (rc) {
LOG(ERROR) << "failed to seek fd, err: " << rc;
return false;
}
rc = read(fd, &c, sizeof(char));
if (rc != 1) {
LOG(ERROR) << "failed to read bool from fd, err: " << rc;
return false;
}
return c != '0';
}
} // anonymous namespace
class XiaomiSm8450UdfpsHander : public UdfpsHandler {
public:
void init(fingerprint_device_t* device) {
mDevice = device;
touch_fd_ = android::base::unique_fd(open(TOUCH_DEV_PATH, O_RDWR));
disp_fd_ = android::base::unique_fd(open(DISP_FEATURE_PATH, O_RDWR));
// Thread to notify fingeprint hwmodule about fod presses
std::thread([this]() {
int fd = open(FOD_PRESS_STATUS_PATH, O_RDONLY);
if (fd < 0) {
LOG(ERROR) << "failed to open " << FOD_PRESS_STATUS_PATH << " , err: " << fd;
return;
}
struct pollfd fodPressStatusPoll = {
.fd = fd,
.events = POLLERR | POLLPRI,
.revents = 0,
};
while (true) {
int rc = poll(&fodPressStatusPoll, 1, -1);
if (rc < 0) {
LOG(ERROR) << "failed to poll " << FOD_PRESS_STATUS_PATH << ", err: " << rc;
continue;
}
mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS,
readBool(fd) ? PARAM_FOD_PRESSED : PARAM_FOD_RELEASED);
}
}).detach();
}
void onFingerDown(uint32_t /*x*/, uint32_t /*y*/, float /*minor*/, float /*major*/) {
LOG(INFO) << __func__;
setFingerDown(true);
}
void onFingerUp() {
LOG(INFO) << __func__;
setFingerDown(false);
}
void onAcquired(int32_t result, int32_t vendorCode) {
LOG(INFO) << __func__ << " result: " << result << " vendorCode: " << vendorCode;
if (result == FINGERPRINT_ACQUIRED_GOOD) {
setFingerDown(false);
if (!enrolling) {
setFodStatus(FOD_STATUS_OFF);
}
} else if (vendorCode == 21 || vendorCode == 23) {
/*
* vendorCode = 21 waiting for fingerprint authentication
* vendorCode = 23 waiting for fingerprint enroll
*/
setFodStatus(FOD_STATUS_ON);
}
}
void cancel() {
LOG(INFO) << __func__;
enrolling = false;
setFingerDown(false);
setFodStatus(FOD_STATUS_OFF);
}
void preEnroll() {
LOG(INFO) << __func__;
enrolling = true;
}
void enroll() {
LOG(INFO) << __func__;
enrolling = true;
}
void postEnroll() {
LOG(INFO) << __func__;
enrolling = false;
setFodStatus(FOD_STATUS_OFF);
}
private:
fingerprint_device_t* mDevice;
android::base::unique_fd touch_fd_;
android::base::unique_fd disp_fd_;
bool enrolling = false;
void setFodStatus(int value) {
int buf[MAX_BUF_SIZE] = {MI_DISP_PRIMARY, Touch_Fod_Enable, value};
ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &buf);
}
void setFingerDown(bool pressed) {
mDevice->extCmd(mDevice, COMMAND_NIT, pressed ? PARAM_NIT_FOD : PARAM_NIT_NONE);
int buf[MAX_BUF_SIZE] = {MI_DISP_PRIMARY, THP_FOD_DOWNUP_CTL, pressed ? 1 : 0};
ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &buf);
// Request HBM
disp_local_hbm_req req;
req.base.flag = 0;
req.base.disp_id = MI_DISP_PRIMARY;
req.local_hbm_value = pressed ? LHBM_TARGET_BRIGHTNESS_WHITE_1000NIT
: LHBM_TARGET_BRIGHTNESS_OFF_FINGER_UP;
ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &req);
}
};
static UdfpsHandler* create() {
return new XiaomiSm8450UdfpsHander();
}
static void destroy(UdfpsHandler* handler) {
delete handler;
}
extern "C" UdfpsHandlerFactory UDFPS_HANDLER_FACTORY = {
.create = create,
.destroy = destroy,
};