Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ 0/2] input: Do not re-page HID devices that disconnected on purpose
@ 2026-11-21 20:58 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 ` [PATCH BlueZ 2/2] input/device: Only auto-reconnect on link loss Dylan MacKenzie
  0 siblings, 2 replies; 4+ messages in thread
From: Dylan MacKenzie @ 2026-11-21 20:58 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Dylan MacKenzie

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(+)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [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

* RE: input: Do not re-page HID devices that disconnected on purpose
  2026-11-21 20:58 ` [PATCH BlueZ 1/2] device: Add btd_device_get_disconnect_reason Dylan MacKenzie
@ 2026-11-21 23:18   ` bluez.test.bot
  0 siblings, 0 replies; 4+ messages in thread
From: bluez.test.bot @ 2026-11-21 23:18 UTC (permalink / raw)
  To: linux-bluetooth, ecstaticmorse

[-- Attachment #1: Type: text/plain, Size: 1020 bytes --]

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-21 23:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox