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