UdfpsHandler.cpp 6.4 KB

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