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