From 44f74e07775874f2baeda4b7c7443d903dbcf68d Mon Sep 17 00:00:00 2001
From: Ben Hutchings <benh@debian.org>
Date: Fri, 27 Mar 2026 12:35:01 +0100
Subject: [PATCH 5/8] Bluetooth: hci_event: Introduce read_key_size_capable()
 macro

This is based on upstream commit a9a830a676a9 ("Bluetooth: hci_event:
Fix sending HCI_OP_READ_ENC_KEY_SIZE").  The
HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE quirk is not implemented here so
this does not fix any bug, but it will ease backporting a bug fix.

Stable-dep-of: c82b6357a546 ("Bluetooth: hci_event: Fix not using key encryption size when its known")
Signed-off-by: Ben Hutchings <benh@debian.org>
---
 include/net/bluetooth/hci_core.h | 3 +++
 net/bluetooth/hci_event.c        | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 2775752a4e4f..83a209d651e2 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -1723,6 +1723,9 @@ void hci_conn_del_sysfs(struct hci_conn *conn);
 	(((dev)->commands[29] & 0x08) && \
 	 !test_bit(HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN, &(dev)->quirks))
 
+#define read_key_size_capable(dev) \
+	((dev)->commands[20] & 0x10)
+
 /* Use ext scanning if set ext scan param and ext scan enable is supported */
 #define use_ext_scan(dev) (((dev)->commands[37] & 0x20) && \
 			   ((dev)->commands[37] & 0x40) && \
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 1f05204ae1fe..2ebd915240fb 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -3230,7 +3230,7 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
 			if (key) {
 				set_bit(HCI_CONN_ENCRYPT, &conn->flags);
 
-				if (!(hdev->commands[20] & 0x10)) {
+				if (!read_key_size_capable(hdev)) {
 					conn->enc_key_size = HCI_LINK_KEY_SIZE;
 				} else {
 					cp.handle = cpu_to_le16(conn->handle);
@@ -3687,7 +3687,7 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, void *data,
 		 * controller really supports it. If it doesn't, assume
 		 * the default size (16).
 		 */
-		if (!(hdev->commands[20] & 0x10)) {
+		if (!read_key_size_capable(hdev)) {
 			conn->enc_key_size = HCI_LINK_KEY_SIZE;
 			goto notify;
 		}
