btrtl.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. goto err_free;
  1030. }
  1031. }
  1032. /* The following chips supports the Microsoft vendor extension,
  1033. * therefore set the corresponding VsMsftOpCode.
  1034. */
  1035. if (btrtl_dev->ic_info->has_msft_ext)
  1036. hci_set_msft_opcode(hdev, 0xFCF0);
  1037. if (btrtl_dev->ic_info)
  1038. coredump_info->rtl_dump.controller = btrtl_dev->ic_info->hw_info;
  1039. return btrtl_dev;
  1040. err_free:
  1041. btrtl_free(btrtl_dev);
  1042. err_alloc:
  1043. return ERR_PTR(ret);
  1044. }
  1045. EXPORT_SYMBOL_GPL(btrtl_initialize);
  1046. int btrtl_download_firmware(struct hci_dev *hdev,
  1047. struct btrtl_device_info *btrtl_dev)
  1048. {
  1049. int err = 0;
  1050. /* Match a set of subver values that correspond to stock firmware,
  1051. * which is not compatible with standard btusb.
  1052. * If matched, upload an alternative firmware that does conform to
  1053. * standard btusb. Once that firmware is uploaded, the subver changes
  1054. * to a different value.
  1055. */
  1056. if (!btrtl_dev->ic_info) {
  1057. rtl_dev_info(hdev, "assuming no firmware upload needed");
  1058. err = 0;
  1059. goto done;
  1060. }
  1061. switch (btrtl_dev->ic_info->lmp_subver) {
  1062. case RTL_ROM_LMP_8723A:
  1063. err = btrtl_setup_rtl8723a(hdev, btrtl_dev);
  1064. break;
  1065. case RTL_ROM_LMP_8723B:
  1066. case RTL_ROM_LMP_8821A:
  1067. case RTL_ROM_LMP_8761A:
  1068. case RTL_ROM_LMP_8822B:
  1069. case RTL_ROM_LMP_8852A:
  1070. case RTL_ROM_LMP_8703B:
  1071. case RTL_ROM_LMP_8851B:
  1072. case RTL_ROM_LMP_8922A:
  1073. err = btrtl_setup_rtl8723b(hdev, btrtl_dev);
  1074. break;
  1075. default:
  1076. rtl_dev_info(hdev, "assuming no firmware upload needed");
  1077. break;
  1078. }
  1079. done:
  1080. btrtl_register_devcoredump_support(hdev);
  1081. return err;
  1082. }
  1083. EXPORT_SYMBOL_GPL(btrtl_download_firmware);
  1084. void btrtl_set_quirks(struct hci_dev *hdev, struct btrtl_device_info *btrtl_dev)
  1085. {
  1086. /* Enable controller to do both LE scan and BR/EDR inquiry
  1087. * simultaneously.
  1088. */
  1089. set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
  1090. /* Enable central-peripheral role (able to create new connections with
  1091. * an existing connection in slave role).
  1092. */
  1093. /* Enable WBS supported for the specific Realtek devices. */
  1094. switch (btrtl_dev->project_id) {
  1095. case CHIP_ID_8822C:
  1096. case CHIP_ID_8852A:
  1097. case CHIP_ID_8852B:
  1098. case CHIP_ID_8852C:
  1099. case CHIP_ID_8851B:
  1100. case CHIP_ID_8922A:
  1101. case CHIP_ID_8852BT:
  1102. set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
  1103. /* RTL8852C needs to transmit mSBC data continuously without
  1104. * the zero length of USB packets for the ALT 6 supported chips
  1105. */
  1106. if (btrtl_dev->project_id == CHIP_ID_8852C)
  1107. btrealtek_set_flag(hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP);
  1108. if (btrtl_dev->project_id == CHIP_ID_8852A ||
  1109. btrtl_dev->project_id == CHIP_ID_8852B ||
  1110. btrtl_dev->project_id == CHIP_ID_8852C)
  1111. set_bit(HCI_QUIRK_USE_MSFT_EXT_ADDRESS_FILTER, &hdev->quirks);
  1112. hci_set_aosp_capable(hdev);
  1113. break;
  1114. default:
  1115. rtl_dev_dbg(hdev, "Central-peripheral role not enabled.");
  1116. rtl_dev_dbg(hdev, "WBS supported not enabled.");
  1117. break;
  1118. }
  1119. if (!btrtl_dev->ic_info)
  1120. return;
  1121. switch (btrtl_dev->ic_info->lmp_subver) {
  1122. case RTL_ROM_LMP_8703B:
  1123. /* 8723CS reports two pages for local ext features,
  1124. * but it doesn't support any features from page 2 -
  1125. * it either responds with garbage or with error status
  1126. */
  1127. set_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
  1128. &hdev->quirks);
  1129. break;
  1130. default:
  1131. break;
  1132. }
  1133. }
  1134. EXPORT_SYMBOL_GPL(btrtl_set_quirks);
  1135. int btrtl_setup_realtek(struct hci_dev *hdev)
  1136. {
  1137. struct btrtl_device_info *btrtl_dev;
  1138. int ret;
  1139. btrtl_dev = btrtl_initialize(hdev, NULL);
  1140. if (IS_ERR(btrtl_dev))
  1141. return PTR_ERR(btrtl_dev);
  1142. ret = btrtl_download_firmware(hdev, btrtl_dev);
  1143. btrtl_set_quirks(hdev, btrtl_dev);
  1144. if (btrtl_dev->ic_info) {
  1145. hci_set_hw_info(hdev,
  1146. "RTL lmp_subver=%u hci_rev=%u hci_ver=%u hci_bus=%u",
  1147. btrtl_dev->ic_info->lmp_subver,
  1148. btrtl_dev->ic_info->hci_rev,
  1149. btrtl_dev->ic_info->hci_ver,
  1150. btrtl_dev->ic_info->hci_bus);
  1151. }
  1152. btrtl_free(btrtl_dev);
  1153. return ret;
  1154. }
  1155. EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
  1156. int btrtl_shutdown_realtek(struct hci_dev *hdev)
  1157. {
  1158. struct sk_buff *skb;
  1159. int ret;
  1160. /* According to the vendor driver, BT must be reset on close to avoid
  1161. * firmware crash.
  1162. */
  1163. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  1164. if (IS_ERR(skb)) {
  1165. ret = PTR_ERR(skb);
  1166. bt_dev_err(hdev, "HCI reset during shutdown failed");
  1167. return ret;
  1168. }
  1169. kfree_skb(skb);
  1170. return 0;
  1171. }
  1172. EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
  1173. static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
  1174. {
  1175. switch (device_baudrate) {
  1176. case 0x0252a00a:
  1177. return 230400;
  1178. case 0x05f75004:
  1179. return 921600;
  1180. case 0x00005004:
  1181. return 1000000;
  1182. case 0x04928002:
  1183. case 0x01128002:
  1184. return 1500000;
  1185. case 0x00005002:
  1186. return 2000000;
  1187. case 0x0000b001:
  1188. return 2500000;
  1189. case 0x04928001:
  1190. return 3000000;
  1191. case 0x052a6001:
  1192. return 3500000;
  1193. case 0x00005001:
  1194. return 4000000;
  1195. case 0x0252c014:
  1196. default:
  1197. return 115200;
  1198. }
  1199. }
  1200. int btrtl_get_uart_settings(struct hci_dev *hdev,
  1201. struct btrtl_device_info *btrtl_dev,
  1202. unsigned int *controller_baudrate,
  1203. u32 *device_baudrate, bool *flow_control)
  1204. {
  1205. struct rtl_vendor_config *config;
  1206. struct rtl_vendor_config_entry *entry;
  1207. int i, total_data_len;
  1208. bool found = false;
  1209. total_data_len = btrtl_dev->cfg_len - sizeof(*config);
  1210. if (total_data_len <= 0) {
  1211. rtl_dev_warn(hdev, "no config loaded");
  1212. return -EINVAL;
  1213. }
  1214. config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
  1215. if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
  1216. rtl_dev_err(hdev, "invalid config magic");
  1217. return -EINVAL;
  1218. }
  1219. if (total_data_len < le16_to_cpu(config->total_len)) {
  1220. rtl_dev_err(hdev, "config is too short");
  1221. return -EINVAL;
  1222. }
  1223. for (i = 0; i < total_data_len; ) {
  1224. entry = ((void *)config->entry) + i;
  1225. switch (le16_to_cpu(entry->offset)) {
  1226. case 0xc:
  1227. if (entry->len < sizeof(*device_baudrate)) {
  1228. rtl_dev_err(hdev, "invalid UART config entry");
  1229. return -EINVAL;
  1230. }
  1231. *device_baudrate = get_unaligned_le32(entry->data);
  1232. *controller_baudrate = btrtl_convert_baudrate(
  1233. *device_baudrate);
  1234. if (entry->len >= 13)
  1235. *flow_control = !!(entry->data[12] & BIT(2));
  1236. else
  1237. *flow_control = false;
  1238. found = true;
  1239. break;
  1240. default:
  1241. rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)",
  1242. le16_to_cpu(entry->offset), entry->len);
  1243. break;
  1244. }
  1245. i += sizeof(*entry) + entry->len;
  1246. }
  1247. if (!found) {
  1248. rtl_dev_err(hdev, "no UART config entry found");
  1249. return -ENOENT;
  1250. }
  1251. rtl_dev_dbg(hdev, "device baudrate = 0x%08x", *device_baudrate);
  1252. rtl_dev_dbg(hdev, "controller baudrate = %u", *controller_baudrate);
  1253. rtl_dev_dbg(hdev, "flow control %d", *flow_control);
  1254. return 0;
  1255. }
  1256. EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
  1257. MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
  1258. MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
  1259. MODULE_VERSION(VERSION);
  1260. MODULE_LICENSE("GPL");
  1261. MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
  1262. MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
  1263. MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
  1264. MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
  1265. MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
  1266. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_fw.bin");
  1267. MODULE_FIRMWARE("rtl_bt/rtl8723cs_cg_config.bin");
  1268. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_fw.bin");
  1269. MODULE_FIRMWARE("rtl_bt/rtl8723cs_vf_config.bin");
  1270. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_fw.bin");
  1271. MODULE_FIRMWARE("rtl_bt/rtl8723cs_xx_config.bin");
  1272. MODULE_FIRMWARE("rtl_bt/rtl8723d_fw.bin");
  1273. MODULE_FIRMWARE("rtl_bt/rtl8723d_config.bin");
  1274. MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
  1275. MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
  1276. MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
  1277. MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
  1278. MODULE_FIRMWARE("rtl_bt/rtl8761b_fw.bin");
  1279. MODULE_FIRMWARE("rtl_bt/rtl8761b_config.bin");
  1280. MODULE_FIRMWARE("rtl_bt/rtl8761bu_fw.bin");
  1281. MODULE_FIRMWARE("rtl_bt/rtl8761bu_config.bin");
  1282. MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
  1283. MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
  1284. MODULE_FIRMWARE("rtl_bt/rtl8821c_fw.bin");
  1285. MODULE_FIRMWARE("rtl_bt/rtl8821c_config.bin");
  1286. MODULE_FIRMWARE("rtl_bt/rtl8821cs_fw.bin");
  1287. MODULE_FIRMWARE("rtl_bt/rtl8821cs_config.bin");
  1288. MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
  1289. MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
  1290. MODULE_FIRMWARE("rtl_bt/rtl8822cs_fw.bin");
  1291. MODULE_FIRMWARE("rtl_bt/rtl8822cs_config.bin");
  1292. MODULE_FIRMWARE("rtl_bt/rtl8822cu_fw.bin");
  1293. MODULE_FIRMWARE("rtl_bt/rtl8822cu_config.bin");
  1294. MODULE_FIRMWARE("rtl_bt/rtl8851bu_fw.bin");
  1295. MODULE_FIRMWARE("rtl_bt/rtl8851bu_config.bin");
  1296. MODULE_FIRMWARE("rtl_bt/rtl8852au_fw.bin");
  1297. MODULE_FIRMWARE("rtl_bt/rtl8852au_config.bin");
  1298. MODULE_FIRMWARE("rtl_bt/rtl8852bs_fw.bin");
  1299. MODULE_FIRMWARE("rtl_bt/rtl8852bs_config.bin");
  1300. MODULE_FIRMWARE("rtl_bt/rtl8852bu_fw.bin");
  1301. MODULE_FIRMWARE("rtl_bt/rtl8852bu_config.bin");
  1302. MODULE_FIRMWARE("rtl_bt/rtl8852btu_fw.bin");
  1303. MODULE_FIRMWARE("rtl_bt/rtl8852btu_config.bin");
  1304. MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw.bin");
  1305. MODULE_FIRMWARE("rtl_bt/rtl8852cu_fw_v2.bin");
  1306. MODULE_FIRMWARE("rtl_bt/rtl8852cu_config.bin");
  1307. MODULE_FIRMWARE("rtl_bt/rtl8922au_fw.bin");
  1308. MODULE_FIRMWARE("rtl_bt/rtl8922au_config.bin");