blob: c7aec977662022d06400f42093d772866ed116a1 [file] [log] [blame]
Marcel Holtmann4fba30f2015-04-05 22:52:10 -07001/*
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 Holtmann1c8ba6d2015-04-05 22:52:13 -070025#include <linux/firmware.h>
26#include <asm/unaligned.h>
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070027
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}})
36
37int btbcm_check_bdaddr(struct hci_dev *hdev)
38{
39 struct hci_rp_read_bd_addr *bda;
40 struct sk_buff *skb;
41
42 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
43 HCI_INIT_TIMEOUT);
44 if (IS_ERR(skb)) {
45 int err = PTR_ERR(skb);
46 BT_ERR("%s: BCM: Reading device address failed (%d)",
47 hdev->name, err);
48 return err;
49 }
50
51 if (skb->len != sizeof(*bda)) {
52 BT_ERR("%s: BCM: Device address length mismatch", hdev->name);
53 kfree_skb(skb);
54 return -EIO;
55 }
56
57 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann4fba30f2015-04-05 22:52:10 -070058
59 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
60 * with no configured address.
61 */
62 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
63 BT_INFO("%s: BCM: Using default device address (%pMR)",
64 hdev->name, &bda->bdaddr);
65 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
66 }
67
68 kfree_skb(skb);
69
70 return 0;
71}
72EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
73
74int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
75{
76 struct sk_buff *skb;
77 int err;
78
79 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
80 if (IS_ERR(skb)) {
81 err = PTR_ERR(skb);
82 BT_ERR("%s: BCM: Change address command failed (%d)",
83 hdev->name, err);
84 return err;
85 }
86 kfree_skb(skb);
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
91
Frederic Danis18aeb442015-05-28 11:25:01 +020092int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
Marcel Holtmann50862ee2015-04-10 14:02:20 -070093{
94 const struct hci_command_hdr *cmd;
Marcel Holtmann50862ee2015-04-10 14:02:20 -070095 const u8 *fw_ptr;
96 size_t fw_size;
97 struct sk_buff *skb;
98 u16 opcode;
Frederic Danis18aeb442015-05-28 11:25:01 +020099 int err = 0;
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700100
101 /* Start Download */
102 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
103 if (IS_ERR(skb)) {
104 err = PTR_ERR(skb);
105 BT_ERR("%s: BCM: Download Minidrv command failed (%d)",
106 hdev->name, err);
107 goto done;
108 }
109 kfree_skb(skb);
110
111 /* 50 msec delay after Download Minidrv completes */
112 msleep(50);
113
114 fw_ptr = fw->data;
115 fw_size = fw->size;
116
117 while (fw_size >= sizeof(*cmd)) {
118 const u8 *cmd_param;
119
120 cmd = (struct hci_command_hdr *)fw_ptr;
121 fw_ptr += sizeof(*cmd);
122 fw_size -= sizeof(*cmd);
123
124 if (fw_size < cmd->plen) {
Frederic Danis18aeb442015-05-28 11:25:01 +0200125 BT_ERR("%s: BCM: Patch is corrupted", hdev->name);
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700126 err = -EINVAL;
127 goto done;
128 }
129
130 cmd_param = fw_ptr;
131 fw_ptr += cmd->plen;
132 fw_size -= cmd->plen;
133
134 opcode = le16_to_cpu(cmd->opcode);
135
136 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
137 HCI_INIT_TIMEOUT);
138 if (IS_ERR(skb)) {
139 err = PTR_ERR(skb);
140 BT_ERR("%s: BCM: Patch command %04x failed (%d)",
141 hdev->name, opcode, err);
142 goto done;
143 }
144 kfree_skb(skb);
145 }
146
147 /* 250 msec delay after Launch Ram completes */
148 msleep(250);
149
150done:
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700151 return err;
152}
153EXPORT_SYMBOL(btbcm_patchram);
154
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700155static int btbcm_reset(struct hci_dev *hdev)
156{
157 struct sk_buff *skb;
158
159 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
160 if (IS_ERR(skb)) {
161 int err = PTR_ERR(skb);
162 BT_ERR("%s: BCM: Reset failed (%d)", hdev->name, err);
163 return err;
164 }
165 kfree_skb(skb);
166
167 return 0;
168}
169
170static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
171{
172 struct sk_buff *skb;
173
174 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
175 HCI_INIT_TIMEOUT);
176 if (IS_ERR(skb)) {
177 BT_ERR("%s: BCM: Reading local version info failed (%ld)",
178 hdev->name, PTR_ERR(skb));
179 return skb;
180 }
181
182 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
183 BT_ERR("%s: BCM: Local version length mismatch", hdev->name);
184 kfree_skb(skb);
185 return ERR_PTR(-EIO);
186 }
187
188 return skb;
189}
190
191static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
192{
193 struct sk_buff *skb;
194
195 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
196 if (IS_ERR(skb)) {
197 BT_ERR("%s: BCM: Read verbose config info failed (%ld)",
198 hdev->name, PTR_ERR(skb));
199 return skb;
200 }
201
202 if (skb->len != 7) {
203 BT_ERR("%s: BCM: Verbose config length mismatch", hdev->name);
204 kfree_skb(skb);
205 return ERR_PTR(-EIO);
206 }
207
208 return skb;
209}
210
211static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
212{
213 struct sk_buff *skb;
214
215 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
216 if (IS_ERR(skb)) {
217 BT_ERR("%s: BCM: Read USB product info failed (%ld)",
218 hdev->name, PTR_ERR(skb));
219 return skb;
220 }
221
222 if (skb->len != 5) {
223 BT_ERR("%s: BCM: USB product length mismatch", hdev->name);
224 kfree_skb(skb);
225 return ERR_PTR(-EIO);
226 }
227
228 return skb;
229}
230
231static const struct {
232 u16 subver;
233 const char *name;
Marcel Holtmann9a0bb572015-04-05 22:52:19 -0700234} bcm_uart_subver_table[] = {
235 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
236 { }
237};
238
239static const struct {
240 u16 subver;
241 const char *name;
242} bcm_usb_subver_table[] = {
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700243 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
244 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
245 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
246 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
247 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
248 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
249 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
250 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
251 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
252 { 0x610c, "BCM4354" }, /* 003.001.012 */
253 { }
254};
255
256int btbcm_setup_patchram(struct hci_dev *hdev)
257{
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700258 char fw_name[64];
Frederic Danis18aeb442015-05-28 11:25:01 +0200259 const struct firmware *fw;
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700260 u16 subver, rev, pid, vid;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700261 const char *hw_name = NULL;
262 struct sk_buff *skb;
263 struct hci_rp_read_local_version *ver;
264 int i, err;
265
266 /* Reset */
267 err = btbcm_reset(hdev);
268 if (err)
269 return err;
270
271 /* Read Local Version Info */
272 skb = btbcm_read_local_version(hdev);
273 if (IS_ERR(skb))
274 return PTR_ERR(skb);
275
276 ver = (struct hci_rp_read_local_version *)skb->data;
277 rev = le16_to_cpu(ver->hci_rev);
278 subver = le16_to_cpu(ver->lmp_subver);
279 kfree_skb(skb);
280
281 /* Read Verbose Config Version Info */
282 skb = btbcm_read_verbose_config(hdev);
283 if (IS_ERR(skb))
284 return PTR_ERR(skb);
285
286 BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
287 kfree_skb(skb);
288
Marcel Holtmann9a0bb572015-04-05 22:52:19 -0700289 switch ((rev & 0xf000) >> 12) {
290 case 0:
291 for (i = 0; bcm_uart_subver_table[i].name; i++) {
292 if (subver == bcm_uart_subver_table[i].subver) {
293 hw_name = bcm_uart_subver_table[i].name;
294 break;
295 }
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700296 }
Marcel Holtmann9a0bb572015-04-05 22:52:19 -0700297
298 snprintf(fw_name, sizeof(fw_name), "brcm/%s.hcd",
299 hw_name ? : "BCM");
300 break;
301 case 1:
302 case 2:
303 /* Read USB Product Info */
304 skb = btbcm_read_usb_product(hdev);
305 if (IS_ERR(skb))
306 return PTR_ERR(skb);
307
308 vid = get_unaligned_le16(skb->data + 1);
309 pid = get_unaligned_le16(skb->data + 3);
310 kfree_skb(skb);
311
312 for (i = 0; bcm_usb_subver_table[i].name; i++) {
313 if (subver == bcm_usb_subver_table[i].subver) {
314 hw_name = bcm_usb_subver_table[i].name;
315 break;
316 }
317 }
318
319 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
320 hw_name ? : "BCM", vid, pid);
321 break;
322 default:
323 return 0;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700324 }
325
326 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
327 hw_name ? : "BCM", (subver & 0x7000) >> 13,
328 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
329
Frederic Danis18aeb442015-05-28 11:25:01 +0200330 err = request_firmware(&fw, fw_name, &hdev->dev);
331 if (err < 0) {
332 BT_INFO("%s: BCM: Patch %s not found", hdev->name, fw_name);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700333 return 0;
Frederic Danis18aeb442015-05-28 11:25:01 +0200334 }
335
336 btbcm_patchram(hdev, fw);
337
338 release_firmware(fw);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700339
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700340 /* Reset */
341 err = btbcm_reset(hdev);
342 if (err)
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700343 return err;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700344
345 /* Read Local Version Info */
346 skb = btbcm_read_local_version(hdev);
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700347 if (IS_ERR(skb))
348 return PTR_ERR(skb);
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700349
350 ver = (struct hci_rp_read_local_version *)skb->data;
351 rev = le16_to_cpu(ver->hci_rev);
352 subver = le16_to_cpu(ver->lmp_subver);
353 kfree_skb(skb);
354
355 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
356 hw_name ? : "BCM", (subver & 0x7000) >> 13,
357 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
358
359 btbcm_check_bdaddr(hdev);
360
Marcel Holtmann941521e2015-04-05 22:52:15 -0700361 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
362
Marcel Holtmann50862ee2015-04-10 14:02:20 -0700363 return 0;
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700364}
365EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
366
367int btbcm_setup_apple(struct hci_dev *hdev)
368{
369 struct sk_buff *skb;
370
371 /* Read Verbose Config Version Info */
372 skb = btbcm_read_verbose_config(hdev);
373 if (IS_ERR(skb))
374 return PTR_ERR(skb);
375
376 BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1],
377 get_unaligned_le16(skb->data + 5));
378 kfree_skb(skb);
379
Marcel Holtmann941521e2015-04-05 22:52:15 -0700380 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
381
Marcel Holtmann1c8ba6d2015-04-05 22:52:13 -0700382 return 0;
383}
384EXPORT_SYMBOL_GPL(btbcm_setup_apple);
385
Marcel Holtmann4fba30f2015-04-05 22:52:10 -0700386MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
387MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
388MODULE_VERSION(VERSION);
389MODULE_LICENSE("GPL");