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_BCM43430A1 (&(bdaddr_t) {{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa}})
28 #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
29 #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
30 #define BDADDR_BCM4334B0 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb0, 0x34, 0x43}})
31 #define BDADDR_BCM4345C5 (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0xc5, 0x45, 0x43}})
32 #define BDADDR_BCM43341B (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0x1b, 0x34, 0x43}})
34 #define BCM_FW_NAME_LEN 64
35 #define BCM_FW_NAME_COUNT_MAX 4
36 /* For kmalloc-ing the fw-name array instead of putting it on the stack */
37 typedef char bcm_fw_name[BCM_FW_NAME_LEN];
40 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
42 efi_guid_t guid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61, 0xb5, 0x1f,
43 0x43, 0x26, 0x81, 0x23, 0xd1, 0x13);
44 bdaddr_t efi_bdaddr, bdaddr;
49 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
52 len = sizeof(efi_bdaddr);
53 status = efi.get_variable(L"BDADDR", &guid, NULL, &len, &efi_bdaddr);
54 if (status != EFI_SUCCESS)
57 if (len != sizeof(efi_bdaddr))
60 baswap(&bdaddr, &efi_bdaddr);
62 ret = btbcm_set_bdaddr(hdev, &bdaddr);
66 bt_dev_info(hdev, "BCM: Using EFI device address (%pMR)", &bdaddr);
70 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
76 int btbcm_check_bdaddr(struct hci_dev *hdev)
78 struct hci_rp_read_bd_addr *bda;
81 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
84 int err = PTR_ERR(skb);
86 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
90 if (skb->len != sizeof(*bda)) {
91 bt_dev_err(hdev, "BCM: Device address length mismatch");
96 bda = (struct hci_rp_read_bd_addr *)skb->data;
98 /* Check if the address indicates a controller with either an
99 * invalid or default address. In both cases the device needs
100 * to be marked as not having a valid address.
102 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
103 * with no configured address.
105 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
106 * with no configured address.
108 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
109 * with no configured address.
111 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
112 * with waiting for configuration state.
114 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
115 * with waiting for configuration state.
117 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
118 * with no configured address.
120 * The address AA:AA:AA:AA:AA:AA indicates a BCM43430A1 controller
121 * with no configured address.
123 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
124 !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
125 !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
126 !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
127 !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
128 !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
129 !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
130 !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
131 !bacmp(&bda->bdaddr, BDADDR_BCM43430A1) ||
132 !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
133 /* Try falling back to BDADDR EFI variable */
134 if (btbcm_set_bdaddr_from_efi(hdev) != 0) {
135 bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
137 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
145 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
147 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
152 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
155 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
162 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
164 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
165 struct bcm_set_pcm_int_params *params)
170 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
173 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
177 if (skb->len != 6 || skb->data[0]) {
178 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
184 memcpy(params, skb->data + 1, 5);
190 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
192 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
193 const struct bcm_set_pcm_int_params *params)
198 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
201 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
208 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
210 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
212 const struct hci_command_hdr *cmd;
220 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
223 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
229 /* 50 msec delay after Download Minidrv completes */
235 while (fw_size >= sizeof(*cmd)) {
238 cmd = (struct hci_command_hdr *)fw_ptr;
239 fw_ptr += sizeof(*cmd);
240 fw_size -= sizeof(*cmd);
242 if (fw_size < cmd->plen) {
243 bt_dev_err(hdev, "BCM: Patch is corrupted");
250 fw_size -= cmd->plen;
252 opcode = le16_to_cpu(cmd->opcode);
254 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
258 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
265 /* 250 msec delay after Launch Ram completes */
271 EXPORT_SYMBOL(btbcm_patchram);
273 static int btbcm_reset(struct hci_dev *hdev)
277 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
279 int err = PTR_ERR(skb);
281 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
286 /* 100 msec delay for module to complete reset process */
292 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
296 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
299 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
304 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
305 bt_dev_err(hdev, "BCM: Local name length mismatch");
307 return ERR_PTR(-EIO);
313 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
317 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
320 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
325 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
326 bt_dev_err(hdev, "BCM: Local version length mismatch");
328 return ERR_PTR(-EIO);
334 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
338 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
340 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
346 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
348 return ERR_PTR(-EIO);
354 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
358 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
360 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
366 bt_dev_err(hdev, "BCM: Controller features length mismatch");
368 return ERR_PTR(-EIO);
374 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
378 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
380 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
386 bt_dev_err(hdev, "BCM: USB product length mismatch");
388 return ERR_PTR(-EIO);
394 static const struct dmi_system_id disable_broken_read_transmit_power[] = {
397 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
398 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,1"),
403 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
404 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
409 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
410 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,4"),
415 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
416 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,1"),
421 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
422 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,2"),
427 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
428 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,1"),
433 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
434 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,2"),
440 static int btbcm_read_info(struct hci_dev *hdev)
444 /* Read Verbose Config Version Info */
445 skb = btbcm_read_verbose_config(hdev);
449 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
455 static int btbcm_print_controller_features(struct hci_dev *hdev)
459 /* Read Controller Features */
460 skb = btbcm_read_controller_features(hdev);
464 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
467 /* Read DMI and disable broken Read LE Min/Max Tx Power */
468 if (dmi_first_match(disable_broken_read_transmit_power))
469 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
474 static int btbcm_print_local_name(struct hci_dev *hdev)
478 /* Read Local Name */
479 skb = btbcm_read_local_name(hdev);
483 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
489 struct bcm_subver_table {
494 static const struct bcm_subver_table bcm_uart_subver_table[] = {
495 { 0x1111, "BCM4362A2" }, /* 000.017.017 */
496 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
497 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
498 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
499 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
500 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
501 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
502 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
503 { 0x610c, "BCM4354" }, /* 003.001.012 */
504 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
505 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
506 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
507 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
508 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
509 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
510 { 0x420d, "BCM4349B1" }, /* 002.002.013 */
511 { 0x420e, "BCM4349B1" }, /* 002.002.014 */
512 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
513 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
514 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
515 { 0x410c, "BCM43430B0" }, /* 002.001.012 */
516 { 0x2119, "BCM4373A0" }, /* 001.001.025 */
520 static const struct bcm_subver_table bcm_usb_subver_table[] = {
521 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
522 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
523 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
524 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
525 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
526 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
527 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
528 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
529 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
530 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
531 { 0x610c, "BCM4354" }, /* 003.001.012 */
532 { 0x6607, "BCM4350C5" }, /* 003.006.007 */
537 * This currently only looks up the device tree board appendix,
538 * but can be expanded to other mechanisms.
540 static const char *btbcm_get_board_name(struct device *dev)
543 struct device_node *root;
549 root = of_find_node_by_path("/");
553 if (of_property_read_string_index(root, "compatible", 0, &tmp))
556 /* get rid of any '/' in the compatible string */
557 len = strlen(tmp) + 1;
558 board_type = devm_kzalloc(dev, len, GFP_KERNEL);
559 strscpy(board_type, tmp, len);
560 for (i = 0; i < len; i++) {
561 if (board_type[i] == '/')
572 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
574 u16 subver, rev, pid, vid;
576 struct hci_rp_read_local_version *ver;
577 const struct bcm_subver_table *bcm_subver_table;
578 const char *hw_name = NULL;
579 const char *board_name;
580 char postfix[16] = "";
581 int fw_name_count = 0;
582 bcm_fw_name *fw_name;
583 const struct firmware *fw;
586 board_name = btbcm_get_board_name(&hdev->dev);
589 err = btbcm_reset(hdev);
593 /* Read Local Version Info */
594 skb = btbcm_read_local_version(hdev);
598 ver = (struct hci_rp_read_local_version *)skb->data;
599 rev = le16_to_cpu(ver->hci_rev);
600 subver = le16_to_cpu(ver->lmp_subver);
603 /* Read controller information */
604 if (!(*fw_load_done)) {
605 err = btbcm_read_info(hdev);
610 if (!use_autobaud_mode) {
611 err = btbcm_print_controller_features(hdev);
615 err = btbcm_print_local_name(hdev);
620 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
621 bcm_uart_subver_table;
623 for (i = 0; bcm_subver_table[i].name; i++) {
624 if (subver == bcm_subver_table[i].subver) {
625 hw_name = bcm_subver_table[i].name;
630 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
631 hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
632 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
637 if (hdev->bus == HCI_USB) {
638 /* Read USB Product Info */
639 skb = btbcm_read_usb_product(hdev);
643 vid = get_unaligned_le16(skb->data + 1);
644 pid = get_unaligned_le16(skb->data + 3);
647 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
650 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
656 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
657 "brcm/%s%s.%s.hcd", hw_name, postfix, board_name);
660 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
661 "brcm/%s%s.hcd", hw_name, postfix);
666 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
667 "brcm/BCM%s.%s.hcd", postfix, board_name);
670 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
671 "brcm/BCM%s.hcd", postfix);
674 for (i = 0; i < fw_name_count; i++) {
675 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
677 bt_dev_info(hdev, "%s '%s' Patch",
678 hw_name ? hw_name : "BCM", fw_name[i]);
679 *fw_load_done = true;
685 err = btbcm_patchram(hdev, fw);
687 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
689 release_firmware(fw);
691 bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
692 for (i = 0; i < fw_name_count; i++)
693 bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
699 EXPORT_SYMBOL_GPL(btbcm_initialize);
701 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
705 /* Re-initialize if necessary */
707 err = btbcm_initialize(hdev, fw_load_done, use_autobaud_mode);
712 btbcm_check_bdaddr(hdev);
714 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
718 EXPORT_SYMBOL_GPL(btbcm_finalize);
720 int btbcm_setup_patchram(struct hci_dev *hdev)
722 bool fw_load_done = false;
723 bool use_autobaud_mode = false;
727 err = btbcm_initialize(hdev, &fw_load_done, use_autobaud_mode);
731 /* Re-initialize after loading Patch */
732 return btbcm_finalize(hdev, &fw_load_done, use_autobaud_mode);
734 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
736 int btbcm_setup_apple(struct hci_dev *hdev)
742 err = btbcm_reset(hdev);
746 /* Read Verbose Config Version Info */
747 skb = btbcm_read_verbose_config(hdev);
749 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
750 skb->data[1], get_unaligned_le16(skb->data + 5));
754 /* Read USB Product Info */
755 skb = btbcm_read_usb_product(hdev);
757 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
758 get_unaligned_le16(skb->data + 1),
759 get_unaligned_le16(skb->data + 3));
763 /* Read Controller Features */
764 skb = btbcm_read_controller_features(hdev);
766 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
770 /* Read Local Name */
771 skb = btbcm_read_local_name(hdev);
773 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
777 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
781 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
783 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
784 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
785 MODULE_VERSION(VERSION);
786 MODULE_LICENSE("GPL");