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