1 // SPDX-License-Identifier: GPL-2.0-only
3 * Bluetooth supports for Qualcomm Atheros chips
5 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
7 #include <linux/module.h>
8 #include <linux/firmware.h>
9 #include <linux/vmalloc.h>
11 #include <net/bluetooth/bluetooth.h>
12 #include <net/bluetooth/hci_core.h>
18 int qca_read_soc_version(struct hci_dev *hdev, struct qca_btsoc_version *ver,
19 enum qca_btsoc_type soc_type)
22 struct edl_event_hdr *edl;
25 u8 event_type = HCI_EV_VENDOR;
26 u8 rlen = sizeof(*edl) + sizeof(*ver);
27 u8 rtype = EDL_APP_VER_RES_EVT;
29 bt_dev_dbg(hdev, "QCA Version Request");
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.
35 if (soc_type >= QCA_WCN3991) {
38 rtype = EDL_PATCH_VER_REQ_CMD;
41 cmd = EDL_PATCH_VER_REQ_CMD;
42 skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN,
43 &cmd, event_type, HCI_INIT_TIMEOUT);
46 bt_dev_err(hdev, "Reading QCA version information failed (%d)",
51 if (skb->len != rlen) {
52 bt_dev_err(hdev, "QCA Version size mismatch len %d", skb->len);
57 edl = (struct edl_event_hdr *)(skb->data);
59 bt_dev_err(hdev, "QCA TLV with no header");
64 if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
65 edl->rtype != rtype) {
66 bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp,
72 if (soc_type >= QCA_WCN3991)
73 memcpy(ver, edl->data + 1, sizeof(*ver));
75 memcpy(ver, &edl->data, sizeof(*ver));
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));
86 if (ver->soc_id == 0 || ver->rom_ver == 0)
92 bt_dev_err(hdev, "QCA Failed to get version (%d)", err);
96 EXPORT_SYMBOL_GPL(qca_read_soc_version);
98 static int qca_read_fw_build_info(struct hci_dev *hdev)
101 struct edl_event_hdr *edl;
102 char cmd, build_label[QCA_FW_BUILD_VER_LEN];
103 int build_lbl_len, err = 0;
105 bt_dev_dbg(hdev, "QCA read fw build info");
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);
112 bt_dev_err(hdev, "Reading QCA fw build info failed (%d)",
117 edl = (struct edl_event_hdr *)(skb->data);
119 bt_dev_err(hdev, "QCA read fw build info with no header");
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,
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';
138 hci_set_fw_info(hdev, "%s", build_label);
145 static int qca_send_patch_config_cmd(struct hci_dev *hdev)
147 const u8 cmd[] = { EDL_PATCH_CONFIG_CMD, 0x01, 0, 0, 0 };
149 struct edl_event_hdr *edl;
152 bt_dev_dbg(hdev, "QCA Patch config");
154 skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, sizeof(cmd),
155 cmd, HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
158 bt_dev_err(hdev, "Sending QCA Patch config failed (%d)", err);
163 bt_dev_err(hdev, "QCA Patch config cmd size mismatch len %d", skb->len);
168 edl = (struct edl_event_hdr *)(skb->data);
170 bt_dev_err(hdev, "QCA Patch config with no header");
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,
189 static int qca_send_reset(struct hci_dev *hdev)
194 bt_dev_dbg(hdev, "QCA HCI_RESET");
196 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
199 bt_dev_err(hdev, "QCA Reset failed (%d)", err);
208 static int qca_read_fw_board_id(struct hci_dev *hdev, u16 *bid)
212 struct edl_event_hdr *edl;
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);
220 bt_dev_err(hdev, "Reading QCA board ID failed (%d)", err);
224 edl = skb_pull_data(skb, sizeof(*edl));
226 bt_dev_err(hdev, "QCA read board ID with no header");
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);
238 *bid = (edl->data[1] << 8) + edl->data[2];
239 bt_dev_dbg(hdev, "%s: bid = %x", __func__, *bid);
246 int qca_send_pre_shutdown_cmd(struct hci_dev *hdev)
251 bt_dev_dbg(hdev, "QCA pre shutdown cmd");
253 skb = __hci_cmd_sync_ev(hdev, QCA_PRE_SHUTDOWN_CMD, 0,
254 NULL, HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
258 bt_dev_err(hdev, "QCA preshutdown_cmd failed (%d)", err);
266 EXPORT_SYMBOL_GPL(qca_send_pre_shutdown_cmd);
268 static void qca_tlv_check_data(struct hci_dev *hdev,
269 struct qca_fw_config *config,
270 u8 *fw_data, enum qca_btsoc_type soc_type)
276 struct tlv_type_hdr *tlv;
277 struct tlv_type_patch *tlv_patch;
278 struct tlv_type_nvm *tlv_nvm;
279 uint8_t nvm_baud_rate = config->user_baud_rate;
281 config->dnld_mode = QCA_SKIP_EVT_NONE;
282 config->dnld_type = QCA_SKIP_EVT_NONE;
284 switch (config->type) {
286 config->dnld_mode = QCA_SKIP_EVT_VSE_CC;
287 config->dnld_type = QCA_SKIP_EVT_VSE_CC;
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]);
294 tlv = (struct tlv_type_hdr *)fw_data;
295 type_len = le32_to_cpu(tlv->type_len);
296 tlv_patch = (struct tlv_type_patch *)tlv->data;
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.
304 config->dnld_mode = tlv_patch->download_mode;
305 config->dnld_type = config->dnld_mode;
307 BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
308 BT_DBG("Total Length : %d bytes",
309 le32_to_cpu(tlv_patch->total_size));
310 BT_DBG("Patch Data Length : %d bytes",
311 le32_to_cpu(tlv_patch->data_length));
312 BT_DBG("Signing Format Version : 0x%x",
313 tlv_patch->format_version);
314 BT_DBG("Signature Algorithm : 0x%x",
315 tlv_patch->signature);
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",
321 le16_to_cpu(tlv_patch->product_id));
322 BT_DBG("Rom Build Version : 0x%04x",
323 le16_to_cpu(tlv_patch->rom_build));
324 BT_DBG("Patch Version : 0x%04x",
325 le16_to_cpu(tlv_patch->patch_version));
326 BT_DBG("Reserved : 0x%x",
327 le16_to_cpu(tlv_patch->reserved2));
328 BT_DBG("Patch Entry Address : 0x%x",
329 le32_to_cpu(tlv_patch->entry));
333 tlv = (struct tlv_type_hdr *)fw_data;
335 type_len = le32_to_cpu(tlv->type_len);
336 length = (type_len >> 8) & 0x00ffffff;
338 BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
339 BT_DBG("Length\t\t : %d bytes", length);
343 while (idx < length) {
344 tlv_nvm = (struct tlv_type_nvm *)(data + idx);
346 tag_id = le16_to_cpu(tlv_nvm->tag_id);
347 tag_len = le16_to_cpu(tlv_nvm->tag_len);
349 /* Update NVM tags as needed */
352 /* HCI transport layer parameters
353 * enabling software inband sleep
354 * onto controller side.
356 tlv_nvm->data[0] |= 0x80;
359 if (soc_type >= QCA_WCN3991)
360 tlv_nvm->data[1] = nvm_baud_rate;
362 tlv_nvm->data[2] = nvm_baud_rate;
366 case EDL_TAG_ID_DEEP_SLEEP:
368 * enabling deep sleep feature on controller.
370 tlv_nvm->data[0] |= 0x01;
375 idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len);
380 BT_ERR("Unknown TLV type %d", config->type);
385 static int qca_tlv_send_segment(struct hci_dev *hdev, int seg_size,
386 const u8 *data, enum qca_tlv_dnld_mode mode,
387 enum qca_btsoc_type soc_type)
390 struct edl_event_hdr *edl;
391 struct tlv_seg_resp *tlv_resp;
392 u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2];
394 u8 event_type = HCI_EV_VENDOR;
395 u8 rlen = (sizeof(*edl) + sizeof(*tlv_resp));
396 u8 rtype = EDL_TVL_DNLD_RES_EVT;
398 cmd[0] = EDL_PATCH_TLV_REQ_CMD;
400 memcpy(cmd + 2, data, seg_size);
402 if (mode == QCA_SKIP_EVT_VSE_CC || mode == QCA_SKIP_EVT_VSE)
403 return __hci_cmd_send(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2,
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.
410 if (soc_type >= QCA_WCN3991) {
413 rtype = EDL_PATCH_TLV_REQ_CMD;
416 skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd,
417 event_type, HCI_INIT_TIMEOUT);
420 bt_dev_err(hdev, "QCA Failed to send TLV segment (%d)", err);
424 if (skb->len != rlen) {
425 bt_dev_err(hdev, "QCA TLV response size mismatch");
430 edl = (struct edl_event_hdr *)(skb->data);
432 bt_dev_err(hdev, "TLV with no header");
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);
443 if (soc_type >= QCA_WCN3991)
446 tlv_resp = (struct tlv_seg_resp *)(edl->data);
447 if (tlv_resp->result) {
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);
458 static int qca_inject_cmd_complete_event(struct hci_dev *hdev)
460 struct hci_event_hdr *hdr;
461 struct hci_ev_cmd_complete *evt;
464 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
468 hdr = skb_put(skb, sizeof(*hdr));
469 hdr->evt = HCI_EV_CMD_COMPLETE;
470 hdr->plen = sizeof(*evt) + 1;
472 evt = skb_put(skb, sizeof(*evt));
474 evt->opcode = cpu_to_le16(QCA_HCI_CC_OPCODE);
476 skb_put_u8(skb, QCA_HCI_CC_SUCCESS);
478 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
480 return hci_recv_frame(hdev, skb);
483 static int qca_download_firmware(struct hci_dev *hdev,
484 struct qca_fw_config *config,
485 enum qca_btsoc_type soc_type,
488 const struct firmware *fw;
491 int ret, size, remain, i = 0;
493 bt_dev_info(hdev, "QCA Downloading %s", config->fwname);
495 ret = request_firmware(&fw, config->fwname, &hdev->dev);
497 /* For WCN6750, if mbn file is not present then check for
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);
509 bt_dev_err(hdev, "QCA Failed to request file: %s (%d)",
510 config->fwname, ret);
514 bt_dev_err(hdev, "QCA Failed to request file: %s (%d)",
515 config->fwname, ret);
521 data = vmalloc(fw->size);
523 bt_dev_err(hdev, "QCA Failed to allocate memory for file: %s",
525 release_firmware(fw);
529 memcpy(data, fw->data, size);
530 release_firmware(fw);
532 qca_tlv_check_data(hdev, config, data, soc_type);
537 int segsize = min(MAX_SIZE_PER_TLV_SEGMENT, remain);
539 bt_dev_dbg(hdev, "Send segment %d, size %d", i++, segsize);
542 /* The last segment is always acked regardless download mode */
543 if (!remain || segsize < MAX_SIZE_PER_TLV_SEGMENT)
544 config->dnld_mode = QCA_SKIP_EVT_NONE;
546 ret = qca_tlv_send_segment(hdev, segsize, segment,
547 config->dnld_mode, soc_type);
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.
560 if (config->dnld_type == QCA_SKIP_EVT_VSE_CC ||
561 config->dnld_type == QCA_SKIP_EVT_VSE)
562 ret = qca_inject_cmd_complete_event(hdev);
570 static int qca_disable_soc_logging(struct hci_dev *hdev)
576 cmd[0] = QCA_DISABLE_LOGGING_SUB_OP;
578 skb = __hci_cmd_sync_ev(hdev, QCA_DISABLE_LOGGING, sizeof(cmd), cmd,
579 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
582 bt_dev_err(hdev, "QCA Failed to disable soc logging(%d)", err);
591 int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr)
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,
602 HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
605 bt_dev_err(hdev, "QCA Change address command failed (%d)", err);
613 EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome);
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)
621 if ((le32_to_cpu(ver.soc_id) & QCA_HSP_GF_SOC_MASK) == QCA_HSP_GF_SOC_ID)
627 snprintf(fwname, max_size, "qca/hpnv%02x%s.bin", rom_ver, variant);
629 snprintf(fwname, max_size, "qca/hpnv%02x%s.%x", rom_ver, variant, bid);
632 int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate,
633 enum qca_btsoc_type soc_type, struct qca_btsoc_version ver,
634 const char *firmware_name)
636 struct qca_fw_config config;
642 bt_dev_dbg(hdev, "QCA setup on UART");
644 soc_ver = get_soc_ver(ver.soc_id, ver.rom_ver);
646 bt_dev_info(hdev, "QCA controller version 0x%08x", soc_ver);
648 config.user_baud_rate = baudrate;
650 /* Firmware files to download are based on ROM version.
651 * ROM version is derived from last two bytes of soc_ver.
653 if (soc_type == QCA_WCN3988)
654 rom_ver = ((soc_ver & 0x00000f00) >> 0x05) | (soc_ver & 0x0000000f);
656 rom_ver = ((soc_ver & 0x00000f00) >> 0x04) | (soc_ver & 0x0000000f);
658 if (soc_type == QCA_WCN6750)
659 qca_send_patch_config_cmd(hdev);
661 /* Download rampatch file */
662 config.type = TLV_TYPE_PATCH;
667 snprintf(config.fwname, sizeof(config.fwname),
668 "qca/crbtfw%02x.tlv", rom_ver);
671 snprintf(config.fwname, sizeof(config.fwname),
672 "qca/apbtfw%02x.tlv", rom_ver);
675 snprintf(config.fwname, sizeof(config.fwname),
676 "qca/hpbtfw%02x.tlv", rom_ver);
679 snprintf(config.fwname, sizeof(config.fwname),
680 "qca/htbtfw%02x.tlv", rom_ver);
683 /* Choose mbn file by default.If mbn file is not found
684 * then choose tlv file
686 config.type = ELF_TYPE_PATCH;
687 snprintf(config.fwname, sizeof(config.fwname),
688 "qca/msbtfw%02x.mbn", rom_ver);
691 snprintf(config.fwname, sizeof(config.fwname),
692 "qca/hpbtfw%02x.tlv", rom_ver);
695 snprintf(config.fwname, sizeof(config.fwname),
696 "qca/hmtbtfw%02x.tlv", rom_ver);
699 snprintf(config.fwname, sizeof(config.fwname),
700 "qca/rampatch_%08x.bin", soc_ver);
703 err = qca_download_firmware(hdev, &config, soc_type, rom_ver);
705 bt_dev_err(hdev, "QCA Failed to download patch (%d)", err);
709 /* Give the controller some time to get ready to receive the NVM */
712 if (soc_type == QCA_QCA2066)
713 qca_read_fw_board_id(hdev, &boardid);
715 /* Download NVM configuration */
716 config.type = TLV_TYPE_NVM;
718 snprintf(config.fwname, sizeof(config.fwname),
719 "qca/%s", firmware_name);
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);
729 snprintf(config.fwname, sizeof(config.fwname),
730 "qca/crnv%02x.bin", rom_ver);
734 snprintf(config.fwname, sizeof(config.fwname),
735 "qca/apnv%02x.bin", rom_ver);
738 qca_generate_hsp_nvm_name(config.fwname,
739 sizeof(config.fwname), ver, rom_ver, boardid);
742 snprintf(config.fwname, sizeof(config.fwname),
743 "qca/htnv%02x.bin", rom_ver);
746 snprintf(config.fwname, sizeof(config.fwname),
747 "qca/msnv%02x.bin", rom_ver);
750 snprintf(config.fwname, sizeof(config.fwname),
751 "qca/hpnv%02x.bin", rom_ver);
754 snprintf(config.fwname, sizeof(config.fwname),
755 "qca/hmtnv%02x.bin", rom_ver);
759 snprintf(config.fwname, sizeof(config.fwname),
760 "qca/nvm_%08x.bin", soc_ver);
764 err = qca_download_firmware(hdev, &config, soc_type, rom_ver);
766 bt_dev_err(hdev, "QCA Failed to download NVM (%d)", err);
777 err = qca_disable_soc_logging(hdev);
785 /* WCN399x and WCN6750 supports the Microsoft vendor extension with 0xFD70 as the
794 hci_set_msft_opcode(hdev, 0xFD70);
800 /* Perform HCI reset */
801 err = qca_send_reset(hdev);
803 bt_dev_err(hdev, "QCA Failed to run HCI_RESET (%d)", err);
812 /* get fw build info */
813 err = qca_read_fw_build_info(hdev);
821 bt_dev_info(hdev, "QCA setup on UART is completed");
825 EXPORT_SYMBOL_GPL(qca_uart_setup);
827 int qca_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
832 skb = __hci_cmd_sync_ev(hdev, EDL_WRITE_BD_ADDR_OPCODE, 6, bdaddr,
833 HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
836 bt_dev_err(hdev, "QCA Change address cmd failed (%d)", err);
844 EXPORT_SYMBOL_GPL(qca_set_bdaddr);
848 MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION);
849 MODULE_VERSION(VERSION);
850 MODULE_LICENSE("GPL");