From 971ea5d074ef111faaf5700ace396bb6442c3e38 Mon Sep 17 00:00:00 2001 From: David Lin Date: Mon, 27 Mar 2017 19:13:31 -0700 Subject: [PATCH] vibrator hal: add support for drv2624 haptics driver on wahoo This adds the vibrator HAL that implements the new setAmplitude and perform(Effect) API. Test done: vibrator_hidl_hal_test Bug: 36782452 Change-Id: If9988434277790becb469d4dd928e75f7e6af41a Signed-off-by: David Lin --- Android.bp | 1 + device.mk | 2 +- init.hardware.rc | 9 + sepolicy/file_contexts | 1 + vibrator/Android.bp | 30 ++++ vibrator/Vibrator.cpp | 167 ++++++++++++++++++ vibrator/Vibrator.h | 59 +++++++ ...oid.hardware.vibrator@1.0-service.wahoo.rc | 4 + vibrator/service.cpp | 115 ++++++++++++ 9 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 vibrator/Android.bp create mode 100644 vibrator/Vibrator.cpp create mode 100644 vibrator/Vibrator.h create mode 100644 vibrator/android.hardware.vibrator@1.0-service.wahoo.rc create mode 100644 vibrator/service.cpp diff --git a/Android.bp b/Android.bp index 74cf9b4b..e4aee1c5 100644 --- a/Android.bp +++ b/Android.bp @@ -1,3 +1,4 @@ subdirs = [ "vr", + "vibrator", ] diff --git a/device.mk b/device.mk index d178f0a9..7483c08b 100644 --- a/device.mk +++ b/device.mk @@ -316,7 +316,7 @@ PRODUCT_PACKAGES += \ # Vibrator HAL PRODUCT_PACKAGES += \ - android.hardware.vibrator@1.0-impl + android.hardware.vibrator@1.0-service.wahoo # Thermal packages PRODUCT_PACKAGES += \ diff --git a/init.hardware.rc b/init.hardware.rc index f4cc6d5d..8abf64fb 100644 --- a/init.hardware.rc +++ b/init.hardware.rc @@ -287,6 +287,15 @@ on boot # Permission for laser sensor driver chown camera camera /sys/class/wahoo_laser/laser/enable_ps_sensor + # Permission for Vibrator + chown system system /sys/class/leds/vibrator/device/mode + chown system system /sys/class/leds/vibrator/device/od_clamp + chown system system /sys/class/leds/vibrator/device/rtp_input + chown system system /sys/class/leds/vibrator/device/scale + chown system system /sys/class/leds/vibrator/device/set_sequencer + chown system system /sys/class/leds/vibrator/device/autocal_result + chown system system /sys/class/leds/vibrator/device/ctrl_loop + # Permission for LED driver chown system system /sys/class/leds/red/on_off_ms chown system system /sys/class/leds/green/on_off_ms diff --git a/sepolicy/file_contexts b/sepolicy/file_contexts index 066e3168..f7acc76f 100644 --- a/sepolicy/file_contexts +++ b/sepolicy/file_contexts @@ -143,6 +143,7 @@ sys/devices/bt_wcn[0-9]+/rfkill/rfkill0/state u:object_r:s /vendor/bin/init\.qcom\.ipastart\.sh u:object_r:init-qcom-ipastart-sh_exec:s0 /vendor/bin/init\.insmod\.sh u:object_r:init-insmod-sh_exec:s0 /vendor/etc/init\.insmod\.cfg u:object_r:init-insmod-sh_exec:s0 +/vendor/bin/hw/android\.hardware\.vibrator@1\.0-service.wahoo u:object_r:hal_vibrator_default_exec:s0 # data files /data/misc/radio(/.*)? u:object_r:radio_data_file:s0 diff --git a/vibrator/Android.bp b/vibrator/Android.bp new file mode 100644 index 00000000..0c46514e --- /dev/null +++ b/vibrator/Android.bp @@ -0,0 +1,30 @@ +// +// Copyright (C) 2017 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +cc_binary { + name: "android.hardware.vibrator@1.0-service.wahoo", + relative_install_path: "hw", + init_rc: ["android.hardware.vibrator@1.0-service.wahoo.rc"], + srcs: ["service.cpp", "Vibrator.cpp"], + shared_libs: [ + "libhidlbase", + "libhidltransport", + "liblog", + "libhwbinder", + "libutils", + "libhardware", + "android.hardware.vibrator@1.0", + ], + proprietary: true, +} diff --git a/vibrator/Vibrator.cpp b/vibrator/Vibrator.cpp new file mode 100644 index 00000000..3f6b0f1f --- /dev/null +++ b/vibrator/Vibrator.cpp @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "VibratorService" + +#include + +#include +#include + +#include "Vibrator.h" + +#include +#include +#include +#include + + +namespace android { +namespace hardware { +namespace vibrator { +namespace V1_0 { +namespace implementation { + +static constexpr int8_t MAX_RTP_INPUT = 127; +static constexpr int8_t MIN_RTP_INPUT = 0; + +static constexpr char RTP_MODE[] = "rtp"; +static constexpr char WAVEFORM_MODE[] = "waveform"; + +// Use effect #1 in the waveform library +static constexpr char WAVEFORM_CLICK_EFFECT_SEQ[] = "1 0"; +static constexpr uint32_t WAVEFORM_CLICK_EFFECT_MS = 45; + +// Make double click a single click and loop once +static constexpr char WAVEFORM_DOUBLE_CLICK_EFFECT_SEQ[] = "1 1"; +static constexpr uint32_t WAVEFORM_DOUBLE_CLICK_EFFECT_MS = WAVEFORM_CLICK_EFFECT_MS * 2; + +Vibrator::Vibrator(std::ofstream&& activate, std::ofstream&& duration, + std::ofstream&& state, std::ofstream&& rtpinput, + std::ofstream&& mode, std::ofstream&& sequencer, + std::ofstream&& scale) : + mActivate(std::move(activate)), + mDuration(std::move(duration)), + mState(std::move(state)), + mRtpInput(std::move(rtpinput)), + mMode(std::move(mode)), + mSequencer(std::move(sequencer)), + mScale(std::move(scale)) {} + +// Methods from ::android::hardware::vibrator::V1_0::IVibrator follow. +Return Vibrator::on(uint32_t timeout_ms) { + + mDuration << timeout_ms << std::endl; + if (!mDuration) { + ALOGE("Failed to set duration (%d): %s", errno, strerror(errno)); + return Status::UNKNOWN_ERROR; + } + + mActivate << 1 << std::endl; + if (!mActivate) { + ALOGE("Failed to activate (%d): %s", errno, strerror(errno)); + return Status::UNKNOWN_ERROR; + } + + return Status::OK; +} + +Return Vibrator::off() { + + mActivate << 0 << std::endl; + if (!mActivate) { + ALOGE("Failed to turn vibrator off (%d): %s", errno, strerror(errno)); + return Status::UNKNOWN_ERROR; + } + + mMode << RTP_MODE << std::endl; + if (!mMode) { + ALOGE("Failed to set RTP mode (%d): %s", errno, strerror(errno)); + return Status::UNKNOWN_ERROR; + } + return Status::OK; +} + +Return Vibrator::supportsAmplitudeControl() { + return (mRtpInput ? true : false); +} + +Return Vibrator::setAmplitude(uint8_t amplitude) { + + if (amplitude == 0) { + return Status::BAD_VALUE; + } + + int8_t rtp_input = + std::round((amplitude - 1) / 254.0 * (MAX_RTP_INPUT - MIN_RTP_INPUT) + + MIN_RTP_INPUT); + + mRtpInput << rtp_input; + if (!mRtpInput) { + ALOGE("Failed to set amplitude (%d): %s", errno, strerror(errno)); + return Status::UNKNOWN_ERROR; + } + + return Status::OK; +} + +static uint8_t convertEffectStrength(EffectStrength strength) { + uint8_t scale; + + switch (strength) { + case EffectStrength::LIGHT: + scale = 2; // 50% + break; + case EffectStrength::MEDIUM: + scale = 1; // 75% + break; + default: + case EffectStrength::STRONG: + scale = 0; // 100% + break; + } + + return scale; +} + +Return Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) { + Status status = Status::OK; + uint32_t timeMS; + + if (effect == Effect::CLICK) { + mSequencer << WAVEFORM_CLICK_EFFECT_SEQ << std::endl; + timeMS = WAVEFORM_CLICK_EFFECT_MS; + } else if (effect == Effect::DOUBLE_CLICK) { + mSequencer << WAVEFORM_DOUBLE_CLICK_EFFECT_SEQ << std::endl; + timeMS = WAVEFORM_DOUBLE_CLICK_EFFECT_MS; + } else { + _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); + return Void(); + } + + mMode << WAVEFORM_MODE << std::endl; + mScale << convertEffectStrength(strength) << std::endl; + on(timeMS); + + _hidl_cb(status, timeMS); + return Void(); +} + +} // namespace implementation +} // namespace V1_0 +} // namespace vibrator +} // namespace hardware +} // namespace android diff --git a/vibrator/Vibrator.h b/vibrator/Vibrator.h new file mode 100644 index 00000000..7a366803 --- /dev/null +++ b/vibrator/Vibrator.h @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef ANDROID_HARDWARE_VIBRATOR_V1_0_VIBRATOR_H +#define ANDROID_HARDWARE_VIBRATOR_V1_0_VIBRATOR_H + +#include +#include + +#include + +namespace android { +namespace hardware { +namespace vibrator { +namespace V1_0 { +namespace implementation { + +class Vibrator : public IVibrator { +public: + Vibrator(std::ofstream&& activate, std::ofstream&& duration, + std::ofstream&& state, std::ofstream&& rtpinput, + std::ofstream&& mode, std::ofstream&& sequencer, + std::ofstream&& scale); + + // Methods from ::android::hardware::vibrator::V1_0::IVibrator follow. + Return on(uint32_t timeoutMs) override; + Return off() override; + Return supportsAmplitudeControl() override; + Return setAmplitude(uint8_t amplitude) override; + Return perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) override; + +private: + std::ofstream mActivate; + std::ofstream mDuration; + std::ofstream mState; + std::ofstream mRtpInput; + std::ofstream mMode; + std::ofstream mSequencer; + std::ofstream mScale; +}; +} // namespace implementation +} // namespace V1_0 +} // namespace vibrator +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_VIBRATOR_V1_0_VIBRATOR_H diff --git a/vibrator/android.hardware.vibrator@1.0-service.wahoo.rc b/vibrator/android.hardware.vibrator@1.0-service.wahoo.rc new file mode 100644 index 00000000..e8487149 --- /dev/null +++ b/vibrator/android.hardware.vibrator@1.0-service.wahoo.rc @@ -0,0 +1,4 @@ +service vibrator-1-0 /vendor/bin/hw/android.hardware.vibrator@1.0-service.wahoo + class hal + user system + group system diff --git a/vibrator/service.cpp b/vibrator/service.cpp new file mode 100644 index 00000000..0d70cb77 --- /dev/null +++ b/vibrator/service.cpp @@ -0,0 +1,115 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#define LOG_TAG "android.hardware.vibrator@1.0-service.wahoo" + +#include +#include +#include +#include +#include + +#include "Vibrator.h" + +using android::hardware::configureRpcThreadpool; +using android::hardware::joinRpcThreadpool; +using android::hardware::vibrator::V1_0::IVibrator; +using android::hardware::vibrator::V1_0::implementation::Vibrator; +using namespace android; + +// Refer to Documentation/ABI/testing/sysfs-class-led-driver-drv2624 +// kernel documentation on the detail usages for ABIs below +static const char *ACTIVATE_PATH = "/sys/class/leds/vibrator/activate"; +static const char *DURATION_PATH = "/sys/class/leds/vibrator/duration"; +static const char *STATE_PATH = "/sys/class/leds/vibrator/state"; +static const char *RTP_INPUT_PATH = "/sys/class/leds/vibrator/device/rtp_input"; +static const char *MODE_PATH = "/sys/class/leds/vibrator/device/mode"; +static const char *SEQUENCER_PATH = "/sys/class/leds/vibrator/device/set_sequencer"; +static const char *SCALE_PATH = "/sys/class/leds/vibrator/device/scale"; + +status_t registerVibratorService() { + // ostreams below are required + std::ofstream activate{ACTIVATE_PATH}; + if (!activate) { + int error = errno; + ALOGE("Failed to open %s (%d): %s", ACTIVATE_PATH, error, strerror(error)); + return -error; + } + + std::ofstream duration{DURATION_PATH}; + if (!duration) { + int error = errno; + ALOGE("Failed to open %s (%d): %s", DURATION_PATH, error, strerror(error)); + return -error; + } + + std::ofstream state{STATE_PATH}; + if (!state) { + int error = errno; + ALOGE("Failed to open %s (%d): %s", STATE_PATH, error, strerror(error)); + return -error; + } + + state << 1 << std::endl; + if (!state) { + int error = errno; + ALOGE("Failed to set state (%d): %s", errno, strerror(errno)); + return -error; + } + + // ostreams below are optional + std::ofstream rtpinput{RTP_INPUT_PATH}; + if (!state) { + int error = errno; + ALOGW("Failed to open %s (%d): %s", RTP_INPUT_PATH, error, strerror(error)); + } + + std::ofstream mode{MODE_PATH}; + if (!state) { + int error = errno; + ALOGW("Failed to open %s (%d): %s", MODE_PATH, error, strerror(error)); + } + + std::ofstream sequencer{SEQUENCER_PATH}; + if (!state) { + int error = errno; + ALOGW("Failed to open %s (%d): %s", SEQUENCER_PATH, error, strerror(error)); + } + + std::ofstream scale{SCALE_PATH}; + if (!state) { + int error = errno; + ALOGW("Failed to open %s (%d): %s", SCALE_PATH, error, strerror(error)); + } + + sp vibrator = new Vibrator(std::move(activate), std::move(duration), + std::move(state), std::move(rtpinput), std::move(mode), + std::move(sequencer), std::move(scale)); + + vibrator->registerAsService(); + + return OK; +} + +int main() { + configureRpcThreadpool(1, true); + status_t status = registerVibratorService(); + + if (status != OK) { + return status; + } + + joinRpcThreadpool(); +}