btrtl.c 19 KB

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