From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796262934; b=b1uB6d8lakF7sgYwk20vlMOpDVY1QwOeY4k+H5Ubc4nyYRwVkCx1biuN6CROhUWggXj5 MNAw9vbZKLgs8ER4vxVBZDTg9fa1O25XUPtgCXkhl4YwhJKsOOPrsAimVlbKAozFtH/eI nG4JEehYBykJ0tu+5eVW3HkeeOum6P5DRBD9oInT9JI2c1Qj2sZwxI+CV3ckeibeZ5gt2 T1fpMdTop3EN+FxCvr+xi1x4G8z3QXPIaoNQ7Hfek3ByT3VB4BbPuSudfELWNLxQDP8Wa 5UmbiSMaG6pMDO53A5ZAjXKe4MJSgjmRB9lzPLBmJ2vsWpkN5yISOJS92PLaUezIIrA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796262934; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=6Ax6rZZIeqJaiIJjHY/iPWCrDdd6O6OsRrGk9dNquFY=; b=K0bGHTrrEFJD2zQDGczPx1qs6gJaeW/M38YS+vB+HPLOVOLCY3yLAFlYSqEVruGSOahF Cej/tpR3IFUO9eSwBThwPD/cFsk64E87rB6UrFAzquNkIEdk5DQIG3w/AlOYiG85IzmZ1 ZJptAhPVtbpwwk2uKCCCNidWN8PlmC26D+7yLf6ns8Axcb8woWHNhXKFaRM4ttqQ0qITR 7vmW/pBDET0QSW9i1Lq6Cy1cky1EXN4edor3LstqLnXsS+VQKDz+xrQjGrwxvdE2beCQF +BPhmAxwoN+e23MIA7L30oYyKLCyiQTu360gc1tPxks1HwMQw21n75f6Sam2oGvUNGQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=kolqh242inc5bavrzhtp6c2r4y.protonmail2 header.b=kS+dti3e; arc=none smtp.remote-ip=109.224.244.18 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D72EB606A69C for ; Thu, 3 Dec 2026 01:55:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D72EB606A69C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=proton.me header.i=@proton.me header.a=rsa-sha256 header.s=kolqh242inc5bavrzhtp6c2r4y.protonmail2 header.b=kS+dti3e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=kolqh242inc5bavrzhtp6c2r4y.protonmail2; t=1796262926; x=1796522126; bh=6Ax6rZZIeqJaiIJjHY/iPWCrDdd6O6OsRrGk9dNquFY=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=kS+dti3e4pU5tNYM2tFJMQMK9D3Kcqcannf2xA2dRdiScHtQT4/PhPHTP6k1hyFtF ut32KjCC9/int1QxDzntZmenX2n1SLn2SR0DcEo0qIqGzWLZ63v0kUEMFhz5jusFPB RUpzq/uKMzewJYfd5dzunSatvoNBZ1wPfqrHvljyVZTJDDxkmSBbCsQzN2rVyExMlf GG3splzXUOkV3ztnXVkAfPTmtrtvYbMTSNDLg3UtN+aYQp7aaLEbk2yqGODZSf5Xsp HMpCuTckZa+BvEv2ck8NeWV9Eo/vN19JogEsxZMn0/1plVjLnpudijqsDhNpYNVbfa Uqu+j+4ZLVU6A== Date: Thu, 03 Dec 2026 01:55:21 +0000 To: linux-bluetooth@vger.kernel.org From: Kyle Pepper Cc: marcel@holtmann.org, luiz.dentz@gmail.com Subject: [RFC PATCH v2 2/2] Bluetooth: Add LE connection event length requests Message-ID: <20261203015454.3566161-3-cochcoder@proton.me> In-Reply-To: <20261203015454.3566161-1-cochcoder@proton.me> References: <20261203015454.3566161-1-cochcoder@proton.me> Feedback-ID: 97716137:user:proton X-Pm-Message-ID: 42d53617494adbe83f21b7909621cd898ce2ad64 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 --- Base: bluetooth-next 7a1a7a5baa1e0f84d0568fc999421ee7dcbccaf5. BlueZ companion: https://lore.kernel.org/linux-bluetooth/20261202160403.226082-1-cochcoder@p= roton.me/ Changes in v2: - Skip locking and completion state on synchronous cancellation. - Take the reference and pending count before queueing; undo both on failur= e. 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/blue= tooth.h index 777bc1a03..720d30d63 100644 --- a/include/net/bluetooth/bluetooth.h +++ b/include/net/bluetooth/bluetooth.h @@ -358,6 +358,19 @@ struct bt_codecs { =20 #define BT_SCM_PKT_SEQNUM=090x05 =20 +#define BT_LE_CONN_EVENT_LEN=0923 +/* Connection-wide advisory hints in 0.625 ms units; zero is unspecified. = */ +struct bt_le_conn_event_len { +=09__u16 min; +=09__u16 max; +}; + +struct bt_le_conn_event_len_state { +=09struct bt_le_conn_event_len request; +=09/* Zero, -EINPROGRESS, or the last completed request's negative errno. = */ +=09__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_c= ore.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 { =09__u16=09=09le_conn_interval; =09__u16=09=09le_conn_latency; =09__u16=09=09le_supv_timeout; +=09__u16=09=09le_min_ce_len; +=09__u16=09=09le_max_ce_len; +=09unsigned int=09le_ce_pending; +=09int=09=09le_ce_status; =09__u16=09=09le_rate_interval; =09__u16=09=09le_subrate; =09__u16=09=09le_rate_latency; @@ -2493,6 +2497,7 @@ void *hci_recv_event_data(struct hci_dev *hdev, __u8 = event); =20 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); =20 /* ----- 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, vo= id *data) =09cp.conn_interval_max=09=3D cpu_to_le16(d->max); =09cp.conn_latency=09=09=3D cpu_to_le16(d->latency); =09cp.supervision_timeout=09=3D cpu_to_le16(d->to_multiplier); -=09cp.min_ce_len=09=09=3D cpu_to_le16(0x0000); -=09cp.max_ce_len=09=09=3D cpu_to_le16(0x0000); +=09cp.min_ce_len=09=09=3D cpu_to_le16(conn->le_min_ce_len); +=09cp.max_ce_len=09=09=3D cpu_to_le16(conn->le_max_ce_len); =09timeout=09=09=09=3D conn->conn_timeout; =20 =09hci_dev_unlock(hdev); @@ -604,6 +604,108 @@ void hci_le_conn_update(struct hci_conn *conn, u16 mi= n, u16 max, u16 latency, =09} } =20 +static int le_conn_event_len_sync(struct hci_dev *hdev, void *data) +{ +=09struct hci_conn *conn =3D data; +=09struct hci_cp_le_conn_update cp =3D {}; +=09u16 timeout; + +=09hci_dev_lock(hdev); + +=09if (!hci_conn_valid(hdev, conn) || conn->state !=3D BT_CONNECTED) { +=09=09hci_dev_unlock(hdev); +=09=09return -ENOTCONN; +=09} + +=09/* Change only the local hints, preserving the live link parameters. */ +=09cp.handle =3D cpu_to_le16(conn->handle); +=09cp.conn_interval_min =3D cpu_to_le16(conn->le_conn_interval); +=09cp.conn_interval_max =3D cpu_to_le16(conn->le_conn_interval); +=09cp.conn_latency =3D cpu_to_le16(conn->le_conn_latency); +=09cp.supervision_timeout =3D cpu_to_le16(conn->le_supv_timeout); +=09cp.min_ce_len =3D cpu_to_le16(conn->le_min_ce_len); +=09cp.max_ce_len =3D cpu_to_le16(conn->le_max_ce_len); +=09timeout =3D conn->conn_timeout; + +=09hci_dev_unlock(hdev); + +=09return __hci_cmd_sync_status_sk(hdev, HCI_OP_LE_CONN_UPDATE, +=09=09=09=09 sizeof(cp), &cp, +=09=09=09=09 HCI_EV_LE_CONN_UPDATE_COMPLETE, +=09=09=09=09 timeout, NULL); +} + +static void le_conn_event_len_complete(struct hci_dev *hdev, void *data, +=09=09=09=09 int err) +{ +=09struct hci_conn *conn =3D data; + +=09/* A cancelled entry is completed synchronously from +=09 * hci_cmd_sync_dequeue(), which hci_conn_del() calls with hdev->lock +=09 * already held, so taking it here would self-deadlock. The connection +=09 * is being torn down and nobody can still wait for this request, so +=09 * only drop the reference taken when the request was queued. +=09 */ +=09if (err =3D=3D -ECANCELED) +=09=09goto done; + +=09hci_dev_lock(hdev); +=09/* An older completion must not mark a newer queued request ready. */ +=09if (!--conn->le_ce_pending) +=09=09conn->le_ce_status =3D err > 0 ? -bt_to_errno(err) : err; +=09hci_dev_unlock(hdev); + +=09if (err) +=09=09bt_dev_dbg(hdev, "LE connection event length request failed (%d)", +=09=09=09 err); + +done: +=09hci_conn_put(conn); +} + +/* Device must be locked. The values describe the local request, not airti= me. */ +int hci_conn_set_le_event_len(struct hci_conn *conn, u16 min, u16 max) +{ +=09struct hci_dev *hdev =3D conn->hdev; +=09u16 old_min, old_max; +=09int err; + +=09if (conn->type !=3D LE_LINK || conn->role !=3D HCI_ROLE_MASTER) +=09=09return -EOPNOTSUPP; + +=09if (!hci_conn_valid(hdev, conn) || conn->state !=3D BT_CONNECTED) +=09=09return -ENOTCONN; + +=09if (min > max) +=09=09return -EINVAL; + +=09old_min =3D conn->le_min_ce_len; +=09old_max =3D conn->le_max_ce_len; +=09conn->le_min_ce_len =3D min; +=09conn->le_max_ce_len =3D max; + +=09/* Queued work reads the latest request under the device lock. */ +=09if (hci_cmd_sync_lookup_entry(hdev, le_conn_event_len_sync, conn, +=09=09=09=09 le_conn_event_len_complete)) +=09=09return 0; + +=09hci_conn_get(conn); +=09conn->le_ce_pending++; +=09err =3D hci_cmd_sync_queue(hdev, le_conn_event_len_sync, +=09=09=09=09 conn, +=09=09=09=09 le_conn_event_len_complete); +=09if (err) { +=09=09conn->le_min_ce_len =3D old_min; +=09=09conn->le_max_ce_len =3D old_max; +=09=09conn->le_ce_pending--; +=09=09hci_conn_put(conn); +=09} else { +=09=09conn->le_ce_status =3D -EINPROGRESS; +=09} + +=09return 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 h= ci_dev *hdev, void *data, =09cp.interval_max =3D ev->interval_max; =09cp.latency =3D ev->latency; =09cp.timeout =3D ev->timeout; -=09cp.min_ce_len =3D 0; -=09cp.max_ce_len =3D 0; +=09cp.min_ce_len =3D cpu_to_le16(hcon->le_min_ce_len); +=09cp.max_ce_len =3D cpu_to_le16(hcon->le_max_ce_len); =20 =09hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp); =20 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, s= truct hci_conn *conn, { =09struct hci_cp_le_conn_update cp; =20 +=09hci_dev_lock(hdev); + +=09if (!hci_conn_valid(hdev, conn) || conn->state !=3D BT_CONNECTED) { +=09=09hci_dev_unlock(hdev); +=09=09return -ENOTCONN; +=09} + =09memset(&cp, 0, sizeof(cp)); =09cp.handle=09=09=3D cpu_to_le16(conn->handle); =09cp.conn_interval_min=09=3D cpu_to_le16(params->conn_min_interval); =09cp.conn_interval_max=09=3D cpu_to_le16(params->conn_max_interval); =09cp.conn_latency=09=09=3D cpu_to_le16(params->conn_latency); =09cp.supervision_timeout=09=3D cpu_to_le16(params->supervision_timeout); -=09cp.min_ce_len=09=09=3D cpu_to_le16(0x0000); -=09cp.max_ce_len=09=09=3D cpu_to_le16(0x0000); +=09cp.min_ce_len=09=09=3D cpu_to_le16(conn->le_min_ce_len); +=09cp.max_ce_len=09=09=3D cpu_to_le16(conn->le_max_ce_len); + +=09hci_dev_unlock(hdev); =20 =09return __hci_cmd_sync_status(hdev, HCI_OP_LE_CONN_UPDATE, =09=09=09=09 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 *so= ck, int optname, =09return err; } =20 +static int l2cap_sock_conn_event_len(struct sock *sk, +=09=09=09=09 struct bt_le_conn_event_len *len, int *status, bool set) +{ +=09struct l2cap_chan *chan =3D l2cap_pi(sk)->chan; +=09struct l2cap_conn *conn; +=09bool have_conn; +=09int err =3D 0; + +=09l2cap_chan_lock(chan); +=09conn =3D l2cap_chan_conn(chan); +=09if (!conn) { +=09=09l2cap_chan_unlock(chan); +=09=09return -ENOTCONN; +=09} +=09l2cap_conn_get(conn); +=09l2cap_chan_unlock(chan); + +=09/* Match the device -> connection -> channel -> socket lock order. */ +=09hci_dev_lock(conn->hcon->hdev); +=09have_conn =3D l2cap_chan_lock_conn(chan); +=09lock_sock(sk); + +=09if (l2cap_chan_conn(chan) !=3D conn || sk->sk_state !=3D BT_CONNECTED |= | +=09 !hci_conn_valid(conn->hcon->hdev, conn->hcon) || +=09 conn->hcon->state !=3D BT_CONNECTED) { +=09=09err =3D -ENOTCONN; +=09} else if (conn->hcon->type !=3D LE_LINK) { +=09=09err =3D -EOPNOTSUPP; +=09} else if (set) { +=09=09err =3D hci_conn_set_le_event_len(conn->hcon, len->min, len->max); +=09} else { +=09=09len->min =3D conn->hcon->le_min_ce_len; +=09=09len->max =3D conn->hcon->le_max_ce_len; +=09=09*status =3D conn->hcon->le_ce_status; +=09} + +=09release_sock(sk); +=09l2cap_chan_unlock_conn(chan, have_conn); +=09hci_dev_unlock(conn->hcon->hdev); +=09l2cap_conn_put(conn); + +=09return err; +} + static int l2cap_sock_getsockopt(struct socket *sock, int level, int optna= me, =09=09=09=09 sockopt_t *sopt) { @@ -613,6 +657,21 @@ static int l2cap_sock_getsockopt(struct socket *sock, = int level, int optname, =09=09return -ENOPROTOOPT; =20 =09len =3D sopt->optlen; +=09if (optname =3D=3D BT_LE_CONN_EVENT_LEN) { +=09=09struct bt_le_conn_event_len_state event_len; + +=09=09err =3D l2cap_sock_conn_event_len(sk, &event_len.request, +=09=09=09=09=09=09&event_len.status, false); +=09=09if (err) +=09=09=09return err; + +=09=09len =3D min_t(size_t, len, sizeof(event_len)); +=09=09if (copy_to_iter(&event_len, len, &sopt->iter_out) !=3D len) +=09=09=09return -EFAULT; + +=09=09sopt->optlen =3D len; +=09=09return 0; +=09} =20 =09l2cap_chan_lock(chan); =09lock_sock(sk); @@ -959,6 +1018,20 @@ static int l2cap_sock_setsockopt(struct socket *sock,= int level, int optname, =09if (level !=3D SOL_BLUETOOTH) =09=09return -ENOPROTOOPT; =20 +=09if (optname =3D=3D BT_LE_CONN_EVENT_LEN) { +=09=09struct bt_le_conn_event_len event_len; + +=09=09if (optlen !=3D sizeof(event_len)) +=09=09=09return -EINVAL; + +=09=09err =3D copy_safe_from_sockptr(&event_len, sizeof(event_len), +=09=09=09=09=09 optval, optlen); +=09=09if (err) +=09=09=09return err; + +=09=09return l2cap_sock_conn_event_len(sk, &event_len, NULL, true); +=09} + =09l2cap_chan_lock(chan); =09lock_sock(sk); =20