Linux 4.19.133
[linux/fpc-iii.git] / drivers / bluetooth / btrtl.c
blob8d1cd2479e36f5dd5c0d057b3cdf4dd92d7ef019
1 /*
2 * Bluetooth support for Realtek devices
4 * Copyright (C) 2015 Endless Mobile, Inc.
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.
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.
18 #include <linux/module.h>
19 #include <linux/firmware.h>
20 #include <asm/unaligned.h>
21 #include <linux/usb.h>
23 #include <net/bluetooth/bluetooth.h>
24 #include <net/bluetooth/hci_core.h>
26 #include "btrtl.h"
28 #define VERSION "0.1"
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
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)
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;
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;
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 },
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 },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
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" },
149 static const struct id_table *btrtl_match_ic(u16 lmp_subver, u16 hci_rev,
150 u8 hci_ver, u8 hci_bus)
152 int i;
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;
168 break;
170 if (i >= ARRAY_SIZE(ic_id_table))
171 return NULL;
173 return &ic_id_table[i];
176 static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
178 struct rtl_rom_version_evt *rom_version;
179 struct sk_buff *skb;
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);
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;
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);
199 *version = rom_version->version;
201 kfree_skb(skb);
202 return 0;
205 static int rtlbt_parse_firmware(struct hci_dev *hdev,
206 struct btrtl_device_info *btrtl_dev,
207 unsigned char **_buf)
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 */
233 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
234 if (btrtl_dev->fw_len < min_size)
235 return -EINVAL;
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;
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.
249 while (fwptr >= btrtl_dev->fw_data + (sizeof(*epatch_info) + 3)) {
250 opcode = *--fwptr;
251 length = *--fwptr;
252 data = *--fwptr;
254 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
256 if (opcode == 0xff) /* EOF */
257 break;
259 if (length == 0) {
260 rtl_dev_err(hdev, "found instruction with length 0\n");
261 return -EINVAL;
264 if (opcode == 0 && length == 1) {
265 project_id = data;
266 break;
269 fwptr -= length;
272 if (project_id < 0) {
273 rtl_dev_err(hdev, "failed to find version instruction\n");
274 return -EINVAL;
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;
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;
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;
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;
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);
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
310 * Find the right patch for this chip.
312 min_size += 8 * num_patches;
313 if (btrtl_dev->fw_len < min_size)
314 return -EINVAL;
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;
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;
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;
342 /* Copy the firmware into a new buffer and write the version at
343 * the end.
345 len = patch_length;
346 buf = kmemdup(btrtl_dev->fw_data + patch_offset, patch_length,
347 GFP_KERNEL);
348 if (!buf)
349 return -ENOMEM;
351 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
353 *_buf = buf;
354 return len;
357 static int rtl_download_firmware(struct hci_dev *hdev,
358 const unsigned char *data, int fw_len)
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;
366 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
367 if (!dl_cmd)
368 return -ENOMEM;
370 for (i = 0; i < frag_num; i++) {
371 struct sk_buff *skb;
373 BT_DBG("download fw (%d/%d)", i, frag_num);
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;
380 memcpy(dl_cmd->data, data, frag_len);
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;
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;
399 kfree_skb(skb);
400 data += RTL_FRAG_LEN;
403 out:
404 kfree(dl_cmd);
405 return ret;
408 static int rtl_load_file(struct hci_dev *hdev, const char *name, u8 **buff)
410 const struct firmware *fw;
411 int ret;
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;
422 release_firmware(fw);
424 return ret;
427 static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
428 struct btrtl_device_info *btrtl_dev)
430 if (btrtl_dev->fw_len < 8)
431 return -EINVAL;
433 /* Check that the firmware doesn't have the epatch signature
434 * (which is only for RTL8723B and newer).
436 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
437 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
438 return -EINVAL;
441 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
442 btrtl_dev->fw_len);
445 static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
446 struct btrtl_device_info *btrtl_dev)
448 unsigned char *fw_data = NULL;
449 int ret;
450 u8 *tbuff;
452 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
453 if (ret < 0)
454 goto out;
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;
463 memcpy(tbuff, fw_data, ret);
464 kfree(fw_data);
466 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
467 ret += btrtl_dev->cfg_len;
469 fw_data = tbuff;
472 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
474 ret = rtl_download_firmware(hdev, fw_data, ret);
476 out:
477 kfree(fw_data);
478 return ret;
481 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
483 struct sk_buff *skb;
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;
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);
499 return skb;
502 void btrtl_free(struct btrtl_device_info *btrtl_dev)
504 kfree(btrtl_dev->fw_data);
505 kfree(btrtl_dev->cfg_data);
506 kfree(btrtl_dev);
508 EXPORT_SYMBOL_GPL(btrtl_free);
510 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
511 const char *postfix)
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;
521 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
522 if (!btrtl_dev) {
523 ret = -ENOMEM;
524 goto err_alloc;
527 skb = btrtl_read_local_version(hdev);
528 if (IS_ERR(skb)) {
529 ret = PTR_ERR(skb);
530 goto err_free;
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);
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);
543 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
544 hdev->bus);
546 if (!btrtl_dev->ic_info) {
547 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
548 lmp_subver, hci_rev, hci_ver);
549 return btrtl_dev;
552 if (btrtl_dev->ic_info->has_rom_version) {
553 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
554 if (ret)
555 goto err_free;
558 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
559 &btrtl_dev->fw_data);
560 if (btrtl_dev->fw_len < 0) {
561 rtl_dev_err(hdev, "firmware file %s not found\n",
562 btrtl_dev->ic_info->fw_name);
563 ret = btrtl_dev->fw_len;
564 goto err_free;
567 if (btrtl_dev->ic_info->cfg_name) {
568 if (postfix) {
569 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
570 btrtl_dev->ic_info->cfg_name, postfix);
571 } else {
572 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
573 btrtl_dev->ic_info->cfg_name);
575 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
576 &btrtl_dev->cfg_data);
577 if (btrtl_dev->ic_info->config_needed &&
578 btrtl_dev->cfg_len <= 0) {
579 rtl_dev_err(hdev, "mandatory config file %s not found\n",
580 btrtl_dev->ic_info->cfg_name);
581 ret = btrtl_dev->cfg_len;
582 goto err_free;
586 return btrtl_dev;
588 err_free:
589 btrtl_free(btrtl_dev);
590 err_alloc:
591 return ERR_PTR(ret);
593 EXPORT_SYMBOL_GPL(btrtl_initialize);
595 int btrtl_download_firmware(struct hci_dev *hdev,
596 struct btrtl_device_info *btrtl_dev)
598 /* Match a set of subver values that correspond to stock firmware,
599 * which is not compatible with standard btusb.
600 * If matched, upload an alternative firmware that does conform to
601 * standard btusb. Once that firmware is uploaded, the subver changes
602 * to a different value.
604 if (!btrtl_dev->ic_info) {
605 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
606 return 0;
609 switch (btrtl_dev->ic_info->lmp_subver) {
610 case RTL_ROM_LMP_8723A:
611 case RTL_ROM_LMP_3499:
612 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
613 case RTL_ROM_LMP_8723B:
614 case RTL_ROM_LMP_8821A:
615 case RTL_ROM_LMP_8761A:
616 case RTL_ROM_LMP_8822B:
617 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
618 default:
619 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
620 return 0;
623 EXPORT_SYMBOL_GPL(btrtl_download_firmware);
625 int btrtl_setup_realtek(struct hci_dev *hdev)
627 struct btrtl_device_info *btrtl_dev;
628 int ret;
630 btrtl_dev = btrtl_initialize(hdev, NULL);
631 if (IS_ERR(btrtl_dev))
632 return PTR_ERR(btrtl_dev);
634 ret = btrtl_download_firmware(hdev, btrtl_dev);
636 btrtl_free(btrtl_dev);
638 return ret;
640 EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
642 int btrtl_shutdown_realtek(struct hci_dev *hdev)
644 struct sk_buff *skb;
645 int ret;
647 /* According to the vendor driver, BT must be reset on close to avoid
648 * firmware crash.
650 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
651 if (IS_ERR(skb)) {
652 ret = PTR_ERR(skb);
653 bt_dev_err(hdev, "HCI reset during shutdown failed");
654 return ret;
656 kfree_skb(skb);
658 return 0;
660 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
662 static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
664 switch (device_baudrate) {
665 case 0x0252a00a:
666 return 230400;
668 case 0x05f75004:
669 return 921600;
671 case 0x00005004:
672 return 1000000;
674 case 0x04928002:
675 case 0x01128002:
676 return 1500000;
678 case 0x00005002:
679 return 2000000;
681 case 0x0000b001:
682 return 2500000;
684 case 0x04928001:
685 return 3000000;
687 case 0x052a6001:
688 return 3500000;
690 case 0x00005001:
691 return 4000000;
693 case 0x0252c014:
694 default:
695 return 115200;
699 int btrtl_get_uart_settings(struct hci_dev *hdev,
700 struct btrtl_device_info *btrtl_dev,
701 unsigned int *controller_baudrate,
702 u32 *device_baudrate, bool *flow_control)
704 struct rtl_vendor_config *config;
705 struct rtl_vendor_config_entry *entry;
706 int i, total_data_len;
707 bool found = false;
709 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
710 if (total_data_len <= 0) {
711 rtl_dev_warn(hdev, "no config loaded\n");
712 return -EINVAL;
715 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
716 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
717 rtl_dev_err(hdev, "invalid config magic\n");
718 return -EINVAL;
721 if (total_data_len < le16_to_cpu(config->total_len)) {
722 rtl_dev_err(hdev, "config is too short\n");
723 return -EINVAL;
726 for (i = 0; i < total_data_len; ) {
727 entry = ((void *)config->entry) + i;
729 switch (le16_to_cpu(entry->offset)) {
730 case 0xc:
731 if (entry->len < sizeof(*device_baudrate)) {
732 rtl_dev_err(hdev, "invalid UART config entry\n");
733 return -EINVAL;
736 *device_baudrate = get_unaligned_le32(entry->data);
737 *controller_baudrate = btrtl_convert_baudrate(
738 *device_baudrate);
740 if (entry->len >= 13)
741 *flow_control = !!(entry->data[12] & BIT(2));
742 else
743 *flow_control = false;
745 found = true;
746 break;
748 default:
749 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
750 le16_to_cpu(entry->offset), entry->len);
751 break;
754 i += sizeof(*entry) + entry->len;
757 if (!found) {
758 rtl_dev_err(hdev, "no UART config entry found\n");
759 return -ENOENT;
762 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
763 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
764 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
766 return 0;
768 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
770 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
771 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
772 MODULE_VERSION(VERSION);
773 MODULE_LICENSE("GPL");
774 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
775 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
776 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
777 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
778 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
779 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
780 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
781 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
782 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
783 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
784 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
785 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
786 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");