xref: /openbmc/linux/drivers/bluetooth/btbcm.c (revision eb3fcf00)
1 /*
2  *
3  *  Bluetooth support for Broadcom devices
4  *
5  *  Copyright (C) 2015  Intel Corporation
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <asm/unaligned.h>
27 
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 
31 #include "btbcm.h"
32 
33 #define VERSION "0.1"
34 
35 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
36 #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
37 #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
38 
39 int btbcm_check_bdaddr(struct hci_dev *hdev)
40 {
41 	struct hci_rp_read_bd_addr *bda;
42 	struct sk_buff *skb;
43 
44 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
45 			     HCI_INIT_TIMEOUT);
46 	if (IS_ERR(skb)) {
47 		int err = PTR_ERR(skb);
48 		BT_ERR("%s: BCM: Reading device address failed (%d)",
49 		       hdev->name, err);
50 		return err;
51 	}
52 
53 	if (skb->len != sizeof(*bda)) {
54 		BT_ERR("%s: BCM: Device address length mismatch", hdev->name);
55 		kfree_skb(skb);
56 		return -EIO;
57 	}
58 
59 	bda = (struct hci_rp_read_bd_addr *)skb->data;
60 
61 	/* Check if the address indicates a controller with either an
62 	 * invalid or default address. In both cases the device needs
63 	 * to be marked as not having a valid address.
64 	 *
65 	 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
66 	 * with no configured address.
67 	 *
68 	 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
69 	 * with waiting for configuration state.
70 	 *
71 	 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
72 	 * with waiting for configuration state.
73 	 */
74 	if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
75 	    !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
76 	    !bacmp(&bda->bdaddr, BDADDR_BCM4330B1)) {
77 		BT_INFO("%s: BCM: Using default device address (%pMR)",
78 			hdev->name, &bda->bdaddr);
79 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
80 	}
81 
82 	kfree_skb(skb);
83 
84 	return 0;
85 }
86 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
87 
88 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
89 {
90 	struct sk_buff *skb;
91 	int err;
92 
93 	skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
94 	if (IS_ERR(skb)) {
95 		err = PTR_ERR(skb);
96 		BT_ERR("%s: BCM: Change address command failed (%d)",
97 		       hdev->name, err);
98 		return err;
99 	}
100 	kfree_skb(skb);
101 
102 	return 0;
103 }
104 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
105 
106 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
107 {
108 	const struct hci_command_hdr *cmd;
109 	const u8 *fw_ptr;
110 	size_t fw_size;
111 	struct sk_buff *skb;
112 	u16 opcode;
113 	int err = 0;
114 
115 	/* Start Download */
116 	skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
117 	if (IS_ERR(skb)) {
118 		err = PTR_ERR(skb);
119 		BT_ERR("%s: BCM: Download Minidrv command failed (%d)",
120 		       hdev->name, err);
121 		goto done;
122 	}
123 	kfree_skb(skb);
124 
125 	/* 50 msec delay after Download Minidrv completes */
126 	msleep(50);
127 
128 	fw_ptr = fw->data;
129 	fw_size = fw->size;
130 
131 	while (fw_size >= sizeof(*cmd)) {
132 		const u8 *cmd_param;
133 
134 		cmd = (struct hci_command_hdr *)fw_ptr;
135 		fw_ptr += sizeof(*cmd);
136 		fw_size -= sizeof(*cmd);
137 
138 		if (fw_size < cmd->plen) {
139 			BT_ERR("%s: BCM: Patch is corrupted", hdev->name);
140 			err = -EINVAL;
141 			goto done;
142 		}
143 
144 		cmd_param = fw_ptr;
145 		fw_ptr += cmd->plen;
146 		fw_size -= cmd->plen;
147 
148 		opcode = le16_to_cpu(cmd->opcode);
149 
150 		skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
151 				     HCI_INIT_TIMEOUT);
152 		if (IS_ERR(skb)) {
153 			err = PTR_ERR(skb);
154 			BT_ERR("%s: BCM: Patch command %04x failed (%d)",
155 			       hdev->name, opcode, err);
156 			goto done;
157 		}
158 		kfree_skb(skb);
159 	}
160 
161 	/* 250 msec delay after Launch Ram completes */
162 	msleep(250);
163 
164 done:
165 	return err;
166 }
167 EXPORT_SYMBOL(btbcm_patchram);
168 
169 static int btbcm_reset(struct hci_dev *hdev)
170 {
171 	struct sk_buff *skb;
172 
173 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
174 	if (IS_ERR(skb)) {
175 		int err = PTR_ERR(skb);
176 		BT_ERR("%s: BCM: Reset failed (%d)", hdev->name, err);
177 		return err;
178 	}
179 	kfree_skb(skb);
180 
181 	return 0;
182 }
183 
184 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
185 {
186 	struct sk_buff *skb;
187 
188 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
189 			     HCI_INIT_TIMEOUT);
190 	if (IS_ERR(skb)) {
191 		BT_ERR("%s: BCM: Reading local version info failed (%ld)",
192 		       hdev->name, PTR_ERR(skb));
193 		return skb;
194 	}
195 
196 	if (skb->len != sizeof(struct hci_rp_read_local_version)) {
197 		BT_ERR("%s: BCM: Local version length mismatch", hdev->name);
198 		kfree_skb(skb);
199 		return ERR_PTR(-EIO);
200 	}
201 
202 	return skb;
203 }
204 
205 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
206 {
207 	struct sk_buff *skb;
208 
209 	skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
210 	if (IS_ERR(skb)) {
211 		BT_ERR("%s: BCM: Read verbose config info failed (%ld)",
212 		       hdev->name, PTR_ERR(skb));
213 		return skb;
214 	}
215 
216 	if (skb->len != 7) {
217 		BT_ERR("%s: BCM: Verbose config length mismatch", hdev->name);
218 		kfree_skb(skb);
219 		return ERR_PTR(-EIO);
220 	}
221 
222 	return skb;
223 }
224 
225 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
226 {
227 	struct sk_buff *skb;
228 
229 	skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
230 	if (IS_ERR(skb)) {
231 		BT_ERR("%s: BCM: Read USB product info failed (%ld)",
232 		       hdev->name, PTR_ERR(skb));
233 		return skb;
234 	}
235 
236 	if (skb->len != 5) {
237 		BT_ERR("%s: BCM: USB product length mismatch", hdev->name);
238 		kfree_skb(skb);
239 		return ERR_PTR(-EIO);
240 	}
241 
242 	return skb;
243 }
244 
245 static const struct {
246 	u16 subver;
247 	const char *name;
248 } bcm_uart_subver_table[] = {
249 	{ 0x4103, "BCM4330B1"	},	/* 002.001.003 */
250 	{ 0x410e, "BCM43341B0"	},	/* 002.001.014 */
251 	{ 0x4406, "BCM4324B3"	},	/* 002.004.006 */
252 	{ 0x610c, "BCM4354"	},	/* 003.001.012 */
253 	{ }
254 };
255 
256 int btbcm_initialize(struct hci_dev *hdev, char *fw_name, size_t len)
257 {
258 	u16 subver, rev;
259 	const char *hw_name = NULL;
260 	struct sk_buff *skb;
261 	struct hci_rp_read_local_version *ver;
262 	int i, err;
263 
264 	/* Reset */
265 	err = btbcm_reset(hdev);
266 	if (err)
267 		return err;
268 
269 	/* Read Local Version Info */
270 	skb = btbcm_read_local_version(hdev);
271 	if (IS_ERR(skb))
272 		return PTR_ERR(skb);
273 
274 	ver = (struct hci_rp_read_local_version *)skb->data;
275 	rev = le16_to_cpu(ver->hci_rev);
276 	subver = le16_to_cpu(ver->lmp_subver);
277 	kfree_skb(skb);
278 
279 	/* Read Verbose Config Version Info */
280 	skb = btbcm_read_verbose_config(hdev);
281 	if (IS_ERR(skb))
282 		return PTR_ERR(skb);
283 
284 	BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
285 	kfree_skb(skb);
286 
287 	switch ((rev & 0xf000) >> 12) {
288 	case 0:
289 	case 1:
290 	case 3:
291 		for (i = 0; bcm_uart_subver_table[i].name; i++) {
292 			if (subver == bcm_uart_subver_table[i].subver) {
293 				hw_name = bcm_uart_subver_table[i].name;
294 				break;
295 			}
296 		}
297 
298 		snprintf(fw_name, len, "brcm/%s.hcd", hw_name ? : "BCM");
299 		break;
300 	default:
301 		return 0;
302 	}
303 
304 	BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
305 		hw_name ? : "BCM", (subver & 0x7000) >> 13,
306 		(subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
307 
308 	return 0;
309 }
310 EXPORT_SYMBOL_GPL(btbcm_initialize);
311 
312 int btbcm_finalize(struct hci_dev *hdev)
313 {
314 	struct sk_buff *skb;
315 	struct hci_rp_read_local_version *ver;
316 	u16 subver, rev;
317 	int err;
318 
319 	/* Reset */
320 	err = btbcm_reset(hdev);
321 	if (err)
322 		return err;
323 
324 	/* Read Local Version Info */
325 	skb = btbcm_read_local_version(hdev);
326 	if (IS_ERR(skb))
327 		return PTR_ERR(skb);
328 
329 	ver = (struct hci_rp_read_local_version *)skb->data;
330 	rev = le16_to_cpu(ver->hci_rev);
331 	subver = le16_to_cpu(ver->lmp_subver);
332 	kfree_skb(skb);
333 
334 	BT_INFO("%s: BCM (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
335 		(subver & 0x7000) >> 13, (subver & 0x1f00) >> 8,
336 		(subver & 0x00ff), rev & 0x0fff);
337 
338 	btbcm_check_bdaddr(hdev);
339 
340 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
341 
342 	return 0;
343 }
344 EXPORT_SYMBOL_GPL(btbcm_finalize);
345 
346 static const struct {
347 	u16 subver;
348 	const char *name;
349 } bcm_usb_subver_table[] = {
350 	{ 0x210b, "BCM43142A0"	},	/* 001.001.011 */
351 	{ 0x2112, "BCM4314A0"	},	/* 001.001.018 */
352 	{ 0x2118, "BCM20702A0"	},	/* 001.001.024 */
353 	{ 0x2126, "BCM4335A0"	},	/* 001.001.038 */
354 	{ 0x220e, "BCM20702A1"	},	/* 001.002.014 */
355 	{ 0x230f, "BCM4354A2"	},	/* 001.003.015 */
356 	{ 0x4106, "BCM4335B0"	},	/* 002.001.006 */
357 	{ 0x410e, "BCM20702B0"	},	/* 002.001.014 */
358 	{ 0x6109, "BCM4335C0"	},	/* 003.001.009 */
359 	{ 0x610c, "BCM4354"	},	/* 003.001.012 */
360 	{ }
361 };
362 
363 int btbcm_setup_patchram(struct hci_dev *hdev)
364 {
365 	char fw_name[64];
366 	const struct firmware *fw;
367 	u16 subver, rev, pid, vid;
368 	const char *hw_name = NULL;
369 	struct sk_buff *skb;
370 	struct hci_rp_read_local_version *ver;
371 	int i, err;
372 
373 	/* Reset */
374 	err = btbcm_reset(hdev);
375 	if (err)
376 		return err;
377 
378 	/* Read Local Version Info */
379 	skb = btbcm_read_local_version(hdev);
380 	if (IS_ERR(skb))
381 		return PTR_ERR(skb);
382 
383 	ver = (struct hci_rp_read_local_version *)skb->data;
384 	rev = le16_to_cpu(ver->hci_rev);
385 	subver = le16_to_cpu(ver->lmp_subver);
386 	kfree_skb(skb);
387 
388 	/* Read Verbose Config Version Info */
389 	skb = btbcm_read_verbose_config(hdev);
390 	if (IS_ERR(skb))
391 		return PTR_ERR(skb);
392 
393 	BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
394 	kfree_skb(skb);
395 
396 	switch ((rev & 0xf000) >> 12) {
397 	case 0:
398 	case 3:
399 		for (i = 0; bcm_uart_subver_table[i].name; i++) {
400 			if (subver == bcm_uart_subver_table[i].subver) {
401 				hw_name = bcm_uart_subver_table[i].name;
402 				break;
403 			}
404 		}
405 
406 		snprintf(fw_name, sizeof(fw_name), "brcm/%s.hcd",
407 			 hw_name ? : "BCM");
408 		break;
409 	case 1:
410 	case 2:
411 		/* Read USB Product Info */
412 		skb = btbcm_read_usb_product(hdev);
413 		if (IS_ERR(skb))
414 			return PTR_ERR(skb);
415 
416 		vid = get_unaligned_le16(skb->data + 1);
417 		pid = get_unaligned_le16(skb->data + 3);
418 		kfree_skb(skb);
419 
420 		for (i = 0; bcm_usb_subver_table[i].name; i++) {
421 			if (subver == bcm_usb_subver_table[i].subver) {
422 				hw_name = bcm_usb_subver_table[i].name;
423 				break;
424 			}
425 		}
426 
427 		snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
428 			 hw_name ? : "BCM", vid, pid);
429 		break;
430 	default:
431 		return 0;
432 	}
433 
434 	BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
435 		hw_name ? : "BCM", (subver & 0x7000) >> 13,
436 		(subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
437 
438 	err = request_firmware(&fw, fw_name, &hdev->dev);
439 	if (err < 0) {
440 		BT_INFO("%s: BCM: Patch %s not found", hdev->name, fw_name);
441 		return 0;
442 	}
443 
444 	btbcm_patchram(hdev, fw);
445 
446 	release_firmware(fw);
447 
448 	/* Reset */
449 	err = btbcm_reset(hdev);
450 	if (err)
451 		return err;
452 
453 	/* Read Local Version Info */
454 	skb = btbcm_read_local_version(hdev);
455 	if (IS_ERR(skb))
456 		return PTR_ERR(skb);
457 
458 	ver = (struct hci_rp_read_local_version *)skb->data;
459 	rev = le16_to_cpu(ver->hci_rev);
460 	subver = le16_to_cpu(ver->lmp_subver);
461 	kfree_skb(skb);
462 
463 	BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
464 		hw_name ? : "BCM", (subver & 0x7000) >> 13,
465 		(subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
466 
467 	btbcm_check_bdaddr(hdev);
468 
469 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
470 
471 	return 0;
472 }
473 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
474 
475 int btbcm_setup_apple(struct hci_dev *hdev)
476 {
477 	struct sk_buff *skb;
478 
479 	/* Read Verbose Config Version Info */
480 	skb = btbcm_read_verbose_config(hdev);
481 	if (!IS_ERR(skb)) {
482 		BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1],
483 			get_unaligned_le16(skb->data + 5));
484 		kfree_skb(skb);
485 	}
486 
487 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
488 
489 	return 0;
490 }
491 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
492 
493 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
494 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
495 MODULE_VERSION(VERSION);
496 MODULE_LICENSE("GPL");
497