1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Bluetooth support for Broadcom devices
6 * Copyright (C) 2015 Intel Corporation
9 #include <linux/module.h>
10 #include <linux/firmware.h>
11 #include <linux/dmi.h>
13 #include <asm/unaligned.h>
15 #include <net/bluetooth/bluetooth.h>
16 #include <net/bluetooth/hci_core.h>
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}})
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];
37 int btbcm_check_bdaddr(struct hci_dev *hdev)
39 struct hci_rp_read_bd_addr *bda;
42 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
45 int err = PTR_ERR(skb);
47 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
51 if (skb->len != sizeof(*bda)) {
52 bt_dev_err(hdev, "BCM: Device address length mismatch");
57 bda = (struct hci_rp_read_bd_addr *)skb->data;
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.
63 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
64 * with no configured address.
66 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
67 * with no configured address.
69 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
70 * with no configured address.
72 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
73 * with waiting for configuration state.
75 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
76 * with waiting for configuration state.
78 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
79 * with no configured address.
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)",
92 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
99 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
101 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
106 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
109 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
116 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
118 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
119 struct bcm_set_pcm_int_params *params)
124 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
127 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
131 if (skb->len != 6 || skb->data[0]) {
132 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
138 memcpy(params, skb->data + 1, 5);
144 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
146 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
147 const struct bcm_set_pcm_int_params *params)
152 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
155 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
162 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
164 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
166 const struct hci_command_hdr *cmd;
174 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
177 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
183 /* 50 msec delay after Download Minidrv completes */
189 while (fw_size >= sizeof(*cmd)) {
192 cmd = (struct hci_command_hdr *)fw_ptr;
193 fw_ptr += sizeof(*cmd);
194 fw_size -= sizeof(*cmd);
196 if (fw_size < cmd->plen) {
197 bt_dev_err(hdev, "BCM: Patch is corrupted");
204 fw_size -= cmd->plen;
206 opcode = le16_to_cpu(cmd->opcode);
208 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
212 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
219 /* 250 msec delay after Launch Ram completes */
225 EXPORT_SYMBOL(btbcm_patchram);
227 static int btbcm_reset(struct hci_dev *hdev)
231 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
233 int err = PTR_ERR(skb);
235 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
240 /* 100 msec delay for module to complete reset process */
246 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
250 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
253 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
258 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
259 bt_dev_err(hdev, "BCM: Local name length mismatch");
261 return ERR_PTR(-EIO);
267 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
271 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
274 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
279 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
280 bt_dev_err(hdev, "BCM: Local version length mismatch");
282 return ERR_PTR(-EIO);
288 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
292 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
294 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
300 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
302 return ERR_PTR(-EIO);
308 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
312 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
314 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
320 bt_dev_err(hdev, "BCM: Controller features length mismatch");
322 return ERR_PTR(-EIO);
328 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
332 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
334 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
340 bt_dev_err(hdev, "BCM: USB product length mismatch");
342 return ERR_PTR(-EIO);
348 static const struct dmi_system_id disable_broken_read_transmit_power[] = {
351 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
352 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,1"),
357 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
358 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
363 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
364 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,4"),
369 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
370 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,1"),
375 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
376 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,2"),
381 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
382 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,1"),
387 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
388 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,2"),
394 static int btbcm_read_info(struct hci_dev *hdev)
398 /* Read Verbose Config Version Info */
399 skb = btbcm_read_verbose_config(hdev);
403 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
409 static int btbcm_print_controller_features(struct hci_dev *hdev)
413 /* Read Controller Features */
414 skb = btbcm_read_controller_features(hdev);
418 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
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);
428 static int btbcm_print_local_name(struct hci_dev *hdev)
432 /* Read Local Name */
433 skb = btbcm_read_local_name(hdev);
437 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
443 struct bcm_subver_table {
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 */
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 */
491 * This currently only looks up the device tree board appendix,
492 * but can be expanded to other mechanisms.
494 static const char *btbcm_get_board_name(struct device *dev)
497 struct device_node *root;
503 root = of_find_node_by_path("/");
507 if (of_property_read_string_index(root, "compatible", 0, &tmp))
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] == '/')
526 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
528 u16 subver, rev, pid, vid;
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;
540 board_name = btbcm_get_board_name(&hdev->dev);
543 err = btbcm_reset(hdev);
547 /* Read Local Version Info */
548 skb = btbcm_read_local_version(hdev);
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);
557 /* Read controller information */
558 if (!(*fw_load_done)) {
559 err = btbcm_read_info(hdev);
564 if (!use_autobaud_mode) {
565 err = btbcm_print_controller_features(hdev);
569 err = btbcm_print_local_name(hdev);
574 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
575 bcm_uart_subver_table;
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;
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);
591 if (hdev->bus == HCI_USB) {
592 /* Read USB Product Info */
593 skb = btbcm_read_usb_product(hdev);
597 vid = get_unaligned_le16(skb->data + 1);
598 pid = get_unaligned_le16(skb->data + 3);
601 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
604 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
610 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
611 "brcm/%s%s.%s.hcd", hw_name, postfix, board_name);
614 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
615 "brcm/%s%s.hcd", hw_name, postfix);
620 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
621 "brcm/BCM%s.%s.hcd", postfix, board_name);
624 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
625 "brcm/BCM%s.hcd", postfix);
628 for (i = 0; i < fw_name_count; i++) {
629 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
631 bt_dev_info(hdev, "%s '%s' Patch",
632 hw_name ? hw_name : "BCM", fw_name[i]);
633 *fw_load_done = true;
639 err = btbcm_patchram(hdev, fw);
641 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
643 release_firmware(fw);
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]);
653 EXPORT_SYMBOL_GPL(btbcm_initialize);
655 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done, bool use_autobaud_mode)
659 /* Re-initialize if necessary */
661 err = btbcm_initialize(hdev, fw_load_done, use_autobaud_mode);
666 btbcm_check_bdaddr(hdev);
668 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
672 EXPORT_SYMBOL_GPL(btbcm_finalize);
674 int btbcm_setup_patchram(struct hci_dev *hdev)
676 bool fw_load_done = false;
677 bool use_autobaud_mode = false;
681 err = btbcm_initialize(hdev, &fw_load_done, use_autobaud_mode);
685 /* Re-initialize after loading Patch */
686 return btbcm_finalize(hdev, &fw_load_done, use_autobaud_mode);
688 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
690 int btbcm_setup_apple(struct hci_dev *hdev)
696 err = btbcm_reset(hdev);
700 /* Read Verbose Config Version Info */
701 skb = btbcm_read_verbose_config(hdev);
703 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
704 skb->data[1], get_unaligned_le16(skb->data + 5));
708 /* Read USB Product Info */
709 skb = btbcm_read_usb_product(hdev);
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));
717 /* Read Controller Features */
718 skb = btbcm_read_controller_features(hdev);
720 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
724 /* Read Local Name */
725 skb = btbcm_read_local_name(hdev);
727 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
731 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
735 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
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");