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
btrtl_match_ic(u16 lmp_subver,u16 hci_rev,u8 hci_ver,u8 hci_bus)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
rtl_read_rom_version(struct hci_dev * hdev,u8 * version)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
rtlbt_parse_firmware(struct hci_dev * hdev,struct btrtl_device_info * btrtl_dev,unsigned char ** _buf)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 = kvmalloc(patch_length, GFP_KERNEL);
347 if (!buf)
348 return -ENOMEM;
349
350 memcpy(buf, btrtl_dev->fw_data + patch_offset, patch_length - 4);
351 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
352
353 *_buf = buf;
354 return len;
355 }
356
rtl_download_firmware(struct hci_dev * hdev,const unsigned char * data,int fw_len)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
rtl_load_file(struct hci_dev * hdev,const char * name,u8 ** buff)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 = kvmalloc(fw->size, GFP_KERNEL);
419 if (*buff)
420 memcpy(*buff, fw->data, ret);
421 else
422 ret = -ENOMEM;
423
424 release_firmware(fw);
425
426 return ret;
427 }
428
btrtl_setup_rtl8723a(struct hci_dev * hdev,struct btrtl_device_info * btrtl_dev)429 static int btrtl_setup_rtl8723a(struct hci_dev *hdev,
430 struct btrtl_device_info *btrtl_dev)
431 {
432 if (btrtl_dev->fw_len < 8)
433 return -EINVAL;
434
435 /* Check that the firmware doesn't have the epatch signature
436 * (which is only for RTL8723B and newer).
437 */
438 if (!memcmp(btrtl_dev->fw_data, RTL_EPATCH_SIGNATURE, 8)) {
439 rtl_dev_err(hdev, "unexpected EPATCH signature!\n");
440 return -EINVAL;
441 }
442
443 return rtl_download_firmware(hdev, btrtl_dev->fw_data,
444 btrtl_dev->fw_len);
445 }
446
btrtl_setup_rtl8723b(struct hci_dev * hdev,struct btrtl_device_info * btrtl_dev)447 static int btrtl_setup_rtl8723b(struct hci_dev *hdev,
448 struct btrtl_device_info *btrtl_dev)
449 {
450 unsigned char *fw_data = NULL;
451 int ret;
452 u8 *tbuff;
453
454 ret = rtlbt_parse_firmware(hdev, btrtl_dev, &fw_data);
455 if (ret < 0)
456 goto out;
457
458 if (btrtl_dev->cfg_len > 0) {
459 tbuff = kvzalloc(ret + btrtl_dev->cfg_len, GFP_KERNEL);
460 if (!tbuff) {
461 ret = -ENOMEM;
462 goto out;
463 }
464
465 memcpy(tbuff, fw_data, ret);
466 kvfree(fw_data);
467
468 memcpy(tbuff + ret, btrtl_dev->cfg_data, btrtl_dev->cfg_len);
469 ret += btrtl_dev->cfg_len;
470
471 fw_data = tbuff;
472 }
473
474 rtl_dev_info(hdev, "cfg_sz %d, total sz %d\n", btrtl_dev->cfg_len, ret);
475
476 ret = rtl_download_firmware(hdev, fw_data, ret);
477
478 out:
479 kvfree(fw_data);
480 return ret;
481 }
482
btrtl_read_local_version(struct hci_dev * hdev)483 static struct sk_buff *btrtl_read_local_version(struct hci_dev *hdev)
484 {
485 struct sk_buff *skb;
486
487 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
488 HCI_INIT_TIMEOUT);
489 if (IS_ERR(skb)) {
490 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION failed (%ld)\n",
491 PTR_ERR(skb));
492 return skb;
493 }
494
495 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
496 rtl_dev_err(hdev, "HCI_OP_READ_LOCAL_VERSION event length mismatch\n");
497 kfree_skb(skb);
498 return ERR_PTR(-EIO);
499 }
500
501 return skb;
502 }
503
btrtl_free(struct btrtl_device_info * btrtl_dev)504 void btrtl_free(struct btrtl_device_info *btrtl_dev)
505 {
506 kvfree(btrtl_dev->fw_data);
507 kvfree(btrtl_dev->cfg_data);
508 kfree(btrtl_dev);
509 }
510 EXPORT_SYMBOL_GPL(btrtl_free);
511
btrtl_initialize(struct hci_dev * hdev,const char * postfix)512 struct btrtl_device_info *btrtl_initialize(struct hci_dev *hdev,
513 const char *postfix)
514 {
515 struct btrtl_device_info *btrtl_dev;
516 struct sk_buff *skb;
517 struct hci_rp_read_local_version *resp;
518 char cfg_name[40];
519 u16 hci_rev, lmp_subver;
520 u8 hci_ver;
521 int ret;
522
523 btrtl_dev = kzalloc(sizeof(*btrtl_dev), GFP_KERNEL);
524 if (!btrtl_dev) {
525 ret = -ENOMEM;
526 goto err_alloc;
527 }
528
529 skb = btrtl_read_local_version(hdev);
530 if (IS_ERR(skb)) {
531 ret = PTR_ERR(skb);
532 goto err_free;
533 }
534
535 resp = (struct hci_rp_read_local_version *)skb->data;
536 rtl_dev_info(hdev, "rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x lmp_subver=%04x\n",
537 resp->hci_ver, resp->hci_rev,
538 resp->lmp_ver, resp->lmp_subver);
539
540 hci_ver = resp->hci_ver;
541 hci_rev = le16_to_cpu(resp->hci_rev);
542 lmp_subver = le16_to_cpu(resp->lmp_subver);
543 kfree_skb(skb);
544
545 btrtl_dev->ic_info = btrtl_match_ic(lmp_subver, hci_rev, hci_ver,
546 hdev->bus);
547
548 if (!btrtl_dev->ic_info) {
549 rtl_dev_info(hdev, "rtl: unknown IC info, lmp subver %04x, hci rev %04x, hci ver %04x",
550 lmp_subver, hci_rev, hci_ver);
551 return btrtl_dev;
552 }
553
554 if (btrtl_dev->ic_info->has_rom_version) {
555 ret = rtl_read_rom_version(hdev, &btrtl_dev->rom_version);
556 if (ret)
557 goto err_free;
558 }
559
560 btrtl_dev->fw_len = rtl_load_file(hdev, btrtl_dev->ic_info->fw_name,
561 &btrtl_dev->fw_data);
562 if (btrtl_dev->fw_len < 0) {
563 rtl_dev_err(hdev, "firmware file %s not found\n",
564 btrtl_dev->ic_info->fw_name);
565 ret = btrtl_dev->fw_len;
566 goto err_free;
567 }
568
569 if (btrtl_dev->ic_info->cfg_name) {
570 if (postfix) {
571 snprintf(cfg_name, sizeof(cfg_name), "%s-%s.bin",
572 btrtl_dev->ic_info->cfg_name, postfix);
573 } else {
574 snprintf(cfg_name, sizeof(cfg_name), "%s.bin",
575 btrtl_dev->ic_info->cfg_name);
576 }
577 btrtl_dev->cfg_len = rtl_load_file(hdev, cfg_name,
578 &btrtl_dev->cfg_data);
579 if (btrtl_dev->ic_info->config_needed &&
580 btrtl_dev->cfg_len <= 0) {
581 rtl_dev_err(hdev, "mandatory config file %s not found\n",
582 btrtl_dev->ic_info->cfg_name);
583 ret = btrtl_dev->cfg_len;
584 goto err_free;
585 }
586 }
587
588 return btrtl_dev;
589
590 err_free:
591 btrtl_free(btrtl_dev);
592 err_alloc:
593 return ERR_PTR(ret);
594 }
595 EXPORT_SYMBOL_GPL(btrtl_initialize);
596
btrtl_download_firmware(struct hci_dev * hdev,struct btrtl_device_info * btrtl_dev)597 int btrtl_download_firmware(struct hci_dev *hdev,
598 struct btrtl_device_info *btrtl_dev)
599 {
600 /* Match a set of subver values that correspond to stock firmware,
601 * which is not compatible with standard btusb.
602 * If matched, upload an alternative firmware that does conform to
603 * standard btusb. Once that firmware is uploaded, the subver changes
604 * to a different value.
605 */
606 if (!btrtl_dev->ic_info) {
607 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
608 return 0;
609 }
610
611 switch (btrtl_dev->ic_info->lmp_subver) {
612 case RTL_ROM_LMP_8723A:
613 case RTL_ROM_LMP_3499:
614 return btrtl_setup_rtl8723a(hdev, btrtl_dev);
615 case RTL_ROM_LMP_8723B:
616 case RTL_ROM_LMP_8821A:
617 case RTL_ROM_LMP_8761A:
618 case RTL_ROM_LMP_8822B:
619 return btrtl_setup_rtl8723b(hdev, btrtl_dev);
620 default:
621 rtl_dev_info(hdev, "rtl: assuming no firmware upload needed\n");
622 return 0;
623 }
624 }
625 EXPORT_SYMBOL_GPL(btrtl_download_firmware);
626
btrtl_setup_realtek(struct hci_dev * hdev)627 int btrtl_setup_realtek(struct hci_dev *hdev)
628 {
629 struct btrtl_device_info *btrtl_dev;
630 int ret;
631
632 btrtl_dev = btrtl_initialize(hdev, NULL);
633 if (IS_ERR(btrtl_dev))
634 return PTR_ERR(btrtl_dev);
635
636 ret = btrtl_download_firmware(hdev, btrtl_dev);
637
638 btrtl_free(btrtl_dev);
639
640 return ret;
641 }
642 EXPORT_SYMBOL_GPL(btrtl_setup_realtek);
643
btrtl_shutdown_realtek(struct hci_dev * hdev)644 int btrtl_shutdown_realtek(struct hci_dev *hdev)
645 {
646 struct sk_buff *skb;
647 int ret;
648
649 /* According to the vendor driver, BT must be reset on close to avoid
650 * firmware crash.
651 */
652 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
653 if (IS_ERR(skb)) {
654 ret = PTR_ERR(skb);
655 bt_dev_err(hdev, "HCI reset during shutdown failed");
656 return ret;
657 }
658 kfree_skb(skb);
659
660 return 0;
661 }
662 EXPORT_SYMBOL_GPL(btrtl_shutdown_realtek);
663
btrtl_convert_baudrate(u32 device_baudrate)664 static unsigned int btrtl_convert_baudrate(u32 device_baudrate)
665 {
666 switch (device_baudrate) {
667 case 0x0252a00a:
668 return 230400;
669
670 case 0x05f75004:
671 return 921600;
672
673 case 0x00005004:
674 return 1000000;
675
676 case 0x04928002:
677 case 0x01128002:
678 return 1500000;
679
680 case 0x00005002:
681 return 2000000;
682
683 case 0x0000b001:
684 return 2500000;
685
686 case 0x04928001:
687 return 3000000;
688
689 case 0x052a6001:
690 return 3500000;
691
692 case 0x00005001:
693 return 4000000;
694
695 case 0x0252c014:
696 default:
697 return 115200;
698 }
699 }
700
btrtl_get_uart_settings(struct hci_dev * hdev,struct btrtl_device_info * btrtl_dev,unsigned int * controller_baudrate,u32 * device_baudrate,bool * flow_control)701 int btrtl_get_uart_settings(struct hci_dev *hdev,
702 struct btrtl_device_info *btrtl_dev,
703 unsigned int *controller_baudrate,
704 u32 *device_baudrate, bool *flow_control)
705 {
706 struct rtl_vendor_config *config;
707 struct rtl_vendor_config_entry *entry;
708 int i, total_data_len;
709 bool found = false;
710
711 total_data_len = btrtl_dev->cfg_len - sizeof(*config);
712 if (total_data_len <= 0) {
713 rtl_dev_warn(hdev, "no config loaded\n");
714 return -EINVAL;
715 }
716
717 config = (struct rtl_vendor_config *)btrtl_dev->cfg_data;
718 if (le32_to_cpu(config->signature) != RTL_CONFIG_MAGIC) {
719 rtl_dev_err(hdev, "invalid config magic\n");
720 return -EINVAL;
721 }
722
723 if (total_data_len < le16_to_cpu(config->total_len)) {
724 rtl_dev_err(hdev, "config is too short\n");
725 return -EINVAL;
726 }
727
728 for (i = 0; i < total_data_len; ) {
729 entry = ((void *)config->entry) + i;
730
731 switch (le16_to_cpu(entry->offset)) {
732 case 0xc:
733 if (entry->len < sizeof(*device_baudrate)) {
734 rtl_dev_err(hdev, "invalid UART config entry\n");
735 return -EINVAL;
736 }
737
738 *device_baudrate = get_unaligned_le32(entry->data);
739 *controller_baudrate = btrtl_convert_baudrate(
740 *device_baudrate);
741
742 if (entry->len >= 13)
743 *flow_control = !!(entry->data[12] & BIT(2));
744 else
745 *flow_control = false;
746
747 found = true;
748 break;
749
750 default:
751 rtl_dev_dbg(hdev, "skipping config entry 0x%x (len %u)\n",
752 le16_to_cpu(entry->offset), entry->len);
753 break;
754 };
755
756 i += sizeof(*entry) + entry->len;
757 }
758
759 if (!found) {
760 rtl_dev_err(hdev, "no UART config entry found\n");
761 return -ENOENT;
762 }
763
764 rtl_dev_dbg(hdev, "device baudrate = 0x%08x\n", *device_baudrate);
765 rtl_dev_dbg(hdev, "controller baudrate = %u\n", *controller_baudrate);
766 rtl_dev_dbg(hdev, "flow control %d\n", *flow_control);
767
768 return 0;
769 }
770 EXPORT_SYMBOL_GPL(btrtl_get_uart_settings);
771
772 MODULE_AUTHOR("Daniel Drake <drake@endlessm.com>");
773 MODULE_DESCRIPTION("Bluetooth support for Realtek devices ver " VERSION);
774 MODULE_VERSION(VERSION);
775 MODULE_LICENSE("GPL");
776 MODULE_FIRMWARE("rtl_bt/rtl8723a_fw.bin");
777 MODULE_FIRMWARE("rtl_bt/rtl8723b_fw.bin");
778 MODULE_FIRMWARE("rtl_bt/rtl8723b_config.bin");
779 MODULE_FIRMWARE("rtl_bt/rtl8723bs_fw.bin");
780 MODULE_FIRMWARE("rtl_bt/rtl8723bs_config.bin");
781 MODULE_FIRMWARE("rtl_bt/rtl8723ds_fw.bin");
782 MODULE_FIRMWARE("rtl_bt/rtl8723ds_config.bin");
783 MODULE_FIRMWARE("rtl_bt/rtl8761a_fw.bin");
784 MODULE_FIRMWARE("rtl_bt/rtl8761a_config.bin");
785 MODULE_FIRMWARE("rtl_bt/rtl8821a_fw.bin");
786 MODULE_FIRMWARE("rtl_bt/rtl8821a_config.bin");
787 MODULE_FIRMWARE("rtl_bt/rtl8822b_fw.bin");
788 MODULE_FIRMWARE("rtl_bt/rtl8822b_config.bin");
789