From 21bf1ba01f000e5869b7f749166090cf55663151 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Sun, 29 Nov 2026 17:39:37 +1000 Subject: [PATCH 1/3] Bluetooth: hci_bcm4377: fix BCM4388 Read Local Supported Codecs v2 layout BCM4388 firmware encodes HCI_OP_READ_LOCAL_CODECS_V2 (0x100d) return parameters as separate ID and transport arrays rather than the Bluetooth Core Spec interleaved (id, transport) pairs and (cid, vid, transport) tuples. On a MacBook Pro M2 Max (J416), BCM4388 returns: 00 07 00 01 02 03 04 05 06 02 02 02 02 02 02 04 01 75 00 10 00 02 hci_read_supported_codecs_v2() parses those bytes as seven interleaved pairs, generating bogus codec/transport probes (10 failing Read Local Codec Capabilities commands) and missing LC3 (id 0x06, LE CIS transport 0x04), whose capabilities BCM4388 does return when queried properly. Add a broken_codecs_v2_layout quirk for BCM4388 and re-interleave successful HCI_OP_READ_LOCAL_CODECS_V2 command-complete replies in place before passing them to hci_recv_frame(). Signed-off-by: Marcelo Alcantara --- drivers/bluetooth/hci_bcm4377.c | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/drivers/bluetooth/hci_bcm4377.c b/drivers/bluetooth/hci_bcm4377.c index 3b9d29ad3670b..2b6231ed6e347 100644 --- a/drivers/bluetooth/hci_bcm4377.c +++ b/drivers/bluetooth/hci_bcm4377.c @@ -517,6 +517,9 @@ struct bcm4377_data; * reserved bits of Primary/Secondary_PHY inside * LE Extended Advertising Report events which * have to be ignored + * broken_codecs_v2_layout: Set to true if HCI_OP_READ_LOCAL_CODECS_V2 replies + * send separate ID and transport arrays instead of + * interleaved (id, transport) records * send_calibration: Optional callback to send calibration data * send_ptb: Callback to send "PTB" regulatory/calibration data */ @@ -538,6 +541,7 @@ struct bcm4377_hw { unsigned long broken_mws_transport_config : 1; unsigned long broken_le_coded : 1; unsigned long broken_le_ext_adv_report_phy : 1; + unsigned long broken_codecs_v2_layout : 1; int (*send_calibration)(struct bcm4377_data *bcm4377); int (*send_ptb)(struct bcm4377_data *bcm4377, @@ -659,6 +663,66 @@ struct bcm4377_data { struct bcm4377_transfer_ring acl_d2h_ring; }; +/* + * BCM4388 firmware encodes HCI_OP_READ_LOCAL_CODECS_V2 return parameters as + * separate ID and transport arrays instead of the Bluetooth Core Spec + * interleaved (id, transport) pairs and (cid, vid, transport) tuples. + * Re-interleave the reply in place before handing it to hci_recv_frame(). + */ +static void bcm4377_fixup_codecs_v2(struct sk_buff *skb) +{ + const struct hci_event_hdr *hdr; + const struct hci_ev_cmd_complete *cc; + u8 *rp, *std_ids, *std_trs, *vnd_ids, *vnd_trs; + u8 num_std, num_vnd; + size_t rp_len, std_len, vnd_len, i; + u8 tmp[U8_MAX]; + + if (skb->len < sizeof(*hdr) + sizeof(*cc) + 3) + return; + + hdr = hci_event_hdr(skb); + if (hdr->evt != HCI_EV_CMD_COMPLETE) + return; + if (hdr->plen < sizeof(*cc) + 3 || skb->len < sizeof(*hdr) + hdr->plen) + return; + + cc = (const struct hci_ev_cmd_complete *)(skb->data + sizeof(*hdr)); + if (le16_to_cpu(cc->opcode) != HCI_OP_READ_LOCAL_CODECS_V2) + return; + + rp = skb->data + sizeof(*hdr) + sizeof(*cc); + rp_len = hdr->plen - sizeof(*cc); + if (rp[0] != 0) + return; + + num_std = rp[1]; + std_len = (size_t)num_std * sizeof(struct hci_std_codec_v2); + if (rp_len < 3 + std_len) + return; + + num_vnd = rp[2 + std_len]; + vnd_len = (size_t)num_vnd * sizeof(struct hci_vnd_codec_v2); + if (rp_len < 3 + std_len + vnd_len) + return; + + std_ids = rp + 2; + std_trs = std_ids + num_std; + for (i = 0; i < num_std; i++) { + tmp[2 * i] = std_ids[i]; + tmp[2 * i + 1] = std_trs[i]; + } + memcpy(std_ids, tmp, std_len); + + vnd_ids = rp + 3 + std_len; + vnd_trs = vnd_ids + (size_t)num_vnd * 4; + for (i = 0; i < num_vnd; i++) { + memcpy(tmp + 5 * i, vnd_ids + 4 * i, 4); + tmp[5 * i + 4] = vnd_trs[i]; + } + memcpy(vnd_ids, tmp, vnd_len); +} + static void bcm4377_ring_doorbell(struct bcm4377_data *bcm4377, u8 doorbell, u16 val) { @@ -757,6 +821,9 @@ static void bcm4377_handle_event(struct bcm4377_data *bcm4377, memcpy(skb_put(skb, len), payload, len); hci_skb_pkt_type(skb) = type; + if (type == HCI_EVENT_PKT && bcm4377->hw && + bcm4377->hw->broken_codecs_v2_layout) + bcm4377_fixup_codecs_v2(skb); hci_recv_frame(bcm4377->hdev, skb); out: @@ -3410,6 +3477,7 @@ static const struct bcm4377_hw bcm4377_hw_variants[] = { .broken_mws_transport_config = true, .broken_le_coded = true, .broken_le_ext_adv_report_phy = true, + .broken_codecs_v2_layout = true, .send_calibration = bcm4388_send_calibration, .send_ptb = bcm4378_send_ptb, }, From aa3002fe6e90dd132bf73800c2254e18d1b28b30 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Sun, 29 Nov 2026 17:39:37 +1000 Subject: [PATCH 2/3] Bluetooth: hci_codec: handle -EINVAL from Read Local Codec Capabilities Some controllers (including Apple/Broadcom BCM4388) advertise HCI_OP_READ_LOCAL_CODEC_CAPS (0x100e) and return capabilities for LE ISO codecs such as LC3, while rejecting Read Local Codec Capabilities with HCI_ERROR_INVALID_PARAMETERS (-EINVAL) for BR/EDR SCO codecs that have no capabilities rather than returning num_caps = 0. When __hci_cmd_sync_sk(HCI_OP_READ_LOCAL_CODEC_CAPS) returns -EINVAL, log at debug level and still register the codec in hdev->local_codecs without capabilities, matching the existing fallback when the command is unsupported. Preserve bt_dev_err() for transport and timeout errors. Signed-off-by: Marcelo Alcantara --- net/bluetooth/hci_codec.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/net/bluetooth/hci_codec.c b/net/bluetooth/hci_codec.c index 7a7e813dcdda5..e55900f0f5ad9 100644 --- a/net/bluetooth/hci_codec.c +++ b/net/bluetooth/hci_codec.c @@ -75,8 +75,18 @@ static void hci_read_codec_capabilities(struct hci_dev *hdev, __u8 transport, skb = __hci_cmd_sync_sk(hdev, HCI_OP_READ_LOCAL_CODEC_CAPS, sizeof(*cmd), cmd, 0, HCI_CMD_TIMEOUT, NULL); if (IS_ERR(skb)) { - bt_dev_err(hdev, "Failed to read codec capabilities (%ld)", - PTR_ERR(skb)); + if (PTR_ERR(skb) == -EINVAL) { + bt_dev_dbg(hdev, + "No codec capabilities for codec %u transport %u", + cmd->id, cmd->transport); + hci_dev_lock(hdev); + hci_codec_list_add(&hdev->local_codecs, cmd, + NULL, NULL, 0); + hci_dev_unlock(hdev); + } else { + bt_dev_err(hdev, "Failed to read codec capabilities (%ld)", + PTR_ERR(skb)); + } continue; } From 40936f1338528a5a33f4410fbeec4d5c2cb92979 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Sun, 29 Nov 2026 17:39:37 +1000 Subject: [PATCH 3/3] Bluetooth: hci_bcm4377: add KUnit tests for BCM4388 codecs v2 fixup Add KUnit coverage for bcm4377_fixup_codecs_v2() covering a live BCM4388 Read Local Supported Codecs v2 capture, multi-vendor re-interleaving, non-zero status, unrelated opcodes, truncated buffers, and empty codec lists. Signed-off-by: Marcelo Alcantara --- drivers/bluetooth/hci_bcm4377_test.c | 116 +++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/drivers/bluetooth/hci_bcm4377_test.c b/drivers/bluetooth/hci_bcm4377_test.c index 7beb3ddd26716..c1eaf6c12db93 100644 --- a/drivers/bluetooth/hci_bcm4377_test.c +++ b/drivers/bluetooth/hci_bcm4377_test.c @@ -122,9 +122,125 @@ static void bcm4377_footer_event_len_test(struct kunit *test) } } +/* + * BCM4388 returns separate ID and transport arrays for Read Local Supported + * Codecs v2; verify re-interleaving against a live BCM4388 capture. + */ +static void bcm4377_fixup_codecs_v2_bcm4388_test(struct kunit *test) +{ + static const u8 raw[] = { + HCI_EV_CMD_COMPLETE, 0x19, 0x01, 0x0d, 0x10, + 0x00, 0x07, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, + 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x04, + 0x01, + 0x75, 0x00, 0x10, 0x00, + 0x02, + }; + static const u8 expected[] = { + HCI_EV_CMD_COMPLETE, 0x19, 0x01, 0x0d, 0x10, + 0x00, 0x07, + 0x00, 0x02, 0x01, 0x02, 0x02, 0x02, 0x03, 0x02, + 0x04, 0x02, 0x05, 0x02, 0x06, 0x04, + 0x01, + 0x75, 0x00, 0x10, 0x00, 0x02, + }; + struct sk_buff *skb; + + skb = bt_skb_alloc(sizeof(raw), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, skb); + memcpy(skb_put(skb, sizeof(raw)), raw, sizeof(raw)); + + bcm4377_fixup_codecs_v2(skb); + KUNIT_EXPECT_EQ(test, skb->len, sizeof(expected)); + KUNIT_EXPECT_EQ(test, memcmp(skb->data, expected, sizeof(expected)), 0); + kfree_skb(skb); +} + +static void bcm4377_fixup_codecs_v2_multi_vendor_test(struct kunit *test) +{ + static const u8 raw[] = { + HCI_EV_CMD_COMPLETE, 0x14, 0x01, 0x0d, 0x10, + 0x00, 0x02, + 0x05, 0x06, + 0x02, 0x04, + 0x02, + 0x75, 0x00, 0x10, 0x00, + 0x4c, 0x00, 0x01, 0x02, + 0x02, 0x04, + }; + static const u8 expected[] = { + HCI_EV_CMD_COMPLETE, 0x14, 0x01, 0x0d, 0x10, + 0x00, 0x02, + 0x05, 0x02, 0x06, 0x04, + 0x02, + 0x75, 0x00, 0x10, 0x00, 0x02, + 0x4c, 0x00, 0x01, 0x02, 0x04, + }; + struct sk_buff *skb; + + skb = bt_skb_alloc(sizeof(raw), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, skb); + memcpy(skb_put(skb, sizeof(raw)), raw, sizeof(raw)); + + bcm4377_fixup_codecs_v2(skb); + KUNIT_EXPECT_EQ(test, skb->len, sizeof(expected)); + KUNIT_EXPECT_EQ(test, memcmp(skb->data, expected, sizeof(expected)), 0); + kfree_skb(skb); +} + +static void bcm4377_fixup_codecs_v2_ignored_test(struct kunit *test) +{ + static const u8 err_status[] = { + HCI_EV_CMD_COMPLETE, 0x08, 0x01, 0x0d, 0x10, + 0x12, 0x01, 0x05, 0x02, 0x00, + }; + static const u8 other_opcode[] = { + HCI_EV_CMD_COMPLETE, 0x08, 0x01, 0x0b, 0x10, + 0x00, 0x01, 0x05, 0x02, 0x00, + }; + static const u8 truncated_skb[] = { + HCI_EV_CMD_COMPLETE, 0x19, 0x01, 0x0d, 0x10, + 0x00, 0x07, 0x00, 0x01, 0x02, + }; + static const u8 truncated_plen[] = { + HCI_EV_CMD_COMPLETE, 0x08, 0x01, 0x0d, 0x10, + 0x00, 0x07, 0x00, 0x01, 0x02, + }; + static const u8 empty_codecs[] = { + HCI_EV_CMD_COMPLETE, 0x06, 0x01, 0x0d, 0x10, + 0x00, 0x00, 0x00, + }; + static const struct { + const u8 *data; + size_t len; + } cases[] = { + { err_status, sizeof(err_status) }, + { other_opcode, sizeof(other_opcode) }, + { truncated_skb, sizeof(truncated_skb) }, + { truncated_plen, sizeof(truncated_plen) }, + { empty_codecs, sizeof(empty_codecs) }, + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + struct sk_buff *skb = bt_skb_alloc(cases[i].len, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, skb); + memcpy(skb_put(skb, cases[i].len), cases[i].data, cases[i].len); + bcm4377_fixup_codecs_v2(skb); + KUNIT_EXPECT_EQ(test, skb->len, cases[i].len); + KUNIT_EXPECT_EQ(test, memcmp(skb->data, cases[i].data, cases[i].len), 0); + kfree_skb(skb); + } +} + static struct kunit_case bcm4377_test_cases[] = { KUNIT_CASE(bcm4377_enqueue_uses_host_head_test), KUNIT_CASE(bcm4377_footer_event_len_test), + KUNIT_CASE(bcm4377_fixup_codecs_v2_bcm4388_test), + KUNIT_CASE(bcm4377_fixup_codecs_v2_multi_vendor_test), + KUNIT_CASE(bcm4377_fixup_codecs_v2_ignored_test), {} };