Hardware.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2021 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <algorithm>
  18. #include "HardwareBase.h"
  19. #include "Vibrator.h"
  20. #define PROC_SND_PCM "/proc/asound/pcm"
  21. #define HAPTIC_PCM_DEVICE_SYMBOL "haptic nohost playback"
  22. static struct pcm_config haptic_nohost_config = {
  23. .channels = 1,
  24. .rate = 48000,
  25. .period_size = 80,
  26. .period_count = 2,
  27. .format = PCM_FORMAT_S16_LE,
  28. };
  29. enum WaveformIndex : uint16_t {
  30. /* Physical waveform */
  31. WAVEFORM_LONG_VIBRATION_EFFECT_INDEX = 0,
  32. WAVEFORM_RESERVED_INDEX_1 = 1,
  33. WAVEFORM_CLICK_INDEX = 2,
  34. WAVEFORM_SHORT_VIBRATION_EFFECT_INDEX = 3,
  35. WAVEFORM_THUD_INDEX = 4,
  36. WAVEFORM_SPIN_INDEX = 5,
  37. WAVEFORM_QUICK_RISE_INDEX = 6,
  38. WAVEFORM_SLOW_RISE_INDEX = 7,
  39. WAVEFORM_QUICK_FALL_INDEX = 8,
  40. WAVEFORM_LIGHT_TICK_INDEX = 9,
  41. WAVEFORM_LOW_TICK_INDEX = 10,
  42. WAVEFORM_RESERVED_MFG_1,
  43. WAVEFORM_RESERVED_MFG_2,
  44. WAVEFORM_RESERVED_MFG_3,
  45. WAVEFORM_MAX_PHYSICAL_INDEX,
  46. /* OWT waveform */
  47. WAVEFORM_COMPOSE = WAVEFORM_MAX_PHYSICAL_INDEX,
  48. WAVEFORM_PWLE,
  49. /*
  50. * Refer to <linux/input.h>, the WAVEFORM_MAX_INDEX must not exceed 96.
  51. * #define FF_GAIN 0x60 // 96 in decimal
  52. * #define FF_MAX_EFFECTS FF_GAIN
  53. */
  54. WAVEFORM_MAX_INDEX,
  55. };
  56. namespace aidl {
  57. namespace android {
  58. namespace hardware {
  59. namespace vibrator {
  60. class HwApi : public Vibrator::HwApi, private HwApiBase {
  61. public:
  62. HwApi() {
  63. open("calibration/f0_stored", &mF0);
  64. open("default/f0_offset", &mF0Offset);
  65. open("calibration/redc_stored", &mRedc);
  66. open("calibration/q_stored", &mQ);
  67. open("default/vibe_state", &mVibeState);
  68. open("default/num_waves", &mEffectCount);
  69. open("default/owt_free_space", &mOwtFreeSpace);
  70. open("default/f0_comp_enable", &mF0CompEnable);
  71. open("default/redc_comp_enable", &mRedcCompEnable);
  72. open("default/delay_before_stop_playback_us", &mMinOnOffInterval);
  73. }
  74. bool setF0(std::string value) override { return set(value, &mF0); }
  75. bool setF0Offset(uint32_t value) override { return set(value, &mF0Offset); }
  76. bool setRedc(std::string value) override { return set(value, &mRedc); }
  77. bool setQ(std::string value) override { return set(value, &mQ); }
  78. bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); }
  79. bool pollVibeState(uint32_t value, int32_t timeoutMs) override {
  80. return poll(value, &mVibeState, timeoutMs);
  81. }
  82. bool hasOwtFreeSpace() override { return has(mOwtFreeSpace); }
  83. bool getOwtFreeSpace(uint32_t *value) override { return get(value, &mOwtFreeSpace); }
  84. bool setF0CompEnable(bool value) override { return set(value, &mF0CompEnable); }
  85. bool setRedcCompEnable(bool value) override { return set(value, &mRedcCompEnable); }
  86. bool setMinOnOffInterval(uint32_t value) override { return set(value, &mMinOnOffInterval); }
  87. uint32_t getContextScale() override {
  88. return utils::getProperty("persist.vendor.vibrator.hal.context.scale", 100);
  89. }
  90. bool getContextEnable() override {
  91. return utils::getProperty("persist.vendor.vibrator.hal.context.enable", false);
  92. }
  93. uint32_t getContextSettlingTime() override {
  94. return utils::getProperty("persist.vendor.vibrator.hal.context.settlingtime", 3000);
  95. }
  96. uint32_t getContextCooldownTime() override {
  97. return utils::getProperty("persist.vendor.vibrator.hal.context.cooldowntime", 1000);
  98. }
  99. bool getContextFadeEnable() override {
  100. return utils::getProperty("persist.vendor.vibrator.hal.context.fade", false);
  101. }
  102. // TODO(b/234338136): Need to add the force feedback HW API test cases
  103. bool setFFGain(int fd, uint16_t value) override {
  104. struct input_event gain = {
  105. .type = EV_FF,
  106. .code = FF_GAIN,
  107. .value = value,
  108. };
  109. if (write(fd, (const void *)&gain, sizeof(gain)) != sizeof(gain)) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. bool setFFEffect(int fd, struct ff_effect *effect, uint16_t timeoutMs) override {
  115. if (((*effect).replay.length != timeoutMs) || (ioctl(fd, EVIOCSFF, effect) < 0)) {
  116. ALOGE("setFFEffect fail");
  117. return false;
  118. } else {
  119. return true;
  120. }
  121. }
  122. bool setFFPlay(int fd, int8_t index, bool value) override {
  123. struct input_event play = {
  124. .type = EV_FF,
  125. .code = static_cast<uint16_t>(index),
  126. .value = value,
  127. };
  128. if (write(fd, (const void *)&play, sizeof(play)) != sizeof(play)) {
  129. return false;
  130. } else {
  131. return true;
  132. }
  133. }
  134. bool getHapticAlsaDevice(int *card, int *device) override {
  135. std::string line;
  136. std::ifstream myfile(PROC_SND_PCM);
  137. if (myfile.is_open()) {
  138. while (getline(myfile, line)) {
  139. if (line.find(HAPTIC_PCM_DEVICE_SYMBOL) != std::string::npos) {
  140. std::stringstream ss(line);
  141. std::string currentToken;
  142. std::getline(ss, currentToken, ':');
  143. sscanf(currentToken.c_str(), "%d-%d", card, device);
  144. return true;
  145. }
  146. }
  147. myfile.close();
  148. } else {
  149. ALOGE("Failed to read file: %s", PROC_SND_PCM);
  150. }
  151. return false;
  152. }
  153. bool setHapticPcmAmp(struct pcm **haptic_pcm, bool enable, int card, int device) override {
  154. int ret = 0;
  155. if (enable) {
  156. *haptic_pcm = pcm_open(card, device, PCM_OUT, &haptic_nohost_config);
  157. if (!pcm_is_ready(*haptic_pcm)) {
  158. ALOGE("cannot open pcm_out driver: %s", pcm_get_error(*haptic_pcm));
  159. goto fail;
  160. }
  161. ret = pcm_prepare(*haptic_pcm);
  162. if (ret < 0) {
  163. ALOGE("cannot prepare haptic_pcm: %s", pcm_get_error(*haptic_pcm));
  164. goto fail;
  165. }
  166. ret = pcm_start(*haptic_pcm);
  167. if (ret < 0) {
  168. ALOGE("cannot start haptic_pcm: %s", pcm_get_error(*haptic_pcm));
  169. goto fail;
  170. }
  171. return true;
  172. } else {
  173. if (*haptic_pcm) {
  174. pcm_close(*haptic_pcm);
  175. *haptic_pcm = NULL;
  176. }
  177. return true;
  178. }
  179. fail:
  180. pcm_close(*haptic_pcm);
  181. *haptic_pcm = NULL;
  182. return false;
  183. }
  184. bool uploadOwtEffect(int fd, uint8_t *owtData, uint32_t numBytes, struct ff_effect *effect,
  185. uint32_t *outEffectIndex, int *status) override {
  186. (*effect).u.periodic.custom_len = numBytes / sizeof(uint16_t);
  187. delete[] ((*effect).u.periodic.custom_data);
  188. (*effect).u.periodic.custom_data = new int16_t[(*effect).u.periodic.custom_len]{0x0000};
  189. if ((*effect).u.periodic.custom_data == nullptr) {
  190. ALOGE("Failed to allocate memory for custom data\n");
  191. *status = EX_NULL_POINTER;
  192. return false;
  193. }
  194. memcpy((*effect).u.periodic.custom_data, owtData, numBytes);
  195. if ((*effect).id != -1) {
  196. ALOGE("(*effect).id != -1");
  197. }
  198. /* Create a new OWT waveform to update the PWLE or composite effect. */
  199. (*effect).id = -1;
  200. if (ioctl(fd, EVIOCSFF, effect) < 0) {
  201. ALOGE("Failed to upload effect %d (%d): %s", *outEffectIndex, errno, strerror(errno));
  202. delete[] ((*effect).u.periodic.custom_data);
  203. *status = EX_ILLEGAL_STATE;
  204. return false;
  205. }
  206. if ((*effect).id >= FF_MAX_EFFECTS || (*effect).id < 0) {
  207. ALOGE("Invalid waveform index after upload OWT effect: %d", (*effect).id);
  208. *status = EX_ILLEGAL_ARGUMENT;
  209. return false;
  210. }
  211. *outEffectIndex = (*effect).id;
  212. *status = 0;
  213. return true;
  214. }
  215. bool eraseOwtEffect(int fd, int8_t effectIndex, std::vector<ff_effect> *effect) override {
  216. uint32_t effectCountBefore, effectCountAfter, i, successFlush = 0;
  217. if (effectIndex < WAVEFORM_MAX_PHYSICAL_INDEX) {
  218. ALOGE("Invalid waveform index for OWT erase: %d", effectIndex);
  219. return false;
  220. }
  221. if (effectIndex < WAVEFORM_MAX_INDEX) {
  222. /* Normal situation. Only erase the effect which we just played. */
  223. if (ioctl(fd, EVIOCRMFF, effectIndex) < 0) {
  224. ALOGE("Failed to erase effect %d (%d): %s", effectIndex, errno, strerror(errno));
  225. }
  226. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < WAVEFORM_MAX_INDEX; i++) {
  227. if ((*effect)[i].id == effectIndex) {
  228. (*effect)[i].id = -1;
  229. break;
  230. }
  231. }
  232. } else {
  233. /* Flush all non-prestored effects of ff-core and driver. */
  234. getEffectCount(&effectCountBefore);
  235. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < FF_MAX_EFFECTS; i++) {
  236. if (ioctl(fd, EVIOCRMFF, i) >= 0) {
  237. successFlush++;
  238. }
  239. }
  240. getEffectCount(&effectCountAfter);
  241. ALOGW("Flushed effects: ff: %d; driver: %d -> %d; success: %d", effectIndex,
  242. effectCountBefore, effectCountAfter, successFlush);
  243. /* Reset all OWT effect index of HAL. */
  244. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < WAVEFORM_MAX_INDEX; i++) {
  245. (*effect)[i].id = -1;
  246. }
  247. }
  248. return true;
  249. }
  250. void debug(int fd) override { HwApiBase::debug(fd); }
  251. private:
  252. std::ofstream mF0;
  253. std::ofstream mF0Offset;
  254. std::ofstream mRedc;
  255. std::ofstream mQ;
  256. std::ifstream mEffectCount;
  257. std::ifstream mVibeState;
  258. std::ifstream mOwtFreeSpace;
  259. std::ofstream mF0CompEnable;
  260. std::ofstream mRedcCompEnable;
  261. std::ofstream mMinOnOffInterval;
  262. };
  263. class HwCal : public Vibrator::HwCal, private HwCalBase {
  264. private:
  265. static constexpr char VERSION[] = "version";
  266. static constexpr char F0_CONFIG[] = "f0_measured";
  267. static constexpr char REDC_CONFIG[] = "redc_measured";
  268. static constexpr char Q_CONFIG[] = "q_measured";
  269. static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
  270. static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
  271. static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";
  272. static constexpr uint32_t VERSION_DEFAULT = 2;
  273. static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
  274. static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {1, 100};
  275. static constexpr std::array<uint32_t, 2> V_CLICK_DEFAULT = {1, 100};
  276. static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {1, 100};
  277. public:
  278. HwCal() {}
  279. bool getVersion(uint32_t *value) override {
  280. if (getPersist(VERSION, value)) {
  281. return true;
  282. }
  283. *value = VERSION_DEFAULT;
  284. return true;
  285. }
  286. bool getLongFrequencyShift(int32_t *value) override {
  287. return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
  288. }
  289. bool getF0(std::string *value) override { return getPersist(F0_CONFIG, value); }
  290. bool getRedc(std::string *value) override { return getPersist(REDC_CONFIG, value); }
  291. bool getQ(std::string *value) override { return getPersist(Q_CONFIG, value); }
  292. bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
  293. if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
  294. return true;
  295. }
  296. *value = V_TICK_DEFAULT;
  297. return true;
  298. }
  299. bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
  300. if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
  301. return true;
  302. }
  303. *value = V_CLICK_DEFAULT;
  304. return true;
  305. }
  306. bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
  307. if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
  308. return true;
  309. }
  310. *value = V_LONG_DEFAULT;
  311. return true;
  312. }
  313. bool isChirpEnabled() override {
  314. bool value;
  315. getProperty("chirp.enabled", &value, false);
  316. return value;
  317. }
  318. bool getSupportedPrimitives(uint32_t *value) override {
  319. return getProperty("supported_primitives", value, (uint32_t)0);
  320. }
  321. bool isF0CompEnabled() override {
  322. bool value;
  323. getProperty("f0.comp.enabled", &value, true);
  324. return value;
  325. }
  326. bool isRedcCompEnabled() override {
  327. bool value;
  328. getProperty("redc.comp.enabled", &value, true);
  329. return value;
  330. }
  331. void debug(int fd) override { HwCalBase::debug(fd); }
  332. };
  333. } // namespace vibrator
  334. } // namespace hardware
  335. } // namespace android
  336. } // namespace aidl