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