Linux bluetooth development
 help / color / mirror / Atom feed
From: Kyle Pepper <cochcoder@proton.me>
To: linux-bluetooth@vger.kernel.org
Cc: marcel@holtmann.org, luiz.dentz@gmail.com
Subject: [RFC PATCH 2/2] Bluetooth: Add LE connection event length requests
Date: Wed, 02 Dec 2026 23:56:58 +0000	[thread overview]
Message-ID: <20261202235641.3242127-3-cochcoder@proton.me> (raw)
In-Reply-To: <20261202235641.3242127-1-cochcoder@proton.me>

Allow connected Central LE sockets to request advisory connection
event length hints without replacing the live interval, latency or timeout.
Keep the request on the shared connection and preserve it in later updates.

Expose queued or executing work as -EINPROGRESS in an optional extended
getter. Report the final error without clearing a newer pending request.
Four-byte reads return the requested hint prefix. HCI completion confirms
the procedure outcome, not reserved airtime.

AI assistance: Codex (GPT-6).

Signed-off-by: Kyle Pepper <cochcoder@proton.me>
---
Base: bluetooth-next 7a1a7a5baa1e0f84d0568fc999421ee7dcbccaf5.
This is a first public kernel CE RFC; local candidate numbers are not
previous public versions. No kernel CE Message-ID is verified.

BlueZ companion, already archived:
https://lore.kernel.org/linux-bluetooth/20261202160403.226082-1-cochcoder@proton.me/

Offline validation: four W=1 objects; GCC/Clang source-extracted LE dispatch
and CE queue tests; getter lengths 0..16 and baseline/negative controls.
The exact source has no hardware, PTS or running-kernel lockdep result.
Older integrated N8/MT7922 trials do not establish general controller support.

diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -358,6 +358,19 @@
 
 #define BT_SCM_PKT_SEQNUM	0x05
 
+#define BT_LE_CONN_EVENT_LEN	23
+/* Connection-wide advisory hints in 0.625 ms units; zero is unspecified. */
+struct bt_le_conn_event_len {
+	__u16 min;
+	__u16 max;
+};
+
+struct bt_le_conn_event_len_state {
+	struct bt_le_conn_event_len request;
+	/* Zero, -EINPROGRESS, or the last completed request's negative errno. */
+	__s32 status;
+};
+
 __printf(1, 2)
 void bt_info(const char *fmt, ...);
 __printf(1, 2)
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -786,6 +786,10 @@
 	__u16		le_conn_interval;
 	__u16		le_conn_latency;
 	__u16		le_supv_timeout;
+	__u16		le_min_ce_len;
+	__u16		le_max_ce_len;
+	unsigned int	le_ce_pending;
+	int		le_ce_status;
 	__u16		le_rate_interval;
 	__u16		le_subrate;
 	__u16		le_rate_latency;
@@ -2493,6 +2497,7 @@
 
 u32 hci_conn_get_phy(struct hci_conn *conn);
 int hci_conn_set_phy(struct hci_conn *conn, u32 phys);
+int hci_conn_set_le_event_len(struct hci_conn *conn, u16 min, u16 max);
 
 /* ----- HCI Sockets ----- */
 void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb);
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -536,8 +536,8 @@
 	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		= cpu_to_le16(conn->le_min_ce_len);
+	cp.max_ce_len		= cpu_to_le16(conn->le_max_ce_len);
 	timeout			= conn->conn_timeout;
 
 	hci_dev_unlock(hdev);
@@ -602,6 +602,96 @@
 		hci_conn_put(conn);
 		kfree(d);
 	}
+}
+
+static int le_conn_event_len_sync(struct hci_dev *hdev, void *data)
+{
+	struct hci_conn *conn = data;
+	struct hci_cp_le_conn_update cp = {};
+	u16 timeout;
+
+	hci_dev_lock(hdev);
+
+	if (!hci_conn_valid(hdev, conn) || conn->state != BT_CONNECTED) {
+		hci_dev_unlock(hdev);
+		return -ENOTCONN;
+	}
+
+	/* Change only the local hints, preserving the live link parameters. */
+	cp.handle = cpu_to_le16(conn->handle);
+	cp.conn_interval_min = cpu_to_le16(conn->le_conn_interval);
+	cp.conn_interval_max = cpu_to_le16(conn->le_conn_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(conn->le_min_ce_len);
+	cp.max_ce_len = cpu_to_le16(conn->le_max_ce_len);
+	timeout = conn->conn_timeout;
+
+	hci_dev_unlock(hdev);
+
+	return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE,
+				       sizeof(cp), &cp,
+				       HCI_EV_LE_CONN_UPDATE_COMPLETE,
+				       timeout, NULL);
+}
+
+static void le_conn_event_len_complete(struct hci_dev *hdev, void *data,
+				       int err)
+{
+	struct hci_conn *conn = data;
+
+	hci_dev_lock(hdev);
+	/* An older completion must not mark a newer queued request ready. */
+	if (!--conn->le_ce_pending)
+		conn->le_ce_status = err > 0 ? -bt_to_errno(err) : err;
+	hci_dev_unlock(hdev);
+
+	if (err)
+		bt_dev_dbg(hdev, "LE connection event length request failed (%d)",
+			   err);
+
+	hci_conn_put(conn);
+}
+
+/* Device must be locked. The values describe the local request, not airtime. */
+int hci_conn_set_le_event_len(struct hci_conn *conn, u16 min, u16 max)
+{
+	struct hci_dev *hdev = conn->hdev;
+	u16 old_min, old_max;
+	int err;
+
+	if (conn->type != LE_LINK || conn->role != HCI_ROLE_MASTER)
+		return -EOPNOTSUPP;
+
+	if (!hci_conn_valid(hdev, conn) || conn->state != BT_CONNECTED)
+		return -ENOTCONN;
+
+	if (min > max)
+		return -EINVAL;
+
+	old_min = conn->le_min_ce_len;
+	old_max = conn->le_max_ce_len;
+	conn->le_min_ce_len = min;
+	conn->le_max_ce_len = max;
+
+	/* Queued work reads the latest request under the device lock. */
+	if (hci_cmd_sync_lookup_entry(hdev, le_conn_event_len_sync, conn,
+				      le_conn_event_len_complete))
+		return 0;
+
+	err = hci_cmd_sync_queue(hdev, le_conn_event_len_sync,
+				 hci_conn_get(conn),
+				 le_conn_event_len_complete);
+	if (err) {
+		conn->le_min_ce_len = old_min;
+		conn->le_max_ce_len = old_max;
+		hci_conn_put(conn);
+	} else {
+		conn->le_ce_pending++;
+		conn->le_ce_status = -EINPROGRESS;
+	}
+
+	return err;
 }
 
 /* Device _must_ be locked */
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -7640,8 +7640,8 @@
 	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 = cpu_to_le16(hcon->le_min_ce_len);
