btrtl.c 19 KB

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