1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth support for Broadcom devices
6 * Copyright (C) 2015 Intel Corporation
10 #include <linux/module.h>
11 #include <linux/firmware.h>
12 #include <linux/dmi.h>
14 #include <asm/unaligned.h>
16 #include <net/bluetooth/bluetooth.h>
17 #include <net/bluetooth/hci_core.h>
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}})
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];
39 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
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;
48 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
51 len = sizeof(efi_bdaddr);
52 status = efi.get_variable(L"BDADDR", &guid, NULL, &len, &efi_bdaddr);
53 if (status != EFI_SUCCESS)
56 if (len != sizeof(efi_bdaddr))
59 baswap(&bdaddr, &efi_bdaddr);
61 ret = btbcm_set_bdaddr(hdev, &bdaddr);
65 bt_dev_info(hdev, "BCM: Using EFI device address (%pMR)", &bdaddr);
69 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
75 int btbcm_check_bdaddr(struct hci_dev *hdev)
77 struct hci_rp_read_bd_addr *bda;
80 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
83 int err = PTR_ERR(skb);
85 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
89 if (skb->len != sizeof(*bda)) {
90 bt_dev_err(hdev, "BCM: Device address length mismatch");
95 bda = (struct hci_rp_read_bd_addr *)skb->data;
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.
101 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
102 * with no configured address.
104 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
105 * with no configured address.
107 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
108 * with no configured address.
110 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
111 * with waiting for configuration state.
113 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
114 * with waiting for configuration state.
116 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
117 * with no configured address.
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)",
132 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
140 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
142 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
147 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
150 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
157 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
159 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
160 struct bcm_set_pcm_int_params *params)
165 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
168 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
172 if (skb->len != 6 || skb->data[0]) {
173 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
179 memcpy(params, skb->data + 1, 5);
185 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
187 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
188 const struct bcm_set_pcm_int_params *params)
193 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
196 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
203 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
205 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
207 const struct hci_command_hdr *cmd;
215 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
218 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
224 /* 50 msec delay after Download Minidrv completes */
230 while (fw_size >= sizeof(*cmd)) {
233 cmd = (struct hci_command_hdr *)fw_ptr;
234 fw_ptr += sizeof(*cmd);
235 fw_size -= sizeof(*cmd);
237 if (fw_size < cmd->plen) {
238 bt_dev_err(hdev, "BCM: Patch is corrupted");
245 fw_size -= cmd->plen;
247 opcode = le16_to_cpu(cmd->opcode);
249 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
253 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
260 /* 250 msec delay after Launch Ram completes */
266 EXPORT_SYMBOL(btbcm_patchram);
268 static int btbcm_reset(struct hci_dev *hdev)
272 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
274 int err = PTR_ERR(skb);
276 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
281 /* 100 msec delay for module to complete reset process */
287 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
291 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
294 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
299 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
300 bt_dev_err(hdev, "BCM: Local name length mismatch");
302 return ERR_PTR(-EIO);
308 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
312 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
315 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
320 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
321 bt_dev_err(hdev, "BCM: Local version length mismatch");
323 return ERR_PTR(-EIO);
329 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
333 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
335 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
341 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
343 return ERR_PTR(-EIO);
349 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
353 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
355 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
361 bt_dev_err(hdev, "BCM: Controller features length mismatch");
363 return ERR_PTR(-EIO);
369 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
373 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
375 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
381 bt_dev_err(hdev, "BCM: USB product length mismatch");
383 return ERR_PTR(-EIO);
389 static const struct dmi_system_id disable_broken_read_transmit_power[] = {
392 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
393 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,1"),
398 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
399 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
404 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
405 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,4"),
410 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
411 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,1"),
416 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
417 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,2"),
422 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
423 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,1"),
428 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
429 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,2"),
435 static int btbcm_read_info(struct hci_dev *hdev)
439 /* Read Verbose Config Version Info */
440 skb = btbcm_read_verbose_config(hdev);
444 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
450 static int btbcm_print_controller_features(struct hci_dev *hdev)
454 /* Read Controller Features */
455 skb = btbcm_read_controller_features(hdev);
459 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
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);
469 static int btbcm_print_local_name(struct hci_dev *hdev)
473 /* Read Local Name */
474 skb = btbcm_read_local_name(hdev);
478 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
484 struct bcm_subver_table {
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 */
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 */
532 * This currently only looks up the device tree board appendix,
533 * but can be expanded to other mechanisms.
535 static const char *btbcm_get_board_name(struct device *dev)
538 struct device_node *root;
544 root = of_find_node_by_path("/");
548 if (of_property_read_string_index(root, "compatible", 0, &tmp))
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] == '/')
567 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
569 u16 subver, rev, pid, vid;
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;
581 board_name = btbcm_get_board_name(&hdev->dev);
584 err = btbcm_reset(hdev);
588 /* Read Local Version Info */
589 skb = btbcm_read_local_version(hdev);
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);
598 /* Read controller information */
599 if (!(*fw_load_done)) {
600 err = btbcm_read_info(hdev);
605 if (!use_autobaud_mode) {
606 err = btbcm_print_controller_features(hdev);
610 err = btbcm_print_local_name(hdev);
615 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
616 bcm_uart_subver_table;
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;
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);
632 if (hdev->bus == HCI_USB) {
633 /* Read USB Product Info */
634 skb = btbcm_read_usb_product(hdev);
638 vid = get_unaligned_le16(skb->data + 1);
639 pid = get_unaligned_le16(skb->data + 3);
642 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
645 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
651 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
652 "brcm/%s%s.%s.hcd", hw_name, postfix, board_name);
655 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
656 "brcm/%s%s.hcd", hw_name, postfix);
661 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
662 "brcm/BCM%s.%s.hcd", postfix, board_name);
665 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
666 "brcm/BCM%s.hcd", postfix);
669 for (i = 0; i < fw_name_count; i++) {
670 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
672 bt_dev_info(hdev, "%s '%s' Patch",
673 hw_name ? hw_name : "BCM", fw_name[i]);
674 *fw_load_done = true;
680 err = btbcm_patchram(hdev, fw);
682 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
684 release_firmware(fw);
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]);
694 EXPORT_SYMBOL_GPL(btbcm_initialize);
696 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
700 /* Re-initialize if necessary */
702 err = btbcm_initialize(hdev, fw_load_done, use_autobaud_mode);
707 btbcm_check_bdaddr(hdev);
709 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
713 EXPORT_SYMBOL_GPL(btbcm_finalize);
715 int btbcm_setup_patchram(struct hci_dev *hdev)
717 bool fw_load_done = false;
718 bool use_autobaud_mode = false;
722 err = btbcm_initialize(hdev, &fw_load_done, use_autobaud_mode);
726 /* Re-initialize after loading Patch */
727 return btbcm_finalize(hdev, &fw_load_done, use_autobaud_mode);
729 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
731 int btbcm_setup_apple(struct hci_dev *hdev)
737 err = btbcm_reset(hdev);
741 /* Read Verbose Config Version Info */
742 skb = btbcm_read_verbose_config(hdev);
744 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
745 skb->data[1], get_unaligned_le16(skb->data + 5));
749 /* Read USB Product Info */
750 skb = btbcm_read_usb_product(hdev);
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));
758 /* Read Controller Features */
759 skb = btbcm_read_controller_features(hdev);
761 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
765 /* Read Local Name */
766 skb = btbcm_read_local_name(hdev);
768 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
772 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
776 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
779 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
780 MODULE_VERSION(VERSION);
781 MODULE_LICENSE("GPL");