+	cp.max_ce_len = cpu_to_le16(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
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -8188,14 +8188,23 @@
 {
 	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		= cpu_to_le16(conn->le_min_ce_len);
+	cp.max_ce_len		= cpu_to_le16(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/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -591,6 +591,50 @@
 	return err;
 }
 
+static int l2cap_sock_conn_event_len(struct sock *sk,
+				     struct bt_le_conn_event_len *len, int *status, bool set)
+{
+	struct l2cap_chan *chan = l2cap_pi(sk)->chan;
+	struct l2cap_conn *conn;
+	bool have_conn;
+	int err = 0;
+
+	l2cap_chan_lock(chan);
+	conn = l2cap_chan_conn(chan);
+	if (!conn) {
+		l2cap_chan_unlock(chan);
+		return -ENOTCONN;
+	}
+	l2cap_conn_get(conn);
+	l2cap_chan_unlock(chan);
+
+	/* Match the device -> connection -> channel -> socket lock order. */
+	hci_dev_lock(conn->hcon->hdev);
+	have_conn = l2cap_chan_lock_conn(chan);
+	lock_sock(sk);
+
+	if (l2cap_chan_conn(chan) != conn || sk->sk_state != BT_CONNECTED ||
+	    !hci_conn_valid(conn->hcon->hdev, conn->hcon) ||
+	    conn->hcon->state != BT_CONNECTED) {
+		err = -ENOTCONN;
+	} else if (conn->hcon->type != LE_LINK) {
+		err = -EOPNOTSUPP;
+	} else if (set) {
+		err = hci_conn_set_le_event_len(conn->hcon, len->min, len->max);
+	} else {
+		len->min = conn->hcon->le_min_ce_len;
+		len->max = conn->hcon->le_max_ce_len;
+		*status = conn->hcon->le_ce_status;
+	}
+
+	release_sock(sk);
+	l2cap_chan_unlock_conn(chan, have_conn);
+	hci_dev_unlock(conn->hcon->hdev);
+	l2cap_conn_put(conn);
+
+	return err;
+}
+
 static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
 				 sockopt_t *sopt)
 {
@@ -613,6 +657,21 @@
 		return -ENOPROTOOPT;
 
 	len = sopt->optlen;
+	if (optname == BT_LE_CONN_EVENT_LEN) {
+		struct bt_le_conn_event_len_state event_len;
+
+		err = l2cap_sock_conn_event_len(sk, &event_len.request,
+						&event_len.status, false);
+		if (err)
+			return err;
+
+		len = min_t(size_t, len, sizeof(event_len));
+		if (copy_to_iter(&event_len, len, &sopt->iter_out) != len)
+			return -EFAULT;
+
+		sopt->optlen = len;
+		return 0;
+	}
 
 	l2cap_chan_lock(chan);
 	lock_sock(sk);
@@ -958,6 +1017,20 @@
 
 	if (level != SOL_BLUETOOTH)
 		return -ENOPROTOOPT;
+
+	if (optname == BT_LE_CONN_EVENT_LEN) {
+		struct bt_le_conn_event_len event_len;
+
+		if (optlen != sizeof(event_len))
+			return -EINVAL;
+
+		err = copy_safe_from_sockptr(&event_len, sizeof(event_len),
+					     optval, optlen);
+		if (err)
+			return err;
+
+		return l2cap_sock_conn_event_len(sk, &event_len, NULL, true);
+	}
 
 	l2cap_chan_lock(chan);
 	lock_sock(sk);


      parent reply	other threads:[~2026-12-02 23:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 23:56 [RFC PATCH 0/2] Bluetooth: LE connection event length requests Kyle Pepper
2026-12-02 23:56 ` [RFC PATCH 1/2] Bluetooth: Match LE connection update completions Kyle Pepper
2026-12-02 23:56 ` 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=20261202235641.3242127-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