Linux 4.19.133
[linux/fpc-iii.git] / drivers / bluetooth / btintel.c
blob5270d55132015a467b15aba8887b944585893b91
1 /*
3 * Bluetooth support for Intel 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 <linux/regmap.h>
27 #include <asm/unaligned.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
32 #include "btintel.h"
34 #define VERSION "0.1"
36 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
38 int btintel_check_bdaddr(struct hci_dev *hdev)
40 struct hci_rp_read_bd_addr *bda;
41 struct sk_buff *skb;
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_dev_err(hdev, "Reading Intel device address failed (%d)",
48 err);
49 return err;
52 if (skb->len != sizeof(*bda)) {
53 bt_dev_err(hdev, "Intel device address length mismatch");
54 kfree_skb(skb);
55 return -EIO;
58 bda = (struct hci_rp_read_bd_addr *)skb->data;
60 /* For some Intel based controllers, the default Bluetooth device
61 * address 00:03:19:9E:8B:00 can be found. These controllers are
62 * fully operational, but have the danger of duplicate addresses
63 * and that in turn can cause problems with Bluetooth operation.
65 if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
66 bt_dev_err(hdev, "Found Intel default device address (%pMR)",
67 &bda->bdaddr);
68 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
71 kfree_skb(skb);
73 return 0;
75 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
77 int btintel_enter_mfg(struct hci_dev *hdev)
79 static const u8 param[] = { 0x01, 0x00 };
80 struct sk_buff *skb;
82 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
83 if (IS_ERR(skb)) {
84 bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
85 PTR_ERR(skb));
86 return PTR_ERR(skb);
88 kfree_skb(skb);
90 return 0;
92 EXPORT_SYMBOL_GPL(btintel_enter_mfg);
94 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
96 u8 param[] = { 0x00, 0x00 };
97 struct sk_buff *skb;
99 /* The 2nd command parameter specifies the manufacturing exit method:
100 * 0x00: Just disable the manufacturing mode (0x00).
101 * 0x01: Disable manufacturing mode and reset with patches deactivated.
102 * 0x02: Disable manufacturing mode and reset with patches activated.
104 if (reset)
105 param[1] |= patched ? 0x02 : 0x01;
107 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
108 if (IS_ERR(skb)) {
109 bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
110 PTR_ERR(skb));
111 return PTR_ERR(skb);
113 kfree_skb(skb);
115 return 0;
117 EXPORT_SYMBOL_GPL(btintel_exit_mfg);
119 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
121 struct sk_buff *skb;
122 int err;
124 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
125 if (IS_ERR(skb)) {
126 err = PTR_ERR(skb);
127 bt_dev_err(hdev, "Changing Intel device address failed (%d)",
128 err);
129 return err;
131 kfree_skb(skb);
133 return 0;
135 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
137 int btintel_set_diag(struct hci_dev *hdev, bool enable)
139 struct sk_buff *skb;
140 u8 param[3];
141 int err;
143 if (enable) {
144 param[0] = 0x03;
145 param[1] = 0x03;
146 param[2] = 0x03;
147 } else {
148 param[0] = 0x00;
149 param[1] = 0x00;
150 param[2] = 0x00;
153 skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
154 if (IS_ERR(skb)) {
155 err = PTR_ERR(skb);
156 if (err == -ENODATA)
157 goto done;
158 bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
159 err);
160 return err;
162 kfree_skb(skb);
164 done:
165 btintel_set_event_mask(hdev, enable);
166 return 0;
168 EXPORT_SYMBOL_GPL(btintel_set_diag);
170 int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
172 int err, ret;
174 err = btintel_enter_mfg(hdev);
175 if (err)
176 return err;
178 ret = btintel_set_diag(hdev, enable);
180 err = btintel_exit_mfg(hdev, false, false);
181 if (err)
182 return err;
184 return ret;
186 EXPORT_SYMBOL_GPL(btintel_set_diag_mfg);
188 void btintel_hw_error(struct hci_dev *hdev, u8 code)
190 struct sk_buff *skb;
191 u8 type = 0x00;
193 bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
195 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
196 if (IS_ERR(skb)) {
197 bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
198 PTR_ERR(skb));
199 return;
201 kfree_skb(skb);
203 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
204 if (IS_ERR(skb)) {
205 bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
206 PTR_ERR(skb));
207 return;
210 if (skb->len != 13) {
211 bt_dev_err(hdev, "Exception info size mismatch");
212 kfree_skb(skb);
213 return;
216 bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
218 kfree_skb(skb);
220 EXPORT_SYMBOL_GPL(btintel_hw_error);
222 void btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
224 const char *variant;
226 switch (ver->fw_variant) {
227 case 0x06:
228 variant = "Bootloader";
229 break;
230 case 0x23:
231 variant = "Firmware";
232 break;
233 default:
234 return;
237 bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
238 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
239 ver->fw_build_num, ver->fw_build_ww,
240 2000 + ver->fw_build_yy);
242 EXPORT_SYMBOL_GPL(btintel_version_info);
244 int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
245 const void *param)
247 while (plen > 0) {
248 struct sk_buff *skb;
249 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
251 cmd_param[0] = fragment_type;
252 memcpy(cmd_param + 1, param, fragment_len);
254 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
255 cmd_param, HCI_INIT_TIMEOUT);
256 if (IS_ERR(skb))
257 return PTR_ERR(skb);
259 kfree_skb(skb);
261 plen -= fragment_len;
262 param += fragment_len;
265 return 0;
267 EXPORT_SYMBOL_GPL(btintel_secure_send);
269 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
271 const struct firmware *fw;
272 struct sk_buff *skb;
273 const u8 *fw_ptr;
274 int err;
276 err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
277 if (err < 0) {
278 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
279 ddc_name, err);
280 return err;
283 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
285 fw_ptr = fw->data;
287 /* DDC file contains one or more DDC structure which has
288 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
290 while (fw->size > fw_ptr - fw->data) {
291 u8 cmd_plen = fw_ptr[0] + sizeof(u8);
293 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
294 HCI_INIT_TIMEOUT);
295 if (IS_ERR(skb)) {
296 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
297 PTR_ERR(skb));
298 release_firmware(fw);
299 return PTR_ERR(skb);
302 fw_ptr += cmd_plen;
303 kfree_skb(skb);
306 release_firmware(fw);
308 bt_dev_info(hdev, "Applying Intel DDC parameters completed");
310 return 0;
312 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
314 int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
316 u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
317 struct sk_buff *skb;
318 int err;
320 if (debug)
321 mask[1] |= 0x62;
323 skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
324 if (IS_ERR(skb)) {
325 err = PTR_ERR(skb);
326 bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
327 return err;
329 kfree_skb(skb);
331 return 0;
333 EXPORT_SYMBOL_GPL(btintel_set_event_mask);
335 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
337 int err, ret;
339 err = btintel_enter_mfg(hdev);
340 if (err)
341 return err;
343 ret = btintel_set_event_mask(hdev, debug);
345 err = btintel_exit_mfg(hdev, false, false);
346 if (err)
347 return err;
349 return ret;
351 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
353 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
355 struct sk_buff *skb;
357 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
358 if (IS_ERR(skb)) {
359 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
360 PTR_ERR(skb));
361 return PTR_ERR(skb);
364 if (skb->len != sizeof(*ver)) {
365 bt_dev_err(hdev, "Intel version event size mismatch");
366 kfree_skb(skb);
367 return -EILSEQ;
370 memcpy(ver, skb->data, sizeof(*ver));
372 kfree_skb(skb);
374 return 0;
376 EXPORT_SYMBOL_GPL(btintel_read_version);
378 /* ------- REGMAP IBT SUPPORT ------- */
380 #define IBT_REG_MODE_8BIT 0x00
381 #define IBT_REG_MODE_16BIT 0x01
382 #define IBT_REG_MODE_32BIT 0x02
384 struct regmap_ibt_context {
385 struct hci_dev *hdev;
386 __u16 op_write;
387 __u16 op_read;
390 struct ibt_cp_reg_access {
391 __le32 addr;
392 __u8 mode;
393 __u8 len;
394 __u8 data[0];
395 } __packed;
397 struct ibt_rp_reg_access {
398 __u8 status;
399 __le32 addr;
400 __u8 data[0];
401 } __packed;
403 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
404 void *val, size_t val_size)
406 struct regmap_ibt_context *ctx = context;
407 struct ibt_cp_reg_access cp;
408 struct ibt_rp_reg_access *rp;
409 struct sk_buff *skb;
410 int err = 0;
412 if (reg_size != sizeof(__le32))
413 return -EINVAL;
415 switch (val_size) {
416 case 1:
417 cp.mode = IBT_REG_MODE_8BIT;
418 break;
419 case 2:
420 cp.mode = IBT_REG_MODE_16BIT;
421 break;
422 case 4:
423 cp.mode = IBT_REG_MODE_32BIT;
424 break;
425 default:
426 return -EINVAL;
429 /* regmap provides a little-endian formatted addr */
430 cp.addr = *(__le32 *)addr;
431 cp.len = val_size;
433 bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
435 skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
436 HCI_CMD_TIMEOUT);
437 if (IS_ERR(skb)) {
438 err = PTR_ERR(skb);
439 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
440 le32_to_cpu(cp.addr), err);
441 return err;
444 if (skb->len != sizeof(*rp) + val_size) {
445 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
446 le32_to_cpu(cp.addr));
447 err = -EINVAL;
448 goto done;
451 rp = (struct ibt_rp_reg_access *)skb->data;
453 if (rp->addr != cp.addr) {
454 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
455 le32_to_cpu(rp->addr));
456 err = -EINVAL;
457 goto done;
460 memcpy(val, rp->data, val_size);
462 done:
463 kfree_skb(skb);
464 return err;
467 static int regmap_ibt_gather_write(void *context,
468 const void *addr, size_t reg_size,
469 const void *val, size_t val_size)
471 struct regmap_ibt_context *ctx = context;
472 struct ibt_cp_reg_access *cp;
473 struct sk_buff *skb;
474 int plen = sizeof(*cp) + val_size;
475 u8 mode;
476 int err = 0;
478 if (reg_size != sizeof(__le32))
479 return -EINVAL;
481 switch (val_size) {
482 case 1:
483 mode = IBT_REG_MODE_8BIT;
484 break;
485 case 2:
486 mode = IBT_REG_MODE_16BIT;
487 break;
488 case 4:
489 mode = IBT_REG_MODE_32BIT;
490 break;
491 default:
492 return -EINVAL;
495 cp = kmalloc(plen, GFP_KERNEL);
496 if (!cp)
497 return -ENOMEM;
499 /* regmap provides a little-endian formatted addr/value */
500 cp->addr = *(__le32 *)addr;
501 cp->mode = mode;
502 cp->len = val_size;
503 memcpy(&cp->data, val, val_size);
505 bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
507 skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
508 if (IS_ERR(skb)) {
509 err = PTR_ERR(skb);
510 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
511 le32_to_cpu(cp->addr), err);
512 goto done;
514 kfree_skb(skb);
516 done:
517 kfree(cp);
518 return err;
521 static int regmap_ibt_write(void *context, const void *data, size_t count)
523 /* data contains register+value, since we only support 32bit addr,
524 * minimum data size is 4 bytes.
526 if (WARN_ONCE(count < 4, "Invalid register access"))
527 return -EINVAL;
529 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
532 static void regmap_ibt_free_context(void *context)
534 kfree(context);
537 static struct regmap_bus regmap_ibt = {
538 .read = regmap_ibt_read,
539 .write = regmap_ibt_write,
540 .gather_write = regmap_ibt_gather_write,
541 .free_context = regmap_ibt_free_context,
542 .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
543 .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
546 /* Config is the same for all register regions */
547 static const struct regmap_config regmap_ibt_cfg = {
548 .name = "btintel_regmap",
549 .reg_bits = 32,
550 .val_bits = 32,
553 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
554 u16 opcode_write)
556 struct regmap_ibt_context *ctx;
558 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
559 opcode_write);
561 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
562 if (!ctx)
563 return ERR_PTR(-ENOMEM);
565 ctx->op_read = opcode_read;
566 ctx->op_write = opcode_write;
567 ctx->hdev = hdev;
569 return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
571 EXPORT_SYMBOL_GPL(btintel_regmap_init);
573 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
575 struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
576 struct sk_buff *skb;
578 params.boot_param = cpu_to_le32(boot_param);
580 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
581 HCI_INIT_TIMEOUT);
582 if (IS_ERR(skb)) {
583 bt_dev_err(hdev, "Failed to send Intel Reset command");
584 return PTR_ERR(skb);
587 kfree_skb(skb);
589 return 0;
591 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
593 int btintel_read_boot_params(struct hci_dev *hdev,
594 struct intel_boot_params *params)
596 struct sk_buff *skb;
598 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
599 if (IS_ERR(skb)) {
600 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
601 PTR_ERR(skb));
602 return PTR_ERR(skb);
605 if (skb->len != sizeof(*params)) {
606 bt_dev_err(hdev, "Intel boot parameters size mismatch");
607 kfree_skb(skb);
608 return -EILSEQ;
611 memcpy(params, skb->data, sizeof(*params));
613 kfree_skb(skb);
615 if (params->status) {
616 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
617 params->status);
618 return -bt_to_errno(params->status);
621 bt_dev_info(hdev, "Device revision is %u",
622 le16_to_cpu(params->dev_revid));
624 bt_dev_info(hdev, "Secure boot is %s",
625 params->secure_boot ? "enabled" : "disabled");
627 bt_dev_info(hdev, "OTP lock is %s",
628 params->otp_lock ? "enabled" : "disabled");
630 bt_dev_info(hdev, "API lock is %s",
631 params->api_lock ? "enabled" : "disabled");
633 bt_dev_info(hdev, "Debug lock is %s",
634 params->debug_lock ? "enabled" : "disabled");
636 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
637 params->min_fw_build_nn, params->min_fw_build_cw,
638 2000 + params->min_fw_build_yy);
640 return 0;
642 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
644 int btintel_download_firmware(struct hci_dev *hdev, const struct firmware *fw,
645 u32 *boot_param)
647 int err;
648 const u8 *fw_ptr;
649 u32 frag_len;
651 /* Start the firmware download transaction with the Init fragment
652 * represented by the 128 bytes of CSS header.
654 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
655 if (err < 0) {
656 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
657 goto done;
660 /* Send the 256 bytes of public key information from the firmware
661 * as the PKey fragment.
663 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
664 if (err < 0) {
665 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
666 goto done;
669 /* Send the 256 bytes of signature information from the firmware
670 * as the Sign fragment.
672 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
673 if (err < 0) {
674 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
675 goto done;
678 fw_ptr = fw->data + 644;
679 frag_len = 0;
681 while (fw_ptr - fw->data < fw->size) {
682 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
684 /* Each SKU has a different reset parameter to use in the
685 * HCI_Intel_Reset command and it is embedded in the firmware
686 * data. So, instead of using static value per SKU, check
687 * the firmware data and save it for later use.
689 if (le16_to_cpu(cmd->opcode) == 0xfc0e) {
690 /* The boot parameter is the first 32-bit value
691 * and rest of 3 octets are reserved.
693 *boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd));
695 bt_dev_dbg(hdev, "boot_param=0x%x", *boot_param);
698 frag_len += sizeof(*cmd) + cmd->plen;
700 /* The parameter length of the secure send command requires
701 * a 4 byte alignment. It happens so that the firmware file
702 * contains proper Intel_NOP commands to align the fragments
703 * as needed.
705 * Send set of commands with 4 byte alignment from the
706 * firmware data buffer as a single Data fragement.
708 if (!(frag_len % 4)) {
709 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
710 if (err < 0) {
711 bt_dev_err(hdev,
712 "Failed to send firmware data (%d)",
713 err);
714 goto done;
717 fw_ptr += frag_len;
718 frag_len = 0;
722 done:
723 return err;
725 EXPORT_SYMBOL_GPL(btintel_download_firmware);
727 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
728 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
729 MODULE_VERSION(VERSION);
730 MODULE_LICENSE("GPL");
731 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
732 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
733 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
734 MODULE_FIRMWARE("intel/ibt-12-16.ddc");