xref: /openbmc/linux/drivers/bluetooth/btbcm.c (revision 2a598d0b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth support for Broadcom devices
5  *
6  *  Copyright (C) 2015  Intel Corporation
7  */
8 
9 #include <linux/efi.h>
10 #include <linux/module.h>
11 #include <linux/firmware.h>
12 #include <linux/dmi.h>
13 #include <linux/of.h>
14 #include <asm/unaligned.h>
15 
16 #include <net/bluetooth/bluetooth.h>
17 #include <net/bluetooth/hci_core.h>
18 
19 #include "btbcm.h"
20 
21 #define VERSION "0.1"
22 
23 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
24 #define BDADDR_BCM20702A1 (&(bdaddr_t) {{0x00, 0x00, 0xa0, 0x02, 0x70, 0x20}})
25 #define BDADDR_BCM2076B1 (&(bdaddr_t) {{0x79, 0x56, 0x00, 0xa0, 0x76, 0x20}})
26 #define BDADDR_BCM43430A0 (&(bdaddr_t) {{0xac, 0x1f, 0x12, 0xa0, 0x43, 0x43}})
27 #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
28 #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
29 #define BDADDR_BCM4334B0 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb0, 0x34, 0x43}})
30 #define BDADDR_BCM4345C5 (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0xc5, 0x45, 0x43}})
31 #define BDADDR_BCM43341B (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0x1b, 0x34, 0x43}})
32 
33 #define BCM_FW_NAME_LEN			64
34 #define BCM_FW_NAME_COUNT_MAX		4
35 /* For kmalloc-ing the fw-name array instead of putting it on the stack */
36 typedef char bcm_fw_name[BCM_FW_NAME_LEN];
37 
38 #ifdef CONFIG_EFI
39 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
40 {
41 	efi_guid_t guid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61, 0xb5, 0x1f,
42 				   0x43, 0x26, 0x81, 0x23, 0xd1, 0x13);
43 	bdaddr_t efi_bdaddr, bdaddr;
44 	efi_status_t status;
45 	unsigned long len;
46 	int ret;
47 
48 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
49 		return -EOPNOTSUPP;
50 
51 	len = sizeof(efi_bdaddr);
52 	status = efi.get_variable(L"BDADDR", &guid, NULL, &len, &efi_bdaddr);
53 	if (status != EFI_SUCCESS)
54 		return -ENXIO;
55 
56 	if (len != sizeof(efi_bdaddr))
57 		return -EIO;
58 
59 	baswap(&bdaddr, &efi_bdaddr);
60 
61 	ret = btbcm_set_bdaddr(hdev, &bdaddr);
62 	if (ret)
63 		return ret;
64 
65 	bt_dev_info(hdev, "BCM: Using EFI device address (%pMR)", &bdaddr);
66 	return 0;
67 }
68 #else
69 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
70 {
71 	return -EOPNOTSUPP;
72 }
73 #endif
74 
75 int btbcm_check_bdaddr(struct hci_dev *hdev)
76 {
77 	struct hci_rp_read_bd_addr *bda;
78 	struct sk_buff *skb;
79 
80 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
81 			     HCI_INIT_TIMEOUT);
82 	if (IS_ERR(skb)) {
83 		int err = PTR_ERR(skb);
84 
85 		bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
86 		return err;
87 	}
88 
89 	if (skb->len != sizeof(*bda)) {
90 		bt_dev_err(hdev, "BCM: Device address length mismatch");
91 		kfree_skb(skb);
92 		return -EIO;
93 	}
94 
95 	bda = (struct hci_rp_read_bd_addr *)skb->data;
96 
97 	/* Check if the address indicates a controller with either an
98 	 * invalid or default address. In both cases the device needs
99 	 * to be marked as not having a valid address.
100 	 *
101 	 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
102 	 * with no configured address.
103 	 *
104 	 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
105 	 * with no configured address.
106 	 *
107 	 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
108 	 * with no configured address.
109 	 *
110 	 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
111 	 * with waiting for configuration state.
112 	 *
113 	 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
114 	 * with waiting for configuration state.
115 	 *
116 	 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
117 	 * with no configured address.
118 	 */
119 	if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
120 	    !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
121 	    !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
122 	    !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
123 	    !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
124 	    !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
125 	    !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
126 	    !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
127 	    !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
128 		/* Try falling back to BDADDR EFI variable */
129 		if (btbcm_set_bdaddr_from_efi(hdev) != 0) {
130 			bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
131 				    &bda->bdaddr);
132 			set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
133 		}
134 	}
135 
136 	kfree_skb(skb);
137 
138 	return 0;
139 }
140 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
141 
142 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
143 {
144 	struct sk_buff *skb;
145 	int err;
146 
147 	skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
148 	if (IS_ERR(skb)) {
149 		err = PTR_ERR(skb);
150 		bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
151 		return err;
152 	}
153 	kfree_skb(skb);
154 
155 	return 0;
156 }
157 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
158 
159 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
160 			      struct bcm_set_pcm_int_params *params)
161 {
162 	struct sk_buff *skb;
163 	int err = 0;
164 
165 	skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
166 	if (IS_ERR(skb)) {
167 		err = PTR_ERR(skb);
168 		bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
169 		return err;
170 	}
171 
172 	if (skb->len != 6 || skb->data[0]) {
173 		bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
174 		kfree_skb(skb);
175 		return -EIO;
176 	}
177 
178 	if (params)
179 		memcpy(params, skb->data + 1, 5);
180 
181 	kfree_skb(skb);
182 
183 	return 0;
184 }
185 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
186 
187 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
188 			       const struct bcm_set_pcm_int_params *params)
189 {
190 	struct sk_buff *skb;
191 	int err;
192 
193 	skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
194 	if (IS_ERR(skb)) {
195 		err = PTR_ERR(skb);
196 		bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
197 		return err;
198 	}
199 	kfree_skb(skb);
200 
201 	return 0;
202 }
203 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
204 
205 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
206 {
207 	const struct hci_command_hdr *cmd;
208 	const u8 *fw_ptr;
209 	size_t fw_size;
210 	struct sk_buff *skb;
211 	u16 opcode;
212 	int err = 0;
213 
214 	/* Start Download */
215 	skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
216 	if (IS_ERR(skb)) {
217 		err = PTR_ERR(skb);
218 		bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
219 			   err);
220 		goto done;
221 	}
222 	kfree_skb(skb);
223 
224 	/* 50 msec delay after Download Minidrv completes */
225 	msleep(50);
226 
227 	fw_ptr = fw->data;
228 	fw_size = fw->size;
229 
230 	while (fw_size >= sizeof(*cmd)) {
231 		const u8 *cmd_param;
232 
233 		cmd = (struct hci_command_hdr *)fw_ptr;
234 		fw_ptr += sizeof(*cmd);
235 		fw_size -= sizeof(*cmd);
236 
237 		if (fw_size < cmd->plen) {
238 			bt_dev_err(hdev, "BCM: Patch is corrupted");
239 			err = -EINVAL;
240 			goto done;
241 		}
242 
243 		cmd_param = fw_ptr;
244 		fw_ptr += cmd->plen;
245 		fw_size -= cmd->plen;
246 
247 		opcode = le16_to_cpu(cmd->opcode);
248 
249 		skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
250 				     HCI_INIT_TIMEOUT);
251 		if (IS_ERR(skb)) {
252 			err = PTR_ERR(skb);
253 			bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
254 				   opcode, err);
255 			goto done;
256 		}
257 		kfree_skb(skb);
258 	}
259 
260 	/* 250 msec delay after Launch Ram completes */
261 	msleep(250);
262 
263 done:
264 	return err;
265 }
266 EXPORT_SYMBOL(btbcm_patchram);
267 
268 static int btbcm_reset(struct hci_dev *hdev)
269 {
270 	struct sk_buff *skb;
271 
272 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
273 	if (IS_ERR(skb)) {
274 		int err = PTR_ERR(skb);
275 
276 		bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
277 		return err;
278 	}
279 	kfree_skb(skb);
280 
281 	/* 100 msec delay for module to complete reset process */
282 	msleep(100);
283 
284 	return 0;
285 }
286 
287 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
288 {
289 	struct sk_buff *skb;
290 
291 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
292 			     HCI_INIT_TIMEOUT);
293 	if (IS_ERR(skb)) {
294 		bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
295 			   PTR_ERR(skb));
296 		return skb;
297 	}
298 
299 	if (skb->len != sizeof(struct hci_rp_read_local_name)) {
300 		bt_dev_err(hdev, "BCM: Local name length mismatch");
301 		kfree_skb(skb);
302 		return ERR_PTR(-EIO);
303 	}
304 
305 	return skb;
306 }
307 
308 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
309 {
310 	struct sk_buff *skb;
311 
312 	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
313 			     HCI_INIT_TIMEOUT);
314 	if (IS_ERR(skb)) {
315 		bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
316 			   PTR_ERR(skb));
317 		return skb;
318 	}
319 
320 	if (skb->len != sizeof(struct hci_rp_read_local_version)) {
321 		bt_dev_err(hdev, "BCM: Local version length mismatch");
322 		kfree_skb(skb);
323 		return ERR_PTR(-EIO);
324 	}
325 
326 	return skb;
327 }
328 
329 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
330 {
331 	struct sk_buff *skb;
332 
333 	skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
334 	if (IS_ERR(skb)) {
335 		bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
336 			   PTR_ERR(skb));
337 		return skb;
338 	}
339 
340 	if (skb->len != 7) {
341 		bt_dev_err(hdev, "BCM: Verbose config length mismatch");
342 		kfree_skb(skb);
343 		return ERR_PTR(-EIO);
344 	}
345 
346 	return skb;
347 }
348 
349 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
350 {
351 	struct sk_buff *skb;
352 
353 	skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
354 	if (IS_ERR(skb)) {
355 		bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
356 			   PTR_ERR(skb));
357 		return skb;
358 	}
359 
360 	if (skb->len != 9) {
361 		bt_dev_err(hdev, "BCM: Controller features length mismatch");
362 		kfree_skb(skb);
363 		return ERR_PTR(-EIO);
364 	}
365 
366 	return skb;
367 }
368 
369 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
370 {
371 	struct sk_buff *skb;
372 
373 	skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
374 	if (IS_ERR(skb)) {
375 		bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
376 			   PTR_ERR(skb));
377 		return skb;
378 	}
379 
380 	if (skb->len != 5) {
381 		bt_dev_err(hdev, "BCM: USB product length mismatch");
382 		kfree_skb(skb);
383 		return ERR_PTR(-EIO);
384 	}
385 
386 	return skb;
387 }
388 
389 static const struct dmi_system_id disable_broken_read_transmit_power[] = {
390 	{
391 		 .matches = {
392 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
393 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,1"),
394 		},
395 	},
396 	{
397 		 .matches = {
398 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
399 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
400 		},
401 	},
402 	{
403 		 .matches = {
404 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
405 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,4"),
406 		},
407 	},
408 	{
409 		 .matches = {
410 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
411 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,1"),
412 		},
413 	},
414 	{
415 		 .matches = {
416 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
417 			DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,2"),
418 		},
419 	},
420 	{
421 		 .matches = {
422 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
423 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,1"),
424 		},
425 	},
426 	{
427 		 .matches = {
428 			DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
429 			DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,2"),
430 		},
431 	},
432 	{ }
433 };
434 
435 static int btbcm_read_info(struct hci_dev *hdev)
436 {
437 	struct sk_buff *skb;
438 
439 	/* Read Verbose Config Version Info */
440 	skb = btbcm_read_verbose_config(hdev);
441 	if (IS_ERR(skb))
442 		return PTR_ERR(skb);
443 
444 	bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
445 	kfree_skb(skb);
446 
447 	return 0;
448 }
449 
450 static int btbcm_print_controller_features(struct hci_dev *hdev)
451 {
452 	struct sk_buff *skb;
453 
454 	/* Read Controller Features */
455 	skb = btbcm_read_controller_features(hdev);
456 	if (IS_ERR(skb))
457 		return PTR_ERR(skb);
458 
459 	bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
460 	kfree_skb(skb);
461 
462 	/* Read DMI and disable broken Read LE Min/Max Tx Power */
463 	if (dmi_first_match(disable_broken_read_transmit_power))
464 		set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
465 
466 	return 0;
467 }
468 
469 static int btbcm_print_local_name(struct hci_dev *hdev)
470 {
471 	struct sk_buff *skb;
472 
473 	/* Read Local Name */
474 	skb = btbcm_read_local_name(hdev);
475 	if (IS_ERR(skb))
476 		return PTR_ERR(skb);
477 
478 	bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
479 	kfree_skb(skb);
480 
481 	return 0;
482 }
483 
484 struct bcm_subver_table {
485 	u16 subver;
486 	const char *name;
487 };
488 
489 static const struct bcm_subver_table bcm_uart_subver_table[] = {
490 	{ 0x1111, "BCM4362A2"	},	/* 000.017.017 */
491 	{ 0x4103, "BCM4330B1"	},	/* 002.001.003 */
492 	{ 0x410d, "BCM4334B0"	},	/* 002.001.013 */
493 	{ 0x410e, "BCM43341B0"	},	/* 002.001.014 */
494 	{ 0x4204, "BCM2076B1"	},	/* 002.002.004 */
495 	{ 0x4406, "BCM4324B3"	},	/* 002.004.006 */
496 	{ 0x4606, "BCM4324B5"	},	/* 002.006.006 */
497 	{ 0x6109, "BCM4335C0"	},	/* 003.001.009 */
498 	{ 0x610c, "BCM4354"	},	/* 003.001.012 */
499 	{ 0x2122, "BCM4343A0"	},	/* 001.001.034 */
500 	{ 0x2209, "BCM43430A1"  },	/* 001.002.009 */
501 	{ 0x6119, "BCM4345C0"	},	/* 003.001.025 */
502 	{ 0x6606, "BCM4345C5"	},	/* 003.006.006 */
503 	{ 0x230f, "BCM4356A2"	},	/* 001.003.015 */
504 	{ 0x220e, "BCM20702A1"  },	/* 001.002.014 */
505 	{ 0x420d, "BCM4349B1"	},	/* 002.002.013 */
506 	{ 0x420e, "BCM4349B1"	},	/* 002.002.014 */
507 	{ 0x4217, "BCM4329B1"   },	/* 002.002.023 */
508 	{ 0x6106, "BCM4359C0"	},	/* 003.001.006 */
509 	{ 0x4106, "BCM4335A0"	},	/* 002.001.006 */
510 	{ 0x410c, "BCM43430B0"	},	/* 002.001.012 */
511 	{ 0x2119, "BCM4373A0"	},	/* 001.001.025 */
512 	{ }
513 };
514 
515 static const struct bcm_subver_table bcm_usb_subver_table[] = {
516 	{ 0x2105, "BCM20703A1"	},	/* 001.001.005 */
517 	{ 0x210b, "BCM43142A0"	},	/* 001.001.011 */
518 	{ 0x2112, "BCM4314A0"	},	/* 001.001.018 */
519 	{ 0x2118, "BCM20702A0"	},	/* 001.001.024 */
520 	{ 0x2126, "BCM4335A0"	},	/* 001.001.038 */
521 	{ 0x220e, "BCM20702A1"	},	/* 001.002.014 */
522 	{ 0x230f, "BCM4356A2"	},	/* 001.003.015 */
523 	{ 0x4106, "BCM4335B0"	},	/* 002.001.006 */
524 	{ 0x410e, "BCM20702B0"	},	/* 002.001.014 */
525 	{ 0x6109, "BCM4335C0"	},	/* 003.001.009 */
526 	{ 0x610c, "BCM4354"	},	/* 003.001.012 */
527 	{ 0x6607, "BCM4350C5"	},	/* 003.006.007 */
528 	{ }
529 };
530 
531 /*
532  * This currently only looks up the device tree board appendix,
533  * but can be expanded to other mechanisms.
534  */
535 static const char *btbcm_get_board_name(struct device *dev)
536 {
537 #ifdef CONFIG_OF
538 	struct device_node *root;
539 	char *board_type;
540 	const char *tmp;
541 	int len;
542 	int i;
543 
544 	root = of_find_node_by_path("/");
545 	if (!root)
546 		return NULL;
547 
548 	if (of_property_read_string_index(root, "compatible", 0, &tmp))
549 		return NULL;
550 
551 	/* get rid of any '/' in the compatible string */
552 	len = strlen(tmp) + 1;
553 	board_type = devm_kzalloc(dev, len, GFP_KERNEL);
554 	strscpy(board_type, tmp, len);
555 	for (i = 0; i < len; i++) {
556 		if (board_type[i] == '/')
557 			board_type[i] = '-';
558 	}
559 	of_node_put(root);
560 
561 	return board_type;
562 #else
563 	return NULL;
564 #endif
565 }
566 
567 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
568 {
569 	u16 subver, rev, pid, vid;
570 	struct sk_buff *skb;
571 	struct hci_rp_read_local_version *ver;
572 	const struct bcm_subver_table *bcm_subver_table;
573 	const char *hw_name = NULL;
574 	const char *board_name;
575 	char postfix[16] = "";
576 	int fw_name_count = 0;
577 	bcm_fw_name *fw_name;
578 	const struct firmware *fw;
579 	int i, err;
580 
581 	board_name = btbcm_get_board_name(&hdev->dev);
582 
583 	/* Reset */
584 	err = btbcm_reset(hdev);
585 	if (err)
586 		return err;
587 
588 	/* Read Local Version Info */
589 	skb = btbcm_read_local_version(hdev);
590 	if (IS_ERR(skb))
591 		return PTR_ERR(skb);
592 
593 	ver = (struct hci_rp_read_local_version *)skb->data;
594 	rev = le16_to_cpu(ver->hci_rev);
595 	subver = le16_to_cpu(ver->lmp_subver);
596 	kfree_skb(skb);
597 
598 	/* Read controller information */
599 	if (!(*fw_load_done)) {
600 		err = btbcm_read_info(hdev);
601 		if (err)
602 			return err;
603 	}
604 
605 	if (!use_autobaud_mode) {
606 		err = btbcm_print_controller_features(hdev);
607 		if (err)
608 			return err;
609 
610 		err = btbcm_print_local_name(hdev);
611 		if (err)
612 			return err;
613 	}
614 
615 	bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
616 						    bcm_uart_subver_table;
617 
618 	for (i = 0; bcm_subver_table[i].name; i++) {
619 		if (subver == bcm_subver_table[i].subver) {
620 			hw_name = bcm_subver_table[i].name;
621 			break;
622 		}
623 	}
624 
625 	bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
626 		    hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
627 		    (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
628 
629 	if (*fw_load_done)
630 		return 0;
631 
632 	if (hdev->bus == HCI_USB) {
633 		/* Read USB Product Info */
634 		skb = btbcm_read_usb_product(hdev);
635 		if (IS_ERR(skb))
636 			return PTR_ERR(skb);
637 
638 		vid = get_unaligned_le16(skb->data + 1);
639 		pid = get_unaligned_le16(skb->data + 3);
640 		kfree_skb(skb);
641 
642 		snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
643 	}
644 
645 	fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
646 	if (!fw_name)
647 		return -ENOMEM;
648 
649 	if (hw_name) {
650 		if (board_name) {
651 			snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
652 				 "brcm/%s%s.%s.hcd", hw_name, postfix, board_name);
653 			fw_name_count++;
654 		}
655 		snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
656 			 "brcm/%s%s.hcd", hw_name, postfix);
657 		fw_name_count++;
658 	}
659 
660 	if (board_name) {
661 		snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
662 			 "brcm/BCM%s.%s.hcd", postfix, board_name);
663 		fw_name_count++;
664 	}
665 	snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
666 		 "brcm/BCM%s.hcd", postfix);
667 	fw_name_count++;
668 
669 	for (i = 0; i < fw_name_count; i++) {
670 		err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
671 		if (err == 0) {
672 			bt_dev_info(hdev, "%s '%s' Patch",
673 				    hw_name ? hw_name : "BCM", fw_name[i]);
674 			*fw_load_done = true;
675 			break;
676 		}
677 	}
678 
679 	if (*fw_load_done) {
680 		err = btbcm_patchram(hdev, fw);
681 		if (err)
682 			bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
683 
684 		release_firmware(fw);
685 	} else {
686 		bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
687 		for (i = 0; i < fw_name_count; i++)
688 			bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
689 	}
690 
691 	kfree(fw_name);
692 	return 0;
693 }
694 EXPORT_SYMBOL_GPL(btbcm_initialize);
695 
696 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
697 {
698 	int err;
699 
700 	/* Re-initialize if necessary */
701 	if (*fw_load_done) {
702 		err = btbcm_initialize(hdev, fw_load_done, use_autobaud_mode);
703 		if (err)
704 			return err;
705 	}
706 
707 	btbcm_check_bdaddr(hdev);
708 
709 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
710 
711 	return 0;
712 }
713 EXPORT_SYMBOL_GPL(btbcm_finalize);
714 
715 int btbcm_setup_patchram(struct hci_dev *hdev)
716 {
717 	bool fw_load_done = false;
718 	bool use_autobaud_mode = false;
719 	int err;
720 
721 	/* Initialize */
722 	err = btbcm_initialize(hdev, &fw_load_done, use_autobaud_mode);
723 	if (err)
724 		return err;
725 
726 	/* Re-initialize after loading Patch */
727 	return btbcm_finalize(hdev, &fw_load_done, use_autobaud_mode);
728 }
729 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
730 
731 int btbcm_setup_apple(struct hci_dev *hdev)
732 {
733 	struct sk_buff *skb;
734 	int err;
735 
736 	/* Reset */
737 	err = btbcm_reset(hdev);
738 	if (err)
739 		return err;
740 
741 	/* Read Verbose Config Version Info */
742 	skb = btbcm_read_verbose_config(hdev);
743 	if (!IS_ERR(skb)) {
744 		bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
745 			    skb->data[1], get_unaligned_le16(skb->data + 5));
746 		kfree_skb(skb);
747 	}
748 
749 	/* Read USB Product Info */
750 	skb = btbcm_read_usb_product(hdev);
751 	if (!IS_ERR(skb)) {
752 		bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
753 			    get_unaligned_le16(skb->data + 1),
754 			    get_unaligned_le16(skb->data + 3));
755 		kfree_skb(skb);
756 	}
757 
758 	/* Read Controller Features */
759 	skb = btbcm_read_controller_features(hdev);
760 	if (!IS_ERR(skb)) {
761 		bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
762 		kfree_skb(skb);
763 	}
764 
765 	/* Read Local Name */
766 	skb = btbcm_read_local_name(hdev);
767 	if (!IS_ERR(skb)) {
768 		bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
769 		kfree_skb(skb);
770 	}
771 
772 	set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
773 
774 	return 0;
775 }
776 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
777 
778 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
779 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
780 MODULE_VERSION(VERSION);
781 MODULE_LICENSE("GPL");
782