xref: /linux/drivers/bluetooth/btqca.c (revision 8520a98dbab61e9e340cdfb72dd17ccc8a98961e)
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 
10 #include <net/bluetooth/bluetooth.h>
11 #include <net/bluetooth/hci_core.h>
12 
13 #include "btqca.h"
14 
15 #define VERSION "0.1"
16 
17 int qca_read_soc_version(struct hci_dev *hdev, u32 *soc_version)
18 {
19 	struct sk_buff *skb;
20 	struct edl_event_hdr *edl;
21 	struct rome_version *ver;
22 	char cmd;
23 	int err = 0;
24 
25 	bt_dev_dbg(hdev, "QCA Version Request");
26 
27 	cmd = EDL_PATCH_VER_REQ_CMD;
28 	skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, EDL_PATCH_CMD_LEN,
29 				&cmd, HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
30 	if (IS_ERR(skb)) {
31 		err = PTR_ERR(skb);
32 		bt_dev_err(hdev, "Reading QCA version information failed (%d)",
33 			   err);
34 		return err;
35 	}
36 
37 	if (skb->len != sizeof(*edl) + sizeof(*ver)) {
38 		bt_dev_err(hdev, "QCA Version size mismatch len %d", skb->len);
39 		err = -EILSEQ;
40 		goto out;
41 	}
42 
43 	edl = (struct edl_event_hdr *)(skb->data);
44 	if (!edl) {
45 		bt_dev_err(hdev, "QCA TLV with no header");
46 		err = -EILSEQ;
47 		goto out;
48 	}
49 
50 	if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
51 	    edl->rtype != EDL_APP_VER_RES_EVT) {
52 		bt_dev_err(hdev, "QCA Wrong packet received %d %d", edl->cresp,
53 			   edl->rtype);
54 		err = -EIO;
55 		goto out;
56 	}
57 
58 	ver = (struct rome_version *)(edl->data);
59 
60 	BT_DBG("%s: Product:0x%08x", hdev->name, le32_to_cpu(ver->product_id));
61 	BT_DBG("%s: Patch  :0x%08x", hdev->name, le16_to_cpu(ver->patch_ver));
62 	BT_DBG("%s: ROM    :0x%08x", hdev->name, le16_to_cpu(ver->rome_ver));
63 	BT_DBG("%s: SOC    :0x%08x", hdev->name, le32_to_cpu(ver->soc_id));
64 
65 	/* QCA chipset version can be decided by patch and SoC
66 	 * version, combination with upper 2 bytes from SoC
67 	 * and lower 2 bytes from patch will be used.
68 	 */
69 	*soc_version = (le32_to_cpu(ver->soc_id) << 16) |
70 			(le16_to_cpu(ver->rome_ver) & 0x0000ffff);
71 	if (*soc_version == 0)
72 		err = -EILSEQ;
73 
74 out:
75 	kfree_skb(skb);
76 	if (err)
77 		bt_dev_err(hdev, "QCA Failed to get version (%d)", err);
78 
79 	return err;
80 }
81 EXPORT_SYMBOL_GPL(qca_read_soc_version);
82 
83 static int qca_send_reset(struct hci_dev *hdev)
84 {
85 	struct sk_buff *skb;
86 	int err;
87 
88 	bt_dev_dbg(hdev, "QCA HCI_RESET");
89 
90 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
91 	if (IS_ERR(skb)) {
92 		err = PTR_ERR(skb);
93 		bt_dev_err(hdev, "QCA Reset failed (%d)", err);
94 		return err;
95 	}
96 
97 	kfree_skb(skb);
98 
99 	return 0;
100 }
101 
102 int qca_send_pre_shutdown_cmd(struct hci_dev *hdev)
103 {
104 	struct sk_buff *skb;
105 	int err;
106 
107 	bt_dev_dbg(hdev, "QCA pre shutdown cmd");
108 
109 	skb = __hci_cmd_sync(hdev, QCA_PRE_SHUTDOWN_CMD, 0,
110 				NULL, HCI_INIT_TIMEOUT);
111 	if (IS_ERR(skb)) {
112 		err = PTR_ERR(skb);
113 		bt_dev_err(hdev, "QCA preshutdown_cmd failed (%d)", err);
114 		return err;
115 	}
116 
117 	kfree_skb(skb);
118 
119 	return 0;
120 }
121 EXPORT_SYMBOL_GPL(qca_send_pre_shutdown_cmd);
122 
123 static void qca_tlv_check_data(struct rome_config *config,
124 				const struct firmware *fw)
125 {
126 	const u8 *data;
127 	u32 type_len;
128 	u16 tag_id, tag_len;
129 	int idx, length;
130 	struct tlv_type_hdr *tlv;
131 	struct tlv_type_patch *tlv_patch;
132 	struct tlv_type_nvm *tlv_nvm;
133 
134 	tlv = (struct tlv_type_hdr *)fw->data;
135 
136 	type_len = le32_to_cpu(tlv->type_len);
137 	length = (type_len >> 8) & 0x00ffffff;
138 
139 	BT_DBG("TLV Type\t\t : 0x%x", type_len & 0x000000ff);
140 	BT_DBG("Length\t\t : %d bytes", length);
141 
142 	config->dnld_mode = ROME_SKIP_EVT_NONE;
143 	config->dnld_type = ROME_SKIP_EVT_NONE;
144 
145 	switch (config->type) {
146 	case TLV_TYPE_PATCH:
147 		tlv_patch = (struct tlv_type_patch *)tlv->data;
148 
149 		/* For Rome version 1.1 to 3.1, all segment commands
150 		 * are acked by a vendor specific event (VSE).
151 		 * For Rome >= 3.2, the download mode field indicates
152 		 * if VSE is skipped by the controller.
153 		 * In case VSE is skipped, only the last segment is acked.
154 		 */
155 		config->dnld_mode = tlv_patch->download_mode;
156 		config->dnld_type = config->dnld_mode;
157 
158 		BT_DBG("Total Length           : %d bytes",
159 		       le32_to_cpu(tlv_patch->total_size));
160 		BT_DBG("Patch Data Length      : %d bytes",
161 		       le32_to_cpu(tlv_patch->data_length));
162 		BT_DBG("Signing Format Version : 0x%x",
163 		       tlv_patch->format_version);
164 		BT_DBG("Signature Algorithm    : 0x%x",
165 		       tlv_patch->signature);
166 		BT_DBG("Download mode          : 0x%x",
167 		       tlv_patch->download_mode);
168 		BT_DBG("Reserved               : 0x%x",
169 		       tlv_patch->reserved1);
170 		BT_DBG("Product ID             : 0x%04x",
171 		       le16_to_cpu(tlv_patch->product_id));
172 		BT_DBG("Rom Build Version      : 0x%04x",
173 		       le16_to_cpu(tlv_patch->rom_build));
174 		BT_DBG("Patch Version          : 0x%04x",
175 		       le16_to_cpu(tlv_patch->patch_version));
176 		BT_DBG("Reserved               : 0x%x",
177 		       le16_to_cpu(tlv_patch->reserved2));
178 		BT_DBG("Patch Entry Address    : 0x%x",
179 		       le32_to_cpu(tlv_patch->entry));
180 		break;
181 
182 	case TLV_TYPE_NVM:
183 		idx = 0;
184 		data = tlv->data;
185 		while (idx < length) {
186 			tlv_nvm = (struct tlv_type_nvm *)(data + idx);
187 
188 			tag_id = le16_to_cpu(tlv_nvm->tag_id);
189 			tag_len = le16_to_cpu(tlv_nvm->tag_len);
190 
191 			/* Update NVM tags as needed */
192 			switch (tag_id) {
193 			case EDL_TAG_ID_HCI:
194 				/* HCI transport layer parameters
195 				 * enabling software inband sleep
196 				 * onto controller side.
197 				 */
198 				tlv_nvm->data[0] |= 0x80;
199 
200 				/* UART Baud Rate */
201 				tlv_nvm->data[2] = config->user_baud_rate;
202 
203 				break;
204 
205 			case EDL_TAG_ID_DEEP_SLEEP:
206 				/* Sleep enable mask
207 				 * enabling deep sleep feature on controller.
208 				 */
209 				tlv_nvm->data[0] |= 0x01;
210 
211 				break;
212 			}
213 
214 			idx += (sizeof(u16) + sizeof(u16) + 8 + tag_len);
215 		}
216 		break;
217 
218 	default:
219 		BT_ERR("Unknown TLV type %d", config->type);
220 		break;
221 	}
222 }
223 
224 static int qca_tlv_send_segment(struct hci_dev *hdev, int seg_size,
225 				 const u8 *data, enum rome_tlv_dnld_mode mode)
226 {
227 	struct sk_buff *skb;
228 	struct edl_event_hdr *edl;
229 	struct tlv_seg_resp *tlv_resp;
230 	u8 cmd[MAX_SIZE_PER_TLV_SEGMENT + 2];
231 	int err = 0;
232 
233 	cmd[0] = EDL_PATCH_TLV_REQ_CMD;
234 	cmd[1] = seg_size;
235 	memcpy(cmd + 2, data, seg_size);
236 
237 	if (mode == ROME_SKIP_EVT_VSE_CC || mode == ROME_SKIP_EVT_VSE)
238 		return __hci_cmd_send(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2,
239 				      cmd);
240 
241 	skb = __hci_cmd_sync_ev(hdev, EDL_PATCH_CMD_OPCODE, seg_size + 2, cmd,
242 				HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
243 	if (IS_ERR(skb)) {
244 		err = PTR_ERR(skb);
245 		bt_dev_err(hdev, "QCA Failed to send TLV segment (%d)", err);
246 		return err;
247 	}
248 
249 	if (skb->len != sizeof(*edl) + sizeof(*tlv_resp)) {
250 		bt_dev_err(hdev, "QCA TLV response size mismatch");
251 		err = -EILSEQ;
252 		goto out;
253 	}
254 
255 	edl = (struct edl_event_hdr *)(skb->data);
256 	if (!edl) {
257 		bt_dev_err(hdev, "TLV with no header");
258 		err = -EILSEQ;
259 		goto out;
260 	}
261 
262 	tlv_resp = (struct tlv_seg_resp *)(edl->data);
263 
264 	if (edl->cresp != EDL_CMD_REQ_RES_EVT ||
265 	    edl->rtype != EDL_TVL_DNLD_RES_EVT || tlv_resp->result != 0x00) {
266 		bt_dev_err(hdev, "QCA TLV with error stat 0x%x rtype 0x%x (0x%x)",
267 			   edl->cresp, edl->rtype, tlv_resp->result);
268 		err = -EIO;
269 	}
270 
271 out:
272 	kfree_skb(skb);
273 
274 	return err;
275 }
276 
277 static int qca_inject_cmd_complete_event(struct hci_dev *hdev)
278 {
279 	struct hci_event_hdr *hdr;
280 	struct hci_ev_cmd_complete *evt;
281 	struct sk_buff *skb;
282 
283 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
284 	if (!skb)
285 		return -ENOMEM;
286 
287 	hdr = skb_put(skb, sizeof(*hdr));
288 	hdr->evt = HCI_EV_CMD_COMPLETE;
289 	hdr->plen = sizeof(*evt) + 1;
290 
291 	evt = skb_put(skb, sizeof(*evt));
292 	evt->ncmd = 1;
293 	evt->opcode = cpu_to_le16(QCA_HCI_CC_OPCODE);
294 
295 	skb_put_u8(skb, QCA_HCI_CC_SUCCESS);
296 
297 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
298 
299 	return hci_recv_frame(hdev, skb);
300 }
301 
302 static int qca_download_firmware(struct hci_dev *hdev,
303 				  struct rome_config *config)
304 {
305 	const struct firmware *fw;
306 	const u8 *segment;
307 	int ret, remain, i = 0;
308 
309 	bt_dev_info(hdev, "QCA Downloading %s", config->fwname);
310 
311 	ret = request_firmware(&fw, config->fwname, &hdev->dev);
312 	if (ret) {
313 		bt_dev_err(hdev, "QCA Failed to request file: %s (%d)",
314 			   config->fwname, ret);
315 		return ret;
316 	}
317 
318 	qca_tlv_check_data(config, fw);
319 
320 	segment = fw->data;
321 	remain = fw->size;
322 	while (remain > 0) {
323 		int segsize = min(MAX_SIZE_PER_TLV_SEGMENT, remain);
324 
325 		bt_dev_dbg(hdev, "Send segment %d, size %d", i++, segsize);
326 
327 		remain -= segsize;
328 		/* The last segment is always acked regardless download mode */
329 		if (!remain || segsize < MAX_SIZE_PER_TLV_SEGMENT)
330 			config->dnld_mode = ROME_SKIP_EVT_NONE;
331 
332 		ret = qca_tlv_send_segment(hdev, segsize, segment,
333 					    config->dnld_mode);
334 		if (ret)
335 			goto out;
336 
337 		segment += segsize;
338 	}
339 
340 	/* Latest qualcomm chipsets are not sending a command complete event
341 	 * for every fw packet sent. They only respond with a vendor specific
342 	 * event for the last packet. This optimization in the chip will
343 	 * decrease the BT in initialization time. Here we will inject a command
344 	 * complete event to avoid a command timeout error message.
345 	 */
346 	if (config->dnld_type == ROME_SKIP_EVT_VSE_CC ||
347 	    config->dnld_type == ROME_SKIP_EVT_VSE)
348 		ret = qca_inject_cmd_complete_event(hdev);
349 
350 out:
351 	release_firmware(fw);
352 
353 	return ret;
354 }
355 
356 int qca_set_bdaddr_rome(struct hci_dev *hdev, const bdaddr_t *bdaddr)
357 {
358 	struct sk_buff *skb;
359 	u8 cmd[9];
360 	int err;
361 
362 	cmd[0] = EDL_NVM_ACCESS_SET_REQ_CMD;
363 	cmd[1] = 0x02; 			/* TAG ID */
364 	cmd[2] = sizeof(bdaddr_t);	/* size */
365 	memcpy(cmd + 3, bdaddr, sizeof(bdaddr_t));
366 	skb = __hci_cmd_sync_ev(hdev, EDL_NVM_ACCESS_OPCODE, sizeof(cmd), cmd,
367 				HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
368 	if (IS_ERR(skb)) {
369 		err = PTR_ERR(skb);
370 		bt_dev_err(hdev, "QCA Change address command failed (%d)", err);
371 		return err;
372 	}
373 
374 	kfree_skb(skb);
375 
376 	return 0;
377 }
378 EXPORT_SYMBOL_GPL(qca_set_bdaddr_rome);
379 
380 int qca_uart_setup(struct hci_dev *hdev, uint8_t baudrate,
381 		   enum qca_btsoc_type soc_type, u32 soc_ver,
382 		   const char *firmware_name)
383 {
384 	struct rome_config config;
385 	int err;
386 	u8 rom_ver = 0;
387 
388 	bt_dev_dbg(hdev, "QCA setup on UART");
389 
390 	config.user_baud_rate = baudrate;
391 
392 	/* Download rampatch file */
393 	config.type = TLV_TYPE_PATCH;
394 	if (qca_is_wcn399x(soc_type)) {
395 		/* Firmware files to download are based on ROM version.
396 		 * ROM version is derived from last two bytes of soc_ver.
397 		 */
398 		rom_ver = ((soc_ver & 0x00000f00) >> 0x04) |
399 			    (soc_ver & 0x0000000f);
400 		snprintf(config.fwname, sizeof(config.fwname),
401 			 "qca/crbtfw%02x.tlv", rom_ver);
402 	} else {
403 		snprintf(config.fwname, sizeof(config.fwname),
404 			 "qca/rampatch_%08x.bin", soc_ver);
405 	}
406 
407 	err = qca_download_firmware(hdev, &config);
408 	if (err < 0) {
409 		bt_dev_err(hdev, "QCA Failed to download patch (%d)", err);
410 		return err;
411 	}
412 
413 	/* Give the controller some time to get ready to receive the NVM */
414 	msleep(10);
415 
416 	/* Download NVM configuration */
417 	config.type = TLV_TYPE_NVM;
418 	if (firmware_name)
419 		snprintf(config.fwname, sizeof(config.fwname),
420 			 "qca/%s", firmware_name);
421 	else if (qca_is_wcn399x(soc_type))
422 		snprintf(config.fwname, sizeof(config.fwname),
423 			 "qca/crnv%02x.bin", rom_ver);
424 	else
425 		snprintf(config.fwname, sizeof(config.fwname),
426 			 "qca/nvm_%08x.bin", soc_ver);
427 
428 	err = qca_download_firmware(hdev, &config);
429 	if (err < 0) {
430 		bt_dev_err(hdev, "QCA Failed to download NVM (%d)", err);
431 		return err;
432 	}
433 
434 	/* Perform HCI reset */
435 	err = qca_send_reset(hdev);
436 	if (err < 0) {
437 		bt_dev_err(hdev, "QCA Failed to run HCI_RESET (%d)", err);
438 		return err;
439 	}
440 
441 	bt_dev_info(hdev, "QCA setup on UART is completed");
442 
443 	return 0;
444 }
445 EXPORT_SYMBOL_GPL(qca_uart_setup);
446 
447 int qca_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
448 {
449 	struct sk_buff *skb;
450 	int err;
451 
452 	skb = __hci_cmd_sync_ev(hdev, EDL_WRITE_BD_ADDR_OPCODE, 6, bdaddr,
453 				HCI_EV_VENDOR, HCI_INIT_TIMEOUT);
454 	if (IS_ERR(skb)) {
455 		err = PTR_ERR(skb);
456 		bt_dev_err(hdev, "QCA Change address cmd failed (%d)", err);
457 		return err;
458 	}
459 
460 	kfree_skb(skb);
461 
462 	return 0;
463 }
464 EXPORT_SYMBOL_GPL(qca_set_bdaddr);
465 
466 
467 MODULE_AUTHOR("Ben Young Tae Kim <ytkim@qca.qualcomm.com>");
468 MODULE_DESCRIPTION("Bluetooth support for Qualcomm Atheros family ver " VERSION);
469 MODULE_VERSION(VERSION);
470 MODULE_LICENSE("GPL");
471