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