Hardware.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. // TODO(b/234338136): Need to add the force feedback HW API test cases
  88. bool setFFGain(int fd, uint16_t value) override {
  89. struct input_event gain = {
  90. .type = EV_FF,
  91. .code = FF_GAIN,
  92. .value = value,
  93. };
  94. if (write(fd, (const void *)&gain, sizeof(gain)) != sizeof(gain)) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. bool setFFEffect(int fd, struct ff_effect *effect, uint16_t timeoutMs) override {
  100. if (((*effect).replay.length != timeoutMs) || (ioctl(fd, EVIOCSFF, effect) < 0)) {
  101. ALOGE("setFFEffect fail");
  102. return false;
  103. } else {
  104. return true;
  105. }
  106. }
  107. bool setFFPlay(int fd, int8_t index, bool value) override {
  108. struct input_event play = {
  109. .type = EV_FF,
  110. .code = static_cast<uint16_t>(index),
  111. .value = value,
  112. };
  113. if (write(fd, (const void *)&play, sizeof(play)) != sizeof(play)) {
  114. return false;
  115. } else {
  116. return true;
  117. }
  118. }
  119. bool getHapticAlsaDevice(int *card, int *device) override {
  120. std::string line;
  121. std::ifstream myfile(PROC_SND_PCM);
  122. if (myfile.is_open()) {
  123. while (getline(myfile, line)) {
  124. if (line.find(HAPTIC_PCM_DEVICE_SYMBOL) != std::string::npos) {
  125. std::stringstream ss(line);
  126. std::string currentToken;
  127. std::getline(ss, currentToken, ':');
  128. sscanf(currentToken.c_str(), "%d-%d", card, device);
  129. return true;
  130. }
  131. }
  132. myfile.close();
  133. } else {
  134. ALOGE("Failed to read file: %s", PROC_SND_PCM);
  135. }
  136. return false;
  137. }
  138. bool setHapticPcmAmp(struct pcm **haptic_pcm, bool enable, int card, int device) override {
  139. int ret = 0;
  140. if (enable) {
  141. *haptic_pcm = pcm_open(card, device, PCM_OUT, &haptic_nohost_config);
  142. if (!pcm_is_ready(*haptic_pcm)) {
  143. ALOGE("cannot open pcm_out driver: %s", pcm_get_error(*haptic_pcm));
  144. goto fail;
  145. }
  146. ret = pcm_prepare(*haptic_pcm);
  147. if (ret < 0) {
  148. ALOGE("cannot prepare haptic_pcm: %s", pcm_get_error(*haptic_pcm));
  149. goto fail;
  150. }
  151. ret = pcm_start(*haptic_pcm);
  152. if (ret < 0) {
  153. ALOGE("cannot start haptic_pcm: %s", pcm_get_error(*haptic_pcm));
  154. goto fail;
  155. }
  156. return true;
  157. } else {
  158. if (*haptic_pcm) {
  159. pcm_close(*haptic_pcm);
  160. *haptic_pcm = NULL;
  161. }
  162. return true;
  163. }
  164. fail:
  165. pcm_close(*haptic_pcm);
  166. *haptic_pcm = NULL;
  167. return false;
  168. }
  169. bool uploadOwtEffect(int fd, uint8_t *owtData, uint32_t numBytes, struct ff_effect *effect,
  170. uint32_t *outEffectIndex, int *status) override {
  171. (*effect).u.periodic.custom_len = numBytes / sizeof(uint16_t);
  172. delete[] ((*effect).u.periodic.custom_data);
  173. (*effect).u.periodic.custom_data = new int16_t[(*effect).u.periodic.custom_len]{0x0000};
  174. if ((*effect).u.periodic.custom_data == nullptr) {
  175. ALOGE("Failed to allocate memory for custom data\n");
  176. *status = EX_NULL_POINTER;
  177. return false;
  178. }
  179. memcpy((*effect).u.periodic.custom_data, owtData, numBytes);
  180. if ((*effect).id != -1) {
  181. ALOGE("(*effect).id != -1");
  182. }
  183. /* Create a new OWT waveform to update the PWLE or composite effect. */
  184. (*effect).id = -1;
  185. if (ioctl(fd, EVIOCSFF, effect) < 0) {
  186. ALOGE("Failed to upload effect %d (%d): %s", *outEffectIndex, errno, strerror(errno));
  187. delete[] ((*effect).u.periodic.custom_data);
  188. *status = EX_ILLEGAL_STATE;
  189. return false;
  190. }
  191. if ((*effect).id >= FF_MAX_EFFECTS || (*effect).id < 0) {
  192. ALOGE("Invalid waveform index after upload OWT effect: %d", (*effect).id);
  193. *status = EX_ILLEGAL_ARGUMENT;
  194. return false;
  195. }
  196. *outEffectIndex = (*effect).id;
  197. *status = 0;
  198. return true;
  199. }
  200. bool eraseOwtEffect(int fd, int8_t effectIndex, std::vector<ff_effect> *effect) override {
  201. uint32_t effectCountBefore, effectCountAfter, i, successFlush = 0;
  202. if (effectIndex < WAVEFORM_MAX_PHYSICAL_INDEX) {
  203. ALOGE("Invalid waveform index for OWT erase: %d", effectIndex);
  204. return false;
  205. }
  206. if (effectIndex < WAVEFORM_MAX_INDEX) {
  207. /* Normal situation. Only erase the effect which we just played. */
  208. if (ioctl(fd, EVIOCRMFF, effectIndex) < 0) {
  209. ALOGE("Failed to erase effect %d (%d): %s", effectIndex, errno, strerror(errno));
  210. }
  211. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < WAVEFORM_MAX_INDEX; i++) {
  212. if ((*effect)[i].id == effectIndex) {
  213. (*effect)[i].id = -1;
  214. break;
  215. }
  216. }
  217. } else {
  218. /* Flush all non-prestored effects of ff-core and driver. */
  219. getEffectCount(&effectCountBefore);
  220. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < FF_MAX_EFFECTS; i++) {
  221. if (ioctl(fd, EVIOCRMFF, i) >= 0) {
  222. successFlush++;
  223. }
  224. }
  225. getEffectCount(&effectCountAfter);
  226. ALOGW("Flushed effects: ff: %d; driver: %d -> %d; success: %d", effectIndex,
  227. effectCountBefore, effectCountAfter, successFlush);
  228. /* Reset all OWT effect index of HAL. */
  229. for (i = WAVEFORM_MAX_PHYSICAL_INDEX; i < WAVEFORM_MAX_INDEX; i++) {
  230. (*effect)[i].id = -1;
  231. }
  232. }
  233. return true;
  234. }
  235. void debug(int fd) override { HwApiBase::debug(fd); }
  236. private:
  237. std::ofstream mF0;
  238. std::ofstream mF0Offset;
  239. std::ofstream mRedc;
  240. std::ofstream mQ;
  241. std::ifstream mEffectCount;
  242. std::ifstream mVibeState;
  243. std::ifstream mOwtFreeSpace;
  244. std::ofstream mF0CompEnable;
  245. std::ofstream mRedcCompEnable;
  246. std::ofstream mMinOnOffInterval;
  247. };
  248. class HwCal : public Vibrator::HwCal, private HwCalBase {
  249. private:
  250. static constexpr char VERSION[] = "version";
  251. static constexpr char F0_CONFIG[] = "f0_measured";
  252. static constexpr char REDC_CONFIG[] = "redc_measured";
  253. static constexpr char Q_CONFIG[] = "q_measured";
  254. static constexpr char TICK_VOLTAGES_CONFIG[] = "v_tick";
  255. static constexpr char CLICK_VOLTAGES_CONFIG[] = "v_click";
  256. static constexpr char LONG_VOLTAGES_CONFIG[] = "v_long";
  257. static constexpr uint32_t VERSION_DEFAULT = 2;
  258. static constexpr int32_t DEFAULT_FREQUENCY_SHIFT = 0;
  259. static constexpr std::array<uint32_t, 2> V_TICK_DEFAULT = {1, 100};
  260. static constexpr std::array<uint32_t, 2> V_CLICK_DEFAULT = {1, 100};
  261. static constexpr std::array<uint32_t, 2> V_LONG_DEFAULT = {1, 100};
  262. public:
  263. HwCal() {}
  264. bool getVersion(uint32_t *value) override {
  265. if (getPersist(VERSION, value)) {
  266. return true;
  267. }
  268. *value = VERSION_DEFAULT;
  269. return true;
  270. }
  271. bool getLongFrequencyShift(int32_t *value) override {
  272. return getProperty("long.frequency.shift", value, DEFAULT_FREQUENCY_SHIFT);
  273. }
  274. bool getF0(std::string *value) override { return getPersist(F0_CONFIG, value); }
  275. bool getRedc(std::string *value) override { return getPersist(REDC_CONFIG, value); }
  276. bool getQ(std::string *value) override { return getPersist(Q_CONFIG, value); }
  277. bool getTickVolLevels(std::array<uint32_t, 2> *value) override {
  278. if (getPersist(TICK_VOLTAGES_CONFIG, value)) {
  279. return true;
  280. }
  281. *value = V_TICK_DEFAULT;
  282. return true;
  283. }
  284. bool getClickVolLevels(std::array<uint32_t, 2> *value) override {
  285. if (getPersist(CLICK_VOLTAGES_CONFIG, value)) {
  286. return true;
  287. }
  288. *value = V_CLICK_DEFAULT;
  289. return true;
  290. }
  291. bool getLongVolLevels(std::array<uint32_t, 2> *value) override {
  292. if (getPersist(LONG_VOLTAGES_CONFIG, value)) {
  293. return true;
  294. }
  295. *value = V_LONG_DEFAULT;
  296. return true;
  297. }
  298. bool isChirpEnabled() override {
  299. bool value;
  300. getProperty("chirp.enabled", &value, false);
  301. return value;
  302. }
  303. bool getSupportedPrimitives(uint32_t *value) override {
  304. return getProperty("supported_primitives", value, (uint32_t)0);
  305. }
  306. bool isF0CompEnabled() override {
  307. bool value;
  308. getProperty("f0.comp.enabled", &value, true);
  309. return value;
  310. }
  311. bool isRedcCompEnabled() override {
  312. bool value;
  313. getProperty("redc.comp.enabled", &value, true);
  314. return value;
  315. }
  316. void debug(int fd) override { HwCalBase::debug(fd); }
  317. };
  318. } // namespace vibrator
  319. } // namespace hardware
  320. } // namespace android
  321. } // namespace aidl