bna: remove oper_state_cbfn from struct bna_rxf
[linux/fpc-iii.git] / drivers / bluetooth / btbcm.c
blob728fce38a5a24cd6fb142f77b7a7178508d8e0c1
1 /*
3 * Bluetooth support for Broadcom devices
5 * Copyright (C) 2015 Intel Corporation
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/firmware.h>
26 #include <asm/unaligned.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
31 #include "btbcm.h"
33 #define VERSION "0.1"
35 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
37 int btbcm_check_bdaddr(struct hci_dev *hdev)
39 struct hci_rp_read_bd_addr *bda;
40 struct sk_buff *skb;
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;
51 if (skb->len != sizeof(*bda)) {
52 BT_ERR("%s: BCM: Device address length mismatch", hdev->name);
53 kfree_skb(skb);
54 return -EIO;
57 bda = (struct hci_rp_read_bd_addr *)skb->data;
59 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
60 * with no configured address.
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);
68 kfree_skb(skb);
70 return 0;
72 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
74 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
76 struct sk_buff *skb;
77 int err;
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;
86 kfree_skb(skb);
88 return 0;
90 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
92 int btbcm_patchram(struct hci_dev *hdev, const char *firmware)
94 const struct hci_command_hdr *cmd;
95 const struct firmware *fw;
96 const u8 *fw_ptr;
97 size_t fw_size;
98 struct sk_buff *skb;
99 u16 opcode;
100 int err;
102 err = request_firmware(&fw, firmware, &hdev->dev);
103 if (err < 0) {
104 BT_INFO("%s: BCM: Patch %s not found", hdev->name, firmware);
105 return err;
108 /* Start Download */
109 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
110 if (IS_ERR(skb)) {
111 err = PTR_ERR(skb);
112 BT_ERR("%s: BCM: Download Minidrv command failed (%d)",
113 hdev->name, err);
114 goto done;
116 kfree_skb(skb);
118 /* 50 msec delay after Download Minidrv completes */
119 msleep(50);
121 fw_ptr = fw->data;
122 fw_size = fw->size;
124 while (fw_size >= sizeof(*cmd)) {
125 const u8 *cmd_param;
127 cmd = (struct hci_command_hdr *)fw_ptr;
128 fw_ptr += sizeof(*cmd);
129 fw_size -= sizeof(*cmd);
131 if (fw_size < cmd->plen) {
132 BT_ERR("%s: BCM: Patch %s is corrupted", hdev->name,
133 firmware);
134 err = -EINVAL;
135 goto done;
138 cmd_param = fw_ptr;
139 fw_ptr += cmd->plen;
140 fw_size -= cmd->plen;
142 opcode = le16_to_cpu(cmd->opcode);
144 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
145 HCI_INIT_TIMEOUT);
146 if (IS_ERR(skb)) {
147 err = PTR_ERR(skb);
148 BT_ERR("%s: BCM: Patch command %04x failed (%d)",
149 hdev->name, opcode, err);
150 goto done;
152 kfree_skb(skb);
155 /* 250 msec delay after Launch Ram completes */
156 msleep(250);
158 done:
159 release_firmware(fw);
160 return err;
162 EXPORT_SYMBOL(btbcm_patchram);
164 static int btbcm_reset(struct hci_dev *hdev)
166 struct sk_buff *skb;
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;
174 kfree_skb(skb);
176 return 0;
179 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
181 struct sk_buff *skb;
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;
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);
197 return skb;
200 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
202 struct sk_buff *skb;
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;
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);
217 return skb;
220 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
222 struct sk_buff *skb;
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;
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);
237 return skb;
240 static const struct {
241 u16 subver;
242 const char *name;
243 } bcm_uart_subver_table[] = {
244 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
248 static const struct {
249 u16 subver;
250 const char *name;
251 } bcm_usb_subver_table[] = {
252 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
253 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
254 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
255 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
256 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
257 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
258 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
259 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
260 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
261 { 0x610c, "BCM4354" }, /* 003.001.012 */
265 int btbcm_setup_patchram(struct hci_dev *hdev)
267 char fw_name[64];
268 u16 subver, rev, pid, vid;
269 const char *hw_name = NULL;
270 struct sk_buff *skb;
271 struct hci_rp_read_local_version *ver;
272 int i, err;
274 /* Reset */
275 err = btbcm_reset(hdev);
276 if (err)
277 return err;
279 /* Read Local Version Info */
280 skb = btbcm_read_local_version(hdev);
281 if (IS_ERR(skb))
282 return PTR_ERR(skb);
284 ver = (struct hci_rp_read_local_version *)skb->data;
285 rev = le16_to_cpu(ver->hci_rev);
286 subver = le16_to_cpu(ver->lmp_subver);
287 kfree_skb(skb);
289 /* Read Verbose Config Version Info */
290 skb = btbcm_read_verbose_config(hdev);
291 if (IS_ERR(skb))
292 return PTR_ERR(skb);
294 BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
295 kfree_skb(skb);
297 switch ((rev & 0xf000) >> 12) {
298 case 0:
299 for (i = 0; bcm_uart_subver_table[i].name; i++) {
300 if (subver == bcm_uart_subver_table[i].subver) {
301 hw_name = bcm_uart_subver_table[i].name;
302 break;
306 snprintf(fw_name, sizeof(fw_name), "brcm/%s.hcd",
307 hw_name ? : "BCM");
308 break;
309 case 1:
310 case 2:
311 /* Read USB Product Info */
312 skb = btbcm_read_usb_product(hdev);
313 if (IS_ERR(skb))
314 return PTR_ERR(skb);
316 vid = get_unaligned_le16(skb->data + 1);
317 pid = get_unaligned_le16(skb->data + 3);
318 kfree_skb(skb);
320 for (i = 0; bcm_usb_subver_table[i].name; i++) {
321 if (subver == bcm_usb_subver_table[i].subver) {
322 hw_name = bcm_usb_subver_table[i].name;
323 break;
327 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
328 hw_name ? : "BCM", vid, pid);
329 break;
330 default:
331 return 0;
334 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
335 hw_name ? : "BCM", (subver & 0x7000) >> 13,
336 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
338 err = btbcm_patchram(hdev, fw_name);
339 if (err == -ENOENT)
340 return 0;
342 /* Reset */
343 err = btbcm_reset(hdev);
344 if (err)
345 return err;
347 /* Read Local Version Info */
348 skb = btbcm_read_local_version(hdev);
349 if (IS_ERR(skb))
350 return PTR_ERR(skb);
352 ver = (struct hci_rp_read_local_version *)skb->data;
353 rev = le16_to_cpu(ver->hci_rev);
354 subver = le16_to_cpu(ver->lmp_subver);
355 kfree_skb(skb);
357 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
358 hw_name ? : "BCM", (subver & 0x7000) >> 13,
359 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
361 btbcm_check_bdaddr(hdev);
363 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
365 return 0;
367 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
369 int btbcm_setup_apple(struct hci_dev *hdev)
371 struct sk_buff *skb;
373 /* Read Verbose Config Version Info */
374 skb = btbcm_read_verbose_config(hdev);
375 if (IS_ERR(skb))
376 return PTR_ERR(skb);
378 BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1],
379 get_unaligned_le16(skb->data + 5));
380 kfree_skb(skb);
382 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
384 return 0;
386 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
388 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
389 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
390 MODULE_VERSION(VERSION);
391 MODULE_LICENSE("GPL");