Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Bluetooth support for Broadcom devices |
| 4 | * |
| 5 | * Copyright (C) 2015 Intel Corporation |
| 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 25 | #include <linux/firmware.h> |
| 26 | #include <asm/unaligned.h> |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 27 | |
| 28 | #include <net/bluetooth/bluetooth.h> |
| 29 | #include <net/bluetooth/hci_core.h> |
| 30 | |
| 31 | #include "btbcm.h" |
| 32 | |
| 33 | #define VERSION "0.1" |
| 34 | |
| 35 | #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}}) |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 36 | #define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}}) |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 37 | |
| 38 | int btbcm_check_bdaddr(struct hci_dev *hdev) |
| 39 | { |
| 40 | struct hci_rp_read_bd_addr *bda; |
| 41 | struct sk_buff *skb; |
| 42 | |
| 43 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL, |
| 44 | HCI_INIT_TIMEOUT); |
| 45 | if (IS_ERR(skb)) { |
| 46 | int err = PTR_ERR(skb); |
| 47 | BT_ERR("%s: BCM: Reading device address failed (%d)", |
| 48 | hdev->name, err); |
| 49 | return err; |
| 50 | } |
| 51 | |
| 52 | if (skb->len != sizeof(*bda)) { |
| 53 | BT_ERR("%s: BCM: Device address length mismatch", hdev->name); |
| 54 | kfree_skb(skb); |
| 55 | return -EIO; |
| 56 | } |
| 57 | |
| 58 | bda = (struct hci_rp_read_bd_addr *)skb->data; |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 59 | |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 60 | /* Check if the address indicates a controller with either an |
| 61 | * invalid or default address. In both cases the device needs |
| 62 | * to be marked as not having a valid address. |
| 63 | * |
| 64 | * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 65 | * with no configured address. |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 66 | * |
| 67 | * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller |
| 68 | * with waiting for configuration state. |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 69 | */ |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 70 | if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) || |
| 71 | !bacmp(&bda->bdaddr, BDADDR_BCM4324B3)) { |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 72 | BT_INFO("%s: BCM: Using default device address (%pMR)", |
| 73 | hdev->name, &bda->bdaddr); |
| 74 | set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks); |
| 75 | } |
| 76 | |
| 77 | kfree_skb(skb); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(btbcm_check_bdaddr); |
| 82 | |
| 83 | int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) |
| 84 | { |
| 85 | struct sk_buff *skb; |
| 86 | int err; |
| 87 | |
| 88 | skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT); |
| 89 | if (IS_ERR(skb)) { |
| 90 | err = PTR_ERR(skb); |
| 91 | BT_ERR("%s: BCM: Change address command failed (%d)", |
| 92 | hdev->name, err); |
| 93 | return err; |
| 94 | } |
| 95 | kfree_skb(skb); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(btbcm_set_bdaddr); |
| 100 | |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 101 | int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw) |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 102 | { |
| 103 | const struct hci_command_hdr *cmd; |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 104 | const u8 *fw_ptr; |
| 105 | size_t fw_size; |
| 106 | struct sk_buff *skb; |
| 107 | u16 opcode; |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 108 | int err = 0; |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 109 | |
| 110 | /* Start Download */ |
| 111 | skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT); |
| 112 | if (IS_ERR(skb)) { |
| 113 | err = PTR_ERR(skb); |
| 114 | BT_ERR("%s: BCM: Download Minidrv command failed (%d)", |
| 115 | hdev->name, err); |
| 116 | goto done; |
| 117 | } |
| 118 | kfree_skb(skb); |
| 119 | |
| 120 | /* 50 msec delay after Download Minidrv completes */ |
| 121 | msleep(50); |
| 122 | |
| 123 | fw_ptr = fw->data; |
| 124 | fw_size = fw->size; |
| 125 | |
| 126 | while (fw_size >= sizeof(*cmd)) { |
| 127 | const u8 *cmd_param; |
| 128 | |
| 129 | cmd = (struct hci_command_hdr *)fw_ptr; |
| 130 | fw_ptr += sizeof(*cmd); |
| 131 | fw_size -= sizeof(*cmd); |
| 132 | |
| 133 | if (fw_size < cmd->plen) { |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 134 | BT_ERR("%s: BCM: Patch is corrupted", hdev->name); |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 135 | err = -EINVAL; |
| 136 | goto done; |
| 137 | } |
| 138 | |
| 139 | cmd_param = fw_ptr; |
| 140 | fw_ptr += cmd->plen; |
| 141 | fw_size -= cmd->plen; |
| 142 | |
| 143 | opcode = le16_to_cpu(cmd->opcode); |
| 144 | |
| 145 | skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param, |
| 146 | HCI_INIT_TIMEOUT); |
| 147 | if (IS_ERR(skb)) { |
| 148 | err = PTR_ERR(skb); |
| 149 | BT_ERR("%s: BCM: Patch command %04x failed (%d)", |
| 150 | hdev->name, opcode, err); |
| 151 | goto done; |
| 152 | } |
| 153 | kfree_skb(skb); |
| 154 | } |
| 155 | |
| 156 | /* 250 msec delay after Launch Ram completes */ |
| 157 | msleep(250); |
| 158 | |
| 159 | done: |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 160 | return err; |
| 161 | } |
| 162 | EXPORT_SYMBOL(btbcm_patchram); |
| 163 | |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 164 | static int btbcm_reset(struct hci_dev *hdev) |
| 165 | { |
| 166 | struct sk_buff *skb; |
| 167 | |
| 168 | skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT); |
| 169 | if (IS_ERR(skb)) { |
| 170 | int err = PTR_ERR(skb); |
| 171 | BT_ERR("%s: BCM: Reset failed (%d)", hdev->name, err); |
| 172 | return err; |
| 173 | } |
| 174 | kfree_skb(skb); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev) |
| 180 | { |
| 181 | struct sk_buff *skb; |
| 182 | |
| 183 | skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL, |
| 184 | HCI_INIT_TIMEOUT); |
| 185 | if (IS_ERR(skb)) { |
| 186 | BT_ERR("%s: BCM: Reading local version info failed (%ld)", |
| 187 | hdev->name, PTR_ERR(skb)); |
| 188 | return skb; |
| 189 | } |
| 190 | |
| 191 | if (skb->len != sizeof(struct hci_rp_read_local_version)) { |
| 192 | BT_ERR("%s: BCM: Local version length mismatch", hdev->name); |
| 193 | kfree_skb(skb); |
| 194 | return ERR_PTR(-EIO); |
| 195 | } |
| 196 | |
| 197 | return skb; |
| 198 | } |
| 199 | |
| 200 | static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev) |
| 201 | { |
| 202 | struct sk_buff *skb; |
| 203 | |
| 204 | skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT); |
| 205 | if (IS_ERR(skb)) { |
| 206 | BT_ERR("%s: BCM: Read verbose config info failed (%ld)", |
| 207 | hdev->name, PTR_ERR(skb)); |
| 208 | return skb; |
| 209 | } |
| 210 | |
| 211 | if (skb->len != 7) { |
| 212 | BT_ERR("%s: BCM: Verbose config length mismatch", hdev->name); |
| 213 | kfree_skb(skb); |
| 214 | return ERR_PTR(-EIO); |
| 215 | } |
| 216 | |
| 217 | return skb; |
| 218 | } |
| 219 | |
| 220 | static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev) |
| 221 | { |
| 222 | struct sk_buff *skb; |
| 223 | |
| 224 | skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT); |
| 225 | if (IS_ERR(skb)) { |
| 226 | BT_ERR("%s: BCM: Read USB product info failed (%ld)", |
| 227 | hdev->name, PTR_ERR(skb)); |
| 228 | return skb; |
| 229 | } |
| 230 | |
| 231 | if (skb->len != 5) { |
| 232 | BT_ERR("%s: BCM: USB product length mismatch", hdev->name); |
| 233 | kfree_skb(skb); |
| 234 | return ERR_PTR(-EIO); |
| 235 | } |
| 236 | |
| 237 | return skb; |
| 238 | } |
| 239 | |
| 240 | static const struct { |
| 241 | u16 subver; |
| 242 | const char *name; |
Marcel Holtmann | 9a0bb57 | 2015-04-05 22:52:19 -0700 | [diff] [blame] | 243 | } bcm_uart_subver_table[] = { |
| 244 | { 0x410e, "BCM43341B0" }, /* 002.001.014 */ |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 245 | { 0x4406, "BCM4324B3" }, /* 002.004.006 */ |
Marcel Holtmann | 9a0bb57 | 2015-04-05 22:52:19 -0700 | [diff] [blame] | 246 | { } |
| 247 | }; |
| 248 | |
| 249 | static const struct { |
| 250 | u16 subver; |
| 251 | const char *name; |
| 252 | } bcm_usb_subver_table[] = { |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 253 | { 0x210b, "BCM43142A0" }, /* 001.001.011 */ |
| 254 | { 0x2112, "BCM4314A0" }, /* 001.001.018 */ |
| 255 | { 0x2118, "BCM20702A0" }, /* 001.001.024 */ |
| 256 | { 0x2126, "BCM4335A0" }, /* 001.001.038 */ |
| 257 | { 0x220e, "BCM20702A1" }, /* 001.002.014 */ |
| 258 | { 0x230f, "BCM4354A2" }, /* 001.003.015 */ |
| 259 | { 0x4106, "BCM4335B0" }, /* 002.001.006 */ |
| 260 | { 0x410e, "BCM20702B0" }, /* 002.001.014 */ |
| 261 | { 0x6109, "BCM4335C0" }, /* 003.001.009 */ |
| 262 | { 0x610c, "BCM4354" }, /* 003.001.012 */ |
| 263 | { } |
| 264 | }; |
| 265 | |
| 266 | int btbcm_setup_patchram(struct hci_dev *hdev) |
| 267 | { |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 268 | char fw_name[64]; |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 269 | const struct firmware *fw; |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 270 | u16 subver, rev, pid, vid; |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 271 | const char *hw_name = NULL; |
| 272 | struct sk_buff *skb; |
| 273 | struct hci_rp_read_local_version *ver; |
| 274 | int i, err; |
| 275 | |
| 276 | /* Reset */ |
| 277 | err = btbcm_reset(hdev); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | /* Read Local Version Info */ |
| 282 | skb = btbcm_read_local_version(hdev); |
| 283 | if (IS_ERR(skb)) |
| 284 | return PTR_ERR(skb); |
| 285 | |
| 286 | ver = (struct hci_rp_read_local_version *)skb->data; |
| 287 | rev = le16_to_cpu(ver->hci_rev); |
| 288 | subver = le16_to_cpu(ver->lmp_subver); |
| 289 | kfree_skb(skb); |
| 290 | |
| 291 | /* Read Verbose Config Version Info */ |
| 292 | skb = btbcm_read_verbose_config(hdev); |
| 293 | if (IS_ERR(skb)) |
| 294 | return PTR_ERR(skb); |
| 295 | |
| 296 | BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]); |
| 297 | kfree_skb(skb); |
| 298 | |
Marcel Holtmann | 9a0bb57 | 2015-04-05 22:52:19 -0700 | [diff] [blame] | 299 | switch ((rev & 0xf000) >> 12) { |
| 300 | case 0: |
Frederic Danis | a8f3b94 | 2015-05-28 11:25:02 +0200 | [diff] [blame^] | 301 | case 3: |
Marcel Holtmann | 9a0bb57 | 2015-04-05 22:52:19 -0700 | [diff] [blame] | 302 | for (i = 0; bcm_uart_subver_table[i].name; i++) { |
| 303 | if (subver == bcm_uart_subver_table[i].subver) { |
| 304 | hw_name = bcm_uart_subver_table[i].name; |
| 305 | break; |
| 306 | } |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 307 | } |
Marcel Holtmann | 9a0bb57 | 2015-04-05 22:52:19 -0700 | [diff] [blame] | 308 | |
| 309 | snprintf(fw_name, sizeof(fw_name), "brcm/%s.hcd", |
| 310 | hw_name ? : "BCM"); |
| 311 | break; |
| 312 | case 1: |
| 313 | case 2: |
| 314 | /* Read USB Product Info */ |
| 315 | skb = btbcm_read_usb_product(hdev); |
| 316 | if (IS_ERR(skb)) |
| 317 | return PTR_ERR(skb); |
| 318 | |
| 319 | vid = get_unaligned_le16(skb->data + 1); |
| 320 | pid = get_unaligned_le16(skb->data + 3); |
| 321 | kfree_skb(skb); |
| 322 | |
| 323 | for (i = 0; bcm_usb_subver_table[i].name; i++) { |
| 324 | if (subver == bcm_usb_subver_table[i].subver) { |
| 325 | hw_name = bcm_usb_subver_table[i].name; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd", |
| 331 | hw_name ? : "BCM", vid, pid); |
| 332 | break; |
| 333 | default: |
| 334 | return 0; |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name, |
| 338 | hw_name ? : "BCM", (subver & 0x7000) >> 13, |
| 339 | (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff); |
| 340 | |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 341 | err = request_firmware(&fw, fw_name, &hdev->dev); |
| 342 | if (err < 0) { |
| 343 | BT_INFO("%s: BCM: Patch %s not found", hdev->name, fw_name); |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 344 | return 0; |
Frederic Danis | 18aeb44 | 2015-05-28 11:25:01 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | btbcm_patchram(hdev, fw); |
| 348 | |
| 349 | release_firmware(fw); |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 350 | |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 351 | /* Reset */ |
| 352 | err = btbcm_reset(hdev); |
| 353 | if (err) |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 354 | return err; |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 355 | |
| 356 | /* Read Local Version Info */ |
| 357 | skb = btbcm_read_local_version(hdev); |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 358 | if (IS_ERR(skb)) |
| 359 | return PTR_ERR(skb); |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 360 | |
| 361 | ver = (struct hci_rp_read_local_version *)skb->data; |
| 362 | rev = le16_to_cpu(ver->hci_rev); |
| 363 | subver = le16_to_cpu(ver->lmp_subver); |
| 364 | kfree_skb(skb); |
| 365 | |
| 366 | BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name, |
| 367 | hw_name ? : "BCM", (subver & 0x7000) >> 13, |
| 368 | (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff); |
| 369 | |
| 370 | btbcm_check_bdaddr(hdev); |
| 371 | |
Marcel Holtmann | 941521e | 2015-04-05 22:52:15 -0700 | [diff] [blame] | 372 | set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); |
| 373 | |
Marcel Holtmann | 50862ee | 2015-04-10 14:02:20 -0700 | [diff] [blame] | 374 | return 0; |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 375 | } |
| 376 | EXPORT_SYMBOL_GPL(btbcm_setup_patchram); |
| 377 | |
| 378 | int btbcm_setup_apple(struct hci_dev *hdev) |
| 379 | { |
| 380 | struct sk_buff *skb; |
| 381 | |
| 382 | /* Read Verbose Config Version Info */ |
| 383 | skb = btbcm_read_verbose_config(hdev); |
| 384 | if (IS_ERR(skb)) |
| 385 | return PTR_ERR(skb); |
| 386 | |
| 387 | BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1], |
| 388 | get_unaligned_le16(skb->data + 5)); |
| 389 | kfree_skb(skb); |
| 390 | |
Marcel Holtmann | 941521e | 2015-04-05 22:52:15 -0700 | [diff] [blame] | 391 | set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks); |
| 392 | |
Marcel Holtmann | 1c8ba6d | 2015-04-05 22:52:13 -0700 | [diff] [blame] | 393 | return 0; |
| 394 | } |
| 395 | EXPORT_SYMBOL_GPL(btbcm_setup_apple); |
| 396 | |
Marcel Holtmann | 4fba30f | 2015-04-05 22:52:10 -0700 | [diff] [blame] | 397 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 398 | MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION); |
| 399 | MODULE_VERSION(VERSION); |
| 400 | MODULE_LICENSE("GPL"); |