btrtl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
  32. {
  33. struct rtl_rom_version_evt *rom_version;
  34. struct sk_buff *skb;
  35. /* Read RTL ROM version command */
  36. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  37. if (IS_ERR(skb)) {
  38. BT_ERR("%s: Read ROM version failed (%ld)",
  39. hdev->name, PTR_ERR(skb));
  40. return PTR_ERR(skb);
  41. }
  42. if (skb->len != sizeof(*rom_version)) {
  43. BT_ERR("%s: RTL version event length mismatch", hdev->name);
  44. kfree_skb(skb);
  45. return -EIO;
  46. }
  47. rom_version = (struct rtl_rom_version_evt *)skb->data;
  48. BT_INFO("%s: rom_version status=%x version=%x",
  49. hdev->name, rom_version->status, rom_version->version);
  50. *version = rom_version->version;
  51. kfree_skb(skb);
  52. return 0;
  53. }
  54. static int rtl8723b_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
  55. const struct firmware *fw,
  56. unsigned char **_buf)
  57. {
  58. const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
  59. struct rtl_epatch_header *epatch_info;
  60. unsigned char *buf;
  61. int i, ret, len;
  62. size_t min_size;
  63. u8 opcode, length, data, rom_version = 0;
  64. int project_id = -1;
  65. const unsigned char *fwptr, *chip_id_base;
  66. const unsigned char *patch_length_base, *patch_offset_base;
  67. u32 patch_offset = 0;
  68. u16 patch_length, num_patches;
  69. const u16 project_id_to_lmp_subver[] = {
  70. RTL_ROM_LMP_8723A,
  71. RTL_ROM_LMP_8723B,
  72. RTL_ROM_LMP_8821A,
  73. RTL_ROM_LMP_8761A
  74. };
  75. ret = rtl_read_rom_version(hdev, &rom_version);
  76. if (ret)
  77. return ret;
  78. min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
  79. if (fw->size < min_size)
  80. return -EINVAL;
  81. fwptr = fw->data + fw->size - sizeof(extension_sig);
  82. if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
  83. BT_ERR("%s: extension section signature mismatch", hdev->name);
  84. return -EINVAL;
  85. }
  86. /* Loop from the end of the firmware parsing instructions, until
  87. * we find an instruction that identifies the "project ID" for the
  88. * hardware supported by this firwmare file.
  89. * Once we have that, we double-check that that project_id is suitable
  90. * for the hardware we are working with.
  91. */
  92. while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
  93. opcode = *--fwptr;
  94. length = *--fwptr;
  95. data = *--fwptr;
  96. BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
  97. if (opcode == 0xff) /* EOF */
  98. break;
  99. if (length == 0) {
  100. BT_ERR("%s: found instruction with length 0",
  101. hdev->name);
  102. return -EINVAL;
  103. }
  104. if (opcode == 0 && length == 1) {
  105. project_id = data;
  106. break;
  107. }
  108. fwptr -= length;
  109. }
  110. if (project_id < 0) {
  111. BT_ERR("%s: failed to find version instruction", hdev->name);
  112. return -EINVAL;
  113. }
  114. if (project_id >= ARRAY_SIZE(project_id_to_lmp_subver)) {
  115. BT_ERR("%s: unknown project id %d", hdev->name, project_id);
  116. return -EINVAL;
  117. }
  118. if (lmp_subver != project_id_to_lmp_subver[project_id]) {
  119. BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
  120. project_id_to_lmp_subver[project_id], lmp_subver);
  121. return -EINVAL;
  122. }
  123. epatch_info = (struct rtl_epatch_header *)fw->data;
  124. if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
  125. BT_ERR("%s: bad EPATCH signature", hdev->name);
  126. return -EINVAL;
  127. }
  128. num_patches = le16_to_cpu(epatch_info->num_patches);
  129. BT_DBG("fw_version=%x, num_patches=%d",
  130. le32_to_cpu(epatch_info->fw_version), num_patches);
  131. /* After the rtl_epatch_header there is a funky patch metadata section.
  132. * Assuming 2 patches, the layout is:
  133. * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
  134. *
  135. * Find the right patch for this chip.
  136. */
  137. min_size += 8 * num_patches;
  138. if (fw->size < min_size)
  139. return -EINVAL;
  140. chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
  141. patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
  142. patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
  143. for (i = 0; i < num_patches; i++) {
  144. u16 chip_id = get_unaligned_le16(chip_id_base +
  145. (i * sizeof(u16)));
  146. if (chip_id == rom_version + 1) {
  147. patch_length = get_unaligned_le16(patch_length_base +
  148. (i * sizeof(u16)));
  149. patch_offset = get_unaligned_le32(patch_offset_base +
  150. (i * sizeof(u32)));
  151. break;
  152. }
  153. }
  154. if (!patch_offset) {
  155. BT_ERR("%s: didn't find patch for chip id %d",
  156. hdev->name, rom_version);
  157. return -EINVAL;
  158. }
  159. BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
  160. min_size = patch_offset + patch_length;
  161. if (fw->size < min_size)
  162. return -EINVAL;
  163. /* Copy the firmware into a new buffer and write the version at
  164. * the end.
  165. */
  166. len = patch_length;
  167. buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
  168. if (!buf)
  169. return -ENOMEM;
  170. memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
  171. *_buf = buf;
  172. return len;
  173. }
  174. static int rtl_download_firmware(struct hci_dev *hdev,
  175. const unsigned char *data, int fw_len)
  176. {
  177. struct rtl_download_cmd *dl_cmd;
  178. int frag_num = fw_len / RTL_FRAG_LEN + 1;
  179. int frag_len = RTL_FRAG_LEN;
  180. int ret = 0;
  181. int i;
  182. dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
  183. if (!dl_cmd)
  184. return -ENOMEM;
  185. for (i = 0; i < frag_num; i++) {
  186. struct sk_buff *skb;
  187. BT_DBG("download fw (%d/%d)", i, frag_num);
  188. dl_cmd->index = i;
  189. if (i == (frag_num - 1)) {
  190. dl_cmd->index |= 0x80; /* data end */
  191. frag_len = fw_len % RTL_FRAG_LEN;
  192. }
  193. memcpy(dl_cmd->data, data, frag_len);
  194. /* Send download command */
  195. skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
  196. HCI_INIT_TIMEOUT);
  197. if (IS_ERR(skb)) {
  198. BT_ERR("%s: download fw command failed (%ld)",
  199. hdev->name, PTR_ERR(skb));
  200. ret = -PTR_ERR(skb);
  201. goto out;
  202. }
  203. if (skb->len != sizeof(struct rtl_download_response)) {
  204. BT_ERR("%s: download fw event length mismatch",
  205. hdev->name);
  206. kfree_skb(skb);
  207. ret = -EIO;
  208. goto out;
  209. }
  210. kfree_skb(skb);
  211. data += RTL_FRAG_LEN;
  212. }
  213. out:
  214. kfree(dl_cmd);
  215. return ret;
  216. }
  217. static int btrtl_setup_rtl8723a(struct hci_dev *hdev)
  218. {
  219. const struct firmware *fw;
  220. int ret;
  221. BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev->name);
  222. ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &hdev->dev);
  223. if (ret < 0) {
  224. BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
  225. return ret;
  226. }
  227. if (fw->size < 8) {
  228. ret = -EINVAL;
  229. goto out;
  230. }
  231. /* Check that the firmware doesn't have the epatch signature
  232. * (which is only for RTL8723B and newer).
  233. */
  234. if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
  235. BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
  236. ret = -EINVAL;
  237. goto out;
  238. }
  239. ret = rtl_download_firmware(hdev, fw->data, fw->size);
  240. out:
  241. release_firmware(fw);
  242. return ret;
  243. }
  244. static int btrtl_setup_rtl8723b(struct hci_dev *hdev, u16 lmp_subver,
  245. const char *fw_name)
  246. {
  247. unsigned char *fw_data = NULL;
  248. const struct firmware *fw;
  249. int ret;
  250. BT_INFO("%s: rtl: loading %s", hdev->name, fw_name);
  251. ret = request_firmware(&fw, fw_name, &hdev->dev);
  252. if (ret < 0) {
  253. BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
  254. return ret;
  255. }
  256. ret = rtl8723b_parse_firmware(hdev, lmp_subver, fw, &fw_data);
  257. if (ret < 0)
  258. goto out;
  259. ret = rtl_download_firmware(hdev, fw_data, ret);
  260. kfree(fw_data);
  261. if (ret < 0)
  262. goto out;
  263. out:
  264. release_firmware(fw);
  265. return ret;
  266. }
  267. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  268. {
  269. struct sk_buff *skb;
  270. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  271. HCI_INIT_TIMEOUT);
  272. if (IS_ERR(skb)) {
  273. BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
  274. hdev->name, PTR_ERR(skb));
  275. return skb;
  276. }
  277. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  278. BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
  279. hdev->name);
  280. kfree_skb(skb);
  281. return ERR_PTR(-EIO);
  282. }
  283. return skb;
  284. }
  285. int btrtl_setup_realtek(struct hci_dev *hdev)
  286. {
  287. struct sk_buff *skb;
  288. struct hci_rp_read_local_version *resp;
  289. u16 lmp_subver;
  290. skb = btrtl_read_local_version(hdev);
  291. if (IS_ERR(skb))
  292. return -PTR_ERR(skb);
  293. resp = (struct hci_rp_read_local_version *)skb->data;
  294. BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
  295. "lmp_subver=%04x", hdev->name, resp->hci_ver, resp->hci_rev,
  296. resp->lmp_ver, resp->lmp_subver);
  297. lmp_subver = le16_to_cpu(resp->lmp_subver);
  298. kfree_skb(skb);
  299. /* Match a set of subver values that correspond to stock firmware,
  300. * which is not compatible with standard btusb.
  301. * If matched, upload an alternative firmware that does conform to
  302. * standard btusb. Once that firmware is uploaded, the subver changes
  303. * to a different value.
  304. */
  305. switch (lmp_subver) {
  306. case RTL_ROM_LMP_8723A:
  307. case RTL_ROM_LMP_3499:
  308. return btrtl_setup_rtl8723a(hdev);
  309. case RTL_ROM_LMP_8723B:
  310. return btrtl_setup_rtl8723b(hdev, lmp_subver,
  311. "rtl_bt/rtl8723b_fw.bin");
  312. case RTL_ROM_LMP_8821A:
  313. return btrtl_setup_rtl8723b(hdev, lmp_subver,
  314. "rtl_bt/rtl8821a_fw.bin");
  315. case RTL_ROM_LMP_8761A:
  316. return btrtl_setup_rtl8723b(hdev, lmp_subver,
  317. "rtl_bt/rtl8761a_fw.bin");
  318. default:
  319. BT_INFO("rtl: assuming no firmware upload needed.");
  320. return 0;
  321. }
  322. }
  323. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  324. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  325. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  326. MODULE_VERSION(VERSION);
  327. MODULE_LICENSE("GPL");