From: Kyle Pepper <cochcoder@proton.me>
To: linux-bluetooth@vger.kernel.org
Cc: marcel@holtmann.org, luiz.dentz@gmail.com
Subject: [RFC PATCH v2 2/2] Bluetooth: Add LE connection event length requests
Date: Thu, 03 Dec 2026 01:55:21 +0000 [thread overview]
Message-ID: <20261203015454.3566161-3-cochcoder@proton.me> (raw)
In-Reply-To: <20261203015454.3566161-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.
BlueZ companion:
https://lore.kernel.org/linux-bluetooth/20261202160403.226082-1-cochcoder@proton.me/
Changes in v2:
- Skip locking and completion state on synchronous cancellation.
- Take the reference and pending count before queueing; undo both on failure.
include/net/bluetooth/bluetooth.h | 13 +++++
include/net/bluetooth/hci_core.h | 5 ++
net/bluetooth/hci_conn.c | 106 +++++++++++++++++++++++++++++++++++++-
net/bluetooth/hci_event.c | 4 +-
net/bluetooth/hci_sync.c | 13 ++++-
net/bluetooth/l2cap_sock.c | 73 ++++++++++++++++++++++++++
6 files changed, 208 insertions(+), 6 deletions(-)
diff --git a/include/net/bluetooth/bluetooth.h b/include/net/bluetooth/bluetooth.h
index 777bc1a03..720d30d63 100644
--- a/include/net/bluetooth/bluetooth.h
+++ b/include/net/bluetooth/bluetooth.h
@@ -358,6 +358,19 @@ struct bt_codecs {
#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
index 4aa0b49ae..88c805bd9 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -786,6 +786,10 @@ struct hci_conn {
__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 @@ void *hci_recv_event_data(struct hci_dev *hdev, __u8 event);
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
index 5b7526a85..69901d23f 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 = 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);
@@ -604,6 +604,108 @@ void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency,
}
}
+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;
+
+ /* A cancelled entry is completed synchronously from
+ * hci_cmd_sync_dequeue(), which hci_conn_del() calls with hdev->lock
+ * already held, so taking it here would self-deadlock. The connection
+ * is being torn down and nobody can still wait for this request, so
+ * only drop the reference taken when the request was queued.
+ */
+ if (err == -ECANCELED)
+ goto done;
+
+ 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);
+
+done:
+ 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;
+
+ hci_conn_get(conn);
+ conn->le_ce_pending++;
+ err = hci_cmd_sync_queue(hdev, le_conn_event_len_sync,
+ conn,
+ le_conn_event_len_complete);
+ if (err) {
+ conn->le_min_ce_len = old_min;
+ conn->le_max_ce_len = old_max;
+ conn->le_ce_pending--;
+ hci_conn_put(conn);
+ } else {
+ conn->le_ce_status = -EINPROGRESS;
+ }
+
+ return err;
+}
+
/* Device _must_ be locked */
void hci_sco_setup(struct hci_conn *conn, __u8 status)
{
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index ff5238e8e..afd7e3141 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 = 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
index ee426ffea..f1d0ed7c3 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -8188,14 +8188,23 @@ 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 = 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
index 4e7787ec0..2cbbae604 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -591,6 +591,50 @@ static int l2cap_sock_getsockopt_old(struct socket *sock, int optname,
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 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
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);
@@ -959,6 +1018,20 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
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);
prev parent reply other threads:[~2026-12-03 1:55 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 1:55 [RFC PATCH v2 0/2] Bluetooth: LE connection event length requests Kyle Pepper
2026-12-03 1:55 ` [RFC PATCH v2 1/2] Bluetooth: Match LE connection update completions Kyle Pepper
2026-12-03 1:55 ` 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=20261203015454.3566161-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