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