Linux bluetooth development
 help / color / mirror / Atom feed
From: Kyle Pepper <cochcoder@proton.me>
To: linux-bluetooth@vger.kernel.org
Cc: luiz.dentz@gmail.com, marcel@holtmann.org
Subject: [RFC PATCH v4 2/2] Bluetooth: Load LE connection event length hints through MGMT
Date: Fri, 04 Dec 2026 01:03:27 +0000	[thread overview]
Message-ID: <20261204010225.3029996-3-cochcoder@proton.me> (raw)
In-Reply-To: <20261204010225.3029996-1-cochcoder@proton.me>

LE connection event length hints are connection parameters. Expose
them through a privileged per-peer MGMT command without changing
the existing Load Connection Parameters wire format.

Store hints for future central connections. For a central link that
is up, queue an LE Connection Update using the current connection
parameters and commit the hints only after the update succeeds.
Zero for both values clears the hints. The controller can choose
whether to honor them.

Codex assisted with the MGMT handler and CE hint changes.
AI assistance: v4
Signed-off-by: Kyle Pepper <cochcoder@proton.me>
---
 include/net/bluetooth/hci_core.h |   4 +
 include/net/bluetooth/mgmt.h     |   8 ++
 net/bluetooth/hci_conn.c         |   4 +-
 net/bluetooth/hci_event.c        |   4 +-
 net/bluetooth/hci_sync.c         |  51 +++++++---
 net/bluetooth/mgmt.c             | 163 +++++++++++++++++++++++++++++++
 6 files changed, 217 insertions(+), 17 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 106fc41a6..67eda3c92 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -786,6 +786,8 @@ struct hci_conn {
 	__u16		le_conn_min_interval;
 	__u16		le_conn_max_interval;
 	__u16		le_conn_interval;
+	__le16		le_min_ce_len;
+	__le16		le_max_ce_len;
 	__u16		le_conn_latency;
 	__u16		le_supv_timeout;
 	__u16		le_rate_interval;
@@ -885,6 +887,8 @@ struct hci_conn_params {
 	u16 conn_max_interval;
 	u16 conn_latency;
 	u16 supervision_timeout;
+	__le16 min_ce_len;
+	__le16 max_ce_len;
 
 	u16 rate_min_interval;
 	u16 rate_max_interval;
diff --git a/include/net/bluetooth/mgmt.h b/include/net/bluetooth/mgmt.h
index 17bdd6345..786039374 100644
--- a/include/net/bluetooth/mgmt.h
+++ b/include/net/bluetooth/mgmt.h
@@ -912,6 +912,14 @@ struct mgmt_cp_load_conn_subrate {
 } __packed;
 #define MGMT_LOAD_CONN_SUBRATE_SIZE	2
 
+#define MGMT_OP_LOAD_CONN_EVENT_LEN	0x005D
+struct mgmt_cp_load_conn_event_len {
+	struct mgmt_addr_info addr;
+	__le16 min_ce_len;
+	__le16 max_ce_len;
+} __packed;
+#define MGMT_LOAD_CONN_EVENT_LEN_SIZE	11
+
 #define MGMT_EV_CMD_COMPLETE		0x0001
 struct mgmt_ev_cmd_complete {
 	__le16	opcode;
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index f37459d41..0a01912f5 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -536,8 +536,8 @@ static int le_conn_update_sync(struct hci_dev *hdev, void *data)
 	cp.conn_interval_max	= cpu_to_le16(d->max);
 	cp.conn_latency		= cpu_to_le16(d->latency);
 	cp.supervision_timeout	= cpu_to_le16(d->to_multiplier);
-	cp.min_ce_len		= cpu_to_le16(0x0000);
-	cp.max_ce_len		= cpu_to_le16(0x0000);
+	cp.min_ce_len		= conn->le_min_ce_len;
+	cp.max_ce_len		= conn->le_max_ce_len;
 	timeout			= conn->conn_timeout;
 
 	hci_dev_unlock(hdev);
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index a6c4fb486..e490faa5f 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -7640,8 +7640,8 @@ static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev, void *data,
 	cp.interval_max = ev->interval_max;
 	cp.latency = ev->latency;
 	cp.timeout = ev->timeout;
-	cp.min_ce_len = 0;
-	cp.max_ce_len = 0;
+	cp.min_ce_len = hcon->le_min_ce_len;
+	cp.max_ce_len = hcon->le_max_ce_len;
 
 	hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
 
diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index ef1009632..e1f3abcf5 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -7644,7 +7644,8 @@ static int hci_le_directed_advertising_sync(struct hci_dev *hdev,
 }
 
 static void set_ext_conn_params(struct hci_conn *conn,
-				struct hci_cp_le_ext_conn_param *p)
+				struct hci_cp_le_ext_conn_param *p,
+				__le16 min_ce_len, __le16 max_ce_len)
 {
 	struct hci_dev *hdev = conn->hdev;
 
@@ -7656,12 +7657,13 @@ static void set_ext_conn_params(struct hci_conn *conn,
 	p->conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
 	p->conn_latency = cpu_to_le16(conn->le_conn_latency);
 	p->supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
-	p->min_ce_len = cpu_to_le16(0x0000);
-	p->max_ce_len = cpu_to_le16(0x0000);
+	p->min_ce_len = min_ce_len;
+	p->max_ce_len = max_ce_len;
 }
 
 static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
-				       struct hci_conn *conn, u8 own_addr_type)
+				       struct hci_conn *conn, u8 own_addr_type,
+				       __le16 min_ce_len, __le16 max_ce_len)
 {
 	struct hci_cp_le_ext_create_conn *cp;
 	struct hci_cp_le_ext_conn_param *p;
@@ -7682,7 +7684,7 @@ static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
 	if (scan_1m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_1M ||
 			      conn->le_adv_sec_phy == HCI_ADV_PHY_1M)) {
 		cp->phys |= LE_SCAN_PHY_1M;
-		set_ext_conn_params(conn, p);
+		set_ext_conn_params(conn, p, min_ce_len, max_ce_len);
 
 		p++;
 		plen += sizeof(*p);
@@ -7691,7 +7693,7 @@ static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
 	if (scan_2m(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_2M ||
 			      conn->le_adv_sec_phy == HCI_ADV_PHY_2M)) {
 		cp->phys |= LE_SCAN_PHY_2M;
-		set_ext_conn_params(conn, p);
+		set_ext_conn_params(conn, p, min_ce_len, max_ce_len);
 
 		p++;
 		plen += sizeof(*p);
@@ -7700,7 +7702,7 @@ static int hci_le_ext_create_conn_sync(struct hci_dev *hdev,
 	if (scan_coded(hdev) && (conn->le_adv_phy == HCI_ADV_PHY_CODED ||
 				 conn->le_adv_sec_phy == HCI_ADV_PHY_CODED)) {
 		cp->phys |= LE_SCAN_PHY_CODED;
-		set_ext_conn_params(conn, p);
+		set_ext_conn_params(conn, p, min_ce_len, max_ce_len);
 
 		plen += sizeof(*p);
 	}
@@ -7715,6 +7717,8 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 {
 	struct hci_cp_le_create_conn cp;
 	struct hci_conn_params *params;
+	struct smp_irk *irk;
+	__le16 min_ce_len, max_ce_len;
 	u8 own_addr_type;
 	int err;
 	struct hci_conn *conn = data;
@@ -7754,19 +7758,32 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 
 	hci_dev_lock(hdev);
 
-	params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
+	/* Parameters are keyed by identity, not the RPA used to connect. */
+	irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
+	if (irk)
+		params = hci_conn_params_lookup(hdev, &irk->bdaddr,
+						irk->addr_type);
+	else
+		params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
 	if (params) {
 		conn->le_conn_min_interval = params->conn_min_interval;
 		conn->le_conn_max_interval = params->conn_max_interval;
 		conn->le_conn_latency = params->conn_latency;
 		conn->le_supv_timeout = params->supervision_timeout;
+		conn->le_min_ce_len = params->min_ce_len;
+		conn->le_max_ce_len = params->max_ce_len;
 	} else {
 		conn->le_conn_min_interval = hdev->le_conn_min_interval;
 		conn->le_conn_max_interval = hdev->le_conn_max_interval;
 		conn->le_conn_latency = hdev->le_conn_latency;
 		conn->le_supv_timeout = hdev->le_supv_timeout;
+		conn->le_min_ce_len = 0;
+		conn->le_max_ce_len = 0;
 	}
 
+	min_ce_len = conn->le_min_ce_len;
+	max_ce_len = conn->le_max_ce_len;
+
 	hci_dev_unlock(hdev);
 
 	/* If controller is scanning, we stop it since some controllers are
@@ -7795,7 +7812,8 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 
 	/* Send command LE Extended Create Connection if supported */
 	if (use_ext_conn(hdev)) {
-		err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type);
+		err = hci_le_ext_create_conn_sync(hdev, conn, own_addr_type,
+						  min_ce_len, max_ce_len);
 		goto done;
 	}
 
@@ -7811,8 +7829,8 @@ static int hci_le_create_conn_sync(struct hci_dev *hdev, void *data)
 	cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval);
 	cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
 	cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
-	cp.min_ce_len = cpu_to_le16(0x0000);
-	cp.max_ce_len = cpu_to_le16(0x0000);
+	cp.min_ce_len = min_ce_len;
+	cp.max_ce_len = max_ce_len;
 
 	/* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2261:
 	 *
@@ -8299,14 +8317,21 @@ int hci_le_conn_update_sync(struct hci_dev *hdev, struct hci_conn *conn,
 {
 	struct hci_cp_le_conn_update cp;
 
+	hci_dev_lock(hdev);
+	if (!hci_conn_valid(hdev, conn) || conn->state != BT_CONNECTED) {
+		hci_dev_unlock(hdev);
+		return -ENOTCONN;
+	}
+
 	memset(&cp, 0, sizeof(cp));
 	cp.handle		= cpu_to_le16(conn->handle);
 	cp.conn_interval_min	= cpu_to_le16(params->conn_min_interval);
 	cp.conn_interval_max	= cpu_to_le16(params->conn_max_interval);
 	cp.conn_latency		= cpu_to_le16(params->conn_latency);
 	cp.supervision_timeout	= cpu_to_le16(params->supervision_timeout);
-	cp.min_ce_len		= cpu_to_le16(0x0000);
-	cp.max_ce_len		= cpu_to_le16(0x0000);
+	cp.min_ce_len		= conn->le_min_ce_len;
+	cp.max_ce_len		= conn->le_max_ce_len;
+	hci_dev_unlock(hdev);
 
 	return __hci_cmd_sync_status(hdev, HCI_OP_LE_CONN_UPDATE,
 				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 01969ab57..6681ee301 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -130,6 +130,7 @@ static const u16 mgmt_commands[] = {
 	MGMT_OP_MESH_SEND,
 	MGMT_OP_MESH_SEND_CANCEL,
 	MGMT_OP_HCI_CMD_SYNC,
+	MGMT_OP_LOAD_CONN_EVENT_LEN,
 };
 
 static const u16 mgmt_events[] = {
@@ -8590,6 +8591,167 @@ static int load_conn_subrate(struct sock *sk, struct hci_dev *hdev, void *data,
 				 NULL, 0);
 }
 
+static int conn_event_len_sync(struct hci_dev *hdev, void *data)
+{
+	struct mgmt_pending_cmd *cmd = data;
+	struct mgmt_cp_load_conn_event_len *param = cmd->param;
+	struct hci_conn *conn = cmd->user_data;
+	struct hci_cp_le_conn_update cp = {};
+	u32 timeout;
+
+	hci_dev_lock(hdev);
+	if (!hci_conn_valid(hdev, conn) ||
+	    (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)) {
+		hci_dev_unlock(hdev);
+		return -ENOTCONN;
+	}
+
+	/* A legacy update would replace non-legacy rate parameters. */
+	if (conn->le_subrate > 1 ||
+	    (conn->le_rate_interval &&
+	     (conn->le_rate_interval != conn->le_conn_interval * 10 ||
+	      conn->le_rate_latency != conn->le_conn_latency ||
+	      conn->le_rate_supv_timeout != conn->le_supv_timeout))) {
+		hci_dev_unlock(hdev);
+		return -EOPNOTSUPP;
+	}
+
+	cp.handle = cpu_to_le16(conn->handle);
+	cp.conn_interval_min = cpu_to_le16(conn->le_conn_interval);
+	cp.conn_interval_max = cp.conn_interval_min;
+	cp.conn_latency = cpu_to_le16(conn->le_conn_latency);
+	cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout);
+	cp.min_ce_len = param->min_ce_len;
+	cp.max_ce_len = param->max_ce_len;
+	timeout = conn->conn_timeout ?: HCI_CMD_TIMEOUT;
+	hci_dev_unlock(hdev);
+
+	return hci_le_conn_update_wait_sync(hdev, &cp, timeout);
+}
+
+/* Caller holds hdev->lock. Clearing an unknown peer is a no-op. */
+static int conn_event_len_commit(struct hci_dev *hdev, struct hci_conn *conn,
+				 struct mgmt_cp_load_conn_event_len *cp, u8 type)
+{
+	struct hci_conn_params *params;
+
+	if (conn && hci_conn_valid(hdev, conn)) {
+		conn->le_min_ce_len = cp->min_ce_len;
+		conn->le_max_ce_len = cp->max_ce_len;
+	}
+
+	params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, type);
+	if (!params) {
+		if (!cp->min_ce_len && !cp->max_ce_len)
+			return 0;
+		params = hci_conn_params_add(hdev, &cp->addr.bdaddr, type);
+		if (!params)
+			return -ENOMEM;
+	}
+
+	params->min_ce_len = cp->min_ce_len;
+	params->max_ce_len = cp->max_ce_len;
+	return 0;
+}
+
+static u8 conn_event_len_status(int err)
+{
+	switch (err) {
+	case -ETIMEDOUT:
+		return MGMT_STATUS_TIMEOUT;
+	case -ENOTCONN:
+		return MGMT_STATUS_NOT_CONNECTED;
+	default:
+		return mgmt_status(err);
+	}
+}
+
+static void conn_event_len_complete(struct hci_dev *hdev, void *data, int err)
+{
+	struct mgmt_pending_cmd *cmd = data;
+	struct mgmt_cp_load_conn_event_len *cp = cmd->param;
+	struct hci_conn *conn = cmd->user_data;
+
+	if (!err) {
+		hci_dev_lock(hdev);
+		if (conn_event_len_commit(hdev, conn, cp,
+					  le_addr_type(cp->addr.type)))
+			bt_dev_warn(hdev, "failed to store CE length hints");
+		hci_dev_unlock(hdev);
+	}
+
+	mgmt_cmd_complete(cmd->sk, hdev->id, cmd->opcode,
+			  conn_event_len_status(err), NULL, 0);
+	hci_conn_put(conn);
+	mgmt_pending_free(cmd);
+}
+
+static int load_conn_event_len(struct sock *sk, struct hci_dev *hdev,
+			       void *data, u16 len)
+{
+	struct mgmt_cp_load_conn_event_len *cp = data;
+	struct hci_conn *conn;
+	u8 status;
+	u8 type;
+
+	if (!lmp_le_capable(hdev))
+		return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_EVENT_LEN,
+				       MGMT_STATUS_NOT_SUPPORTED);
+
+	if (cp->addr.type != BDADDR_LE_PUBLIC && cp->addr.type != BDADDR_LE_RANDOM)
+		goto invalid;
+	type = le_addr_type(cp->addr.type);
+
+	if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY) ||
+	    le16_to_cpu(cp->min_ce_len) > le16_to_cpu(cp->max_ce_len))
+		goto invalid;
+
+	hci_dev_lock(hdev);
+	conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, type);
+	if (conn && conn->role == HCI_ROLE_MASTER && conn->state == BT_CONNECT) {
+		status = MGMT_STATUS_BUSY;
+		goto done;
+	}
+
+	if (conn && conn->role == HCI_ROLE_MASTER &&
+	    (conn->state == BT_CONFIG || conn->state == BT_CONNECTED)) {
+		struct mgmt_pending_cmd *cmd;
+		int err;
+
+		cmd = mgmt_pending_new(sk, MGMT_OP_LOAD_CONN_EVENT_LEN, hdev,
+				       data, len);
+		if (!cmd) {
+			status = MGMT_STATUS_NO_RESOURCES;
+			goto done;
+		}
+
+		cmd->user_data = hci_conn_get(conn);
+		err = hci_cmd_sync_queue(hdev, conn_event_len_sync, cmd,
+					 conn_event_len_complete);
+		if (err) {
+			hci_conn_put(conn);
+			mgmt_pending_free(cmd);
+			status = mgmt_status(err);
+			goto done;
+		}
+
+		hci_dev_unlock(hdev);
+		return 0;
+	}
+
+	status = conn_event_len_commit(hdev, NULL, cp, type) ?
+		 MGMT_STATUS_NO_RESOURCES : MGMT_STATUS_SUCCESS;
+
+done:
+	hci_dev_unlock(hdev);
+	return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_EVENT_LEN,
+				 status, NULL, 0);
+
+invalid:
+	return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_EVENT_LEN,
+			       MGMT_STATUS_INVALID_PARAMS);
+}
+
 static int set_external_config(struct sock *sk, struct hci_dev *hdev,
 			       void *data, u16 len)
 {
@@ -10058,6 +10220,7 @@ static const struct hci_mgmt_handler mgmt_handlers[] = {
 	{ mgmt_hci_cmd_sync,       MGMT_HCI_CMD_SYNC_SIZE, HCI_MGMT_VAR_LEN },
 	{ load_conn_subrate,       MGMT_LOAD_CONN_SUBRATE_SIZE,
 						HCI_MGMT_VAR_LEN },
+	{ load_conn_event_len,      MGMT_LOAD_CONN_EVENT_LEN_SIZE },
 };
 
 void mgmt_index_added(struct hci_dev *hdev)


      parent reply	other threads:[~2026-12-04  1:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:41 [RFC PATCH v3 0/2] Bluetooth: Load LE connection event length hints through MGMT Kyle Pepper
2026-12-03 18:41 ` [RFC PATCH v3 1/2] Bluetooth: Allow custom matching of synchronous LE events Kyle Pepper
2026-12-04  2:10   ` Bluetooth: Load LE connection event length hints through MGMT bluez.test.bot
2026-12-03 18:41 ` [RFC PATCH v3 2/2] " Kyle Pepper
2026-12-04  1:02 ` [RFC PATCH v4 0/2] " Kyle Pepper
2026-12-04  1:03   ` [RFC PATCH v4 1/2] Bluetooth: Allow custom matching of synchronous LE events Kyle Pepper
2026-12-04  2:19     ` Bluetooth: Load LE connection event length hints through MGMT bluez.test.bot
2026-12-04  1:03   ` Kyle Pepper [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204010225.3029996-3-cochcoder@proton.me \
    --to=cochcoder@proton.me \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox