Commit | Line | Data |
---|---|---|
45051539 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
83e81961 BYTK |
2 | /* |
3 | * Bluetooth supports for Qualcomm Atheros chips | |
4 | * | |
5 | * Copyright (c) 2015 The Linux Foundation. All rights reserved. | |
83e81961 BYTK |
6 | */ |
7 | #include <linux/module.h> | |
8 | #include <linux/firmware.h> | |
b6459415 | 9 | #include <linux/vmalloc.h> |
83e81961 BYTK |
10 | |
11 | #include <net/bluetooth/bluetooth.h> | |
12 | #include <net/bluetooth/hci_core.h> | |
13 | ||
14 | #include "btqca.h" | |
15 | ||
16 | #define VERSION "0.1" | |
17 | ||
059924fd | 18 | int qca_read_soc_version(struct hci_dev *hdev, struct qca_btsoc_version *ver, |
7d250a06 | 19 | enum qca_btsoc_type soc_type) |
83e81961 BYTK |
20 | { |
21 | struct sk_buff *skb; | |
22 | struct edl_event_hdr *edl; | |
83e81961 BYTK |
23 | char cmd; |
24 | int err = 0; | |
7d250a06 BG |
25 | u8 event_type = HCI_EV_VENDOR; |
26 | u8 rlen = sizeof(*edl) + sizeof(*ver); | |
27 | u8 rtype = EDL_APP_VER_RES_EVT; | |
83e81961 | 28 | |
ba493d4f | 29 | bt_dev_dbg(hdev, "QCA Version Request"); |
83e81961 | 30 | |
7d250a06 BG |
31 | /* Unlike other SoC's sending version command response as payload to |
32 | * VSE event. WCN3991 sends version command response as a payload to | |
33 | * command complete event. | |
34 | */ | |
e5d6468f | 35 | if (soc_type >= QCA_WCN3991) { |
7d250a06 BG |
36 | event_type = 0; |
37 | rlen += 1; | |
38 | rtype = EDL_PATCH_VER_REQ_CMD; | |
39 | } | |
40 | ||
83e81961 BYTK |
41 | cmd = EDL_PATCH_VER_REQ_CMD; |
42 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN, | |
7d250a06 | 43 | &cmd, event_type, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
44 | if (IS_ERR(skb)) { |
45 | err = PTR_ERR(skb); | |
ba493d4f BG |
46 | bt_dev_err(hdev, "Reading QCA version information failed (%d)", |
47 | err); | |
83e81961 BYTK |
48 | return err; |
49 | } | |
50 | ||
7d250a06 | 51 | if (skb->len != rlen) { |
ba493d4f | 52 | bt_dev_err(hdev, "QCA Version size mismatch len %d", skb->len); |
83e81961 BYTK |
53 | err = -EILSEQ; |
54 | goto out; | |
55 | } | |
56 | ||
57 | edl = (struct edl_event_hdr *)(skb->data); | |
0676cab4 | 58 | if (!edl) { |
ba493d4f | 59 | bt_dev_err(hdev, "QCA TLV with no header"); |
83e81961 BYTK |
60 | err = -EILSEQ; |
61 | goto out; | |
62 | } | |
63 | ||
64 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || | |
7d250a06 | 65 | edl->rtype != rtype) { |
ba493d4f BG |
66 | bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp, |
67 | edl->rtype); | |
83e81961 BYTK |
68 | err = -EIO; |
69 | goto out; | |
70 | } | |
71 | ||
e5d6468f | 72 | if (soc_type >= QCA_WCN3991) |
059924fd VLNG |
73 | memcpy(ver, edl->data + 1, sizeof(*ver)); |
74 | else | |
75 | memcpy(ver, &edl->data, sizeof(*ver)); | |
83e81961 | 76 | |
4942857b ZH |
77 | bt_dev_info(hdev, "QCA Product ID :0x%08x", |
78 | le32_to_cpu(ver->product_id)); | |
79 | bt_dev_info(hdev, "QCA SOC Version :0x%08x", | |
80 | le32_to_cpu(ver->soc_id)); | |
81 | bt_dev_info(hdev, "QCA ROM Version :0x%08x", | |
82 | le16_to_cpu(ver->rom_ver)); | |
83 | bt_dev_info(hdev, "QCA Patch Version:0x%08x", | |
84 | le16_to_cpu(ver->patch_ver)); | |
83e81961 | 85 | |
059924fd | 86 | if (ver->soc_id == 0 || ver->rom_ver == 0) |
aadebac4 | 87 | err = -EILSEQ; |
83e81961 BYTK |
88 | |
89 | out: | |
90 | kfree_skb(skb); | |
aadebac4 BG |
91 | if (err) |
92 | bt_dev_err(hdev, "QCA Failed to get version (%d)", err); | |
83e81961 BYTK |
93 | |
94 | return err; | |
95 | } | |
ba493d4f | 96 | EXPORT_SYMBOL_GPL(qca_read_soc_version); |
83e81961 | 97 | |
c0187b0b VLNG |
98 | static int qca_read_fw_build_info(struct hci_dev *hdev) |
99 | { | |
100 | struct sk_buff *skb; | |
101 | struct edl_event_hdr *edl; | |
102 | char cmd, build_label[QCA_FW_BUILD_VER_LEN]; | |
103 | int build_lbl_len, err = 0; | |
104 | ||
105 | bt_dev_dbg(hdev, "QCA read fw build info"); | |
106 | ||
107 | cmd = EDL_GET_BUILD_INFO_CMD; | |
108 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN, | |
109 | &cmd, 0, HCI_INIT_TIMEOUT); | |
110 | if (IS_ERR(skb)) { | |
111 | err = PTR_ERR(skb); | |
112 | bt_dev_err(hdev, "Reading QCA fw build info failed (%d)", | |
113 | err); | |
114 | return err; | |
115 | } | |
116 | ||
117 | edl = (struct edl_event_hdr *)(skb->data); | |
118 | if (!edl) { | |
119 | bt_dev_err(hdev, "QCA read fw build info with no header"); | |
120 | err = -EILSEQ; | |
121 | goto out; | |
122 | } | |
123 | ||
124 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || | |
125 | edl->rtype != EDL_GET_BUILD_INFO_CMD) { | |
126 | bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp, | |
127 | edl->rtype); | |
128 | err = -EIO; | |
129 | goto out; | |
130 | } | |
131 | ||
132 | build_lbl_len = edl->data[0]; | |
133 | if (build_lbl_len <= QCA_FW_BUILD_VER_LEN - 1) { | |
134 | memcpy(build_label, edl->data + 1, build_lbl_len); | |
135 | *(build_label + build_lbl_len) = '\0'; | |
136 | } | |
137 | ||
138 | hci_set_fw_info(hdev, "%s", build_label); | |
139 | ||
140 | out: | |
141 | kfree_skb(skb); | |
142 | return err; | |
143 | } | |
144 | ||
4fac8a7a STA |
145 | static int qca_send_patch_config_cmd(struct hci_dev *hdev) |
146 | { | |
147 | const u8 cmd[] = { EDL_PATCH_CONFIG_CMD, 0x01, 0, 0, 0 }; | |
148 | struct sk_buff *skb; | |
149 | struct edl_event_hdr *edl; | |
150 | int err; | |
151 | ||
152 | bt_dev_dbg(hdev, "QCA Patch config"); | |
153 | ||
154 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, sizeof(cmd), | |
c0dbc560 | 155 | cmd, 0, HCI_INIT_TIMEOUT); |
4fac8a7a STA |
156 | if (IS_ERR(skb)) { |
157 | err = PTR_ERR(skb); | |
158 | bt_dev_err(hdev, "Sending QCA Patch config failed (%d)", err); | |
159 | return err; | |
160 | } | |
161 | ||
162 | if (skb->len != 2) { | |
163 | bt_dev_err(hdev, "QCA Patch config cmd size mismatch len %d", skb->len); | |
164 | err = -EILSEQ; | |
165 | goto out; | |
166 | } | |
167 | ||
168 | edl = (struct edl_event_hdr *)(skb->data); | |
169 | if (!edl) { | |
170 | bt_dev_err(hdev, "QCA Patch config with no header"); | |
171 | err = -EILSEQ; | |
172 | goto out; | |
173 | } | |
174 | ||
175 | if (edl->cresp != EDL_PATCH_CONFIG_RES_EVT || edl->rtype != EDL_PATCH_CONFIG_CMD) { | |
176 | bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp, | |
177 | edl->rtype); | |
178 | err = -EIO; | |
179 | goto out; | |
180 | } | |
181 | ||
182 | err = 0; | |
183 | ||
184 | out: | |
185 | kfree_skb(skb); | |
186 | return err; | |
187 | } | |
188 | ||
ba493d4f | 189 | static int qca_send_reset(struct hci_dev *hdev) |
83e81961 BYTK |
190 | { |
191 | struct sk_buff *skb; | |
192 | int err; | |
193 | ||
ba493d4f | 194 | bt_dev_dbg(hdev, "QCA HCI_RESET"); |
83e81961 BYTK |
195 | |
196 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); | |
197 | if (IS_ERR(skb)) { | |
198 | err = PTR_ERR(skb); | |
ba493d4f | 199 | bt_dev_err(hdev, "QCA Reset failed (%d)", err); |
83e81961 BYTK |
200 | return err; |
201 | } | |
202 | ||
203 | kfree_skb(skb); | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
a7f8dedb TJ |
208 | static int qca_read_fw_board_id(struct hci_dev *hdev, u16 *bid) |
209 | { | |
210 | u8 cmd; | |
211 | struct sk_buff *skb; | |
212 | struct edl_event_hdr *edl; | |
213 | int err = 0; | |
214 | ||
215 | cmd = EDL_GET_BID_REQ_CMD; | |
216 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN, | |
217 | &cmd, 0, HCI_INIT_TIMEOUT); | |
218 | if (IS_ERR(skb)) { | |
219 | err = PTR_ERR(skb); | |
220 | bt_dev_err(hdev, "Reading QCA board ID failed (%d)", err); | |
221 | return err; | |
222 | } | |
223 | ||
224 | edl = skb_pull_data(skb, sizeof(*edl)); | |
225 | if (!edl) { | |
226 | bt_dev_err(hdev, "QCA read board ID with no header"); | |
227 | err = -EILSEQ; | |
228 | goto out; | |
229 | } | |
230 | ||
231 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || | |
232 | edl->rtype != EDL_GET_BID_REQ_CMD) { | |
233 | bt_dev_err(hdev, "QCA Wrong packet: %d %d", edl->cresp, edl->rtype); | |
234 | err = -EIO; | |
235 | goto out; | |
236 | } | |
237 | ||
238 | *bid = (edl->data[1] << 8) + edl->data[2]; | |
239 | bt_dev_dbg(hdev, "%s: bid = %x", __func__, *bid); | |
240 | ||
241 | out: | |
242 | kfree_skb(skb); | |
243 | return err; | |
244 | } | |
245 | ||
a2780889 HB |
246 | int qca_send_pre_shutdown_cmd(struct hci_dev *hdev) |
247 | { | |
248 | struct sk_buff *skb; | |
249 | int err; | |
250 | ||
251 | bt_dev_dbg(hdev, "QCA pre shutdown cmd"); | |
252 | ||
010376ab HB |
253 | skb = __hci_cmd_sync_ev(hdev, QCA_PRE_SHUTDOWN_CMD, 0, |
254 | NULL, HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT); | |
255 | ||
a2780889 HB |
256 | if (IS_ERR(skb)) { |
257 | err = PTR_ERR(skb); | |
258 | bt_dev_err(hdev, "QCA preshutdown_cmd failed (%d)", err); | |
259 | return err; | |
260 | } | |
261 | ||
262 | kfree_skb(skb); | |
263 | ||
264 | return 0; | |
265 | } | |
266 | EXPORT_SYMBOL_GPL(qca_send_pre_shutdown_cmd); | |
267 | ||
ecf6b2d9 VLNG |
268 | static void qca_tlv_check_data(struct hci_dev *hdev, |
269 | struct qca_fw_config *config, | |
b43ca511 | 270 | u8 *fw_data, enum qca_btsoc_type soc_type) |
83e81961 BYTK |
271 | { |
272 | const u8 *data; | |
273 | u32 type_len; | |
274 | u16 tag_id, tag_len; | |
275 | int idx, length; | |
276 | struct tlv_type_hdr *tlv; | |
277 | struct tlv_type_patch *tlv_patch; | |
278 | struct tlv_type_nvm *tlv_nvm; | |
b6388254 | 279 | uint8_t nvm_baud_rate = config->user_baud_rate; |
83e81961 | 280 | |
e303d124 BG |
281 | config->dnld_mode = QCA_SKIP_EVT_NONE; |
282 | config->dnld_type = QCA_SKIP_EVT_NONE; | |
6e03126a | 283 | |
83e81961 | 284 | switch (config->type) { |
ecf6b2d9 VLNG |
285 | case ELF_TYPE_PATCH: |
286 | config->dnld_mode = QCA_SKIP_EVT_VSE_CC; | |
287 | config->dnld_type = QCA_SKIP_EVT_VSE_CC; | |
288 | ||
289 | bt_dev_dbg(hdev, "File Class : 0x%x", fw_data[4]); | |
290 | bt_dev_dbg(hdev, "Data Encoding : 0x%x", fw_data[5]); | |
291 | bt_dev_dbg(hdev, "File version : 0x%x", fw_data[6]); | |
292 | break; | |
83e81961 | 293 | case TLV_TYPE_PATCH: |
ecf6b2d9 VLNG |
294 | tlv = (struct tlv_type_hdr *)fw_data; |
295 | type_len = le32_to_cpu(tlv->type_len); | |
83e81961 | 296 | tlv_patch = (struct tlv_type_patch *)tlv->data; |
6e03126a LP |
297 | |
298 | /* For Rome version 1.1 to 3.1, all segment commands | |
299 | * are acked by a vendor specific event (VSE). | |
300 | * For Rome >= 3.2, the download mode field indicates | |
301 | * if VSE is skipped by the controller. | |
302 | * In case VSE is skipped, only the last segment is acked. | |
303 | */ | |
304 | config->dnld_mode = tlv_patch->download_mode; | |
32646db8 | 305 | config->dnld_type = config->dnld_mode; |
6e03126a | 306 | |
ecf6b2d9 | 307 | BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff); |
6e03126a | 308 | BT_DBG("Total Length : %d bytes", |
83e81961 | 309 | le32_to_cpu(tlv_patch->total_size)); |
6e03126a | 310 | BT_DBG("Patch Data Length : %d bytes", |
83e81961 BYTK |
311 | le32_to_cpu(tlv_patch->data_length)); |
312 | BT_DBG("Signing Format Version : 0x%x", | |
313 | tlv_patch->format_version); | |
6e03126a | 314 | BT_DBG("Signature Algorithm : 0x%x", |
83e81961 | 315 | tlv_patch->signature); |
6e03126a LP |
316 | BT_DBG("Download mode : 0x%x", |
317 | tlv_patch->download_mode); | |
318 | BT_DBG("Reserved : 0x%x", | |
319 | tlv_patch->reserved1); | |
320 | BT_DBG("Product ID : 0x%04x", | |
83e81961 | 321 | le16_to_cpu(tlv_patch->product_id)); |
6e03126a | 322 | BT_DBG("Rom Build Version : 0x%04x", |
83e81961 | 323 | le16_to_cpu(tlv_patch->rom_build)); |
6e03126a | 324 | BT_DBG("Patch Version : 0x%04x", |
83e81961 | 325 | le16_to_cpu(tlv_patch->patch_version)); |
6e03126a | 326 | BT_DBG("Reserved : 0x%x", |
83e81961 | 327 | le16_to_cpu(tlv_patch->reserved2)); |
6e03126a | 328 | BT_DBG("Patch Entry Address : 0x%x", |
83e81961 BYTK |
329 | le32_to_cpu(tlv_patch->entry)); |
330 | break; | |
331 | ||
332 | case TLV_TYPE_NVM: | |
ecf6b2d9 VLNG |
333 | tlv = (struct tlv_type_hdr *)fw_data; |
334 | ||
335 | type_len = le32_to_cpu(tlv->type_len); | |
336 | length = (type_len >> 8) & 0x00ffffff; | |
337 | ||
338 | BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff); | |
339 | BT_DBG("Length\t\t : %d bytes", length); | |
340 | ||
83e81961 BYTK |
341 | idx = 0; |
342 | data = tlv->data; | |
343 | while (idx < length) { | |
344 | tlv_nvm = (struct tlv_type_nvm *)(data + idx); | |
345 | ||
346 | tag_id = le16_to_cpu(tlv_nvm->tag_id); | |
347 | tag_len = le16_to_cpu(tlv_nvm->tag_len); | |
348 | ||
349 | /* Update NVM tags as needed */ | |
350 | switch (tag_id) { | |
351 | case EDL_TAG_ID_HCI: | |
352 | /* HCI transport layer parameters | |
353 | * enabling software inband sleep | |
354 | * onto controller side. | |
355 | */ | |
356 | tlv_nvm->data[0] |= 0x80; | |
357 | ||
358 | /* UART Baud Rate */ | |
e5d6468f | 359 | if (soc_type >= QCA_WCN3991) |
b6388254 RL |
360 | tlv_nvm->data[1] = nvm_baud_rate; |
361 | else | |
362 | tlv_nvm->data[2] = nvm_baud_rate; | |
83e81961 BYTK |
363 | |
364 | break; | |
365 | ||
366 | case EDL_TAG_ID_DEEP_SLEEP: | |
367 | /* Sleep enable mask | |
368 | * enabling deep sleep feature on controller. | |
369 | */ | |
370 | tlv_nvm->data[0] |= 0x01; | |
371 | ||
372 | break; | |
373 | } | |
374 | ||
375 | idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len); | |
376 | } | |
377 | break; | |
378 | ||
379 | default: | |
380 | BT_ERR("Unknown TLV type %d", config->type); | |
381 | break; | |
382 | } | |
383 | } | |
384 | ||
ba493d4f | 385 | static int qca_tlv_send_segment(struct hci_dev *hdev, int seg_size, |
7d250a06 BG |
386 | const u8 *data, enum qca_tlv_dnld_mode mode, |
387 | enum qca_btsoc_type soc_type) | |
83e81961 BYTK |
388 | { |
389 | struct sk_buff *skb; | |
390 | struct edl_event_hdr *edl; | |
391 | struct tlv_seg_resp *tlv_resp; | |
392 | u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2]; | |
393 | int err = 0; | |
7d250a06 BG |
394 | u8 event_type = HCI_EV_VENDOR; |
395 | u8 rlen = (sizeof(*edl) + sizeof(*tlv_resp)); | |
396 | u8 rtype = EDL_TVL_DNLD_RES_EVT; | |
83e81961 | 397 | |
83e81961 BYTK |
398 | cmd[0] = EDL_PATCH_TLV_REQ_CMD; |
399 | cmd[1] = seg_size; | |
400 | memcpy(cmd + 2, data, seg_size); | |
401 | ||
e303d124 | 402 | if (mode == QCA_SKIP_EVT_VSE_CC || mode == QCA_SKIP_EVT_VSE) |
6e03126a LP |
403 | return __hci_cmd_send(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, |
404 | cmd); | |
405 | ||
7d250a06 BG |
406 | /* Unlike other SoC's sending version command response as payload to |
407 | * VSE event. WCN3991 sends version command response as a payload to | |
408 | * command complete event. | |
409 | */ | |
e5d6468f | 410 | if (soc_type >= QCA_WCN3991) { |
7d250a06 BG |
411 | event_type = 0; |
412 | rlen = sizeof(*edl); | |
413 | rtype = EDL_PATCH_TLV_REQ_CMD; | |
414 | } | |
415 | ||
83e81961 | 416 | skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd, |
7d250a06 | 417 | event_type, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
418 | if (IS_ERR(skb)) { |
419 | err = PTR_ERR(skb); | |
ba493d4f | 420 | bt_dev_err(hdev, "QCA Failed to send TLV segment (%d)", err); |
83e81961 BYTK |
421 | return err; |
422 | } | |
423 | ||
7d250a06 | 424 | if (skb->len != rlen) { |
ba493d4f | 425 | bt_dev_err(hdev, "QCA TLV response size mismatch"); |
83e81961 BYTK |
426 | err = -EILSEQ; |
427 | goto out; | |
428 | } | |
429 | ||
430 | edl = (struct edl_event_hdr *)(skb->data); | |
0676cab4 | 431 | if (!edl) { |
ba493d4f | 432 | bt_dev_err(hdev, "TLV with no header"); |
83e81961 BYTK |
433 | err = -EILSEQ; |
434 | goto out; | |
435 | } | |
436 | ||
7d250a06 BG |
437 | if (edl->cresp != EDL_CMD_REQ_RES_EVT || edl->rtype != rtype) { |
438 | bt_dev_err(hdev, "QCA TLV with error stat 0x%x rtype 0x%x", | |
439 | edl->cresp, edl->rtype); | |
440 | err = -EIO; | |
441 | } | |
83e81961 | 442 | |
e5d6468f | 443 | if (soc_type >= QCA_WCN3991) |
7d250a06 BG |
444 | goto out; |
445 | ||
446 | tlv_resp = (struct tlv_seg_resp *)(edl->data); | |
447 | if (tlv_resp->result) { | |
ba493d4f BG |
448 | bt_dev_err(hdev, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)", |
449 | edl->cresp, edl->rtype, tlv_resp->result); | |
83e81961 BYTK |
450 | } |
451 | ||
452 | out: | |
453 | kfree_skb(skb); | |
454 | ||
455 | return err; | |
456 | } | |
457 | ||
32646db8 BG |
458 | static int qca_inject_cmd_complete_event(struct hci_dev *hdev) |
459 | { | |
460 | struct hci_event_hdr *hdr; | |
461 | struct hci_ev_cmd_complete *evt; | |
462 | struct sk_buff *skb; | |
463 | ||
464 | skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL); | |
465 | if (!skb) | |
466 | return -ENOMEM; | |
467 | ||
468 | hdr = skb_put(skb, sizeof(*hdr)); | |
469 | hdr->evt = HCI_EV_CMD_COMPLETE; | |
470 | hdr->plen = sizeof(*evt) + 1; | |
471 | ||
472 | evt = skb_put(skb, sizeof(*evt)); | |
473 | evt->ncmd = 1; | |
2fde6afb | 474 | evt->opcode = cpu_to_le16(QCA_HCI_CC_OPCODE); |
32646db8 BG |
475 | |
476 | skb_put_u8(skb, QCA_HCI_CC_SUCCESS); | |
477 | ||
478 | hci_skb_pkt_type(skb) = HCI_EVENT_PKT; | |
479 | ||
480 | return hci_recv_frame(hdev, skb); | |
481 | } | |
482 | ||
ba493d4f | 483 | static int qca_download_firmware(struct hci_dev *hdev, |
7d250a06 | 484 | struct qca_fw_config *config, |
ecf6b2d9 VLNG |
485 | enum qca_btsoc_type soc_type, |
486 | u8 rom_ver) | |
83e81961 BYTK |
487 | { |
488 | const struct firmware *fw; | |
b43ca511 | 489 | u8 *data; |
6e03126a | 490 | const u8 *segment; |
b43ca511 | 491 | int ret, size, remain, i = 0; |
83e81961 | 492 | |
ba493d4f | 493 | bt_dev_info(hdev, "QCA Downloading %s", config->fwname); |
83e81961 BYTK |
494 | |
495 | ret = request_firmware(&fw, config->fwname, &hdev->dev); | |
496 | if (ret) { | |
ecf6b2d9 VLNG |
497 | /* For WCN6750, if mbn file is not present then check for |
498 | * tlv file. | |
499 | */ | |
500 | if (soc_type == QCA_WCN6750 && config->type == ELF_TYPE_PATCH) { | |
501 | bt_dev_dbg(hdev, "QCA Failed to request file: %s (%d)", | |
502 | config->fwname, ret); | |
503 | config->type = TLV_TYPE_PATCH; | |
504 | snprintf(config->fwname, sizeof(config->fwname), | |
505 | "qca/msbtfw%02x.tlv", rom_ver); | |
506 | bt_dev_info(hdev, "QCA Downloading %s", config->fwname); | |
507 | ret = request_firmware(&fw, config->fwname, &hdev->dev); | |
508 | if (ret) { | |
509 | bt_dev_err(hdev, "QCA Failed to request file: %s (%d)", | |
510 | config->fwname, ret); | |
511 | return ret; | |
512 | } | |
513 | } else { | |
514 | bt_dev_err(hdev, "QCA Failed to request file: %s (%d)", | |
515 | config->fwname, ret); | |
516 | return ret; | |
517 | } | |
83e81961 BYTK |
518 | } |
519 | ||
b43ca511 CA |
520 | size = fw->size; |
521 | data = vmalloc(fw->size); | |
522 | if (!data) { | |
523 | bt_dev_err(hdev, "QCA Failed to allocate memory for file: %s", | |
524 | config->fwname); | |
525 | release_firmware(fw); | |
526 | return -ENOMEM; | |
527 | } | |
528 | ||
529 | memcpy(data, fw->data, size); | |
530 | release_firmware(fw); | |
531 | ||
ecf6b2d9 | 532 | qca_tlv_check_data(hdev, config, data, soc_type); |
83e81961 | 533 | |
b43ca511 CA |
534 | segment = data; |
535 | remain = size; | |
6e03126a LP |
536 | while (remain > 0) { |
537 | int segsize = min(MAX_SIZE_PER_TLV_SEGMENT, remain); | |
538 | ||
539 | bt_dev_dbg(hdev, "Send segment %d, size %d", i++, segsize); | |
540 | ||
541 | remain -= segsize; | |
542 | /* The last segment is always acked regardless download mode */ | |
543 | if (!remain || segsize < MAX_SIZE_PER_TLV_SEGMENT) | |
e303d124 | 544 | config->dnld_mode = QCA_SKIP_EVT_NONE; |
6e03126a | 545 | |
ba493d4f | 546 | ret = qca_tlv_send_segment(hdev, segsize, segment, |
7d250a06 | 547 | config->dnld_mode, soc_type); |
6e03126a | 548 | if (ret) |
32646db8 | 549 | goto out; |
6e03126a LP |
550 | |
551 | segment += segsize; | |
83e81961 BYTK |
552 | } |
553 | ||
32646db8 BG |
554 | /* Latest qualcomm chipsets are not sending a command complete event |
555 | * for every fw packet sent. They only respond with a vendor specific | |
556 | * event for the last packet. This optimization in the chip will | |
557 | * decrease the BT in initialization time. Here we will inject a command | |
558 | * complete event to avoid a command timeout error message. | |
559 | */ | |
e303d124 BG |
560 | if (config->dnld_type == QCA_SKIP_EVT_VSE_CC || |
561 | config->dnld_type == QCA_SKIP_EVT_VSE) | |
c7c5ae29 | 562 | ret = qca_inject_cmd_complete_event(hdev); |
32646db8 BG |
563 | |
564 | out: | |
b43ca511 | 565 | vfree(data); |
83e81961 BYTK |
566 | |
567 | return ret; | |
568 | } | |
569 | ||
590deccf BG |
570 | static int qca_disable_soc_logging(struct hci_dev *hdev) |
571 | { | |
572 | struct sk_buff *skb; | |
573 | u8 cmd[2]; | |
574 | int err; | |
575 | ||
576 | cmd[0] = QCA_DISABLE_LOGGING_SUB_OP; | |
577 | cmd[1] = 0x00; | |
578 | skb = __hci_cmd_sync_ev(hdev, QCA_DISABLE_LOGGING, sizeof(cmd), cmd, | |
579 | HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT); | |
580 | if (IS_ERR(skb)) { | |
581 | err = PTR_ERR(skb); | |
582 | bt_dev_err(hdev, "QCA Failed to disable soc logging(%d)", err); | |
583 | return err; | |
584 | } | |
585 | ||
586 | kfree_skb(skb); | |
587 | ||
588 | return 0; | |
589 | } | |
590 | ||
83e81961 BYTK |
591 | int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
592 | { | |
593 | struct sk_buff *skb; | |
594 | u8 cmd[9]; | |
595 | int err; | |
596 | ||
597 | cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD; | |
598 | cmd[1] = 0x02; /* TAG ID */ | |
599 | cmd[2] = sizeof(bdaddr_t); /* size */ | |
600 | memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t)); | |
601 | skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd, | |
e4cc5a18 | 602 | HCI_EV_VENDOR, HCI_INIT_TIMEOUT); |
83e81961 BYTK |
603 | if (IS_ERR(skb)) { |
604 | err = PTR_ERR(skb); | |
ba493d4f | 605 | bt_dev_err(hdev, "QCA Change address command failed (%d)", err); |
83e81961 BYTK |
606 | return err; |
607 | } | |
608 | ||
609 | kfree_skb(skb); | |
610 | ||
611 | return 0; | |
612 | } | |
613 | EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome); | |
614 | ||
a7f8dedb TJ |
615 | static void qca_generate_hsp_nvm_name(char *fwname, size_t max_size, |
616 | struct qca_btsoc_version ver, u8 rom_ver, u16 bid) | |
617 | { | |
618 | const char *variant; | |
619 | ||
620 | /* hsp gf chip */ | |
621 | if ((le32_to_cpu(ver.soc_id) & QCA_HSP_GF_SOC_MASK) == QCA_HSP_GF_SOC_ID) | |
622 | variant = "g"; | |
623 | else | |
624 | variant = ""; | |
625 | ||
626 | if (bid == 0x0) | |
627 | snprintf(fwname, max_size, "qca/hpnv%02x%s.bin", rom_ver, variant); | |
628 | else | |
629 | snprintf(fwname, max_size, "qca/hpnv%02x%s.%x", rom_ver, variant, bid); | |
630 | } | |
631 | ||
aadebac4 | 632 | int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate, |
059924fd | 633 | enum qca_btsoc_type soc_type, struct qca_btsoc_version ver, |
99c905c6 | 634 | const char *firmware_name) |
83e81961 | 635 | { |
e303d124 | 636 | struct qca_fw_config config; |
83e81961 | 637 | int err; |
523760b7 | 638 | u8 rom_ver = 0; |
059924fd | 639 | u32 soc_ver; |
a7f8dedb | 640 | u16 boardid = 0; |
83e81961 | 641 | |
ba493d4f | 642 | bt_dev_dbg(hdev, "QCA setup on UART"); |
83e81961 | 643 | |
059924fd VLNG |
644 | soc_ver = get_soc_ver(ver.soc_id, ver.rom_ver); |
645 | ||
646 | bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver); | |
647 | ||
83e81961 BYTK |
648 | config.user_baud_rate = baudrate; |
649 | ||
99fba8e3 VLNG |
650 | /* Firmware files to download are based on ROM version. |
651 | * ROM version is derived from last two bytes of soc_ver. | |
652 | */ | |
f904feef LW |
653 | if (soc_type == QCA_WCN3988) |
654 | rom_ver = ((soc_ver & 0x00000f00) >> 0x05) | (soc_ver & 0x0000000f); | |
655 | else | |
656 | rom_ver = ((soc_ver & 0x00000f00) >> 0x04) | (soc_ver & 0x0000000f); | |
99fba8e3 | 657 | |
4fac8a7a STA |
658 | if (soc_type == QCA_WCN6750) |
659 | qca_send_patch_config_cmd(hdev); | |
660 | ||
83e81961 BYTK |
661 | /* Download rampatch file */ |
662 | config.type = TLV_TYPE_PATCH; | |
691d54d0 NA |
663 | switch (soc_type) { |
664 | case QCA_WCN3990: | |
665 | case QCA_WCN3991: | |
666 | case QCA_WCN3998: | |
4219d468 BG |
667 | snprintf(config.fwname, sizeof(config.fwname), |
668 | "qca/crbtfw%02x.tlv", rom_ver); | |
691d54d0 NA |
669 | break; |
670 | case QCA_WCN3988: | |
671 | snprintf(config.fwname, sizeof(config.fwname), | |
672 | "qca/apbtfw%02x.tlv", rom_ver); | |
673 | break; | |
a7f8dedb TJ |
674 | case QCA_QCA2066: |
675 | snprintf(config.fwname, sizeof(config.fwname), | |
676 | "qca/hpbtfw%02x.tlv", rom_ver); | |
677 | break; | |
691d54d0 | 678 | case QCA_QCA6390: |
e5d6468f RL |
679 | snprintf(config.fwname, sizeof(config.fwname), |
680 | "qca/htbtfw%02x.tlv", rom_ver); | |
691d54d0 NA |
681 | break; |
682 | case QCA_WCN6750: | |
ecf6b2d9 VLNG |
683 | /* Choose mbn file by default.If mbn file is not found |
684 | * then choose tlv file | |
685 | */ | |
686 | config.type = ELF_TYPE_PATCH; | |
d8f97da1 | 687 | snprintf(config.fwname, sizeof(config.fwname), |
ecf6b2d9 | 688 | "qca/msbtfw%02x.mbn", rom_ver); |
691d54d0 NA |
689 | break; |
690 | case QCA_WCN6855: | |
095327fe SK |
691 | snprintf(config.fwname, sizeof(config.fwname), |
692 | "qca/hpbtfw%02x.tlv", rom_ver); | |
691d54d0 | 693 | break; |
e0c1278a NA |
694 | case QCA_WCN7850: |
695 | snprintf(config.fwname, sizeof(config.fwname), | |
696 | "qca/hmtbtfw%02x.tlv", rom_ver); | |
697 | break; | |
691d54d0 | 698 | default: |
4219d468 BG |
699 | snprintf(config.fwname, sizeof(config.fwname), |
700 | "qca/rampatch_%08x.bin", soc_ver); | |
701 | } | |
702 | ||
ecf6b2d9 | 703 | err = qca_download_firmware(hdev, &config, soc_type, rom_ver); |
83e81961 | 704 | if (err < 0) { |
ba493d4f | 705 | bt_dev_err(hdev, "QCA Failed to download patch (%d)", err); |
83e81961 BYTK |
706 | return err; |
707 | } | |
708 | ||
8059ba0b MK |
709 | /* Give the controller some time to get ready to receive the NVM */ |
710 | msleep(10); | |
711 | ||
a7f8dedb TJ |
712 | if (soc_type == QCA_QCA2066) |
713 | qca_read_fw_board_id(hdev, &boardid); | |
714 | ||
83e81961 BYTK |
715 | /* Download NVM configuration */ |
716 | config.type = TLV_TYPE_NVM; | |
691d54d0 | 717 | if (firmware_name) { |
99c905c6 RL |
718 | snprintf(config.fwname, sizeof(config.fwname), |
719 | "qca/%s", firmware_name); | |
691d54d0 NA |
720 | } else { |
721 | switch (soc_type) { | |
722 | case QCA_WCN3990: | |
723 | case QCA_WCN3991: | |
724 | case QCA_WCN3998: | |
725 | if (le32_to_cpu(ver.soc_id) == QCA_WCN3991_SOC_ID) { | |
726 | snprintf(config.fwname, sizeof(config.fwname), | |
727 | "qca/crnv%02xu.bin", rom_ver); | |
728 | } else { | |
729 | snprintf(config.fwname, sizeof(config.fwname), | |
730 | "qca/crnv%02x.bin", rom_ver); | |
731 | } | |
732 | break; | |
733 | case QCA_WCN3988: | |
059924fd | 734 | snprintf(config.fwname, sizeof(config.fwname), |
691d54d0 NA |
735 | "qca/apnv%02x.bin", rom_ver); |
736 | break; | |
a7f8dedb TJ |
737 | case QCA_QCA2066: |
738 | qca_generate_hsp_nvm_name(config.fwname, | |
739 | sizeof(config.fwname), ver, rom_ver, boardid); | |
740 | break; | |
691d54d0 NA |
741 | case QCA_QCA6390: |
742 | snprintf(config.fwname, sizeof(config.fwname), | |
743 | "qca/htnv%02x.bin", rom_ver); | |
744 | break; | |
745 | case QCA_WCN6750: | |
059924fd | 746 | snprintf(config.fwname, sizeof(config.fwname), |
691d54d0 NA |
747 | "qca/msnv%02x.bin", rom_ver); |
748 | break; | |
749 | case QCA_WCN6855: | |
750 | snprintf(config.fwname, sizeof(config.fwname), | |
751 | "qca/hpnv%02x.bin", rom_ver); | |
752 | break; | |
e0c1278a NA |
753 | case QCA_WCN7850: |
754 | snprintf(config.fwname, sizeof(config.fwname), | |
755 | "qca/hmtnv%02x.bin", rom_ver); | |
756 | break; | |
691d54d0 NA |
757 | |
758 | default: | |
759 | snprintf(config.fwname, sizeof(config.fwname), | |
760 | "qca/nvm_%08x.bin", soc_ver); | |
059924fd VLNG |
761 | } |
762 | } | |
4219d468 | 763 | |
ecf6b2d9 | 764 | err = qca_download_firmware(hdev, &config, soc_type, rom_ver); |
83e81961 | 765 | if (err < 0) { |
ba493d4f | 766 | bt_dev_err(hdev, "QCA Failed to download NVM (%d)", err); |
83e81961 BYTK |
767 | return err; |
768 | } | |
769 | ||
691d54d0 NA |
770 | switch (soc_type) { |
771 | case QCA_WCN3991: | |
a7f8dedb | 772 | case QCA_QCA2066: |
691d54d0 NA |
773 | case QCA_QCA6390: |
774 | case QCA_WCN6750: | |
775 | case QCA_WCN6855: | |
e0c1278a | 776 | case QCA_WCN7850: |
590deccf BG |
777 | err = qca_disable_soc_logging(hdev); |
778 | if (err < 0) | |
779 | return err; | |
691d54d0 NA |
780 | break; |
781 | default: | |
782 | break; | |
590deccf BG |
783 | } |
784 | ||
d8f97da1 | 785 | /* WCN399x and WCN6750 supports the Microsoft vendor extension with 0xFD70 as the |
eaf19b0c MC |
786 | * VsMsftOpCode. |
787 | */ | |
788 | switch (soc_type) { | |
691d54d0 | 789 | case QCA_WCN3988: |
eaf19b0c MC |
790 | case QCA_WCN3990: |
791 | case QCA_WCN3991: | |
792 | case QCA_WCN3998: | |
d8f97da1 | 793 | case QCA_WCN6750: |
eaf19b0c MC |
794 | hci_set_msft_opcode(hdev, 0xFD70); |
795 | break; | |
796 | default: | |
797 | break; | |
798 | } | |
799 | ||
83e81961 | 800 | /* Perform HCI reset */ |
ba493d4f | 801 | err = qca_send_reset(hdev); |
83e81961 | 802 | if (err < 0) { |
ba493d4f | 803 | bt_dev_err(hdev, "QCA Failed to run HCI_RESET (%d)", err); |
83e81961 BYTK |
804 | return err; |
805 | } | |
806 | ||
095327fe SK |
807 | switch (soc_type) { |
808 | case QCA_WCN3991: | |
809 | case QCA_WCN6750: | |
810 | case QCA_WCN6855: | |
e0c1278a | 811 | case QCA_WCN7850: |
c0187b0b VLNG |
812 | /* get fw build info */ |
813 | err = qca_read_fw_build_info(hdev); | |
814 | if (err < 0) | |
815 | return err; | |
095327fe SK |
816 | break; |
817 | default: | |
818 | break; | |
c0187b0b VLNG |
819 | } |
820 | ||
ba493d4f | 821 | bt_dev_info(hdev, "QCA setup on UART is completed"); |
83e81961 BYTK |
822 | |
823 | return 0; | |
824 | } | |
ba493d4f | 825 | EXPORT_SYMBOL_GPL(qca_uart_setup); |
83e81961 | 826 | |
5c0a1001 BG |
827 | int qca_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
828 | { | |
829 | struct sk_buff *skb; | |
830 | int err; | |
831 | ||
832 | skb = __hci_cmd_sync_ev(hdev, EDL_WRITE_BD_ADDR_OPCODE, 6, bdaddr, | |
833 | HCI_EV_VENDOR, HCI_INIT_TIMEOUT); | |
834 | if (IS_ERR(skb)) { | |
835 | err = PTR_ERR(skb); | |
836 | bt_dev_err(hdev, "QCA Change address cmd failed (%d)", err); | |
837 | return err; | |
838 | } | |
839 | ||
840 | kfree_skb(skb); | |
841 | ||
842 | return 0; | |
843 | } | |
844 | EXPORT_SYMBOL_GPL(qca_set_bdaddr); | |
845 | ||
523760b7 | 846 | |
83e81961 BYTK |
847 | MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>"); |
848 | MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION); | |
849 | MODULE_VERSION(VERSION); | |
850 | MODULE_LICENSE("GPL"); |