btrtl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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 = kvmalloc(patch_length, GFP_KERNEL);
  299. if (!buf)
  300. return -ENOMEM;
  301. memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
  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 = kvmalloc(fw->size, GFP_KERNEL);
  358. if (*buff)
  359. memcpy(*buff, fw->data, ret);
  360. else
  361. ret = -ENOMEM;
  362. release_firmware(fw);
  363. return ret;
  364. }
  365. static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
  366. struct btrtl_device_info *btrtl_dev)
  367. {
  368. if (btrtl_dev->fw_len < 8)
  369. return -EINVAL;
  370. /* Check that the firmware doesn't have the epatch signature
  371. * (which is only for RTL8723B and newer).
  372. */
  373. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
  374. rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
  375. return -EINVAL;
  376. }
  377. return rtl_download_firmware(hdev, btrtl_dev->fw_data,
  378. btrtl_dev->fw_len);
  379. }
  380. static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
  381. struct btrtl_device_info *btrtl_dev)
  382. {
  383. unsigned char *fw_data = NULL;
  384. int ret;
  385. u8 *tbuff;
  386. ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
  387. if (ret < 0)
  388. goto out;
  389. if (btrtl_dev->cfg_len > 0) {
  390. tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
  391. if (!tbuff) {
  392. ret = -ENOMEM;
  393. goto out;
  394. }
  395. memcpy(tbuff, fw_data, ret);
  396. kvfree(fw_data);
  397. memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
  398. ret += btrtl_dev->cfg_len;
  399. fw_data = tbuff;
  400. }
  401. rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
  402. ret = rtl_download_firmware(hdev, fw_data, ret);
  403. out:
  404. kvfree(fw_data);
  405. return ret;
  406. }
  407. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  408. {
  409. struct sk_buff *skb;
  410. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  411. HCI_INIT_TIMEOUT);
  412. if (IS_ERR(skb)) {
  413. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
  414. PTR_ERR(skb));
  415. return skb;
  416. }
  417. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  418. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
  419. kfree_skb(skb);
  420. return ERR_PTR(-EIO);
  421. }
  422. return skb;
  423. }
  424. void btrtl_free(struct btrtl_device_info *btrtl_dev)
  425. {
  426. kvfree(btrtl_dev->fw_data);
  427. kvfree(btrtl_dev->cfg_data);
  428. kfree(btrtl_dev);
  429. }
  430. EXPORT_SYMBOL_GPL(btrtl_free);
  431. struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
  432. const char *postfix)
  433. {
  434. struct btrtl_device_info *btrtl_dev;
  435. struct sk_buff *skb;
  436. struct hci_rp_read_local_version *resp;
  437. char cfg_name[40];
  438. u16 hci_rev, lmp_subver;
  439. u8 hci_ver;
  440. int ret;
  441. btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
  442. if (!btrtl_dev) {
  443. ret = -ENOMEM;
  444. goto err_alloc;
  445. }
  446. skb = btrtl_read_local_version(hdev);
  447. if (IS_ERR(skb)) {
  448. ret = PTR_ERR(skb);
  449. goto err_free;
  450. }
  451. resp = (struct hci_rp_read_local_version *)skb->data;
  452. rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
  453. resp->hci_ver, resp->hci_rev,
  454. resp->lmp_ver, resp->lmp_subver);
  455. hci_ver = resp->hci_ver;
  456. hci_rev = le16_to_cpu(resp->hci_rev);
  457. lmp_subver = le16_to_cpu(resp->lmp_subver);
  458. kfree_skb(skb);
  459. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
  460. hdev->bus);
  461. if (!btrtl_dev->ic_info) {
  462. rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
  463. lmp_subver, hci_rev, hci_ver);
  464. return btrtl_dev;
  465. }
  466. if (btrtl_dev->ic_info->has_rom_version) {
  467. ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
  468. if (ret)
  469. goto err_free;
  470. }
  471. btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
  472. &btrtl_dev->fw_data);
  473. if (btrtl_dev->fw_len < 0) {
  474. rtl_dev_err(hdev, "firmware file %s not found\n",
  475. btrtl_dev->ic_info->fw_name);
  476. ret = btrtl_dev->fw_len;
  477. goto err_free;
  478. }
  479. if (btrtl_dev->ic_info->cfg_name) {
  480. if (postfix) {
  481. snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
  482. btrtl_dev->ic_info->cfg_name, postfix);
  483. } else {
  484. snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
  485. btrtl_dev->ic_info->cfg_name);
  486. }
  487. btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
  488. &btrtl_dev->cfg_data);
  489. if (btrtl_dev->ic_info->config_needed &&
  490. btrtl_dev->cfg_len <= 0) {
  491. rtl_dev_err(hdev, "mandatory config file %s not found\n",
  492. btrtl_dev->ic_info->cfg_name);
  493. ret = btrtl_dev->cfg_len;
  494. goto err_free;
  495. }
  496. }
  497. return btrtl_dev;
  498. err_free:
  499. btrtl_free(btrtl_dev);
  500. err_alloc:
  501. return ERR_PTR(ret);
  502. }
  503. EXPORT_SYMBOL_GPL(btrtl_initialize);
  504. int btrtl_download_firmware(struct hci_dev *hdev,
  505. struct btrtl_device_info *btrtl_dev)
  506. {
  507. /* Match a set of subver values that correspond to stock firmware,
  508. * which is not compatible with standard btusb.
  509. * If matched, upload an alternative firmware that does conform to
  510. * standard btusb. Once that firmware is uploaded, the subver changes
  511. * to a different value.
  512. */
  513. if (!btrtl_dev->ic_info) {
  514. rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
  515. return 0;
  516. }
  517. switch (btrtl_dev->ic_info->lmp_subver) {
  518. case RTL_ROM_LMP_8723A:
  519. case RTL_ROM_LMP_3499:
  520. return btrtl_setup_rtl8723a(hdev, btrtl_dev);
  521. case RTL_ROM_LMP_8723B:
  522. case RTL_ROM_LMP_8821A:
  523. case RTL_ROM_LMP_8761A:
  524. case RTL_ROM_LMP_8822B:
  525. return btrtl_setup_rtl8723b(hdev, btrtl_dev);
  526. default:
  527. rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
  528. return 0;
  529. }
  530. }
  531. EXPORT_SYMBOL_GPL(btrtl_download_firmware);
  532. int btrtl_setup_realtek(struct hci_dev *hdev)
  533. {
  534. struct btrtl_device_info *btrtl_dev;
  535. int ret;
  536. btrtl_dev = btrtl_initialize(hdev, NULL);
  537. if (IS_ERR(btrtl_dev))
  538. return PTR_ERR(btrtl_dev);
  539. ret = btrtl_download_firmware(hdev, btrtl_dev);
  540. btrtl_free(btrtl_dev);
  541. return ret;
  542. }
  543. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  544. int btrtl_shutdown_realtek(struct hci_dev *hdev)
  545. {
  546. struct sk_buff *skb;
  547. int ret;
  548. /* According to the vendor driver, BT must be reset on close to avoid
  549. * firmware crash.
  550. */
  551. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  552. if (IS_ERR(skb)) {
  553. ret = PTR_ERR(skb);
  554. bt_dev_err(hdev, "HCI reset during shutdown failed");
  555. return ret;
  556. }
  557. kfree_skb(skb);
  558. return 0;
  559. }
  560. EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
  561. static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
  562. {
  563. switch (device_baudrate) {
  564. case 0x0252a00a:
  565. return 230400;
  566. case 0x05f75004:
  567. return 921600;
  568. case 0x00005004:
  569. return 1000000;
  570. case 0x04928002:
  571. case 0x01128002:
  572. return 1500000;
  573. case 0x00005002:
  574. return 2000000;
  575. case 0x0000b001:
  576. return 2500000;
  577. case 0x04928001:
  578. return 3000000;
  579. case 0x052a6001:
  580. return 3500000;
  581. case 0x00005001:
  582. return 4000000;
  583. case 0x0252c014:
  584. default:
  585. return 115200;
  586. }
  587. }
  588. int btrtl_get_uart_settings(struct hci_dev *hdev,
  589. struct btrtl_device_info *btrtl_dev,
  590. unsigned int *controller_baudrate,
  591. u32 *device_baudrate, bool *flow_control)
  592. {
  593. struct rtl_vendor_config *config;
  594. struct rtl_vendor_config_entry *entry;
  595. int i, total_data_len;
  596. bool found = false;
  597. total_data_len = btrtl_dev->cfg_len - sizeof(*config);
  598. if (total_data_len <= 0) {
  599. rtl_dev_warn(hdev, "no config loaded\n");
  600. return -EINVAL;
  601. }
  602. config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
  603. if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
  604. rtl_dev_err(hdev, "invalid config magic\n");
  605. return -EINVAL;
  606. }
  607. if (total_data_len < le16_to_cpu(config->total_len)) {
  608. rtl_dev_err(hdev, "config is too short\n");
  609. return -EINVAL;
  610. }
  611. for (i = 0; i < total_data_len; ) {
  612. entry = ((void *)config->entry) + i;
  613. switch (le16_to_cpu(entry->offset)) {
  614. case 0xc:
  615. if (entry->len < sizeof(*device_baudrate)) {
  616. rtl_dev_err(hdev, "invalid UART config entry\n");
  617. return -EINVAL;
  618. }
  619. *device_baudrate = get_unaligned_le32(entry->data);
  620. *controller_baudrate = btrtl_convert_baudrate(
  621. *device_baudrate);
  622. if (entry->len >= 13)
  623. *flow_control = !!(entry->data[12] & BIT(2));
  624. else
  625. *flow_control = false;
  626. found = true;
  627. break;
  628. default:
  629. rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
  630. le16_to_cpu(entry->offset), entry->len);
  631. break;
  632. };
  633. i += sizeof(*entry) + entry->len;
  634. }
  635. if (!found) {
  636. rtl_dev_err(hdev, "no UART config entry found\n");
  637. return -ENOENT;
  638. }
  639. rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
  640. rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
  641. rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
  642. return 0;
  643. }
  644. EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
  645. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  646. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  647. MODULE_VERSION(VERSION);
  648. MODULE_LICENSE("GPL");
  649. MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
  650. MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
  651. MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
  652. MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
  653. MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
  654. MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
  655. MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
  656. MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
  657. MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
  658. MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
  659. MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
  660. MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
  661. MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");