1db33c77dSCarlo Caione /* 2db33c77dSCarlo Caione * Bluetooth support for Realtek devices 3db33c77dSCarlo Caione * 4db33c77dSCarlo Caione * Copyright (C) 2015 Endless Mobile, Inc. 5db33c77dSCarlo Caione * 6db33c77dSCarlo Caione * This program is free software; you can redistribute it and/or modify 7db33c77dSCarlo Caione * it under the terms of the GNU General Public License as published by 8db33c77dSCarlo Caione * the Free Software Foundation; either version 2 of the License, or 9db33c77dSCarlo Caione * (at your option) any later version. 10db33c77dSCarlo Caione * 11db33c77dSCarlo Caione * This program is distributed in the hope that it will be useful, 12db33c77dSCarlo Caione * but WITHOUT ANY WARRANTY; without even the implied warranty of 13db33c77dSCarlo Caione * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14db33c77dSCarlo Caione * GNU General Public License for more details. 15db33c77dSCarlo Caione * 16db33c77dSCarlo Caione */ 17db33c77dSCarlo Caione 18db33c77dSCarlo Caione #include <linux/module.h> 19db33c77dSCarlo Caione #include <linux/firmware.h> 20db33c77dSCarlo Caione #include <asm/unaligned.h> 21db33c77dSCarlo Caione #include <linux/usb.h> 22db33c77dSCarlo Caione 23db33c77dSCarlo Caione #include <net/bluetooth/bluetooth.h> 24db33c77dSCarlo Caione #include <net/bluetooth/hci_core.h> 25db33c77dSCarlo Caione 26db33c77dSCarlo Caione #include "btrtl.h" 27db33c77dSCarlo Caione 28db33c77dSCarlo Caione #define VERSION "0.1" 29db33c77dSCarlo Caione 30db33c77dSCarlo Caione #define RTL_EPATCH_SIGNATURE "Realtech" 31db33c77dSCarlo Caione #define RTL_ROM_LMP_3499 0x3499 32db33c77dSCarlo Caione #define RTL_ROM_LMP_8723A 0x1200 33db33c77dSCarlo Caione #define RTL_ROM_LMP_8723B 0x8723 34db33c77dSCarlo Caione #define RTL_ROM_LMP_8821A 0x8821 35db33c77dSCarlo Caione #define RTL_ROM_LMP_8761A 0x8761 361110a2dbSLarry Finger #define RTL_ROM_LMP_8822B 0x8822 37db33c77dSCarlo Caione 38907f8499SAlex Lu #define IC_MATCH_FL_LMPSUBV (1 << 0) 39907f8499SAlex Lu #define IC_MATCH_FL_HCIREV (1 << 1) 40907f8499SAlex Lu #define IC_INFO(lmps, hcir) \ 41907f8499SAlex Lu .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \ 42907f8499SAlex Lu .lmp_subver = (lmps), \ 43907f8499SAlex Lu .hci_rev = (hcir) 44907f8499SAlex Lu 45907f8499SAlex Lu struct id_table { 46907f8499SAlex Lu __u16 match_flags; 47907f8499SAlex Lu __u16 lmp_subver; 48907f8499SAlex Lu __u16 hci_rev; 49907f8499SAlex Lu bool config_needed; 50*26503ad2SMartin Blumenstingl bool has_rom_version; 51907f8499SAlex Lu char *fw_name; 52907f8499SAlex Lu char *cfg_name; 53907f8499SAlex Lu }; 54907f8499SAlex Lu 55*26503ad2SMartin Blumenstingl struct btrtl_device_info { 56*26503ad2SMartin Blumenstingl const struct id_table *ic_info; 57*26503ad2SMartin Blumenstingl u8 rom_version; 58*26503ad2SMartin Blumenstingl u8 *fw_data; 59*26503ad2SMartin Blumenstingl int fw_len; 60*26503ad2SMartin Blumenstingl u8 *cfg_data; 61*26503ad2SMartin Blumenstingl int cfg_len; 62*26503ad2SMartin Blumenstingl }; 63*26503ad2SMartin Blumenstingl 64907f8499SAlex Lu static const struct id_table ic_id_table[] = { 65*26503ad2SMartin Blumenstingl { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0, 66*26503ad2SMartin Blumenstingl .config_needed = false, 67*26503ad2SMartin Blumenstingl .has_rom_version = false, 68*26503ad2SMartin Blumenstingl .fw_name = "rtl_bt/rtl8723a_fw.bin", 69*26503ad2SMartin Blumenstingl .cfg_name = NULL }, 70*26503ad2SMartin Blumenstingl 71*26503ad2SMartin Blumenstingl { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0, 72*26503ad2SMartin Blumenstingl .config_needed = false, 73*26503ad2SMartin Blumenstingl .has_rom_version = false, 74*26503ad2SMartin Blumenstingl .fw_name = "rtl_bt/rtl8723a_fw.bin", 75*26503ad2SMartin Blumenstingl .cfg_name = NULL }, 76*26503ad2SMartin Blumenstingl 77907f8499SAlex Lu /* 8723B */ 78907f8499SAlex Lu { IC_INFO(RTL_ROM_LMP_8723B, 0xb), 79907f8499SAlex Lu .config_needed = false, 80*26503ad2SMartin Blumenstingl .has_rom_version = true, 81907f8499SAlex Lu .fw_name = "rtl_bt/rtl8723b_fw.bin", 82907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8723b_config.bin" }, 83907f8499SAlex Lu 84907f8499SAlex Lu /* 8723D */ 85907f8499SAlex Lu { IC_INFO(RTL_ROM_LMP_8723B, 0xd), 86907f8499SAlex Lu .config_needed = true, 87*26503ad2SMartin Blumenstingl .has_rom_version = true, 88907f8499SAlex Lu .fw_name = "rtl_bt/rtl8723d_fw.bin", 89907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8723d_config.bin" }, 90907f8499SAlex Lu 91907f8499SAlex Lu /* 8821A */ 92907f8499SAlex Lu { IC_INFO(RTL_ROM_LMP_8821A, 0xa), 93907f8499SAlex Lu .config_needed = false, 94*26503ad2SMartin Blumenstingl .has_rom_version = true, 95907f8499SAlex Lu .fw_name = "rtl_bt/rtl8821a_fw.bin", 96907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8821a_config.bin" }, 97907f8499SAlex Lu 98907f8499SAlex Lu /* 8821C */ 99907f8499SAlex Lu { IC_INFO(RTL_ROM_LMP_8821A, 0xc), 100907f8499SAlex Lu .config_needed = false, 101*26503ad2SMartin Blumenstingl .has_rom_version = true, 102907f8499SAlex Lu .fw_name = "rtl_bt/rtl8821c_fw.bin", 103907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8821c_config.bin" }, 104907f8499SAlex Lu 105907f8499SAlex Lu /* 8761A */ 106907f8499SAlex Lu { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0, 107907f8499SAlex Lu .config_needed = false, 108*26503ad2SMartin Blumenstingl .has_rom_version = true, 109907f8499SAlex Lu .fw_name = "rtl_bt/rtl8761a_fw.bin", 110907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8761a_config.bin" }, 111907f8499SAlex Lu 112907f8499SAlex Lu /* 8822B */ 113907f8499SAlex Lu { IC_INFO(RTL_ROM_LMP_8822B, 0xb), 114907f8499SAlex Lu .config_needed = true, 115*26503ad2SMartin Blumenstingl .has_rom_version = true, 116907f8499SAlex Lu .fw_name = "rtl_bt/rtl8822b_fw.bin", 117907f8499SAlex Lu .cfg_name = "rtl_bt/rtl8822b_config.bin" }, 118907f8499SAlex Lu }; 119907f8499SAlex Lu 120*26503ad2SMartin Blumenstingl static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev) 121*26503ad2SMartin Blumenstingl { 122*26503ad2SMartin Blumenstingl int i; 123*26503ad2SMartin Blumenstingl 124*26503ad2SMartin Blumenstingl for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) { 125*26503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) && 126*26503ad2SMartin Blumenstingl (ic_id_table[i].lmp_subver != lmp_subver)) 127*26503ad2SMartin Blumenstingl continue; 128*26503ad2SMartin Blumenstingl if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) && 129*26503ad2SMartin Blumenstingl (ic_id_table[i].hci_rev != hci_rev)) 130*26503ad2SMartin Blumenstingl continue; 131*26503ad2SMartin Blumenstingl 132*26503ad2SMartin Blumenstingl break; 133*26503ad2SMartin Blumenstingl } 134*26503ad2SMartin Blumenstingl if (i >= ARRAY_SIZE(ic_id_table)) 135*26503ad2SMartin Blumenstingl return NULL; 136*26503ad2SMartin Blumenstingl 137*26503ad2SMartin Blumenstingl return &ic_id_table[i]; 138*26503ad2SMartin Blumenstingl } 139*26503ad2SMartin Blumenstingl 140db33c77dSCarlo Caione static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version) 141db33c77dSCarlo Caione { 142db33c77dSCarlo Caione struct rtl_rom_version_evt *rom_version; 143db33c77dSCarlo Caione struct sk_buff *skb; 144db33c77dSCarlo Caione 145db33c77dSCarlo Caione /* Read RTL ROM version command */ 146db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT); 147db33c77dSCarlo Caione if (IS_ERR(skb)) { 148db33c77dSCarlo Caione BT_ERR("%s: Read ROM version failed (%ld)", 149db33c77dSCarlo Caione hdev->name, PTR_ERR(skb)); 150db33c77dSCarlo Caione return PTR_ERR(skb); 151db33c77dSCarlo Caione } 152db33c77dSCarlo Caione 153db33c77dSCarlo Caione if (skb->len != sizeof(*rom_version)) { 154db33c77dSCarlo Caione BT_ERR("%s: RTL version event length mismatch", hdev->name); 155db33c77dSCarlo Caione kfree_skb(skb); 156db33c77dSCarlo Caione return -EIO; 157db33c77dSCarlo Caione } 158db33c77dSCarlo Caione 159db33c77dSCarlo Caione rom_version = (struct rtl_rom_version_evt *)skb->data; 1602064ee33SMarcel Holtmann bt_dev_info(hdev, "rom_version status=%x version=%x", 1612064ee33SMarcel Holtmann rom_version->status, rom_version->version); 162db33c77dSCarlo Caione 163db33c77dSCarlo Caione *version = rom_version->version; 164db33c77dSCarlo Caione 165db33c77dSCarlo Caione kfree_skb(skb); 166db33c77dSCarlo Caione return 0; 167db33c77dSCarlo Caione } 168db33c77dSCarlo Caione 169*26503ad2SMartin Blumenstingl static int rtlbt_parse_firmware(struct hci_dev *hdev, 170*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev, 171db33c77dSCarlo Caione unsigned char **_buf) 172db33c77dSCarlo Caione { 173db33c77dSCarlo Caione const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 }; 174db33c77dSCarlo Caione struct rtl_epatch_header *epatch_info; 175db33c77dSCarlo Caione unsigned char *buf; 176*26503ad2SMartin Blumenstingl int i, len; 177db33c77dSCarlo Caione size_t min_size; 178*26503ad2SMartin Blumenstingl u8 opcode, length, data; 179db33c77dSCarlo Caione int project_id = -1; 180db33c77dSCarlo Caione const unsigned char *fwptr, *chip_id_base; 181db33c77dSCarlo Caione const unsigned char *patch_length_base, *patch_offset_base; 182db33c77dSCarlo Caione u32 patch_offset = 0; 183db33c77dSCarlo Caione u16 patch_length, num_patches; 1841110a2dbSLarry Finger static const struct { 1851110a2dbSLarry Finger __u16 lmp_subver; 1861110a2dbSLarry Finger __u8 id; 1871110a2dbSLarry Finger } project_id_to_lmp_subver[] = { 1881110a2dbSLarry Finger { RTL_ROM_LMP_8723A, 0 }, 1891110a2dbSLarry Finger { RTL_ROM_LMP_8723B, 1 }, 1901110a2dbSLarry Finger { RTL_ROM_LMP_8821A, 2 }, 1911110a2dbSLarry Finger { RTL_ROM_LMP_8761A, 3 }, 1921110a2dbSLarry Finger { RTL_ROM_LMP_8822B, 8 }, 193907f8499SAlex Lu { RTL_ROM_LMP_8723B, 9 }, /* 8723D */ 194907f8499SAlex Lu { RTL_ROM_LMP_8821A, 10 }, /* 8821C */ 195db33c77dSCarlo Caione }; 196db33c77dSCarlo Caione 197db33c77dSCarlo Caione min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3; 198*26503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 199db33c77dSCarlo Caione return -EINVAL; 200db33c77dSCarlo Caione 201*26503ad2SMartin Blumenstingl fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig); 202db33c77dSCarlo Caione if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) { 203db33c77dSCarlo Caione BT_ERR("%s: extension section signature mismatch", hdev->name); 204db33c77dSCarlo Caione return -EINVAL; 205db33c77dSCarlo Caione } 206db33c77dSCarlo Caione 207db33c77dSCarlo Caione /* Loop from the end of the firmware parsing instructions, until 208db33c77dSCarlo Caione * we find an instruction that identifies the "project ID" for the 209db33c77dSCarlo Caione * hardware supported by this firwmare file. 210db33c77dSCarlo Caione * Once we have that, we double-check that that project_id is suitable 211db33c77dSCarlo Caione * for the hardware we are working with. 212db33c77dSCarlo Caione */ 213*26503ad2SMartin Blumenstingl while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) { 214db33c77dSCarlo Caione opcode = *--fwptr; 215db33c77dSCarlo Caione length = *--fwptr; 216db33c77dSCarlo Caione data = *--fwptr; 217db33c77dSCarlo Caione 218db33c77dSCarlo Caione BT_DBG("check op=%x len=%x data=%x", opcode, length, data); 219db33c77dSCarlo Caione 220db33c77dSCarlo Caione if (opcode == 0xff) /* EOF */ 221db33c77dSCarlo Caione break; 222db33c77dSCarlo Caione 223db33c77dSCarlo Caione if (length == 0) { 224db33c77dSCarlo Caione BT_ERR("%s: found instruction with length 0", 225db33c77dSCarlo Caione hdev->name); 226db33c77dSCarlo Caione return -EINVAL; 227db33c77dSCarlo Caione } 228db33c77dSCarlo Caione 229db33c77dSCarlo Caione if (opcode == 0 && length == 1) { 230db33c77dSCarlo Caione project_id = data; 231db33c77dSCarlo Caione break; 232db33c77dSCarlo Caione } 233db33c77dSCarlo Caione 234db33c77dSCarlo Caione fwptr -= length; 235db33c77dSCarlo Caione } 236db33c77dSCarlo Caione 237db33c77dSCarlo Caione if (project_id < 0) { 238db33c77dSCarlo Caione BT_ERR("%s: failed to find version instruction", hdev->name); 239db33c77dSCarlo Caione return -EINVAL; 240db33c77dSCarlo Caione } 241db33c77dSCarlo Caione 2421110a2dbSLarry Finger /* Find project_id in table */ 2431110a2dbSLarry Finger for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) { 2441110a2dbSLarry Finger if (project_id == project_id_to_lmp_subver[i].id) 2451110a2dbSLarry Finger break; 2461110a2dbSLarry Finger } 2471110a2dbSLarry Finger 2481110a2dbSLarry Finger if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) { 249db33c77dSCarlo Caione BT_ERR("%s: unknown project id %d", hdev->name, project_id); 250db33c77dSCarlo Caione return -EINVAL; 251db33c77dSCarlo Caione } 252db33c77dSCarlo Caione 253*26503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->lmp_subver != 254*26503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver) { 255db33c77dSCarlo Caione BT_ERR("%s: firmware is for %x but this is a %x", hdev->name, 256*26503ad2SMartin Blumenstingl project_id_to_lmp_subver[i].lmp_subver, 257*26503ad2SMartin Blumenstingl btrtl_dev->ic_info->lmp_subver); 258db33c77dSCarlo Caione return -EINVAL; 259db33c77dSCarlo Caione } 260db33c77dSCarlo Caione 261*26503ad2SMartin Blumenstingl epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data; 262db33c77dSCarlo Caione if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) { 263db33c77dSCarlo Caione BT_ERR("%s: bad EPATCH signature", hdev->name); 264db33c77dSCarlo Caione return -EINVAL; 265db33c77dSCarlo Caione } 266db33c77dSCarlo Caione 267db33c77dSCarlo Caione num_patches = le16_to_cpu(epatch_info->num_patches); 268db33c77dSCarlo Caione BT_DBG("fw_version=%x, num_patches=%d", 269db33c77dSCarlo Caione le32_to_cpu(epatch_info->fw_version), num_patches); 270db33c77dSCarlo Caione 271db33c77dSCarlo Caione /* After the rtl_epatch_header there is a funky patch metadata section. 272db33c77dSCarlo Caione * Assuming 2 patches, the layout is: 273db33c77dSCarlo Caione * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2 274db33c77dSCarlo Caione * 275db33c77dSCarlo Caione * Find the right patch for this chip. 276db33c77dSCarlo Caione */ 277db33c77dSCarlo Caione min_size += 8 * num_patches; 278*26503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 279db33c77dSCarlo Caione return -EINVAL; 280db33c77dSCarlo Caione 281*26503ad2SMartin Blumenstingl chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header); 282db33c77dSCarlo Caione patch_length_base = chip_id_base + (sizeof(u16) * num_patches); 283db33c77dSCarlo Caione patch_offset_base = patch_length_base + (sizeof(u16) * num_patches); 284db33c77dSCarlo Caione for (i = 0; i < num_patches; i++) { 285db33c77dSCarlo Caione u16 chip_id = get_unaligned_le16(chip_id_base + 286db33c77dSCarlo Caione (i * sizeof(u16))); 287*26503ad2SMartin Blumenstingl if (chip_id == btrtl_dev->rom_version + 1) { 288db33c77dSCarlo Caione patch_length = get_unaligned_le16(patch_length_base + 289db33c77dSCarlo Caione (i * sizeof(u16))); 290db33c77dSCarlo Caione patch_offset = get_unaligned_le32(patch_offset_base + 291db33c77dSCarlo Caione (i * sizeof(u32))); 292db33c77dSCarlo Caione break; 293db33c77dSCarlo Caione } 294db33c77dSCarlo Caione } 295db33c77dSCarlo Caione 296db33c77dSCarlo Caione if (!patch_offset) { 297db33c77dSCarlo Caione BT_ERR("%s: didn't find patch for chip id %d", 298*26503ad2SMartin Blumenstingl hdev->name, btrtl_dev->rom_version); 299db33c77dSCarlo Caione return -EINVAL; 300db33c77dSCarlo Caione } 301db33c77dSCarlo Caione 302db33c77dSCarlo Caione BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i); 303db33c77dSCarlo Caione min_size = patch_offset + patch_length; 304*26503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < min_size) 305db33c77dSCarlo Caione return -EINVAL; 306db33c77dSCarlo Caione 307db33c77dSCarlo Caione /* Copy the firmware into a new buffer and write the version at 308db33c77dSCarlo Caione * the end. 309db33c77dSCarlo Caione */ 310db33c77dSCarlo Caione len = patch_length; 311*26503ad2SMartin Blumenstingl buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length, 312*26503ad2SMartin Blumenstingl GFP_KERNEL); 313db33c77dSCarlo Caione if (!buf) 314db33c77dSCarlo Caione return -ENOMEM; 315db33c77dSCarlo Caione 316db33c77dSCarlo Caione memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4); 317db33c77dSCarlo Caione 318db33c77dSCarlo Caione *_buf = buf; 319db33c77dSCarlo Caione return len; 320db33c77dSCarlo Caione } 321db33c77dSCarlo Caione 322db33c77dSCarlo Caione static int rtl_download_firmware(struct hci_dev *hdev, 323db33c77dSCarlo Caione const unsigned char *data, int fw_len) 324db33c77dSCarlo Caione { 325db33c77dSCarlo Caione struct rtl_download_cmd *dl_cmd; 326db33c77dSCarlo Caione int frag_num = fw_len / RTL_FRAG_LEN + 1; 327db33c77dSCarlo Caione int frag_len = RTL_FRAG_LEN; 328db33c77dSCarlo Caione int ret = 0; 329db33c77dSCarlo Caione int i; 330db33c77dSCarlo Caione 331db33c77dSCarlo Caione dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL); 332db33c77dSCarlo Caione if (!dl_cmd) 333db33c77dSCarlo Caione return -ENOMEM; 334db33c77dSCarlo Caione 335db33c77dSCarlo Caione for (i = 0; i < frag_num; i++) { 336db33c77dSCarlo Caione struct sk_buff *skb; 337db33c77dSCarlo Caione 338db33c77dSCarlo Caione BT_DBG("download fw (%d/%d)", i, frag_num); 339db33c77dSCarlo Caione 340db33c77dSCarlo Caione dl_cmd->index = i; 341db33c77dSCarlo Caione if (i == (frag_num - 1)) { 342db33c77dSCarlo Caione dl_cmd->index |= 0x80; /* data end */ 343db33c77dSCarlo Caione frag_len = fw_len % RTL_FRAG_LEN; 344db33c77dSCarlo Caione } 345db33c77dSCarlo Caione memcpy(dl_cmd->data, data, frag_len); 346db33c77dSCarlo Caione 347db33c77dSCarlo Caione /* Send download command */ 348db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd, 349db33c77dSCarlo Caione HCI_INIT_TIMEOUT); 350db33c77dSCarlo Caione if (IS_ERR(skb)) { 351db33c77dSCarlo Caione BT_ERR("%s: download fw command failed (%ld)", 352db33c77dSCarlo Caione hdev->name, PTR_ERR(skb)); 353db33c77dSCarlo Caione ret = -PTR_ERR(skb); 354db33c77dSCarlo Caione goto out; 355db33c77dSCarlo Caione } 356db33c77dSCarlo Caione 357db33c77dSCarlo Caione if (skb->len != sizeof(struct rtl_download_response)) { 358db33c77dSCarlo Caione BT_ERR("%s: download fw event length mismatch", 359db33c77dSCarlo Caione hdev->name); 360db33c77dSCarlo Caione kfree_skb(skb); 361db33c77dSCarlo Caione ret = -EIO; 362db33c77dSCarlo Caione goto out; 363db33c77dSCarlo Caione } 364db33c77dSCarlo Caione 365db33c77dSCarlo Caione kfree_skb(skb); 366db33c77dSCarlo Caione data += RTL_FRAG_LEN; 367db33c77dSCarlo Caione } 368db33c77dSCarlo Caione 369db33c77dSCarlo Caione out: 370db33c77dSCarlo Caione kfree(dl_cmd); 371db33c77dSCarlo Caione return ret; 372db33c77dSCarlo Caione } 373db33c77dSCarlo Caione 374*26503ad2SMartin Blumenstingl static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff) 3751110a2dbSLarry Finger { 3761110a2dbSLarry Finger const struct firmware *fw; 3771110a2dbSLarry Finger int ret; 3781110a2dbSLarry Finger 3792064ee33SMarcel Holtmann bt_dev_info(hdev, "rtl: loading %s", name); 3801110a2dbSLarry Finger ret = request_firmware(&fw, name, &hdev->dev); 381abed84a0SLarry Finger if (ret < 0) 3821110a2dbSLarry Finger return ret; 3831110a2dbSLarry Finger ret = fw->size; 3841110a2dbSLarry Finger *buff = kmemdup(fw->data, ret, GFP_KERNEL); 385c3327bdeSDan Carpenter if (!*buff) 386c3327bdeSDan Carpenter ret = -ENOMEM; 3871110a2dbSLarry Finger 3881110a2dbSLarry Finger release_firmware(fw); 3891110a2dbSLarry Finger 3901110a2dbSLarry Finger return ret; 3911110a2dbSLarry Finger } 3921110a2dbSLarry Finger 393*26503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723a(struct hci_dev *hdev, 394*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 395db33c77dSCarlo Caione { 396*26503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 8) 397*26503ad2SMartin Blumenstingl return -EINVAL; 398db33c77dSCarlo Caione 399db33c77dSCarlo Caione /* Check that the firmware doesn't have the epatch signature 400db33c77dSCarlo Caione * (which is only for RTL8723B and newer). 401db33c77dSCarlo Caione */ 402*26503ad2SMartin Blumenstingl if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) { 403db33c77dSCarlo Caione BT_ERR("%s: unexpected EPATCH signature!", hdev->name); 404907f8499SAlex Lu return -EINVAL; 405907f8499SAlex Lu } 406907f8499SAlex Lu 407*26503ad2SMartin Blumenstingl return rtl_download_firmware(hdev, btrtl_dev->fw_data, 408*26503ad2SMartin Blumenstingl btrtl_dev->fw_len); 409db33c77dSCarlo Caione } 410db33c77dSCarlo Caione 411*26503ad2SMartin Blumenstingl static int btrtl_setup_rtl8723b(struct hci_dev *hdev, 412*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 413*26503ad2SMartin Blumenstingl { 414*26503ad2SMartin Blumenstingl unsigned char *fw_data = NULL; 415*26503ad2SMartin Blumenstingl int ret; 416*26503ad2SMartin Blumenstingl u8 *tbuff; 417*26503ad2SMartin Blumenstingl 418*26503ad2SMartin Blumenstingl ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data); 419db33c77dSCarlo Caione if (ret < 0) 420db33c77dSCarlo Caione goto out; 421db33c77dSCarlo Caione 422*26503ad2SMartin Blumenstingl if (btrtl_dev->cfg_len > 0) { 423*26503ad2SMartin Blumenstingl tbuff = kzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL); 4241110a2dbSLarry Finger if (!tbuff) { 4251110a2dbSLarry Finger ret = -ENOMEM; 426db33c77dSCarlo Caione goto out; 4271110a2dbSLarry Finger } 4281110a2dbSLarry Finger 4291110a2dbSLarry Finger memcpy(tbuff, fw_data, ret); 4301110a2dbSLarry Finger kfree(fw_data); 4311110a2dbSLarry Finger 432*26503ad2SMartin Blumenstingl memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len); 433*26503ad2SMartin Blumenstingl ret += btrtl_dev->cfg_len; 4341110a2dbSLarry Finger 4351110a2dbSLarry Finger fw_data = tbuff; 4361110a2dbSLarry Finger } 4371110a2dbSLarry Finger 438*26503ad2SMartin Blumenstingl rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret); 4391110a2dbSLarry Finger 4401110a2dbSLarry Finger ret = rtl_download_firmware(hdev, fw_data, ret); 441db33c77dSCarlo Caione 442db33c77dSCarlo Caione out: 4431110a2dbSLarry Finger kfree(fw_data); 444db33c77dSCarlo Caione return ret; 445db33c77dSCarlo Caione } 446db33c77dSCarlo Caione 447db33c77dSCarlo Caione static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev) 448db33c77dSCarlo Caione { 449db33c77dSCarlo Caione struct sk_buff *skb; 450db33c77dSCarlo Caione 451db33c77dSCarlo Caione skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, 452db33c77dSCarlo Caione HCI_INIT_TIMEOUT); 453db33c77dSCarlo Caione if (IS_ERR(skb)) { 454db33c77dSCarlo Caione BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)", 455db33c77dSCarlo Caione hdev->name, PTR_ERR(skb)); 456db33c77dSCarlo Caione return skb; 457db33c77dSCarlo Caione } 458db33c77dSCarlo Caione 459db33c77dSCarlo Caione if (skb->len != sizeof(struct hci_rp_read_local_version)) { 460db33c77dSCarlo Caione BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch", 461db33c77dSCarlo Caione hdev->name); 462db33c77dSCarlo Caione kfree_skb(skb); 463db33c77dSCarlo Caione return ERR_PTR(-EIO); 464db33c77dSCarlo Caione } 465db33c77dSCarlo Caione 466db33c77dSCarlo Caione return skb; 467db33c77dSCarlo Caione } 468db33c77dSCarlo Caione 469*26503ad2SMartin Blumenstingl void btrtl_free(struct btrtl_device_info *btrtl_dev) 470db33c77dSCarlo Caione { 471*26503ad2SMartin Blumenstingl kfree(btrtl_dev->fw_data); 472*26503ad2SMartin Blumenstingl kfree(btrtl_dev->cfg_data); 473*26503ad2SMartin Blumenstingl kfree(btrtl_dev); 474*26503ad2SMartin Blumenstingl } 475*26503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_free); 476*26503ad2SMartin Blumenstingl 477*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev) 478*26503ad2SMartin Blumenstingl { 479*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev; 480db33c77dSCarlo Caione struct sk_buff *skb; 481db33c77dSCarlo Caione struct hci_rp_read_local_version *resp; 482907f8499SAlex Lu u16 hci_rev, lmp_subver; 483*26503ad2SMartin Blumenstingl int ret; 484*26503ad2SMartin Blumenstingl 485*26503ad2SMartin Blumenstingl btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL); 486*26503ad2SMartin Blumenstingl if (!btrtl_dev) { 487*26503ad2SMartin Blumenstingl ret = -ENOMEM; 488*26503ad2SMartin Blumenstingl goto err_alloc; 489*26503ad2SMartin Blumenstingl } 490db33c77dSCarlo Caione 491db33c77dSCarlo Caione skb = btrtl_read_local_version(hdev); 492*26503ad2SMartin Blumenstingl if (IS_ERR(skb)) { 493*26503ad2SMartin Blumenstingl ret = PTR_ERR(skb); 494*26503ad2SMartin Blumenstingl goto err_free; 495*26503ad2SMartin Blumenstingl } 496db33c77dSCarlo Caione 497db33c77dSCarlo Caione resp = (struct hci_rp_read_local_version *)skb->data; 4982064ee33SMarcel Holtmann bt_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x " 4992064ee33SMarcel Holtmann "lmp_ver=%02x lmp_subver=%04x", 5002064ee33SMarcel Holtmann resp->hci_ver, resp->hci_rev, 501db33c77dSCarlo Caione resp->lmp_ver, resp->lmp_subver); 502db33c77dSCarlo Caione 503907f8499SAlex Lu hci_rev = le16_to_cpu(resp->hci_rev); 504db33c77dSCarlo Caione lmp_subver = le16_to_cpu(resp->lmp_subver); 505db33c77dSCarlo Caione kfree_skb(skb); 506db33c77dSCarlo Caione 507*26503ad2SMartin Blumenstingl btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev); 508*26503ad2SMartin Blumenstingl if (!btrtl_dev->ic_info) { 509*26503ad2SMartin Blumenstingl rtl_dev_err(hdev, "unknown IC info, lmp subver %04x, hci rev %04x\n", 510*26503ad2SMartin Blumenstingl lmp_subver, hci_rev); 511*26503ad2SMartin Blumenstingl ret = -EINVAL; 512*26503ad2SMartin Blumenstingl goto err_free; 513*26503ad2SMartin Blumenstingl } 514*26503ad2SMartin Blumenstingl 515*26503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->has_rom_version) { 516*26503ad2SMartin Blumenstingl ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version); 517*26503ad2SMartin Blumenstingl if (ret) 518*26503ad2SMartin Blumenstingl goto err_free; 519*26503ad2SMartin Blumenstingl } 520*26503ad2SMartin Blumenstingl 521*26503ad2SMartin Blumenstingl btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name, 522*26503ad2SMartin Blumenstingl &btrtl_dev->fw_data); 523*26503ad2SMartin Blumenstingl if (btrtl_dev->fw_len < 0) { 524*26503ad2SMartin Blumenstingl rtl_dev_err(hdev, "firmware file %s not found\n", 525*26503ad2SMartin Blumenstingl btrtl_dev->ic_info->fw_name); 526*26503ad2SMartin Blumenstingl ret = btrtl_dev->fw_len; 527*26503ad2SMartin Blumenstingl goto err_free; 528*26503ad2SMartin Blumenstingl } 529*26503ad2SMartin Blumenstingl 530*26503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->cfg_name) { 531*26503ad2SMartin Blumenstingl btrtl_dev->cfg_len = rtl_load_file(hdev, 532*26503ad2SMartin Blumenstingl btrtl_dev->ic_info->cfg_name, 533*26503ad2SMartin Blumenstingl &btrtl_dev->cfg_data); 534*26503ad2SMartin Blumenstingl if (btrtl_dev->ic_info->config_needed && 535*26503ad2SMartin Blumenstingl btrtl_dev->cfg_len <= 0) { 536*26503ad2SMartin Blumenstingl rtl_dev_err(hdev, "mandatory config file %s not found\n", 537*26503ad2SMartin Blumenstingl btrtl_dev->ic_info->cfg_name); 538*26503ad2SMartin Blumenstingl ret = btrtl_dev->cfg_len; 539*26503ad2SMartin Blumenstingl goto err_free; 540*26503ad2SMartin Blumenstingl } 541*26503ad2SMartin Blumenstingl } 542*26503ad2SMartin Blumenstingl 543*26503ad2SMartin Blumenstingl return btrtl_dev; 544*26503ad2SMartin Blumenstingl 545*26503ad2SMartin Blumenstingl err_free: 546*26503ad2SMartin Blumenstingl btrtl_free(btrtl_dev); 547*26503ad2SMartin Blumenstingl err_alloc: 548*26503ad2SMartin Blumenstingl return ERR_PTR(ret); 549*26503ad2SMartin Blumenstingl } 550*26503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_initialize); 551*26503ad2SMartin Blumenstingl 552*26503ad2SMartin Blumenstingl int btrtl_download_firmware(struct hci_dev *hdev, 553*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev) 554*26503ad2SMartin Blumenstingl { 555db33c77dSCarlo Caione /* Match a set of subver values that correspond to stock firmware, 556db33c77dSCarlo Caione * which is not compatible with standard btusb. 557db33c77dSCarlo Caione * If matched, upload an alternative firmware that does conform to 558db33c77dSCarlo Caione * standard btusb. Once that firmware is uploaded, the subver changes 559db33c77dSCarlo Caione * to a different value. 560db33c77dSCarlo Caione */ 561*26503ad2SMartin Blumenstingl switch (btrtl_dev->ic_info->lmp_subver) { 562db33c77dSCarlo Caione case RTL_ROM_LMP_8723A: 563db33c77dSCarlo Caione case RTL_ROM_LMP_3499: 564*26503ad2SMartin Blumenstingl return btrtl_setup_rtl8723a(hdev, btrtl_dev); 565db33c77dSCarlo Caione case RTL_ROM_LMP_8723B: 566db33c77dSCarlo Caione case RTL_ROM_LMP_8821A: 567db33c77dSCarlo Caione case RTL_ROM_LMP_8761A: 5681110a2dbSLarry Finger case RTL_ROM_LMP_8822B: 569*26503ad2SMartin Blumenstingl return btrtl_setup_rtl8723b(hdev, btrtl_dev); 570db33c77dSCarlo Caione default: 5712064ee33SMarcel Holtmann bt_dev_info(hdev, "rtl: assuming no firmware upload needed"); 572db33c77dSCarlo Caione return 0; 573db33c77dSCarlo Caione } 574db33c77dSCarlo Caione } 575*26503ad2SMartin Blumenstingl EXPORT_SYMBOL_GPL(btrtl_download_firmware); 576*26503ad2SMartin Blumenstingl 577*26503ad2SMartin Blumenstingl int btrtl_setup_realtek(struct hci_dev *hdev) 578*26503ad2SMartin Blumenstingl { 579*26503ad2SMartin Blumenstingl struct btrtl_device_info *btrtl_dev; 580*26503ad2SMartin Blumenstingl int ret; 581*26503ad2SMartin Blumenstingl 582*26503ad2SMartin Blumenstingl btrtl_dev = btrtl_initialize(hdev); 583*26503ad2SMartin Blumenstingl if (IS_ERR(btrtl_dev)) 584*26503ad2SMartin Blumenstingl return PTR_ERR(btrtl_dev); 585*26503ad2SMartin Blumenstingl 586*26503ad2SMartin Blumenstingl ret = btrtl_download_firmware(hdev, btrtl_dev); 587*26503ad2SMartin Blumenstingl 588*26503ad2SMartin Blumenstingl btrtl_free(btrtl_dev); 589*26503ad2SMartin Blumenstingl 590*26503ad2SMartin Blumenstingl return ret; 591*26503ad2SMartin Blumenstingl } 592db33c77dSCarlo Caione EXPORT_SYMBOL_GPL(btrtl_setup_realtek); 593db33c77dSCarlo Caione 594db33c77dSCarlo Caione MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>"); 595db33c77dSCarlo Caione MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION); 596db33c77dSCarlo Caione MODULE_VERSION(VERSION); 597db33c77dSCarlo Caione MODULE_LICENSE("GPL"); 598f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin"); 599f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin"); 600f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin"); 601f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin"); 602f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin"); 603f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin"); 604f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin"); 605f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin"); 606f96dbd32SMartin Blumenstingl MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin"); 607