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>
12 #include <asm/unaligned.h>
14 #include <net/bluetooth/bluetooth.h>
15 #include <net/bluetooth/hci_core.h>
21 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
22 #define BDADDR_BCM20702A1 (&(bdaddr_t) {{0x00, 0x00, 0xa0, 0x02, 0x70, 0x20}})
23 #define BDADDR_BCM2076B1 (&(bdaddr_t) {{0x79, 0x56, 0x00, 0xa0, 0x76, 0x20}})
24 #define BDADDR_BCM43430A0 (&(bdaddr_t) {{0xac, 0x1f, 0x12, 0xa0, 0x43, 0x43}})
25 #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
26 #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
27 #define BDADDR_BCM4334B0 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb0, 0x34, 0x43}})
28 #define BDADDR_BCM4345C5 (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0xc5, 0x45, 0x43}})
29 #define BDADDR_BCM43341B (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0x1b, 0x34, 0x43}})
31 #define BCM_FW_NAME_LEN 64
32 #define BCM_FW_NAME_COUNT_MAX 2
33 /* For kmalloc-ing the fw-name array instead of putting it on the stack */
34 typedef char bcm_fw_name[BCM_FW_NAME_LEN];
36 int btbcm_check_bdaddr(struct hci_dev *hdev)
38 struct hci_rp_read_bd_addr *bda;
41 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
44 int err = PTR_ERR(skb);
46 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
50 if (skb->len != sizeof(*bda)) {
51 bt_dev_err(hdev, "BCM: Device address length mismatch");
56 bda = (struct hci_rp_read_bd_addr *)skb->data;
58 /* Check if the address indicates a controller with either an
59 * invalid or default address. In both cases the device needs
60 * to be marked as not having a valid address.
62 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
63 * with no configured address.
65 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
66 * with no configured address.
68 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
69 * with no configured address.
71 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
72 * with waiting for configuration state.
74 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
75 * with waiting for configuration state.
77 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
78 * with no configured address.
80 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
81 !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
82 !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
83 !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
84 !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
85 !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
86 !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
87 !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
88 !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
89 bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
91 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
98 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
100 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
105 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
108 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
115 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
117 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
118 struct bcm_set_pcm_int_params *params)
123 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
126 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
130 if (skb->len != 6 || skb->data[0]) {
131 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
137 memcpy(params, skb->data + 1, 5);
143 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
145 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
146 const struct bcm_set_pcm_int_params *params)
151 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
154 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
161 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
163 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
165 const struct hci_command_hdr *cmd;
173 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
176 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
182 /* 50 msec delay after Download Minidrv completes */
188 while (fw_size >= sizeof(*cmd)) {
191 cmd = (struct hci_command_hdr *)fw_ptr;
192 fw_ptr += sizeof(*cmd);
193 fw_size -= sizeof(*cmd);
195 if (fw_size < cmd->plen) {
196 bt_dev_err(hdev, "BCM: Patch is corrupted");
203 fw_size -= cmd->plen;
205 opcode = le16_to_cpu(cmd->opcode);
207 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
211 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
218 /* 250 msec delay after Launch Ram completes */
224 EXPORT_SYMBOL(btbcm_patchram);
226 static int btbcm_reset(struct hci_dev *hdev)
230 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
232 int err = PTR_ERR(skb);
234 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
239 /* 100 msec delay for module to complete reset process */
245 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
249 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
252 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
257 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
258 bt_dev_err(hdev, "BCM: Local name length mismatch");
260 return ERR_PTR(-EIO);
266 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
270 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
273 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
278 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
279 bt_dev_err(hdev, "BCM: Local version length mismatch");
281 return ERR_PTR(-EIO);
287 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
291 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
293 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
299 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
301 return ERR_PTR(-EIO);
307 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
311 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
313 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
319 bt_dev_err(hdev, "BCM: Controller features length mismatch");
321 return ERR_PTR(-EIO);
327 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
331 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
333 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
339 bt_dev_err(hdev, "BCM: USB product length mismatch");
341 return ERR_PTR(-EIO);
347 static const struct dmi_system_id disable_broken_read_transmit_power[] = {
350 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
351 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,1"),
356 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
357 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,2"),
362 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
363 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro16,4"),
368 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
369 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,1"),
374 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
375 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir8,2"),
380 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
381 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,1"),
386 DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."),
387 DMI_MATCH(DMI_PRODUCT_NAME, "iMac20,2"),
393 static int btbcm_read_info(struct hci_dev *hdev)
397 /* Read Verbose Config Version Info */
398 skb = btbcm_read_verbose_config(hdev);
402 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
405 /* Read Controller Features */
406 skb = btbcm_read_controller_features(hdev);
410 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
413 /* Read DMI and disable broken Read LE Min/Max Tx Power */
414 if (dmi_first_match(disable_broken_read_transmit_power))
415 set_bit(HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER, &hdev->quirks);
420 static int btbcm_print_local_name(struct hci_dev *hdev)
424 /* Read Local Name */
425 skb = btbcm_read_local_name(hdev);
429 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
435 struct bcm_subver_table {
440 static const struct bcm_subver_table bcm_uart_subver_table[] = {
441 { 0x1111, "BCM4362A2" }, /* 000.017.017 */
442 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
443 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
444 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
445 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
446 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
447 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
448 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
449 { 0x610c, "BCM4354" }, /* 003.001.012 */
450 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
451 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
452 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
453 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
454 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
455 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
456 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
457 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
458 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
459 { 0x410c, "BCM43430B0" }, /* 002.001.012 */
463 static const struct bcm_subver_table bcm_usb_subver_table[] = {
464 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
465 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
466 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
467 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
468 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
469 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
470 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
471 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
472 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
473 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
474 { 0x610c, "BCM4354" }, /* 003.001.012 */
475 { 0x6607, "BCM4350C5" }, /* 003.006.007 */
479 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done)
481 u16 subver, rev, pid, vid;
483 struct hci_rp_read_local_version *ver;
484 const struct bcm_subver_table *bcm_subver_table;
485 const char *hw_name = NULL;
486 char postfix[16] = "";
487 int fw_name_count = 0;
488 bcm_fw_name *fw_name;
489 const struct firmware *fw;
493 err = btbcm_reset(hdev);
497 /* Read Local Version Info */
498 skb = btbcm_read_local_version(hdev);
502 ver = (struct hci_rp_read_local_version *)skb->data;
503 rev = le16_to_cpu(ver->hci_rev);
504 subver = le16_to_cpu(ver->lmp_subver);
507 /* Read controller information */
508 if (!(*fw_load_done)) {
509 err = btbcm_read_info(hdev);
513 err = btbcm_print_local_name(hdev);
517 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
518 bcm_uart_subver_table;
520 for (i = 0; bcm_subver_table[i].name; i++) {
521 if (subver == bcm_subver_table[i].subver) {
522 hw_name = bcm_subver_table[i].name;
527 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
528 hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
529 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
534 if (hdev->bus == HCI_USB) {
535 /* Read USB Product Info */
536 skb = btbcm_read_usb_product(hdev);
540 vid = get_unaligned_le16(skb->data + 1);
541 pid = get_unaligned_le16(skb->data + 3);
544 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
547 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
552 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
553 "brcm/%s%s.hcd", hw_name, postfix);
557 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
558 "brcm/BCM%s.hcd", postfix);
561 for (i = 0; i < fw_name_count; i++) {
562 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
564 bt_dev_info(hdev, "%s '%s' Patch",
565 hw_name ? hw_name : "BCM", fw_name[i]);
566 *fw_load_done = true;
572 err = btbcm_patchram(hdev, fw);
574 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
576 release_firmware(fw);
578 bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
579 for (i = 0; i < fw_name_count; i++)
580 bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
586 EXPORT_SYMBOL_GPL(btbcm_initialize);
588 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done)
592 /* Re-initialize if necessary */
594 err = btbcm_initialize(hdev, fw_load_done);
599 btbcm_check_bdaddr(hdev);
601 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
605 EXPORT_SYMBOL_GPL(btbcm_finalize);
607 int btbcm_setup_patchram(struct hci_dev *hdev)
609 bool fw_load_done = false;
613 err = btbcm_initialize(hdev, &fw_load_done);
617 /* Re-initialize after loading Patch */
618 return btbcm_finalize(hdev, &fw_load_done);
620 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
622 int btbcm_setup_apple(struct hci_dev *hdev)
628 err = btbcm_reset(hdev);
632 /* Read Verbose Config Version Info */
633 skb = btbcm_read_verbose_config(hdev);
635 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
636 skb->data[1], get_unaligned_le16(skb->data + 5));
640 /* Read USB Product Info */
641 skb = btbcm_read_usb_product(hdev);
643 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
644 get_unaligned_le16(skb->data + 1),
645 get_unaligned_le16(skb->data + 3));
649 /* Read Controller Features */
650 skb = btbcm_read_controller_features(hdev);
652 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
656 /* Read Local Name */
657 skb = btbcm_read_local_name(hdev);
659 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
663 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
667 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
669 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
670 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
671 MODULE_VERSION(VERSION);
672 MODULE_LICENSE("GPL");