UdfpsHandler.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. struct disp_base displayBasePrimary = {
  55. .flag = 0,
  56. .disp_id = MI_DISP_PRIMARY,
  57. };
  58. } // anonymous namespace
  59. class XiaomiSm8450UdfpsHander : public UdfpsHandler {
  60. public:
  61. void init(fingerprint_device_t* device) {
  62. mDevice = device;
  63. disp_fd_ = android::base::unique_fd(open(DISP_FEATURE_PATH, O_RDWR));
  64. touch_fd_ = android::base::unique_fd(open(TOUCH_DEV_PATH, O_RDWR));
  65. // Thread to listen for fod ui changes
  66. std::thread([this]() {
  67. int fd = open(DISP_FEATURE_PATH, O_RDWR);
  68. if (fd < 0) {
  69. LOG(ERROR) << "failed to open " << DISP_FEATURE_PATH << " , err: " << fd;
  70. return;
  71. }
  72. // Register for FOD events
  73. disp_event_req req;
  74. req.base.flag = 0;
  75. req.base.disp_id = MI_DISP_PRIMARY;
  76. req.type = MI_DISP_EVENT_FOD;
  77. ioctl(fd, MI_DISP_IOCTL_REGISTER_EVENT, &req);
  78. struct pollfd dispEventPoll = {
  79. .fd = fd,
  80. .events = POLLIN,
  81. .revents = 0,
  82. };
  83. while (true) {
  84. int rc = poll(&dispEventPoll, 1, -1);
  85. if (rc < 0) {
  86. LOG(ERROR) << "failed to poll " << DISP_FEATURE_PATH << ", err: " << rc;
  87. continue;
  88. }
  89. struct disp_event_resp* response = parseDispEvent(fd);
  90. if (response == nullptr) {
  91. continue;
  92. }
  93. if (response->base.type != MI_DISP_EVENT_FOD) {
  94. LOG(ERROR) << "unexpected display event: " << response->base.type;
  95. continue;
  96. }
  97. int value = response->data[0];
  98. LOG(DEBUG) << "received data: " << std::bitset<8>(value);
  99. bool localHbmUiReady = value & LOCAL_HBM_UI_READY;
  100. bool requestLowBrightnessCapture = value & FOD_LOW_BRIGHTNESS_CAPTURE;
  101. mDevice->extCmd(mDevice, COMMAND_NIT,
  102. localHbmUiReady
  103. ? (requestLowBrightnessCapture ? TARGET_BRIGHTNESS_110NIT
  104. : TARGET_BRIGHTNESS_1000NIT)
  105. : TARGET_BRIGHTNESS_OFF);
  106. }
  107. }).detach();
  108. }
  109. void onFingerDown(uint32_t x, uint32_t y, float /*minor*/, float /*major*/) {
  110. LOG(DEBUG) << __func__ << "x: " << x << ", y: " << y;
  111. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, x);
  112. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, y);
  113. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_PRESSED);
  114. // Update fod_finger_state node in case hwmodule polls it
  115. struct touch_mode_request touchRequest = {
  116. .mode = TOUCH_MODE_FOD_FINGER_STATE,
  117. .value = 1,
  118. };
  119. ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &touchRequest);
  120. // Request HBM
  121. struct disp_local_hbm_req displayLhbmRequest = {
  122. .base = displayBasePrimary,
  123. .local_hbm_value = LHBM_TARGET_BRIGHTNESS_WHITE_1000NIT,
  124. };
  125. ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &displayLhbmRequest);
  126. }
  127. void onFingerUp() {
  128. LOG(DEBUG) << __func__;
  129. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_X, 0);
  130. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_Y, 0);
  131. mDevice->extCmd(mDevice, COMMAND_FOD_PRESS_STATUS, PARAM_FOD_RELEASED);
  132. // Disable HBM
  133. struct disp_local_hbm_req displayLhbmRequest = {
  134. .base = displayBasePrimary,
  135. .local_hbm_value = LHBM_TARGET_BRIGHTNESS_OFF_FINGER_UP,
  136. };
  137. ioctl(disp_fd_.get(), MI_DISP_IOCTL_SET_LOCAL_HBM, &displayLhbmRequest);
  138. // Update fod_finger_state node in case hwmodule polls it
  139. struct touch_mode_request touchRequest = {
  140. .mode = TOUCH_MODE_FOD_FINGER_STATE,
  141. .value = 0,
  142. };
  143. ioctl(touch_fd_.get(), TOUCH_IOC_SET_CUR_VALUE, &touchRequest);
  144. }
  145. void onAcquired(int32_t result, int32_t vendorCode) {
  146. LOG(DEBUG) << __func__ << " result: " << result << " vendorCode: " << vendorCode;
  147. if (static_cast<AcquiredInfo>(result) == AcquiredInfo::GOOD) {
  148. onFingerUp();
  149. }
  150. }
  151. void cancel() { LOG(DEBUG) << __func__; }
  152. private:
  153. fingerprint_device_t* mDevice;
  154. android::base::unique_fd disp_fd_;
  155. android::base::unique_fd touch_fd_;
  156. };
  157. static UdfpsHandler* create() {
  158. return new XiaomiSm8450UdfpsHander();
  159. }
  160. static void destroy(UdfpsHandler* handler) {
  161. delete handler;
  162. }
  163. extern "C" UdfpsHandlerFactory UDFPS_HANDLER_FACTORY = {
  164. .create = create,
  165. .destroy = destroy,
  166. };