123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- /*
- *
- * Bluetooth support for Intel devices
- *
- * Copyright (C) 2015 Intel Corporation
- *
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #include <linux/module.h>
- #include <linux/firmware.h>
- #include <linux/regmap.h>
- #include <net/bluetooth/bluetooth.h>
- #include <net/bluetooth/hci_core.h>
- #include "btintel.h"
- #define VERSION "0.1"
- #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
- int btintel_check_bdaddr(struct hci_dev *hdev)
- {
- struct hci_rp_read_bd_addr *bda;
- struct sk_buff *skb;
- skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
- HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- int err = PTR_ERR(skb);
- BT_ERR("%s: Reading Intel device address failed (%d)",
- hdev->name, err);
- return err;
- }
- if (skb->len != sizeof(*bda)) {
- BT_ERR("%s: Intel device address length mismatch", hdev->name);
- kfree_skb(skb);
- return -EIO;
- }
- bda = (struct hci_rp_read_bd_addr *)skb->data;
- /* For some Intel based controllers, the default Bluetooth device
- * address 00:03:19:9E:8B:00 can be found. These controllers are
- * fully operational, but have the danger of duplicate addresses
- * and that in turn can cause problems with Bluetooth operation.
- */
- if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
- BT_ERR("%s: Found Intel default device address (%pMR)",
- hdev->name, &bda->bdaddr);
- set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
- }
- kfree_skb(skb);
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
- int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
- {
- struct sk_buff *skb;
- int err;
- skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Changing Intel device address failed (%d)",
- hdev->name, err);
- return err;
- }
- kfree_skb(skb);
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
- int btintel_set_diag(struct hci_dev *hdev, bool enable)
- {
- struct sk_buff *skb;
- u8 param[3];
- int err;
- if (enable) {
- param[0] = 0x03;
- param[1] = 0x03;
- param[2] = 0x03;
- } else {
- param[0] = 0x00;
- param[1] = 0x00;
- param[2] = 0x00;
- }
- skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- if (err == -ENODATA)
- goto done;
- BT_ERR("%s: Changing Intel diagnostic mode failed (%d)",
- hdev->name, err);
- return err;
- }
- kfree_skb(skb);
- done:
- btintel_set_event_mask(hdev, enable);
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_set_diag);
- int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
- {
- struct sk_buff *skb;
- u8 param[2];
- int err;
- param[0] = 0x01;
- param[1] = 0x00;
- skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
- hdev->name, err);
- return PTR_ERR(skb);
- }
- kfree_skb(skb);
- err = btintel_set_diag(hdev, enable);
- param[0] = 0x00;
- param[1] = 0x00;
- skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
- hdev->name, err);
- return PTR_ERR(skb);
- }
- kfree_skb(skb);
- return err;
- }
- EXPORT_SYMBOL_GPL(btintel_set_diag_mfg);
- void btintel_hw_error(struct hci_dev *hdev, u8 code)
- {
- struct sk_buff *skb;
- u8 type = 0x00;
- BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
- skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- BT_ERR("%s: Reset after hardware error failed (%ld)",
- hdev->name, PTR_ERR(skb));
- return;
- }
- kfree_skb(skb);
- skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
- hdev->name, PTR_ERR(skb));
- return;
- }
- if (skb->len != 13) {
- BT_ERR("%s: Exception info size mismatch", hdev->name);
- kfree_skb(skb);
- return;
- }
- BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
- kfree_skb(skb);
- }
- EXPORT_SYMBOL_GPL(btintel_hw_error);
- void btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
- {
- const char *variant;
- switch (ver->fw_variant) {
- case 0x06:
- variant = "Bootloader";
- break;
- case 0x23:
- variant = "Firmware";
- break;
- default:
- return;
- }
- BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
- variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
- ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
- }
- EXPORT_SYMBOL_GPL(btintel_version_info);
- int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
- const void *param)
- {
- while (plen > 0) {
- struct sk_buff *skb;
- u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
- cmd_param[0] = fragment_type;
- memcpy(cmd_param + 1, param, fragment_len);
- skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
- cmd_param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb))
- return PTR_ERR(skb);
- kfree_skb(skb);
- plen -= fragment_len;
- param += fragment_len;
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_secure_send);
- int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
- {
- const struct firmware *fw;
- struct sk_buff *skb;
- const u8 *fw_ptr;
- int err;
- err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
- if (err < 0) {
- bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
- ddc_name, err);
- return err;
- }
- bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
- fw_ptr = fw->data;
- /* DDC file contains one or more DDC structure which has
- * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
- */
- while (fw->size > fw_ptr - fw->data) {
- u8 cmd_plen = fw_ptr[0] + sizeof(u8);
- skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
- HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
- PTR_ERR(skb));
- release_firmware(fw);
- return PTR_ERR(skb);
- }
- fw_ptr += cmd_plen;
- kfree_skb(skb);
- }
- release_firmware(fw);
- bt_dev_info(hdev, "Applying Intel DDC parameters completed");
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
- int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
- {
- u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- struct sk_buff *skb;
- int err;
- if (debug)
- mask[1] |= 0x62;
- skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Setting Intel event mask failed (%d)",
- hdev->name, err);
- return err;
- }
- kfree_skb(skb);
- return 0;
- }
- EXPORT_SYMBOL_GPL(btintel_set_event_mask);
- int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
- {
- struct sk_buff *skb;
- u8 param[2];
- int err;
- param[0] = 0x01;
- param[1] = 0x00;
- skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
- hdev->name, err);
- return PTR_ERR(skb);
- }
- kfree_skb(skb);
- err = btintel_set_event_mask(hdev, debug);
- param[0] = 0x00;
- param[1] = 0x00;
- skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
- hdev->name, err);
- return PTR_ERR(skb);
- }
- kfree_skb(skb);
- return err;
- }
- EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
- /* ------- REGMAP IBT SUPPORT ------- */
- #define IBT_REG_MODE_8BIT 0x00
- #define IBT_REG_MODE_16BIT 0x01
- #define IBT_REG_MODE_32BIT 0x02
- struct regmap_ibt_context {
- struct hci_dev *hdev;
- __u16 op_write;
- __u16 op_read;
- };
- struct ibt_cp_reg_access {
- __le32 addr;
- __u8 mode;
- __u8 len;
- __u8 data[0];
- } __packed;
- struct ibt_rp_reg_access {
- __u8 status;
- __le32 addr;
- __u8 data[0];
- } __packed;
- static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
- void *val, size_t val_size)
- {
- struct regmap_ibt_context *ctx = context;
- struct ibt_cp_reg_access cp;
- struct ibt_rp_reg_access *rp;
- struct sk_buff *skb;
- int err = 0;
- if (reg_size != sizeof(__le32))
- return -EINVAL;
- switch (val_size) {
- case 1:
- cp.mode = IBT_REG_MODE_8BIT;
- break;
- case 2:
- cp.mode = IBT_REG_MODE_16BIT;
- break;
- case 4:
- cp.mode = IBT_REG_MODE_32BIT;
- break;
- default:
- return -EINVAL;
- }
- /* regmap provides a little-endian formatted addr */
- cp.addr = *(__le32 *)addr;
- cp.len = val_size;
- bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
- skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
- HCI_CMD_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
- le32_to_cpu(cp.addr), err);
- return err;
- }
- if (skb->len != sizeof(*rp) + val_size) {
- bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
- le32_to_cpu(cp.addr));
- err = -EINVAL;
- goto done;
- }
- rp = (struct ibt_rp_reg_access *)skb->data;
- if (rp->addr != cp.addr) {
- bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
- le32_to_cpu(rp->addr));
- err = -EINVAL;
- goto done;
- }
- memcpy(val, rp->data, val_size);
- done:
- kfree_skb(skb);
- return err;
- }
- static int regmap_ibt_gather_write(void *context,
- const void *addr, size_t reg_size,
- const void *val, size_t val_size)
- {
- struct regmap_ibt_context *ctx = context;
- struct ibt_cp_reg_access *cp;
- struct sk_buff *skb;
- int plen = sizeof(*cp) + val_size;
- u8 mode;
- int err = 0;
- if (reg_size != sizeof(__le32))
- return -EINVAL;
- switch (val_size) {
- case 1:
- mode = IBT_REG_MODE_8BIT;
- break;
- case 2:
- mode = IBT_REG_MODE_16BIT;
- break;
- case 4:
- mode = IBT_REG_MODE_32BIT;
- break;
- default:
- return -EINVAL;
- }
- cp = kmalloc(plen, GFP_KERNEL);
- if (!cp)
- return -ENOMEM;
- /* regmap provides a little-endian formatted addr/value */
- cp->addr = *(__le32 *)addr;
- cp->mode = mode;
- cp->len = val_size;
- memcpy(&cp->data, val, val_size);
- bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
- skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
- if (IS_ERR(skb)) {
- err = PTR_ERR(skb);
- bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
- le32_to_cpu(cp->addr), err);
- goto done;
- }
- kfree_skb(skb);
- done:
- kfree(cp);
- return err;
- }
- static int regmap_ibt_write(void *context, const void *data, size_t count)
- {
- /* data contains register+value, since we only support 32bit addr,
- * minimum data size is 4 bytes.
- */
- if (WARN_ONCE(count < 4, "Invalid register access"))
- return -EINVAL;
- return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
- }
- static void regmap_ibt_free_context(void *context)
- {
- kfree(context);
- }
- static struct regmap_bus regmap_ibt = {
- .read = regmap_ibt_read,
- .write = regmap_ibt_write,
- .gather_write = regmap_ibt_gather_write,
- .free_context = regmap_ibt_free_context,
- .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
- .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
- };
- /* Config is the same for all register regions */
- static const struct regmap_config regmap_ibt_cfg = {
- .name = "btintel_regmap",
- .reg_bits = 32,
- .val_bits = 32,
- };
- struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
- u16 opcode_write)
- {
- struct regmap_ibt_context *ctx;
- bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
- opcode_write);
- ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
- if (!ctx)
- return ERR_PTR(-ENOMEM);
- ctx->op_read = opcode_read;
- ctx->op_write = opcode_write;
- ctx->hdev = hdev;
- return regmap_init(&hdev->dev, ®map_ibt, ctx, ®map_ibt_cfg);
- }
- EXPORT_SYMBOL_GPL(btintel_regmap_init);
- MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
- MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
- MODULE_VERSION(VERSION);
- MODULE_LICENSE("GPL");
- MODULE_FIRMWARE("intel/ibt-11-5.sfi");
- MODULE_FIRMWARE("intel/ibt-11-5.ddc");
|