btrtl.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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_CHIP_8723CS_CG 3
  16. #define RTL_CHIP_8723CS_VF 4
  17. #define RTL_CHIP_8723CS_XX 5
  18. #define RTL_EPATCH_SIGNATURE "Realtech"
  19. #define RTL_ROM_LMP_8703B 0x8703
  20. #define RTL_ROM_LMP_8723A 0x1200
  21. #define RTL_ROM_LMP_8723B 0x8723
  22. #define RTL_ROM_LMP_8821A 0x8821
  23. #define RTL_ROM_LMP_8761A 0x8761
  24. #define RTL_ROM_LMP_8822B 0x8822
  25. #define RTL_ROM_LMP_8852A 0x8852
  26. #define RTL_ROM_LMP_8851B 0x8851
  27. #define RTL_CONFIG_MAGIC 0x8723ab55
  28. #define IC_MATCH_FL_LMPSUBV (1 << 0)
  29. #define IC_MATCH_FL_HCIREV (1 << 1)
  30. #define IC_MATCH_FL_HCIVER (1 << 2)
  31. #define IC_MATCH_FL_HCIBUS (1 << 3)
  32. #define IC_MATCH_FL_CHIP_TYPE (1 << 4)
  33. #define IC_INFO(lmps, hcir, hciv, bus) \
  34. .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \
  35. IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \
  36. .lmp_subver = (lmps), \
  37. .hci_rev = (hcir), \
  38. .hci_ver = (hciv), \
  39. .hci_bus = (bus)
  40. enum btrtl_chip_id {
  41. CHIP_ID_8723A,
  42. CHIP_ID_8723B,
  43. CHIP_ID_8821A,
  44. CHIP_ID_8761A,
  45. CHIP_ID_8822B = 8,
  46. CHIP_ID_8723D,
  47. CHIP_ID_8821C,
  48. CHIP_ID_8822C = 13,
  49. CHIP_ID_8761B,
  50. CHIP_ID_8852A = 18,
  51. CHIP_ID_8852B = 20,
  52. CHIP_ID_8852C = 25,
  53. CHIP_ID_8851B = 36,
  54. };
  55. struct id_table {
  56. __u16 match_flags;
  57. __u16 lmp_subver;
  58. __u16 hci_rev;
  59. __u8 hci_ver;
  60. __u8 hci_bus;
  61. __u8 chip_type;
  62. bool config_needed;
  63. bool has_rom_version;
  64. bool has_msft_ext;
  65. char *fw_name;
  66. char *cfg_name;
  67. };
  68. struct btrtl_device_info {
  69. const struct id_table *ic_info;
  70. u8 rom_version;
  71. u8 *fw_data;
  72. int fw_len;
  73. u8 *cfg_data;
  74. int cfg_len;
  75. bool drop_fw;
  76. int project_id;
  77. };
  78. static const struct id_table ic_id_table[] = {
  79. /* 8723A */
  80. { IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB),
  81. .config_needed = false,
  82. .has_rom_version = false,
  83. .fw_name = "rtl_bt/rtl8723a_fw.bin",
  84. .cfg_name = NULL },
  85. /* 8723BS */
  86. { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART),
  87. .config_needed = true,
  88. .has_rom_version = true,
  89. .fw_name = "rtl_bt/rtl8723bs_fw.bin",
  90. .cfg_name = "rtl_bt/rtl8723bs_config" },
  91. /* 8723B */
  92. { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB),
  93. .config_needed = false,
  94. .has_rom_version = true,
  95. .fw_name = "rtl_bt/rtl8723b_fw.bin",
  96. .cfg_name = "rtl_bt/rtl8723b_config" },
  97. /* 8723CS-CG */
  98. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  99. IC_MATCH_FL_HCIBUS,
  100. .lmp_subver = RTL_ROM_LMP_8703B,
  101. .chip_type = RTL_CHIP_8723CS_CG,
  102. .hci_bus = HCI_UART,
  103. .config_needed = true,
  104. .has_rom_version = true,
  105. .fw_name = "rtl_bt/rtl8723cs_cg_fw.bin",
  106. .cfg_name = "rtl_bt/rtl8723cs_cg_config" },
  107. /* 8723CS-VF */
  108. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  109. IC_MATCH_FL_HCIBUS,
  110. .lmp_subver = RTL_ROM_LMP_8703B,
  111. .chip_type = RTL_CHIP_8723CS_VF,
  112. .hci_bus = HCI_UART,
  113. .config_needed = true,
  114. .has_rom_version = true,
  115. .fw_name = "rtl_bt/rtl8723cs_vf_fw.bin",
  116. .cfg_name = "rtl_bt/rtl8723cs_vf_config" },
  117. /* 8723CS-XX */
  118. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  119. IC_MATCH_FL_HCIBUS,
  120. .lmp_subver = RTL_ROM_LMP_8703B,
  121. .chip_type = RTL_CHIP_8723CS_XX,
  122. .hci_bus = HCI_UART,
  123. .config_needed = true,
  124. .has_rom_version = true,
  125. .fw_name = "rtl_bt/rtl8723cs_xx_fw.bin",
  126. .cfg_name = "rtl_bt/rtl8723cs_xx_config" },
  127. /* 8723D */
  128. { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB),
  129. .config_needed = true,
  130. .has_rom_version = true,
  131. .fw_name = "rtl_bt/rtl8723d_fw.bin",
  132. .cfg_name = "rtl_bt/rtl8723d_config" },
  133. /* 8723DS */
  134. { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART),
  135. .config_needed = true,
  136. .has_rom_version = true,
  137. .fw_name = "rtl_bt/rtl8723ds_fw.bin",
  138. .cfg_name = "rtl_bt/rtl8723ds_config" },
  139. /* 8821A */
  140. { IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB),
  141. .config_needed = false,
  142. .has_rom_version = true,
  143. .fw_name = "rtl_bt/rtl8821a_fw.bin",
  144. .cfg_name = "rtl_bt/rtl8821a_config" },
  145. /* 8821C */
  146. { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB),
  147. .config_needed = false,
  148. .has_rom_version = true,
  149. .has_msft_ext = true,
  150. .fw_name = "rtl_bt/rtl8821c_fw.bin",
  151. .cfg_name = "rtl_bt/rtl8821c_config" },
  152. /* 8761A */
  153. { IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB),
  154. .config_needed = false,
  155. .has_rom_version = true,
  156. .fw_name = "rtl_bt/rtl8761a_fw.bin",
  157. .cfg_name = "rtl_bt/rtl8761a_config" },
  158. /* 8761B */
  159. { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART),
  160. .config_needed = false,
  161. .has_rom_version = true,
  162. .has_msft_ext = true,
  163. .fw_name = "rtl_bt/rtl8761b_fw.bin",
  164. .cfg_name = "rtl_bt/rtl8761b_config" },
  165. /* 8761BU */
  166. { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB),
  167. .config_needed = false,
  168. .has_rom_version = true,
  169. .fw_name = "rtl_bt/rtl8761bu_fw.bin",
  170. .cfg_name = "rtl_bt/rtl8761bu_config" },
  171. /* 8822C with UART interface */
  172. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART),
  173. .config_needed = true,
  174. .has_rom_version = true,
  175. .has_msft_ext = true,
  176. .fw_name = "rtl_bt/rtl8822cs_fw.bin",
  177. .cfg_name = "rtl_bt/rtl8822cs_config" },
  178. /* 8822C with UART interface */
  179. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART),
  180. .config_needed = true,
  181. .has_rom_version = true,
  182. .has_msft_ext = true,
  183. .fw_name = "rtl_bt/rtl8822cs_fw.bin",
  184. .cfg_name = "rtl_bt/rtl8822cs_config" },
  185. /* 8822C with USB interface */
  186. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB),
  187. .config_needed = false,
  188. .has_rom_version = true,
  189. .has_msft_ext = true,
  190. .fw_name = "rtl_bt/rtl8822cu_fw.bin",
  191. .cfg_name = "rtl_bt/rtl8822cu_config" },
  192. /* 8822B */
  193. { IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB),
  194. .config_needed = true,
  195. .has_rom_version = true,
  196. .has_msft_ext = true,
  197. .fw_name = "rtl_bt/rtl8822b_fw.bin",
  198. .cfg_name = "rtl_bt/rtl8822b_config" },
  199. /* 8852A */
  200. { IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB),
  201. .config_needed = false,
  202. .has_rom_version = true,
  203. .has_msft_ext = true,
  204. .fw_name = "rtl_bt/rtl8852au_fw.bin",
  205. .cfg_name = "rtl_bt/rtl8852au_config" },
  206. /* 8852B */
  207. { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB),
  208. .config_needed = false,
  209. .has_rom_version = true,
  210. .has_msft_ext = true,
  211. .fw_name = "rtl_bt/rtl8852bu_fw.bin",
  212. .cfg_name = "rtl_bt/rtl8852bu_config" },
  213. /* 8852C */
  214. { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB),
  215. .config_needed = false,
  216. .has_rom_version = true,
  217. .has_msft_ext = true,
  218. .fw_name = "rtl_bt/rtl8852cu_fw.bin",
  219. .cfg_name = "rtl_bt/rtl8852cu_config" },
  220. /* 8851B */
  221. { IC_INFO(RTL_ROM_LMP_8851B, 0xb, 0xc, HCI_USB),
  222. .config_needed = false,
  223. .has_rom_version = true,
  224. .has_msft_ext = false,
  225. .fw_name = "rtl_bt/rtl8851bu_fw.bin",
  226. .cfg_name = "rtl_bt/rtl8851bu_config" },
  227. };
  228. static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
  229. u8 hci_ver, u8 hci_bus,
  230. u8 chip_type)
  231. {
  232. int i;
  233. for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
  234. if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
  235. (ic_id_table[i].lmp_subver != lmp_subver))
  236. continue;
  237. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
  238. (ic_id_table[i].hci_rev != hci_rev))
  239. continue;
  240. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
  241. (ic_id_table[i].hci_ver != hci_ver))
  242. continue;
  243. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
  244. (ic_id_table[i].hci_bus != hci_bus))
  245. continue;
  246. if ((ic_id_table[i].match_flags & IC_MATCH_FL_CHIP_TYPE) &&
  247. (ic_id_table[i].chip_type != chip_type))
  248. continue;
  249. break;
  250. }
  251. if (i >= ARRAY_SIZE(ic_id_table))
  252. return NULL;
  253. return &ic_id_table[i];
  254. }
  255. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  256. {
  257. struct sk_buff *skb;
  258. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  259. HCI_INIT_TIMEOUT);
  260. if (IS_ERR(skb)) {
  261. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
  262. PTR_ERR(skb));
  263. return skb;
  264. }
  265. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  266. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
  267. kfree_skb(skb);
  268. return ERR_PTR(-EIO);
  269. }
  270. return skb;
  271. }
  272. static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
  273. {
  274. struct rtl_rom_version_evt *rom_version;
  275. struct sk_buff *skb;
  276. /* Read RTL ROM version command */
  277. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  278. if (IS_ERR(skb)) {
  279. rtl_dev_err(hdev, "Read ROM version failed (%ld)",
  280. PTR_ERR(skb));
  281. return PTR_ERR(skb);
  282. }
  283. if (skb->len != sizeof(*rom_version)) {
  284. rtl_dev_err(hdev, "version event length mismatch");
  285. kfree_skb(skb);
  286. return -EIO;
  287. }
  288. rom_version = (struct rtl_rom_version_evt *)skb->data;
  289. rtl_dev_info(hdev, "rom_version status=%x version=%x",
  290. rom_version->status, rom_version->version);
  291. *version = rom_version->version;
  292. kfree_skb(skb);
  293. return 0;
  294. }
  295. static int rtlbt_parse_firmware(struct hci_dev *hdev,
  296. struct btrtl_device_info *btrtl_dev,
  297. unsigned char **_buf)
  298. {
  299. static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
  300. struct rtl_epatch_header *epatch_info;
  301. unsigned char *buf;
  302. int i, len;
  303. size_t min_size;
  304. u8 opcode, length, data;
  305. int project_id = -1;
  306. const unsigned char *fwptr, *chip_id_base;
  307. const unsigned char *patch_length_base, *patch_offset_base;
  308. u32 patch_offset = 0;
  309. u16 patch_length, num_patches;
  310. static const struct {
  311. __u16 lmp_subver;
  312. __u8 id;
  313. } project_id_to_lmp_subver[] = {
  314. { RTL_ROM_LMP_8723A, 0 },
  315. { RTL_ROM_LMP_8723B, 1 },
  316. { RTL_ROM_LMP_8821A, 2 },
  317. { RTL_ROM_LMP_8761A, 3 },
  318. { RTL_ROM_LMP_8703B, 7 },
  319. { RTL_ROM_LMP_8822B, 8 },
  320. { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
  321. { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
  322. { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
  323. { RTL_ROM_LMP_8761A, 14 }, /* 8761B */
  324. { RTL_ROM_LMP_8852A, 18 }, /* 8852A */
  325. { RTL_ROM_LMP_8852A, 20 }, /* 8852B */
  326. { RTL_ROM_LMP_8852A, 25 }, /* 8852C */
  327. { RTL_ROM_LMP_8851B, 36 }, /* 8851B */
  328. };
  329. min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
  330. if (btrtl_dev->fw_len < min_size)
  331. return -EINVAL;
  332. fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
  333. if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
  334. rtl_dev_err(hdev, "extension section signature mismatch");
  335. return -EINVAL;
  336. }
  337. /* Loop from the end of the firmware parsing instructions, until
  338. * we find an instruction that identifies the "project ID" for the
  339. * hardware supported by this firwmare file.
  340. * Once we have that, we double-check that project_id is suitable
  341. * for the hardware we are working with.
  342. */
  343. while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
  344. opcode = *--fwptr;
  345. length = *--fwptr;
  346. data = *--fwptr;
  347. BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
  348. if (opcode == 0xff) /* EOF */
  349. break;
  350. if (length == 0) {
  351. rtl_dev_err(hdev, "found instruction with length 0");
  352. return -EINVAL;
  353. }
  354. if (opcode == 0 && length == 1) {
  355. project_id = data;
  356. break;
  357. }
  358. fwptr -= length;
  359. }
  360. if (project_id < 0) {
  361. rtl_dev_err(hdev, "failed to find version instruction");
  362. return -EINVAL;
  363. }
  364. /* Find project_id in table */
  365. for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
  366. if (project_id == project_id_to_lmp_subver[i].id) {
  367. btrtl_dev->project_id = project_id;
  368. break;
  369. }
  370. }
  371. if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
  372. rtl_dev_err(hdev, "unknown project id %d", project_id);
  373. return -EINVAL;
  374. }
  375. if (btrtl_dev->ic_info->lmp_subver !=
  376. project_id_to_lmp_subver[i].lmp_subver) {
  377. rtl_dev_err(hdev, "firmware is for %x but this is a %x",
  378. project_id_to_lmp_subver[i].lmp_subver,
  379. btrtl_dev->ic_info->lmp_subver);
  380. return -EINVAL;
  381. }
  382. epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
  383. if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
  384. rtl_dev_err(hdev, "bad EPATCH signature");
  385. return -EINVAL;
  386. }
  387. num_patches = le16_to_cpu(epatch_info->num_patches);
  388. BT_DBG("fw_version=%x, num_patches=%d",
  389. le32_to_cpu(epatch_info->fw_version), num_patches);
  390. /* After the rtl_epatch_header there is a funky patch metadata section.
  391. * Assuming 2 patches, the layout is:
  392. * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
  393. *
  394. * Find the right patch for this chip.
  395. */
  396. min_size += 8 * num_patches;
  397. if (btrtl_dev->fw_len < min_size)
  398. return -EINVAL;
  399. chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
  400. patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
  401. patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
  402. for (i = 0; i < num_patches; i++) {
  403. u16 chip_id = get_unaligned_le16(chip_id_base +
  404. (i * sizeof(u16)));
  405. if (chip_id == btrtl_dev->rom_version + 1) {
  406. patch_length = get_unaligned_le16(patch_length_base +
  407. (i * sizeof(u16)));
  408. patch_offset = get_unaligned_le32(patch_offset_base +
  409. (i * sizeof(u32)));
  410. break;
  411. }
  412. }
  413. if (!patch_offset) {
  414. rtl_dev_err(hdev, "didn't find patch for chip id %d",
  415. btrtl_dev->rom_version);
  416. return -EINVAL;
  417. }
  418. BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
  419. min_size = patch_offset + patch_length;
  420. if (btrtl_dev->fw_len < min_size)
  421. return -EINVAL;
  422. /* Copy the firmware into a new buffer and write the version at
  423. * the end.
  424. */
  425. len = patch_length;
  426. buf = kvmalloc(patch_length, GFP_KERNEL);
  427. if (!buf)
  428. return -ENOMEM;
  429. memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
  430. memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
  431. *_buf = buf;
  432. return len;
  433. }
  434. static int rtl_download_firmware(struct hci_dev *hdev,
  435. const unsigned char *data, int fw_len)
  436. {
  437. struct rtl_download_cmd *dl_cmd;
  438. int frag_num = fw_len / RTL_FRAG_LEN + 1;
  439. int frag_len = RTL_FRAG_LEN;
  440. int ret = 0;
  441. int i;
  442. struct sk_buff *skb;
  443. struct hci_rp_read_local_version *rp;
  444. dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
  445. if (!dl_cmd)
  446. return -ENOMEM;
  447. for (i = 0; i < frag_num; i++) {
  448. struct sk_buff *skb;
  449. BT_DBG("download fw (%d/%d)", i, frag_num);
  450. if (i > 0x7f)
  451. dl_cmd->index = (i & 0x7f) + 1;
  452. else
  453. dl_cmd->index = i;
  454. if (i == (frag_num - 1)) {
  455. dl_cmd->index |= 0x80; /* data end */
  456. frag_len = fw_len % RTL_FRAG_LEN;
  457. }
  458. memcpy(dl_cmd->data, data, frag_len);
  459. /* Send download command */
  460. skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
  461. HCI_INIT_TIMEOUT);
  462. if (IS_ERR(skb)) {
  463. rtl_dev_err(hdev, "download fw command failed (%ld)",
  464. PTR_ERR(skb));
  465. ret = PTR_ERR(skb);
  466. goto out;
  467. }
  468. if (skb->len != sizeof(struct rtl_download_response)) {
  469. rtl_dev_err(hdev, "download fw event length mismatch");
  470. kfree_skb(skb);
  471. ret = -EIO;
  472. goto out;
  473. }
  474. kfree_skb(skb);
  475. data += RTL_FRAG_LEN;
  476. }
  477. skb = btrtl_read_local_version(hdev);
  478. if (IS_ERR(skb)) {
  479. ret = PTR_ERR(skb);
  480. rtl_dev_err(hdev, "read local version failed");
  481. goto out;
  482. }
  483. rp = (struct hci_rp_read_local_version *)skb->data;
  484. rtl_dev_info(hdev, "fw version 0x%04x%04x",
  485. __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
  486. kfree_skb(skb);
  487. out:
  488. kfree(dl_cmd);
  489. return ret;
  490. }
  491. static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
  492. {
  493. const struct firmware *fw;
  494. int ret;
  495. rtl_dev_info(hdev, "loading %s", name);
  496. ret = request_firmware(&fw, name, &hdev->dev);
  497. if (ret < 0)
  498. return ret;
  499. ret = fw->size;
  500. *buff = kvmalloc(fw->size, GFP_KERNEL);
  501. if (*buff)
  502. memcpy(*buff, fw->data, ret);
  503. else
  504. ret = -ENOMEM;
  505. release_firmware(fw);
  506. return ret;
  507. }
  508. static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
  509. struct btrtl_device_info *btrtl_dev)
  510. {
  511. if (btrtl_dev->fw_len < 8)
  512. return -EINVAL;
  513. /* Check that the firmware doesn't have the epatch signature
  514. * (which is only for RTL8723B and newer).
  515. */
  516. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
  517. rtl_dev_err(hdev, "unexpected EPATCH signature!");
  518. return -EINVAL;
  519. }
  520. return rtl_download_firmware(hdev, btrtl_dev->fw_data,
  521. btrtl_dev->fw_len);
  522. }
  523. static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
  524. struct btrtl_device_info *btrtl_dev)
  525. {
  526. unsigned char *fw_data = NULL;
  527. int ret;
  528. u8 *tbuff;
  529. ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
  530. if (ret < 0)
  531. goto out;
  532. if (btrtl_dev->cfg_len > 0) {
  533. tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
  534. if (!tbuff) {
  535. ret = -ENOMEM;
  536. goto out;
  537. }
  538. memcpy(tbuff, fw_data, ret);
  539. kvfree(fw_data);
  540. memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
  541. ret += btrtl_dev->cfg_len;
  542. fw_data = tbuff;
  543. }
  544. rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
  545. ret = rtl_download_firmware(hdev, fw_data, ret);
  546. out:
  547. kvfree(fw_data);
  548. return ret;
  549. }
  550. static bool rtl_has_chip_type(u16 lmp_subver)
  551. {
  552. switch (lmp_subver) {
  553. case RTL_ROM_LMP_8703B:
  554. return true;
  555. default:
  556. break;
  557. }
  558. return false;
  559. }
  560. static int rtl_read_chip_type(struct hci_dev *hdev, u8 *type)
  561. {
  562. struct rtl_chip_type_evt *chip_type;
  563. struct sk_buff *skb;
  564. const unsigned char cmd_buf[] = {0x00, 0x94, 0xa0, 0x00, 0xb0};
  565. /* Read RTL chip type command */
  566. skb = __hci_cmd_sync(hdev, 0xfc61, 5, cmd_buf, HCI_INIT_TIMEOUT);
  567. if (IS_ERR(skb)) {
  568. rtl_dev_err(hdev, "Read chip type failed (%ld)",
  569. PTR_ERR(skb));
  570. return PTR_ERR(skb);
  571. }
  572. chip_type = skb_pull_data(skb, sizeof(*chip_type));
  573. if (!chip_type) {
  574. rtl_dev_err(hdev, "RTL chip type event length mismatch");
  575. kfree_skb(skb);
  576. return -EIO;
  577. }
  578. rtl_dev_info(hdev, "chip_type status=%x type=%x",
  579. chip_type->status, chip_type->type);
  580. *type = chip_type->type & 0x0f;
  581. kfree_skb(skb);
  582. return 0;
  583. }
  584. void btrtl_free(struct btrtl_device_info *btrtl_dev)
  585. {
  586. kvfree(btrtl_dev->fw_data);
  587. kvfree(btrtl_dev->cfg_data);
  588. kfree(btrtl_dev);
  589. }
  590. EXPORT_SYMBOL_GPL(btrtl_free);
  591. struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
  592. const char *postfix)
  593. {
  594. struct btrtl_device_info *btrtl_dev;
  595. struct sk_buff *skb;
  596. struct hci_rp_read_local_version *resp;
  597. char cfg_name[40];
  598. u16 hci_rev, lmp_subver;
  599. u8 hci_ver, chip_type = 0;
  600. int ret;
  601. u16 opcode;
  602. u8 cmd[2];
  603. btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
  604. if (!btrtl_dev) {
  605. ret = -ENOMEM;
  606. goto err_alloc;
  607. }
  608. skb = btrtl_read_local_version(hdev);
  609. if (IS_ERR(skb)) {
  610. ret = PTR_ERR(skb);
  611. goto err_free;
  612. }
  613. resp = (struct hci_rp_read_local_version *)skb->data;
  614. rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
  615. resp->hci_ver, resp->hci_rev,
  616. resp->lmp_ver, resp->lmp_subver);
  617. hci_ver = resp->hci_ver;
  618. hci_rev = le16_to_cpu(resp->hci_rev);
  619. lmp_subver = le16_to_cpu(resp->lmp_subver);
  620. if (rtl_has_chip_type(lmp_subver)) {
  621. ret = rtl_read_chip_type(hdev, &chip_type);
  622. if (ret)
  623. goto err_free;
  624. }
  625. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
  626. hdev->bus, chip_type);
  627. if (!btrtl_dev->ic_info)
  628. btrtl_dev->drop_fw = true;
  629. if (btrtl_dev->drop_fw) {
  630. opcode = hci_opcode_pack(0x3f, 0x66);
  631. cmd[0] = opcode & 0xff;
  632. cmd[1] = opcode >> 8;
  633. skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
  634. if (!skb)
  635. goto out_free;
  636. skb_put_data(skb, cmd, sizeof(cmd));
  637. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  638. hdev->send(hdev, skb);
  639. /* Ensure the above vendor command is sent to controller and
  640. * process has done.
  641. */
  642. msleep(200);
  643. /* Read the local version again. Expect to have the vanilla
  644. * version as cold boot.
  645. */
  646. skb = btrtl_read_local_version(hdev);
  647. if (IS_ERR(skb)) {
  648. ret = PTR_ERR(skb);
  649. goto err_free;
  650. }
  651. resp = (struct hci_rp_read_local_version *)skb->data;
  652. rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
  653. resp->hci_ver, resp->hci_rev,
  654. resp->lmp_ver, resp->lmp_subver);
  655. hci_ver = resp->hci_ver;
  656. hci_rev = le16_to_cpu(resp->hci_rev);
  657. lmp_subver = le16_to_cpu(resp->lmp_subver);
  658. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
  659. hdev->bus, chip_type);
  660. }
  661. out_free:
  662. kfree_skb(skb);
  663. if (!btrtl_dev->ic_info) {
  664. rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
  665. lmp_subver, hci_rev, hci_ver);
  666. return btrtl_dev;
  667. }
  668. if (btrtl_dev->ic_info->has_rom_version) {
  669. ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
  670. if (ret)
  671. goto err_free;
  672. }
  673. btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
  674. &btrtl_dev->fw_data);
  675. if (btrtl_dev->fw_len < 0) {
  676. rtl_dev_err(hdev, "firmware file %s not found",
  677. btrtl_dev->ic_info->fw_name);
  678. ret = btrtl_dev->fw_len;
  679. goto err_free;
  680. }
  681. if (btrtl_dev->ic_info->cfg_name) {
  682. if (postfix) {
  683. snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
  684. btrtl_dev->ic_info->cfg_name, postfix);
  685. } else {
  686. snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
  687. btrtl_dev->ic_info->cfg_name);
  688. }
  689. btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
  690. &btrtl_dev->cfg_data);
  691. if (btrtl_dev->ic_info->config_needed &&
  692. btrtl_dev->cfg_len <= 0) {
  693. rtl_dev_err(hdev, "mandatory config file %s not found",
  694. btrtl_dev->ic_info->cfg_name);
  695. ret = btrtl_dev->cfg_len;
  696. goto err_free;
  697. }
  698. }
  699. /* The following chips supports the Microsoft vendor extension,
  700. * therefore set the corresponding VsMsftOpCode.
  701. */
  702. if (btrtl_dev->ic_info->has_msft_ext)
  703. hci_set_msft_opcode(hdev, 0xFCF0);
  704. return btrtl_dev;
  705. err_free:
  706. btrtl_free(btrtl_dev);
  707. err_alloc:
  708. return ERR_PTR(ret);
  709. }
  710. EXPORT_SYMBOL_GPL(btrtl_initialize);
  711. int btrtl_download_firmware(struct hci_dev *hdev,
  712. struct btrtl_device_info *btrtl_dev)
  713. {
  714. /* Match a set of subver values that correspond to stock firmware,
  715. * which is not compatible with standard btusb.
  716. * If matched, upload an alternative firmware that does conform to
  717. * standard btusb. Once that firmware is uploaded, the subver changes
  718. * to a different value.
  719. */
  720. if (!btrtl_dev->ic_info) {
  721. rtl_dev_info(hdev, "assuming no firmware upload needed");
  722. return 0;
  723. }
  724. switch (btrtl_dev->ic_info->lmp_subver) {
  725. case RTL_ROM_LMP_8723A:
  726. return btrtl_setup_rtl8723a(hdev, btrtl_dev);
  727. case RTL_ROM_LMP_8723B:
  728. case RTL_ROM_LMP_8821A:
  729. case RTL_ROM_LMP_8761A:
  730. case RTL_ROM_LMP_8822B:
  731. case RTL_ROM_LMP_8852A:
  732. case RTL_ROM_LMP_8703B:
  733. case RTL_ROM_LMP_8851B:
  734. return btrtl_setup_rtl8723b(hdev, btrtl_dev);
  735. default:
  736. rtl_dev_info(hdev, "assuming no firmware upload needed");
  737. return 0;
  738. }
  739. }
  740. EXPORT_SYMBOL_GPL(btrtl_download_firmware);
  741. void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
  742. {
  743. /* Enable controller to do both LE scan and BR/EDR inquiry
  744. * simultaneously.
  745. */
  746. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  747. /* Enable central-peripheral role (able to create new connections with
  748. * an existing connection in slave role).
  749. */
  750. /* Enable WBS supported for the specific Realtek devices. */
  751. switch (btrtl_dev->project_id) {
  752. case CHIP_ID_8822C:
  753. case CHIP_ID_8852A:
  754. case CHIP_ID_8852B:
  755. case CHIP_ID_8852C:
  756. case CHIP_ID_8851B:
  757. set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
  758. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  759. hci_set_aosp_capable(hdev);
  760. break;
  761. default:
  762. rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
  763. rtl_dev_dbg(hdev, "WBS supported not enabled.");
  764. break;
  765. }
  766. if (!btrtl_dev->ic_info)
  767. return;
  768. switch (btrtl_dev->ic_info->lmp_subver) {
  769. case RTL_ROM_LMP_8703B:
  770. /* 8723CS reports two pages for local ext features,
  771. * but it doesn't support any features from page 2 -
  772. * it either responds with garbage or with error status
  773. */
  774. set_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
  775. &hdev->quirks);
  776. break;
  777. default:
  778. break;
  779. }
  780. }
  781. EXPORT_SYMBOL_GPL(btrtl_set_quirks);
  782. int btrtl_setup_realtek(struct hci_dev *hdev)
  783. {
  784. struct btrtl_device_info *btrtl_dev;
  785. int ret;
  786. btrtl_dev = btrtl_initialize(hdev, NULL);
  787. if (IS_ERR(btrtl_dev))
  788. return PTR_ERR(btrtl_dev);
  789. ret = btrtl_download_firmware(hdev, btrtl_dev);
  790. btrtl_set_quirks(hdev, btrtl_dev);
  791. btrtl_free(btrtl_dev);
  792. return ret;
  793. }
  794. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  795. int btrtl_shutdown_realtek(struct hci_dev *hdev)
  796. {
  797. struct sk_buff *skb;
  798. int ret;
  799. /* According to the vendor driver, BT must be reset on close to avoid
  800. * firmware crash.
  801. */
  802. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  803. if (IS_ERR(skb)) {
  804. ret = PTR_ERR(skb);
  805. bt_dev_err(hdev, "HCI reset during shutdown failed");
  806. return ret;
  807. }
  808. kfree_skb(skb);
  809. return 0;
  810. }
  811. EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
  812. static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
  813. {
  814. switch (device_baudrate) {
  815. case 0x0252a00a:
  816. return 230400;
  817. case 0x05f75004:
  818. return 921600;
  819. case 0x00005004:
  820. return 1000000;
  821. case 0x04928002:
  822. case 0x01128002:
  823. return 1500000;
  824. case 0x00005002:
  825. return 2000000;
  826. case 0x0000b001:
  827. return 2500000;
  828. case 0x04928001:
  829. return 3000000;
  830. case 0x052a6001:
  831. return 3500000;
  832. case 0x00005001:
  833. return 4000000;
  834. case 0x0252c014:
  835. default:
  836. return 115200;
  837. }
  838. }
  839. int btrtl_get_uart_settings(struct hci_dev *hdev,
  840. struct btrtl_device_info *btrtl_dev,
  841. unsigned int *controller_baudrate,
  842. u32 *device_baudrate, bool *flow_control)
  843. {
  844. struct rtl_vendor_config *config;
  845. struct rtl_vendor_config_entry *entry;
  846. int i, total_data_len;
  847. bool found = false;
  848. total_data_len = btrtl_dev->cfg_len - sizeof(*config);
  849. if (total_data_len <= 0) {
  850. rtl_dev_warn(hdev, "no config loaded");
  851. return -EINVAL;
  852. }
  853. config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
  854. if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
  855. rtl_dev_err(hdev, "invalid config magic");
  856. return -EINVAL;
  857. }
  858. if (total_data_len < le16_to_cpu(config->total_len)) {
  859. rtl_dev_err(hdev, "config is too short");
  860. return -EINVAL;
  861. }
  862. for (i = 0; i < total_data_len; ) {
  863. entry = ((void *)config->entry) + i;
  864. switch (le16_to_cpu(entry->offset)) {
  865. case 0xc:
  866. if (entry->len < sizeof(*device_baudrate)) {
  867. rtl_dev_err(hdev, "invalid UART config entry");
  868. return -EINVAL;
  869. }
  870. *device_baudrate = get_unaligned_le32(entry->data);
  871. *controller_baudrate = btrtl_convert_baudrate(
  872. *device_baudrate);
  873. if (entry->len >= 13)
  874. *flow_control = !!(entry->data[12] & BIT(2));
  875. else
  876. *flow_control = false;
  877. found = true;
  878. break;
  879. default:
  880. rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
  881. le16_to_cpu(entry->offset), entry->len);
  882. break;
  883. }
  884. i += sizeof(*entry) + entry->len;
  885. }
  886. if (!found) {
  887. rtl_dev_err(hdev, "no UART config entry found");
  888. return -ENOENT;
  889. }
  890. rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
  891. rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
  892. rtl_dev_dbg(hdev, "flow control %d", *flow_control);
  893. return 0;
  894. }
  895. EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
  896. MODULE_AUTHOR("Daniel Drake <[email protected]>");
  897. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  898. MODULE_VERSION(VERSION);
  899. MODULE_LICENSE("GPL");
  900. MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
  901. MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
  902. MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
  903. MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
  904. MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
  905. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_fw.bin");
  906. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_config.bin");
  907. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_fw.bin");
  908. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_config.bin");
  909. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_fw.bin");
  910. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_config.bin");
  911. MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
  912. MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
  913. MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
  914. MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
  915. MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
  916. MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
  917. MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
  918. MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
  919. MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
  920. MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
  921. MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
  922. MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
  923. MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
  924. MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");
  925. MODULE_FIRMWARE("rtl_bt/rtl8851bu_fw.bin");
  926. MODULE_FIRMWARE("rtl_bt/rtl8851bu_config.bin");