btrtl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * Bluetooth support for Realtek devices
  3. *
  4. * Copyright (C) 2015 Endless Mobile, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/usb.h>
  21. #include <net/bluetooth/bluetooth.h>
  22. #include <net/bluetooth/hci_core.h>
  23. #include "btrtl.h"
  24. #define VERSION "0.1"
  25. #define RTL_EPATCH_SIGNATURE "Realtech"
  26. #define RTL_ROM_LMP_3499 0x3499
  27. #define RTL_ROM_LMP_8723A 0x1200
  28. #define RTL_ROM_LMP_8723B 0x8723
  29. #define RTL_ROM_LMP_8821A 0x8821
  30. #define RTL_ROM_LMP_8761A 0x8761
  31. #define RTL_ROM_LMP_8822B 0x8822
  32. #define IC_MATCH_FL_LMPSUBV (1 << 0)
  33. #define IC_MATCH_FL_HCIREV (1 << 1)
  34. #define IC_INFO(lmps, hcir) \
  35. .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
  36. .lmp_subver = (lmps), \
  37. .hci_rev = (hcir)
  38. struct id_table {
  39. __u16 match_flags;
  40. __u16 lmp_subver;
  41. __u16 hci_rev;
  42. bool config_needed;
  43. char *fw_name;
  44. char *cfg_name;
  45. };
  46. static const struct id_table ic_id_table[] = {
  47. /* 8723B */
  48. { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
  49. .config_needed = false,
  50. .fw_name = "rtl_bt/rtl8723b_fw.bin",
  51. .cfg_name = "rtl_bt/rtl8723b_config.bin" },
  52. /* 8723D */
  53. { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
  54. .config_needed = true,
  55. .fw_name = "rtl_bt/rtl8723d_fw.bin",
  56. .cfg_name = "rtl_bt/rtl8723d_config.bin" },
  57. /* 8821A */
  58. { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
  59. .config_needed = false,
  60. .fw_name = "rtl_bt/rtl8821a_fw.bin",
  61. .cfg_name = "rtl_bt/rtl8821a_config.bin" },
  62. /* 8821C */
  63. { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
  64. .config_needed = false,
  65. .fw_name = "rtl_bt/rtl8821c_fw.bin",
  66. .cfg_name = "rtl_bt/rtl8821c_config.bin" },
  67. /* 8761A */
  68. { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8761A, 0x0,
  69. .config_needed = false,
  70. .fw_name = "rtl_bt/rtl8761a_fw.bin",
  71. .cfg_name = "rtl_bt/rtl8761a_config.bin" },
  72. /* 8822B */
  73. { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
  74. .config_needed = true,
  75. .fw_name = "rtl_bt/rtl8822b_fw.bin",
  76. .cfg_name = "rtl_bt/rtl8822b_config.bin" },
  77. };
  78. static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
  79. {
  80. struct rtl_rom_version_evt *rom_version;
  81. struct sk_buff *skb;
  82. /* Read RTL ROM version command */
  83. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  84. if (IS_ERR(skb)) {
  85. BT_ERR("%s: Read ROM version failed (%ld)",
  86. hdev->name, PTR_ERR(skb));
  87. return PTR_ERR(skb);
  88. }
  89. if (skb->len != sizeof(*rom_version)) {
  90. BT_ERR("%s: RTL version event length mismatch", hdev->name);
  91. kfree_skb(skb);
  92. return -EIO;
  93. }
  94. rom_version = (struct rtl_rom_version_evt *)skb->data;
  95. bt_dev_info(hdev, "rom_version status=%x version=%x",
  96. rom_version->status, rom_version->version);
  97. *version = rom_version->version;
  98. kfree_skb(skb);
  99. return 0;
  100. }
  101. static int rtlbt_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
  102. const struct firmware *fw,
  103. unsigned char **_buf)
  104. {
  105. const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
  106. struct rtl_epatch_header *epatch_info;
  107. unsigned char *buf;
  108. int i, ret, len;
  109. size_t min_size;
  110. u8 opcode, length, data, rom_version = 0;
  111. int project_id = -1;
  112. const unsigned char *fwptr, *chip_id_base;
  113. const unsigned char *patch_length_base, *patch_offset_base;
  114. u32 patch_offset = 0;
  115. u16 patch_length, num_patches;
  116. static const struct {
  117. __u16 lmp_subver;
  118. __u8 id;
  119. } project_id_to_lmp_subver[] = {
  120. { RTL_ROM_LMP_8723A, 0 },
  121. { RTL_ROM_LMP_8723B, 1 },
  122. { RTL_ROM_LMP_8821A, 2 },
  123. { RTL_ROM_LMP_8761A, 3 },
  124. { RTL_ROM_LMP_8822B, 8 },
  125. { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
  126. { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
  127. };
  128. ret = rtl_read_rom_version(hdev, &rom_version);
  129. if (ret)
  130. return ret;
  131. min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
  132. if (fw->size < min_size)
  133. return -EINVAL;
  134. fwptr = fw->data + fw->size - sizeof(extension_sig);
  135. if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
  136. BT_ERR("%s: extension section signature mismatch", hdev->name);
  137. return -EINVAL;
  138. }
  139. /* Loop from the end of the firmware parsing instructions, until
  140. * we find an instruction that identifies the "project ID" for the
  141. * hardware supported by this firwmare file.
  142. * Once we have that, we double-check that that project_id is suitable
  143. * for the hardware we are working with.
  144. */
  145. while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
  146. opcode = *--fwptr;
  147. length = *--fwptr;
  148. data = *--fwptr;
  149. BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
  150. if (opcode == 0xff) /* EOF */
  151. break;
  152. if (length == 0) {
  153. BT_ERR("%s: found instruction with length 0",
  154. hdev->name);
  155. return -EINVAL;
  156. }
  157. if (opcode == 0 && length == 1) {
  158. project_id = data;
  159. break;
  160. }
  161. fwptr -= length;
  162. }
  163. if (project_id < 0) {
  164. BT_ERR("%s: failed to find version instruction", hdev->name);
  165. return -EINVAL;
  166. }
  167. /* Find project_id in table */
  168. for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
  169. if (project_id == project_id_to_lmp_subver[i].id)
  170. break;
  171. }
  172. if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
  173. BT_ERR("%s: unknown project id %d", hdev->name, project_id);
  174. return -EINVAL;
  175. }
  176. if (lmp_subver != project_id_to_lmp_subver[i].lmp_subver) {
  177. BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
  178. project_id_to_lmp_subver[i].lmp_subver, lmp_subver);
  179. return -EINVAL;
  180. }
  181. epatch_info = (struct rtl_epatch_header *)fw->data;
  182. if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
  183. BT_ERR("%s: bad EPATCH signature", hdev->name);
  184. return -EINVAL;
  185. }
  186. num_patches = le16_to_cpu(epatch_info->num_patches);
  187. BT_DBG("fw_version=%x, num_patches=%d",
  188. le32_to_cpu(epatch_info->fw_version), num_patches);
  189. /* After the rtl_epatch_header there is a funky patch metadata section.
  190. * Assuming 2 patches, the layout is:
  191. * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
  192. *
  193. * Find the right patch for this chip.
  194. */
  195. min_size += 8 * num_patches;
  196. if (fw->size < min_size)
  197. return -EINVAL;
  198. chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
  199. patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
  200. patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
  201. for (i = 0; i < num_patches; i++) {
  202. u16 chip_id = get_unaligned_le16(chip_id_base +
  203. (i * sizeof(u16)));
  204. if (chip_id == rom_version + 1) {
  205. patch_length = get_unaligned_le16(patch_length_base +
  206. (i * sizeof(u16)));
  207. patch_offset = get_unaligned_le32(patch_offset_base +
  208. (i * sizeof(u32)));
  209. break;
  210. }
  211. }
  212. if (!patch_offset) {
  213. BT_ERR("%s: didn't find patch for chip id %d",
  214. hdev->name, rom_version);
  215. return -EINVAL;
  216. }
  217. BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
  218. min_size = patch_offset + patch_length;
  219. if (fw->size < min_size)
  220. return -EINVAL;
  221. /* Copy the firmware into a new buffer and write the version at
  222. * the end.
  223. */
  224. len = patch_length;
  225. buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
  226. if (!buf)
  227. return -ENOMEM;
  228. memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
  229. *_buf = buf;
  230. return len;
  231. }
  232. static int rtl_download_firmware(struct hci_dev *hdev,
  233. const unsigned char *data, int fw_len)
  234. {
  235. struct rtl_download_cmd *dl_cmd;
  236. int frag_num = fw_len / RTL_FRAG_LEN + 1;
  237. int frag_len = RTL_FRAG_LEN;
  238. int ret = 0;
  239. int i;
  240. dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
  241. if (!dl_cmd)
  242. return -ENOMEM;
  243. for (i = 0; i < frag_num; i++) {
  244. struct sk_buff *skb;
  245. BT_DBG("download fw (%d/%d)", i, frag_num);
  246. dl_cmd->index = i;
  247. if (i == (frag_num - 1)) {
  248. dl_cmd->index |= 0x80; /* data end */
  249. frag_len = fw_len % RTL_FRAG_LEN;
  250. }
  251. memcpy(dl_cmd->data, data, frag_len);
  252. /* Send download command */
  253. skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
  254. HCI_INIT_TIMEOUT);
  255. if (IS_ERR(skb)) {
  256. BT_ERR("%s: download fw command failed (%ld)",
  257. hdev->name, PTR_ERR(skb));
  258. ret = -PTR_ERR(skb);
  259. goto out;
  260. }
  261. if (skb->len != sizeof(struct rtl_download_response)) {
  262. BT_ERR("%s: download fw event length mismatch",
  263. hdev->name);
  264. kfree_skb(skb);
  265. ret = -EIO;
  266. goto out;
  267. }
  268. kfree_skb(skb);
  269. data += RTL_FRAG_LEN;
  270. }
  271. out:
  272. kfree(dl_cmd);
  273. return ret;
  274. }
  275. static int rtl_load_config(struct hci_dev *hdev, const char *name, u8 **buff)
  276. {
  277. const struct firmware *fw;
  278. int ret;
  279. bt_dev_info(hdev, "rtl: loading %s", name);
  280. ret = request_firmware(&fw, name, &hdev->dev);
  281. if (ret < 0)
  282. return ret;
  283. ret = fw->size;
  284. *buff = kmemdup(fw->data, ret, GFP_KERNEL);
  285. if (!*buff)
  286. ret = -ENOMEM;
  287. release_firmware(fw);
  288. return ret;
  289. }
  290. static int btrtl_setup_rtl8723a(struct hci_dev *hdev)
  291. {
  292. const struct firmware *fw;
  293. int ret;
  294. bt_dev_info(hdev, "rtl: loading rtl_bt/rtl8723a_fw.bin");
  295. ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &hdev->dev);
  296. if (ret < 0) {
  297. BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
  298. return ret;
  299. }
  300. if (fw->size < 8) {
  301. ret = -EINVAL;
  302. goto out;
  303. }
  304. /* Check that the firmware doesn't have the epatch signature
  305. * (which is only for RTL8723B and newer).
  306. */
  307. if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
  308. BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
  309. ret = -EINVAL;
  310. goto out;
  311. }
  312. ret = rtl_download_firmware(hdev, fw->data, fw->size);
  313. out:
  314. release_firmware(fw);
  315. return ret;
  316. }
  317. static int btrtl_setup_rtl8723b(struct hci_dev *hdev, u16 hci_rev,
  318. u16 lmp_subver)
  319. {
  320. unsigned char *fw_data = NULL;
  321. const struct firmware *fw;
  322. int ret;
  323. int cfg_sz;
  324. u8 *cfg_buff = NULL;
  325. u8 *tbuff;
  326. char *cfg_name = NULL;
  327. char *fw_name = NULL;
  328. int i;
  329. for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
  330. if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
  331. (ic_id_table[i].lmp_subver != lmp_subver))
  332. continue;
  333. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
  334. (ic_id_table[i].hci_rev != hci_rev))
  335. continue;
  336. break;
  337. }
  338. if (i >= ARRAY_SIZE(ic_id_table)) {
  339. BT_ERR("%s: unknown IC info, lmp subver %04x, hci rev %04x",
  340. hdev->name, lmp_subver, hci_rev);
  341. return -EINVAL;
  342. }
  343. cfg_name = ic_id_table[i].cfg_name;
  344. if (cfg_name) {
  345. cfg_sz = rtl_load_config(hdev, cfg_name, &cfg_buff);
  346. if (cfg_sz < 0) {
  347. cfg_sz = 0;
  348. if (ic_id_table[i].config_needed)
  349. BT_ERR("Necessary config file %s not found\n",
  350. cfg_name);
  351. }
  352. } else
  353. cfg_sz = 0;
  354. fw_name = ic_id_table[i].fw_name;
  355. bt_dev_info(hdev, "rtl: loading %s", fw_name);
  356. ret = request_firmware(&fw, fw_name, &hdev->dev);
  357. if (ret < 0) {
  358. BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
  359. goto err_req_fw;
  360. }
  361. ret = rtlbt_parse_firmware(hdev, lmp_subver, fw, &fw_data);
  362. if (ret < 0)
  363. goto out;
  364. if (cfg_sz) {
  365. tbuff = kzalloc(ret + cfg_sz, GFP_KERNEL);
  366. if (!tbuff) {
  367. ret = -ENOMEM;
  368. goto out;
  369. }
  370. memcpy(tbuff, fw_data, ret);
  371. kfree(fw_data);
  372. memcpy(tbuff + ret, cfg_buff, cfg_sz);
  373. ret += cfg_sz;
  374. fw_data = tbuff;
  375. }
  376. bt_dev_info(hdev, "cfg_sz %d, total size %d", cfg_sz, ret);
  377. ret = rtl_download_firmware(hdev, fw_data, ret);
  378. out:
  379. release_firmware(fw);
  380. kfree(fw_data);
  381. err_req_fw:
  382. if (cfg_sz)
  383. kfree(cfg_buff);
  384. return ret;
  385. }
  386. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  387. {
  388. struct sk_buff *skb;
  389. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  390. HCI_INIT_TIMEOUT);
  391. if (IS_ERR(skb)) {
  392. BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
  393. hdev->name, PTR_ERR(skb));
  394. return skb;
  395. }
  396. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  397. BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
  398. hdev->name);
  399. kfree_skb(skb);
  400. return ERR_PTR(-EIO);
  401. }
  402. return skb;
  403. }
  404. int btrtl_setup_realtek(struct hci_dev *hdev)
  405. {
  406. struct sk_buff *skb;
  407. struct hci_rp_read_local_version *resp;
  408. u16 hci_rev, lmp_subver;
  409. skb = btrtl_read_local_version(hdev);
  410. if (IS_ERR(skb))
  411. return -PTR_ERR(skb);
  412. resp = (struct hci_rp_read_local_version *)skb->data;
  413. bt_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x "
  414. "lmp_ver=%02x lmp_subver=%04x",
  415. resp->hci_ver, resp->hci_rev,
  416. resp->lmp_ver, resp->lmp_subver);
  417. hci_rev = le16_to_cpu(resp->hci_rev);
  418. lmp_subver = le16_to_cpu(resp->lmp_subver);
  419. kfree_skb(skb);
  420. /* Match a set of subver values that correspond to stock firmware,
  421. * which is not compatible with standard btusb.
  422. * If matched, upload an alternative firmware that does conform to
  423. * standard btusb. Once that firmware is uploaded, the subver changes
  424. * to a different value.
  425. */
  426. switch (lmp_subver) {
  427. case RTL_ROM_LMP_8723A:
  428. case RTL_ROM_LMP_3499:
  429. return btrtl_setup_rtl8723a(hdev);
  430. case RTL_ROM_LMP_8723B:
  431. case RTL_ROM_LMP_8821A:
  432. case RTL_ROM_LMP_8761A:
  433. case RTL_ROM_LMP_8822B:
  434. return btrtl_setup_rtl8723b(hdev, hci_rev, lmp_subver);
  435. default:
  436. bt_dev_info(hdev, "rtl: assuming no firmware upload needed");
  437. return 0;
  438. }
  439. }
  440. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  441. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  442. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  443. MODULE_VERSION(VERSION);
  444. MODULE_LICENSE("GPL");