Skip to contentSkip to conversation

input: Do not re-page HID devices that disconnected on purpose

PATCH In review Dylan MacKenzie 3 participants 5 messages Nov 21 – Nov 23 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

cover input: Do not re-page HID devices that disconnected on purpose

The input plugin re-pages a HID device after its connection is closed even when the link was terminated on purpose, which takes devices that can be bonded to several hosts, e.g. earbuds, away from the host they just switched to. Details are in patch 2.

Patch 1 keeps the reason of the last disconnection in the per-bearer state of the device, patch 2 uses it to skip the reconnection when the link was terminated by the remote device or by the local host. Link loss keeps being handled as before.

One behaviour change to be aware of: the kernel reports Remote Device Terminated Connection due to Power Off (0x15) as MGMT_DEV_DISCONN_REMOTE as well, so a device with ReconnectMode "host" that announces it is powering off is no longer paged if it comes back within 3 minutes.

Tested with Pixel Buds Pro 2 (ReconnectMode "any") on top of 5.87.

Dylan MacKenzie (2):
device: Add btd_device_get_disconnect_reason
input/device: Only auto-reconnect on link loss

 profiles/input/device.c | 18 ++++++++++++++++++
 src/device.c            |  7 +++++++
 src/device.h            |  1 +
 3 files changed, 26 insertions(+)
Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/2 device: Add btd_device_get_disconnect_reason

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(+)
src/device.c +7 -0
index 36a980d1e..720b4af69 100644
@@ -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);
src/device.h +1 -0
index 047a894c3..4527c16d1 100644
@@ -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);
Signature · 1 line
2.56.0
#4 lore raw

PATCH 2/2 input/device: Only auto-reconnect on link loss

In reply to 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:

Quoted 1 line

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(+)
profiles/input/device.c +18 -0
index 5b1be2b16..93d51be49 100644
@@ -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);
 
Signature · 1 line
2.56.0
#2 lore raw
In reply to Dylan MacKenzie

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1218301/

---Test result---

Test Summary:

CheckPatch                    PASS      0.73 seconds
GitLint                       PASS      0.50 seconds
BuildEll                      PASS      17.21 seconds
BluezMake                     PASS      257.03 seconds
MakeCheck                     PASS      0.95 seconds
MakeDistcheck                 PASS      129.88 seconds
CheckValgrind                 PASS      134.93 seconds
CheckSmatch                   PASS      233.08 seconds
bluezmakeextell               PASS      85.62 seconds
TestFunctional                PASS      1069.87 seconds
IncrementalBuild              PASS      259.98 seconds
ScanBuild                     PASS      908.77 seconds

https://github.com/bluez/bluez/pull/2864

---
Regards,
Linux Bluetooth

#3 lore raw

Hi,

Quoted 27 lines · On Sat, Nov 21, 2026 at 6:18 PM <[email protected]> wrote:

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1218301/

---Test result---

Test Summary:

CheckPatch                    PASS      0.73 seconds
GitLint                       PASS      0.50 seconds
BuildEll                      PASS      17.21 seconds
BluezMake                     PASS      257.03 seconds
MakeCheck                     PASS      0.95 seconds
MakeDistcheck                 PASS      129.88 seconds
CheckValgrind                 PASS      134.93 seconds
CheckSmatch                   PASS      233.08 seconds
bluezmakeextell               PASS      85.62 seconds
TestFunctional                PASS      1069.87 seconds
IncrementalBuild              PASS      259.98 seconds
ScanBuild                     PASS      908.77 seconds
Quoted 1 line

https://github.com/bluez/bluez/pull/2864

Copilot flagged the following problems:

https://github.com/bluez/bluez/pull/2864#pullrequestreview-6024217611

Quoted 4 lines

---
Regards,
Linux Bluetooth

Signature · 1 line
Luiz Augusto von Dentz