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 193*fe4b71b5SVictor Hassan /* 8852B with UART interface */ 194*fe4b71b5SVictor Hassan { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_UART), 195*fe4b71b5SVictor Hassan .config_needed = true, 196*fe4b71b5SVictor Hassan .has_rom_version = true, 197*fe4b71b5SVictor Hassan .has_msft_ext = true, 198*fe4b71b5SVictor Hassan .fw_name = "rtl_bt/rtl8852bs_fw.bin", 199*fe4b71b5SVictor Hassan .cfg_name = "rtl_bt/rtl8852bs_config" }, 200*fe4b71b5SVictor Hassan 20118e8055cSMax Chou /* 8852B */ 20218e8055cSMax Chou { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB), 20318e8055cSMax Chou .config_needed = false, 20418e8055cSMax Chou .has_rom_version = true, 20518e8055cSMax Chou .has_msft_ext = true, 20618e8055cSMax Chou .fw_name = "rtl_bt/rtl8852bu_fw.bin", 20718e8055cSMax Chou .cfg_name = "rtl_bt/rtl8852bu_config" }, 2088b1d66b5SMax Chou 2098b1d66b5SMax Chou /* 8852C */ 2108b1d66b5SMax Chou { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB), 2118b1d66b5SMax Chou .config_needed = false, 2128b1d66b5SMax Chou .has_rom_version = true, 2138b1d66b5SMax Chou .has_msft_ext = true, 2148b1d66b5SMax Chou .fw_name = "rtl_bt/rtl8852cu_fw.bin", 2158b1d66b5SMax Chou .cfg_name = "rtl_bt/rtl8852cu_config" }, 216907f8499SAlex Lu }; 217907f8499SAlex Lu 218c50903e3SMartin Blumenstingl static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev, 219c50903e3SMartin Blumenstingl u8 hci_ver, u8 hci_bus) 22026503ad2SMartin Blumenstingl { 22126503ad2SMartin Blumenstingl int i; 22226503ad2SMartin Blumenstingl 22326503ad2SMartin Blumenstingl for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { 22426503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && 22526503ad2SMartin Blumenstingl (ic_id_table[i].lmp_subver != lmp_subver)) 22626503ad2SMartin Blumenstingl continue; 22726503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && 22826503ad2SMartin Blumenstingl (ic_id_table[i].hci_rev != hci_rev)) 22926503ad2SMartin Blumenstingl continue; 230c50903e3SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) && 231c50903e3SMartin Blumenstingl (ic_id_table[i].hci_ver != hci_ver)) 232c50903e3SMartin Blumenstingl continue; 233c50903e3SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) && 234c50903e3SMartin Blumenstingl (ic_id_table[i].hci_bus != hci_bus)) 235c50903e3SMartin Blumenstingl continue; 23626503ad2SMartin Blumenstingl 23726503ad2SMartin Blumenstingl break; 23826503ad2SMartin Blumenstingl } 23926503ad2SMartin Blumenstingl if (i >= ARRAY_SIZE(ic_id_table)) 24026503ad2SMartin Blumenstingl return NULL; 24126503ad2SMartin Blumenstingl 24226503ad2SMartin Blumenstingl return &ic_id_table[i]; 24326503ad2SMartin Blumenstingl } 24426503ad2SMartin Blumenstingl 245240b64a8SAlex Lu static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) 246240b64a8SAlex Lu { 247240b64a8SAlex Lu struct sk_buff *skb; 248240b64a8SAlex Lu 249240b64a8SAlex Lu skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 250240b64a8SAlex Lu HCI_INIT_TIMEOUT); 251240b64a8SAlex Lu if (IS_ERR(skb)) { 252240b64a8SAlex Lu rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)", 253240b64a8SAlex Lu PTR_ERR(skb)); 254240b64a8SAlex Lu return skb; 255240b64a8SAlex Lu } 256240b64a8SAlex Lu 257240b64a8SAlex Lu if (skb->len != sizeof(struct hci_rp_read_local_version)) { 258240b64a8SAlex Lu rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch"); 259240b64a8SAlex Lu kfree_skb(skb); 260240b64a8SAlex Lu return ERR_PTR(-EIO); 261240b64a8SAlex Lu } 262240b64a8SAlex Lu 263240b64a8SAlex Lu return skb; 264240b64a8SAlex Lu } 265240b64a8SAlex Lu 266db33c77dSCarlo Caione static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) 267db33c77dSCarlo Caione { 268db33c77dSCarlo Caione struct rtl_rom_version_evt *rom_version; 269db33c77dSCarlo Caione struct sk_buff *skb; 270db33c77dSCarlo Caione 271db33c77dSCarlo Caione /* Read RTL ROM version command */ 272db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); 273db33c77dSCarlo Caione if (IS_ERR(skb)) { 274f1300c03SAlex Lu rtl_dev_err(hdev, "Read ROM version failed (%ld)", 275a5c76e67SHans de Goede PTR_ERR(skb)); 276db33c77dSCarlo Caione return PTR_ERR(skb); 277db33c77dSCarlo Caione } 278db33c77dSCarlo Caione 279db33c77dSCarlo Caione if (skb->len != sizeof(*rom_version)) { 280f1300c03SAlex Lu rtl_dev_err(hdev, "version event length mismatch"); 281db33c77dSCarlo Caione kfree_skb(skb); 282db33c77dSCarlo Caione return -EIO; 283db33c77dSCarlo Caione } 284db33c77dSCarlo Caione 285db33c77dSCarlo Caione rom_version = (struct rtl_rom_version_evt *)skb->data; 286f1300c03SAlex Lu rtl_dev_info(hdev, "rom_version status=%x version=%x", 2872064ee33SMarcel Holtmann rom_version->status, rom_version->version); 288db33c77dSCarlo Caione 289db33c77dSCarlo Caione *version = rom_version->version; 290db33c77dSCarlo Caione 291db33c77dSCarlo Caione kfree_skb(skb); 292db33c77dSCarlo Caione return 0; 293db33c77dSCarlo Caione } 294db33c77dSCarlo Caione 29526503ad2SMartin Blumenstingl static int rtlbt_parse_firmware(struct hci_dev *hdev, 29626503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev, 297db33c77dSCarlo Caione unsigned char **_buf) 298db33c77dSCarlo Caione { 299e5070e07SColin Ian King static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; 300db33c77dSCarlo Caione struct rtl_epatch_header *epatch_info; 301db33c77dSCarlo Caione unsigned char *buf; 30226503ad2SMartin Blumenstingl int i, len; 303db33c77dSCarlo Caione size_t min_size; 30426503ad2SMartin Blumenstingl u8 opcode, length, data; 305db33c77dSCarlo Caione int project_id = -1; 306db33c77dSCarlo Caione const unsigned char *fwptr, *chip_id_base; 307db33c77dSCarlo Caione const unsigned char *patch_length_base, *patch_offset_base; 308db33c77dSCarlo Caione u32 patch_offset = 0; 309db33c77dSCarlo Caione u16 patch_length, num_patches; 3101110a2dbSLarry Finger static const struct { 3111110a2dbSLarry Finger __u16 lmp_subver; 3121110a2dbSLarry Finger __u8 id; 3131110a2dbSLarry Finger } project_id_to_lmp_subver[] = { 3141110a2dbSLarry Finger { RTL_ROM_LMP_8723A, 0 }, 3151110a2dbSLarry Finger { RTL_ROM_LMP_8723B, 1 }, 3161110a2dbSLarry Finger { RTL_ROM_LMP_8821A, 2 }, 3171110a2dbSLarry Finger { RTL_ROM_LMP_8761A, 3 }, 3181110a2dbSLarry Finger { RTL_ROM_LMP_8822B, 8 }, 319907f8499SAlex Lu { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ 320907f8499SAlex Lu { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ 3218ecfdc95SAlex Lu { RTL_ROM_LMP_8822B, 13 }, /* 8822C */ 32204896832SZiqian SUN (Zamir) { RTL_ROM_LMP_8761A, 14 }, /* 8761B */ 3230d484db6SMax Chou { RTL_ROM_LMP_8852A, 18 }, /* 8852A */ 32418e8055cSMax Chou { RTL_ROM_LMP_8852A, 20 }, /* 8852B */ 3258b1d66b5SMax Chou { RTL_ROM_LMP_8852A, 25 }, /* 8852C */ 326db33c77dSCarlo Caione }; 327db33c77dSCarlo Caione 328db33c77dSCarlo Caione min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; 32926503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 330db33c77dSCarlo Caione return -EINVAL; 331db33c77dSCarlo Caione 33226503ad2SMartin Blumenstingl fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); 333db33c77dSCarlo Caione if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { 334f1300c03SAlex Lu rtl_dev_err(hdev, "extension section signature mismatch"); 335db33c77dSCarlo Caione return -EINVAL; 336db33c77dSCarlo Caione } 337db33c77dSCarlo Caione 338db33c77dSCarlo Caione /* Loop from the end of the firmware parsing instructions, until 339db33c77dSCarlo Caione * we find an instruction that identifies the "project ID" for the 340db33c77dSCarlo Caione * hardware supported by this firwmare file. 34138f230f1Sshaomin Deng * Once we have that, we double-check that project_id is suitable 342db33c77dSCarlo Caione * for the hardware we are working with. 343db33c77dSCarlo Caione */ 34426503ad2SMartin Blumenstingl while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { 345db33c77dSCarlo Caione opcode = *--fwptr; 346db33c77dSCarlo Caione length = *--fwptr; 347db33c77dSCarlo Caione data = *--fwptr; 348db33c77dSCarlo Caione 349db33c77dSCarlo Caione BT_DBG("check op=%x len=%x data=%x", opcode, length, data); 350db33c77dSCarlo Caione 351db33c77dSCarlo Caione if (opcode == 0xff) /* EOF */ 352db33c77dSCarlo Caione break; 353db33c77dSCarlo Caione 354db33c77dSCarlo Caione if (length == 0) { 355f1300c03SAlex Lu rtl_dev_err(hdev, "found instruction with length 0"); 356db33c77dSCarlo Caione return -EINVAL; 357db33c77dSCarlo Caione } 358db33c77dSCarlo Caione 359db33c77dSCarlo Caione if (opcode == 0 && length == 1) { 360db33c77dSCarlo Caione project_id = data; 361db33c77dSCarlo Caione break; 362db33c77dSCarlo Caione } 363db33c77dSCarlo Caione 364db33c77dSCarlo Caione fwptr -= length; 365db33c77dSCarlo Caione } 366db33c77dSCarlo Caione 367db33c77dSCarlo Caione if (project_id < 0) { 368f1300c03SAlex Lu rtl_dev_err(hdev, "failed to find version instruction"); 369db33c77dSCarlo Caione return -EINVAL; 370db33c77dSCarlo Caione } 371db33c77dSCarlo Caione 3721110a2dbSLarry Finger /* Find project_id in table */ 3731110a2dbSLarry Finger for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { 3749ab9235fSMax Chou if (project_id == project_id_to_lmp_subver[i].id) { 3759ab9235fSMax Chou btrtl_dev->project_id = project_id; 3761110a2dbSLarry Finger break; 3771110a2dbSLarry Finger } 3789ab9235fSMax Chou } 3791110a2dbSLarry Finger 3801110a2dbSLarry Finger if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { 381f1300c03SAlex Lu rtl_dev_err(hdev, "unknown project id %d", project_id); 382db33c77dSCarlo Caione return -EINVAL; 383db33c77dSCarlo Caione } 384db33c77dSCarlo Caione 38526503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->lmp_subver != 38626503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver) { 387f1300c03SAlex Lu rtl_dev_err(hdev, "firmware is for %x but this is a %x", 38826503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver, 38926503ad2SMartin Blumenstingl btrtl_dev->ic_info->lmp_subver); 390db33c77dSCarlo Caione return -EINVAL; 391db33c77dSCarlo Caione } 392db33c77dSCarlo Caione 39326503ad2SMartin Blumenstingl epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; 394db33c77dSCarlo Caione if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { 395f1300c03SAlex Lu rtl_dev_err(hdev, "bad EPATCH signature"); 396db33c77dSCarlo Caione return -EINVAL; 397db33c77dSCarlo Caione } 398db33c77dSCarlo Caione 399db33c77dSCarlo Caione num_patches = le16_to_cpu(epatch_info->num_patches); 400db33c77dSCarlo Caione BT_DBG("fw_version=%x, num_patches=%d", 401db33c77dSCarlo Caione le32_to_cpu(epatch_info->fw_version), num_patches); 402db33c77dSCarlo Caione 403db33c77dSCarlo Caione /* After the rtl_epatch_header there is a funky patch metadata section. 404db33c77dSCarlo Caione * Assuming 2 patches, the layout is: 405db33c77dSCarlo Caione * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 406db33c77dSCarlo Caione * 407db33c77dSCarlo Caione * Find the right patch for this chip. 408db33c77dSCarlo Caione */ 409db33c77dSCarlo Caione min_size += 8 * num_patches; 41026503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 411db33c77dSCarlo Caione return -EINVAL; 412db33c77dSCarlo Caione 41326503ad2SMartin Blumenstingl chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); 414db33c77dSCarlo Caione patch_length_base = chip_id_base + (sizeof(u16) * num_patches); 415db33c77dSCarlo Caione patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); 416db33c77dSCarlo Caione for (i = 0; i < num_patches; i++) { 417db33c77dSCarlo Caione u16 chip_id = get_unaligned_le16(chip_id_base + 418db33c77dSCarlo Caione (i * sizeof(u16))); 41926503ad2SMartin Blumenstingl if (chip_id == btrtl_dev->rom_version + 1) { 420db33c77dSCarlo Caione patch_length = get_unaligned_le16(patch_length_base + 421db33c77dSCarlo Caione (i * sizeof(u16))); 422db33c77dSCarlo Caione patch_offset = get_unaligned_le32(patch_offset_base + 423db33c77dSCarlo Caione (i * sizeof(u32))); 424db33c77dSCarlo Caione break; 425db33c77dSCarlo Caione } 426db33c77dSCarlo Caione } 427db33c77dSCarlo Caione 428db33c77dSCarlo Caione if (!patch_offset) { 429a5c76e67SHans de Goede rtl_dev_err(hdev, "didn't find patch for chip id %d", 430a5c76e67SHans de Goede btrtl_dev->rom_version); 431db33c77dSCarlo Caione return -EINVAL; 432db33c77dSCarlo Caione } 433db33c77dSCarlo Caione 434db33c77dSCarlo Caione BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); 435db33c77dSCarlo Caione min_size = patch_offset + patch_length; 43626503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 437db33c77dSCarlo Caione return -EINVAL; 438db33c77dSCarlo Caione 439db33c77dSCarlo Caione /* Copy the firmware into a new buffer and write the version at 440db33c77dSCarlo Caione * the end. 441db33c77dSCarlo Caione */ 442db33c77dSCarlo Caione len = patch_length; 443268d3636SMaxim Mikityanskiy buf = kvmalloc(patch_length, GFP_KERNEL); 444db33c77dSCarlo Caione if (!buf) 445db33c77dSCarlo Caione return -ENOMEM; 446db33c77dSCarlo Caione 447268d3636SMaxim Mikityanskiy memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4); 448db33c77dSCarlo Caione memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); 449db33c77dSCarlo Caione 450db33c77dSCarlo Caione *_buf = buf; 451db33c77dSCarlo Caione return len; 452db33c77dSCarlo Caione } 453db33c77dSCarlo Caione 454db33c77dSCarlo Caione static int rtl_download_firmware(struct hci_dev *hdev, 455db33c77dSCarlo Caione const unsigned char *data, int fw_len) 456db33c77dSCarlo Caione { 457db33c77dSCarlo Caione struct rtl_download_cmd *dl_cmd; 458db33c77dSCarlo Caione int frag_num = fw_len / RTL_FRAG_LEN + 1; 459db33c77dSCarlo Caione int frag_len = RTL_FRAG_LEN; 460db33c77dSCarlo Caione int ret = 0; 461db33c77dSCarlo Caione int i; 462240b64a8SAlex Lu struct sk_buff *skb; 463240b64a8SAlex Lu struct hci_rp_read_local_version *rp; 464db33c77dSCarlo Caione 465db33c77dSCarlo Caione dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); 466db33c77dSCarlo Caione if (!dl_cmd) 467db33c77dSCarlo Caione return -ENOMEM; 468db33c77dSCarlo Caione 469db33c77dSCarlo Caione for (i = 0; i < frag_num; i++) { 470db33c77dSCarlo Caione struct sk_buff *skb; 471db33c77dSCarlo Caione 472db33c77dSCarlo Caione BT_DBG("download fw (%d/%d)", i, frag_num); 473db33c77dSCarlo Caione 474cf0d9a70SMax Chou if (i > 0x7f) 475cf0d9a70SMax Chou dl_cmd->index = (i & 0x7f) + 1; 476cf0d9a70SMax Chou else 477db33c77dSCarlo Caione dl_cmd->index = i; 478cf0d9a70SMax Chou 479db33c77dSCarlo Caione if (i == (frag_num - 1)) { 480db33c77dSCarlo Caione dl_cmd->index |= 0x80; /* data end */ 481db33c77dSCarlo Caione frag_len = fw_len % RTL_FRAG_LEN; 482db33c77dSCarlo Caione } 483db33c77dSCarlo Caione memcpy(dl_cmd->data, data, frag_len); 484db33c77dSCarlo Caione 485db33c77dSCarlo Caione /* Send download command */ 486db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, 487db33c77dSCarlo Caione HCI_INIT_TIMEOUT); 488db33c77dSCarlo Caione if (IS_ERR(skb)) { 489f1300c03SAlex Lu rtl_dev_err(hdev, "download fw command failed (%ld)", 490a5c76e67SHans de Goede PTR_ERR(skb)); 491d171dfb6SMax Chou ret = PTR_ERR(skb); 492db33c77dSCarlo Caione goto out; 493db33c77dSCarlo Caione } 494db33c77dSCarlo Caione 495db33c77dSCarlo Caione if (skb->len != sizeof(struct rtl_download_response)) { 496f1300c03SAlex Lu rtl_dev_err(hdev, "download fw event length mismatch"); 497db33c77dSCarlo Caione kfree_skb(skb); 498db33c77dSCarlo Caione ret = -EIO; 499db33c77dSCarlo Caione goto out; 500db33c77dSCarlo Caione } 501db33c77dSCarlo Caione 502db33c77dSCarlo Caione kfree_skb(skb); 503db33c77dSCarlo Caione data += RTL_FRAG_LEN; 504db33c77dSCarlo Caione } 505db33c77dSCarlo Caione 506240b64a8SAlex Lu skb = btrtl_read_local_version(hdev); 507240b64a8SAlex Lu if (IS_ERR(skb)) { 508240b64a8SAlex Lu ret = PTR_ERR(skb); 509240b64a8SAlex Lu rtl_dev_err(hdev, "read local version failed"); 510240b64a8SAlex Lu goto out; 511240b64a8SAlex Lu } 512240b64a8SAlex Lu 513240b64a8SAlex Lu rp = (struct hci_rp_read_local_version *)skb->data; 514240b64a8SAlex Lu rtl_dev_info(hdev, "fw version 0x%04x%04x", 515240b64a8SAlex Lu __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver)); 516240b64a8SAlex Lu kfree_skb(skb); 517240b64a8SAlex Lu 518db33c77dSCarlo Caione out: 519db33c77dSCarlo Caione kfree(dl_cmd); 520db33c77dSCarlo Caione return ret; 521db33c77dSCarlo Caione } 522db33c77dSCarlo Caione 52326503ad2SMartin Blumenstingl static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) 5241110a2dbSLarry Finger { 5251110a2dbSLarry Finger const struct firmware *fw; 5261110a2dbSLarry Finger int ret; 5271110a2dbSLarry Finger 528f1300c03SAlex Lu rtl_dev_info(hdev, "loading %s", name); 5291110a2dbSLarry Finger ret = request_firmware(&fw, name, &hdev->dev); 530abed84a0SLarry Finger if (ret < 0) 5311110a2dbSLarry Finger return ret; 5321110a2dbSLarry Finger ret = fw->size; 533268d3636SMaxim Mikityanskiy *buff = kvmalloc(fw->size, GFP_KERNEL); 534268d3636SMaxim Mikityanskiy if (*buff) 535268d3636SMaxim Mikityanskiy memcpy(*buff, fw->data, ret); 536268d3636SMaxim Mikityanskiy else 537c3327bdeSDan Carpenter ret = -ENOMEM; 5381110a2dbSLarry Finger 5391110a2dbSLarry Finger release_firmware(fw); 5401110a2dbSLarry Finger 5411110a2dbSLarry Finger return ret; 5421110a2dbSLarry Finger } 5431110a2dbSLarry Finger 54426503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723a(struct hci_dev *hdev, 54526503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 546db33c77dSCarlo Caione { 54726503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 8) 54826503ad2SMartin Blumenstingl return -EINVAL; 549db33c77dSCarlo Caione 550db33c77dSCarlo Caione /* Check that the firmware doesn't have the epatch signature 551db33c77dSCarlo Caione * (which is only for RTL8723B and newer). 552db33c77dSCarlo Caione */ 55326503ad2SMartin Blumenstingl if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { 554f1300c03SAlex Lu rtl_dev_err(hdev, "unexpected EPATCH signature!"); 555907f8499SAlex Lu return -EINVAL; 556907f8499SAlex Lu } 557907f8499SAlex Lu 55826503ad2SMartin Blumenstingl return rtl_download_firmware(hdev, btrtl_dev->fw_data, 55926503ad2SMartin Blumenstingl btrtl_dev->fw_len); 560db33c77dSCarlo Caione } 561db33c77dSCarlo Caione 56226503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723b(struct hci_dev *hdev, 56326503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 56426503ad2SMartin Blumenstingl { 56526503ad2SMartin Blumenstingl unsigned char *fw_data = NULL; 56626503ad2SMartin Blumenstingl int ret; 56726503ad2SMartin Blumenstingl u8 *tbuff; 56826503ad2SMartin Blumenstingl 56926503ad2SMartin Blumenstingl ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); 570db33c77dSCarlo Caione if (ret < 0) 571db33c77dSCarlo Caione goto out; 572db33c77dSCarlo Caione 57326503ad2SMartin Blumenstingl if (btrtl_dev->cfg_len > 0) { 574268d3636SMaxim Mikityanskiy tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); 5751110a2dbSLarry Finger if (!tbuff) { 5761110a2dbSLarry Finger ret = -ENOMEM; 577db33c77dSCarlo Caione goto out; 5781110a2dbSLarry Finger } 5791110a2dbSLarry Finger 5801110a2dbSLarry Finger memcpy(tbuff, fw_data, ret); 581268d3636SMaxim Mikityanskiy kvfree(fw_data); 5821110a2dbSLarry Finger 58326503ad2SMartin Blumenstingl memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); 58426503ad2SMartin Blumenstingl ret += btrtl_dev->cfg_len; 5851110a2dbSLarry Finger 5861110a2dbSLarry Finger fw_data = tbuff; 5871110a2dbSLarry Finger } 5881110a2dbSLarry Finger 589f1300c03SAlex Lu rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret); 5901110a2dbSLarry Finger 5911110a2dbSLarry Finger ret = rtl_download_firmware(hdev, fw_data, ret); 592db33c77dSCarlo Caione 593db33c77dSCarlo Caione out: 594268d3636SMaxim Mikityanskiy kvfree(fw_data); 595db33c77dSCarlo Caione return ret; 596db33c77dSCarlo Caione } 597db33c77dSCarlo Caione 59826503ad2SMartin Blumenstingl void btrtl_free(struct btrtl_device_info *btrtl_dev) 599db33c77dSCarlo Caione { 600268d3636SMaxim Mikityanskiy kvfree(btrtl_dev->fw_data); 601268d3636SMaxim Mikityanskiy kvfree(btrtl_dev->cfg_data); 60226503ad2SMartin Blumenstingl kfree(btrtl_dev); 60326503ad2SMartin Blumenstingl } 60426503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_free); 60526503ad2SMartin Blumenstingl 6061cc194caSHans de Goede struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev, 6071cc194caSHans de Goede const char *postfix) 60826503ad2SMartin Blumenstingl { 60926503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev; 610db33c77dSCarlo Caione struct sk_buff *skb; 611db33c77dSCarlo Caione struct hci_rp_read_local_version *resp; 6121cc194caSHans de Goede char cfg_name[40]; 613907f8499SAlex Lu u16 hci_rev, lmp_subver; 614c50903e3SMartin Blumenstingl u8 hci_ver; 61526503ad2SMartin Blumenstingl int ret; 6161996d9caSKai-Heng Feng u16 opcode; 6171996d9caSKai-Heng Feng u8 cmd[2]; 61826503ad2SMartin Blumenstingl 61926503ad2SMartin Blumenstingl btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); 62026503ad2SMartin Blumenstingl if (!btrtl_dev) { 62126503ad2SMartin Blumenstingl ret = -ENOMEM; 62226503ad2SMartin Blumenstingl goto err_alloc; 62326503ad2SMartin Blumenstingl } 624db33c77dSCarlo Caione 625db33c77dSCarlo Caione skb = btrtl_read_local_version(hdev); 62626503ad2SMartin Blumenstingl if (IS_ERR(skb)) { 62726503ad2SMartin Blumenstingl ret = PTR_ERR(skb); 62826503ad2SMartin Blumenstingl goto err_free; 62926503ad2SMartin Blumenstingl } 630db33c77dSCarlo Caione 631db33c77dSCarlo Caione resp = (struct hci_rp_read_local_version *)skb->data; 632f1300c03SAlex Lu rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 6332064ee33SMarcel Holtmann resp->hci_ver, resp->hci_rev, 634db33c77dSCarlo Caione resp->lmp_ver, resp->lmp_subver); 635db33c77dSCarlo Caione 636c50903e3SMartin Blumenstingl hci_ver = resp->hci_ver; 637907f8499SAlex Lu hci_rev = le16_to_cpu(resp->hci_rev); 638db33c77dSCarlo Caione lmp_subver = le16_to_cpu(resp->lmp_subver); 6391996d9caSKai-Heng Feng 640cd36742aSHilda Wu btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 641cd36742aSHilda Wu hdev->bus); 642cd36742aSHilda Wu 643cd36742aSHilda Wu if (!btrtl_dev->ic_info) 6441996d9caSKai-Heng Feng btrtl_dev->drop_fw = true; 6451996d9caSKai-Heng Feng 6461996d9caSKai-Heng Feng if (btrtl_dev->drop_fw) { 6471996d9caSKai-Heng Feng opcode = hci_opcode_pack(0x3f, 0x66); 6481996d9caSKai-Heng Feng cmd[0] = opcode & 0xff; 6491996d9caSKai-Heng Feng cmd[1] = opcode >> 8; 6501996d9caSKai-Heng Feng 6511996d9caSKai-Heng Feng skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); 652f5e8e215SColin Ian King if (!skb) 6531996d9caSKai-Heng Feng goto out_free; 6541996d9caSKai-Heng Feng 6551996d9caSKai-Heng Feng skb_put_data(skb, cmd, sizeof(cmd)); 6561996d9caSKai-Heng Feng hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 6571996d9caSKai-Heng Feng 6581996d9caSKai-Heng Feng hdev->send(hdev, skb); 6591996d9caSKai-Heng Feng 6601996d9caSKai-Heng Feng /* Ensure the above vendor command is sent to controller and 6611996d9caSKai-Heng Feng * process has done. 6621996d9caSKai-Heng Feng */ 6631996d9caSKai-Heng Feng msleep(200); 6641996d9caSKai-Heng Feng 6651996d9caSKai-Heng Feng /* Read the local version again. Expect to have the vanilla 6661996d9caSKai-Heng Feng * version as cold boot. 6671996d9caSKai-Heng Feng */ 6681996d9caSKai-Heng Feng skb = btrtl_read_local_version(hdev); 6691996d9caSKai-Heng Feng if (IS_ERR(skb)) { 6701996d9caSKai-Heng Feng ret = PTR_ERR(skb); 6711996d9caSKai-Heng Feng goto err_free; 6721996d9caSKai-Heng Feng } 6731996d9caSKai-Heng Feng 6741996d9caSKai-Heng Feng resp = (struct hci_rp_read_local_version *)skb->data; 6751996d9caSKai-Heng Feng rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x", 6761996d9caSKai-Heng Feng resp->hci_ver, resp->hci_rev, 6771996d9caSKai-Heng Feng resp->lmp_ver, resp->lmp_subver); 6781996d9caSKai-Heng Feng 6791996d9caSKai-Heng Feng hci_ver = resp->hci_ver; 6801996d9caSKai-Heng Feng hci_rev = le16_to_cpu(resp->hci_rev); 6811996d9caSKai-Heng Feng lmp_subver = le16_to_cpu(resp->lmp_subver); 682db33c77dSCarlo Caione 683c50903e3SMartin Blumenstingl btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver, 684c50903e3SMartin Blumenstingl hdev->bus); 685cd36742aSHilda Wu } 686cd36742aSHilda Wu out_free: 687cd36742aSHilda Wu kfree_skb(skb); 688c50903e3SMartin Blumenstingl 68926503ad2SMartin Blumenstingl if (!btrtl_dev->ic_info) { 690d182215dSAlex Lu rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x", 691c50903e3SMartin Blumenstingl lmp_subver, hci_rev, hci_ver); 69200df214bSKai-Heng Feng return btrtl_dev; 69326503ad2SMartin Blumenstingl } 69426503ad2SMartin Blumenstingl 69526503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->has_rom_version) { 69626503ad2SMartin Blumenstingl ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); 69726503ad2SMartin Blumenstingl if (ret) 69826503ad2SMartin Blumenstingl goto err_free; 69926503ad2SMartin Blumenstingl } 70026503ad2SMartin Blumenstingl 70126503ad2SMartin Blumenstingl btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, 70226503ad2SMartin Blumenstingl &btrtl_dev->fw_data); 70326503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 0) { 704f1300c03SAlex Lu rtl_dev_err(hdev, "firmware file %s not found", 70526503ad2SMartin Blumenstingl btrtl_dev->ic_info->fw_name); 70626503ad2SMartin Blumenstingl ret = btrtl_dev->fw_len; 70726503ad2SMartin Blumenstingl goto err_free; 70826503ad2SMartin Blumenstingl } 70926503ad2SMartin Blumenstingl 71026503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->cfg_name) { 7111cc194caSHans de Goede if (postfix) { 7121cc194caSHans de Goede snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin", 7131cc194caSHans de Goede btrtl_dev->ic_info->cfg_name, postfix); 7141cc194caSHans de Goede } else { 7151cc194caSHans de Goede snprintf(cfg_name, sizeof(cfg_name), "%s.bin", 7161cc194caSHans de Goede btrtl_dev->ic_info->cfg_name); 7171cc194caSHans de Goede } 7181cc194caSHans de Goede btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name, 71926503ad2SMartin Blumenstingl &btrtl_dev->cfg_data); 72026503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->config_needed && 72126503ad2SMartin Blumenstingl btrtl_dev->cfg_len <= 0) { 722f1300c03SAlex Lu rtl_dev_err(hdev, "mandatory config file %s not found", 72326503ad2SMartin Blumenstingl btrtl_dev->ic_info->cfg_name); 72426503ad2SMartin Blumenstingl ret = btrtl_dev->cfg_len; 72526503ad2SMartin Blumenstingl goto err_free; 72626503ad2SMartin Blumenstingl } 72726503ad2SMartin Blumenstingl } 72826503ad2SMartin Blumenstingl 7297f6a750aSArchie Pusaka /* The following chips supports the Microsoft vendor extension, 7307f6a750aSArchie Pusaka * therefore set the corresponding VsMsftOpCode. 731673fae14SMiao-chen Chou */ 732f4bcba0eSMarcel Holtmann if (btrtl_dev->ic_info->has_msft_ext) 733673fae14SMiao-chen Chou hci_set_msft_opcode(hdev, 0xFCF0); 734673fae14SMiao-chen Chou 73526503ad2SMartin Blumenstingl return btrtl_dev; 73626503ad2SMartin Blumenstingl 73726503ad2SMartin Blumenstingl err_free: 73826503ad2SMartin Blumenstingl btrtl_free(btrtl_dev); 73926503ad2SMartin Blumenstingl err_alloc: 74026503ad2SMartin Blumenstingl return ERR_PTR(ret); 74126503ad2SMartin Blumenstingl } 74226503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_initialize); 74326503ad2SMartin Blumenstingl 74426503ad2SMartin Blumenstingl int btrtl_download_firmware(struct hci_dev *hdev, 74526503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 74626503ad2SMartin Blumenstingl { 747db33c77dSCarlo Caione /* Match a set of subver values that correspond to stock firmware, 748db33c77dSCarlo Caione * which is not compatible with standard btusb. 749db33c77dSCarlo Caione * If matched, upload an alternative firmware that does conform to 750db33c77dSCarlo Caione * standard btusb. Once that firmware is uploaded, the subver changes 751db33c77dSCarlo Caione * to a different value. 752db33c77dSCarlo Caione */ 75300df214bSKai-Heng Feng if (!btrtl_dev->ic_info) { 754f1300c03SAlex Lu rtl_dev_info(hdev, "assuming no firmware upload needed"); 75500df214bSKai-Heng Feng return 0; 75600df214bSKai-Heng Feng } 75700df214bSKai-Heng Feng 75826503ad2SMartin Blumenstingl switch (btrtl_dev->ic_info->lmp_subver) { 759db33c77dSCarlo Caione case RTL_ROM_LMP_8723A: 76026503ad2SMartin Blumenstingl return btrtl_setup_rtl8723a(hdev, btrtl_dev); 761db33c77dSCarlo Caione case RTL_ROM_LMP_8723B: 762db33c77dSCarlo Caione case RTL_ROM_LMP_8821A: 763db33c77dSCarlo Caione case RTL_ROM_LMP_8761A: 7641110a2dbSLarry Finger case RTL_ROM_LMP_8822B: 7650d484db6SMax Chou case RTL_ROM_LMP_8852A: 76626503ad2SMartin Blumenstingl return btrtl_setup_rtl8723b(hdev, btrtl_dev); 767db33c77dSCarlo Caione default: 768f1300c03SAlex Lu rtl_dev_info(hdev, "assuming no firmware upload needed"); 769db33c77dSCarlo Caione return 0; 770db33c77dSCarlo Caione } 771db33c77dSCarlo Caione } 77226503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_download_firmware); 77326503ad2SMartin Blumenstingl 7743011faa2SArchie Pusaka void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev) 77526503ad2SMartin Blumenstingl { 77665251e2eSAlex Lu /* Enable controller to do both LE scan and BR/EDR inquiry 77765251e2eSAlex Lu * simultaneously. 77865251e2eSAlex Lu */ 77965251e2eSAlex Lu set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks); 78065251e2eSAlex Lu 78105672a2cSAbhishek Pandit-Subedi /* Enable central-peripheral role (able to create new connections with 78205672a2cSAbhishek Pandit-Subedi * an existing connection in slave role). 78305672a2cSAbhishek Pandit-Subedi */ 7849ab9235fSMax Chou /* Enable WBS supported for the specific Realtek devices. */ 7859ab9235fSMax Chou switch (btrtl_dev->project_id) { 7869ab9235fSMax Chou case CHIP_ID_8822C: 7879ab9235fSMax Chou case CHIP_ID_8852A: 78818e8055cSMax Chou case CHIP_ID_8852B: 7898b1d66b5SMax Chou case CHIP_ID_8852C: 79005672a2cSAbhishek Pandit-Subedi set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks); 7919ab9235fSMax Chou set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks); 792a479e713SHilda Wu 793a479e713SHilda Wu /* RTL8852C needs to transmit mSBC data continuously without 794a479e713SHilda Wu * the zero length of USB packets for the ALT 6 supported chips 795a479e713SHilda Wu */ 796a479e713SHilda Wu if (btrtl_dev->project_id == CHIP_ID_8852C) 797a479e713SHilda Wu btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP); 798a479e713SHilda Wu 799099c6d31SJoseph Hwang hci_set_aosp_capable(hdev); 80005672a2cSAbhishek Pandit-Subedi break; 80105672a2cSAbhishek Pandit-Subedi default: 80205672a2cSAbhishek Pandit-Subedi rtl_dev_dbg(hdev, "Central-peripheral role not enabled."); 8039ab9235fSMax Chou rtl_dev_dbg(hdev, "WBS supported not enabled."); 80405672a2cSAbhishek Pandit-Subedi break; 80505672a2cSAbhishek Pandit-Subedi } 8063011faa2SArchie Pusaka } 8073011faa2SArchie Pusaka EXPORT_SYMBOL_GPL(btrtl_set_quirks); 8083011faa2SArchie Pusaka 8093011faa2SArchie Pusaka int btrtl_setup_realtek(struct hci_dev *hdev) 8103011faa2SArchie Pusaka { 8113011faa2SArchie Pusaka struct btrtl_device_info *btrtl_dev; 8123011faa2SArchie Pusaka int ret; 8133011faa2SArchie Pusaka 8143011faa2SArchie Pusaka btrtl_dev = btrtl_initialize(hdev, NULL); 8153011faa2SArchie Pusaka if (IS_ERR(btrtl_dev)) 8163011faa2SArchie Pusaka return PTR_ERR(btrtl_dev); 8173011faa2SArchie Pusaka 8183011faa2SArchie Pusaka ret = btrtl_download_firmware(hdev, btrtl_dev); 8193011faa2SArchie Pusaka 8203011faa2SArchie Pusaka btrtl_set_quirks(hdev, btrtl_dev); 82105672a2cSAbhishek Pandit-Subedi 82205672a2cSAbhishek Pandit-Subedi btrtl_free(btrtl_dev); 82326503ad2SMartin Blumenstingl return ret; 82426503ad2SMartin Blumenstingl } 825db33c77dSCarlo Caione EXPORT_SYMBOL_GPL(btrtl_setup_realtek); 826db33c77dSCarlo Caione 8277af3f558SJian-Hong Pan int btrtl_shutdown_realtek(struct hci_dev *hdev) 8287af3f558SJian-Hong Pan { 8297af3f558SJian-Hong Pan struct sk_buff *skb; 8307af3f558SJian-Hong Pan int ret; 8317af3f558SJian-Hong Pan 8327af3f558SJian-Hong Pan /* According to the vendor driver, BT must be reset on close to avoid 8337af3f558SJian-Hong Pan * firmware crash. 8347af3f558SJian-Hong Pan */ 8357af3f558SJian-Hong Pan skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); 8367af3f558SJian-Hong Pan if (IS_ERR(skb)) { 8377af3f558SJian-Hong Pan ret = PTR_ERR(skb); 8387af3f558SJian-Hong Pan bt_dev_err(hdev, "HCI reset during shutdown failed"); 8397af3f558SJian-Hong Pan return ret; 8407af3f558SJian-Hong Pan } 8417af3f558SJian-Hong Pan kfree_skb(skb); 8427af3f558SJian-Hong Pan 8437af3f558SJian-Hong Pan return 0; 8447af3f558SJian-Hong Pan } 8457af3f558SJian-Hong Pan EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek); 8467af3f558SJian-Hong Pan 847b85b0ee1SMartin Blumenstingl static unsigned int btrtl_convert_baudrate(u32 device_baudrate) 848b85b0ee1SMartin Blumenstingl { 849b85b0ee1SMartin Blumenstingl switch (device_baudrate) { 850b85b0ee1SMartin Blumenstingl case 0x0252a00a: 851b85b0ee1SMartin Blumenstingl return 230400; 852b85b0ee1SMartin Blumenstingl 853b85b0ee1SMartin Blumenstingl case 0x05f75004: 854b85b0ee1SMartin Blumenstingl return 921600; 855b85b0ee1SMartin Blumenstingl 856b85b0ee1SMartin Blumenstingl case 0x00005004: 857b85b0ee1SMartin Blumenstingl return 1000000; 858b85b0ee1SMartin Blumenstingl 859b85b0ee1SMartin Blumenstingl case 0x04928002: 860b85b0ee1SMartin Blumenstingl case 0x01128002: 861b85b0ee1SMartin Blumenstingl return 1500000; 862b85b0ee1SMartin Blumenstingl 863b85b0ee1SMartin Blumenstingl case 0x00005002: 864b85b0ee1SMartin Blumenstingl return 2000000; 865b85b0ee1SMartin Blumenstingl 866b85b0ee1SMartin Blumenstingl case 0x0000b001: 867b85b0ee1SMartin Blumenstingl return 2500000; 868b85b0ee1SMartin Blumenstingl 869b85b0ee1SMartin Blumenstingl case 0x04928001: 870b85b0ee1SMartin Blumenstingl return 3000000; 871b85b0ee1SMartin Blumenstingl 872b85b0ee1SMartin Blumenstingl case 0x052a6001: 873b85b0ee1SMartin Blumenstingl return 3500000; 874b85b0ee1SMartin Blumenstingl 875b85b0ee1SMartin Blumenstingl case 0x00005001: 876b85b0ee1SMartin Blumenstingl return 4000000; 877b85b0ee1SMartin Blumenstingl 878b85b0ee1SMartin Blumenstingl case 0x0252c014: 879b85b0ee1SMartin Blumenstingl default: 880b85b0ee1SMartin Blumenstingl return 115200; 881b85b0ee1SMartin Blumenstingl } 882b85b0ee1SMartin Blumenstingl } 883b85b0ee1SMartin Blumenstingl 884b85b0ee1SMartin Blumenstingl int btrtl_get_uart_settings(struct hci_dev *hdev, 885b85b0ee1SMartin Blumenstingl struct btrtl_device_info *btrtl_dev, 886b85b0ee1SMartin Blumenstingl unsigned int *controller_baudrate, 887b85b0ee1SMartin Blumenstingl u32 *device_baudrate, bool *flow_control) 888b85b0ee1SMartin Blumenstingl { 889b85b0ee1SMartin Blumenstingl struct rtl_vendor_config *config; 890b85b0ee1SMartin Blumenstingl struct rtl_vendor_config_entry *entry; 891b85b0ee1SMartin Blumenstingl int i, total_data_len; 892b85b0ee1SMartin Blumenstingl bool found = false; 893b85b0ee1SMartin Blumenstingl 894b85b0ee1SMartin Blumenstingl total_data_len = btrtl_dev->cfg_len - sizeof(*config); 895b85b0ee1SMartin Blumenstingl if (total_data_len <= 0) { 896f1300c03SAlex Lu rtl_dev_warn(hdev, "no config loaded"); 897b85b0ee1SMartin Blumenstingl return -EINVAL; 898b85b0ee1SMartin Blumenstingl } 899b85b0ee1SMartin Blumenstingl 900b85b0ee1SMartin Blumenstingl config = (struct rtl_vendor_config *)btrtl_dev->cfg_data; 901b85b0ee1SMartin Blumenstingl if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) { 902f1300c03SAlex Lu rtl_dev_err(hdev, "invalid config magic"); 903b85b0ee1SMartin Blumenstingl return -EINVAL; 904b85b0ee1SMartin Blumenstingl } 905b85b0ee1SMartin Blumenstingl 906b85b0ee1SMartin Blumenstingl if (total_data_len < le16_to_cpu(config->total_len)) { 907f1300c03SAlex Lu rtl_dev_err(hdev, "config is too short"); 908b85b0ee1SMartin Blumenstingl return -EINVAL; 909b85b0ee1SMartin Blumenstingl } 910b85b0ee1SMartin Blumenstingl 911b85b0ee1SMartin Blumenstingl for (i = 0; i < total_data_len; ) { 912b85b0ee1SMartin Blumenstingl entry = ((void *)config->entry) + i; 913b85b0ee1SMartin Blumenstingl 914b85b0ee1SMartin Blumenstingl switch (le16_to_cpu(entry->offset)) { 915b85b0ee1SMartin Blumenstingl case 0xc: 916b85b0ee1SMartin Blumenstingl if (entry->len < sizeof(*device_baudrate)) { 917f1300c03SAlex Lu rtl_dev_err(hdev, "invalid UART config entry"); 918b85b0ee1SMartin Blumenstingl return -EINVAL; 919b85b0ee1SMartin Blumenstingl } 920b85b0ee1SMartin Blumenstingl 921b85b0ee1SMartin Blumenstingl *device_baudrate = get_unaligned_le32(entry->data); 922b85b0ee1SMartin Blumenstingl *controller_baudrate = btrtl_convert_baudrate( 923b85b0ee1SMartin Blumenstingl *device_baudrate); 924b85b0ee1SMartin Blumenstingl 925b85b0ee1SMartin Blumenstingl if (entry->len >= 13) 926b85b0ee1SMartin Blumenstingl *flow_control = !!(entry->data[12] & BIT(2)); 927b85b0ee1SMartin Blumenstingl else 928b85b0ee1SMartin Blumenstingl *flow_control = false; 929b85b0ee1SMartin Blumenstingl 930b85b0ee1SMartin Blumenstingl found = true; 931b85b0ee1SMartin Blumenstingl break; 932b85b0ee1SMartin Blumenstingl 933b85b0ee1SMartin Blumenstingl default: 934f1300c03SAlex Lu rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)", 935b85b0ee1SMartin Blumenstingl le16_to_cpu(entry->offset), entry->len); 936b85b0ee1SMartin Blumenstingl break; 937515d6798SYueHaibing } 938b85b0ee1SMartin Blumenstingl 939b85b0ee1SMartin Blumenstingl i += sizeof(*entry) + entry->len; 940b85b0ee1SMartin Blumenstingl } 941b85b0ee1SMartin Blumenstingl 942b85b0ee1SMartin Blumenstingl if (!found) { 943f1300c03SAlex Lu rtl_dev_err(hdev, "no UART config entry found"); 944b85b0ee1SMartin Blumenstingl return -ENOENT; 945b85b0ee1SMartin Blumenstingl } 946b85b0ee1SMartin Blumenstingl 947f1300c03SAlex Lu rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate); 948f1300c03SAlex Lu rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate); 949f1300c03SAlex Lu rtl_dev_dbg(hdev, "flow control %d", *flow_control); 950b85b0ee1SMartin Blumenstingl 951b85b0ee1SMartin Blumenstingl return 0; 952b85b0ee1SMartin Blumenstingl } 953b85b0ee1SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_get_uart_settings); 954b85b0ee1SMartin Blumenstingl 955db33c77dSCarlo Caione MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); 956db33c77dSCarlo Caione MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); 957db33c77dSCarlo Caione MODULE_VERSION(VERSION); 958db33c77dSCarlo Caione MODULE_LICENSE("GPL"); 959f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); 960f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); 961f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); 962c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin"); 963c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin"); 964c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin"); 965c50903e3SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin"); 966f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); 967f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); 968f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); 969f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); 970f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); 971f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); 9720d484db6SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin"); 9730d484db6SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin"); 974*fe4b71b5SVictor Hassan MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin"); 975*fe4b71b5SVictor Hassan MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin"); 97618e8055cSMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin"); 97718e8055cSMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin"); 9788b1d66b5SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin"); 9798b1d66b5SMax Chou MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin"); 980