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 <linux/string.h>
15 #include <linux/unaligned.h>
17 #include <net/bluetooth/bluetooth.h>
18 #include <net/bluetooth/hci_core.h>
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}})
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
];
41 static int btbcm_set_bdaddr_from_efi(struct hci_dev
*hdev
)
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
;
50 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE
))
53 len
= sizeof(efi_bdaddr
);
54 status
= efi
.get_variable(L
"BDADDR", &guid
, NULL
, &len
, &efi_bdaddr
);
55 if (status
!= EFI_SUCCESS
)
58 if (len
!= sizeof(efi_bdaddr
))
61 baswap(&bdaddr
, &efi_bdaddr
);
63 ret
= btbcm_set_bdaddr(hdev
, &bdaddr
);
67 bt_dev_info(hdev
, "BCM: Using EFI device address (%pMR)", &bdaddr
);
71 static int btbcm_set_bdaddr_from_efi(struct hci_dev
*hdev
)
77 int btbcm_check_bdaddr(struct hci_dev
*hdev
)
79 struct hci_rp_read_bd_addr
*bda
;
82 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
85 int err
= PTR_ERR(skb
);
87 bt_dev_err(hdev
, "BCM: Reading device address failed (%d)", err
);
91 if (skb
->len
!= sizeof(*bda
)) {
92 bt_dev_err(hdev
, "BCM: Device address length mismatch");
97 bda
= (struct hci_rp_read_bd_addr
*)skb
->data
;
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.
103 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
104 * with no configured address.
106 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
107 * with no configured address.
109 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
110 * with no configured address.
112 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
113 * with waiting for configuration state.
115 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
116 * with waiting for configuration state.
118 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
119 * with no configured address.
121 * The address AA:AA:AA:AA:AA:AA indicates a BCM43430A1 controller
122 * with no configured address.
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)",
138 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
146 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr
);
148 int btbcm_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
153 skb
= __hci_cmd_sync(hdev
, 0xfc01, 6, bdaddr
, HCI_INIT_TIMEOUT
);
156 bt_dev_err(hdev
, "BCM: Change address command failed (%d)", err
);
163 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr
);
165 int btbcm_read_pcm_int_params(struct hci_dev
*hdev
,
166 struct bcm_set_pcm_int_params
*params
)
171 skb
= __hci_cmd_sync(hdev
, 0xfc1d, 0, NULL
, HCI_INIT_TIMEOUT
);
174 bt_dev_err(hdev
, "BCM: Read PCM int params failed (%d)", err
);
178 if (skb
->len
!= 6 || skb
->data
[0]) {
179 bt_dev_err(hdev
, "BCM: Read PCM int params length mismatch");
185 memcpy(params
, skb
->data
+ 1, 5);
191 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params
);
193 int btbcm_write_pcm_int_params(struct hci_dev
*hdev
,
194 const struct bcm_set_pcm_int_params
*params
)
199 skb
= __hci_cmd_sync(hdev
, 0xfc1c, 5, params
, HCI_INIT_TIMEOUT
);
202 bt_dev_err(hdev
, "BCM: Write PCM int params failed (%d)", err
);
209 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params
);
211 int btbcm_patchram(struct hci_dev
*hdev
, const struct firmware
*fw
)
213 const struct hci_command_hdr
*cmd
;
221 skb
= __hci_cmd_sync(hdev
, 0xfc2e, 0, NULL
, HCI_INIT_TIMEOUT
);
224 bt_dev_err(hdev
, "BCM: Download Minidrv command failed (%d)",
230 /* 50 msec delay after Download Minidrv completes */
236 while (fw_size
>= sizeof(*cmd
)) {
239 cmd
= (struct hci_command_hdr
*)fw_ptr
;
240 fw_ptr
+= sizeof(*cmd
);
241 fw_size
-= sizeof(*cmd
);
243 if (fw_size
< cmd
->plen
) {
244 bt_dev_err(hdev
, "BCM: Patch is corrupted");
251 fw_size
-= cmd
->plen
;
253 opcode
= le16_to_cpu(cmd
->opcode
);
255 skb
= __hci_cmd_sync(hdev
, opcode
, cmd
->plen
, cmd_param
,
259 bt_dev_err(hdev
, "BCM: Patch command %04x failed (%d)",
266 /* 250 msec delay after Launch Ram completes */
272 EXPORT_SYMBOL(btbcm_patchram
);
274 static int btbcm_reset(struct hci_dev
*hdev
)
278 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
280 int err
= PTR_ERR(skb
);
282 bt_dev_err(hdev
, "BCM: Reset failed (%d)", err
);
287 /* 100 msec delay for module to complete reset process */
293 static struct sk_buff
*btbcm_read_local_name(struct hci_dev
*hdev
)
297 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_NAME
, 0, NULL
,
300 bt_dev_err(hdev
, "BCM: Reading local name failed (%ld)",
305 if (skb
->len
!= sizeof(struct hci_rp_read_local_name
)) {
306 bt_dev_err(hdev
, "BCM: Local name length mismatch");
308 return ERR_PTR(-EIO
);
314 static struct sk_buff
*btbcm_read_local_version(struct hci_dev
*hdev
)
318 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
321 bt_dev_err(hdev
, "BCM: Reading local version info failed (%ld)",
326 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
327 bt_dev_err(hdev
, "BCM: Local version length mismatch");
329 return ERR_PTR(-EIO
);
335 static struct sk_buff
*btbcm_read_verbose_config(struct hci_dev
*hdev
)
339 skb
= __hci_cmd_sync(hdev
, 0xfc79, 0, NULL
, HCI_INIT_TIMEOUT
);
341 bt_dev_err(hdev
, "BCM: Read verbose config info failed (%ld)",
347 bt_dev_err(hdev
, "BCM: Verbose config length mismatch");
349 return ERR_PTR(-EIO
);
355 static struct sk_buff
*btbcm_read_controller_features(struct hci_dev
*hdev
)
359 skb
= __hci_cmd_sync(hdev
, 0xfc6e, 0, NULL
, HCI_INIT_TIMEOUT
);
361 bt_dev_err(hdev
, "BCM: Read controller features failed (%ld)",
367 bt_dev_err(hdev
, "BCM: Controller features length mismatch");
369 return ERR_PTR(-EIO
);
375 static struct sk_buff
*btbcm_read_usb_product(struct hci_dev
*hdev
)
379 skb
= __hci_cmd_sync(hdev
, 0xfc5a, 0, NULL
, HCI_INIT_TIMEOUT
);
381 bt_dev_err(hdev
, "BCM: Read USB product info failed (%ld)",
387 bt_dev_err(hdev
, "BCM: USB product length mismatch");
389 return ERR_PTR(-EIO
);
395 static const struct dmi_system_id disable_broken_read_transmit_power
[] = {
398 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
399 DMI_MATCH(DMI_PRODUCT_NAME
, "MacBookPro16,1"),
404 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
405 DMI_MATCH(DMI_PRODUCT_NAME
, "MacBookPro16,2"),
410 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
411 DMI_MATCH(DMI_PRODUCT_NAME
, "MacBookPro16,4"),
416 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
417 DMI_MATCH(DMI_PRODUCT_NAME
, "MacBookAir8,1"),
422 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
423 DMI_MATCH(DMI_PRODUCT_NAME
, "MacBookAir8,2"),
428 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
429 DMI_MATCH(DMI_PRODUCT_NAME
, "iMac20,1"),
434 DMI_MATCH(DMI_BOARD_VENDOR
, "Apple Inc."),
435 DMI_MATCH(DMI_PRODUCT_NAME
, "iMac20,2"),
441 static int btbcm_read_info(struct hci_dev
*hdev
)
445 /* Read Verbose Config Version Info */
446 skb
= btbcm_read_verbose_config(hdev
);
450 bt_dev_info(hdev
, "BCM: chip id %u", skb
->data
[1]);
456 static int btbcm_print_controller_features(struct hci_dev
*hdev
)
460 /* Read Controller Features */
461 skb
= btbcm_read_controller_features(hdev
);
465 bt_dev_info(hdev
, "BCM: features 0x%2.2x", skb
->data
[1]);
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
);
475 static int btbcm_print_local_name(struct hci_dev
*hdev
)
479 /* Read Local Name */
480 skb
= btbcm_read_local_name(hdev
);
484 bt_dev_info(hdev
, "%s", (char *)(skb
->data
+ 1));
490 struct bcm_subver_table
{
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 */
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 */
538 * This currently only looks up the device tree board appendix,
539 * but can be expanded to other mechanisms.
541 static const char *btbcm_get_board_name(struct device
*dev
)
544 struct device_node
*root
__free(device_node
) = of_find_node_by_path("/");
551 if (of_property_read_string_index(root
, "compatible", 0, &tmp
))
554 /* get rid of any '/' in the compatible string */
555 board_type
= devm_kstrdup(dev
, tmp
, GFP_KERNEL
);
556 strreplace(board_type
, '/', '-');
564 int btbcm_initialize(struct hci_dev
*hdev
, bool *fw_load_done
, bool use_autobaud_mode
)
566 u16 subver
, rev
, pid
, vid
;
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
;
578 board_name
= btbcm_get_board_name(&hdev
->dev
);
581 err
= btbcm_reset(hdev
);
585 /* Read Local Version Info */
586 skb
= btbcm_read_local_version(hdev
);
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
);
595 /* Read controller information */
596 if (!(*fw_load_done
)) {
597 err
= btbcm_read_info(hdev
);
602 if (!use_autobaud_mode
) {
603 err
= btbcm_print_controller_features(hdev
);
607 err
= btbcm_print_local_name(hdev
);
612 bcm_subver_table
= (hdev
->bus
== HCI_USB
) ? bcm_usb_subver_table
:
613 bcm_uart_subver_table
;
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
;
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);
629 if (hdev
->bus
== HCI_USB
) {
630 /* Read USB Product Info */
631 skb
= btbcm_read_usb_product(hdev
);
635 vid
= get_unaligned_le16(skb
->data
+ 1);
636 pid
= get_unaligned_le16(skb
->data
+ 3);
639 snprintf(postfix
, sizeof(postfix
), "-%4.4x-%4.4x", vid
, pid
);
642 fw_name
= kmalloc(BCM_FW_NAME_COUNT_MAX
* BCM_FW_NAME_LEN
, GFP_KERNEL
);
648 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
649 "brcm/%s%s.%s.hcd", hw_name
, postfix
, board_name
);
652 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
653 "brcm/%s%s.hcd", hw_name
, postfix
);
658 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
659 "brcm/BCM%s.%s.hcd", postfix
, board_name
);
662 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
663 "brcm/BCM%s.hcd", postfix
);
666 for (i
= 0; i
< fw_name_count
; i
++) {
667 err
= firmware_request_nowarn(&fw
, fw_name
[i
], &hdev
->dev
);
669 bt_dev_info(hdev
, "%s '%s' Patch",
670 hw_name
? hw_name
: "BCM", fw_name
[i
]);
671 *fw_load_done
= true;
677 err
= btbcm_patchram(hdev
, fw
);
679 bt_dev_info(hdev
, "BCM: Patch failed (%d)", err
);
681 release_firmware(fw
);
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
]);
691 EXPORT_SYMBOL_GPL(btbcm_initialize
);
693 int btbcm_finalize(struct hci_dev
*hdev
, bool *fw_load_done
, bool use_autobaud_mode
)
697 /* Re-initialize if necessary */
699 err
= btbcm_initialize(hdev
, fw_load_done
, use_autobaud_mode
);
704 btbcm_check_bdaddr(hdev
);
706 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
710 EXPORT_SYMBOL_GPL(btbcm_finalize
);
712 int btbcm_setup_patchram(struct hci_dev
*hdev
)
714 bool fw_load_done
= false;
715 bool use_autobaud_mode
= false;
719 err
= btbcm_initialize(hdev
, &fw_load_done
, use_autobaud_mode
);
723 /* Re-initialize after loading Patch */
724 return btbcm_finalize(hdev
, &fw_load_done
, use_autobaud_mode
);
726 EXPORT_SYMBOL_GPL(btbcm_setup_patchram
);
728 int btbcm_setup_apple(struct hci_dev
*hdev
)
734 err
= btbcm_reset(hdev
);
738 /* Read Verbose Config Version Info */
739 skb
= btbcm_read_verbose_config(hdev
);
741 bt_dev_info(hdev
, "BCM: chip id %u build %4.4u",
742 skb
->data
[1], get_unaligned_le16(skb
->data
+ 5));
746 /* Read USB Product Info */
747 skb
= btbcm_read_usb_product(hdev
);
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));
755 /* Read Controller Features */
756 skb
= btbcm_read_controller_features(hdev
);
758 bt_dev_info(hdev
, "BCM: features 0x%2.2x", skb
->data
[1]);
762 /* Read Local Name */
763 skb
= btbcm_read_local_name(hdev
);
765 bt_dev_info(hdev
, "%s", (char *)(skb
->data
+ 1));
769 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
773 EXPORT_SYMBOL_GPL(btbcm_setup_apple
);
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");