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 <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];
37 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
39 efi_guid_t guid = EFI_GUID(0x74b00bd9, 0x805a, 0x4d61, 0xb5, 0x1f,
40 0x43, 0x26, 0x81, 0x23, 0xd1, 0x13);
41 bdaddr_t efi_bdaddr, bdaddr;
46 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE))
49 len = sizeof(efi_bdaddr);
50 status = efi.get_variable(L"BDADDR", &guid, NULL, &len, &efi_bdaddr);
51 if (status != EFI_SUCCESS)
54 if (len != sizeof(efi_bdaddr))
57 baswap(&bdaddr, &efi_bdaddr);
59 ret = btbcm_set_bdaddr(hdev, &bdaddr);
63 bt_dev_info(hdev, "BCM: Using EFI device address (%pMR)", &bdaddr);
67 static int btbcm_set_bdaddr_from_efi(struct hci_dev *hdev)
73 int btbcm_check_bdaddr(struct hci_dev *hdev)
75 struct hci_rp_read_bd_addr *bda;
78 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
81 int err = PTR_ERR(skb);
83 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
87 if (skb->len != sizeof(*bda)) {
88 bt_dev_err(hdev, "BCM: Device address length mismatch");
93 bda = (struct hci_rp_read_bd_addr *)skb->data;
95 /* Check if the address indicates a controller with either an
96 * invalid or default address. In both cases the device needs
97 * to be marked as not having a valid address.
99 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
100 * with no configured address.
102 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
103 * with no configured address.
105 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
106 * with no configured address.
108 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
109 * with waiting for configuration state.
111 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
112 * with waiting for configuration state.
114 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
115 * with no configured address.
117 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
118 !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
119 !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
120 !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
121 !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
122 !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
123 !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
124 !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
125 !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
126 /* Try falling back to BDADDR EFI variable */
127 if (btbcm_set_bdaddr_from_efi(hdev) != 0) {
128 bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
130 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
138 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
140 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
145 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
148 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
155 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
157 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
158 struct bcm_set_pcm_int_params *params)
163 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
166 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
170 if (skb->len != 6 || skb->data[0]) {
171 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
177 memcpy(params, skb->data + 1, 5);
183 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
185 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
186 const struct bcm_set_pcm_int_params *params)
191 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
194 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
201 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
203 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
205 const struct hci_command_hdr *cmd;
213 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
216 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
222 /* 50 msec delay after Download Minidrv completes */
228 while (fw_size >= sizeof(*cmd)) {
231 cmd = (struct hci_command_hdr *)fw_ptr;
232 fw_ptr += sizeof(*cmd);
233 fw_size -= sizeof(*cmd);
235 if (fw_size < cmd->plen) {
236 bt_dev_err(hdev, "BCM: Patch is corrupted");
243 fw_size -= cmd->plen;
245 opcode = le16_to_cpu(cmd->opcode);
247 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
251 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
258 /* 250 msec delay after Launch Ram completes */
264 EXPORT_SYMBOL(btbcm_patchram);
266 static int btbcm_reset(struct hci_dev *hdev)
270 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
272 int err = PTR_ERR(skb);
274 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
279 /* 100 msec delay for module to complete reset process */
285 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
289 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
292 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
297 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
298 bt_dev_err(hdev, "BCM: Local name length mismatch");
300 return ERR_PTR(-EIO);
306 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
310 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
313 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
318 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
319 bt_dev_err(hdev, "BCM: Local version length mismatch");
321 return ERR_PTR(-EIO);
327 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
331 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
333 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
339 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
341 return ERR_PTR(-EIO);
347 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
351 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
353 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
359 bt_dev_err(hdev, "BCM: Controller features length mismatch");
361 return ERR_PTR(-EIO);
367 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
371 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
373 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
379 bt_dev_err(hdev, "BCM: USB product length mismatch");
381 return ERR_PTR(-EIO);
387 static int btbcm_read_info(struct hci_dev *hdev)
391 /* Read Verbose Config Version Info */
392 skb = btbcm_read_verbose_config(hdev);
396 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
399 /* Read Controller Features */
400 skb = btbcm_read_controller_features(hdev);
404 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
410 static int btbcm_print_local_name(struct hci_dev *hdev)
414 /* Read Local Name */
415 skb = btbcm_read_local_name(hdev);
419 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
425 struct bcm_subver_table {
430 static const struct bcm_subver_table bcm_uart_subver_table[] = {
431 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
432 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
433 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
434 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
435 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
436 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
437 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
438 { 0x610c, "BCM4354" }, /* 003.001.012 */
439 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
440 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
441 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
442 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
443 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
444 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
445 { 0x420d, "BCM4349B1" }, /* 002.002.013 */
446 { 0x420e, "BCM4349B1" }, /* 002.002.014 */
447 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
448 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
449 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
453 static const struct bcm_subver_table bcm_usb_subver_table[] = {
454 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
455 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
456 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
457 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
458 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
459 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
460 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
461 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
462 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
463 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
464 { 0x610c, "BCM4354" }, /* 003.001.012 */
465 { 0x6607, "BCM4350C5" }, /* 003.006.007 */
469 int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done)
471 u16 subver, rev, pid, vid;
473 struct hci_rp_read_local_version *ver;
474 const struct bcm_subver_table *bcm_subver_table;
475 const char *hw_name = NULL;
476 char postfix[16] = "";
477 int fw_name_count = 0;
478 bcm_fw_name *fw_name;
479 const struct firmware *fw;
483 err = btbcm_reset(hdev);
487 /* Read Local Version Info */
488 skb = btbcm_read_local_version(hdev);
492 ver = (struct hci_rp_read_local_version *)skb->data;
493 rev = le16_to_cpu(ver->hci_rev);
494 subver = le16_to_cpu(ver->lmp_subver);
497 /* Read controller information */
498 if (!(*fw_load_done)) {
499 err = btbcm_read_info(hdev);
503 err = btbcm_print_local_name(hdev);
507 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
508 bcm_uart_subver_table;
510 for (i = 0; bcm_subver_table[i].name; i++) {
511 if (subver == bcm_subver_table[i].subver) {
512 hw_name = bcm_subver_table[i].name;
517 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
518 hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
519 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
524 if (hdev->bus == HCI_USB) {
525 /* Read USB Product Info */
526 skb = btbcm_read_usb_product(hdev);
530 vid = get_unaligned_le16(skb->data + 1);
531 pid = get_unaligned_le16(skb->data + 3);
534 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
537 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
542 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
543 "/*(DEBLOBBED)*/", hw_name, postfix);
547 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
548 "/*(DEBLOBBED)*/", postfix);
551 for (i = 0; i < fw_name_count; i++) {
552 err = firmware_reject_nowarn(&fw, fw_name[i], &hdev->dev);
554 bt_dev_info(hdev, "%s '%s' Patch",
555 hw_name ? hw_name : "BCM", fw_name[i]);
556 *fw_load_done = true;
562 err = btbcm_patchram(hdev, fw);
564 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
566 release_firmware(fw);
568 bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
569 for (i = 0; i < fw_name_count; i++)
570 bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
576 EXPORT_SYMBOL_GPL(btbcm_initialize);
578 int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done)
582 /* Re-initialize if necessary */
584 err = btbcm_initialize(hdev, fw_load_done);
589 btbcm_check_bdaddr(hdev);
591 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
595 EXPORT_SYMBOL_GPL(btbcm_finalize);
597 int btbcm_setup_patchram(struct hci_dev *hdev)
599 bool fw_load_done = false;
603 err = btbcm_initialize(hdev, &fw_load_done);
607 /* Re-initialize after loading Patch */
608 return btbcm_finalize(hdev, &fw_load_done);
610 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
612 int btbcm_setup_apple(struct hci_dev *hdev)
618 err = btbcm_reset(hdev);
622 /* Read Verbose Config Version Info */
623 skb = btbcm_read_verbose_config(hdev);
625 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
626 skb->data[1], get_unaligned_le16(skb->data + 5));
630 /* Read USB Product Info */
631 skb = btbcm_read_usb_product(hdev);
633 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
634 get_unaligned_le16(skb->data + 1),
635 get_unaligned_le16(skb->data + 3));
639 /* Read Controller Features */
640 skb = btbcm_read_controller_features(hdev);
642 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
646 /* Read Local Name */
647 skb = btbcm_read_local_name(hdev);
649 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
653 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
657 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
659 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
660 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
661 MODULE_VERSION(VERSION);
662 MODULE_LICENSE("GPL");