of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / drivers / bluetooth / btintel.c
blob1f13e617bf560d7dde4c30af30e7b6ded180f1c4
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>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
31 #include "btintel.h"
33 #define VERSION "0.1"
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;
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: Reading Intel device address failed (%d)",
47 hdev->name, err);
48 return err;
51 if (skb->len != sizeof(*bda)) {
52 BT_ERR("%s: Intel device address length mismatch", hdev->name);
53 kfree_skb(skb);
54 return -EIO;
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);
70 kfree_skb(skb);
72 return 0;
74 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
76 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
78 struct sk_buff *skb;
79 int err;
81 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
82 if (IS_ERR(skb)) {
83 err = PTR_ERR(skb);
84 BT_ERR("%s: Changing Intel device address failed (%d)",
85 hdev->name, err);
86 return err;
88 kfree_skb(skb);
90 return 0;
92 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
94 int btintel_set_diag(struct hci_dev *hdev, bool enable)
96 struct sk_buff *skb;
97 u8 param[3];
98 int err;
100 if (enable) {
101 param[0] = 0x03;
102 param[1] = 0x03;
103 param[2] = 0x03;
104 } else {
105 param[0] = 0x00;
106 param[1] = 0x00;
107 param[2] = 0x00;
110 skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
111 if (IS_ERR(skb)) {
112 err = PTR_ERR(skb);
113 if (err == -ENODATA)
114 goto done;
115 BT_ERR("%s: Changing Intel diagnostic mode failed (%d)",
116 hdev->name, err);
117 return err;
119 kfree_skb(skb);
121 done:
122 btintel_set_event_mask(hdev, enable);
123 return 0;
125 EXPORT_SYMBOL_GPL(btintel_set_diag);
127 int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
129 struct sk_buff *skb;
130 u8 param[2];
131 int err;
133 param[0] = 0x01;
134 param[1] = 0x00;
136 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
137 if (IS_ERR(skb)) {
138 err = PTR_ERR(skb);
139 BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
140 hdev->name, err);
141 return PTR_ERR(skb);
143 kfree_skb(skb);
145 err = btintel_set_diag(hdev, enable);
147 param[0] = 0x00;
148 param[1] = 0x00;
150 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
151 if (IS_ERR(skb)) {
152 err = PTR_ERR(skb);
153 BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
154 hdev->name, err);
155 return PTR_ERR(skb);
157 kfree_skb(skb);
159 return err;
161 EXPORT_SYMBOL_GPL(btintel_set_diag_mfg);
163 void btintel_hw_error(struct hci_dev *hdev, u8 code)
165 struct sk_buff *skb;
166 u8 type = 0x00;
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);
171 if (IS_ERR(skb)) {
172 BT_ERR("%s: Reset after hardware error failed (%ld)",
173 hdev->name, PTR_ERR(skb));
174 return;
176 kfree_skb(skb);
178 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
179 if (IS_ERR(skb)) {
180 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
181 hdev->name, PTR_ERR(skb));
182 return;
185 if (skb->len != 13) {
186 BT_ERR("%s: Exception info size mismatch", hdev->name);
187 kfree_skb(skb);
188 return;
191 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
193 kfree_skb(skb);
195 EXPORT_SYMBOL_GPL(btintel_hw_error);
197 void btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
199 const char *variant;
201 switch (ver->fw_variant) {
202 case 0x06:
203 variant = "Bootloader";
204 break;
205 case 0x23:
206 variant = "Firmware";
207 break;
208 default:
209 return;
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,
219 const void *param)
221 while (plen > 0) {
222 struct sk_buff *skb;
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);
230 if (IS_ERR(skb))
231 return PTR_ERR(skb);
233 kfree_skb(skb);
235 plen -= fragment_len;
236 param += fragment_len;
239 return 0;
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;
246 struct sk_buff *skb;
247 const u8 *fw_ptr;
248 int err;
250 err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
251 if (err < 0) {
252 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
253 ddc_name, err);
254 return err;
257 bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
259 fw_ptr = fw->data;
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,
268 HCI_INIT_TIMEOUT);
269 if (IS_ERR(skb)) {
270 bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
271 PTR_ERR(skb));
272 release_firmware(fw);
273 return PTR_ERR(skb);
276 fw_ptr += cmd_plen;
277 kfree_skb(skb);
280 release_firmware(fw);
282 bt_dev_info(hdev, "Applying Intel DDC parameters completed");
284 return 0;
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 };
291 struct sk_buff *skb;
292 int err;
294 if (debug)
295 mask[1] |= 0x62;
297 skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
298 if (IS_ERR(skb)) {
299 err = PTR_ERR(skb);
300 BT_ERR("%s: Setting Intel event mask failed (%d)",
301 hdev->name, err);
302 return err;
304 kfree_skb(skb);
306 return 0;
308 EXPORT_SYMBOL_GPL(btintel_set_event_mask);
310 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
312 struct sk_buff *skb;
313 u8 param[2];
314 int err;
316 param[0] = 0x01;
317 param[1] = 0x00;
319 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
320 if (IS_ERR(skb)) {
321 err = PTR_ERR(skb);
322 BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
323 hdev->name, err);
324 return PTR_ERR(skb);
326 kfree_skb(skb);
328 err = btintel_set_event_mask(hdev, debug);
330 param[0] = 0x00;
331 param[1] = 0x00;
333 skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
334 if (IS_ERR(skb)) {
335 err = PTR_ERR(skb);
336 BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
337 hdev->name, err);
338 return PTR_ERR(skb);
340 kfree_skb(skb);
342 return err;
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;
354 __u16 op_write;
355 __u16 op_read;
358 struct ibt_cp_reg_access {
359 __le32 addr;
360 __u8 mode;
361 __u8 len;
362 __u8 data[0];
363 } __packed;
365 struct ibt_rp_reg_access {
366 __u8 status;
367 __le32 addr;
368 __u8 data[0];
369 } __packed;
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;
377 struct sk_buff *skb;
378 int err = 0;
380 if (reg_size != sizeof(__le32))
381 return -EINVAL;
383 switch (val_size) {
384 case 1:
385 cp.mode = IBT_REG_MODE_8BIT;
386 break;
387 case 2:
388 cp.mode = IBT_REG_MODE_16BIT;
389 break;
390 case 4:
391 cp.mode = IBT_REG_MODE_32BIT;
392 break;
393 default:
394 return -EINVAL;
397 /* regmap provides a little-endian formatted addr */
398 cp.addr = *(__le32 *)addr;
399 cp.len = val_size;
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,
404 HCI_CMD_TIMEOUT);
405 if (IS_ERR(skb)) {
406 err = PTR_ERR(skb);
407 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
408 le32_to_cpu(cp.addr), err);
409 return 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));
415 err = -EINVAL;
416 goto done;
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));
424 err = -EINVAL;
425 goto done;
428 memcpy(val, rp->data, val_size);
430 done:
431 kfree_skb(skb);
432 return err;
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;
441 struct sk_buff *skb;
442 int plen = sizeof(*cp) + val_size;
443 u8 mode;
444 int err = 0;
446 if (reg_size != sizeof(__le32))
447 return -EINVAL;
449 switch (val_size) {
450 case 1:
451 mode = IBT_REG_MODE_8BIT;
452 break;
453 case 2:
454 mode = IBT_REG_MODE_16BIT;
455 break;
456 case 4:
457 mode = IBT_REG_MODE_32BIT;
458 break;
459 default:
460 return -EINVAL;
463 cp = kmalloc(plen, GFP_KERNEL);
464 if (!cp)
465 return -ENOMEM;
467 /* regmap provides a little-endian formatted addr/value */
468 cp->addr = *(__le32 *)addr;
469 cp->mode = mode;
470 cp->len = val_size;
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);
476 if (IS_ERR(skb)) {
477 err = PTR_ERR(skb);
478 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
479 le32_to_cpu(cp->addr), err);
480 goto done;
482 kfree_skb(skb);
484 done:
485 kfree(cp);
486 return 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"))
495 return -EINVAL;
497 return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
500 static void regmap_ibt_free_context(void *context)
502 kfree(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",
517 .reg_bits = 32,
518 .val_bits = 32,
521 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
522 u16 opcode_write)
524 struct regmap_ibt_context *ctx;
526 bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
527 opcode_write);
529 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
530 if (!ctx)
531 return ERR_PTR(-ENOMEM);
533 ctx->op_read = opcode_read;
534 ctx->op_write = opcode_write;
535 ctx->hdev = hdev;
537 return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_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");