UdfpsHandler.cpp 6.9 KB

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