btrtl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Bluetooth support for Realtek devices
  4. *
  5. * Copyright (C) 2015 Endless Mobile, Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/firmware.h>
  9. #include <asm/unaligned.h>
  10. #include <linux/usb.h>
  11. #include <net/bluetooth/bluetooth.h>
  12. #include <net/bluetooth/hci_core.h>
  13. #include "btrtl.h"
  14. #define VERSION "0.1"
  15. #define RTL_EPATCH_SIGNATURE "Realtech"
  16. #define RTL_ROM_LMP_3499 0x3499
  17. #define RTL_ROM_LMP_8723A 0x1200
  18. #define RTL_ROM_LMP_8723B 0x8723
  19. #define RTL_ROM_LMP_8723D 0x8873
  20. #define RTL_ROM_LMP_8821A 0x8821
  21. #define RTL_ROM_LMP_8761A 0x8761
  22. #define RTL_ROM_LMP_8822B 0x8822
  23. #define RTL_CONFIG_MAGIC 0x8723ab55
  24. #define IC_MATCH_FL_LMPSUBV (1 << 0)
  25. #define IC_MATCH_FL_HCIREV (1 << 1)
  26. #define IC_MATCH_FL_HCIVER (1 << 2)
  27. #define IC_MATCH_FL_HCIBUS (1 << 3)
  28. #define IC_INFO(lmps, hcir) \
  29. .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV, \
  30. .lmp_subver = (lmps), \
  31. .hci_rev = (hcir)
  32. struct id_table {
  33. __u16 match_flags;
  34. __u16 lmp_subver;
  35. __u16 hci_rev;
  36. __u8 hci_ver;
  37. __u8 hci_bus;
  38. bool config_needed;
  39. bool has_rom_version;
  40. char *fw_name;
  41. char *cfg_name;
  42. };
  43. struct btrtl_device_info {
  44. const struct id_table *ic_info;
  45. u8 rom_version;
  46. u8 *fw_data;
  47. int fw_len;
  48. u8 *cfg_data;
  49. int cfg_len;
  50. };
  51. static const struct id_table ic_id_table[] = {
  52. { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_8723A, 0x0,
  53. .config_needed = false,
  54. .has_rom_version = false,
  55. .fw_name = "rtl_bt/rtl8723a_fw.bin",
  56. .cfg_name = NULL },
  57. { IC_MATCH_FL_LMPSUBV, RTL_ROM_LMP_3499, 0x0,
  58. .config_needed = false,
  59. .has_rom_version = false,
  60. .fw_name = "rtl_bt/rtl8723a_fw.bin",
  61. .cfg_name = NULL },
  62. /* 8723BS */
  63. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
  64. IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
  65. .lmp_subver = RTL_ROM_LMP_8723B,
  66. .hci_rev = 0xb,
  67. .hci_ver = 6,
  68. .hci_bus = HCI_UART,
  69. .config_needed = true,
  70. .has_rom_version = true,
  71. .fw_name = "rtl_bt/rtl8723bs_fw.bin",
  72. .cfg_name = "rtl_bt/rtl8723bs_config" },
  73. /* 8723B */
  74. { IC_INFO(RTL_ROM_LMP_8723B, 0xb),
  75. .config_needed = false,
  76. .has_rom_version = true,
  77. .fw_name = "rtl_bt/rtl8723b_fw.bin",
  78. .cfg_name = "rtl_bt/rtl8723b_config" },
  79. /* 8723D */
  80. { IC_INFO(RTL_ROM_LMP_8723B, 0xd),
  81. .config_needed = true,
  82. .has_rom_version = true,
  83. .fw_name = "rtl_bt/rtl8723d_fw.bin",
  84. .cfg_name = "rtl_bt/rtl8723d_config" },
  85. /* 8723DS */
  86. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
  87. IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS,
  88. .lmp_subver = RTL_ROM_LMP_8723B,
  89. .hci_rev = 0xd,
  90. .hci_ver = 8,
  91. .hci_bus = HCI_UART,
  92. .config_needed = true,
  93. .has_rom_version = true,
  94. .fw_name = "rtl_bt/rtl8723ds_fw.bin",
  95. .cfg_name = "rtl_bt/rtl8723ds_config" },
  96. /* 8723DU */
  97. { IC_INFO(RTL_ROM_LMP_8723D, 0x826C),
  98. .config_needed = true,
  99. .has_rom_version = true,
  100. .fw_name = "rtl_bt/rtl8723d_fw.bin",
  101. .cfg_name = "rtl_bt/rtl8723d_config" },
  102. /* 8821A */
  103. { IC_INFO(RTL_ROM_LMP_8821A, 0xa),
  104. .config_needed = false,
  105. .has_rom_version = true,
  106. .fw_name = "rtl_bt/rtl8821a_fw.bin",
  107. .cfg_name = "rtl_bt/rtl8821a_config" },
  108. /* 8821C */
  109. { IC_INFO(RTL_ROM_LMP_8821A, 0xc),
  110. .config_needed = false,
  111. .has_rom_version = true,
  112. .fw_name = "rtl_bt/rtl8821c_fw.bin",
  113. .cfg_name = "rtl_bt/rtl8821c_config" },
  114. /* 8761A */
  115. { IC_INFO(RTL_ROM_LMP_8761A, 0xa),
  116. .config_needed = false,
  117. .has_rom_version = true,
  118. .fw_name = "rtl_bt/rtl8761a_fw.bin",
  119. .cfg_name = "rtl_bt/rtl8761a_config" },
  120. /* 8761B */
  121. { IC_INFO(RTL_ROM_LMP_8761A, 0xb),
  122. .config_needed = false,
  123. .has_rom_version = true,
  124. .fw_name = "rtl_bt/rtl8761b_fw.bin",
  125. .cfg_name = "rtl_bt/rtl8761b_config" },
  126. /* 8822C with UART interface */
  127. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV |
  128. IC_MATCH_FL_HCIBUS,
  129. .lmp_subver = RTL_ROM_LMP_8822B,
  130. .hci_rev = 0x000c,
  131. .hci_ver = 0x0a,
  132. .hci_bus = HCI_UART,
  133. .config_needed = true,
  134. .has_rom_version = true,
  135. .fw_name = "rtl_bt/rtl8822cs_fw.bin",
  136. .cfg_name = "rtl_bt/rtl8822cs_config" },
  137. /* 8822C with USB interface */
  138. { IC_INFO(RTL_ROM_LMP_8822B, 0xc),
  139. .config_needed = false,
  140. .has_rom_version = true,
  141. .fw_name = "rtl_bt/rtl8822cu_fw.bin",
  142. .cfg_name = "rtl_bt/rtl8822cu_config" },
  143. /* 8822B */
  144. { IC_INFO(RTL_ROM_LMP_8822B, 0xb),
  145. .config_needed = true,
  146. .has_rom_version = true,
  147. .fw_name = "rtl_bt/rtl8822b_fw.bin",
  148. .cfg_name = "rtl_bt/rtl8822b_config" },
  149. };
  150. static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
  151. u8 hci_ver, u8 hci_bus)
  152. {
  153. int i;
  154. for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
  155. if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
  156. (ic_id_table[i].lmp_subver != lmp_subver))
  157. continue;
  158. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
  159. (ic_id_table[i].hci_rev != hci_rev))
  160. continue;
  161. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
  162. (ic_id_table[i].hci_ver != hci_ver))
  163. continue;
  164. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
  165. (ic_id_table[i].hci_bus != hci_bus))
  166. continue;
  167. break;
  168. }
  169. if (i >= ARRAY_SIZE(ic_id_table))
  170. return NULL;
  171. return &ic_id_table[i];
  172. }
  173. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  174. {
  175. struct sk_buff *skb;
  176. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  177. HCI_INIT_TIMEOUT);
  178. if (IS_ERR(skb)) {
  179. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
  180. PTR_ERR(skb));
  181. return skb;
  182. }
  183. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  184. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
  185. kfree_skb(skb);
  186. return ERR_PTR(-EIO);
  187. }
  188. return skb;
  189. }
  190. static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
  191. {
  192. struct rtl_rom_version_evt *rom_version;
  193. struct sk_buff *skb;
  194. /* Read RTL ROM version command */
  195. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  196. if (IS_ERR(skb)) {
  197. rtl_dev_err(hdev, "Read ROM version failed (%ld)",
  198. PTR_ERR(skb));
  199. return PTR_ERR(skb);
  200. }
  201. if (skb->len != sizeof(*rom_version)) {
  202. rtl_dev_err(hdev, "version event length mismatch");
  203. kfree_skb(skb);
  204. return -EIO;
  205. }
  206. rom_version = (struct rtl_rom_version_evt *)skb->data;
  207. rtl_dev_info(hdev, "rom_version status=%x version=%x",
  208. rom_version->status, rom_version->version);
  209. *version = rom_version->version;
  210. kfree_skb(skb);
  211. return 0;
  212. }
  213. static int rtlbt_parse_firmware(struct hci_dev *hdev,
  214. struct btrtl_device_info *btrtl_dev,
  215. unsigned char **_buf)
  216. {
  217. static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
  218. struct rtl_epatch_header *epatch_info;
  219. unsigned char *buf;
  220. int i, len;
  221. size_t min_size;
  222. u8 opcode, length, data;
  223. int project_id = -1;
  224. const unsigned char *fwptr, *chip_id_base;
  225. const unsigned char *patch_length_base, *patch_offset_base;
  226. u32 patch_offset = 0;
  227. u16 patch_length, num_patches;
  228. static const struct {
  229. __u16 lmp_subver;
  230. __u8 id;
  231. } project_id_to_lmp_subver[] = {
  232. { RTL_ROM_LMP_8723A, 0 },
  233. { RTL_ROM_LMP_8723B, 1 },
  234. { RTL_ROM_LMP_8821A, 2 },
  235. { RTL_ROM_LMP_8761A, 3 },
  236. { RTL_ROM_LMP_8822B, 8 },
  237. { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
  238. { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
  239. { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
  240. { RTL_ROM_LMP_8761A, 14 }, /* 8761B */
  241. };
  242. min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
  243. if (btrtl_dev->fw_len < min_size)
  244. return -EINVAL;
  245. fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
  246. if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
  247. rtl_dev_err(hdev, "extension section signature mismatch");
  248. return -EINVAL;
  249. }
  250. /* Loop from the end of the firmware parsing instructions, until
  251. * we find an instruction that identifies the "project ID" for the
  252. * hardware supported by this firwmare file.
  253. * Once we have that, we double-check that that project_id is suitable
  254. * for the hardware we are working with.
  255. */
  256. while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
  257. opcode = *--fwptr;
  258. length = *--fwptr;
  259. data = *--fwptr;
  260. BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
  261. if (opcode == 0xff) /* EOF */
  262. break;
  263. if (length == 0) {
  264. rtl_dev_err(hdev, "found instruction with length 0");
  265. return -EINVAL;
  266. }
  267. if (opcode == 0 && length == 1) {
  268. project_id = data;
  269. break;
  270. }
  271. fwptr -= length;
  272. }
  273. if (project_id < 0) {
  274. rtl_dev_err(hdev, "failed to find version instruction");
  275. return -EINVAL;
  276. }
  277. /* Find project_id in table */
  278. for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
  279. if (project_id == project_id_to_lmp_subver[i].id)
  280. break;
  281. }
  282. if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
  283. rtl_dev_err(hdev, "unknown project id %d", project_id);
  284. return -EINVAL;
  285. }
  286. if (btrtl_dev->ic_info->lmp_subver !=
  287. project_id_to_lmp_subver[i].lmp_subver) {
  288. rtl_dev_err(hdev, "firmware is for %x but this is a %x",
  289. project_id_to_lmp_subver[i].lmp_subver,
  290. btrtl_dev->ic_info->lmp_subver);
  291. return -EINVAL;
  292. }
  293. epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
  294. if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
  295. rtl_dev_err(hdev, "bad EPATCH signature");
  296. return -EINVAL;
  297. }
  298. num_patches = le16_to_cpu(epatch_info->num_patches);
  299. BT_DBG("fw_version=%x, num_patches=%d",
  300. le32_to_cpu(epatch_info->fw_version), num_patches);
  301. /* After the rtl_epatch_header there is a funky patch metadata section.
  302. * Assuming 2 patches, the layout is:
  303. * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
  304. *
  305. * Find the right patch for this chip.
  306. */
  307. min_size += 8 * num_patches;
  308. if (btrtl_dev->fw_len < min_size)
  309. return -EINVAL;
  310. chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
  311. patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
  312. patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
  313. for (i = 0; i < num_patches; i++) {
  314. u16 chip_id = get_unaligned_le16(chip_id_base +
  315. (i * sizeof(u16)));
  316. if (chip_id == btrtl_dev->rom_version + 1) {
  317. patch_length = get_unaligned_le16(patch_length_base +
  318. (i * sizeof(u16)));
  319. patch_offset = get_unaligned_le32(patch_offset_base +
  320. (i * sizeof(u32)));
  321. break;
  322. }
  323. }
  324. if (!patch_offset) {
  325. rtl_dev_err(hdev, "didn't find patch for chip id %d",
  326. btrtl_dev->rom_version);
  327. return -EINVAL;
  328. }
  329. BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
  330. min_size = patch_offset + patch_length;
  331. if (btrtl_dev->fw_len < min_size)
  332. return -EINVAL;
  333. /* Copy the firmware into a new buffer and write the version at
  334. * the end.
  335. */
  336. len = patch_length;
  337. buf = kvmalloc(patch_length, GFP_KERNEL);
  338. if (!buf)
  339. return -ENOMEM;
  340. memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
  341. memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
  342. *_buf = buf;
  343. return len;
  344. }
  345. static int rtl_download_firmware(struct hci_dev *hdev,
  346. const unsigned char *data, int fw_len)
  347. {
  348. struct rtl_download_cmd *dl_cmd;
  349. int frag_num = fw_len / RTL_FRAG_LEN + 1;
  350. int frag_len = RTL_FRAG_LEN;
  351. int ret = 0;
  352. int i;
  353. struct sk_buff *skb;
  354. struct hci_rp_read_local_version *rp;
  355. dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
  356. if (!dl_cmd)
  357. return -ENOMEM;
  358. for (i = 0; i < frag_num; i++) {
  359. struct sk_buff *skb;
  360. BT_DBG("download fw (%d/%d)", i, frag_num);
  361. if (i > 0x7f)
  362. dl_cmd->index = (i & 0x7f) + 1;
  363. else
  364. dl_cmd->index = i;
  365. if (i == (frag_num - 1)) {
  366. dl_cmd->index |= 0x80; /* data end */
  367. frag_len = fw_len % RTL_FRAG_LEN;
  368. }
  369. memcpy(dl_cmd->data, data, frag_len);
  370. /* Send download command */
  371. skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
  372. HCI_INIT_TIMEOUT);
  373. if (IS_ERR(skb)) {
  374. rtl_dev_err(hdev, "download fw command failed (%ld)",
  375. PTR_ERR(skb));
  376. ret = PTR_ERR(skb);
  377. goto out;
  378. }
  379. if (skb->len != sizeof(struct rtl_download_response)) {
  380. rtl_dev_err(hdev, "download fw event length mismatch");
  381. kfree_skb(skb);
  382. ret = -EIO;
  383. goto out;
  384. }
  385. kfree_skb(skb);
  386. data += RTL_FRAG_LEN;
  387. }
  388. skb = btrtl_read_local_version(hdev);
  389. if (IS_ERR(skb)) {
  390. ret = PTR_ERR(skb);
  391. rtl_dev_err(hdev, "read local version failed");
  392. goto out;
  393. }
  394. rp = (struct hci_rp_read_local_version *)skb->data;
  395. rtl_dev_info(hdev, "fw version 0x%04x%04x",
  396. __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
  397. kfree_skb(skb);
  398. out:
  399. kfree(dl_cmd);
  400. return ret;
  401. }
  402. static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
  403. {
  404. const struct firmware *fw;
  405. int ret;
  406. rtl_dev_info(hdev, "loading %s", name);
  407. ret = request_firmware(&fw, name, &hdev->dev);
  408. if (ret < 0)
  409. return ret;
  410. ret = fw->size;
  411. *buff = kvmalloc(fw->size, GFP_KERNEL);
  412. if (*buff)
  413. memcpy(*buff, fw->data, ret);
  414. else
  415. ret = -ENOMEM;
  416. release_firmware(fw);
  417. return ret;
  418. }
  419. static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
  420. struct btrtl_device_info *btrtl_dev)
  421. {
  422. if (btrtl_dev->fw_len < 8)
  423. return -EINVAL;
  424. /* Check that the firmware doesn't have the epatch signature
  425. * (which is only for RTL8723B and newer).
  426. */
  427. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
  428. rtl_dev_err(hdev, "unexpected EPATCH signature!");
  429. return -EINVAL;
  430. }
  431. return rtl_download_firmware(hdev, btrtl_dev->fw_data,
  432. btrtl_dev->fw_len);
  433. }
  434. static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
  435. struct btrtl_device_info *btrtl_dev)
  436. {
  437. unsigned char *fw_data = NULL;
  438. int ret;
  439. u8 *tbuff;
  440. ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
  441. if (ret < 0)
  442. goto out;
  443. if (btrtl_dev->cfg_len > 0) {
  444. tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
  445. if (!tbuff) {
  446. ret = -ENOMEM;
  447. goto out;
  448. }
  449. memcpy(tbuff, fw_data, ret);
  450. kvfree(fw_data);
  451. memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
  452. ret += btrtl_dev->cfg_len;
  453. fw_data = tbuff;
  454. }
  455. rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
  456. ret = rtl_download_firmware(hdev, fw_data, ret);
  457. out:
  458. kvfree(fw_data);
  459. return ret;
  460. }
  461. void btrtl_free(struct btrtl_device_info *btrtl_dev)
  462. {
  463. kvfree(btrtl_dev->fw_data);
  464. kvfree(btrtl_dev->cfg_data);
  465. kfree(btrtl_dev);
  466. }
  467. EXPORT_SYMBOL_GPL(btrtl_free);
  468. struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
  469. const char *postfix)
  470. {
  471. struct btrtl_device_info *btrtl_dev;
  472. struct sk_buff *skb;
  473. struct hci_rp_read_local_version *resp;
  474. char cfg_name[40];
  475. u16 hci_rev, lmp_subver;
  476. u8 hci_ver;
  477. int ret;
  478. btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
  479. if (!btrtl_dev) {
  480. ret = -ENOMEM;
  481. goto err_alloc;
  482. }
  483. skb = btrtl_read_local_version(hdev);
  484. if (IS_ERR(skb)) {
  485. ret = PTR_ERR(skb);
  486. goto err_free;
  487. }
  488. resp = (struct hci_rp_read_local_version *)skb->data;
  489. rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
  490. resp->hci_ver, resp->hci_rev,
  491. resp->lmp_ver, resp->lmp_subver);
  492. hci_ver = resp->hci_ver;
  493. hci_rev = le16_to_cpu(resp->hci_rev);
  494. lmp_subver = le16_to_cpu(resp->lmp_subver);
  495. kfree_skb(skb);
  496. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
  497. hdev->bus);
  498. if (!btrtl_dev->ic_info) {
  499. rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
  500. lmp_subver, hci_rev, hci_ver);
  501. return btrtl_dev;
  502. }
  503. if (btrtl_dev->ic_info->has_rom_version) {
  504. ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
  505. if (ret)
  506. goto err_free;
  507. }
  508. btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
  509. &btrtl_dev->fw_data);
  510. if (btrtl_dev->fw_len < 0) {
  511. rtl_dev_err(hdev, "firmware file %s not found",
  512. btrtl_dev->ic_info->fw_name);
  513. ret = btrtl_dev->fw_len;
  514. goto err_free;
  515. }
  516. if (btrtl_dev->ic_info->cfg_name) {
  517. if (postfix) {
  518. snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
  519. btrtl_dev->ic_info->cfg_name, postfix);
  520. } else {
  521. snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
  522. btrtl_dev->ic_info->cfg_name);
  523. }
  524. btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
  525. &btrtl_dev->cfg_data);
  526. if (btrtl_dev->ic_info->config_needed &&
  527. btrtl_dev->cfg_len <= 0) {
  528. rtl_dev_err(hdev, "mandatory config file %s not found",
  529. btrtl_dev->ic_info->cfg_name);
  530. ret = btrtl_dev->cfg_len;
  531. goto err_free;
  532. }
  533. }
  534. return btrtl_dev;
  535. err_free:
  536. btrtl_free(btrtl_dev);
  537. err_alloc:
  538. return ERR_PTR(ret);
  539. }
  540. EXPORT_SYMBOL_GPL(btrtl_initialize);
  541. int btrtl_download_firmware(struct hci_dev *hdev,
  542. struct btrtl_device_info *btrtl_dev)
  543. {
  544. /* Match a set of subver values that correspond to stock firmware,
  545. * which is not compatible with standard btusb.
  546. * If matched, upload an alternative firmware that does conform to
  547. * standard btusb. Once that firmware is uploaded, the subver changes
  548. * to a different value.
  549. */
  550. if (!btrtl_dev->ic_info) {
  551. rtl_dev_info(hdev, "assuming no firmware upload needed");
  552. return 0;
  553. }
  554. switch (btrtl_dev->ic_info->lmp_subver) {
  555. case RTL_ROM_LMP_8723A:
  556. case RTL_ROM_LMP_3499:
  557. return btrtl_setup_rtl8723a(hdev, btrtl_dev);
  558. case RTL_ROM_LMP_8723B:
  559. case RTL_ROM_LMP_8821A:
  560. case RTL_ROM_LMP_8761A:
  561. case RTL_ROM_LMP_8822B:
  562. return btrtl_setup_rtl8723b(hdev, btrtl_dev);
  563. default:
  564. rtl_dev_info(hdev, "assuming no firmware upload needed");
  565. return 0;
  566. }
  567. }
  568. EXPORT_SYMBOL_GPL(btrtl_download_firmware);
  569. int btrtl_setup_realtek(struct hci_dev *hdev)
  570. {
  571. struct btrtl_device_info *btrtl_dev;
  572. int ret;
  573. btrtl_dev = btrtl_initialize(hdev, NULL);
  574. if (IS_ERR(btrtl_dev))
  575. return PTR_ERR(btrtl_dev);
  576. ret = btrtl_download_firmware(hdev, btrtl_dev);
  577. btrtl_free(btrtl_dev);
  578. /* Enable controller to do both LE scan and BR/EDR inquiry
  579. * simultaneously.
  580. */
  581. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  582. return ret;
  583. }
  584. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  585. int btrtl_shutdown_realtek(struct hci_dev *hdev)
  586. {
  587. struct sk_buff *skb;
  588. int ret;
  589. /* According to the vendor driver, BT must be reset on close to avoid
  590. * firmware crash.
  591. */
  592. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  593. if (IS_ERR(skb)) {
  594. ret = PTR_ERR(skb);
  595. bt_dev_err(hdev, "HCI reset during shutdown failed");
  596. return ret;
  597. }
  598. kfree_skb(skb);
  599. return 0;
  600. }
  601. EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
  602. static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
  603. {
  604. switch (device_baudrate) {
  605. case 0x0252a00a:
  606. return 230400;
  607. case 0x05f75004:
  608. return 921600;
  609. case 0x00005004:
  610. return 1000000;
  611. case 0x04928002:
  612. case 0x01128002:
  613. return 1500000;
  614. case 0x00005002:
  615. return 2000000;
  616. case 0x0000b001:
  617. return 2500000;
  618. case 0x04928001:
  619. return 3000000;
  620. case 0x052a6001:
  621. return 3500000;
  622. case 0x00005001:
  623. return 4000000;
  624. case 0x0252c014:
  625. default:
  626. return 115200;
  627. }
  628. }
  629. int btrtl_get_uart_settings(struct hci_dev *hdev,
  630. struct btrtl_device_info *btrtl_dev,
  631. unsigned int *controller_baudrate,
  632. u32 *device_baudrate, bool *flow_control)
  633. {
  634. struct rtl_vendor_config *config;
  635. struct rtl_vendor_config_entry *entry;
  636. int i, total_data_len;
  637. bool found = false;
  638. total_data_len = btrtl_dev->cfg_len - sizeof(*config);
  639. if (total_data_len <= 0) {
  640. rtl_dev_warn(hdev, "no config loaded");
  641. return -EINVAL;
  642. }
  643. config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
  644. if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
  645. rtl_dev_err(hdev, "invalid config magic");
  646. return -EINVAL;
  647. }
  648. if (total_data_len < le16_to_cpu(config->total_len)) {
  649. rtl_dev_err(hdev, "config is too short");
  650. return -EINVAL;
  651. }
  652. for (i = 0; i < total_data_len; ) {
  653. entry = ((void *)config->entry) + i;
  654. switch (le16_to_cpu(entry->offset)) {
  655. case 0xc:
  656. if (entry->len < sizeof(*device_baudrate)) {
  657. rtl_dev_err(hdev, "invalid UART config entry");
  658. return -EINVAL;
  659. }
  660. *device_baudrate = get_unaligned_le32(entry->data);
  661. *controller_baudrate = btrtl_convert_baudrate(
  662. *device_baudrate);
  663. if (entry->len >= 13)
  664. *flow_control = !!(entry->data[12] & BIT(2));
  665. else
  666. *flow_control = false;
  667. found = true;
  668. break;
  669. default:
  670. rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
  671. le16_to_cpu(entry->offset), entry->len);
  672. break;
  673. }
  674. i += sizeof(*entry) + entry->len;
  675. }
  676. if (!found) {
  677. rtl_dev_err(hdev, "no UART config entry found");
  678. return -ENOENT;
  679. }
  680. rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
  681. rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
  682. rtl_dev_dbg(hdev, "flow control %d", *flow_control);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
  686. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  687. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  688. MODULE_VERSION(VERSION);
  689. MODULE_LICENSE("GPL");
  690. MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
  691. MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
  692. MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
  693. MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
  694. MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
  695. MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
  696. MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
  697. MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
  698. MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
  699. MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
  700. MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
  701. MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
  702. MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");