UdfpsHandler.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2022 The LineageOS Project
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #define LOG_TAG "UdfpsHandler.xiaomi_sm8450"
  7. #include <aidl/android/hardware/biometrics/fingerprint/BnFingerprint.h>
  8. #include <android-base/logging.h>
  9. #include <android-base/properties.h>
  10. #include <android-base/unique_fd.h>
  11. #include <poll.h>
  12. #include <sys/ioctl.h>
  13. #include <fstream>
  14. #include <thread>
  15. #include <display/drm/mi_disp.h>
  16. #include "UdfpsHandler.h"
  17. #define COMMAND_NIT 10
  18. #define TARGET_BRIGHTNESS_OFF 0
  19. #define TARGET_BRIGHTNESS_1000NIT 1
  20. #define TARGET_BRIGHTNESS_110NIT 6
  21. #define LOW_BRIGHTNESS_THRESHHOLD 100
  22. #define COMMAND_FOD_PRESS_STATUS 1
  23. #define COMMAND_FOD_PRESS_X 2
  24. #define COMMAND_FOD_PRESS_Y 3
  25. #define PARAM_FOD_PRESSED 1
  26. #define PARAM_FOD_RELEASED 0
  27. #define DISP_FEATURE_PATH "/dev/mi_display/disp_feature"
  28. using ::aidl::android::hardware::biometrics::fingerprint::AcquiredInfo;
  29. namespace {
  30. static disp_event_resp* parseDispEvent(int fd) {
  31. disp_event header;
  32. ssize_t headerSize = read(fd, &header, sizeof(header));
  33. if (headerSize < sizeof(header)) {
  34. LOG(ERROR) << "unexpected display event header size: " << headerSize;
  35. return nullptr;
  36. }
  37. struct disp_event_resp* response =
  38. reinterpret_cast<struct disp_event_resp*>(malloc(header.length));
  39. response->base = header;
  40. int dataLength = response->base.length - sizeof(response->base);
  41. if (dataLength < 0) {
  42. LOG(ERROR) << "invalid data length: " << response->base.length;
  43. return nullptr;
  44. }
  45. ssize_t dataSize = read(fd, &response->data, dataLength);
  46. if (dataSize < dataLength) {
  47. LOG(ERROR) << "unexpected display event data size: " << dataSize;
  48. return nullptr;
  49. }
  50. return response;
  51. }
  52. } // anonymous namespace
  53. class XiaomiSm8450UdfpsHander : public UdfpsHandler {
  54. public:
  55. void init(fingerprint_device_t* device) {
  56. mDevice = device;
  57. disp_fd_ = android::base::unique_fd(open(DISP_FEATURE_PATH, O_RDWR));
  58. // Thread to listen for fod ui changes
  59. std::thread([this]() {
  60. int fd = open(DISP_FEATURE_PATH, O_RDWR);
  61. if (fd < 0) {
  62. LOG(ERROR) << "failed to open " << DISP_FEATURE_PATH << " , err: " << fd;
  63. return;
  64. }
  65. // Register for FOD events
  66. disp_event_req req;
  67. req.base.flag = 0;
  68. req.base.disp_id = MI_DISP_PRIMARY;
  69. req.type = MI_DISP_EVENT_FOD;
  70. ioctl(fd, MI_DISP_IOCTL_REGISTER_EVENT, &req);
  71. struct pollfd dispEventPoll = {
  72. .fd = fd,
  73. .events = POLLIN,
  74. .revents = 0,
  75. };
  76. while (true) {
  77. int rc = poll(&dispEventPoll, 1, -1);
  78. if (rc < 0) {
  79. LOG(ERROR) << "failed to poll " << DISP_FEATURE_PATH << ", err: " << rc;
  80. continue;
  81. }
  82. struct disp_event_resp* response = parseDispEvent(fd);
  83. if (response == nullptr) {
  84. continue;
  85. }
  86. if (response->base.type != MI_DISP_EVENT_FOD) {
  87. LOG(ERROR) << "unexpected display event: " << response->base.type;
  88. continue;
  89. }
  90. int value = response->data[0];
  91. LOG(DEBUG) << "received data: " << std::bitset<8>(value);
  92. bool localHbmUiReady = value & LOCAL_HBM_UI_READY;
  93. bool requestLowBrightnessCapture = value & FOD_LOW_BRIGHTNESS_CAPTURE;
  94. mDevice->extCmd(mDevice, COMMAND_NIT,
  95. localHbmUiReady
  96. ? (requestLowBrightnessCapture ? TARGET_BRIGHTNESS_110NIT
  97. : TARGET_BRIGHTNESS_1000NIT)
  98. : TARGET_BRIGHTNESS_OFF);
  99. }
  100. }).detach();
  101. }
  102. void onFingerDown(uint32_t x, uint32_t y, float /*minor*/, float /*major*/) {
  103. LOG(DEBUG) << __func__ << "x: " << x << ", y: " << y;
  104. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, x);
  105. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, y);
  106. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_PRESSED);
  107. // Request HBM
  108. disp_local_hbm_req req;
  109. req.base.flag = 0;
  110. req.base.disp_id = MI_DISP_PRIMARY;
  111. req.local_hbm_value = LHBM_TARGET_BRIGHTNESS_WHITE_1000NIT;
  112. ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &req);
  113. }
  114. void onFingerUp() {
  115. LOG(DEBUG) << __func__;
  116. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, 0);
  117. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, 0);
  118. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_RELEASED);
  119. // Disable HBM
  120. disp_local_hbm_req req;
  121. req.base.flag = 0;
  122. req.base.disp_id = MI_DISP_PRIMARY;
  123. req.local_hbm_value = LHBM_TARGET_BRIGHTNESS_OFF_FINGER_UP;
  124. ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &req);
  125. }
  126. void onAcquired(int32_t result, int32_t vendorCode) {
  127. LOG(DEBUG) << __func__ << " result: " << result << " vendorCode: " << vendorCode;
  128. if (static_cast<AcquiredInfo>(result) == AcquiredInfo::GOOD) {
  129. onFingerUp();
  130. }
  131. }
  132. void cancel() {
  133. LOG(DEBUG) << __func__;
  134. }
  135. private:
  136. fingerprint_device_t* mDevice;
  137. android::base::unique_fd disp_fd_;
  138. };
  139. static UdfpsHandler* create() {
  140. return new XiaomiSm8450UdfpsHander();
  141. }
  142. static void destroy(UdfpsHandler* handler) {
  143. delete handler;
  144. }
  145. extern "C" UdfpsHandlerFactory UDFPS_HANDLER_FACTORY = {
  146. .create = create,
  147. .destroy = destroy,
  148. };