Bluetooth: btrtl: Add firmware format v3 support and RTL8922D
v11 cover Bluetooth: btrtl: Add firmware format v3 support and RTL8922D
This series adds support for the new Realtek firmware format v3 and for the first controller that uses it, the RTL8922D.
Format v3 replaces the flat epatch container with a section/subsection layout that can carry several independently configurable images, selected by key id and IC cut, each with its own register fixups and optional per-image config file. Patch 1 adds the parser and the downloader. The new code lives in btrtl_enh.c, which is linked into the same module as btrtl_main.c, so the lifetime of the parsed image list follows the existing btrtl_device_info and no extra module or symbol_get() dependency is introduced. Support for the existing v1 and v2 firmware is unchanged.
Patch 2 adds the RTL8922D id table entry, the matching quirks and the MODULE_FIRMWARE() entries. The firmware binary will be submitted to the linux-firmware project separately.
Both patches are needed for a working RTL8922D. Patch 2 depends only on patch 1 and the series should be applied as a whole.
btrtl.c is renamed to btrtl_main.c so that it and btrtl_enh.c can be built as one composite object while keeping the module name btrtl.ko. The CONFIG_BT_RTL option and the exported symbols are unchanged.
v11 fixes a link error reported by the kernel test robot and addresses the Sashiko review of v10; see the changelog in patch 1.
Testing was done on RTL8922D over USB (0bda:a82d, chip id 55) with rtl8922du_fw.bin; the boot log is included in patch 1.
Alex Lu (1):
Bluetooth: btrtl: Add firmware format v3 support
Hilda Wu (1):
Bluetooth: btrtl: Add the support for RTL8922D
drivers/bluetooth/Makefile | 2 +
drivers/bluetooth/btrtl.h | 87 ++-
drivers/bluetooth/btrtl_enh.c | 794 ++++++++++++++++++++
drivers/bluetooth/{btrtl.c => btrtl_main.c} | 238 ++++--
drivers/bluetooth/btusb_main.c | 26 +-
5 files changed, 1066 insertions(+), 81 deletions(-)
create mode 100644 drivers/bluetooth/btrtl_enh.c
rename drivers/bluetooth/{btrtl.c => btrtl_main.c} (88%)Signature · 1 line
2.34.1
PATCH v11 1/2 Bluetooth: btrtl: Add firmware format v3 support
From: Alex Lu <[email protected]>
Realtek has introduced a new Bluetooth firmware format, firmware format v3. This is a Realtek-internal specification.
This patch extends the btrtl driver to recognize and parse the new v3 file format, including:
- New signature string and subsection ID definitions
- Extension of btrtl_device_info to store v3-specific metadata
- Logic to extract and load firmware data out of v3
- Maintains compatibility with the existing v2 firmware format
The v3 code is in the new btrtl_enh.c. btrtl.c is renamed to btrtl_main.c so that both are linked into btrtl.ko; the module name is unchanged.
Changes for existing:
- btrtl_initialize() first sends 0xfc6f (read chip id). Only RTL8922D and later support it; older chips return an error and continue with the existing probe.
- The key id register is read only on RTL8852B, RTL8922A and V3 chips; other chips use key_id 0.
- Firmware and config files are loaded with firmware_request_nowarn(); a missing optional config is logged at debug level.
- btusb passes Realtek vendor events through btrtl_recv_event().
- Some log messages are reworded.
The RTL8922D is the first IC to use firmware format v3. The firmware binary (rtl8922du_fw.bin) payload is approximately 260 KB (0x3f603 bytes), and on a typical boot the full sequence from rtl_load_file() to rtl_finalize_download() completing takes average ~454 ms.
The following is RTL8922D log as expected fw format v3 output:
[ 109.957852] Bluetooth: hci0: RTL: chip_id status=0x00 id=0x37 [ 109.958089] Bluetooth: hci0: RTL: hci 0e.000d lmp 0e.8922 [ 109.958339] Bluetooth: hci0: RTL: rom_version status=0 version=0 [ 109.958588] Bluetooth: hci0: RTL: key id 0 [ 109.958590] Bluetooth: hci0: RTL: loading rtl_bt/rtl8922du_fw.bin [ 110.052880] Bluetooth: hci0: RTL: loading rtl_bt/rtl8922du_config.bin [ 110.052910] Bluetooth: hci0: RTL: fw key id 0 [ 110.052911] Bluetooth: hci0: RTL: sub f000:00 chip 55 cut 01 len 0000c2a1 [ 110.052918] Bluetooth: hci0: RTL: subsection ver e6c9e544 [ 110.052937] Bluetooth: hci0: RTL: sub f001:00 chip 55 cut 01 len 0001bac5 [ 110.052951] Bluetooth: hci0: RTL: subsection ver e6c9e544 [ 110.053276] Bluetooth: hci0: RTL: sub f002:00 chip 55 cut 01 len 0001789d [ 110.053290] Bluetooth: hci0: RTL: subsection ver 3cddadfd [ 110.078958] Bluetooth: hci0: RTL: cfg rtl_bt/rtl8922du_config_f002.bin found [ 110.078967] Bluetooth: hci0: RTL: skip f000:00: key_id 1 != 0 [ 110.078970] Bluetooth: hci0: RTL: skip f001:00: key_id 1 != 0 [ 110.078971] Bluetooth: hci0: RTL: skip f002:00: key_id 1 != 0 [ 110.078972] Bluetooth: hci0: RTL: skip f000:00: key_id 2 != 0 [ 110.078973] Bluetooth: hci0: RTL: skip f001:00: key_id 2 != 0 [ 110.078975] Bluetooth: hci0: RTL: skip f002:00: key_id 2 != 0 [ 110.078976] Bluetooth: hci0: RTL: fw payload len 0x0003f603 [ 110.516274] Bluetooth: hci0: RTL: Watchdog reset status 00 [ 110.519757] Bluetooth: hci0: RTL: fw version 0x3cddadfd
---
Note: checkpatch reports FILE_PATH_CHANGES for the new and renamed files. They are already covered by "F: drivers/bluetooth/" in the BLUETOOTH SUBSYSTEM entry, so MAINTAINERS is not updated.
About the rename:Kbuild does not allow a composite module to share a name with one of its objects, so btrtl.c becomes btrtl_main.c (as btusb_main.c does for btusb.ko). This keeps btrtl.ko for existing users and module configs.
Notes on Sashiko feedback:
- 0xfc6f is only implemented by RTL8922D and later. Older ROMs and patch firmware reply at once with "Unknown HCI Command" (0x01) and use 0xfc6f for nothing else.
- V3_1 chips do not support 0xfc6f, so the chip id comes from the image; the firmware file itself is selected by ic_info.
- Only RTL8852B, RTL8922A and V3 chips have the key id register (8852BT and 8761C do not). RTL8852B and RTL8922A share the same register address.
- V3_2 ROMs accept the appended config with secure boot enabled.
- 0xfdcf only reports whether another driver is downloading. It holds nothing, so a failed BT download leaves nothing to release.
- Only err 2 in the end-of-image reply means verification failure.
- Only the V3_2 container needs the download-state handshake (0xfdcf + vendor event 0x77). V3 firmware is shipped for USB parts only.
- firmware_request_nowarn(): a missing firmware or required config is reported with rtl_dev_err(); a missing optional config is expected and logged at debug level only.
V10 -> V11:
- Rename btrtl.c to btrtl_main.c so the module stays btrtl.ko instead of btrtl_core.ko
- Only call btrtl_recv_event() when CONFIG_BT_HCIBTUSB_RTL is enabled, fixing a link error with BT_HCIBTUSB=y and BT_RTL=m (kernel test robot)
- Print the converted section opcode in debug logs, also in rtlbt_parse_firmware_v2()
- Log a missing optional config at debug level
- Drop unused function parameters, an unused macro and stray blank lines
- Read the key id register on RTL8852B as well
- Update comments on the key id read, V3_1 chip id, V3_2 config and image verification
- Describe changes to existing controllers in the commit message; drop the claim that the format is documented inline
- Drop Tested-by from Veysi Özgün since the code changed
V9 -> V10:
- Only use the chip id from btrtl_read_chip_id() when the command succeeds; a non-zero status is already reported as an error by __hci_cmd_sync()
- Use DIV_ROUND_UP() for the V3 fragment count and keep a full-size last fragment when fw_len is a multiple of RTL_FRAG_LEN
- V3_1: reuse the config already loaded by btrtl_initialize() for the first image instead of loading it again
V3_2: fall back to the 4-byte default config when cfg_name is NULL or the per-image config file is missing
- Log the config file name when it is not found
- Drop the now unnecessary EXPORT_SYMBOL_GPL() for the helpers that live in the same module
- Refresh the sample log
- Keep Tested-by from Veysi Özgün (v8); no change to the firmware download flow for RTL8852BU
V8 -> V9:
- Adjust the length of each log message (< 80)
- Add Tested-by
V7 -> V8:
- Fix potential memory leak of v3 firmware patch images: merge btrtl_enh.c into the same btrtl_core.ko module via kbuild composite object, instead of a separate module accessed through symbol_get()/symbol_put(). This removes the mismatch between the module's reference count and the lifetime of the dynamically allocated patch image/config buffers (Reported by Sashiko)
- Add a bounds check on skb->len before accessing skb->data[0] in btusb_recv_event_realtek(), to avoid an out-of-bounds read when a zero-length packet is received (Reported by Sashiko)
- Return the error immediately instead of silently continuing to the next subsection when rtlbt_parse_section_v3() fails, to avoid loading incomplete firmware onto the controller (Reported by Sashiko)
- Fix a skb leak in the btrtl_recv_event() (Reported by Sashiko)
- Add a comment clarifying that the security project ID (key_id) register is only present starting from RTL8922A and V3-capable chips; legacy chips intentionally skip this read (Reported by Sashiko)
V6 -> V7:
- Fix smatch warning in btrtl_free(): move the NULL check on btrtl_dev to the very beginning of the function, before any dereference, instead of after it has already been dereferenced multiple times (Reported by kernel test / Dan Carpenter)
- Revise log message to use "subsection" terminology for clarity
- Add informational message when a config file is not found
V5 -> V6:
- Add missing parentheses around the entire is_v3_fw() macro expression to avoid incorrect evaluation with the logical NOT operator on legacy firmware types
- Add NULL check on ic_info->cfg_name before use in rtlbt_parse_config() to avoid NULL pointer dereference when a crafted firmware specifies FW_TYPE_V3_2 on legacy chips without configuration
- Add bounds check to reject patch_image_len < 4 before reading the image version, preventing an out-of-bounds read
- Propagate the return value of rtlbt_parse_config() and abort parsing on failure instead of silently continuing without the required configuration
- Fix a race condition in rtl_check_download_state() by setting the REALTEK_DOWNLOADING flag before issuing the check-download command, instead of after, to avoid missing the completion event
- Split v3 firmware parse/download logic into new btrtl_enh.c to keep btrtl.c size manageable
V4 -> V5:
- Add independent support for RTL8922D section
- Introduce macros to improve code readability
- Document firmware format v3 and its differences
- Align implementation with reviewer feedback
V3 -> V4:
- Rework skb->data access and add clarifying comments
- Fix latent issues
V2 -> V3:
- Address coccinelle warning
V1 -> V2:
- Add missing symbols
- Resolve build warnings
---
drivers/bluetooth/Makefile | 2 +
drivers/bluetooth/btrtl.h | 87 ++-
drivers/bluetooth/btrtl_enh.c | 794 ++++++++++++++++++++
drivers/bluetooth/{btrtl.c => btrtl_main.c} | 215 ++++--
drivers/bluetooth/btusb_main.c | 26 +-
5 files changed, 1043 insertions(+), 81 deletions(-)
create mode 100644 drivers/bluetooth/btrtl_enh.c
rename drivers/bluetooth/{btrtl.c => btrtl_main.c} (89%)drivers/bluetooth/Makefile +2 -0
@@ -36,6 +36,8 @@ obj-$(CONFIG_BT_HCIRSI) += btrsi.o btmrvl-y := btmrvl_main.o btmrvl-$(CONFIG_DEBUG_FS) += btmrvl_debugfs.o +btrtl-y := btrtl_main.o btrtl_enh.o + hci_uart-y := hci_ldisc.o hci_uart-$(CONFIG_BT_HCIUART_SERDEV) += hci_serdev.o hci_uart-$(CONFIG_BT_HCIUART_H4) += hci_h4.o
drivers/bluetooth/btrtl.h +86 -1
120 lines not on this page · raw message
drivers/bluetooth/btrtl_enh.c new file +794 -0
794 lines not on this page · raw message
drivers/bluetooth/btrtl_main.c renamed from drivers/bluetooth/btrtl.c +146 -69
459 lines not on this page · raw message
drivers/bluetooth/btusb_main.c +15 -11
@@ -2976,20 +2976,24 @@ static int btusb_setup_realtek(struct hci_dev *hdev)static int btusb_recv_event_realtek (struct hci_dev *hdev,struct sk_buff *skb) { -if (skb->len >=HCI_EVENT_HDR_SIZE +1 && - skb->data[0 ] ==HCI_EV_VENDOR && - skb->data[2 ] ==RTK_SUB_EVENT_CODE_COREDUMP ) { -struct rtk_dev_coredump_hdr hdr = { - .code =RTK_DEVCOREDUMP_CODE_MEMDUMP , - }; +if (skb->len >=1 && skb->data[0 ] ==HCI_EV_VENDOR ) { +if (skb->len >=HCI_EVENT_HDR_SIZE +1 && + skb->data[2 ] ==RTK_SUB_EVENT_CODE_COREDUMP ) { +struct rtk_dev_coredump_hdr hdr = { + .code =RTK_DEVCOREDUMP_CODE_MEMDUMP , + }; -bt_dev_dbg (hdev,"RTL: received coredump vendor evt, len %u" , - skb->len); +bt_dev_dbg (hdev,"RTL: received coredump vendor evt, len %u" , + skb->len); -btusb_rtl_alloc_devcoredump (hdev, &hdr, skb->data, skb->len); -kfree_skb (skb); +btusb_rtl_alloc_devcoredump (hdev, &hdr, skb->data, skb->len); +kfree_skb (skb); -return 0 ; +return 0 ; + } + +if (IS_ENABLED (CONFIG_BT_HCIBTUSB_RTL )) +return btrtl_recv_event (hdev, skb); }return hci_recv_frame (hdev, skb);
Signature · 1 line
2.34.1
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1221822/
---Test result---
Test Summary:
CheckPatch FAIL 2.01 seconds VerifyFixes PASS 0.05 seconds VerifySignedoff PASS 0.05 seconds GitLint PASS 0.31 seconds SubjectPrefix PASS 0.08 seconds BuildKernel PASS 20.83 seconds CheckAllWarning PASS 23.66 seconds CheckSparse PASS 25.25 seconds BuildKernel32 PASS 20.88 seconds CheckKernelLLVM PASS 20.93 seconds TestRunnerSetup PASS 486.45 seconds IncrementalBuild PASS 20.53 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v11,1/2] Bluetooth: btrtl: Add firmware format v3 support
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #296:
drivers/bluetooth/{btrtl.c => btrtl_main.c} | 215 ++++--total: 0 errors, 1 warnings, 1416 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/home/runner/work/bluetooth-next/bluetooth-next/src/patch/15021100.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
https://github.com/bluez/bluetooth-next/pull/1223
---
Regards,
Linux Bluetooth
PATCH v11 2/2 Bluetooth: btrtl: Add the support for RTL8922D
In reply to Hilda WuAdd the support for RTL8922D BT controller on USB interface. The necessary firmware will be submitted to linux-firmware project.
The device info from /sys/kernel/debug/usb/devices as below.
T: Bus=01 Lev=01 Prnt=01 Port=03 Cnt=01 Dev#= 2 Spd=480 MxCh= 0 D: Ver= 2.00 Cls=ef(misc ) Sub=02 Prot=01 MxPS=64 #Cfgs= 1 P: Vendor=0bda ProdID=a82d Rev= 0.00 S: Manufacturer=Realtek S: Product=802.11be WLAN NIC S: SerialNumber=00e04c000001 C:* #Ifs= 4 Cfg#= 1 Atr=e0 MxPwr=500mA A: FirstIf#= 0 IfCount= 2 Cls=e0(wlcon) Sub=01 Prot=01 I:* If#= 0 Alt= 0 #EPs= 3 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=81(I) Atr=03(Int.) MxPS= 64 Ivl=125us E: Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=82(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms I:* If#= 1 Alt= 0 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 0 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 0 Ivl=1ms I: If#= 1 Alt= 1 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 9 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 9 Ivl=1ms I: If#= 1 Alt= 2 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 17 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 17 Ivl=1ms I: If#= 1 Alt= 3 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 25 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 25 Ivl=1ms I: If#= 1 Alt= 4 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 33 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 33 Ivl=1ms I: If#= 1 Alt= 5 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 49 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 49 Ivl=1ms I: If#= 1 Alt= 6 #EPs= 2 Cls=e0(wlcon) Sub=01 Prot=01 Driver=btusb E: Ad=03(O) Atr=01(Isoc) MxPS= 63 Ivl=1ms E: Ad=83(I) Atr=01(Isoc) MxPS= 63 Ivl=1ms I:* If#= 2 Alt= 0 #EPs= 9 Cls=ff(vend.) Sub=ff Prot=ff Driver=(none) E: Ad=84(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=05(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=06(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=07(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=88(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=09(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=0a(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=0b(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=0c(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms I:* If#= 3 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=ff Prot=ff Driver=(none) E: Ad=8d(I) Atr=03(Int.) MxPS= 16 Ivl=125us E: Ad=0e(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms E: Ad=8e(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
---
V10 -> V11:
- Read the 8922A key id register only on hci_rev 0xa, and fail setup when the chip id cannot be read on 8922D (Sashiko)
--- drivers/bluetooth/btrtl_main.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-)
drivers/bluetooth/btrtl_main.c +24 -1
@@ -73,6 +73,7 @@ enum btrtl_chip_id {CHIP_ID_8922A =44 ,CHIP_ID_8852BT =47 ,CHIP_ID_8761C =51 , + CHIP_ID_8922D = 55, }; static const struct id_table ic_id_table[] = {
@@ -310,6 +311,15 @@ static const struct id_table ic_id_table[] = { .cfg_name = "rtl_bt/rtl8852btu_config", .hw_info = "rtl8852btu" }, +/* 8922DU */ + { IC_INFO(RTL_ROM_LMP_8922A, 0xd, 0xe, HCI_USB), + .config_needed = false, + .has_rom_version = true, + .has_msft_ext = true, + .fw_name = "rtl_bt/rtl8922du_fw", + .cfg_name = "rtl_bt/rtl8922du_config", + .hw_info = "rtl8922du" }, + }; static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
@@ -1211,6 +1221,16 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,return btrtl_dev; } +/* 8922D always reports its chip id; without it the key id and the +* v3 images cannot be selected. +*/ +if (lmp_subver ==RTL_ROM_LMP_8922A && hci_rev ==0x000d && + !btrtl_dev->project_id) { +rtl_dev_err (hdev,"failed to read chip id" ); + ret = -EIO ; + goto err_free; + } +if (btrtl_dev->ic_info->has_rom_version) { ret =rtl_read_rom_version (hdev, &btrtl_dev->rom_version);if (ret)
@@ -1230,7 +1250,7 @@ struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,if (ret <0 ) goto err_free; key_id = reg_val[0 ]; - }else if (lmp_subver ==RTL_ROM_LMP_8922A || + }else if ((lmp_subver ==RTL_ROM_LMP_8922A && hci_rev ==0x000a ) || (lmp_subver ==RTL_ROM_LMP_8852A && hci_rev ==0x000b )) { ret =btrtl_vendor_read_reg16 (hdev,RTL_SEC_PROJ_V2 , reg_val);if (ret <0 )
@@ -1367,6 +1387,7 @@ void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)case CHIP_ID_8922A :case CHIP_ID_8852BT :case CHIP_ID_8761C : +case CHIP_ID_8922D :hci_set_quirk (hdev,HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED );/* RTL8852C needs to transmit mSBC data continuously without
@@ -1643,3 +1664,5 @@ MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw_v2.bin");MODULE_FIRMWARE ("rtl_bt/rtl8852cu_config.bin" );MODULE_FIRMWARE ("rtl_bt/rtl8922au_fw.bin" );MODULE_FIRMWARE ("rtl_bt/rtl8922au_config.bin" ); +MODULE_FIRMWARE ("rtl_bt/rtl8922du_fw.bin" ); +MODULE_FIRMWARE ("rtl_bt/rtl8922du_config.bin" );
Signature · 1 line
2.34.1