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