* [PATCH BlueZ 1/2] device: Add btd_device_get_disconnect_reason
2026-11-21 20:58 [PATCH BlueZ 0/2] input: Do not re-page HID devices that disconnected on purpose Dylan MacKenzie
@ 2026-11-21 20:58 ` Dylan MacKenzie
2026-11-21 23:18 ` input: Do not re-page HID devices that disconnected on purpose bluez.test.bot
2026-11-21 20:58 ` [PATCH BlueZ 2/2] input/device: Only auto-reconnect on link loss Dylan MacKenzie
1 sibling, 1 reply; 4+ messages in thread
From: Dylan MacKenzie @ 2026-11-21 20:58 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Dylan MacKenzie
Keep the reason of the last disconnection in the bearer state and add
btd_device_get_disconnect_reason so it can be queried per bearer. The
new field takes the place of padding so struct bearer_state does not
grow.
Assisted-by: Claude:claude-fable-5-2
---
src/device.c | 7 +++++++
src/device.h | 1 +
2 files changed, 8 insertions(+)
diff --git a/src/device.c b/src/device.c
index 36a980d1e..720b4af69 100644
--- a/src/device.c
+++ b/src/device.c
@@ -155,6 +155,7 @@ struct bearer_state {
bool svc_resolved;
bool initiator;
bool connectable;
+ uint8_t disconn_reason;
time_t last_seen;
time_t last_used;
};
@@ -3864,6 +3865,11 @@ bool btd_device_bdaddr_type_connected(struct btd_device *dev, uint8_t type)
return dev->le_state.connected;
}
+uint8_t btd_device_get_disconnect_reason(struct btd_device *dev, uint8_t type)
+{
+ return get_state(dev, type)->disconn_reason;
+}
+
bool btd_device_bdaddr_type_connectable(struct btd_device *dev, uint8_t type)
{
if (type == BDADDR_BREDR)
@@ -4071,6 +4077,7 @@ void device_remove_connection(struct btd_device *device, uint8_t bdaddr_type,
state->connected = false;
state->initiator = false;
+ state->disconn_reason = reason;
device->general_connect = FALSE;
device_set_svc_refreshed(device, false);
diff --git a/src/device.h b/src/device.h
index 047a894c3..4527c16d1 100644
--- a/src/device.h
+++ b/src/device.h
@@ -127,6 +127,7 @@ void device_set_flags(struct btd_device *device, uint8_t flags);
bool btd_device_is_connected(struct btd_device *dev);
bool btd_device_bearer_is_connected(struct btd_device *dev);
bool btd_device_bdaddr_type_connected(struct btd_device *dev, uint8_t type);
+uint8_t btd_device_get_disconnect_reason(struct btd_device *dev, uint8_t type);
bool btd_device_bdaddr_type_connectable(struct btd_device *dev, uint8_t type);
bool btd_device_bdaddr_type_seen(struct btd_device *dev, uint8_t type);
bool btd_device_bdaddr_type_initiator(struct btd_device *dev, uint8_t type);
--
2.56.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH BlueZ 2/2] input/device: Only auto-reconnect on link loss
2026-11-21 20:58 [PATCH BlueZ 0/2] input: Do not re-page HID devices that disconnected on purpose Dylan MacKenzie
2026-11-21 20:58 ` [PATCH BlueZ 1/2] device: Add btd_device_get_disconnect_reason Dylan MacKenzie
@ 2026-11-21 20:58 ` Dylan MacKenzie
1 sibling, 0 replies; 4+ messages in thread
From: Dylan MacKenzie @ 2026-11-21 20:58 UTC (permalink / raw)
To: linux-bluetooth; +Cc: Dylan MacKenzie
Commit 5cbdfbd83d66 ("input: Automatically attempt a reconnect when
required.") added a timer that pages a HID device every 30 seconds
for up to 3 minutes after its connection is closed, in order to
restore the connection of a device that "goes out of range and comes
back". The timer is armed whenever the interrupt channel is closed
though, including when the link was terminated on purpose.
This breaks devices which can be bonded to several hosts and expose
HID next to other profiles, such as earbuds with a head tracking
sensor (ReconnectMode "any"). When they move to another host they
disconnect with Remote User Terminated Connection:
> HCI Event: Disconnect Complete (0x05) plen 4
Status: Success (0x00)
Handle: 51
Reason: Remote User Terminated Connection (0x13)
and 30 seconds later bluetoothd connects HID again, which brings the
audio profiles back with it and takes the device away from the host
the user just switched to.
Check why the BR/EDR link ended and do not start the timer if it was
terminated by the remote device or by the local host. Other reasons,
including an unknown one, keep reconnecting as before.
Assisted-by: Claude:claude-fable-5-2
---
profiles/input/device.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/profiles/input/device.c b/profiles/input/device.c
index 5b1be2b16..93d51be49 100644
--- a/profiles/input/device.c
+++ b/profiles/input/device.c
@@ -24,6 +24,7 @@
#include "bluetooth/bluetooth.h"
#include "bluetooth/hidp.h"
+#include "bluetooth/mgmt.h"
#include "bluetooth/sdp.h"
#include "bluetooth/sdp_lib.h"
#include "bluetooth/uuid.h"
@@ -1422,6 +1423,8 @@ static const char *reconnect_mode_to_string(const enum reconnect_mode_t mode)
static void input_device_enter_reconnect_mode(struct input_device *idev)
{
+ uint8_t reason;
+
DBG("path=%s reconnect_mode=%s", idev->path,
reconnect_mode_to_string(idev->reconnect_mode));
@@ -1443,6 +1446,21 @@ static void input_device_enter_reconnect_mode(struct input_device *idev)
btd_device_is_connected(idev->device))
return;
+ /* The host is only expected to restore a connection that was lost,
+ * e.g. because the device went out of range. Do not page a device
+ * that terminated the link on purpose, or that the host itself
+ * disconnected: devices which can be bonded to several hosts (e.g.
+ * earbuds exposing a HID sensor) would otherwise be pulled back from
+ * the host they just switched to.
+ */
+ reason = btd_device_get_disconnect_reason(idev->device, BDADDR_BREDR);
+ if (reason == MGMT_DEV_DISCONN_REMOTE ||
+ reason == MGMT_DEV_DISCONN_LOCAL_HOST) {
+ DBG("link terminated on purpose (reason %u), not reconnecting",
+ reason);
+ return;
+ }
+
if (idev->reconnect_timer > 0)
timeout_remove(idev->reconnect_timer);
--
2.56.0
^ permalink raw reply related [flat|nested] 4+ messages in thread