input: Do not re-page HID devices that disconnected on purpose
v2 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. The reason is checked both before the reconnection timer is started and every time it fires, and a timer still pending from an earlier link loss is cancelled when the connection is then closed on purpose. Link loss keeps being handled as before.
I tested with Pixel Buds Pro 2 with these changes applied on top of 5.87. They no longer reconnect to my Linux laptop after I connect them to my phone.
According to Claude:
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.
Hopefully a maintainer can evaluate the correctness/significance of this.
Changes in v2:
- Check the disconnect reason every time the timer fires as well as before starting it, so a device that was lost, came back and then left on purpose before the next attempt is not paged by the timer started for the earlier loss.
- Cancel a timer still pending from an earlier link loss when the connection is then closed on purpose.
The v1 problem was found by the AI review (Copilot) of the GitHub mirror of this series, bluez/bluez#2864 and seems legit. It suggested cancelling the pending timer where the reason was already checked. More robust is to check the condition twice, once before registering a timer and once when the timer fires.
Dylan MacKenzie (2):
device: Add btd_device_get_disconnect_reason
input/device: Only auto-reconnect on link loss
profiles/input/device.c | 34 +++++++++++++++++++++++++++++++++- src/device.c | 7 +++++++ src/device.h | 1 + 3 files changed, 41 insertions(+), 1 deletion(-)
Signature · 1 line
2.56.1
PATCH v2 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.
--- src/device.c | 7 +++++++ src/device.h | 1 + 2 files changed, 8 insertions(+)
src/device.c +7 -0
@@ -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
@@ -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.1
PATCH v2 2/2 input/device: Only auto-reconnect on link loss
In reply to Dylan MacKenzieCommit 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 last ended, both before starting the timer and every time it fires, and do not reconnect if the link was terminated by the remote device or by the local host. The check when the timer fires covers a device that was lost, came back and left on purpose between two attempts. Also cancel a timer that is still pending from an earlier link loss when the connection is then closed on purpose. Other reasons, including an unknown one, keep reconnecting as before.
--- profiles/input/device.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-)
profiles/input/device.c +33 -1
@@ -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"
@@ -1374,6 +1375,26 @@ static int dev_connect(struct input_device *idev)return -EIO ; } +/* 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. +*/ +static bool link_terminated_on_purpose (struct input_device *idev) +{ +uint8_t reason; + + reason =btd_device_get_disconnect_reason (idev->device,BDADDR_BREDR ); +if (reason !=MGMT_DEV_DISCONN_REMOTE && + reason !=MGMT_DEV_DISCONN_LOCAL_HOST ) +return false; + +DBG ("link terminated on purpose (reason %u), not reconnecting" , reason); + +return true; +} +static bool input_device_auto_reconnect (gpointer user_data) {struct input_device *idev = user_data;
@@ -1387,6 +1408,12 @@ static bool input_device_auto_reconnect(gpointer user_data) btd_device_is_connected(idev->device)) goto bail; +/* The device may have come back and left again since the timer was +* armed, e.g. to switch to another host. +*/ +if (link_terminated_on_purpose (idev)) + goto bail; +/* Only attempt an auto-reconnect for at most 3 minutes (6 * 30s). */ if (idev->reconnect_attempt >=6 ) goto bail;
@@ -1443,8 +1470,13 @@ static void input_device_enter_reconnect_mode(struct input_device *idev) btd_device_is_connected(idev->device))return ; -if (idev->reconnect_timer >0 ) +if (idev->reconnect_timer >0 ) {timeout_remove (idev->reconnect_timer); + idev->reconnect_timer =0 ; + } + +if (link_terminated_on_purpose (idev)) +return ;DBG ("registering auto-reconnect" ); idev->reconnect_attempt =0 ;
Signature · 1 line
2.56.1
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/1221566/
---Test result---
Test Summary:
CheckPatch PASS 1.01 seconds GitLint PASS 0.67 seconds BuildEll PASS 20.65 seconds BluezMake PASS 310.16 seconds MakeCheck PASS 1.06 seconds MakeDistcheck PASS 147.92 seconds CheckValgrind PASS 158.77 seconds CheckSmatch PASS 320.17 seconds bluezmakeextell PASS 101.15 seconds TestFunctional PASS 1224.91 seconds IncrementalBuild PASS 318.36 seconds ScanBuild PASS 1199.22 seconds
https://github.com/bluez/bluez/pull/2917
---
Regards,
Linux Bluetooth