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