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>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
35 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
37 int btintel_check_bdaddr(struct hci_dev
*hdev
)
39 struct hci_rp_read_bd_addr
*bda
;
42 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
45 int err
= PTR_ERR(skb
);
46 BT_ERR("%s: Reading Intel device address failed (%d)",
51 if (skb
->len
!= sizeof(*bda
)) {
52 BT_ERR("%s: Intel device address length mismatch", hdev
->name
);
57 bda
= (struct hci_rp_read_bd_addr
*)skb
->data
;
59 /* For some Intel based controllers, the default Bluetooth device
60 * address 00:03:19:9E:8B:00 can be found. These controllers are
61 * fully operational, but have the danger of duplicate addresses
62 * and that in turn can cause problems with Bluetooth operation.
64 if (!bacmp(&bda
->bdaddr
, BDADDR_INTEL
)) {
65 BT_ERR("%s: Found Intel default device address (%pMR)",
66 hdev
->name
, &bda
->bdaddr
);
67 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
74 EXPORT_SYMBOL_GPL(btintel_check_bdaddr
);
76 int btintel_set_bdaddr(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
81 skb
= __hci_cmd_sync(hdev
, 0xfc31, 6, bdaddr
, HCI_INIT_TIMEOUT
);
84 BT_ERR("%s: Changing Intel device address failed (%d)",
92 EXPORT_SYMBOL_GPL(btintel_set_bdaddr
);
94 int btintel_set_diag(struct hci_dev
*hdev
, bool enable
)
110 skb
= __hci_cmd_sync(hdev
, 0xfc43, 3, param
, HCI_INIT_TIMEOUT
);
115 BT_ERR("%s: Changing Intel diagnostic mode failed (%d)",
122 btintel_set_event_mask(hdev
, enable
);
125 EXPORT_SYMBOL_GPL(btintel_set_diag
);
127 int btintel_set_diag_mfg(struct hci_dev
*hdev
, bool enable
)
136 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, param
, HCI_INIT_TIMEOUT
);
139 BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
145 err
= btintel_set_diag(hdev
, enable
);
150 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, param
, HCI_INIT_TIMEOUT
);
153 BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
161 EXPORT_SYMBOL_GPL(btintel_set_diag_mfg
);
163 void btintel_hw_error(struct hci_dev
*hdev
, u8 code
)
168 BT_ERR("%s: Hardware error 0x%2.2x", hdev
->name
, code
);
170 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
172 BT_ERR("%s: Reset after hardware error failed (%ld)",
173 hdev
->name
, PTR_ERR(skb
));
178 skb
= __hci_cmd_sync(hdev
, 0xfc22, 1, &type
, HCI_INIT_TIMEOUT
);
180 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
181 hdev
->name
, PTR_ERR(skb
));
185 if (skb
->len
!= 13) {
186 BT_ERR("%s: Exception info size mismatch", hdev
->name
);
191 BT_ERR("%s: Exception info %s", hdev
->name
, (char *)(skb
->data
+ 1));
195 EXPORT_SYMBOL_GPL(btintel_hw_error
);
197 void btintel_version_info(struct hci_dev
*hdev
, struct intel_version
*ver
)
201 switch (ver
->fw_variant
) {
203 variant
= "Bootloader";
206 variant
= "Firmware";
212 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev
->name
,
213 variant
, ver
->fw_revision
>> 4, ver
->fw_revision
& 0x0f,
214 ver
->fw_build_num
, ver
->fw_build_ww
, 2000 + ver
->fw_build_yy
);
216 EXPORT_SYMBOL_GPL(btintel_version_info
);
218 int btintel_secure_send(struct hci_dev
*hdev
, u8 fragment_type
, u32 plen
,
223 u8 cmd_param
[253], fragment_len
= (plen
> 252) ? 252 : plen
;
225 cmd_param
[0] = fragment_type
;
226 memcpy(cmd_param
+ 1, param
, fragment_len
);
228 skb
= __hci_cmd_sync(hdev
, 0xfc09, fragment_len
+ 1,
229 cmd_param
, HCI_INIT_TIMEOUT
);
235 plen
-= fragment_len
;
236 param
+= fragment_len
;
241 EXPORT_SYMBOL_GPL(btintel_secure_send
);
243 int btintel_load_ddc_config(struct hci_dev
*hdev
, const char *ddc_name
)
245 const struct firmware
*fw
;
250 err
= request_firmware_direct(&fw
, ddc_name
, &hdev
->dev
);
252 bt_dev_err(hdev
, "Failed to load Intel DDC file %s (%d)",
257 bt_dev_info(hdev
, "Found Intel DDC parameters: %s", ddc_name
);
261 /* DDC file contains one or more DDC structure which has
262 * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
264 while (fw
->size
> fw_ptr
- fw
->data
) {
265 u8 cmd_plen
= fw_ptr
[0] + sizeof(u8
);
267 skb
= __hci_cmd_sync(hdev
, 0xfc8b, cmd_plen
, fw_ptr
,
270 bt_dev_err(hdev
, "Failed to send Intel_Write_DDC (%ld)",
272 release_firmware(fw
);
280 release_firmware(fw
);
282 bt_dev_info(hdev
, "Applying Intel DDC parameters completed");
286 EXPORT_SYMBOL_GPL(btintel_load_ddc_config
);
288 int btintel_set_event_mask(struct hci_dev
*hdev
, bool debug
)
290 u8 mask
[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
297 skb
= __hci_cmd_sync(hdev
, 0xfc52, 8, mask
, HCI_INIT_TIMEOUT
);
300 BT_ERR("%s: Setting Intel event mask failed (%d)",
308 EXPORT_SYMBOL_GPL(btintel_set_event_mask
);
310 int btintel_set_event_mask_mfg(struct hci_dev
*hdev
, bool debug
)
319 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, param
, HCI_INIT_TIMEOUT
);
322 BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
328 err
= btintel_set_event_mask(hdev
, debug
);
333 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, param
, HCI_INIT_TIMEOUT
);
336 BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
344 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg
);
346 /* ------- REGMAP IBT SUPPORT ------- */
348 #define IBT_REG_MODE_8BIT 0x00
349 #define IBT_REG_MODE_16BIT 0x01
350 #define IBT_REG_MODE_32BIT 0x02
352 struct regmap_ibt_context
{
353 struct hci_dev
*hdev
;
358 struct ibt_cp_reg_access
{
365 struct ibt_rp_reg_access
{
371 static int regmap_ibt_read(void *context
, const void *addr
, size_t reg_size
,
372 void *val
, size_t val_size
)
374 struct regmap_ibt_context
*ctx
= context
;
375 struct ibt_cp_reg_access cp
;
376 struct ibt_rp_reg_access
*rp
;
380 if (reg_size
!= sizeof(__le32
))
385 cp
.mode
= IBT_REG_MODE_8BIT
;
388 cp
.mode
= IBT_REG_MODE_16BIT
;
391 cp
.mode
= IBT_REG_MODE_32BIT
;
397 /* regmap provides a little-endian formatted addr */
398 cp
.addr
= *(__le32
*)addr
;
401 bt_dev_dbg(ctx
->hdev
, "Register (0x%x) read", le32_to_cpu(cp
.addr
));
403 skb
= hci_cmd_sync(ctx
->hdev
, ctx
->op_read
, sizeof(cp
), &cp
,
407 bt_dev_err(ctx
->hdev
, "regmap: Register (0x%x) read error (%d)",
408 le32_to_cpu(cp
.addr
), err
);
412 if (skb
->len
!= sizeof(*rp
) + val_size
) {
413 bt_dev_err(ctx
->hdev
, "regmap: Register (0x%x) read error, bad len",
414 le32_to_cpu(cp
.addr
));
419 rp
= (struct ibt_rp_reg_access
*)skb
->data
;
421 if (rp
->addr
!= cp
.addr
) {
422 bt_dev_err(ctx
->hdev
, "regmap: Register (0x%x) read error, bad addr",
423 le32_to_cpu(rp
->addr
));
428 memcpy(val
, rp
->data
, val_size
);
435 static int regmap_ibt_gather_write(void *context
,
436 const void *addr
, size_t reg_size
,
437 const void *val
, size_t val_size
)
439 struct regmap_ibt_context
*ctx
= context
;
440 struct ibt_cp_reg_access
*cp
;
442 int plen
= sizeof(*cp
) + val_size
;
446 if (reg_size
!= sizeof(__le32
))
451 mode
= IBT_REG_MODE_8BIT
;
454 mode
= IBT_REG_MODE_16BIT
;
457 mode
= IBT_REG_MODE_32BIT
;
463 cp
= kmalloc(plen
, GFP_KERNEL
);
467 /* regmap provides a little-endian formatted addr/value */
468 cp
->addr
= *(__le32
*)addr
;
471 memcpy(&cp
->data
, val
, val_size
);
473 bt_dev_dbg(ctx
->hdev
, "Register (0x%x) write", le32_to_cpu(cp
->addr
));
475 skb
= hci_cmd_sync(ctx
->hdev
, ctx
->op_write
, plen
, cp
, HCI_CMD_TIMEOUT
);
478 bt_dev_err(ctx
->hdev
, "regmap: Register (0x%x) write error (%d)",
479 le32_to_cpu(cp
->addr
), err
);
489 static int regmap_ibt_write(void *context
, const void *data
, size_t count
)
491 /* data contains register+value, since we only support 32bit addr,
492 * minimum data size is 4 bytes.
494 if (WARN_ONCE(count
< 4, "Invalid register access"))
497 return regmap_ibt_gather_write(context
, data
, 4, data
+ 4, count
- 4);
500 static void regmap_ibt_free_context(void *context
)
505 static struct regmap_bus regmap_ibt
= {
506 .read
= regmap_ibt_read
,
507 .write
= regmap_ibt_write
,
508 .gather_write
= regmap_ibt_gather_write
,
509 .free_context
= regmap_ibt_free_context
,
510 .reg_format_endian_default
= REGMAP_ENDIAN_LITTLE
,
511 .val_format_endian_default
= REGMAP_ENDIAN_LITTLE
,
514 /* Config is the same for all register regions */
515 static const struct regmap_config regmap_ibt_cfg
= {
516 .name
= "btintel_regmap",
521 struct regmap
*btintel_regmap_init(struct hci_dev
*hdev
, u16 opcode_read
,
524 struct regmap_ibt_context
*ctx
;
526 bt_dev_info(hdev
, "regmap: Init R%x-W%x region", opcode_read
,
529 ctx
= kzalloc(sizeof(*ctx
), GFP_KERNEL
);
531 return ERR_PTR(-ENOMEM
);
533 ctx
->op_read
= opcode_read
;
534 ctx
->op_write
= opcode_write
;
537 return regmap_init(&hdev
->dev
, ®map_ibt
, ctx
, ®map_ibt_cfg
);
539 EXPORT_SYMBOL_GPL(btintel_regmap_init
);
541 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
542 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION
);
543 MODULE_VERSION(VERSION
);
544 MODULE_LICENSE("GPL");
545 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
546 MODULE_FIRMWARE("intel/ibt-11-5.ddc");