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