Device cleanup (#592)

- Replaced device_find_\* usage by device_get_\* variants.
- Removed deprecated device_find_\* functions.
- Improved reliability of Bluetooth scanning, pairing, connection, and HID host lifecycle behavior, including peer auto-connect handling.
- Improved Bluetooth/USB status indicators and clarified “Eject failed” alerts with the affected mount path.
This commit is contained in:
Ken Van Hoeylandt
2026-07-27 17:29:12 +02:00
committed by GitHub
parent 3354924359
commit d1f06cb774
20 changed files with 328 additions and 204 deletions
@@ -1,16 +1,18 @@
#include "tab5_headphone_detect.h"
#include <tactility/error.h>
#include <tactility/device.h>
#include <tactility/drivers/gpio.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/log.h>
#include <tactility/concurrent/mutex.h>
#include <freertos/FreeRTOS.h>
#include <freertos/timers.h>
#include <atomic>
#define TAG "Tab5"
constexpr auto* TAG = "Tab5";
// PI4IOE5V6408-0 (0x43) bit 1
constexpr auto GPIO_EXP0_PIN_SPEAKER_ENABLE = 1;
@@ -20,18 +22,88 @@ constexpr auto GPIO_EXP0_PIN_HEADPHONE_DETECT = 7;
constexpr auto HP_DETECT_POLL_MS = 1000;
static TimerHandle_t hp_detect_timer = nullptr;
static std::atomic<Device*> io_expander0_cached { nullptr };
// Flags are written by the timer daemon task
static std::atomic hp_detect_last { false };
static std::atomic hp_detect_initialized { false };
static void headphone_detect_callback(TimerHandle_t /*timer*/) {
Device* cached = io_expander0_cached.load(std::memory_order_acquire);
if (!cached) {
cached = device_find_by_name("io_expander0");
io_expander0_cached.store(cached, std::memory_order_release);
// Owns the cached io_expander0 reference.
// Takes care of refcounting and concurrency.
struct HeadphoneDetectCache {
Mutex mutex {};
Device* io_expander0 = nullptr;
bool active = false;
HeadphoneDetectCache() {
mutex_construct(&mutex);
}
bool isActive() {
mutex_lock(&mutex);
bool result = active;
mutex_unlock(&mutex);
return result;
}
void setActive(bool value) {
mutex_lock(&mutex);
active = value;
mutex_unlock(&mutex);
}
Device* getIoExpander0() {
mutex_lock(&mutex);
Device* dev = io_expander0;
if (dev) {
device_get(dev);
}
mutex_unlock(&mutex);
return dev;
}
// Pass nullptr to clear/release the current entry - that always succeeds, regardless of
// `active`, since it's what stop() uses to tear the cache down.
bool setIoExpander0(Device* dev) {
mutex_lock(&mutex);
if (dev && !active) {
mutex_unlock(&mutex);
return false;
}
Device* old = io_expander0;
if (dev) {
device_get(dev);
}
io_expander0 = dev;
mutex_unlock(&mutex);
if (old) {
device_put(old);
}
return true;
}
};
static HeadphoneDetectCache& headphoneDetectCache() {
static HeadphoneDetectCache instance;
return instance;
}
static void headphone_detect_callback(TimerHandle_t /*timer*/) {
auto& cache = headphoneDetectCache();
if (!cache.isActive()) {
return; // Teardown is in progress or done - don't acquire/publish a new reference
}
Device* io_expander0 = cache.getIoExpander0();
if (!io_expander0) {
Device* dev = nullptr;
if (device_get_by_name("io_expander0", &dev) == ERROR_NONE) {
if (cache.setIoExpander0(dev)) {
device_put(dev); // Cache now holds its own reference
io_expander0 = cache.getIoExpander0();
} else {
io_expander0 = dev; // Deactivated concurrently - use our own reference just this once
}
}
}
auto* io_expander0 = cached;
if (!io_expander0) {
return; // Not ready yet, will retry on next tick
}
@@ -39,6 +111,7 @@ static void headphone_detect_callback(TimerHandle_t /*timer*/) {
auto* hp_pin = gpio_descriptor_acquire(io_expander0, GPIO_EXP0_PIN_HEADPHONE_DETECT, GPIO_FLAG_DIRECTION_INPUT, GPIO_OWNER_GPIO);
if (!hp_pin) {
LOG_W(TAG, "hp_detect: HP_DET pin busy");
device_put(io_expander0);
return;
}
@@ -48,6 +121,7 @@ static void headphone_detect_callback(TimerHandle_t /*timer*/) {
if (err != ERROR_NONE) {
LOG_W(TAG, "hp_detect: HP_DET read error: %s", error_to_string(err));
device_put(io_expander0);
return;
}
@@ -57,18 +131,22 @@ static void headphone_detect_callback(TimerHandle_t /*timer*/) {
auto* spk_pin = gpio_descriptor_acquire(io_expander0, GPIO_EXP0_PIN_SPEAKER_ENABLE, GPIO_FLAG_DIRECTION_OUTPUT, GPIO_OWNER_GPIO);
if (!spk_pin) {
LOG_W(TAG, "hp_detect: SPK_EN pin busy, will retry");
device_put(io_expander0);
return;
}
error_t spk_err = gpio_descriptor_set_level(spk_pin, !hp);
gpio_descriptor_release(spk_pin);
if (spk_err != ERROR_NONE) {
LOG_W(TAG, "hp_detect: SPK_EN set error: %s, will retry", error_to_string(spk_err));
device_put(io_expander0);
return;
}
hp_detect_last = hp;
hp_detect_initialized = true;
LOG_I(TAG, "Headphones %s, speaker %s", hp ? "detected" : "removed", hp ? "disabled" : "enabled");
}
device_put(io_expander0);
}
void tab5_headphone_detect_start() {
@@ -80,15 +158,20 @@ void tab5_headphone_detect_start() {
hp_detect_initialized = false;
hp_detect_last = false;
auto& cache = headphoneDetectCache();
cache.setActive(true);
hp_detect_timer = xTimerCreate("hp_detect", pdMS_TO_TICKS(HP_DETECT_POLL_MS), pdTRUE, nullptr, headphone_detect_callback);
if (!hp_detect_timer) {
LOG_E(TAG, "Failed to create hp_detect timer");
cache.setActive(false);
return;
}
if (xTimerStart(hp_detect_timer, pdMS_TO_TICKS(100)) != pdPASS) {
LOG_E(TAG, "Failed to start hp_detect timer");
xTimerDelete(hp_detect_timer, pdMS_TO_TICKS(100));
hp_detect_timer = nullptr;
cache.setActive(false);
}
}
@@ -97,6 +180,10 @@ void tab5_headphone_detect_stop() {
return;
}
auto& cache = headphoneDetectCache();
// Block any callback invocation from this point on from installing a new reference.
cache.setActive(false);
if (xTimerStop(hp_detect_timer, pdMS_TO_TICKS(100)) != pdPASS) {
LOG_W(TAG, "Failed to stop hp_detect timer");
}
@@ -106,5 +193,6 @@ void tab5_headphone_detect_stop() {
// Always clear the handle — stale non-null handle is worse than a resource leak, as it would
// cause tab5_headphone_detect_start() to silently skip re-creating the timer.
hp_detect_timer = nullptr;
io_expander0_cached.store(nullptr, std::memory_order_release);
cache.setIoExpander0(nullptr);
}