dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / bluetooth / btbcm.c
blobe1377934507cc74af786e07d8bcb5d6e7fe82d0d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 * Bluetooth support for Broadcom devices
6 * Copyright (C) 2015 Intel Corporation
7 */
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>
16 #include "btbcm.h"
18 #define VERSION "0.1"
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 int btbcm_check_bdaddr(struct hci_dev *hdev)
32 struct hci_rp_read_bd_addr *bda;
33 struct sk_buff *skb;
35 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
36 HCI_INIT_TIMEOUT);
37 if (IS_ERR(skb)) {
38 int err = PTR_ERR(skb);
40 bt_dev_err(hdev, "BCM: Reading device address failed (%d)", err);
41 return err;
44 if (skb->len != sizeof(*bda)) {
45 bt_dev_err(hdev, "BCM: Device address length mismatch");
46 kfree_skb(skb);
47 return -EIO;
50 bda = (struct hci_rp_read_bd_addr *)skb->data;
52 /* Check if the address indicates a controller with either an
53 * invalid or default address. In both cases the device needs
54 * to be marked as not having a valid address.
56 * The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
57 * with no configured address.
59 * The address 20:70:02:A0:00:00 indicates a BCM20702A1 controller
60 * with no configured address.
62 * The address 20:76:A0:00:56:79 indicates a BCM2076B1 controller
63 * with no configured address.
65 * The address 43:24:B3:00:00:00 indicates a BCM4324B3 controller
66 * with waiting for configuration state.
68 * The address 43:30:B1:00:00:00 indicates a BCM4330B1 controller
69 * with waiting for configuration state.
71 * The address 43:43:A0:12:1F:AC indicates a BCM43430A0 controller
72 * with no configured address.
74 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0) ||
75 !bacmp(&bda->bdaddr, BDADDR_BCM20702A1) ||
76 !bacmp(&bda->bdaddr, BDADDR_BCM2076B1) ||
77 !bacmp(&bda->bdaddr, BDADDR_BCM4324B3) ||
78 !bacmp(&bda->bdaddr, BDADDR_BCM4330B1) ||
79 !bacmp(&bda->bdaddr, BDADDR_BCM4334B0) ||
80 !bacmp(&bda->bdaddr, BDADDR_BCM4345C5) ||
81 !bacmp(&bda->bdaddr, BDADDR_BCM43430A0) ||
82 !bacmp(&bda->bdaddr, BDADDR_BCM43341B)) {
83 bt_dev_info(hdev, "BCM: Using default device address (%pMR)",
84 &bda->bdaddr);
85 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
88 kfree_skb(skb);
90 return 0;
92 EXPORT_SYMBOL_GPL(btbcm_check_bdaddr);
94 int btbcm_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
96 struct sk_buff *skb;
97 int err;
99 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
100 if (IS_ERR(skb)) {
101 err = PTR_ERR(skb);
102 bt_dev_err(hdev, "BCM: Change address command failed (%d)", err);
103 return err;
105 kfree_skb(skb);
107 return 0;
109 EXPORT_SYMBOL_GPL(btbcm_set_bdaddr);
111 int btbcm_read_pcm_int_params(struct hci_dev *hdev,
112 struct bcm_set_pcm_int_params *params)
114 struct sk_buff *skb;
115 int err = 0;
117 skb = __hci_cmd_sync(hdev, 0xfc1d, 0, NULL, HCI_INIT_TIMEOUT);
118 if (IS_ERR(skb)) {
119 err = PTR_ERR(skb);
120 bt_dev_err(hdev, "BCM: Read PCM int params failed (%d)", err);
121 return err;
124 if (skb->len != 6 || skb->data[0]) {
125 bt_dev_err(hdev, "BCM: Read PCM int params length mismatch");
126 kfree_skb(skb);
127 return -EIO;
130 if (params)
131 memcpy(params, skb->data + 1, 5);
133 kfree_skb(skb);
135 return 0;
137 EXPORT_SYMBOL_GPL(btbcm_read_pcm_int_params);
139 int btbcm_write_pcm_int_params(struct hci_dev *hdev,
140 const struct bcm_set_pcm_int_params *params)
142 struct sk_buff *skb;
143 int err;
145 skb = __hci_cmd_sync(hdev, 0xfc1c, 5, params, HCI_INIT_TIMEOUT);
146 if (IS_ERR(skb)) {
147 err = PTR_ERR(skb);
148 bt_dev_err(hdev, "BCM: Write PCM int params failed (%d)", err);
149 return err;
151 kfree_skb(skb);
153 return 0;
155 EXPORT_SYMBOL_GPL(btbcm_write_pcm_int_params);
157 int btbcm_patchram(struct hci_dev *hdev, const struct firmware *fw)
159 const struct hci_command_hdr *cmd;
160 const u8 *fw_ptr;
161 size_t fw_size;
162 struct sk_buff *skb;
163 u16 opcode;
164 int err = 0;
166 /* Start Download */
167 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
168 if (IS_ERR(skb)) {
169 err = PTR_ERR(skb);
170 bt_dev_err(hdev, "BCM: Download Minidrv command failed (%d)",
171 err);
172 goto done;
174 kfree_skb(skb);
176 /* 50 msec delay after Download Minidrv completes */
177 msleep(50);
179 fw_ptr = fw->data;
180 fw_size = fw->size;
182 while (fw_size >= sizeof(*cmd)) {
183 const u8 *cmd_param;
185 cmd = (struct hci_command_hdr *)fw_ptr;
186 fw_ptr += sizeof(*cmd);
187 fw_size -= sizeof(*cmd);
189 if (fw_size < cmd->plen) {
190 bt_dev_err(hdev, "BCM: Patch is corrupted");
191 err = -EINVAL;
192 goto done;
195 cmd_param = fw_ptr;
196 fw_ptr += cmd->plen;
197 fw_size -= cmd->plen;
199 opcode = le16_to_cpu(cmd->opcode);
201 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
202 HCI_INIT_TIMEOUT);
203 if (IS_ERR(skb)) {
204 err = PTR_ERR(skb);
205 bt_dev_err(hdev, "BCM: Patch command %04x failed (%d)",
206 opcode, err);
207 goto done;
209 kfree_skb(skb);
212 /* 250 msec delay after Launch Ram completes */
213 msleep(250);
215 done:
216 return err;
218 EXPORT_SYMBOL(btbcm_patchram);
220 static int btbcm_reset(struct hci_dev *hdev)
222 struct sk_buff *skb;
224 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
225 if (IS_ERR(skb)) {
226 int err = PTR_ERR(skb);
228 bt_dev_err(hdev, "BCM: Reset failed (%d)", err);
229 return err;
231 kfree_skb(skb);
233 /* 100 msec delay for module to complete reset process */
234 msleep(100);
236 return 0;
239 static struct sk_buff *btbcm_read_local_name(struct hci_dev *hdev)
241 struct sk_buff *skb;
243 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_NAME, 0, NULL,
244 HCI_INIT_TIMEOUT);
245 if (IS_ERR(skb)) {
246 bt_dev_err(hdev, "BCM: Reading local name failed (%ld)",
247 PTR_ERR(skb));
248 return skb;
251 if (skb->len != sizeof(struct hci_rp_read_local_name)) {
252 bt_dev_err(hdev, "BCM: Local name length mismatch");
253 kfree_skb(skb);
254 return ERR_PTR(-EIO);
257 return skb;
260 static struct sk_buff *btbcm_read_local_version(struct hci_dev *hdev)
262 struct sk_buff *skb;
264 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
265 HCI_INIT_TIMEOUT);
266 if (IS_ERR(skb)) {
267 bt_dev_err(hdev, "BCM: Reading local version info failed (%ld)",
268 PTR_ERR(skb));
269 return skb;
272 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
273 bt_dev_err(hdev, "BCM: Local version length mismatch");
274 kfree_skb(skb);
275 return ERR_PTR(-EIO);
278 return skb;
281 static struct sk_buff *btbcm_read_verbose_config(struct hci_dev *hdev)
283 struct sk_buff *skb;
285 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
286 if (IS_ERR(skb)) {
287 bt_dev_err(hdev, "BCM: Read verbose config info failed (%ld)",
288 PTR_ERR(skb));
289 return skb;
292 if (skb->len != 7) {
293 bt_dev_err(hdev, "BCM: Verbose config length mismatch");
294 kfree_skb(skb);
295 return ERR_PTR(-EIO);
298 return skb;
301 static struct sk_buff *btbcm_read_controller_features(struct hci_dev *hdev)
303 struct sk_buff *skb;
305 skb = __hci_cmd_sync(hdev, 0xfc6e, 0, NULL, HCI_INIT_TIMEOUT);
306 if (IS_ERR(skb)) {
307 bt_dev_err(hdev, "BCM: Read controller features failed (%ld)",
308 PTR_ERR(skb));
309 return skb;
312 if (skb->len != 9) {
313 bt_dev_err(hdev, "BCM: Controller features length mismatch");
314 kfree_skb(skb);
315 return ERR_PTR(-EIO);
318 return skb;
321 static struct sk_buff *btbcm_read_usb_product(struct hci_dev *hdev)
323 struct sk_buff *skb;
325 skb = __hci_cmd_sync(hdev, 0xfc5a, 0, NULL, HCI_INIT_TIMEOUT);
326 if (IS_ERR(skb)) {
327 bt_dev_err(hdev, "BCM: Read USB product info failed (%ld)",
328 PTR_ERR(skb));
329 return skb;
332 if (skb->len != 5) {
333 bt_dev_err(hdev, "BCM: USB product length mismatch");
334 kfree_skb(skb);
335 return ERR_PTR(-EIO);
338 return skb;
341 static int btbcm_read_info(struct hci_dev *hdev)
343 struct sk_buff *skb;
345 /* Read Verbose Config Version Info */
346 skb = btbcm_read_verbose_config(hdev);
347 if (IS_ERR(skb))
348 return PTR_ERR(skb);
350 bt_dev_info(hdev, "BCM: chip id %u", skb->data[1]);
351 kfree_skb(skb);
353 /* Read Controller Features */
354 skb = btbcm_read_controller_features(hdev);
355 if (IS_ERR(skb))
356 return PTR_ERR(skb);
358 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
359 kfree_skb(skb);
361 /* Read Local Name */
362 skb = btbcm_read_local_name(hdev);
363 if (IS_ERR(skb))
364 return PTR_ERR(skb);
366 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
367 kfree_skb(skb);
369 return 0;
372 struct bcm_subver_table {
373 u16 subver;
374 const char *name;
377 static const struct bcm_subver_table bcm_uart_subver_table[] = {
378 { 0x4103, "BCM4330B1" }, /* 002.001.003 */
379 { 0x410d, "BCM4334B0" }, /* 002.001.013 */
380 { 0x410e, "BCM43341B0" }, /* 002.001.014 */
381 { 0x4204, "BCM2076B1" }, /* 002.002.004 */
382 { 0x4406, "BCM4324B3" }, /* 002.004.006 */
383 { 0x4606, "BCM4324B5" }, /* 002.006.006 */
384 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
385 { 0x610c, "BCM4354" }, /* 003.001.012 */
386 { 0x2122, "BCM4343A0" }, /* 001.001.034 */
387 { 0x2209, "BCM43430A1" }, /* 001.002.009 */
388 { 0x6119, "BCM4345C0" }, /* 003.001.025 */
389 { 0x6606, "BCM4345C5" }, /* 003.006.006 */
390 { 0x230f, "BCM4356A2" }, /* 001.003.015 */
391 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
392 { 0x4217, "BCM4329B1" }, /* 002.002.023 */
393 { 0x6106, "BCM4359C0" }, /* 003.001.006 */
394 { 0x4106, "BCM4335A0" }, /* 002.001.006 */
398 static const struct bcm_subver_table bcm_usb_subver_table[] = {
399 { 0x2105, "BCM20703A1" }, /* 001.001.005 */
400 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
401 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
402 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
403 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
404 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
405 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
406 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
407 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
408 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
409 { 0x610c, "BCM4354" }, /* 003.001.012 */
413 int btbcm_initialize(struct hci_dev *hdev, char *fw_name, size_t len,
414 bool reinit)
416 u16 subver, rev, pid, vid;
417 const char *hw_name = "BCM";
418 struct sk_buff *skb;
419 struct hci_rp_read_local_version *ver;
420 const struct bcm_subver_table *bcm_subver_table;
421 int i, err;
423 /* Reset */
424 err = btbcm_reset(hdev);
425 if (err)
426 return err;
428 /* Read Local Version Info */
429 skb = btbcm_read_local_version(hdev);
430 if (IS_ERR(skb))
431 return PTR_ERR(skb);
433 ver = (struct hci_rp_read_local_version *)skb->data;
434 rev = le16_to_cpu(ver->hci_rev);
435 subver = le16_to_cpu(ver->lmp_subver);
436 kfree_skb(skb);
438 /* Read controller information */
439 if (!reinit) {
440 err = btbcm_read_info(hdev);
441 if (err)
442 return err;
445 /* Upper nibble of rev should be between 0 and 3? */
446 if (((rev & 0xf000) >> 12) > 3)
447 return 0;
449 bcm_subver_table = (hdev->bus == HCI_USB) ? bcm_usb_subver_table :
450 bcm_uart_subver_table;
452 for (i = 0; bcm_subver_table[i].name; i++) {
453 if (subver == bcm_subver_table[i].subver) {
454 hw_name = bcm_subver_table[i].name;
455 break;
459 if (hdev->bus == HCI_USB) {
460 /* Read USB Product Info */
461 skb = btbcm_read_usb_product(hdev);
462 if (IS_ERR(skb))
463 return PTR_ERR(skb);
465 vid = get_unaligned_le16(skb->data + 1);
466 pid = get_unaligned_le16(skb->data + 3);
467 kfree_skb(skb);
469 snprintf(fw_name, len, "brcm/%s-%4.4x-%4.4x.hcd",
470 hw_name, vid, pid);
471 } else {
472 snprintf(fw_name, len, "brcm/%s.hcd", hw_name);
475 bt_dev_info(hdev, "%s (%3.3u.%3.3u.%3.3u) build %4.4u",
476 hw_name, (subver & 0xe000) >> 13,
477 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
479 return 0;
481 EXPORT_SYMBOL_GPL(btbcm_initialize);
483 int btbcm_finalize(struct hci_dev *hdev)
485 char fw_name[64];
486 int err;
488 /* Re-initialize */
489 err = btbcm_initialize(hdev, fw_name, sizeof(fw_name), true);
490 if (err)
491 return err;
493 btbcm_check_bdaddr(hdev);
495 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
497 /* Some devices ship with the controller default address.
498 * Allow the bootloader to set a valid address through the
499 * device tree.
501 set_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks);
503 return 0;
505 EXPORT_SYMBOL_GPL(btbcm_finalize);
507 int btbcm_setup_patchram(struct hci_dev *hdev)
509 char fw_name[64];
510 const struct firmware *fw;
511 struct sk_buff *skb;
512 int err;
514 /* Initialize */
515 err = btbcm_initialize(hdev, fw_name, sizeof(fw_name), false);
516 if (err)
517 return err;
519 err = request_firmware(&fw, fw_name, &hdev->dev);
520 if (err < 0) {
521 bt_dev_info(hdev, "BCM: Patch %s not found", fw_name);
522 goto done;
525 btbcm_patchram(hdev, fw);
527 release_firmware(fw);
529 /* Re-initialize */
530 err = btbcm_initialize(hdev, fw_name, sizeof(fw_name), true);
531 if (err)
532 return err;
534 /* Read Local Name */
535 skb = btbcm_read_local_name(hdev);
536 if (IS_ERR(skb))
537 return PTR_ERR(skb);
539 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
540 kfree_skb(skb);
542 done:
543 btbcm_check_bdaddr(hdev);
545 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
547 return 0;
549 EXPORT_SYMBOL_GPL(btbcm_setup_patchram);
551 int btbcm_setup_apple(struct hci_dev *hdev)
553 struct sk_buff *skb;
554 int err;
556 /* Reset */
557 err = btbcm_reset(hdev);
558 if (err)
559 return err;
561 /* Read Verbose Config Version Info */
562 skb = btbcm_read_verbose_config(hdev);
563 if (!IS_ERR(skb)) {
564 bt_dev_info(hdev, "BCM: chip id %u build %4.4u",
565 skb->data[1], get_unaligned_le16(skb->data + 5));
566 kfree_skb(skb);
569 /* Read USB Product Info */
570 skb = btbcm_read_usb_product(hdev);
571 if (!IS_ERR(skb)) {
572 bt_dev_info(hdev, "BCM: product %4.4x:%4.4x",
573 get_unaligned_le16(skb->data + 1),
574 get_unaligned_le16(skb->data + 3));
575 kfree_skb(skb);
578 /* Read Controller Features */
579 skb = btbcm_read_controller_features(hdev);
580 if (!IS_ERR(skb)) {
581 bt_dev_info(hdev, "BCM: features 0x%2.2x", skb->data[1]);
582 kfree_skb(skb);
585 /* Read Local Name */
586 skb = btbcm_read_local_name(hdev);
587 if (!IS_ERR(skb)) {
588 bt_dev_info(hdev, "%s", (char *)(skb->data + 1));
589 kfree_skb(skb);
592 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
594 return 0;
596 EXPORT_SYMBOL_GPL(btbcm_setup_apple);
598 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
599 MODULE_DESCRIPTION("Bluetooth support for Broadcom devices ver " VERSION);
600 MODULE_VERSION(VERSION);
601 MODULE_LICENSE("GPL");