blob: df7a8a22e53c9a42acc70e36927fb8f25738d4ea [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Marcel Holtmann4fba30f2015-04-05 22:52:10 -07002/*
3 *
4 * Bluetooth support for Broadcom devices
5 *
6 * Copyright (C) 2015 Intel Corporation
Marcel Holtmann4fba30f2015-04-05 22:52:10 -07007 */
8
9#include <linux/module.h>
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -070010#include <linux/firmware.h>
11#include <asm/unaligned.h>
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070012
13#include <net/bluetooth/bluetooth.h>
14#include <net/bluetooth/hci_core.h>
15
16#include "btbcm.h"
17
18#define VERSION "0.1"
19
20#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
Maxime Ripard92ffe0d2018-12-17 12:04:45 +080021#define BDADDR_BCM20702A1 (&(bdaddr_t) {{0x00, 0x00, 0xa0, 0x02, 0x70, 0x20}})
Stephan Gerhold300926b2019-05-01 09:18:23 +020022#define BDADDR_BCM2076B1 (&(bdaddr_t) {{0x79, 0x56, 0x00, 0xa0, 0x76, 0x20}})
Chen-Yu Tsai0697607a2018-12-17 12:04:47 +080023#define BDADDR_BCM43430A0 (&(bdaddr_t) {{0xac, 0x1f, 0x12, 0xa0, 0x43, 0x43}})
Frederic Danisa8f3b942015-05-28 11:25:02 +020024#define BDADDR_BCM4324B3 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb3, 0x24, 0x43}})
Frederic Danis4a546ec2015-07-27 18:02:25 +020025#define BDADDR_BCM4330B1 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb1, 0x30, 0x43}})
Stephan Gerholdb8dc6472019-11-17 21:39:46 +010026#define BDADDR_BCM4334B0 (&(bdaddr_t) {{0x00, 0x00, 0x00, 0xb0, 0x34, 0x43}})
Ondrej Jirman52c8c7a2019-08-23 12:31:36 +020027#define BDADDR_BCM4345C5 (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0xc5, 0x45, 0x43}})
Ferry Toth50357262019-04-09 16:15:50 +020028#define BDADDR_BCM43341B (&(bdaddr_t) {{0xac, 0x1f, 0x00, 0x1b, 0x34, 0x43}})
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070029
Hans de Goede0287c5d2020-04-17 19:15:27 +020030#define BCM_FW_NAME_LEN 64
Hans de Goede74530a62020-04-17 19:15:31 +020031#define BCM_FW_NAME_COUNT_MAX 2
32/* For kmalloc-ing the fw-name array instead of putting it on the stack */
33typedef char bcm_fw_name[BCM_FW_NAME_LEN];
Hans de Goede0287c5d2020-04-17 19:15:27 +020034
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070035int btbcm_check_bdaddr(struct hci_dev *hdev)
36{
37 struct hci_rp_read_bd_addr *bda;
38 struct sk_buff *skb;
39
40 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
41 HCI_INIT_TIMEOUT);
42 if (IS_ERR(skb)) {
43 int err = PTR_ERR(skb);
Changqi Dudde80102020-01-08 23:40:19 -050044
Marcel Holtmann2064ee32017-10-30 10:42:59 +010045 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070046 return err;
47 }
48
49 if (skb->len != sizeof(*bda)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +010050 bt_dev_err(hdev, "BCM: Device address length mismatch");
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070051 kfree_skb(skb);
52 return -EIO;
53 }
54
55 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070056
Frederic Danisa8f3b942015-05-28 11:25:02 +020057 /* 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.
60 *
61 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070062 * with no configured address.
Frederic Danisa8f3b942015-05-28 11:25:02 +020063 *
Maxime Ripard92ffe0d2018-12-17 12:04:45 +080064 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
65 * with no configured address.
66 *
Stephan Gerhold300926b2019-05-01 09:18:23 +020067 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
68 * with no configured address.
69 *
Frederic Danisa8f3b942015-05-28 11:25:02 +020070 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
71 * with waiting for configuration state.
Frederic Danis4a546ec2015-07-27 18:02:25 +020072 *
73 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
74 * with waiting for configuration state.
Chen-Yu Tsai0697607a2018-12-17 12:04:47 +080075 *
76 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
77 * with no configured address.
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070078 */
Frederic Danisa8f3b942015-05-28 11:25:02 +020079 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
Maxime Ripard92ffe0d2018-12-17 12:04:45 +080080 !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
Stephan Gerhold300926b2019-05-01 09:18:23 +020081 !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
Frederic Danis4a546ec2015-07-27 18:02:25 +020082 !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
Chen-Yu Tsai0697607a2018-12-17 12:04:47 +080083 !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
Stephan Gerholdb8dc6472019-11-17 21:39:46 +010084 !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
Ondrej Jirman52c8c7a2019-08-23 12:31:36 +020085 !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
Ferry Toth50357262019-04-09 16:15:50 +020086 !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
87 !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +010088 bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
89 &bda->bdaddr);
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070090 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
91 }
92
93 kfree_skb(skb);
94
95 return 0;
96}
97EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
98
99int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
100{
101 struct sk_buff *skb;
102 int err;
103
104 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
105 if (IS_ERR(skb)) {
106 err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100107 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
Marcel Holtmann4fba30f2015-04-05 22:52:10 -0700108 return err;
109 }
110 kfree_skb(skb);
111
112 return 0;
113}
114EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
115
Abhishek Pandit-Subedi52837992019-11-26 08:17:30 +0100116int btbcm_read_pcm_int_params(struct hci_dev *hdev,
117 struct bcm_set_pcm_int_params *params)
118{
119 struct sk_buff *skb;
120 int err = 0;
121
122 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
123 if (IS_ERR(skb)) {
124 err = PTR_ERR(skb);
125 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
126 return err;
127 }
128
129 if (skb->len != 6 || skb->data[0]) {
130 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
131 kfree_skb(skb);
132 return -EIO;
133 }
134
135 if (params)
136 memcpy(params, skb->data + 1, 5);
137
138 kfree_skb(skb);
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
143
144int btbcm_write_pcm_int_params(struct hci_dev *hdev,
145 const struct bcm_set_pcm_int_params *params)
146{
147 struct sk_buff *skb;
148 int err;
149
150 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
151 if (IS_ERR(skb)) {
152 err = PTR_ERR(skb);
153 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
154 return err;
155 }
156 kfree_skb(skb);
157
158 return 0;
159}
160EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
161
Frederic Danis18aeb442015-05-28 11:25:01 +0200162int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700163{
164 const struct hci_command_hdr *cmd;
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700165 const u8 *fw_ptr;
166 size_t fw_size;
167 struct sk_buff *skb;
168 u16 opcode;
Frederic Danis18aeb442015-05-28 11:25:01 +0200169 int err = 0;
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700170
171 /* Start Download */
172 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
173 if (IS_ERR(skb)) {
174 err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100175 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
176 err);
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700177 goto done;
178 }
179 kfree_skb(skb);
180
181 /* 50 msec delay after Download Minidrv completes */
182 msleep(50);
183
184 fw_ptr = fw->data;
185 fw_size = fw->size;
186
187 while (fw_size >= sizeof(*cmd)) {
188 const u8 *cmd_param;
189
190 cmd = (struct hci_command_hdr *)fw_ptr;
191 fw_ptr += sizeof(*cmd);
192 fw_size -= sizeof(*cmd);
193
194 if (fw_size < cmd->plen) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100195 bt_dev_err(hdev, "BCM: Patch is corrupted");
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700196 err = -EINVAL;
197 goto done;
198 }
199
200 cmd_param = fw_ptr;
201 fw_ptr += cmd->plen;
202 fw_size -= cmd->plen;
203
204 opcode = le16_to_cpu(cmd->opcode);
205
206 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
207 HCI_INIT_TIMEOUT);
208 if (IS_ERR(skb)) {
209 err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100210 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
211 opcode, err);
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700212 goto done;
213 }
214 kfree_skb(skb);
215 }
216
217 /* 250 msec delay after Launch Ram completes */
218 msleep(250);
219
220done:
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700221 return err;
222}
223EXPORT_SYMBOL(btbcm_patchram);
224
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700225static int btbcm_reset(struct hci_dev *hdev)
226{
227 struct sk_buff *skb;
228
229 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
230 if (IS_ERR(skb)) {
231 int err = PTR_ERR(skb);
Changqi Dudde80102020-01-08 23:40:19 -0500232
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100233 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700234 return err;
235 }
236 kfree_skb(skb);
237
Wen-chien Jesse Sung3af3a592017-01-10 11:46:28 +0800238 /* 100 msec delay for module to complete reset process */
239 msleep(100);
240
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700241 return 0;
242}
243
Marcel Holtmann9bc63ca2015-10-05 12:43:57 +0200244static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
245{
246 struct sk_buff *skb;
247
248 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
249 HCI_INIT_TIMEOUT);
250 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100251 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
252 PTR_ERR(skb));
Marcel Holtmann9bc63ca2015-10-05 12:43:57 +0200253 return skb;
254 }
255
256 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100257 bt_dev_err(hdev, "BCM: Local name length mismatch");
Marcel Holtmann9bc63ca2015-10-05 12:43:57 +0200258 kfree_skb(skb);
259 return ERR_PTR(-EIO);
260 }
261
262 return skb;
263}
264
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700265static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
266{
267 struct sk_buff *skb;
268
269 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
270 HCI_INIT_TIMEOUT);
271 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100272 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
273 PTR_ERR(skb));
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700274 return skb;
275 }
276
277 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100278 bt_dev_err(hdev, "BCM: Local version length mismatch");
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700279 kfree_skb(skb);
280 return ERR_PTR(-EIO);
281 }
282
283 return skb;
284}
285
286static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
287{
288 struct sk_buff *skb;
289
290 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
291 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100292 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
293 PTR_ERR(skb));
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700294 return skb;
295 }
296
297 if (skb->len != 7) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100298 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700299 kfree_skb(skb);
300 return ERR_PTR(-EIO);
301 }
302
303 return skb;
304}
305
Marcel Holtmann4284ecb2017-06-10 14:33:16 +0200306static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
307{
308 struct sk_buff *skb;
309
310 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
311 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100312 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
313 PTR_ERR(skb));
Marcel Holtmann4284ecb2017-06-10 14:33:16 +0200314 return skb;
315 }
316
317 if (skb->len != 9) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100318 bt_dev_err(hdev, "BCM: Controller features length mismatch");
Marcel Holtmann4284ecb2017-06-10 14:33:16 +0200319 kfree_skb(skb);
320 return ERR_PTR(-EIO);
321 }
322
323 return skb;
324}
325
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700326static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
327{
328 struct sk_buff *skb;
329
330 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
331 if (IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100332 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
333 PTR_ERR(skb));
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700334 return skb;
335 }
336
337 if (skb->len != 5) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100338 bt_dev_err(hdev, "BCM: USB product length mismatch");
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700339 kfree_skb(skb);
340 return ERR_PTR(-EIO);
341 }
342
343 return skb;
344}
345
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200346static int btbcm_read_info(struct hci_dev *hdev)
347{
348 struct sk_buff *skb;
349
350 /* Read Verbose Config Version Info */
351 skb = btbcm_read_verbose_config(hdev);
352 if (IS_ERR(skb))
353 return PTR_ERR(skb);
354
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100355 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200356 kfree_skb(skb);
357
358 /* Read Controller Features */
359 skb = btbcm_read_controller_features(hdev);
360 if (IS_ERR(skb))
361 return PTR_ERR(skb);
362
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100363 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200364 kfree_skb(skb);
365
Hans de Goede2fcdd562020-04-17 19:15:28 +0200366 return 0;
367}
368
369static int btbcm_print_local_name(struct hci_dev *hdev)
370{
371 struct sk_buff *skb;
372
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200373 /* Read Local Name */
374 skb = btbcm_read_local_name(hdev);
375 if (IS_ERR(skb))
376 return PTR_ERR(skb);
377
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100378 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200379 kfree_skb(skb);
380
381 return 0;
382}
383
Hans de Goedef7328382018-04-20 14:44:03 +0200384struct bcm_subver_table {
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700385 u16 subver;
386 const char *name;
Hans de Goedef7328382018-04-20 14:44:03 +0200387};
388
389static const struct bcm_subver_table bcm_uart_subver_table[] = {
Frederic Danis4a546ec2015-07-27 18:02:25 +0200390 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
Stephan Gerholdb8dc6472019-11-17 21:39:46 +0100391 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
Marcel Holtmann9a0bb572015-04-05 22:52:19 -0700392 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
Stephan Gerhold039287a2019-03-05 14:09:00 +0100393 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
Frederic Danisa8f3b942015-05-28 11:25:02 +0200394 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
Hans de Goedec03ee9a2020-04-17 19:15:32 +0200395 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
Christian Hewitta357ea02018-09-04 21:50:57 +0400396 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
Ilya Faenson40db5f02015-06-17 11:42:46 -0400397 { 0x610c, "BCM4354" }, /* 003.001.012 */
Jörg Kraused456f672018-01-18 10:21:55 +0100398 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
Ian Moltonfeb16722017-06-28 20:10:55 +0100399 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
Ian W MORRISON18a39b92017-10-06 18:34:18 +1100400 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
Ondrej Jirman52c8c7a2019-08-23 12:31:36 +0200401 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
Hans de Goedeb133e0c2017-10-13 17:54:01 +0200402 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
Maxime Ripard92ffe0d2018-12-17 12:04:45 +0800403 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
Paweł Chmiele3ca60d2018-12-19 12:22:46 +0100404 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
Neil Armstrongf4d297e2019-05-20 15:41:04 +0200405 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
Mohammad Rasim1199ab42019-11-20 14:02:35 +0300406 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
Marcel Holtmann9a0bb572015-04-05 22:52:19 -0700407 { }
408};
409
Hans de Goedefec0de22018-04-20 14:44:04 +0200410static const struct bcm_subver_table bcm_usb_subver_table[] = {
Hans de Goedec03ee9a2020-04-17 19:15:32 +0200411 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
Hans de Goedefec0de22018-04-20 14:44:04 +0200412 { 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, "BCM4354A2" }, /* 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 { }
423};
424
Hans de Goede0287c5d2020-04-17 19:15:27 +0200425int btbcm_initialize(struct hci_dev *hdev, bool *fw_load_done)
Frederic Danis75e167e2015-05-28 11:25:04 +0200426{
Hans de Goedefec0de22018-04-20 14:44:04 +0200427 u16 subver, rev, pid, vid;
Frederic Danis75e167e2015-05-28 11:25:04 +0200428 struct sk_buff *skb;
429 struct hci_rp_read_local_version *ver;
Hans de Goedefec0de22018-04-20 14:44:04 +0200430 const struct bcm_subver_table *bcm_subver_table;
Hans de Goede74530a62020-04-17 19:15:31 +0200431 const char *hw_name = NULL;
432 char postfix[16] = "";
433 int fw_name_count = 0;
434 bcm_fw_name *fw_name;
Hans de Goede0287c5d2020-04-17 19:15:27 +0200435 const struct firmware *fw;
Frederic Danis75e167e2015-05-28 11:25:04 +0200436 int i, err;
437
438 /* Reset */
439 err = btbcm_reset(hdev);
440 if (err)
441 return err;
442
443 /* Read Local Version Info */
444 skb = btbcm_read_local_version(hdev);
445 if (IS_ERR(skb))
446 return PTR_ERR(skb);
447
448 ver = (struct hci_rp_read_local_version *)skb->data;
449 rev = le16_to_cpu(ver->hci_rev);
450 subver = le16_to_cpu(ver->lmp_subver);
451 kfree_skb(skb);
452
Marcel Holtmanne76dc1d2017-08-17 11:02:40 +0200453 /* Read controller information */
Hans de Goede0287c5d2020-04-17 19:15:27 +0200454 if (!(*fw_load_done)) {
Hans de Goede22ac1912018-04-20 14:44:05 +0200455 err = btbcm_read_info(hdev);
456 if (err)
457 return err;
458 }
Hans de Goede2fcdd562020-04-17 19:15:28 +0200459 err = btbcm_print_local_name(hdev);
460 if (err)
461 return err;
Frederic Danis75e167e2015-05-28 11:25:04 +0200462
Hans de Goedefec0de22018-04-20 14:44:04 +0200463 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
464 bcm_uart_subver_table;
465
466 for (i = 0; bcm_subver_table[i].name; i++) {
467 if (subver == bcm_subver_table[i].subver) {
468 hw_name = bcm_subver_table[i].name;
469 break;
470 }
471 }
472
Hans de Goedef53b975c2020-04-17 19:15:30 +0200473 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
Hans de Goede74530a62020-04-17 19:15:31 +0200474 hw_name ? hw_name : "BCM", (subver & 0xe000) >> 13,
Hans de Goedef53b975c2020-04-17 19:15:30 +0200475 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
476
477 if (*fw_load_done)
478 return 0;
479
Hans de Goedefec0de22018-04-20 14:44:04 +0200480 if (hdev->bus == HCI_USB) {
481 /* Read USB Product Info */
482 skb = btbcm_read_usb_product(hdev);
483 if (IS_ERR(skb))
484 return PTR_ERR(skb);
485
486 vid = get_unaligned_le16(skb->data + 1);
487 pid = get_unaligned_le16(skb->data + 3);
488 kfree_skb(skb);
489
Hans de Goede74530a62020-04-17 19:15:31 +0200490 snprintf(postfix, sizeof(postfix), "-%4.4x-%4.4x", vid, pid);
491 }
492
493 fw_name = kmalloc(BCM_FW_NAME_COUNT_MAX * BCM_FW_NAME_LEN, GFP_KERNEL);
494 if (!fw_name)
495 return -ENOMEM;
496
497 if (hw_name) {
498 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
499 "brcm/%s%s.hcd", hw_name, postfix);
500 fw_name_count++;
501 }
502
503 snprintf(fw_name[fw_name_count], BCM_FW_NAME_LEN,
504 "brcm/BCM%s.hcd", postfix);
505 fw_name_count++;
506
507 for (i = 0; i < fw_name_count; i++) {
508 err = firmware_request_nowarn(&fw, fw_name[i], &hdev->dev);
509 if (err == 0) {
510 bt_dev_info(hdev, "%s '%s' Patch",
511 hw_name ? hw_name : "BCM", fw_name[i]);
512 *fw_load_done = true;
513 break;
514 }
515 }
516
517 if (*fw_load_done) {
518 err = btbcm_patchram(hdev, fw);
519 if (err)
520 bt_dev_info(hdev, "BCM: Patch failed (%d)", err);
521
522 release_firmware(fw);
Hans de Goedefec0de22018-04-20 14:44:04 +0200523 } else {
Hans de Goede74530a62020-04-17 19:15:31 +0200524 bt_dev_err(hdev, "BCM: firmware Patch file not found, tried:");
525 for (i = 0; i < fw_name_count; i++)
526 bt_dev_err(hdev, "BCM: '%s'", fw_name[i]);
Frederic Danis75e167e2015-05-28 11:25:04 +0200527 }
528
Hans de Goede74530a62020-04-17 19:15:31 +0200529 kfree(fw_name);
Frederic Danis75e167e2015-05-28 11:25:04 +0200530 return 0;
531}
532EXPORT_SYMBOL_GPL(btbcm_initialize);
533
Hans de Goede0383f162020-04-17 19:15:29 +0200534int btbcm_finalize(struct hci_dev *hdev, bool *fw_load_done)
Frederic Danis75e167e2015-05-28 11:25:04 +0200535{
Frederic Danis75e167e2015-05-28 11:25:04 +0200536 int err;
537
Hans de Goede0383f162020-04-17 19:15:29 +0200538 /* Re-initialize if necessary */
539 if (*fw_load_done) {
540 err = btbcm_initialize(hdev, fw_load_done);
541 if (err)
542 return err;
543 }
Frederic Danis75e167e2015-05-28 11:25:04 +0200544
Frederic Danis75e167e2015-05-28 11:25:04 +0200545 btbcm_check_bdaddr(hdev);
546
547 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
548
549 return 0;
550}
551EXPORT_SYMBOL_GPL(btbcm_finalize);
552
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700553int btbcm_setup_patchram(struct hci_dev *hdev)
554{
Hans de Goede0287c5d2020-04-17 19:15:27 +0200555 bool fw_load_done = false;
Hans de Goede59ce5692018-04-20 14:44:06 +0200556 int err;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700557
Hans de Goede59ce5692018-04-20 14:44:06 +0200558 /* Initialize */
Hans de Goede0287c5d2020-04-17 19:15:27 +0200559 err = btbcm_initialize(hdev, &fw_load_done);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700560 if (err)
561 return err;
562
Hans de Goede0287c5d2020-04-17 19:15:27 +0200563 /* Re-initialize after loading Patch */
Hans de Goede0383f162020-04-17 19:15:29 +0200564 return btbcm_finalize(hdev, &fw_load_done);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700565}
566EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
567
568int btbcm_setup_apple(struct hci_dev *hdev)
569{
570 struct sk_buff *skb;
Marcel Holtmannb224d3f2015-10-03 12:01:08 +0200571 int err;
572
573 /* Reset */
574 err = btbcm_reset(hdev);
575 if (err)
576 return err;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700577
578 /* Read Verbose Config Version Info */
579 skb = btbcm_read_verbose_config(hdev);
Chris Mason7bee8b02015-07-14 16:25:30 -0400580 if (!IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100581 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
582 skb->data[1], get_unaligned_le16(skb->data + 5));
Chris Mason7bee8b02015-07-14 16:25:30 -0400583 kfree_skb(skb);
584 }
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700585
Marcel Holtmann34cea412015-10-18 22:48:28 +0200586 /* Read USB Product Info */
587 skb = btbcm_read_usb_product(hdev);
588 if (!IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100589 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
590 get_unaligned_le16(skb->data + 1),
591 get_unaligned_le16(skb->data + 3));
Marcel Holtmann34cea412015-10-18 22:48:28 +0200592 kfree_skb(skb);
593 }
594
Marcel Holtmann4284ecb2017-06-10 14:33:16 +0200595 /* Read Controller Features */
596 skb = btbcm_read_controller_features(hdev);
597 if (!IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100598 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
Marcel Holtmann4284ecb2017-06-10 14:33:16 +0200599 kfree_skb(skb);
600 }
601
Marcel Holtmann9bc63ca2015-10-05 12:43:57 +0200602 /* Read Local Name */
603 skb = btbcm_read_local_name(hdev);
604 if (!IS_ERR(skb)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100605 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
Marcel Holtmann9bc63ca2015-10-05 12:43:57 +0200606 kfree_skb(skb);
607 }
608
Marcel Holtmann941521e2015-04-05 22:52:15 -0700609 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
610
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700611 return 0;
612}
613EXPORT_SYMBOL_GPL(btbcm_setup_apple);
614
Marcel Holtmann4fba30f2015-04-05 22:52:10 -0700615MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
616MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
617MODULE_VERSION(VERSION);
618MODULE_LICENSE("GPL");