btintel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. *
  3. * Bluetooth support for Intel devices
  4. *
  5. * Copyright (C) 2015 Intel Corporation
  6. *
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/firmware.h>
  25. #include <linux/regmap.h>
  26. #include <net/bluetooth/bluetooth.h>
  27. #include <net/bluetooth/hci_core.h>
  28. #include "btintel.h"
  29. #define VERSION "0.1"
  30. #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
  31. int btintel_check_bdaddr(struct hci_dev *hdev)
  32. {
  33. struct hci_rp_read_bd_addr *bda;
  34. 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);
  39. BT_ERR("%s: Reading Intel device address failed (%d)",
  40. hdev->name, err);
  41. return err;
  42. }
  43. if (skb->len != sizeof(*bda)) {
  44. BT_ERR("%s: Intel device address length mismatch", hdev->name);
  45. kfree_skb(skb);
  46. return -EIO;
  47. }
  48. bda = (struct hci_rp_read_bd_addr *)skb->data;
  49. /* For some Intel based controllers, the default Bluetooth device
  50. * address 00:03:19:9E:8B:00 can be found. These controllers are
  51. * fully operational, but have the danger of duplicate addresses
  52. * and that in turn can cause problems with Bluetooth operation.
  53. */
  54. if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
  55. BT_ERR("%s: Found Intel default device address (%pMR)",
  56. hdev->name, &bda->bdaddr);
  57. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  58. }
  59. kfree_skb(skb);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
  63. int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
  64. {
  65. struct sk_buff *skb;
  66. int err;
  67. skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
  68. if (IS_ERR(skb)) {
  69. err = PTR_ERR(skb);
  70. BT_ERR("%s: Changing Intel device address failed (%d)",
  71. hdev->name, err);
  72. return err;
  73. }
  74. kfree_skb(skb);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
  78. int btintel_set_diag(struct hci_dev *hdev, bool enable)
  79. {
  80. struct sk_buff *skb;
  81. u8 param[3];
  82. int err;
  83. if (enable) {
  84. param[0] = 0x03;
  85. param[1] = 0x03;
  86. param[2] = 0x03;
  87. } else {
  88. param[0] = 0x00;
  89. param[1] = 0x00;
  90. param[2] = 0x00;
  91. }
  92. skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
  93. if (IS_ERR(skb)) {
  94. err = PTR_ERR(skb);
  95. if (err == -ENODATA)
  96. goto done;
  97. BT_ERR("%s: Changing Intel diagnostic mode failed (%d)",
  98. hdev->name, err);
  99. return err;
  100. }
  101. kfree_skb(skb);
  102. done:
  103. btintel_set_event_mask(hdev, enable);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(btintel_set_diag);
  107. int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
  108. {
  109. struct sk_buff *skb;
  110. u8 param[2];
  111. int err;
  112. param[0] = 0x01;
  113. param[1] = 0x00;
  114. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
  115. if (IS_ERR(skb)) {
  116. err = PTR_ERR(skb);
  117. BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
  118. hdev->name, err);
  119. return PTR_ERR(skb);
  120. }
  121. kfree_skb(skb);
  122. err = btintel_set_diag(hdev, enable);
  123. param[0] = 0x00;
  124. param[1] = 0x00;
  125. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
  126. if (IS_ERR(skb)) {
  127. err = PTR_ERR(skb);
  128. BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
  129. hdev->name, err);
  130. return PTR_ERR(skb);
  131. }
  132. kfree_skb(skb);
  133. return err;
  134. }
  135. EXPORT_SYMBOL_GPL(btintel_set_diag_mfg);
  136. void btintel_hw_error(struct hci_dev *hdev, u8 code)
  137. {
  138. struct sk_buff *skb;
  139. u8 type = 0x00;
  140. BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
  141. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
  142. if (IS_ERR(skb)) {
  143. BT_ERR("%s: Reset after hardware error failed (%ld)",
  144. hdev->name, PTR_ERR(skb));
  145. return;
  146. }
  147. kfree_skb(skb);
  148. skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
  149. if (IS_ERR(skb)) {
  150. BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
  151. hdev->name, PTR_ERR(skb));
  152. return;
  153. }
  154. if (skb->len != 13) {
  155. BT_ERR("%s: Exception info size mismatch", hdev->name);
  156. kfree_skb(skb);
  157. return;
  158. }
  159. BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
  160. kfree_skb(skb);
  161. }
  162. EXPORT_SYMBOL_GPL(btintel_hw_error);
  163. void btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
  164. {
  165. const char *variant;
  166. switch (ver->fw_variant) {
  167. case 0x06:
  168. variant = "Bootloader";
  169. break;
  170. case 0x23:
  171. variant = "Firmware";
  172. break;
  173. default:
  174. return;
  175. }
  176. BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
  177. variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
  178. ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
  179. }
  180. EXPORT_SYMBOL_GPL(btintel_version_info);
  181. int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
  182. const void *param)
  183. {
  184. while (plen > 0) {
  185. struct sk_buff *skb;
  186. u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
  187. cmd_param[0] = fragment_type;
  188. memcpy(cmd_param + 1, param, fragment_len);
  189. skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
  190. cmd_param, HCI_INIT_TIMEOUT);
  191. if (IS_ERR(skb))
  192. return PTR_ERR(skb);
  193. kfree_skb(skb);
  194. plen -= fragment_len;
  195. param += fragment_len;
  196. }
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(btintel_secure_send);
  200. int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
  201. {
  202. const struct firmware *fw;
  203. struct sk_buff *skb;
  204. const u8 *fw_ptr;
  205. int err;
  206. err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
  207. if (err < 0) {
  208. bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
  209. ddc_name, err);
  210. return err;
  211. }
  212. bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
  213. fw_ptr = fw->data;
  214. /* DDC file contains one or more DDC structure which has
  215. * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
  216. */
  217. while (fw->size > fw_ptr - fw->data) {
  218. u8 cmd_plen = fw_ptr[0] + sizeof(u8);
  219. skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
  220. HCI_INIT_TIMEOUT);
  221. if (IS_ERR(skb)) {
  222. bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
  223. PTR_ERR(skb));
  224. release_firmware(fw);
  225. return PTR_ERR(skb);
  226. }
  227. fw_ptr += cmd_plen;
  228. kfree_skb(skb);
  229. }
  230. release_firmware(fw);
  231. bt_dev_info(hdev, "Applying Intel DDC parameters completed");
  232. return 0;
  233. }
  234. EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
  235. int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
  236. {
  237. u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  238. struct sk_buff *skb;
  239. int err;
  240. if (debug)
  241. mask[1] |= 0x62;
  242. skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
  243. if (IS_ERR(skb)) {
  244. err = PTR_ERR(skb);
  245. BT_ERR("%s: Setting Intel event mask failed (%d)",
  246. hdev->name, err);
  247. return err;
  248. }
  249. kfree_skb(skb);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(btintel_set_event_mask);
  253. int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
  254. {
  255. struct sk_buff *skb;
  256. u8 param[2];
  257. int err;
  258. param[0] = 0x01;
  259. param[1] = 0x00;
  260. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
  261. if (IS_ERR(skb)) {
  262. err = PTR_ERR(skb);
  263. BT_ERR("%s: Entering Intel manufacturer mode failed (%d)",
  264. hdev->name, err);
  265. return PTR_ERR(skb);
  266. }
  267. kfree_skb(skb);
  268. err = btintel_set_event_mask(hdev, debug);
  269. param[0] = 0x00;
  270. param[1] = 0x00;
  271. skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_INIT_TIMEOUT);
  272. if (IS_ERR(skb)) {
  273. err = PTR_ERR(skb);
  274. BT_ERR("%s: Leaving Intel manufacturer mode failed (%d)",
  275. hdev->name, err);
  276. return PTR_ERR(skb);
  277. }
  278. kfree_skb(skb);
  279. return err;
  280. }
  281. EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
  282. /* ------- REGMAP IBT SUPPORT ------- */
  283. #define IBT_REG_MODE_8BIT 0x00
  284. #define IBT_REG_MODE_16BIT 0x01
  285. #define IBT_REG_MODE_32BIT 0x02
  286. struct regmap_ibt_context {
  287. struct hci_dev *hdev;
  288. __u16 op_write;
  289. __u16 op_read;
  290. };
  291. struct ibt_cp_reg_access {
  292. __le32 addr;
  293. __u8 mode;
  294. __u8 len;
  295. __u8 data[0];
  296. } __packed;
  297. struct ibt_rp_reg_access {
  298. __u8 status;
  299. __le32 addr;
  300. __u8 data[0];
  301. } __packed;
  302. static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
  303. void *val, size_t val_size)
  304. {
  305. struct regmap_ibt_context *ctx = context;
  306. struct ibt_cp_reg_access cp;
  307. struct ibt_rp_reg_access *rp;
  308. struct sk_buff *skb;
  309. int err = 0;
  310. if (reg_size != sizeof(__le32))
  311. return -EINVAL;
  312. switch (val_size) {
  313. case 1:
  314. cp.mode = IBT_REG_MODE_8BIT;
  315. break;
  316. case 2:
  317. cp.mode = IBT_REG_MODE_16BIT;
  318. break;
  319. case 4:
  320. cp.mode = IBT_REG_MODE_32BIT;
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. /* regmap provides a little-endian formatted addr */
  326. cp.addr = *(__le32 *)addr;
  327. cp.len = val_size;
  328. bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
  329. skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
  330. HCI_CMD_TIMEOUT);
  331. if (IS_ERR(skb)) {
  332. err = PTR_ERR(skb);
  333. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
  334. le32_to_cpu(cp.addr), err);
  335. return err;
  336. }
  337. if (skb->len != sizeof(*rp) + val_size) {
  338. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
  339. le32_to_cpu(cp.addr));
  340. err = -EINVAL;
  341. goto done;
  342. }
  343. rp = (struct ibt_rp_reg_access *)skb->data;
  344. if (rp->addr != cp.addr) {
  345. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
  346. le32_to_cpu(rp->addr));
  347. err = -EINVAL;
  348. goto done;
  349. }
  350. memcpy(val, rp->data, val_size);
  351. done:
  352. kfree_skb(skb);
  353. return err;
  354. }
  355. static int regmap_ibt_gather_write(void *context,
  356. const void *addr, size_t reg_size,
  357. const void *val, size_t val_size)
  358. {
  359. struct regmap_ibt_context *ctx = context;
  360. struct ibt_cp_reg_access *cp;
  361. struct sk_buff *skb;
  362. int plen = sizeof(*cp) + val_size;
  363. u8 mode;
  364. int err = 0;
  365. if (reg_size != sizeof(__le32))
  366. return -EINVAL;
  367. switch (val_size) {
  368. case 1:
  369. mode = IBT_REG_MODE_8BIT;
  370. break;
  371. case 2:
  372. mode = IBT_REG_MODE_16BIT;
  373. break;
  374. case 4:
  375. mode = IBT_REG_MODE_32BIT;
  376. break;
  377. default:
  378. return -EINVAL;
  379. }
  380. cp = kmalloc(plen, GFP_KERNEL);
  381. if (!cp)
  382. return -ENOMEM;
  383. /* regmap provides a little-endian formatted addr/value */
  384. cp->addr = *(__le32 *)addr;
  385. cp->mode = mode;
  386. cp->len = val_size;
  387. memcpy(&cp->data, val, val_size);
  388. bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
  389. skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
  390. if (IS_ERR(skb)) {
  391. err = PTR_ERR(skb);
  392. bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
  393. le32_to_cpu(cp->addr), err);
  394. goto done;
  395. }
  396. kfree_skb(skb);
  397. done:
  398. kfree(cp);
  399. return err;
  400. }
  401. static int regmap_ibt_write(void *context, const void *data, size_t count)
  402. {
  403. /* data contains register+value, since we only support 32bit addr,
  404. * minimum data size is 4 bytes.
  405. */
  406. if (WARN_ONCE(count < 4, "Invalid register access"))
  407. return -EINVAL;
  408. return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
  409. }
  410. static void regmap_ibt_free_context(void *context)
  411. {
  412. kfree(context);
  413. }
  414. static struct regmap_bus regmap_ibt = {
  415. .read = regmap_ibt_read,
  416. .write = regmap_ibt_write,
  417. .gather_write = regmap_ibt_gather_write,
  418. .free_context = regmap_ibt_free_context,
  419. .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
  420. .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
  421. };
  422. /* Config is the same for all register regions */
  423. static const struct regmap_config regmap_ibt_cfg = {
  424. .name = "btintel_regmap",
  425. .reg_bits = 32,
  426. .val_bits = 32,
  427. };
  428. struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
  429. u16 opcode_write)
  430. {
  431. struct regmap_ibt_context *ctx;
  432. bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
  433. opcode_write);
  434. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  435. if (!ctx)
  436. return ERR_PTR(-ENOMEM);
  437. ctx->op_read = opcode_read;
  438. ctx->op_write = opcode_write;
  439. ctx->hdev = hdev;
  440. return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
  441. }
  442. EXPORT_SYMBOL_GPL(btintel_regmap_init);
  443. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  444. MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
  445. MODULE_VERSION(VERSION);
  446. MODULE_LICENSE("GPL");
  447. MODULE_FIRMWARE("intel/ibt-11-5.sfi");
  448. MODULE_FIRMWARE("intel/ibt-11-5.ddc");