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 <asm/unaligned.h>
13 #include <net/bluetooth/bluetooth.h>
14 #include <net/bluetooth/hci_core.h>
20 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
21 #define BDADDR_BCM20702A1 (&(bdaddr_t) {{0x00, 0x00, 0xa0, 0x02, 0x70, 0x20}})
22 #define BDADDR_BCM2076B1 (&(bdaddr_t) {{0x79, 0x56, 0x00, 0xa0, 0x76, 0x20}})
23 #define BDADDR_BCM43430A0 (&(bdaddr_t) {{0xac, 0x1f, 0x12, 0xa0, 0x43, 0x43}})
24 #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
25 #define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
26 #define BDADDR_BCM4334B0 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb0, 0x34, 0x43}})
27 #define BDADDR_BCM4345C5 (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0xc5, 0x45, 0x43}})
28 #define BDADDR_BCM43341B (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0x1b, 0x34, 0x43}})
30 #define BCM_FW_NAME_LEN 64
31 #define BCM_FW_NAME_COUNT_MAX 2
32 /* For kmalloc-ing the fw-name array instead of putting it on the stack */
33 typedef char bcm_fw_name
[BCM_FW_NAME_LEN
];
35 int btbcm_check_bdaddr(struct hci_dev
*hdev
)
37 struct hci_rp_read_bd_addr
*bda
;
40 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
43 int err
= PTR_ERR(skb
);
45 bt_dev_err(hdev
, "BCM: Reading device address failed (%d)", err
);
49 if (skb
->len
!= sizeof(*bda
)) {
50 bt_dev_err(hdev
, "BCM: Device address length mismatch");
55 bda
= (struct hci_rp_read_bd_addr
*)skb
->data
;
57 /* Check if the address indicates a controller with either an
58 * invalid or default address. In both cases the device needs
59 * to be marked as not having a valid address.
61 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
62 * with no configured address.
64 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
65 * with no configured address.
67 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
68 * with no configured address.
70 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
71 * with waiting for configuration state.
73 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
74 * with waiting for configuration state.
76 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
77 * with no configured address.
79 if (!bacmp(&bda
->bdaddr
, BDADDR_BCM20702A0
) ||
80 !bacmp(&bda
->bdaddr
, BDADDR_BCM20702A1
) ||
81 !bacmp(&bda
->bdaddr
, BDADDR_BCM2076B1
) ||
82 !bacmp(&bda
->bdaddr
, BDADDR_BCM4324B3
) ||
83 !bacmp(&bda
->bdaddr
, BDADDR_BCM4330B1
) ||
84 !bacmp(&bda
->bdaddr
, BDADDR_BCM4334B0
) ||
85 !bacmp(&bda
->bdaddr
, BDADDR_BCM4345C5
) ||
86 !bacmp(&bda
->bdaddr
, BDADDR_BCM43430A0
) ||
87 !bacmp(&bda
->bdaddr
, BDADDR_BCM43341B
)) {
88 bt_dev_info(hdev
, "BCM: Using default device address (%pMR)",
90 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
97 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr
);
99 int btbcm_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
104 skb
= __hci_cmd_sync(hdev
, 0xfc01, 6, bdaddr
, HCI_INIT_TIMEOUT
);
107 bt_dev_err(hdev
, "BCM: Change address command failed (%d)", err
);
114 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr
);
116 int btbcm_read_pcm_int_params(struct hci_dev
*hdev
,
117 struct bcm_set_pcm_int_params
*params
)
122 skb
= __hci_cmd_sync(hdev
, 0xfc1d, 0, NULL
, HCI_INIT_TIMEOUT
);
125 bt_dev_err(hdev
, "BCM: Read PCM int params failed (%d)", err
);
129 if (skb
->len
!= 6 || skb
->data
[0]) {
130 bt_dev_err(hdev
, "BCM: Read PCM int params length mismatch");
136 memcpy(params
, skb
->data
+ 1, 5);
142 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params
);
144 int btbcm_write_pcm_int_params(struct hci_dev
*hdev
,
145 const struct bcm_set_pcm_int_params
*params
)
150 skb
= __hci_cmd_sync(hdev
, 0xfc1c, 5, params
, HCI_INIT_TIMEOUT
);
153 bt_dev_err(hdev
, "BCM: Write PCM int params failed (%d)", err
);
160 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params
);
162 int btbcm_patchram(struct hci_dev
*hdev
, const struct firmware
*fw
)
164 const struct hci_command_hdr
*cmd
;
172 skb
= __hci_cmd_sync(hdev
, 0xfc2e, 0, NULL
, HCI_INIT_TIMEOUT
);
175 bt_dev_err(hdev
, "BCM: Download Minidrv command failed (%d)",
181 /* 50 msec delay after Download Minidrv completes */
187 while (fw_size
>= sizeof(*cmd
)) {
190 cmd
= (struct hci_command_hdr
*)fw_ptr
;
191 fw_ptr
+= sizeof(*cmd
);
192 fw_size
-= sizeof(*cmd
);
194 if (fw_size
< cmd
->plen
) {
195 bt_dev_err(hdev
, "BCM: Patch is corrupted");
202 fw_size
-= cmd
->plen
;
204 opcode
= le16_to_cpu(cmd
->opcode
);
206 skb
= __hci_cmd_sync(hdev
, opcode
, cmd
->plen
, cmd_param
,
210 bt_dev_err(hdev
, "BCM: Patch command %04x failed (%d)",
217 /* 250 msec delay after Launch Ram completes */
223 EXPORT_SYMBOL(btbcm_patchram
);
225 static int btbcm_reset(struct hci_dev
*hdev
)
229 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
231 int err
= PTR_ERR(skb
);
233 bt_dev_err(hdev
, "BCM: Reset failed (%d)", err
);
238 /* 100 msec delay for module to complete reset process */
244 static struct sk_buff
*btbcm_read_local_name(struct hci_dev
*hdev
)
248 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_NAME
, 0, NULL
,
251 bt_dev_err(hdev
, "BCM: Reading local name failed (%ld)",
256 if (skb
->len
!= sizeof(struct hci_rp_read_local_name
)) {
257 bt_dev_err(hdev
, "BCM: Local name length mismatch");
259 return ERR_PTR(-EIO
);
265 static struct sk_buff
*btbcm_read_local_version(struct hci_dev
*hdev
)
269 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
272 bt_dev_err(hdev
, "BCM: Reading local version info failed (%ld)",
277 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
278 bt_dev_err(hdev
, "BCM: Local version length mismatch");
280 return ERR_PTR(-EIO
);
286 static struct sk_buff
*btbcm_read_verbose_config(struct hci_dev
*hdev
)
290 skb
= __hci_cmd_sync(hdev
, 0xfc79, 0, NULL
, HCI_INIT_TIMEOUT
);
292 bt_dev_err(hdev
, "BCM: Read verbose config info failed (%ld)",
298 bt_dev_err(hdev
, "BCM: Verbose config length mismatch");
300 return ERR_PTR(-EIO
);
306 static struct sk_buff
*btbcm_read_controller_features(struct hci_dev
*hdev
)
310 skb
= __hci_cmd_sync(hdev
, 0xfc6e, 0, NULL
, HCI_INIT_TIMEOUT
);
312 bt_dev_err(hdev
, "BCM: Read controller features failed (%ld)",
318 bt_dev_err(hdev
, "BCM: Controller features length mismatch");
320 return ERR_PTR(-EIO
);
326 static struct sk_buff
*btbcm_read_usb_product(struct hci_dev
*hdev
)
330 skb
= __hci_cmd_sync(hdev
, 0xfc5a, 0, NULL
, HCI_INIT_TIMEOUT
);
332 bt_dev_err(hdev
, "BCM: Read USB product info failed (%ld)",
338 bt_dev_err(hdev
, "BCM: USB product length mismatch");
340 return ERR_PTR(-EIO
);
346 static int btbcm_read_info(struct hci_dev
*hdev
)
350 /* Read Verbose Config Version Info */
351 skb
= btbcm_read_verbose_config(hdev
);
355 bt_dev_info(hdev
, "BCM: chip id %u", skb
->data
[1]);
358 /* Read Controller Features */
359 skb
= btbcm_read_controller_features(hdev
);
363 bt_dev_info(hdev
, "BCM: features 0x%2.2x", skb
->data
[1]);
369 static int btbcm_print_local_name(struct hci_dev
*hdev
)
373 /* Read Local Name */
374 skb
= btbcm_read_local_name(hdev
);
378 bt_dev_info(hdev
, "%s", (char *)(skb
->data
+ 1));
384 struct bcm_subver_table
{
389 static const struct bcm_subver_table bcm_uart_subver_table
[] = {
390 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
391 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
392 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
393 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
394 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
395 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
396 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
397 { 0x610c, "BCM4354" }, /* 003.001.012 */
398 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
399 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
400 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
401 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
402 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
403 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
404 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
405 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
406 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
410 static const struct bcm_subver_table bcm_usb_subver_table
[] = {
411 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
412 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
413 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
414 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
415 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
416 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
417 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
418 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
419 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
420 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
421 { 0x610c, "BCM4354" }, /* 003.001.012 */
422 { 0x6607, "BCM4350C5" }, /* 003.006.007 */
426 int btbcm_initialize(struct hci_dev
*hdev
, bool *fw_load_done
)
428 u16 subver
, rev
, pid
, vid
;
430 struct hci_rp_read_local_version
*ver
;
431 const struct bcm_subver_table
*bcm_subver_table
;
432 const char *hw_name
= NULL
;
433 char postfix
[16] = "";
434 int fw_name_count
= 0;
435 bcm_fw_name
*fw_name
;
436 const struct firmware
*fw
;
440 err
= btbcm_reset(hdev
);
444 /* Read Local Version Info */
445 skb
= btbcm_read_local_version(hdev
);
449 ver
= (struct hci_rp_read_local_version
*)skb
->data
;
450 rev
= le16_to_cpu(ver
->hci_rev
);
451 subver
= le16_to_cpu(ver
->lmp_subver
);
454 /* Read controller information */
455 if (!(*fw_load_done
)) {
456 err
= btbcm_read_info(hdev
);
460 err
= btbcm_print_local_name(hdev
);
464 bcm_subver_table
= (hdev
->bus
== HCI_USB
) ? bcm_usb_subver_table
:
465 bcm_uart_subver_table
;
467 for (i
= 0; bcm_subver_table
[i
].name
; i
++) {
468 if (subver
== bcm_subver_table
[i
].subver
) {
469 hw_name
= bcm_subver_table
[i
].name
;
474 bt_dev_info(hdev
, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
475 hw_name
? hw_name
: "BCM", (subver
& 0xe000) >> 13,
476 (subver
& 0x1f00) >> 8, (subver
& 0x00ff), rev
& 0x0fff);
481 if (hdev
->bus
== HCI_USB
) {
482 /* Read USB Product Info */
483 skb
= btbcm_read_usb_product(hdev
);
487 vid
= get_unaligned_le16(skb
->data
+ 1);
488 pid
= get_unaligned_le16(skb
->data
+ 3);
491 snprintf(postfix
, sizeof(postfix
), "-%4.4x-%4.4x", vid
, pid
);
494 fw_name
= kmalloc(BCM_FW_NAME_COUNT_MAX
* BCM_FW_NAME_LEN
, GFP_KERNEL
);
499 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
500 "brcm/%s%s.hcd", hw_name
, postfix
);
504 snprintf(fw_name
[fw_name_count
], BCM_FW_NAME_LEN
,
505 "brcm/BCM%s.hcd", postfix
);
508 for (i
= 0; i
< fw_name_count
; i
++) {
509 err
= firmware_request_nowarn(&fw
, fw_name
[i
], &hdev
->dev
);
511 bt_dev_info(hdev
, "%s '%s' Patch",
512 hw_name
? hw_name
: "BCM", fw_name
[i
]);
513 *fw_load_done
= true;
519 err
= btbcm_patchram(hdev
, fw
);
521 bt_dev_info(hdev
, "BCM: Patch failed (%d)", err
);
523 release_firmware(fw
);
525 bt_dev_err(hdev
, "BCM: firmware Patch file not found, tried:");
526 for (i
= 0; i
< fw_name_count
; i
++)
527 bt_dev_err(hdev
, "BCM: '%s'", fw_name
[i
]);
533 EXPORT_SYMBOL_GPL(btbcm_initialize
);
535 int btbcm_finalize(struct hci_dev
*hdev
, bool *fw_load_done
)
539 /* Re-initialize if necessary */
541 err
= btbcm_initialize(hdev
, fw_load_done
);
546 btbcm_check_bdaddr(hdev
);
548 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
552 EXPORT_SYMBOL_GPL(btbcm_finalize
);
554 int btbcm_setup_patchram(struct hci_dev
*hdev
)
556 bool fw_load_done
= false;
560 err
= btbcm_initialize(hdev
, &fw_load_done
);
564 /* Re-initialize after loading Patch */
565 return btbcm_finalize(hdev
, &fw_load_done
);
567 EXPORT_SYMBOL_GPL(btbcm_setup_patchram
);
569 int btbcm_setup_apple(struct hci_dev
*hdev
)
575 err
= btbcm_reset(hdev
);
579 /* Read Verbose Config Version Info */
580 skb
= btbcm_read_verbose_config(hdev
);
582 bt_dev_info(hdev
, "BCM: chip id %u build %4.4u",
583 skb
->data
[1], get_unaligned_le16(skb
->data
+ 5));
587 /* Read USB Product Info */
588 skb
= btbcm_read_usb_product(hdev
);
590 bt_dev_info(hdev
, "BCM: product %4.4x:%4.4x",
591 get_unaligned_le16(skb
->data
+ 1),
592 get_unaligned_le16(skb
->data
+ 3));
596 /* Read Controller Features */
597 skb
= btbcm_read_controller_features(hdev
);
599 bt_dev_info(hdev
, "BCM: features 0x%2.2x", skb
->data
[1]);
603 /* Read Local Name */
604 skb
= btbcm_read_local_name(hdev
);
606 bt_dev_info(hdev
, "%s", (char *)(skb
->data
+ 1));
610 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
614 EXPORT_SYMBOL_GPL(btbcm_setup_apple
);
616 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
617 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION
);
618 MODULE_VERSION(VERSION
);
619 MODULE_LICENSE("GPL");