1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2db33c77dSCarlo Caione /* 3db33c77dSCarlo Caione * Bluetooth support for Realtek devices 4db33c77dSCarlo Caione * 5db33c77dSCarlo Caione * Copyright (C) 2015 Endless Mobile, Inc. 6db33c77dSCarlo Caione */ 7db33c77dSCarlo Caione 8db33c77dSCarlo Caione #include <linux/module.h> 9db33c77dSCarlo Caione #include <linux/firmware.h> 10db33c77dSCarlo Caione #include <asm/unaligned.h> 11db33c77dSCarlo Caione #include <linux/usb.h> 12db33c77dSCarlo Caione 13db33c77dSCarlo Caione #include <net/bluetooth/bluetooth.h> 14db33c77dSCarlo Caione #include <net/bluetooth/hci_core.h> 15db33c77dSCarlo Caione 16db33c77dSCarlo Caione #include "btrtl.h" 17db33c77dSCarlo Caione 18db33c77dSCarlo Caione #define VERSION "0.1" 19db33c77dSCarlo Caione 20db33c77dSCarlo Caione #define RTL_EPATCH_SIGNATURE "Realtech" 21db33c77dSCarlo Caione #define RTL_ROM_LMP_8723A 0x1200 22db33c77dSCarlo Caione #define RTL_ROM_LMP_8723B 0x8723 23db33c77dSCarlo Caione #define RTL_ROM_LMP_8821A 0x8821 24db33c77dSCarlo Caione #define RTL_ROM_LMP_8761A 0x8761 251110a2dbSLarry Finger #define RTL_ROM_LMP_8822B 0x8822 260d484db6SMax Chou #define RTL_ROM_LMP_8852A 0x8852 27b85b0ee1SMartin Blumenstingl #define RTL_CONFIG_MAGIC 0x8723ab55 28db33c77dSCarlo Caione 29907f8499SAlex Lu #define IC_MATCH_FL_LMPSUBV (1 << 0) 30907f8499SAlex Lu #define IC_MATCH_FL_HCIREV (1 << 1) 31c50903e3SMartin Blumenstingl #define IC_MATCH_FL_HCIVER (1 << 2) 32c50903e3SMartin Blumenstingl #define IC_MATCH_FL_HCIBUS (1 << 3) 336f9ff246SMax Chou #define IC_INFO(lmps, hcir, hciv, bus) \ 346f9ff246SMax Chou .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \ 356f9ff246SMax Chou IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \ 36907f8499SAlex Lu .lmp_subver = (lmps), \ 376f9ff246SMax Chou .hci_rev = (hcir), \ 386f9ff246SMax Chou .hci_ver = (hciv), \ 396f9ff246SMax Chou .hci_bus = (bus) 40907f8499SAlex Lu 419ab9235fSMax Chou enum btrtl_chip_id { 429ab9235fSMax Chou CHIP_ID_8723A, 439ab9235fSMax Chou CHIP_ID_8723B, 449ab9235fSMax Chou CHIP_ID_8821A, 459ab9235fSMax Chou CHIP_ID_8761A, 469ab9235fSMax Chou CHIP_ID_8822B = 8, 479ab9235fSMax Chou CHIP_ID_8723D, 489ab9235fSMax Chou CHIP_ID_8821C, 499ab9235fSMax Chou CHIP_ID_8822C = 13, 509ab9235fSMax Chou CHIP_ID_8761B, 519ab9235fSMax Chou CHIP_ID_8852A = 18, 5218e8055cSMax Chou CHIP_ID_8852B = 20, 538b1d66b5SMax Chou CHIP_ID_8852C = 25, 549ab9235fSMax Chou }; 559ab9235fSMax Chou 56907f8499SAlex Lu struct id_table { 57907f8499SAlex Lu __u16 match_flags; 58907f8499SAlex Lu __u16 lmp_subver; 59907f8499SAlex Lu __u16 hci_rev; 60c50903e3SMartin Blumenstingl __u8 hci_ver; 61c50903e3SMartin Blumenstingl __u8 hci_bus; 62907f8499SAlex Lu bool config_needed; 6326503ad2SMartin Blumenstingl bool has_rom_version; 64f4bcba0eSMarcel Holtmann bool has_msft_ext; 65907f8499SAlex Lu char *fw_name; 66907f8499SAlex Lu char *cfg_name; 67907f8499SAlex Lu }; 68907f8499SAlex Lu 6926503ad2SMartin Blumenstingl struct btrtl_device_info { 7026503ad2SMartin Blumenstingl const struct id_table *ic_info; 7126503ad2SMartin Blumenstingl u8 rom_version; 7226503ad2SMartin Blumenstingl u8 *fw_data; 7326503ad2SMartin Blumenstingl int fw_len; 7426503ad2SMartin Blumenstingl u8 *cfg_data; 7526503ad2SMartin Blumenstingl int cfg_len; 761996d9caSKai-Heng Feng bool drop_fw; 779ab9235fSMax Chou int project_id; 7826503ad2SMartin Blumenstingl }; 7926503ad2SMartin Blumenstingl 80907f8499SAlex Lu static const struct id_table ic_id_table[] = { 816f9ff246SMax Chou /* 8723A */ 826f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB), 8326503ad2SMartin Blumenstingl .config_needed = false, 8426503ad2SMartin Blumenstingl .has_rom_version = false, 8526503ad2SMartin Blumenstingl .fw_name = "rtl_bt/rtl8723a_fw.bin", 8626503ad2SMartin Blumenstingl .cfg_name = NULL }, 8726503ad2SMartin Blumenstingl 88c50903e3SMartin Blumenstingl /* 8723BS */ 896f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART), 90c50903e3SMartin Blumenstingl .config_needed = true, 91c50903e3SMartin Blumenstingl .has_rom_version = true, 92c50903e3SMartin Blumenstingl .fw_name = "rtl_bt/rtl8723bs_fw.bin", 931cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8723bs_config" }, 94c50903e3SMartin Blumenstingl 95907f8499SAlex Lu /* 8723B */ 966f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB), 97907f8499SAlex Lu .config_needed = false, 9826503ad2SMartin Blumenstingl .has_rom_version = true, 99907f8499SAlex Lu .fw_name = "rtl_bt/rtl8723b_fw.bin", 1001cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8723b_config" }, 101907f8499SAlex Lu 102907f8499SAlex Lu /* 8723D */ 1036f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB), 104907f8499SAlex Lu .config_needed = true, 10526503ad2SMartin Blumenstingl .has_rom_version = true, 106907f8499SAlex Lu .fw_name = "rtl_bt/rtl8723d_fw.bin", 1071cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8723d_config" }, 108907f8499SAlex Lu 109c50903e3SMartin Blumenstingl /* 8723DS */ 1106f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART), 111c50903e3SMartin Blumenstingl .config_needed = true, 112c50903e3SMartin Blumenstingl .has_rom_version = true, 113c50903e3SMartin Blumenstingl .fw_name = "rtl_bt/rtl8723ds_fw.bin", 1141cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8723ds_config" }, 115c50903e3SMartin Blumenstingl 116907f8499SAlex Lu /* 8821A */ 1176f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB), 118907f8499SAlex Lu .config_needed = false, 11926503ad2SMartin Blumenstingl .has_rom_version = true, 120907f8499SAlex Lu .fw_name = "rtl_bt/rtl8821a_fw.bin", 1211cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8821a_config" }, 122907f8499SAlex Lu 123907f8499SAlex Lu /* 8821C */ 1246f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB), 125907f8499SAlex Lu .config_needed = false, 12626503ad2SMartin Blumenstingl .has_rom_version = true, 1274d51fb04SMarcel Holtmann .has_msft_ext = true, 128907f8499SAlex Lu .fw_name = "rtl_bt/rtl8821c_fw.bin", 1291cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8821c_config" }, 130907f8499SAlex Lu 131907f8499SAlex Lu /* 8761A */ 1326f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB), 133907f8499SAlex Lu .config_needed = false, 13426503ad2SMartin Blumenstingl .has_rom_version = true, 135907f8499SAlex Lu .fw_name = "rtl_bt/rtl8761a_fw.bin", 1361cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8761a_config" }, 137907f8499SAlex Lu 13804896832SZiqian SUN (Zamir) /* 8761B */ 1399fd2e294SJoakim Tjernlund { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART), 14004896832SZiqian SUN (Zamir) .config_needed = false, 14104896832SZiqian SUN (Zamir) .has_rom_version = true, 142f4bcba0eSMarcel Holtmann .has_msft_ext = true, 14304896832SZiqian SUN (Zamir) .fw_name = "rtl_bt/rtl8761b_fw.bin", 14404896832SZiqian SUN (Zamir) .cfg_name = "rtl_bt/rtl8761b_config" }, 14504896832SZiqian SUN (Zamir) 1469fd2e294SJoakim Tjernlund /* 8761BU */ 1479fd2e294SJoakim Tjernlund { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB), 1489fd2e294SJoakim Tjernlund .config_needed = false, 1499fd2e294SJoakim Tjernlund .has_rom_version = true, 1509fd2e294SJoakim Tjernlund .fw_name = "rtl_bt/rtl8761bu_fw.bin", 1519fd2e294SJoakim Tjernlund .cfg_name = "rtl_bt/rtl8761bu_config" }, 1529fd2e294SJoakim Tjernlund 153848fc616SMax Chou /* 8822C with UART interface */ 154b050c5bbSVyacheslav Bocharov { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART), 155b050c5bbSVyacheslav Bocharov .config_needed = true, 156b050c5bbSVyacheslav Bocharov .has_rom_version = true, 157b050c5bbSVyacheslav Bocharov .has_msft_ext = true, 158b050c5bbSVyacheslav Bocharov .fw_name = "rtl_bt/rtl8822cs_fw.bin", 159b050c5bbSVyacheslav Bocharov .cfg_name = "rtl_bt/rtl8822cs_config" }, 160b050c5bbSVyacheslav Bocharov 161b050c5bbSVyacheslav Bocharov /* 8822C with UART interface */ 1626f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART), 163848fc616SMax Chou .config_needed = true, 164848fc616SMax Chou .has_rom_version = true, 165f4bcba0eSMarcel Holtmann .has_msft_ext = true, 166848fc616SMax Chou .fw_name = "rtl_bt/rtl8822cs_fw.bin", 167848fc616SMax Chou .cfg_name = "rtl_bt/rtl8822cs_config" }, 168848fc616SMax Chou 1698ecfdc95SAlex Lu /* 8822C with USB interface */ 1706f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB), 1718ecfdc95SAlex Lu .config_needed = false, 1728ecfdc95SAlex Lu .has_rom_version = true, 173f4bcba0eSMarcel Holtmann .has_msft_ext = true, 1748ecfdc95SAlex Lu .fw_name = "rtl_bt/rtl8822cu_fw.bin", 1758ecfdc95SAlex Lu .cfg_name = "rtl_bt/rtl8822cu_config" }, 1768ecfdc95SAlex Lu 177907f8499SAlex Lu /* 8822B */ 1786f9ff246SMax Chou { IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB), 179907f8499SAlex Lu .config_needed = true, 18026503ad2SMartin Blumenstingl .has_rom_version = true, 181f4bcba0eSMarcel Holtmann .has_msft_ext = true, 182907f8499SAlex Lu .fw_name = "rtl_bt/rtl8822b_fw.bin", 1831cc194caSHans de Goede .cfg_name = "rtl_bt/rtl8822b_config" }, 1840d484db6SMax Chou 1850d484db6SMax Chou /* 8852A */ 1860d484db6SMax Chou { IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB), 1870d484db6SMax Chou .config_needed = false, 1880d484db6SMax Chou .has_rom_version = true, 189f4bcba0eSMarcel Holtmann .has_msft_ext = true, 1900d484db6SMax Chou .fw_name = "rtl_bt/rtl8852au_fw.bin", 1910d484db6SMax Chou .cfg_name = "rtl_bt/rtl8852au_config" }, 19218e8055cSMax Chou 19318e8055cSMax Chou /* 8852B */ 19418e8055cSMax Chou { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB), 19518e8055cSMax Chou .config_needed = false, 19618e8055cSMax Chou .has_rom_version = true, 19718e8055cSMax Chou .has_msft_ext = true, 19818e8055cSMax Chou .fw_name = "rtl_bt/rtl8852bu_fw.bin", 19918e8055cSMax Chou .cfg_name = "rtl_bt/rtl8852bu_config" }, 2008b1d66b5SMax Chou 2018b1d66b5SMax Chou /* 8852C */ 2028b1d66b5SMax Chou { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB), 2038b1d66b5SMax Chou .config_needed = false, 2048b1d66b5SMax Chou .has_rom_version = true, 2058b1d66b5SMax Chou .has_msft_ext = true, 2068b1d66b5SMax Chou .fw_name = "rtl_bt/rtl8852cu_fw.bin", 2078b1d66b5SMax Chou .cfg_name = "rtl_bt/rtl8852cu_config" }, 208907f8499SAlex Lu }; 209907f8499SAlex Lu 210c50903e3SMartin Blumenstingl static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, 211c50903e3SMartin Blumenstingl u8 hci_ver, u8 hci_bus) 21226503ad2SMartin Blumenstingl { 21326503ad2SMartin Blumenstingl int i; 21426503ad2SMartin Blumenstingl 21526503ad2SMartin Blumenstingl for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { 21626503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && 21726503ad2SMartin Blumenstingl (ic_id_table[i].lmp_subver != lmp_subver)) 21826503ad2SMartin Blumenstingl continue; 21926503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && 22026503ad2SMartin Blumenstingl (ic_id_table[i].hci_rev != hci_rev)) 22126503ad2SMartin Blumenstingl continue; 222c50903e3SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) && 223c50903e3SMartin Blumenstingl (ic_id_table[i].hci_ver != hci_ver)) 224c50903e3SMartin Blumenstingl continue; 225c50903e3SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) && 226c50903e3SMartin Blumenstingl (ic_id_table[i].hci_bus != hci_bus)) 227c50903e3SMartin Blumenstingl continue; 22826503ad2SMartin Blumenstingl 22926503ad2SMartin Blumenstingl break; 23026503ad2SMartin Blumenstingl } 23126503ad2SMartin Blumenstingl if (i >= ARRAY_SIZE(ic_id_table)) 23226503ad2SMartin Blumenstingl return NULL; 23326503ad2SMartin Blumenstingl 23426503ad2SMartin Blumenstingl return &ic_id_table[i]; 23526503ad2SMartin Blumenstingl } 23626503ad2SMartin Blumenstingl 237240b64a8SAlex Lu static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) 238240b64a8SAlex Lu { 239240b64a8SAlex Lu struct sk_buff *skb; 240240b64a8SAlex Lu 241240b64a8SAlex Lu skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 242240b64a8SAlex Lu HCI_INIT_TIMEOUT); 243240b64a8SAlex Lu if (IS_ERR(skb)) { 244240b64a8SAlex Lu rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)", 245240b64a8SAlex Lu PTR_ERR(skb)); 246240b64a8SAlex Lu return skb; 247240b64a8SAlex Lu } 248240b64a8SAlex Lu 249240b64a8SAlex Lu if (skb->len != sizeof(struct hci_rp_read_local_version)) { 250240b64a8SAlex Lu rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch"); 251240b64a8SAlex Lu kfree_skb(skb); 252240b64a8SAlex Lu return ERR_PTR(-EIO); 253240b64a8SAlex Lu } 254240b64a8SAlex Lu 255240b64a8SAlex Lu return skb; 256240b64a8SAlex Lu } 257240b64a8SAlex Lu 258db33c77dSCarlo Caione static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) 259db33c77dSCarlo Caione { 260db33c77dSCarlo Caione struct rtl_rom_version_evt *rom_version; 261db33c77dSCarlo Caione struct sk_buff *skb; 262db33c77dSCarlo Caione 263db33c77dSCarlo Caione /* Read RTL ROM version command */ 264db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); 265db33c77dSCarlo Caione if (IS_ERR(skb)) { 266f1300c03SAlex Lu rtl_dev_err(hdev, "Read ROM version failed (%ld)", 267a5c76e67SHans de Goede PTR_ERR(skb)); 268db33c77dSCarlo Caione return PTR_ERR(skb); 269db33c77dSCarlo Caione } 270db33c77dSCarlo Caione 271db33c77dSCarlo Caione if (skb->len != sizeof(*rom_version)) { 272f1300c03SAlex Lu rtl_dev_err(hdev, "version event length mismatch"); 273db33c77dSCarlo Caione kfree_skb(skb); 274db33c77dSCarlo Caione return -EIO; 275db33c77dSCarlo Caione } 276db33c77dSCarlo Caione 277db33c77dSCarlo Caione rom_version = (struct rtl_rom_version_evt *)skb->data; 278f1300c03SAlex Lu rtl_dev_info(hdev, "rom_version status=%x version=%x", 2792064ee33SMarcel Holtmann rom_version->status, rom_version->version); 280db33c77dSCarlo Caione 281db33c77dSCarlo Caione *version = rom_version->version; 282db33c77dSCarlo Caione 283db33c77dSCarlo Caione kfree_skb(skb); 284db33c77dSCarlo Caione return 0; 285db33c77dSCarlo Caione } 286db33c77dSCarlo Caione 28726503ad2SMartin Blumenstingl static int rtlbt_parse_firmware(struct hci_dev *hdev, 28826503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev, 289db33c77dSCarlo Caione unsigned char **_buf) 290db33c77dSCarlo Caione { 291e5070e07SColin Ian King static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; 292db33c77dSCarlo Caione struct rtl_epatch_header *epatch_info; 293db33c77dSCarlo Caione unsigned char *buf; 29426503ad2SMartin Blumenstingl int i, len; 295db33c77dSCarlo Caione size_t min_size; 29626503ad2SMartin Blumenstingl u8 opcode, length, data; 297db33c77dSCarlo Caione int project_id = -1; 298db33c77dSCarlo Caione const unsigned char *fwptr, *chip_id_base; 299db33c77dSCarlo Caione const unsigned char *patch_length_base, *patch_offset_base; 300db33c77dSCarlo Caione u32 patch_offset = 0; 301db33c77dSCarlo Caione u16 patch_length, num_patches; 3021110a2dbSLarry Finger static const struct { 3031110a2dbSLarry Finger __u16 lmp_subver; 3041110a2dbSLarry Finger __u8 id; 3051110a2dbSLarry Finger } project_id_to_lmp_subver[] = { 3061110a2dbSLarry Finger { RTL_ROM_LMP_8723A, 0 }, 3071110a2dbSLarry Finger { RTL_ROM_LMP_8723B, 1 }, 3081110a2dbSLarry Finger { RTL_ROM_LMP_8821A, 2 }, 3091110a2dbSLarry Finger { RTL_ROM_LMP_8761A, 3 }, 3101110a2dbSLarry Finger { RTL_ROM_LMP_8822B, 8 }, 311907f8499SAlex Lu { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ 312907f8499SAlex Lu { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ 3138ecfdc95SAlex Lu { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ 31404896832SZiqian SUN (Zamir) { RTL_ROM_LMP_8761A, 14 }, /* 8761B */ 3150d484db6SMax Chou { RTL_ROM_LMP_8852A, 18 }, /* 8852A */ 31618e8055cSMax Chou { RTL_ROM_LMP_8852A, 20 }, /* 8852B */ 3178b1d66b5SMax Chou { RTL_ROM_LMP_8852A, 25 }, /* 8852C */ 318db33c77dSCarlo Caione }; 319db33c77dSCarlo Caione 320db33c77dSCarlo Caione min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; 32126503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 322db33c77dSCarlo Caione return -EINVAL; 323db33c77dSCarlo Caione 32426503ad2SMartin Blumenstingl fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); 325db33c77dSCarlo Caione if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { 326f1300c03SAlex Lu rtl_dev_err(hdev, "extension section signature mismatch"); 327db33c77dSCarlo Caione return -EINVAL; 328db33c77dSCarlo Caione } 329db33c77dSCarlo Caione 330db33c77dSCarlo Caione /* Loop from the end of the firmware parsing instructions, until 331db33c77dSCarlo Caione * we find an instruction that identifies the "project ID" for the 332db33c77dSCarlo Caione * hardware supported by this firwmare file. 33338f230f1Sshaomin Deng * Once we have that, we double-check that project_id is suitable 334db33c77dSCarlo Caione * for the hardware we are working with. 335db33c77dSCarlo Caione */ 33626503ad2SMartin Blumenstingl while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { 337db33c77dSCarlo Caione opcode = *--fwptr; 338db33c77dSCarlo Caione length = *--fwptr; 339db33c77dSCarlo Caione data = *--fwptr; 340db33c77dSCarlo Caione 341db33c77dSCarlo Caione BT_DBG("check op=%x len=%x data=%x", opcode, length, data); 342db33c77dSCarlo Caione 343db33c77dSCarlo Caione if (opcode == 0xff) /* EOF */ 344db33c77dSCarlo Caione break; 345db33c77dSCarlo Caione 346db33c77dSCarlo Caione if (length == 0) { 347f1300c03SAlex Lu rtl_dev_err(hdev, "found instruction with length 0"); 348db33c77dSCarlo Caione return -EINVAL; 349db33c77dSCarlo Caione } 350db33c77dSCarlo Caione 351db33c77dSCarlo Caione if (opcode == 0 && length == 1) { 352db33c77dSCarlo Caione project_id = data; 353db33c77dSCarlo Caione break; 354db33c77dSCarlo Caione } 355db33c77dSCarlo Caione 356db33c77dSCarlo Caione fwptr -= length; 357db33c77dSCarlo Caione } 358db33c77dSCarlo Caione 359db33c77dSCarlo Caione if (project_id < 0) { 360f1300c03SAlex Lu rtl_dev_err(hdev, "failed to find version instruction"); 361db33c77dSCarlo Caione return -EINVAL; 362db33c77dSCarlo Caione } 363db33c77dSCarlo Caione 3641110a2dbSLarry Finger /* Find project_id in table */ 3651110a2dbSLarry Finger for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { 3669ab9235fSMax Chou if (project_id == project_id_to_lmp_subver[i].id) { 3679ab9235fSMax Chou btrtl_dev->project_id = project_id; 3681110a2dbSLarry Finger break; 3691110a2dbSLarry Finger } 3709ab9235fSMax Chou } 3711110a2dbSLarry Finger 3721110a2dbSLarry Finger if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { 373f1300c03SAlex Lu rtl_dev_err(hdev, "unknown project id %d", project_id); 374db33c77dSCarlo Caione return -EINVAL; 375db33c77dSCarlo Caione } 376db33c77dSCarlo Caione 37726503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->lmp_subver != 37826503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver) { 379f1300c03SAlex Lu rtl_dev_err(hdev, "firmware is for %x but this is a %x", 38026503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver, 38126503ad2SMartin Blumenstingl btrtl_dev->ic_info->lmp_subver); 382db33c77dSCarlo Caione return -EINVAL; 383db33c77dSCarlo Caione } 384db33c77dSCarlo Caione 38526503ad2SMartin Blumenstingl epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; 386db33c77dSCarlo Caione if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { 387f1300c03SAlex Lu rtl_dev_err(hdev, "bad EPATCH signature"); 388db33c77dSCarlo Caione return -EINVAL; 389db33c77dSCarlo Caione } 390db33c77dSCarlo Caione 391db33c77dSCarlo Caione num_patches = le16_to_cpu(epatch_info->num_patches); 392db33c77dSCarlo Caione BT_DBG("fw_version=%x, num_patches=%d", 393db33c77dSCarlo Caione le32_to_cpu(epatch_info->fw_version), num_patches); 394db33c77dSCarlo Caione 395db33c77dSCarlo Caione /* After the rtl_epatch_header there is a funky patch metadata section. 396db33c77dSCarlo Caione * Assuming 2 patches, the layout is: 397db33c77dSCarlo Caione * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 398db33c77dSCarlo Caione * 399db33c77dSCarlo Caione * Find the right patch for this chip. 400db33c77dSCarlo Caione */ 401db33c77dSCarlo Caione min_size += 8 * num_patches; 40226503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 403db33c77dSCarlo Caione return -EINVAL; 404db33c77dSCarlo Caione 40526503ad2SMartin Blumenstingl chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); 406db33c77dSCarlo Caione patch_length_base = chip_id_base + (sizeof(u16) * num_patches); 407db33c77dSCarlo Caione patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); 408db33c77dSCarlo Caione for (i = 0; i < num_patches; i++) { 409db33c77dSCarlo Caione u16 chip_id = get_unaligned_le16(chip_id_base + 410db33c77dSCarlo Caione (i * sizeof(u16))); 41126503ad2SMartin Blumenstingl if (chip_id == btrtl_dev->rom_version + 1) { 412db33c77dSCarlo Caione patch_length = get_unaligned_le16(patch_length_base + 413db33c77dSCarlo Caione (i * sizeof(u16))); 414db33c77dSCarlo Caione patch_offset = get_unaligned_le32(patch_offset_base + 415db33c77dSCarlo Caione (i * sizeof(u32))); 416db33c77dSCarlo Caione break; 417db33c77dSCarlo Caione } 418db33c77dSCarlo Caione } 419db33c77dSCarlo Caione 420db33c77dSCarlo Caione if (!patch_offset) { 421a5c76e67SHans de Goede rtl_dev_err(hdev, "didn't find patch for chip id %d", 422a5c76e67SHans de Goede btrtl_dev->rom_version); 423db33c77dSCarlo Caione return -EINVAL; 424db33c77dSCarlo Caione } 425db33c77dSCarlo Caione 426db33c77dSCarlo Caione BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); 427db33c77dSCarlo Caione min_size = patch_offset + patch_length; 42826503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 429db33c77dSCarlo Caione return -EINVAL; 430db33c77dSCarlo Caione 431db33c77dSCarlo Caione /* Copy the firmware into a new buffer and write the version at 432db33c77dSCarlo Caione * the end. 433db33c77dSCarlo Caione */ 434db33c77dSCarlo Caione len = patch_length; 435268d3636SMaxim Mikityanskiy buf = kvmalloc(patch_length, GFP_KERNEL); 436db33c77dSCarlo Caione if (!buf) 437db33c77dSCarlo Caione return -ENOMEM; 438db33c77dSCarlo Caione 439268d3636SMaxim Mikityanskiy memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4); 440db33c77dSCarlo Caione memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); 441db33c77dSCarlo Caione 442db33c77dSCarlo Caione *_buf = buf; 443db33c77dSCarlo Caione return len; 444db33c77dSCarlo Caione } 445db33c77dSCarlo Caione 446db33c77dSCarlo Caione static int rtl_download_firmware(struct hci_dev *hdev, 447db33c77dSCarlo Caione const unsigned char *data, int fw_len) 448db33c77dSCarlo Caione { 449db33c77dSCarlo Caione struct rtl_download_cmd *dl_cmd; 450db33c77dSCarlo Caione int frag_num = fw_len / RTL_FRAG_LEN + 1; 451db33c77dSCarlo Caione int frag_len = RTL_FRAG_LEN; 452db33c77dSCarlo Caione int ret = 0; 453db33c77dSCarlo Caione int i; 454240b64a8SAlex Lu struct sk_buff *skb; 455240b64a8SAlex Lu struct hci_rp_read_local_version *rp; 456db33c77dSCarlo Caione 457db33c77dSCarlo Caione dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); 458db33c77dSCarlo Caione if (!dl_cmd) 459db33c77dSCarlo Caione return -ENOMEM; 460db33c77dSCarlo Caione 461db33c77dSCarlo Caione for (i = 0; i < frag_num; i++) { 462db33c77dSCarlo Caione struct sk_buff *skb; 463db33c77dSCarlo Caione 464db33c77dSCarlo Caione BT_DBG("download fw (%d/%d)", i, frag_num); 465db33c77dSCarlo Caione 466cf0d9a70SMax Chou if (i > 0x7f) 467cf0d9a70SMax Chou dl_cmd->index = (i & 0x7f) + 1; 468cf0d9a70SMax Chou else 469db33c77dSCarlo Caione dl_cmd->index = i; 470cf0d9a70SMax Chou 471db33c77dSCarlo Caione if (i == (frag_num - 1)) { 472db33c77dSCarlo Caione dl_cmd->index |= 0x80; /* data end */ 473db33c77dSCarlo Caione frag_len = fw_len % RTL_FRAG_LEN; 474db33c77dSCarlo Caione } 475db33c77dSCarlo Caione memcpy(dl_cmd->data, data, frag_len); 476db33c77dSCarlo Caione 477db33c77dSCarlo Caione /* Send download command */ 478db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, 479db33c77dSCarlo Caione HCI_INIT_TIMEOUT); 480db33c77dSCarlo Caione if (IS_ERR(skb)) { 481f1300c03SAlex Lu rtl_dev_err(hdev, "download fw command failed (%ld)", 482a5c76e67SHans de Goede PTR_ERR(skb)); 483d171dfb6SMax Chou ret = PTR_ERR(skb); 484db33c77dSCarlo Caione goto out; 485db33c77dSCarlo Caione } 486db33c77dSCarlo Caione 487db33c77dSCarlo Caione if (skb->len != sizeof(struct rtl_download_response)) { 488f1300c03SAlex Lu rtl_dev_err(hdev, "download fw event length mismatch"); 489db33c77dSCarlo Caione kfree_skb(skb); 490db33c77dSCarlo Caione ret = -EIO; 491db33c77dSCarlo Caione goto out; 492db33c77dSCarlo Caione } 493db33c77dSCarlo Caione 494db33c77dSCarlo Caione kfree_skb(skb); 495db33c77dSCarlo Caione data += RTL_FRAG_LEN; 496db33c77dSCarlo Caione } 497db33c77dSCarlo Caione 498240b64a8SAlex Lu skb = btrtl_read_local_version(hdev); 499240b64a8SAlex Lu if (IS_ERR(skb)) { 500240b64a8SAlex Lu ret = PTR_ERR(skb); 501240b64a8SAlex Lu rtl_dev_err(hdev, "read local version failed"); 502240b64a8SAlex Lu goto out; 503240b64a8SAlex Lu } 504240b64a8SAlex Lu 505240b64a8SAlex Lu rp = (struct hci_rp_read_local_version *)skb->data; 506240b64a8SAlex Lu rtl_dev_info(hdev, "fw version 0x%04x%04x", 507240b64a8SAlex Lu __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver)); 508240b64a8SAlex Lu kfree_skb(skb); 509240b64a8SAlex Lu 510db33c77dSCarlo Caione out: 511db33c77dSCarlo Caione kfree(dl_cmd); 512db33c77dSCarlo Caione return ret; 513db33c77dSCarlo Caione } 514db33c77dSCarlo Caione 51526503ad2SMartin Blumenstingl static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) 5161110a2dbSLarry Finger { 5171110a2dbSLarry Finger const struct firmware *fw; 5181110a2dbSLarry Finger int ret; 5191110a2dbSLarry Finger 520f1300c03SAlex Lu rtl_dev_info(hdev, "loading %s", name); 5211110a2dbSLarry Finger ret = request_firmware(&fw, name, &hdev->dev); 522abed84a0SLarry Finger if (ret < 0) 5231110a2dbSLarry Finger return ret; 5241110a2dbSLarry Finger ret = fw->size; 525268d3636SMaxim Mikityanskiy *buff = kvmalloc(fw->size, GFP_KERNEL); 526268d3636SMaxim Mikityanskiy if (*buff) 527268d3636SMaxim Mikityanskiy memcpy(*buff, fw->data, ret); 528268d3636SMaxim Mikityanskiy else 529c3327bdeSDan Carpenter ret = -ENOMEM; 5301110a2dbSLarry Finger 5311110a2dbSLarry Finger release_firmware(fw); 5321110a2dbSLarry Finger 5331110a2dbSLarry Finger return ret; 5341110a2dbSLarry Finger } 5351110a2dbSLarry Finger 53626503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723a(struct hci_dev *hdev, 53726503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 538db33c77dSCarlo Caione { 53926503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 8) 54026503ad2SMartin Blumenstingl return -EINVAL; 541db33c77dSCarlo Caione 542db33c77dSCarlo Caione /* Check that the firmware doesn't have the epatch signature 543db33c77dSCarlo Caione * (which is only for RTL8723B and newer). 544db33c77dSCarlo Caione */ 54526503ad2SMartin Blumenstingl if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { 546f1300c03SAlex Lu rtl_dev_err(hdev, "unexpected EPATCH signature!"); 547907f8499SAlex Lu return -EINVAL; 548907f8499SAlex Lu } 549907f8499SAlex Lu 55026503ad2SMartin Blumenstingl return rtl_download_firmware(hdev, btrtl_dev->fw_data, 55126503ad2SMartin Blumenstingl btrtl_dev->fw_len); 552db33c77dSCarlo Caione } 553db33c77dSCarlo Caione 55426503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723b(struct hci_dev *hdev, 55526503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 55626503ad2SMartin Blumenstingl { 55726503ad2SMartin Blumenstingl unsigned char *fw_data = NULL; 55826503ad2SMartin Blumenstingl int ret; 55926503ad2SMartin Blumenstingl u8 *tbuff; 56026503ad2SMartin Blumenstingl 56126503ad2SMartin Blumenstingl ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); 562db33c77dSCarlo Caione if (ret < 0) 563db33c77dSCarlo Caione goto out; 564db33c77dSCarlo Caione 56526503ad2SMartin Blumenstingl if (btrtl_dev->cfg_len > 0) { 566268d3636SMaxim Mikityanskiy tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); 5671110a2dbSLarry Finger if (!tbuff) { 5681110a2dbSLarry Finger ret = -ENOMEM; 569db33c77dSCarlo Caione goto out; 5701110a2dbSLarry Finger } 5711110a2dbSLarry Finger 5721110a2dbSLarry Finger memcpy(tbuff, fw_data, ret); 573268d3636SMaxim Mikityanskiy kvfree(fw_data); 5741110a2dbSLarry Finger 57526503ad2SMartin Blumenstingl memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); 57626503ad2SMartin Blumenstingl ret += btrtl_dev->cfg_len; 5771110a2dbSLarry Finger 5781110a2dbSLarry Finger fw_data = tbuff; 5791110a2dbSLarry Finger } 5801110a2dbSLarry Finger 581f1300c03SAlex Lu rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret); 5821110a2dbSLarry Finger 5831110a2dbSLarry Finger ret = rtl_download_firmware(hdev, fw_data, ret); 584db33c77dSCarlo Caione 585db33c77dSCarlo Caione out: 586268d3636SMaxim Mikityanskiy kvfree(fw_data); 587db33c77dSCarlo Caione return ret; 588db33c77dSCarlo Caione } 589db33c77dSCarlo Caione 59026503ad2SMartin Blumenstingl void btrtl_free(struct btrtl_device_info *btrtl_dev) 591db33c77dSCarlo Caione { 592268d3636SMaxim Mikityanskiy kvfree(btrtl_dev->fw_data); 593268d3636SMaxim Mikityanskiy kvfree(btrtl_dev->cfg_data); 59426503ad2SMartin Blumenstingl kfree(btrtl_dev); 59526503ad2SMartin Blumenstingl } 59626503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_free); 59726503ad2SMartin Blumenstingl 5981cc194caSHans de Goede struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, 5991cc194caSHans de Goede const char *postfix) 60026503ad2SMartin Blumenstingl { 60126503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev; 602db33c77dSCarlo Caione struct sk_buff *skb; 603db33c77dSCarlo Caione struct hci_rp_read_local_version *resp; 6041cc194caSHans de Goede char cfg_name[40]; 605907f8499SAlex Lu u16 hci_rev, lmp_subver; 606c50903e3SMartin Blumenstingl u8 hci_ver; 60726503ad2SMartin Blumenstingl int ret; 6081996d9caSKai-Heng Feng u16 opcode; 6091996d9caSKai-Heng Feng u8 cmd[2]; 61026503ad2SMartin Blumenstingl 61126503ad2SMartin Blumenstingl btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); 61226503ad2SMartin Blumenstingl if (!btrtl_dev) { 61326503ad2SMartin Blumenstingl ret = -ENOMEM; 61426503ad2SMartin Blumenstingl goto err_alloc; 61526503ad2SMartin Blumenstingl } 616db33c77dSCarlo Caione 617db33c77dSCarlo Caione skb = btrtl_read_local_version(hdev); 61826503ad2SMartin Blumenstingl if (IS_ERR(skb)) { 61926503ad2SMartin Blumenstingl ret = PTR_ERR(skb); 62026503ad2SMartin Blumenstingl goto err_free; 62126503ad2SMartin Blumenstingl } 622db33c77dSCarlo Caione 623db33c77dSCarlo Caione resp = (struct hci_rp_read_local_version *)skb->data; 624f1300c03SAlex Lu rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 6252064ee33SMarcel Holtmann resp->hci_ver, resp->hci_rev, 626db33c77dSCarlo Caione resp->lmp_ver, resp->lmp_subver); 627db33c77dSCarlo Caione 628c50903e3SMartin Blumenstingl hci_ver = resp->hci_ver; 629907f8499SAlex Lu hci_rev = le16_to_cpu(resp->hci_rev); 630db33c77dSCarlo Caione lmp_subver = le16_to_cpu(resp->lmp_subver); 6311996d9caSKai-Heng Feng 632cd36742aSHilda Wu btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 633cd36742aSHilda Wu hdev->bus); 634cd36742aSHilda Wu 635cd36742aSHilda Wu if (!btrtl_dev->ic_info) 6361996d9caSKai-Heng Feng btrtl_dev->drop_fw = true; 6371996d9caSKai-Heng Feng 6381996d9caSKai-Heng Feng if (btrtl_dev->drop_fw) { 6391996d9caSKai-Heng Feng opcode = hci_opcode_pack(0x3f, 0x66); 6401996d9caSKai-Heng Feng cmd[0] = opcode & 0xff; 6411996d9caSKai-Heng Feng cmd[1] = opcode >> 8; 6421996d9caSKai-Heng Feng 6431996d9caSKai-Heng Feng skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 644f5e8e215SColin Ian King if (!skb) 6451996d9caSKai-Heng Feng goto out_free; 6461996d9caSKai-Heng Feng 6471996d9caSKai-Heng Feng skb_put_data(skb, cmd, sizeof(cmd)); 6481996d9caSKai-Heng Feng hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 6491996d9caSKai-Heng Feng 6501996d9caSKai-Heng Feng hdev->send(hdev, skb); 6511996d9caSKai-Heng Feng 6521996d9caSKai-Heng Feng /* Ensure the above vendor command is sent to controller and 6531996d9caSKai-Heng Feng * process has done. 6541996d9caSKai-Heng Feng */ 6551996d9caSKai-Heng Feng msleep(200); 6561996d9caSKai-Heng Feng 6571996d9caSKai-Heng Feng /* Read the local version again. Expect to have the vanilla 6581996d9caSKai-Heng Feng * version as cold boot. 6591996d9caSKai-Heng Feng */ 6601996d9caSKai-Heng Feng skb = btrtl_read_local_version(hdev); 6611996d9caSKai-Heng Feng if (IS_ERR(skb)) { 6621996d9caSKai-Heng Feng ret = PTR_ERR(skb); 6631996d9caSKai-Heng Feng goto err_free; 6641996d9caSKai-Heng Feng } 6651996d9caSKai-Heng Feng 6661996d9caSKai-Heng Feng resp = (struct hci_rp_read_local_version *)skb->data; 6671996d9caSKai-Heng Feng rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 6681996d9caSKai-Heng Feng resp->hci_ver, resp->hci_rev, 6691996d9caSKai-Heng Feng resp->lmp_ver, resp->lmp_subver); 6701996d9caSKai-Heng Feng 6711996d9caSKai-Heng Feng hci_ver = resp->hci_ver; 6721996d9caSKai-Heng Feng hci_rev = le16_to_cpu(resp->hci_rev); 6731996d9caSKai-Heng Feng lmp_subver = le16_to_cpu(resp->lmp_subver); 674db33c77dSCarlo Caione 675c50903e3SMartin Blumenstingl btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 676c50903e3SMartin Blumenstingl hdev->bus); 677cd36742aSHilda Wu } 678cd36742aSHilda Wu out_free: 679cd36742aSHilda Wu kfree_skb(skb); 680c50903e3SMartin Blumenstingl 68126503ad2SMartin Blumenstingl if (!btrtl_dev->ic_info) { 682d182215dSAlex Lu rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", 683c50903e3SMartin Blumenstingl lmp_subver, hci_rev, hci_ver); 68400df214bSKai-Heng Feng return btrtl_dev; 68526503ad2SMartin Blumenstingl } 68626503ad2SMartin Blumenstingl 68726503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->has_rom_version) { 68826503ad2SMartin Blumenstingl ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); 68926503ad2SMartin Blumenstingl if (ret) 69026503ad2SMartin Blumenstingl goto err_free; 69126503ad2SMartin Blumenstingl } 69226503ad2SMartin Blumenstingl 69326503ad2SMartin Blumenstingl btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, 69426503ad2SMartin Blumenstingl &btrtl_dev->fw_data); 69526503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 0) { 696f1300c03SAlex Lu rtl_dev_err(hdev, "firmware file %s not found", 69726503ad2SMartin Blumenstingl btrtl_dev->ic_info->fw_name); 69826503ad2SMartin Blumenstingl ret = btrtl_dev->fw_len; 69926503ad2SMartin Blumenstingl goto err_free; 70026503ad2SMartin Blumenstingl } 70126503ad2SMartin Blumenstingl 70226503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->cfg_name) { 7031cc194caSHans de Goede if (postfix) { 7041cc194caSHans de Goede snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin", 7051cc194caSHans de Goede btrtl_dev->ic_info->cfg_name, postfix); 7061cc194caSHans de Goede } else { 7071cc194caSHans de Goede snprintf(cfg_name, sizeof(cfg_name), "%s.bin", 7081cc194caSHans de Goede btrtl_dev->ic_info->cfg_name); 7091cc194caSHans de Goede } 7101cc194caSHans de Goede btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name, 71126503ad2SMartin Blumenstingl &btrtl_dev->cfg_data); 71226503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->config_needed && 71326503ad2SMartin Blumenstingl btrtl_dev->cfg_len <= 0) { 714f1300c03SAlex Lu rtl_dev_err(hdev, "mandatory config file %s not found", 71526503ad2SMartin Blumenstingl btrtl_dev->ic_info->cfg_name); 71626503ad2SMartin Blumenstingl ret = btrtl_dev->cfg_len; 71726503ad2SMartin Blumenstingl goto err_free; 71826503ad2SMartin Blumenstingl } 71926503ad2SMartin Blumenstingl } 72026503ad2SMartin Blumenstingl 7217f6a750aSArchie Pusaka /* The following chips supports the Microsoft vendor extension, 7227f6a750aSArchie Pusaka * therefore set the corresponding VsMsftOpCode. 723673fae14SMiao-chen Chou */ 724f4bcba0eSMarcel Holtmann if (btrtl_dev->ic_info->has_msft_ext) 725673fae14SMiao-chen Chou hci_set_msft_opcode(hdev, 0xFCF0); 726673fae14SMiao-chen Chou 72726503ad2SMartin Blumenstingl return btrtl_dev; 72826503ad2SMartin Blumenstingl 72926503ad2SMartin Blumenstingl err_free: 73026503ad2SMartin Blumenstingl btrtl_free(btrtl_dev); 73126503ad2SMartin Blumenstingl err_alloc: 73226503ad2SMartin Blumenstingl return ERR_PTR(ret); 73326503ad2SMartin Blumenstingl } 73426503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_initialize); 73526503ad2SMartin Blumenstingl 73626503ad2SMartin Blumenstingl int btrtl_download_firmware(struct hci_dev *hdev, 73726503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 73826503ad2SMartin Blumenstingl { 739db33c77dSCarlo Caione /* Match a set of subver values that correspond to stock firmware, 740db33c77dSCarlo Caione * which is not compatible with standard btusb. 741db33c77dSCarlo Caione * If matched, upload an alternative firmware that does conform to 742db33c77dSCarlo Caione * standard btusb. Once that firmware is uploaded, the subver changes 743db33c77dSCarlo Caione * to a different value. 744db33c77dSCarlo Caione */ 74500df214bSKai-Heng Feng if (!btrtl_dev->ic_info) { 746f1300c03SAlex Lu rtl_dev_info(hdev, "assuming no firmware upload needed"); 74700df214bSKai-Heng Feng return 0; 74800df214bSKai-Heng Feng } 74900df214bSKai-Heng Feng 75026503ad2SMartin Blumenstingl switch (btrtl_dev->ic_info->lmp_subver) { 751db33c77dSCarlo Caione case RTL_ROM_LMP_8723A: 75226503ad2SMartin Blumenstingl return btrtl_setup_rtl8723a(hdev, btrtl_dev); 753db33c77dSCarlo Caione case RTL_ROM_LMP_8723B: 754db33c77dSCarlo Caione case RTL_ROM_LMP_8821A: 755db33c77dSCarlo Caione case RTL_ROM_LMP_8761A: 7561110a2dbSLarry Finger case RTL_ROM_LMP_8822B: 7570d484db6SMax Chou case RTL_ROM_LMP_8852A: 75826503ad2SMartin Blumenstingl return btrtl_setup_rtl8723b(hdev, btrtl_dev); 759db33c77dSCarlo Caione default: 760f1300c03SAlex Lu rtl_dev_info(hdev, "assuming no firmware upload needed"); 761db33c77dSCarlo Caione return 0; 762db33c77dSCarlo Caione } 763db33c77dSCarlo Caione } 76426503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_download_firmware); 76526503ad2SMartin Blumenstingl 7663011faa2SArchie Pusaka void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev) 76726503ad2SMartin Blumenstingl { 76865251e2eSAlex Lu /* Enable controller to do both LE scan and BR/EDR inquiry 76965251e2eSAlex Lu * simultaneously. 77065251e2eSAlex Lu */ 77165251e2eSAlex Lu set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 77265251e2eSAlex Lu 77305672a2cSAbhishek Pandit-Subedi /* Enable central-peripheral role (able to create new connections with 77405672a2cSAbhishek Pandit-Subedi * an existing connection in slave role). 77505672a2cSAbhishek Pandit-Subedi */ 7769ab9235fSMax Chou /* Enable WBS supported for the specific Realtek devices. */ 7779ab9235fSMax Chou switch (btrtl_dev->project_id) { 7789ab9235fSMax Chou case CHIP_ID_8822C: 7799ab9235fSMax Chou case CHIP_ID_8852A: 78018e8055cSMax Chou case CHIP_ID_8852B: 7818b1d66b5SMax Chou case CHIP_ID_8852C: 78205672a2cSAbhishek Pandit-Subedi set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 7839ab9235fSMax Chou set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); 784*a479e713SHilda Wu 785*a479e713SHilda Wu /* RTL8852C needs to transmit mSBC data continuously without 786*a479e713SHilda Wu * the zero length of USB packets for the ALT 6 supported chips 787*a479e713SHilda Wu */ 788*a479e713SHilda Wu if (btrtl_dev->project_id == CHIP_ID_8852C) 789*a479e713SHilda Wu btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP); 790*a479e713SHilda Wu 791099c6d31SJoseph Hwang hci_set_aosp_capable(hdev); 79205672a2cSAbhishek Pandit-Subedi break; 79305672a2cSAbhishek Pandit-Subedi default: 79405672a2cSAbhishek Pandit-Subedi rtl_dev_dbg(hdev, "Central-peripheral role not enabled."); 7959ab9235fSMax Chou rtl_dev_dbg(hdev, "WBS supported not enabled."); 79605672a2cSAbhishek Pandit-Subedi break; 79705672a2cSAbhishek Pandit-Subedi } 7983011faa2SArchie Pusaka } 7993011faa2SArchie Pusaka EXPORT_SYMBOL_GPL(btrtl_set_quirks); 8003011faa2SArchie Pusaka 8013011faa2SArchie Pusaka int btrtl_setup_realtek(struct hci_dev *hdev) 8023011faa2SArchie Pusaka { 8033011faa2SArchie Pusaka struct btrtl_device_info *btrtl_dev; 8043011faa2SArchie Pusaka int ret; 8053011faa2SArchie Pusaka 8063011faa2SArchie Pusaka btrtl_dev = btrtl_initialize(hdev, NULL); 8073011faa2SArchie Pusaka if (IS_ERR(btrtl_dev)) 8083011faa2SArchie Pusaka return PTR_ERR(btrtl_dev); 8093011faa2SArchie Pusaka 8103011faa2SArchie Pusaka ret = btrtl_download_firmware(hdev, btrtl_dev); 8113011faa2SArchie Pusaka 8123011faa2SArchie Pusaka btrtl_set_quirks(hdev, btrtl_dev); 81305672a2cSAbhishek Pandit-Subedi 81405672a2cSAbhishek Pandit-Subedi btrtl_free(btrtl_dev); 81526503ad2SMartin Blumenstingl return ret; 81626503ad2SMartin Blumenstingl } 817db33c77dSCarlo Caione EXPORT_SYMBOL_GPL(btrtl_setup_realtek); 818db33c77dSCarlo Caione 8197af3f558SJian-Hong Pan int btrtl_shutdown_realtek(struct hci_dev *hdev) 8207af3f558SJian-Hong Pan { 8217af3f558SJian-Hong Pan struct sk_buff *skb; 8227af3f558SJian-Hong Pan int ret; 8237af3f558SJian-Hong Pan 8247af3f558SJian-Hong Pan /* According to the vendor driver, BT must be reset on close to avoid 8257af3f558SJian-Hong Pan * firmware crash. 8267af3f558SJian-Hong Pan */ 8277af3f558SJian-Hong Pan skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 8287af3f558SJian-Hong Pan if (IS_ERR(skb)) { 8297af3f558SJian-Hong Pan ret = PTR_ERR(skb); 8307af3f558SJian-Hong Pan bt_dev_err(hdev, "HCI reset during shutdown failed"); 8317af3f558SJian-Hong Pan return ret; 8327af3f558SJian-Hong Pan } 8337af3f558SJian-Hong Pan kfree_skb(skb); 8347af3f558SJian-Hong Pan 8357af3f558SJian-Hong Pan return 0; 8367af3f558SJian-Hong Pan } 8377af3f558SJian-Hong Pan EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); 8387af3f558SJian-Hong Pan 839b85b0ee1SMartin Blumenstingl static unsigned int btrtl_convert_baudrate(u32 device_baudrate) 840b85b0ee1SMartin Blumenstingl { 841b85b0ee1SMartin Blumenstingl switch (device_baudrate) { 842b85b0ee1SMartin Blumenstingl case 0x0252a00a: 843b85b0ee1SMartin Blumenstingl return 230400; 844b85b0ee1SMartin Blumenstingl 845b85b0ee1SMartin Blumenstingl case 0x05f75004: 846b85b0ee1SMartin Blumenstingl return 921600; 847b85b0ee1SMartin Blumenstingl 848b85b0ee1SMartin Blumenstingl case 0x00005004: 849b85b0ee1SMartin Blumenstingl return 1000000; 850b85b0ee1SMartin Blumenstingl 851b85b0ee1SMartin Blumenstingl case 0x04928002: 852b85b0ee1SMartin Blumenstingl case 0x01128002: 853b85b0ee1SMartin Blumenstingl return 1500000; 854b85b0ee1SMartin Blumenstingl 855b85b0ee1SMartin Blumenstingl case 0x00005002: 856b85b0ee1SMartin Blumenstingl return 2000000; 857b85b0ee1SMartin Blumenstingl 858b85b0ee1SMartin Blumenstingl case 0x0000b001: 859b85b0ee1SMartin Blumenstingl return 2500000; 860b85b0ee1SMartin Blumenstingl 861b85b0ee1SMartin Blumenstingl case 0x04928001: 862b85b0ee1SMartin Blumenstingl return 3000000; 863b85b0ee1SMartin Blumenstingl 864b85b0ee1SMartin Blumenstingl case 0x052a6001: 865b85b0ee1SMartin Blumenstingl return 3500000; 866b85b0ee1SMartin Blumenstingl 867b85b0ee1SMartin Blumenstingl case 0x00005001: 868b85b0ee1SMartin Blumenstingl return 4000000; 869b85b0ee1SMartin Blumenstingl 870b85b0ee1SMartin Blumenstingl case 0x0252c014: 871b85b0ee1SMartin Blumenstingl default: 872b85b0ee1SMartin Blumenstingl return 115200; 873b85b0ee1SMartin Blumenstingl } 874b85b0ee1SMartin Blumenstingl } 875b85b0ee1SMartin Blumenstingl 876b85b0ee1SMartin Blumenstingl int btrtl_get_uart_settings(struct hci_dev *hdev, 877b85b0ee1SMartin Blumenstingl struct btrtl_device_info *btrtl_dev, 878b85b0ee1SMartin Blumenstingl unsigned int *controller_baudrate, 879b85b0ee1SMartin Blumenstingl u32 *device_baudrate, bool *flow_control) 880b85b0ee1SMartin Blumenstingl { 881b85b0ee1SMartin Blumenstingl struct rtl_vendor_config *config; 882b85b0ee1SMartin Blumenstingl struct rtl_vendor_config_entry *entry; 883b85b0ee1SMartin Blumenstingl int i, total_data_len; 884b85b0ee1SMartin Blumenstingl bool found = false; 885b85b0ee1SMartin Blumenstingl 886b85b0ee1SMartin Blumenstingl total_data_len = btrtl_dev->cfg_len - sizeof(*config); 887b85b0ee1SMartin Blumenstingl if (total_data_len <= 0) { 888f1300c03SAlex Lu rtl_dev_warn(hdev, "no config loaded"); 889b85b0ee1SMartin Blumenstingl return -EINVAL; 890b85b0ee1SMartin Blumenstingl } 891b85b0ee1SMartin Blumenstingl 892b85b0ee1SMartin Blumenstingl config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; 893b85b0ee1SMartin Blumenstingl if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { 894f1300c03SAlex Lu rtl_dev_err(hdev, "invalid config magic"); 895b85b0ee1SMartin Blumenstingl return -EINVAL; 896b85b0ee1SMartin Blumenstingl } 897b85b0ee1SMartin Blumenstingl 898b85b0ee1SMartin Blumenstingl if (total_data_len < le16_to_cpu(config->total_len)) { 899f1300c03SAlex Lu rtl_dev_err(hdev, "config is too short"); 900b85b0ee1SMartin Blumenstingl return -EINVAL; 901b85b0ee1SMartin Blumenstingl } 902b85b0ee1SMartin Blumenstingl 903b85b0ee1SMartin Blumenstingl for (i = 0; i < total_data_len; ) { 904b85b0ee1SMartin Blumenstingl entry = ((void *)config->entry) + i; 905b85b0ee1SMartin Blumenstingl 906b85b0ee1SMartin Blumenstingl switch (le16_to_cpu(entry->offset)) { 907b85b0ee1SMartin Blumenstingl case 0xc: 908b85b0ee1SMartin Blumenstingl if (entry->len < sizeof(*device_baudrate)) { 909f1300c03SAlex Lu rtl_dev_err(hdev, "invalid UART config entry"); 910b85b0ee1SMartin Blumenstingl return -EINVAL; 911b85b0ee1SMartin Blumenstingl } 912b85b0ee1SMartin Blumenstingl 913b85b0ee1SMartin Blumenstingl *device_baudrate = get_unaligned_le32(entry->data); 914b85b0ee1SMartin Blumenstingl *controller_baudrate = btrtl_convert_baudrate( 915b85b0ee1SMartin Blumenstingl *device_baudrate); 916b85b0ee1SMartin Blumenstingl 917b85b0ee1SMartin Blumenstingl if (entry->len >= 13) 918b85b0ee1SMartin Blumenstingl *flow_control = !!(entry->data[12] & BIT(2)); 919b85b0ee1SMartin Blumenstingl else 920b85b0ee1SMartin Blumenstingl *flow_control = false; 921b85b0ee1SMartin Blumenstingl 922b85b0ee1SMartin Blumenstingl found = true; 923b85b0ee1SMartin Blumenstingl break; 924b85b0ee1SMartin Blumenstingl 925b85b0ee1SMartin Blumenstingl default: 926f1300c03SAlex Lu rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)", 927b85b0ee1SMartin Blumenstingl le16_to_cpu(entry->offset), entry->len); 928b85b0ee1SMartin Blumenstingl break; 929515d6798SYueHaibing } 930b85b0ee1SMartin Blumenstingl 931b85b0ee1SMartin Blumenstingl i += sizeof(*entry) + entry->len; 932b85b0ee1SMartin Blumenstingl } 933b85b0ee1SMartin Blumenstingl 934b85b0ee1SMartin Blumenstingl if (!found) { 935f1300c03SAlex Lu rtl_dev_err(hdev, "no UART config entry found"); 936b85b0ee1SMartin Blumenstingl return -ENOENT; 937b85b0ee1SMartin Blumenstingl } 938b85b0ee1SMartin Blumenstingl 939f1300c03SAlex Lu rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate); 940f1300c03SAlex Lu rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate); 941f1300c03SAlex Lu rtl_dev_dbg(hdev, "flow control %d", *flow_control); 942b85b0ee1SMartin Blumenstingl 943b85b0ee1SMartin Blumenstingl return 0; 944b85b0ee1SMartin Blumenstingl } 945b85b0ee1SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); 946b85b0ee1SMartin Blumenstingl 947db33c77dSCarlo Caione MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); 948db33c77dSCarlo Caione MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); 949db33c77dSCarlo Caione MODULE_VERSION(VERSION); 950db33c77dSCarlo Caione MODULE_LICENSE("GPL"); 951f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); 952f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); 953f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); 954c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); 955c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); 956c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); 957c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); 958f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); 959f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); 960f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); 961f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); 962f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); 963f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); 9640d484db6SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin"); 9650d484db6SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin"); 96618e8055cSMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin"); 96718e8055cSMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin"); 9688b1d66b5SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin"); 9698b1d66b5SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin"); 970