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