btrtl.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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 <linux/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_EPATCH_SIGNATURE_V2 "RTBTCore"
  20. #define RTL_ROM_LMP_8703B 0x8703
  21. #define RTL_ROM_LMP_8723A 0x1200
  22. #define RTL_ROM_LMP_8723B 0x8723
  23. #define RTL_ROM_LMP_8821A 0x8821
  24. #define RTL_ROM_LMP_8761A 0x8761
  25. #define RTL_ROM_LMP_8822B 0x8822
  26. #define RTL_ROM_LMP_8852A 0x8852
  27. #define RTL_ROM_LMP_8851B 0x8851
  28. #define RTL_ROM_LMP_8922A 0x8922
  29. #define RTL_CONFIG_MAGIC 0x8723ab55
  30. #define RTL_VSC_OP_COREDUMP 0xfcff
  31. #define IC_MATCH_FL_LMPSUBV (1 << 0)
  32. #define IC_MATCH_FL_HCIREV (1 << 1)
  33. #define IC_MATCH_FL_HCIVER (1 << 2)
  34. #define IC_MATCH_FL_HCIBUS (1 << 3)
  35. #define IC_MATCH_FL_CHIP_TYPE (1 << 4)
  36. #define IC_INFO(lmps, hcir, hciv, bus) \
  37. .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_HCIREV | \
  38. IC_MATCH_FL_HCIVER | IC_MATCH_FL_HCIBUS, \
  39. .lmp_subver = (lmps), \
  40. .hci_rev = (hcir), \
  41. .hci_ver = (hciv), \
  42. .hci_bus = (bus)
  43. #define RTL_CHIP_SUBVER (&(struct rtl_vendor_cmd) {{0x10, 0x38, 0x04, 0x28, 0x80}})
  44. #define RTL_CHIP_REV (&(struct rtl_vendor_cmd) {{0x10, 0x3A, 0x04, 0x28, 0x80}})
  45. #define RTL_SEC_PROJ (&(struct rtl_vendor_cmd) {{0x10, 0xA4, 0x0D, 0x00, 0xb0}})
  46. #define RTL_PATCH_SNIPPETS 0x01
  47. #define RTL_PATCH_DUMMY_HEADER 0x02
  48. #define RTL_PATCH_SECURITY_HEADER 0x03
  49. enum btrtl_chip_id {
  50. CHIP_ID_8723A,
  51. CHIP_ID_8723B,
  52. CHIP_ID_8821A,
  53. CHIP_ID_8761A,
  54. CHIP_ID_8822B = 8,
  55. CHIP_ID_8723D,
  56. CHIP_ID_8821C,
  57. CHIP_ID_8822C = 13,
  58. CHIP_ID_8761B,
  59. CHIP_ID_8852A = 18,
  60. CHIP_ID_8852B = 20,
  61. CHIP_ID_8852C = 25,
  62. CHIP_ID_8851B = 36,
  63. CHIP_ID_8922A = 44,
  64. CHIP_ID_8852BT = 47,
  65. };
  66. struct id_table {
  67. __u16 match_flags;
  68. __u16 lmp_subver;
  69. __u16 hci_rev;
  70. __u8 hci_ver;
  71. __u8 hci_bus;
  72. __u8 chip_type;
  73. bool config_needed;
  74. bool has_rom_version;
  75. bool has_msft_ext;
  76. char *fw_name;
  77. char *cfg_name;
  78. char *hw_info;
  79. };
  80. struct btrtl_device_info {
  81. const struct id_table *ic_info;
  82. u8 rom_version;
  83. u8 *fw_data;
  84. int fw_len;
  85. u8 *cfg_data;
  86. int cfg_len;
  87. bool drop_fw;
  88. int project_id;
  89. u8 key_id;
  90. struct list_head patch_subsecs;
  91. };
  92. static const struct id_table ic_id_table[] = {
  93. /* 8723A */
  94. { IC_INFO(RTL_ROM_LMP_8723A, 0xb, 0x6, HCI_USB),
  95. .config_needed = false,
  96. .has_rom_version = false,
  97. .fw_name = "rtl_bt/rtl8723a_fw",
  98. .cfg_name = NULL,
  99. .hw_info = "rtl8723au" },
  100. /* 8723BS */
  101. { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_UART),
  102. .config_needed = true,
  103. .has_rom_version = true,
  104. .fw_name = "rtl_bt/rtl8723bs_fw",
  105. .cfg_name = "rtl_bt/rtl8723bs_config",
  106. .hw_info = "rtl8723bs" },
  107. /* 8723B */
  108. { IC_INFO(RTL_ROM_LMP_8723B, 0xb, 0x6, HCI_USB),
  109. .config_needed = false,
  110. .has_rom_version = true,
  111. .fw_name = "rtl_bt/rtl8723b_fw",
  112. .cfg_name = "rtl_bt/rtl8723b_config",
  113. .hw_info = "rtl8723bu" },
  114. /* 8723CS-CG */
  115. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  116. IC_MATCH_FL_HCIBUS,
  117. .lmp_subver = RTL_ROM_LMP_8703B,
  118. .chip_type = RTL_CHIP_8723CS_CG,
  119. .hci_bus = HCI_UART,
  120. .config_needed = true,
  121. .has_rom_version = true,
  122. .fw_name = "rtl_bt/rtl8723cs_cg_fw",
  123. .cfg_name = "rtl_bt/rtl8723cs_cg_config",
  124. .hw_info = "rtl8723cs-cg" },
  125. /* 8723CS-VF */
  126. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  127. IC_MATCH_FL_HCIBUS,
  128. .lmp_subver = RTL_ROM_LMP_8703B,
  129. .chip_type = RTL_CHIP_8723CS_VF,
  130. .hci_bus = HCI_UART,
  131. .config_needed = true,
  132. .has_rom_version = true,
  133. .fw_name = "rtl_bt/rtl8723cs_vf_fw",
  134. .cfg_name = "rtl_bt/rtl8723cs_vf_config",
  135. .hw_info = "rtl8723cs-vf" },
  136. /* 8723CS-XX */
  137. { .match_flags = IC_MATCH_FL_LMPSUBV | IC_MATCH_FL_CHIP_TYPE |
  138. IC_MATCH_FL_HCIBUS,
  139. .lmp_subver = RTL_ROM_LMP_8703B,
  140. .chip_type = RTL_CHIP_8723CS_XX,
  141. .hci_bus = HCI_UART,
  142. .config_needed = true,
  143. .has_rom_version = true,
  144. .fw_name = "rtl_bt/rtl8723cs_xx_fw",
  145. .cfg_name = "rtl_bt/rtl8723cs_xx_config",
  146. .hw_info = "rtl8723cs" },
  147. /* 8723D */
  148. { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_USB),
  149. .config_needed = true,
  150. .has_rom_version = true,
  151. .fw_name = "rtl_bt/rtl8723d_fw",
  152. .cfg_name = "rtl_bt/rtl8723d_config",
  153. .hw_info = "rtl8723du" },
  154. /* 8723DS */
  155. { IC_INFO(RTL_ROM_LMP_8723B, 0xd, 0x8, HCI_UART),
  156. .config_needed = true,
  157. .has_rom_version = true,
  158. .fw_name = "rtl_bt/rtl8723ds_fw",
  159. .cfg_name = "rtl_bt/rtl8723ds_config",
  160. .hw_info = "rtl8723ds" },
  161. /* 8821A */
  162. { IC_INFO(RTL_ROM_LMP_8821A, 0xa, 0x6, HCI_USB),
  163. .config_needed = false,
  164. .has_rom_version = true,
  165. .fw_name = "rtl_bt/rtl8821a_fw",
  166. .cfg_name = "rtl_bt/rtl8821a_config",
  167. .hw_info = "rtl8821au" },
  168. /* 8821C */
  169. { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_USB),
  170. .config_needed = false,
  171. .has_rom_version = true,
  172. .has_msft_ext = true,
  173. .fw_name = "rtl_bt/rtl8821c_fw",
  174. .cfg_name = "rtl_bt/rtl8821c_config",
  175. .hw_info = "rtl8821cu" },
  176. /* 8821CS */
  177. { IC_INFO(RTL_ROM_LMP_8821A, 0xc, 0x8, HCI_UART),
  178. .config_needed = true,
  179. .has_rom_version = true,
  180. .has_msft_ext = true,
  181. .fw_name = "rtl_bt/rtl8821cs_fw",
  182. .cfg_name = "rtl_bt/rtl8821cs_config",
  183. .hw_info = "rtl8821cs" },
  184. /* 8761A */
  185. { IC_INFO(RTL_ROM_LMP_8761A, 0xa, 0x6, HCI_USB),
  186. .config_needed = false,
  187. .has_rom_version = true,
  188. .fw_name = "rtl_bt/rtl8761a_fw",
  189. .cfg_name = "rtl_bt/rtl8761a_config",
  190. .hw_info = "rtl8761au" },
  191. /* 8761B */
  192. { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_UART),
  193. .config_needed = false,
  194. .has_rom_version = true,
  195. .has_msft_ext = true,
  196. .fw_name = "rtl_bt/rtl8761b_fw",
  197. .cfg_name = "rtl_bt/rtl8761b_config",
  198. .hw_info = "rtl8761btv" },
  199. /* 8761BU */
  200. { IC_INFO(RTL_ROM_LMP_8761A, 0xb, 0xa, HCI_USB),
  201. .config_needed = false,
  202. .has_rom_version = true,
  203. .fw_name = "rtl_bt/rtl8761bu_fw",
  204. .cfg_name = "rtl_bt/rtl8761bu_config",
  205. .hw_info = "rtl8761bu" },
  206. /* 8822C with UART interface */
  207. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0x8, HCI_UART),
  208. .config_needed = true,
  209. .has_rom_version = true,
  210. .has_msft_ext = true,
  211. .fw_name = "rtl_bt/rtl8822cs_fw",
  212. .cfg_name = "rtl_bt/rtl8822cs_config",
  213. .hw_info = "rtl8822cs" },
  214. /* 8822C with UART interface */
  215. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_UART),
  216. .config_needed = true,
  217. .has_rom_version = true,
  218. .has_msft_ext = true,
  219. .fw_name = "rtl_bt/rtl8822cs_fw",
  220. .cfg_name = "rtl_bt/rtl8822cs_config",
  221. .hw_info = "rtl8822cs" },
  222. /* 8822C with USB interface */
  223. { IC_INFO(RTL_ROM_LMP_8822B, 0xc, 0xa, HCI_USB),
  224. .config_needed = false,
  225. .has_rom_version = true,
  226. .has_msft_ext = true,
  227. .fw_name = "rtl_bt/rtl8822cu_fw",
  228. .cfg_name = "rtl_bt/rtl8822cu_config",
  229. .hw_info = "rtl8822cu" },
  230. /* 8822B */
  231. { IC_INFO(RTL_ROM_LMP_8822B, 0xb, 0x7, HCI_USB),
  232. .config_needed = true,
  233. .has_rom_version = true,
  234. .has_msft_ext = true,
  235. .fw_name = "rtl_bt/rtl8822b_fw",
  236. .cfg_name = "rtl_bt/rtl8822b_config",
  237. .hw_info = "rtl8822bu" },
  238. /* 8852A */
  239. { IC_INFO(RTL_ROM_LMP_8852A, 0xa, 0xb, HCI_USB),
  240. .config_needed = false,
  241. .has_rom_version = true,
  242. .has_msft_ext = true,
  243. .fw_name = "rtl_bt/rtl8852au_fw",
  244. .cfg_name = "rtl_bt/rtl8852au_config",
  245. .hw_info = "rtl8852au" },
  246. /* 8852B with UART interface */
  247. { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_UART),
  248. .config_needed = true,
  249. .has_rom_version = true,
  250. .has_msft_ext = true,
  251. .fw_name = "rtl_bt/rtl8852bs_fw",
  252. .cfg_name = "rtl_bt/rtl8852bs_config",
  253. .hw_info = "rtl8852bs" },
  254. /* 8852B */
  255. { IC_INFO(RTL_ROM_LMP_8852A, 0xb, 0xb, HCI_USB),
  256. .config_needed = false,
  257. .has_rom_version = true,
  258. .has_msft_ext = true,
  259. .fw_name = "rtl_bt/rtl8852bu_fw",
  260. .cfg_name = "rtl_bt/rtl8852bu_config",
  261. .hw_info = "rtl8852bu" },
  262. /* 8852C */
  263. { IC_INFO(RTL_ROM_LMP_8852A, 0xc, 0xc, HCI_USB),
  264. .config_needed = false,
  265. .has_rom_version = true,
  266. .has_msft_ext = true,
  267. .fw_name = "rtl_bt/rtl8852cu_fw",
  268. .cfg_name = "rtl_bt/rtl8852cu_config",
  269. .hw_info = "rtl8852cu" },
  270. /* 8851B */
  271. { IC_INFO(RTL_ROM_LMP_8851B, 0xb, 0xc, HCI_USB),
  272. .config_needed = false,
  273. .has_rom_version = true,
  274. .has_msft_ext = false,
  275. .fw_name = "rtl_bt/rtl8851bu_fw",
  276. .cfg_name = "rtl_bt/rtl8851bu_config",
  277. .hw_info = "rtl8851bu" },
  278. /* 8922A */
  279. { IC_INFO(RTL_ROM_LMP_8922A, 0xa, 0xc, HCI_USB),
  280. .config_needed = false,
  281. .has_rom_version = true,
  282. .has_msft_ext = true,
  283. .fw_name = "rtl_bt/rtl8922au_fw",
  284. .cfg_name = "rtl_bt/rtl8922au_config",
  285. .hw_info = "rtl8922au" },
  286. /* 8852BT/8852BE-VT */
  287. { IC_INFO(RTL_ROM_LMP_8852A, 0x87, 0xc, HCI_USB),
  288. .config_needed = false,
  289. .has_rom_version = true,
  290. .has_msft_ext = true,
  291. .fw_name = "rtl_bt/rtl8852btu_fw",
  292. .cfg_name = "rtl_bt/rtl8852btu_config",
  293. .hw_info = "rtl8852btu" },
  294. };
  295. static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
  296. u8 hci_ver, u8 hci_bus,
  297. u8 chip_type)
  298. {
  299. int i;
  300. for (i = 0; i < ARRAY_SIZE(ic_id_table); i++) {
  301. if ((ic_id_table[i].match_flags & IC_MATCH_FL_LMPSUBV) &&
  302. (ic_id_table[i].lmp_subver != lmp_subver))
  303. continue;
  304. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIREV) &&
  305. (ic_id_table[i].hci_rev != hci_rev))
  306. continue;
  307. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIVER) &&
  308. (ic_id_table[i].hci_ver != hci_ver))
  309. continue;
  310. if ((ic_id_table[i].match_flags & IC_MATCH_FL_HCIBUS) &&
  311. (ic_id_table[i].hci_bus != hci_bus))
  312. continue;
  313. if ((ic_id_table[i].match_flags & IC_MATCH_FL_CHIP_TYPE) &&
  314. (ic_id_table[i].chip_type != chip_type))
  315. continue;
  316. break;
  317. }
  318. if (i >= ARRAY_SIZE(ic_id_table))
  319. return NULL;
  320. return &ic_id_table[i];
  321. }
  322. static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
  323. {
  324. struct sk_buff *skb;
  325. skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
  326. HCI_INIT_TIMEOUT);
  327. if (IS_ERR(skb)) {
  328. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)",
  329. PTR_ERR(skb));
  330. return skb;
  331. }
  332. if (skb->len != sizeof(struct hci_rp_read_local_version)) {
  333. rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch");
  334. kfree_skb(skb);
  335. return ERR_PTR(-EIO);
  336. }
  337. return skb;
  338. }
  339. static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
  340. {
  341. struct rtl_rom_version_evt *rom_version;
  342. struct sk_buff *skb;
  343. /* Read RTL ROM version command */
  344. skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
  345. if (IS_ERR(skb)) {
  346. rtl_dev_err(hdev, "Read ROM version failed (%ld)",
  347. PTR_ERR(skb));
  348. return PTR_ERR(skb);
  349. }
  350. if (skb->len != sizeof(*rom_version)) {
  351. rtl_dev_err(hdev, "version event length mismatch");
  352. kfree_skb(skb);
  353. return -EIO;
  354. }
  355. rom_version = (struct rtl_rom_version_evt *)skb->data;
  356. rtl_dev_info(hdev, "rom_version status=%x version=%x",
  357. rom_version->status, rom_version->version);
  358. *version = rom_version->version;
  359. kfree_skb(skb);
  360. return 0;
  361. }
  362. static int btrtl_vendor_read_reg16(struct hci_dev *hdev,
  363. struct rtl_vendor_cmd *cmd, u8 *rp)
  364. {
  365. struct sk_buff *skb;
  366. int err = 0;
  367. skb = __hci_cmd_sync(hdev, 0xfc61, sizeof(*cmd), cmd,
  368. HCI_INIT_TIMEOUT);
  369. if (IS_ERR(skb)) {
  370. err = PTR_ERR(skb);
  371. rtl_dev_err(hdev, "RTL: Read reg16 failed (%d)", err);
  372. return err;
  373. }
  374. if (skb->len != 3 || skb->data[0]) {
  375. bt_dev_err(hdev, "RTL: Read reg16 length mismatch");
  376. kfree_skb(skb);
  377. return -EIO;
  378. }
  379. if (rp)
  380. memcpy(rp, skb->data + 1, 2);
  381. kfree_skb(skb);
  382. return 0;
  383. }
  384. static void *rtl_iov_pull_data(struct rtl_iovec *iov, u32 len)
  385. {
  386. void *data = iov->data;
  387. if (iov->len < len)
  388. return NULL;
  389. iov->data += len;
  390. iov->len -= len;
  391. return data;
  392. }
  393. static void btrtl_insert_ordered_subsec(struct rtl_subsection *node,
  394. struct btrtl_device_info *btrtl_dev)
  395. {
  396. struct list_head *pos;
  397. struct list_head *next;
  398. struct rtl_subsection *subsec;
  399. list_for_each_safe(pos, next, &btrtl_dev->patch_subsecs) {
  400. subsec = list_entry(pos, struct rtl_subsection, list);
  401. if (subsec->prio >= node->prio)
  402. break;
  403. }
  404. __list_add(&node->list, pos->prev, pos);
  405. }
  406. static int btrtl_parse_section(struct hci_dev *hdev,
  407. struct btrtl_device_info *btrtl_dev, u32 opcode,
  408. u8 *data, u32 len)
  409. {
  410. struct rtl_section_hdr *hdr;
  411. struct rtl_subsection *subsec;
  412. struct rtl_common_subsec *common_subsec;
  413. struct rtl_sec_hdr *sec_hdr;
  414. int i;
  415. u8 *ptr;
  416. u16 num_subsecs;
  417. u32 subsec_len;
  418. int rc = 0;
  419. struct rtl_iovec iov = {
  420. .data = data,
  421. .len = len,
  422. };
  423. hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
  424. if (!hdr)
  425. return -EINVAL;
  426. num_subsecs = le16_to_cpu(hdr->num);
  427. for (i = 0; i < num_subsecs; i++) {
  428. common_subsec = rtl_iov_pull_data(&iov, sizeof(*common_subsec));
  429. if (!common_subsec)
  430. break;
  431. subsec_len = le32_to_cpu(common_subsec->len);
  432. rtl_dev_dbg(hdev, "subsec, eco 0x%02x, len %08x",
  433. common_subsec->eco, subsec_len);
  434. ptr = rtl_iov_pull_data(&iov, subsec_len);
  435. if (!ptr)
  436. break;
  437. if (common_subsec->eco != btrtl_dev->rom_version + 1)
  438. continue;
  439. switch (opcode) {
  440. case RTL_PATCH_SECURITY_HEADER:
  441. sec_hdr = (void *)common_subsec;
  442. if (sec_hdr->key_id != btrtl_dev->key_id)
  443. continue;
  444. break;
  445. }
  446. subsec = kzalloc(sizeof(*subsec), GFP_KERNEL);
  447. if (!subsec)
  448. return -ENOMEM;
  449. subsec->opcode = opcode;
  450. subsec->prio = common_subsec->prio;
  451. subsec->len = subsec_len;
  452. subsec->data = ptr;
  453. btrtl_insert_ordered_subsec(subsec, btrtl_dev);
  454. rc += subsec_len;
  455. }
  456. return rc;
  457. }
  458. static int rtlbt_parse_firmware_v2(struct hci_dev *hdev,
  459. struct btrtl_device_info *btrtl_dev,
  460. unsigned char **_buf)
  461. {
  462. struct rtl_epatch_header_v2 *hdr;
  463. int rc;
  464. u8 reg_val[2];
  465. u8 key_id;
  466. u32 num_sections;
  467. struct rtl_section *section;
  468. struct rtl_subsection *entry, *tmp;
  469. u32 section_len;
  470. u32 opcode;
  471. int len = 0;
  472. int i;
  473. u8 *ptr;
  474. struct rtl_iovec iov = {
  475. .data = btrtl_dev->fw_data,
  476. .len = btrtl_dev->fw_len - 7, /* Cut the tail */
  477. };
  478. rc = btrtl_vendor_read_reg16(hdev, RTL_SEC_PROJ, reg_val);
  479. if (rc < 0)
  480. return -EIO;
  481. key_id = reg_val[0];
  482. rtl_dev_dbg(hdev, "%s: key id %u", __func__, key_id);
  483. btrtl_dev->key_id = key_id;
  484. hdr = rtl_iov_pull_data(&iov, sizeof(*hdr));
  485. if (!hdr)
  486. return -EINVAL;
  487. num_sections = le32_to_cpu(hdr->num_sections);
  488. rtl_dev_dbg(hdev, "FW version %08x-%08x", *((u32 *)hdr->fw_version),
  489. *((u32 *)(hdr->fw_version + 4)));
  490. for (i = 0; i < num_sections; i++) {
  491. section = rtl_iov_pull_data(&iov, sizeof(*section));
  492. if (!section)
  493. break;
  494. section_len = le32_to_cpu(section->len);
  495. opcode = le32_to_cpu(section->opcode);
  496. rtl_dev_dbg(hdev, "opcode 0x%04x", section->opcode);
  497. ptr = rtl_iov_pull_data(&iov, section_len);
  498. if (!ptr)
  499. break;
  500. switch (opcode) {
  501. case RTL_PATCH_SNIPPETS:
  502. rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
  503. ptr, section_len);
  504. break;
  505. case RTL_PATCH_SECURITY_HEADER:
  506. /* If key_id from chip is zero, ignore all security
  507. * headers.
  508. */
  509. if (!key_id)
  510. break;
  511. rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
  512. ptr, section_len);
  513. break;
  514. case RTL_PATCH_DUMMY_HEADER:
  515. rc = btrtl_parse_section(hdev, btrtl_dev, opcode,
  516. ptr, section_len);
  517. break;
  518. default:
  519. rc = 0;
  520. break;
  521. }
  522. if (rc < 0) {
  523. rtl_dev_err(hdev, "RTL: Parse section (%u) err %d",
  524. opcode, rc);
  525. return rc;
  526. }
  527. len += rc;
  528. }
  529. if (!len)
  530. return -ENODATA;
  531. /* Allocate mem and copy all found subsecs. */
  532. ptr = kvmalloc(len, GFP_KERNEL);
  533. if (!ptr)
  534. return -ENOMEM;
  535. len = 0;
  536. list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
  537. rtl_dev_dbg(hdev, "RTL: opcode %08x, addr %p, len 0x%x",
  538. entry->opcode, entry->data, entry->len);
  539. memcpy(ptr + len, entry->data, entry->len);
  540. len += entry->len;
  541. }
  542. if (!len)
  543. return -EPERM;
  544. *_buf = ptr;
  545. return len;
  546. }
  547. static int rtlbt_parse_firmware(struct hci_dev *hdev,
  548. struct btrtl_device_info *btrtl_dev,
  549. unsigned char **_buf)
  550. {
  551. static const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
  552. struct btrealtek_data *coredump_info = hci_get_priv(hdev);
  553. struct rtl_epatch_header *epatch_info;
  554. unsigned char *buf;
  555. int i, len;
  556. size_t min_size;
  557. u8 opcode, length, data;
  558. int project_id = -1;
  559. const unsigned char *fwptr, *chip_id_base;
  560. const unsigned char *patch_length_base, *patch_offset_base;
  561. u32 patch_offset = 0;
  562. u16 patch_length, num_patches;
  563. static const struct {
  564. __u16 lmp_subver;
  565. __u8 id;
  566. } project_id_to_lmp_subver[] = {
  567. { RTL_ROM_LMP_8723A, 0 },
  568. { RTL_ROM_LMP_8723B, 1 },
  569. { RTL_ROM_LMP_8821A, 2 },
  570. { RTL_ROM_LMP_8761A, 3 },
  571. { RTL_ROM_LMP_8703B, 7 },
  572. { RTL_ROM_LMP_8822B, 8 },
  573. { RTL_ROM_LMP_8723B, 9 }, /* 8723D */
  574. { RTL_ROM_LMP_8821A, 10 }, /* 8821C */
  575. { RTL_ROM_LMP_8822B, 13 }, /* 8822C */
  576. { RTL_ROM_LMP_8761A, 14 }, /* 8761B */
  577. { RTL_ROM_LMP_8852A, 18 }, /* 8852A */
  578. { RTL_ROM_LMP_8852A, 20 }, /* 8852B */
  579. { RTL_ROM_LMP_8852A, 25 }, /* 8852C */
  580. { RTL_ROM_LMP_8851B, 36 }, /* 8851B */
  581. { RTL_ROM_LMP_8922A, 44 }, /* 8922A */
  582. { RTL_ROM_LMP_8852A, 47 }, /* 8852BT */
  583. };
  584. if (btrtl_dev->fw_len <= 8)
  585. return -EINVAL;
  586. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8))
  587. min_size = sizeof(struct rtl_epatch_header) +
  588. sizeof(extension_sig) + 3;
  589. else if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
  590. min_size = sizeof(struct rtl_epatch_header_v2) +
  591. sizeof(extension_sig) + 3;
  592. else
  593. return -EINVAL;
  594. if (btrtl_dev->fw_len < min_size)
  595. return -EINVAL;
  596. fwptr = btrtl_dev->fw_data + btrtl_dev->fw_len - sizeof(extension_sig);
  597. if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
  598. rtl_dev_err(hdev, "extension section signature mismatch");
  599. return -EINVAL;
  600. }
  601. /* Loop from the end of the firmware parsing instructions, until
  602. * we find an instruction that identifies the "project ID" for the
  603. * hardware supported by this firwmare file.
  604. * Once we have that, we double-check that project_id is suitable
  605. * for the hardware we are working with.
  606. */
  607. while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
  608. opcode = *--fwptr;
  609. length = *--fwptr;
  610. data = *--fwptr;
  611. BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
  612. if (opcode == 0xff) /* EOF */
  613. break;
  614. if (length == 0) {
  615. rtl_dev_err(hdev, "found instruction with length 0");
  616. return -EINVAL;
  617. }
  618. if (opcode == 0 && length == 1) {
  619. project_id = data;
  620. break;
  621. }
  622. fwptr -= length;
  623. }
  624. if (project_id < 0) {
  625. rtl_dev_err(hdev, "failed to find version instruction");
  626. return -EINVAL;
  627. }
  628. /* Find project_id in table */
  629. for (i = 0; i < ARRAY_SIZE(project_id_to_lmp_subver); i++) {
  630. if (project_id == project_id_to_lmp_subver[i].id) {
  631. btrtl_dev->project_id = project_id;
  632. break;
  633. }
  634. }
  635. if (i >= ARRAY_SIZE(project_id_to_lmp_subver)) {
  636. rtl_dev_err(hdev, "unknown project id %d", project_id);
  637. return -EINVAL;
  638. }
  639. if (btrtl_dev->ic_info->lmp_subver !=
  640. project_id_to_lmp_subver[i].lmp_subver) {
  641. rtl_dev_err(hdev, "firmware is for %x but this is a %x",
  642. project_id_to_lmp_subver[i].lmp_subver,
  643. btrtl_dev->ic_info->lmp_subver);
  644. return -EINVAL;
  645. }
  646. if (memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8) != 0) {
  647. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE_V2, 8))
  648. return rtlbt_parse_firmware_v2(hdev, btrtl_dev, _buf);
  649. rtl_dev_err(hdev, "bad EPATCH signature");
  650. return -EINVAL;
  651. }
  652. epatch_info = (struct rtl_epatch_header *)btrtl_dev->fw_data;
  653. num_patches = le16_to_cpu(epatch_info->num_patches);
  654. BT_DBG("fw_version=%x, num_patches=%d",
  655. le32_to_cpu(epatch_info->fw_version), num_patches);
  656. coredump_info->rtl_dump.fw_version = le32_to_cpu(epatch_info->fw_version);
  657. /* After the rtl_epatch_header there is a funky patch metadata section.
  658. * Assuming 2 patches, the layout is:
  659. * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
  660. *
  661. * Find the right patch for this chip.
  662. */
  663. min_size += 8 * num_patches;
  664. if (btrtl_dev->fw_len < min_size)
  665. return -EINVAL;
  666. chip_id_base = btrtl_dev->fw_data + sizeof(struct rtl_epatch_header);
  667. patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
  668. patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
  669. for (i = 0; i < num_patches; i++) {
  670. u16 chip_id = get_unaligned_le16(chip_id_base +
  671. (i * sizeof(u16)));
  672. if (chip_id == btrtl_dev->rom_version + 1) {
  673. patch_length = get_unaligned_le16(patch_length_base +
  674. (i * sizeof(u16)));
  675. patch_offset = get_unaligned_le32(patch_offset_base +
  676. (i * sizeof(u32)));
  677. break;
  678. }
  679. }
  680. if (!patch_offset) {
  681. rtl_dev_err(hdev, "didn't find patch for chip id %d",
  682. btrtl_dev->rom_version);
  683. return -EINVAL;
  684. }
  685. BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
  686. min_size = patch_offset + patch_length;
  687. if (btrtl_dev->fw_len < min_size)
  688. return -EINVAL;
  689. /* Copy the firmware into a new buffer and write the version at
  690. * the end.
  691. */
  692. len = patch_length;
  693. buf = kvmalloc(patch_length, GFP_KERNEL);
  694. if (!buf)
  695. return -ENOMEM;
  696. memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
  697. memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
  698. *_buf = buf;
  699. return len;
  700. }
  701. static int rtl_download_firmware(struct hci_dev *hdev,
  702. const unsigned char *data, int fw_len)
  703. {
  704. struct rtl_download_cmd *dl_cmd;
  705. int frag_num = fw_len / RTL_FRAG_LEN + 1;
  706. int frag_len = RTL_FRAG_LEN;
  707. int ret = 0;
  708. int i;
  709. int j = 0;
  710. struct sk_buff *skb;
  711. struct hci_rp_read_local_version *rp;
  712. dl_cmd = kmalloc(sizeof(*dl_cmd), GFP_KERNEL);
  713. if (!dl_cmd)
  714. return -ENOMEM;
  715. for (i = 0; i < frag_num; i++) {
  716. struct sk_buff *skb;
  717. dl_cmd->index = j++;
  718. if (dl_cmd->index == 0x7f)
  719. j = 1;
  720. if (i == (frag_num - 1)) {
  721. dl_cmd->index |= 0x80; /* data end */
  722. frag_len = fw_len % RTL_FRAG_LEN;
  723. }
  724. rtl_dev_dbg(hdev, "download fw (%d/%d). index = %d", i,
  725. frag_num, dl_cmd->index);
  726. memcpy(dl_cmd->data, data, frag_len);
  727. /* Send download command */
  728. skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
  729. HCI_INIT_TIMEOUT);
  730. if (IS_ERR(skb)) {
  731. rtl_dev_err(hdev, "download fw command failed (%ld)",
  732. PTR_ERR(skb));
  733. ret = PTR_ERR(skb);
  734. goto out;
  735. }
  736. if (skb->len != sizeof(struct rtl_download_response)) {
  737. rtl_dev_err(hdev, "download fw event length mismatch");
  738. kfree_skb(skb);
  739. ret = -EIO;
  740. goto out;
  741. }
  742. kfree_skb(skb);
  743. data += RTL_FRAG_LEN;
  744. }
  745. skb = btrtl_read_local_version(hdev);
  746. if (IS_ERR(skb)) {
  747. ret = PTR_ERR(skb);
  748. rtl_dev_err(hdev, "read local version failed");
  749. goto out;
  750. }
  751. rp = (struct hci_rp_read_local_version *)skb->data;
  752. rtl_dev_info(hdev, "fw version 0x%04x%04x",
  753. __le16_to_cpu(rp->hci_rev), __le16_to_cpu(rp->lmp_subver));
  754. kfree_skb(skb);
  755. out:
  756. kfree(dl_cmd);
  757. return ret;
  758. }
  759. static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
  760. {
  761. const struct firmware *fw;
  762. int ret;
  763. rtl_dev_info(hdev, "loading %s", name);
  764. ret = request_firmware(&fw, name, &hdev->dev);
  765. if (ret < 0)
  766. return ret;
  767. ret = fw->size;
  768. *buff = kvmemdup(fw->data, fw->size, GFP_KERNEL);
  769. if (!*buff)
  770. ret = -ENOMEM;
  771. release_firmware(fw);
  772. return ret;
  773. }
  774. static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
  775. struct btrtl_device_info *btrtl_dev)
  776. {
  777. if (btrtl_dev->fw_len < 8)
  778. return -EINVAL;
  779. /* Check that the firmware doesn't have the epatch signature
  780. * (which is only for RTL8723B and newer).
  781. */
  782. if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
  783. rtl_dev_err(hdev, "unexpected EPATCH signature!");
  784. return -EINVAL;
  785. }
  786. return rtl_download_firmware(hdev, btrtl_dev->fw_data,
  787. btrtl_dev->fw_len);
  788. }
  789. static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
  790. struct btrtl_device_info *btrtl_dev)
  791. {
  792. unsigned char *fw_data = NULL;
  793. int ret;
  794. u8 *tbuff;
  795. ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
  796. if (ret < 0)
  797. goto out;
  798. if (btrtl_dev->cfg_len > 0) {
  799. tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
  800. if (!tbuff) {
  801. ret = -ENOMEM;
  802. goto out;
  803. }
  804. memcpy(tbuff, fw_data, ret);
  805. kvfree(fw_data);
  806. memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
  807. ret += btrtl_dev->cfg_len;
  808. fw_data = tbuff;
  809. }
  810. rtl_dev_info(hdev, "cfg_sz %d, total sz %d", btrtl_dev->cfg_len, ret);
  811. ret = rtl_download_firmware(hdev, fw_data, ret);
  812. out:
  813. kvfree(fw_data);
  814. return ret;
  815. }
  816. static void btrtl_coredump(struct hci_dev *hdev)
  817. {
  818. static const u8 param[] = { 0x00, 0x00 };
  819. __hci_cmd_send(hdev, RTL_VSC_OP_COREDUMP, sizeof(param), param);
  820. }
  821. static void btrtl_dmp_hdr(struct hci_dev *hdev, struct sk_buff *skb)
  822. {
  823. struct btrealtek_data *coredump_info = hci_get_priv(hdev);
  824. char buf[80];
  825. if (coredump_info->rtl_dump.controller)
  826. snprintf(buf, sizeof(buf), "Controller Name: %s\n",
  827. coredump_info->rtl_dump.controller);
  828. else
  829. snprintf(buf, sizeof(buf), "Controller Name: Unknown\n");
  830. skb_put_data(skb, buf, strlen(buf));
  831. snprintf(buf, sizeof(buf), "Firmware Version: 0x%X\n",
  832. coredump_info->rtl_dump.fw_version);
  833. skb_put_data(skb, buf, strlen(buf));
  834. snprintf(buf, sizeof(buf), "Driver: %s\n", coredump_info->rtl_dump.driver_name);
  835. skb_put_data(skb, buf, strlen(buf));
  836. snprintf(buf, sizeof(buf), "Vendor: Realtek\n");
  837. skb_put_data(skb, buf, strlen(buf));
  838. }
  839. static void btrtl_register_devcoredump_support(struct hci_dev *hdev)
  840. {
  841. hci_devcd_register(hdev, btrtl_coredump, btrtl_dmp_hdr, NULL);
  842. }
  843. void btrtl_set_driver_name(struct hci_dev *hdev, const char *driver_name)
  844. {
  845. struct btrealtek_data *coredump_info = hci_get_priv(hdev);
  846. coredump_info->rtl_dump.driver_name = driver_name;
  847. }
  848. EXPORT_SYMBOL_GPL(btrtl_set_driver_name);
  849. static bool rtl_has_chip_type(u16 lmp_subver)
  850. {
  851. switch (lmp_subver) {
  852. case RTL_ROM_LMP_8703B:
  853. return true;
  854. default:
  855. break;
  856. }
  857. return false;
  858. }
  859. static int rtl_read_chip_type(struct hci_dev *hdev, u8 *type)
  860. {
  861. struct rtl_chip_type_evt *chip_type;
  862. struct sk_buff *skb;
  863. const unsigned char cmd_buf[] = {0x00, 0x94, 0xa0, 0x00, 0xb0};
  864. /* Read RTL chip type command */
  865. skb = __hci_cmd_sync(hdev, 0xfc61, 5, cmd_buf, HCI_INIT_TIMEOUT);
  866. if (IS_ERR(skb)) {
  867. rtl_dev_err(hdev, "Read chip type failed (%ld)",
  868. PTR_ERR(skb));
  869. return PTR_ERR(skb);
  870. }
  871. chip_type = skb_pull_data(skb, sizeof(*chip_type));
  872. if (!chip_type) {
  873. rtl_dev_err(hdev, "RTL chip type event length mismatch");
  874. kfree_skb(skb);
  875. return -EIO;
  876. }
  877. rtl_dev_info(hdev, "chip_type status=%x type=%x",
  878. chip_type->status, chip_type->type);
  879. *type = chip_type->type & 0x0f;
  880. kfree_skb(skb);
  881. return 0;
  882. }
  883. void btrtl_free(struct btrtl_device_info *btrtl_dev)
  884. {
  885. struct rtl_subsection *entry, *tmp;
  886. kvfree(btrtl_dev->fw_data);
  887. kvfree(btrtl_dev->cfg_data);
  888. list_for_each_entry_safe(entry, tmp, &btrtl_dev->patch_subsecs, list) {
  889. list_del(&entry->list);
  890. kfree(entry);
  891. }
  892. kfree(btrtl_dev);
  893. }
  894. EXPORT_SYMBOL_GPL(btrtl_free);
  895. struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
  896. const char *postfix)
  897. {
  898. struct btrealtek_data *coredump_info = hci_get_priv(hdev);
  899. struct btrtl_device_info *btrtl_dev;
  900. struct sk_buff *skb;
  901. struct hci_rp_read_local_version *resp;
  902. struct hci_command_hdr *cmd;
  903. char fw_name[40];
  904. char cfg_name[40];
  905. u16 hci_rev, lmp_subver;
  906. u8 hci_ver, lmp_ver, chip_type = 0;
  907. int ret;
  908. u8 reg_val[2];
  909. btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
  910. if (!btrtl_dev) {
  911. ret = -ENOMEM;
  912. goto err_alloc;
  913. }
  914. INIT_LIST_HEAD(&btrtl_dev->patch_subsecs);
  915. check_version:
  916. ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_SUBVER, reg_val);
  917. if (ret < 0)
  918. goto err_free;
  919. lmp_subver = get_unaligned_le16(reg_val);
  920. if (lmp_subver == RTL_ROM_LMP_8822B) {
  921. ret = btrtl_vendor_read_reg16(hdev, RTL_CHIP_REV, reg_val);
  922. if (ret < 0)
  923. goto err_free;
  924. hci_rev = get_unaligned_le16(reg_val);
  925. /* 8822E */
  926. if (hci_rev == 0x000e) {
  927. hci_ver = 0x0c;
  928. lmp_ver = 0x0c;
  929. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev,
  930. hci_ver, hdev->bus,
  931. chip_type);
  932. goto next;
  933. }
  934. }
  935. skb = btrtl_read_local_version(hdev);
  936. if (IS_ERR(skb)) {
  937. ret = PTR_ERR(skb);
  938. goto err_free;
  939. }
  940. resp = (struct hci_rp_read_local_version *)skb->data;
  941. hci_ver = resp->hci_ver;
  942. hci_rev = le16_to_cpu(resp->hci_rev);
  943. lmp_ver = resp->lmp_ver;
  944. lmp_subver = le16_to_cpu(resp->lmp_subver);
  945. kfree_skb(skb);
  946. if (rtl_has_chip_type(lmp_subver)) {
  947. ret = rtl_read_chip_type(hdev, &chip_type);
  948. if (ret)
  949. goto err_free;
  950. }
  951. btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
  952. hdev->bus, chip_type);
  953. next:
  954. rtl_dev_info(hdev, "examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x",
  955. hci_ver, hci_rev,
  956. lmp_ver, lmp_subver);
  957. if (!btrtl_dev->ic_info && !btrtl_dev->drop_fw)
  958. btrtl_dev->drop_fw = true;
  959. else
  960. btrtl_dev->drop_fw = false;
  961. if (btrtl_dev->drop_fw) {
  962. skb = bt_skb_alloc(sizeof(*cmd), GFP_KERNEL);
  963. if (!skb)
  964. goto err_free;
  965. cmd = skb_put(skb, HCI_COMMAND_HDR_SIZE);
  966. cmd->opcode = cpu_to_le16(0xfc66);
  967. cmd->plen = 0;
  968. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  969. ret = hdev->send(hdev, skb);
  970. if (ret < 0) {
  971. bt_dev_err(hdev, "sending frame failed (%d)", ret);
  972. kfree_skb(skb);
  973. goto err_free;
  974. }
  975. /* Ensure the above vendor command is sent to controller and
  976. * process has done.
  977. */
  978. msleep(200);
  979. goto check_version;
  980. }
  981. if (!btrtl_dev->ic_info) {
  982. rtl_dev_info(hdev, "unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
  983. lmp_subver, hci_rev, hci_ver);
  984. return btrtl_dev;
  985. }
  986. if (btrtl_dev->ic_info->has_rom_version) {
  987. ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
  988. if (ret)
  989. goto err_free;
  990. }
  991. if (!btrtl_dev->ic_info->fw_name) {
  992. ret = -ENOMEM;
  993. goto err_free;
  994. }
  995. btrtl_dev->fw_len = -EIO;
  996. if (lmp_subver == RTL_ROM_LMP_8852A && hci_rev == 0x000c) {
  997. snprintf(fw_name, sizeof(fw_name), "%s_v2.bin",
  998. btrtl_dev->ic_info->fw_name);
  999. btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
  1000. &btrtl_dev->fw_data);
  1001. }
  1002. if (btrtl_dev->fw_len < 0) {
  1003. snprintf(fw_name, sizeof(fw_name), "%s.bin",
  1004. btrtl_dev->ic_info->fw_name);
  1005. btrtl_dev->fw_len = rtl_load_file(hdev, fw_name,
  1006. &btrtl_dev->fw_data);
  1007. }
  1008. if (btrtl_dev->fw_len < 0) {
  1009. rtl_dev_err(hdev, "firmware file %s not found",
  1010. btrtl_dev->ic_info->fw_name);
  1011. ret = btrtl_dev->fw_len;
  1012. goto err_free;
  1013. }
  1014. if (btrtl_dev->ic_info->cfg_name) {
  1015. if (postfix) {
  1016. snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
  1017. btrtl_dev->ic_info->cfg_name, postfix);
  1018. } else {
  1019. snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
  1020. btrtl_dev->ic_info->cfg_name);
  1021. }
  1022. btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
  1023. &btrtl_dev->cfg_data);
  1024. if (btrtl_dev->ic_info->config_needed &&
  1025. btrtl_dev->cfg_len <= 0) {
  1026. rtl_dev_err(hdev, "mandatory config file %s not found",
  1027. btrtl_dev->ic_info->cfg_name);
  1028. ret = btrtl_dev->cfg_len;
  1029. if (!ret)
  1030. ret = -EINVAL;
  1031. goto err_free;
  1032. }
  1033. }
  1034. /* The following chips supports the Microsoft vendor extension,
  1035. * therefore set the corresponding VsMsftOpCode.
  1036. */
  1037. if (btrtl_dev->ic_info->has_msft_ext)
  1038. hci_set_msft_opcode(hdev, 0xFCF0);
  1039. if (btrtl_dev->ic_info)
  1040. coredump_info->rtl_dump.controller = btrtl_dev->ic_info->hw_info;
  1041. return btrtl_dev;
  1042. err_free:
  1043. btrtl_free(btrtl_dev);
  1044. err_alloc:
  1045. return ERR_PTR(ret);
  1046. }
  1047. EXPORT_SYMBOL_GPL(btrtl_initialize);
  1048. int btrtl_download_firmware(struct hci_dev *hdev,
  1049. struct btrtl_device_info *btrtl_dev)
  1050. {
  1051. int err = 0;
  1052. /* Match a set of subver values that correspond to stock firmware,
  1053. * which is not compatible with standard btusb.
  1054. * If matched, upload an alternative firmware that does conform to
  1055. * standard btusb. Once that firmware is uploaded, the subver changes
  1056. * to a different value.
  1057. */
  1058. if (!btrtl_dev->ic_info) {
  1059. rtl_dev_info(hdev, "assuming no firmware upload needed");
  1060. err = 0;
  1061. goto done;
  1062. }
  1063. switch (btrtl_dev->ic_info->lmp_subver) {
  1064. case RTL_ROM_LMP_8723A:
  1065. err = btrtl_setup_rtl8723a(hdev, btrtl_dev);
  1066. break;
  1067. case RTL_ROM_LMP_8723B:
  1068. case RTL_ROM_LMP_8821A:
  1069. case RTL_ROM_LMP_8761A:
  1070. case RTL_ROM_LMP_8822B:
  1071. case RTL_ROM_LMP_8852A:
  1072. case RTL_ROM_LMP_8703B:
  1073. case RTL_ROM_LMP_8851B:
  1074. case RTL_ROM_LMP_8922A:
  1075. err = btrtl_setup_rtl8723b(hdev, btrtl_dev);
  1076. break;
  1077. default:
  1078. rtl_dev_info(hdev, "assuming no firmware upload needed");
  1079. break;
  1080. }
  1081. done:
  1082. btrtl_register_devcoredump_support(hdev);
  1083. return err;
  1084. }
  1085. EXPORT_SYMBOL_GPL(btrtl_download_firmware);
  1086. void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
  1087. {
  1088. /* Enable controller to do both LE scan and BR/EDR inquiry
  1089. * simultaneously.
  1090. */
  1091. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  1092. /* Enable central-peripheral role (able to create new connections with
  1093. * an existing connection in slave role).
  1094. */
  1095. /* Enable WBS supported for the specific Realtek devices. */
  1096. switch (btrtl_dev->project_id) {
  1097. case CHIP_ID_8822C:
  1098. case CHIP_ID_8852A:
  1099. case CHIP_ID_8852B:
  1100. case CHIP_ID_8852C:
  1101. case CHIP_ID_8851B:
  1102. case CHIP_ID_8922A:
  1103. case CHIP_ID_8852BT:
  1104. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  1105. /* RTL8852C needs to transmit mSBC data continuously without
  1106. * the zero length of USB packets for the ALT 6 supported chips
  1107. */
  1108. if (btrtl_dev->project_id == CHIP_ID_8852C)
  1109. btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP);
  1110. if (btrtl_dev->project_id == CHIP_ID_8852A ||
  1111. btrtl_dev->project_id == CHIP_ID_8852B ||
  1112. btrtl_dev->project_id == CHIP_ID_8852C)
  1113. set_bit(HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER, &hdev->quirks);
  1114. hci_set_aosp_capable(hdev);
  1115. break;
  1116. default:
  1117. rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
  1118. rtl_dev_dbg(hdev, "WBS supported not enabled.");
  1119. break;
  1120. }
  1121. if (!btrtl_dev->ic_info)
  1122. return;
  1123. switch (btrtl_dev->ic_info->lmp_subver) {
  1124. case RTL_ROM_LMP_8703B:
  1125. /* 8723CS reports two pages for local ext features,
  1126. * but it doesn't support any features from page 2 -
  1127. * it either responds with garbage or with error status
  1128. */
  1129. set_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
  1130. &hdev->quirks);
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. }
  1136. EXPORT_SYMBOL_GPL(btrtl_set_quirks);
  1137. int btrtl_setup_realtek(struct hci_dev *hdev)
  1138. {
  1139. struct btrtl_device_info *btrtl_dev;
  1140. int ret;
  1141. btrtl_dev = btrtl_initialize(hdev, NULL);
  1142. if (IS_ERR(btrtl_dev))
  1143. return PTR_ERR(btrtl_dev);
  1144. ret = btrtl_download_firmware(hdev, btrtl_dev);
  1145. btrtl_set_quirks(hdev, btrtl_dev);
  1146. if (btrtl_dev->ic_info) {
  1147. hci_set_hw_info(hdev,
  1148. "RTL lmp_subver=%u hci_rev=%u hci_ver=%u hci_bus=%u",
  1149. btrtl_dev->ic_info->lmp_subver,
  1150. btrtl_dev->ic_info->hci_rev,
  1151. btrtl_dev->ic_info->hci_ver,
  1152. btrtl_dev->ic_info->hci_bus);
  1153. }
  1154. btrtl_free(btrtl_dev);
  1155. return ret;
  1156. }
  1157. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  1158. int btrtl_shutdown_realtek(struct hci_dev *hdev)
  1159. {
  1160. struct sk_buff *skb;
  1161. int ret;
  1162. /* According to the vendor driver, BT must be reset on close to avoid
  1163. * firmware crash.
  1164. */
  1165. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  1166. if (IS_ERR(skb)) {
  1167. ret = PTR_ERR(skb);
  1168. bt_dev_err(hdev, "HCI reset during shutdown failed");
  1169. return ret;
  1170. }
  1171. kfree_skb(skb);
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
  1175. static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
  1176. {
  1177. switch (device_baudrate) {
  1178. case 0x0252a00a:
  1179. return 230400;
  1180. case 0x05f75004:
  1181. return 921600;
  1182. case 0x00005004:
  1183. return 1000000;
  1184. case 0x04928002:
  1185. case 0x01128002:
  1186. return 1500000;
  1187. case 0x00005002:
  1188. return 2000000;
  1189. case 0x0000b001:
  1190. return 2500000;
  1191. case 0x04928001:
  1192. return 3000000;
  1193. case 0x052a6001:
  1194. return 3500000;
  1195. case 0x00005001:
  1196. return 4000000;
  1197. case 0x0252c014:
  1198. default:
  1199. return 115200;
  1200. }
  1201. }
  1202. int btrtl_get_uart_settings(struct hci_dev *hdev,
  1203. struct btrtl_device_info *btrtl_dev,
  1204. unsigned int *controller_baudrate,
  1205. u32 *device_baudrate, bool *flow_control)
  1206. {
  1207. struct rtl_vendor_config *config;
  1208. struct rtl_vendor_config_entry *entry;
  1209. int i, total_data_len;
  1210. bool found = false;
  1211. total_data_len = btrtl_dev->cfg_len - sizeof(*config);
  1212. if (total_data_len <= 0) {
  1213. rtl_dev_warn(hdev, "no config loaded");
  1214. return -EINVAL;
  1215. }
  1216. config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
  1217. if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
  1218. rtl_dev_err(hdev, "invalid config magic");
  1219. return -EINVAL;
  1220. }
  1221. if (total_data_len < le16_to_cpu(config->total_len)) {
  1222. rtl_dev_err(hdev, "config is too short");
  1223. return -EINVAL;
  1224. }
  1225. for (i = 0; i < total_data_len; ) {
  1226. entry = ((void *)config->entry) + i;
  1227. switch (le16_to_cpu(entry->offset)) {
  1228. case 0xc:
  1229. if (entry->len < sizeof(*device_baudrate)) {
  1230. rtl_dev_err(hdev, "invalid UART config entry");
  1231. return -EINVAL;
  1232. }
  1233. *device_baudrate = get_unaligned_le32(entry->data);
  1234. *controller_baudrate = btrtl_convert_baudrate(
  1235. *device_baudrate);
  1236. if (entry->len >= 13)
  1237. *flow_control = !!(entry->data[12] & BIT(2));
  1238. else
  1239. *flow_control = false;
  1240. found = true;
  1241. break;
  1242. default:
  1243. rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
  1244. le16_to_cpu(entry->offset), entry->len);
  1245. break;
  1246. }
  1247. i += sizeof(*entry) + entry->len;
  1248. }
  1249. if (!found) {
  1250. rtl_dev_err(hdev, "no UART config entry found");
  1251. return -ENOENT;
  1252. }
  1253. rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
  1254. rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
  1255. rtl_dev_dbg(hdev, "flow control %d", *flow_control);
  1256. return 0;
  1257. }
  1258. EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
  1259. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  1260. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  1261. MODULE_VERSION(VERSION);
  1262. MODULE_LICENSE("GPL");
  1263. MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
  1264. MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
  1265. MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
  1266. MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
  1267. MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
  1268. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_fw.bin");
  1269. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_config.bin");
  1270. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_fw.bin");
  1271. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_config.bin");
  1272. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_fw.bin");
  1273. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_config.bin");
  1274. MODULE_FIRMWARE("rtl_bt/rtl8723d_fw.bin");
  1275. MODULE_FIRMWARE("rtl_bt/rtl8723d_config.bin");
  1276. MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
  1277. MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
  1278. MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
  1279. MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
  1280. MODULE_FIRMWARE("rtl_bt/rtl8761b_fw.bin");
  1281. MODULE_FIRMWARE("rtl_bt/rtl8761b_config.bin");
  1282. MODULE_FIRMWARE("rtl_bt/rtl8761bu_fw.bin");
  1283. MODULE_FIRMWARE("rtl_bt/rtl8761bu_config.bin");
  1284. MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
  1285. MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
  1286. MODULE_FIRMWARE("rtl_bt/rtl8821c_fw.bin");
  1287. MODULE_FIRMWARE("rtl_bt/rtl8821c_config.bin");
  1288. MODULE_FIRMWARE("rtl_bt/rtl8821cs_fw.bin");
  1289. MODULE_FIRMWARE("rtl_bt/rtl8821cs_config.bin");
  1290. MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
  1291. MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
  1292. MODULE_FIRMWARE("rtl_bt/rtl8822cs_fw.bin");
  1293. MODULE_FIRMWARE("rtl_bt/rtl8822cs_config.bin");
  1294. MODULE_FIRMWARE("rtl_bt/rtl8822cu_fw.bin");
  1295. MODULE_FIRMWARE("rtl_bt/rtl8822cu_config.bin");
  1296. MODULE_FIRMWARE("rtl_bt/rtl8851bu_fw.bin");
  1297. MODULE_FIRMWARE("rtl_bt/rtl8851bu_config.bin");
  1298. MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
  1299. MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
  1300. MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin");
  1301. MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin");
  1302. MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
  1303. MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
  1304. MODULE_FIRMWARE("rtl_bt/rtl8852btu_fw.bin");
  1305. MODULE_FIRMWARE("rtl_bt/rtl8852btu_config.bin");
  1306. MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
  1307. MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw_v2.bin");
  1308. MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");
  1309. MODULE_FIRMWARE("rtl_bt/rtl8922au_fw.bin");
  1310. MODULE_FIRMWARE("rtl_bt/rtl8922au_config.bin");