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