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]);
406 /* Read Controller Features */
407 skb = btbcm_read_controller_features(hdev);
411 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
414 /* Read DMI and disable broken Read LE Min/Max Tx Power */
415 if (dmi_first_match(disable_broken_read_transmit_power))
416 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
421 static int btbcm_print_local_name(struct hci_dev *hdev)
425 /* Read Local Name */
426 skb = btbcm_read_local_name(hdev);
430 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
436 struct bcm_subver_table {
441 static const struct bcm_subver_table bcm_uart_subver_table[] = {
442 { 0x1111, "BCM4362A2" }, /* 000.017.017 */
443 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
444 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
445 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
446 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
447 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
448 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
449 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
450 { 0x610c, "BCM4354" }, /* 003.001.012 */
451 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
452 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
453 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
454 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
455 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
456 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
457 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
458 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
459 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
460 { 0x410c, "BCM43430B0" }, /* 002.001.012 */
461 { 0x2119, "BCM4373A0" }, /* 001.001.025 */
465 static const struct bcm_subver_table bcm_usb_subver_table[] = {
466 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
467 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
468 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
469 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
470 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
471 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
472 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
473 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
474 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
475 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
476 { 0x610c, "BCM4354" }, /* 003.001.012 */
477 { 0x6607, "BCM4350C5" }, /* 003.006.007 */
482 * This currently only looks up the device tree board appendix,
483 * but can be expanded to other mechanisms.
485 static const char *btbcm_get_board_name(struct device *dev)
488 struct device_node *root;
494 root = of_find_node_by_path("/");
498 if (of_property_read_string_index(root, "compatible", 0, &tmp))
501 /* get rid of any '/' in the compatible string */
502 len = strlen(tmp) + 1;
503 board_type = devm_kzalloc(dev, len, GFP_KERNEL);
504 strscpy(board_type, tmp, len);
505 for (i = 0; i < board_type[i]; i++) {
506 if (board_type[i] == '/')
517 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done)
519 u16 subver, rev, pid, vid;
521 struct hci_rp_read_local_version *ver;
522 const struct bcm_subver_table *bcm_subver_table;
523 const char *hw_name = NULL;
524 const char *board_name;
525 char postfix[16] = "";
526 int fw_name_count = 0;
527 bcm_fw_name *fw_name;
528 const struct firmware *fw;
531 board_name = btbcm_get_board_name(&hdev->dev);
534 err = btbcm_reset(hdev);
538 /* Read Local Version Info */
539 skb = btbcm_read_local_version(hdev);
543 ver = (struct hci_rp_read_local_version *)skb->data;
544 rev = le16_to_cpu(ver->hci_rev);
545 subver = le16_to_cpu(ver->lmp_subver);
548 /* Read controller information */
549 if (!(*fw_load_done)) {
550 err = btbcm_read_info(hdev);
554 err = btbcm_print_local_name(hdev);
558 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
559 bcm_uart_subver_table;
561 for (i = 0; bcm_subver_table[i].name; i++) {
562 if (subver == bcm_subver_table[i].subver) {
563 hw_name = bcm_subver_table[i].name;
568 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
569 hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
570 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
575 if (hdev->bus == HCI_USB) {
576 /* Read USB Product Info */
577 skb = btbcm_read_usb_product(hdev);
581 vid = get_unaligned_le16(skb->data + 1);
582 pid = get_unaligned_le16(skb->data + 3);
585 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
588 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
594 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
595 "brcm/%s%s.%s.hcd", hw_name, postfix, board_name);
598 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
599 "brcm/%s%s.hcd", hw_name, postfix);
604 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
605 "brcm/BCM%s.%s.hcd", postfix, board_name);
608 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
609 "brcm/BCM%s.hcd", postfix);
612 for (i = 0; i < fw_name_count; i++) {
613 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
615 bt_dev_info(hdev, "%s '%s' Patch",
616 hw_name ? hw_name : "BCM", fw_name[i]);
617 *fw_load_done = true;
623 err = btbcm_patchram(hdev, fw);
625 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
627 release_firmware(fw);
629 bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
630 for (i = 0; i < fw_name_count; i++)
631 bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
637 EXPORT_SYMBOL_GPL(btbcm_initialize);
639 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done)
643 /* Re-initialize if necessary */
645 err = btbcm_initialize(hdev, fw_load_done);
650 btbcm_check_bdaddr(hdev);
652 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
656 EXPORT_SYMBOL_GPL(btbcm_finalize);
658 int btbcm_setup_patchram(struct hci_dev *hdev)
660 bool fw_load_done = false;
664 err = btbcm_initialize(hdev, &fw_load_done);
668 /* Re-initialize after loading Patch */
669 return btbcm_finalize(hdev, &fw_load_done);
671 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
673 int btbcm_setup_apple(struct hci_dev *hdev)
679 err = btbcm_reset(hdev);
683 /* Read Verbose Config Version Info */
684 skb = btbcm_read_verbose_config(hdev);
686 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
687 skb->data[1], get_unaligned_le16(skb->data + 5));
691 /* Read USB Product Info */
692 skb = btbcm_read_usb_product(hdev);
694 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
695 get_unaligned_le16(skb->data + 1),
696 get_unaligned_le16(skb->data + 3));
700 /* Read Controller Features */
701 skb = btbcm_read_controller_features(hdev);
703 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
707 /* Read Local Name */
708 skb = btbcm_read_local_name(hdev);
710 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
714 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
718 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
720 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
721 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
722 MODULE_VERSION(VERSION);
723 MODULE_LICENSE("GPL");