btintel.c 13 KB

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