123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /*
- * Copyright (C) 2022 The LineageOS Project
- *
- * SPDX-License-Identifier: Apache-2.0
- */
- #define LOG_TAG "UdfpsHandler.xiaomi_sm8450"
- #include <aidl/android/hardware/biometrics/fingerprint/BnFingerprint.h>
- #include <android-base/logging.h>
- #include <android-base/properties.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 <linux/xiaomi_touch.h>
- #include "UdfpsHandler.h"
- #define COMMAND_NIT 10
- #define TARGET_BRIGHTNESS_OFF 0
- #define TARGET_BRIGHTNESS_1000NIT 1
- #define TARGET_BRIGHTNESS_110NIT 6
- #define LOW_BRIGHTNESS_THRESHHOLD 100
- #define COMMAND_FOD_PRESS_STATUS 1
- #define COMMAND_FOD_PRESS_X 2
- #define COMMAND_FOD_PRESS_Y 3
- #define PARAM_FOD_PRESSED 1
- #define PARAM_FOD_RELEASED 0
- #define DISP_FEATURE_PATH "/dev/mi_display/disp_feature"
- #define TOUCH_DEV_PATH "/dev/xiaomi-touch"
- using ::aidl::android::hardware::biometrics::fingerprint::AcquiredInfo;
- namespace {
- static disp_event_resp* parseDispEvent(int fd) {
- disp_event header;
- ssize_t headerSize = read(fd, &header, sizeof(header));
- if (headerSize < sizeof(header)) {
- LOG(ERROR) << "unexpected display event header size: " << headerSize;
- return nullptr;
- }
- struct disp_event_resp* response =
- reinterpret_cast<struct disp_event_resp*>(malloc(header.length));
- response->base = header;
- int dataLength = response->base.length - sizeof(response->base);
- if (dataLength < 0) {
- LOG(ERROR) << "invalid data length: " << response->base.length;
- return nullptr;
- }
- ssize_t dataSize = read(fd, &response->data, dataLength);
- if (dataSize < dataLength) {
- LOG(ERROR) << "unexpected display event data size: " << dataSize;
- return nullptr;
- }
- return response;
- }
- struct disp_base displayBasePrimary = {
- .flag = 0,
- .disp_id = MI_DISP_PRIMARY,
- };
- } // anonymous namespace
- class XiaomiSm8450UdfpsHander : public UdfpsHandler {
- public:
- void init(fingerprint_device_t* device) {
- mDevice = device;
- disp_fd_ = android::base::unique_fd(open(DISP_FEATURE_PATH, O_RDWR));
- touch_fd_ = android::base::unique_fd(open(TOUCH_DEV_PATH, O_RDWR));
- // Thread to listen for fod ui changes
- std::thread([this]() {
- int fd = open(DISP_FEATURE_PATH, O_RDWR);
- if (fd < 0) {
- LOG(ERROR) << "failed to open " << DISP_FEATURE_PATH << " , err: " << fd;
- return;
- }
- // Register for FOD events
- disp_event_req req;
- req.base.flag = 0;
- req.base.disp_id = MI_DISP_PRIMARY;
- req.type = MI_DISP_EVENT_FOD;
- ioctl(fd, MI_DISP_IOCTL_REGISTER_EVENT, &req);
- struct pollfd dispEventPoll = {
- .fd = fd,
- .events = POLLIN,
- .revents = 0,
- };
- while (true) {
- int rc = poll(&dispEventPoll, 1, -1);
- if (rc < 0) {
- LOG(ERROR) << "failed to poll " << DISP_FEATURE_PATH << ", err: " << rc;
- continue;
- }
- struct disp_event_resp* response = parseDispEvent(fd);
- if (response == nullptr) {
- continue;
- }
- if (response->base.type != MI_DISP_EVENT_FOD) {
- LOG(ERROR) << "unexpected display event: " << response->base.type;
- continue;
- }
- int value = response->data[0];
- LOG(DEBUG) << "received data: " << std::bitset<8>(value);
- bool localHbmUiReady = value & LOCAL_HBM_UI_READY;
- bool requestLowBrightnessCapture = value & FOD_LOW_BRIGHTNESS_CAPTURE;
- mDevice->extCmd(mDevice, COMMAND_NIT,
- localHbmUiReady
- ? (requestLowBrightnessCapture ? TARGET_BRIGHTNESS_110NIT
- : TARGET_BRIGHTNESS_1000NIT)
- : TARGET_BRIGHTNESS_OFF);
- }
- }).detach();
- }
- void onFingerDown(uint32_t x, uint32_t y, float /*minor*/, float /*major*/) {
- LOG(DEBUG) << __func__ << "x: " << x << ", y: " << y;
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, x);
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, y);
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_PRESSED);
- // Update fod_finger_state node in case hwmodule polls it
- struct touch_mode_request touchRequest = {
- .mode = TOUCH_MODE_FOD_FINGER_STATE,
- .value = 1,
- };
- ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &touchRequest);
- // Request HBM
- struct disp_local_hbm_req displayLhbmRequest = {
- .base = displayBasePrimary,
- .local_hbm_value = LHBM_TARGET_BRIGHTNESS_WHITE_1000NIT,
- };
- ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &displayLhbmRequest);
- }
- void onFingerUp() {
- LOG(DEBUG) << __func__;
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, 0);
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, 0);
- mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_RELEASED);
- // Disable HBM
- struct disp_local_hbm_req displayLhbmRequest = {
- .base = displayBasePrimary,
- .local_hbm_value = LHBM_TARGET_BRIGHTNESS_OFF_FINGER_UP,
- };
- ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &displayLhbmRequest);
- // Update fod_finger_state node in case hwmodule polls it
- struct touch_mode_request touchRequest = {
- .mode = TOUCH_MODE_FOD_FINGER_STATE,
- .value = 0,
- };
- ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &touchRequest);
- }
- void onAcquired(int32_t result, int32_t vendorCode) {
- LOG(DEBUG) << __func__ << " result: " << result << " vendorCode: " << vendorCode;
- if (static_cast<AcquiredInfo>(result) == AcquiredInfo::GOOD) {
- onFingerUp();
- }
- }
- void cancel() { LOG(DEBUG) << __func__; }
- private:
- fingerprint_device_t* mDevice;
- android::base::unique_fd disp_fd_;
- android::base::unique_fd touch_fd_;
- };
- static UdfpsHandler* create() {
- return new XiaomiSm8450UdfpsHander();
- }
- static void destroy(UdfpsHandler* handler) {
- delete handler;
- }
- extern "C" UdfpsHandlerFactory UDFPS_HANDLER_FACTORY = {
- .create = create,
- .destroy = destroy,
- };
|