btrtl.c 12 KB

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