hci_intel.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. *
  3. * Bluetooth HCI UART driver 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/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/firmware.h>
  27. #include <linux/wait.h>
  28. #include <net/bluetooth/bluetooth.h>
  29. #include <net/bluetooth/hci_core.h>
  30. #include "hci_uart.h"
  31. #include "btintel.h"
  32. #define STATE_BOOTLOADER 0
  33. #define STATE_DOWNLOADING 1
  34. #define STATE_FIRMWARE_LOADED 2
  35. #define STATE_FIRMWARE_FAILED 3
  36. #define STATE_BOOTING 4
  37. struct intel_data {
  38. struct sk_buff *rx_skb;
  39. struct sk_buff_head txq;
  40. unsigned long flags;
  41. };
  42. static int intel_open(struct hci_uart *hu)
  43. {
  44. struct intel_data *intel;
  45. BT_DBG("hu %p", hu);
  46. intel = kzalloc(sizeof(*intel), GFP_KERNEL);
  47. if (!intel)
  48. return -ENOMEM;
  49. skb_queue_head_init(&intel->txq);
  50. hu->priv = intel;
  51. return 0;
  52. }
  53. static int intel_close(struct hci_uart *hu)
  54. {
  55. struct intel_data *intel = hu->priv;
  56. BT_DBG("hu %p", hu);
  57. skb_queue_purge(&intel->txq);
  58. kfree_skb(intel->rx_skb);
  59. kfree(intel);
  60. hu->priv = NULL;
  61. return 0;
  62. }
  63. static int intel_flush(struct hci_uart *hu)
  64. {
  65. struct intel_data *intel = hu->priv;
  66. BT_DBG("hu %p", hu);
  67. skb_queue_purge(&intel->txq);
  68. return 0;
  69. }
  70. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  71. {
  72. struct sk_buff *skb;
  73. struct hci_event_hdr *hdr;
  74. struct hci_ev_cmd_complete *evt;
  75. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
  76. if (!skb)
  77. return -ENOMEM;
  78. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  79. hdr->evt = HCI_EV_CMD_COMPLETE;
  80. hdr->plen = sizeof(*evt) + 1;
  81. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  82. evt->ncmd = 0x01;
  83. evt->opcode = cpu_to_le16(opcode);
  84. *skb_put(skb, 1) = 0x00;
  85. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  86. return hci_recv_frame(hdev, skb);
  87. }
  88. static void intel_version_info(struct hci_dev *hdev,
  89. struct intel_version *ver)
  90. {
  91. const char *variant;
  92. switch (ver->fw_variant) {
  93. case 0x06:
  94. variant = "Bootloader";
  95. break;
  96. case 0x23:
  97. variant = "Firmware";
  98. break;
  99. default:
  100. return;
  101. }
  102. BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
  103. variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
  104. ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
  105. }
  106. static int intel_setup(struct hci_uart *hu)
  107. {
  108. static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
  109. 0x00, 0x08, 0x04, 0x00 };
  110. struct intel_data *intel = hu->priv;
  111. struct hci_dev *hdev = hu->hdev;
  112. struct sk_buff *skb;
  113. struct intel_version *ver;
  114. struct intel_boot_params *params;
  115. const struct firmware *fw;
  116. const u8 *fw_ptr;
  117. char fwname[64];
  118. u32 frag_len;
  119. ktime_t calltime, delta, rettime;
  120. unsigned long long duration;
  121. int err;
  122. BT_DBG("%s", hdev->name);
  123. hu->hdev->set_bdaddr = btintel_set_bdaddr;
  124. calltime = ktime_get();
  125. set_bit(STATE_BOOTLOADER, &intel->flags);
  126. /* Read the Intel version information to determine if the device
  127. * is in bootloader mode or if it already has operational firmware
  128. * loaded.
  129. */
  130. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  131. if (IS_ERR(skb)) {
  132. BT_ERR("%s: Reading Intel version information failed (%ld)",
  133. hdev->name, PTR_ERR(skb));
  134. return PTR_ERR(skb);
  135. }
  136. if (skb->len != sizeof(*ver)) {
  137. BT_ERR("%s: Intel version event size mismatch", hdev->name);
  138. kfree_skb(skb);
  139. return -EILSEQ;
  140. }
  141. ver = (struct intel_version *)skb->data;
  142. if (ver->status) {
  143. BT_ERR("%s: Intel version command failure (%02x)",
  144. hdev->name, ver->status);
  145. err = -bt_to_errno(ver->status);
  146. kfree_skb(skb);
  147. return err;
  148. }
  149. /* The hardware platform number has a fixed value of 0x37 and
  150. * for now only accept this single value.
  151. */
  152. if (ver->hw_platform != 0x37) {
  153. BT_ERR("%s: Unsupported Intel hardware platform (%u)",
  154. hdev->name, ver->hw_platform);
  155. kfree_skb(skb);
  156. return -EINVAL;
  157. }
  158. /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
  159. * supported by this firmware loading method. This check has been
  160. * put in place to ensure correct forward compatibility options
  161. * when newer hardware variants come along.
  162. */
  163. if (ver->hw_variant != 0x0b) {
  164. BT_ERR("%s: Unsupported Intel hardware variant (%u)",
  165. hdev->name, ver->hw_variant);
  166. kfree_skb(skb);
  167. return -EINVAL;
  168. }
  169. intel_version_info(hdev, ver);
  170. /* The firmware variant determines if the device is in bootloader
  171. * mode or is running operational firmware. The value 0x06 identifies
  172. * the bootloader and the value 0x23 identifies the operational
  173. * firmware.
  174. *
  175. * When the operational firmware is already present, then only
  176. * the check for valid Bluetooth device address is needed. This
  177. * determines if the device will be added as configured or
  178. * unconfigured controller.
  179. *
  180. * It is not possible to use the Secure Boot Parameters in this
  181. * case since that command is only available in bootloader mode.
  182. */
  183. if (ver->fw_variant == 0x23) {
  184. kfree_skb(skb);
  185. clear_bit(STATE_BOOTLOADER, &intel->flags);
  186. btintel_check_bdaddr(hdev);
  187. return 0;
  188. }
  189. /* If the device is not in bootloader mode, then the only possible
  190. * choice is to return an error and abort the device initialization.
  191. */
  192. if (ver->fw_variant != 0x06) {
  193. BT_ERR("%s: Unsupported Intel firmware variant (%u)",
  194. hdev->name, ver->fw_variant);
  195. kfree_skb(skb);
  196. return -ENODEV;
  197. }
  198. kfree_skb(skb);
  199. /* Read the secure boot parameters to identify the operating
  200. * details of the bootloader.
  201. */
  202. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  203. if (IS_ERR(skb)) {
  204. BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
  205. hdev->name, PTR_ERR(skb));
  206. return PTR_ERR(skb);
  207. }
  208. if (skb->len != sizeof(*params)) {
  209. BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
  210. kfree_skb(skb);
  211. return -EILSEQ;
  212. }
  213. params = (struct intel_boot_params *)skb->data;
  214. if (params->status) {
  215. BT_ERR("%s: Intel boot parameters command failure (%02x)",
  216. hdev->name, params->status);
  217. err = -bt_to_errno(params->status);
  218. kfree_skb(skb);
  219. return err;
  220. }
  221. BT_INFO("%s: Device revision is %u", hdev->name,
  222. le16_to_cpu(params->dev_revid));
  223. BT_INFO("%s: Secure boot is %s", hdev->name,
  224. params->secure_boot ? "enabled" : "disabled");
  225. BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
  226. params->min_fw_build_nn, params->min_fw_build_cw,
  227. 2000 + params->min_fw_build_yy);
  228. /* It is required that every single firmware fragment is acknowledged
  229. * with a command complete event. If the boot parameters indicate
  230. * that this bootloader does not send them, then abort the setup.
  231. */
  232. if (params->limited_cce != 0x00) {
  233. BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
  234. hdev->name, params->limited_cce);
  235. kfree_skb(skb);
  236. return -EINVAL;
  237. }
  238. /* If the OTP has no valid Bluetooth device address, then there will
  239. * also be no valid address for the operational firmware.
  240. */
  241. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  242. BT_INFO("%s: No device address configured", hdev->name);
  243. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  244. }
  245. /* With this Intel bootloader only the hardware variant and device
  246. * revision information are used to select the right firmware.
  247. *
  248. * Currently this bootloader support is limited to hardware variant
  249. * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
  250. */
  251. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
  252. le16_to_cpu(params->dev_revid));
  253. err = request_firmware(&fw, fwname, &hdev->dev);
  254. if (err < 0) {
  255. BT_ERR("%s: Failed to load Intel firmware file (%d)",
  256. hdev->name, err);
  257. kfree_skb(skb);
  258. return err;
  259. }
  260. BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
  261. kfree_skb(skb);
  262. if (fw->size < 644) {
  263. BT_ERR("%s: Invalid size of firmware file (%zu)",
  264. hdev->name, fw->size);
  265. err = -EBADF;
  266. goto done;
  267. }
  268. set_bit(STATE_DOWNLOADING, &intel->flags);
  269. /* Start the firmware download transaction with the Init fragment
  270. * represented by the 128 bytes of CSS header.
  271. */
  272. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  273. if (err < 0) {
  274. BT_ERR("%s: Failed to send firmware header (%d)",
  275. hdev->name, err);
  276. goto done;
  277. }
  278. /* Send the 256 bytes of public key information from the firmware
  279. * as the PKey fragment.
  280. */
  281. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  282. if (err < 0) {
  283. BT_ERR("%s: Failed to send firmware public key (%d)",
  284. hdev->name, err);
  285. goto done;
  286. }
  287. /* Send the 256 bytes of signature information from the firmware
  288. * as the Sign fragment.
  289. */
  290. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  291. if (err < 0) {
  292. BT_ERR("%s: Failed to send firmware signature (%d)",
  293. hdev->name, err);
  294. goto done;
  295. }
  296. fw_ptr = fw->data + 644;
  297. frag_len = 0;
  298. while (fw_ptr - fw->data < fw->size) {
  299. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  300. frag_len += sizeof(*cmd) + cmd->plen;
  301. BT_DBG("%s: patching %td/%zu", hdev->name,
  302. (fw_ptr - fw->data), fw->size);
  303. /* The parameter length of the secure send command requires
  304. * a 4 byte alignment. It happens so that the firmware file
  305. * contains proper Intel_NOP commands to align the fragments
  306. * as needed.
  307. *
  308. * Send set of commands with 4 byte alignment from the
  309. * firmware data buffer as a single Data fragement.
  310. */
  311. if (frag_len % 4)
  312. continue;
  313. /* Send each command from the firmware data buffer as
  314. * a single Data fragment.
  315. */
  316. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  317. if (err < 0) {
  318. BT_ERR("%s: Failed to send firmware data (%d)",
  319. hdev->name, err);
  320. goto done;
  321. }
  322. fw_ptr += frag_len;
  323. frag_len = 0;
  324. }
  325. set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
  326. BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
  327. /* Before switching the device into operational mode and with that
  328. * booting the loaded firmware, wait for the bootloader notification
  329. * that all fragments have been successfully received.
  330. *
  331. * When the event processing receives the notification, then the
  332. * STATE_DOWNLOADING flag will be cleared.
  333. *
  334. * The firmware loading should not take longer than 5 seconds
  335. * and thus just timeout if that happens and fail the setup
  336. * of this device.
  337. */
  338. err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
  339. TASK_INTERRUPTIBLE,
  340. msecs_to_jiffies(5000));
  341. if (err == 1) {
  342. BT_ERR("%s: Firmware loading interrupted", hdev->name);
  343. err = -EINTR;
  344. goto done;
  345. }
  346. if (err) {
  347. BT_ERR("%s: Firmware loading timeout", hdev->name);
  348. err = -ETIMEDOUT;
  349. goto done;
  350. }
  351. if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
  352. BT_ERR("%s: Firmware loading failed", hdev->name);
  353. err = -ENOEXEC;
  354. goto done;
  355. }
  356. rettime = ktime_get();
  357. delta = ktime_sub(rettime, calltime);
  358. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  359. BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
  360. done:
  361. release_firmware(fw);
  362. if (err < 0)
  363. return err;
  364. calltime = ktime_get();
  365. set_bit(STATE_BOOTING, &intel->flags);
  366. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
  367. HCI_INIT_TIMEOUT);
  368. if (IS_ERR(skb))
  369. return PTR_ERR(skb);
  370. kfree_skb(skb);
  371. /* The bootloader will not indicate when the device is ready. This
  372. * is done by the operational firmware sending bootup notification.
  373. *
  374. * Booting into operational firmware should not take longer than
  375. * 1 second. However if that happens, then just fail the setup
  376. * since something went wrong.
  377. */
  378. BT_INFO("%s: Waiting for device to boot", hdev->name);
  379. err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
  380. TASK_INTERRUPTIBLE,
  381. msecs_to_jiffies(1000));
  382. if (err == 1) {
  383. BT_ERR("%s: Device boot interrupted", hdev->name);
  384. return -EINTR;
  385. }
  386. if (err) {
  387. BT_ERR("%s: Device boot timeout", hdev->name);
  388. return -ETIMEDOUT;
  389. }
  390. rettime = ktime_get();
  391. delta = ktime_sub(rettime, calltime);
  392. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  393. BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
  394. clear_bit(STATE_BOOTLOADER, &intel->flags);
  395. return 0;
  396. }
  397. static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  398. {
  399. struct hci_uart *hu = hci_get_drvdata(hdev);
  400. struct intel_data *intel = hu->priv;
  401. struct hci_event_hdr *hdr;
  402. if (!test_bit(STATE_BOOTLOADER, &intel->flags))
  403. goto recv;
  404. hdr = (void *)skb->data;
  405. /* When the firmware loading completes the device sends
  406. * out a vendor specific event indicating the result of
  407. * the firmware loading.
  408. */
  409. if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
  410. skb->data[2] == 0x06) {
  411. if (skb->data[3] != 0x00)
  412. set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
  413. if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
  414. test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
  415. smp_mb__after_atomic();
  416. wake_up_bit(&intel->flags, STATE_DOWNLOADING);
  417. }
  418. /* When switching to the operational firmware the device
  419. * sends a vendor specific event indicating that the bootup
  420. * completed.
  421. */
  422. } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
  423. skb->data[2] == 0x02) {
  424. if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
  425. smp_mb__after_atomic();
  426. wake_up_bit(&intel->flags, STATE_BOOTING);
  427. }
  428. }
  429. recv:
  430. return hci_recv_frame(hdev, skb);
  431. }
  432. static const struct h4_recv_pkt intel_recv_pkts[] = {
  433. { H4_RECV_ACL, .recv = hci_recv_frame },
  434. { H4_RECV_SCO, .recv = hci_recv_frame },
  435. { H4_RECV_EVENT, .recv = intel_recv_event },
  436. };
  437. static int intel_recv(struct hci_uart *hu, const void *data, int count)
  438. {
  439. struct intel_data *intel = hu->priv;
  440. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  441. return -EUNATCH;
  442. intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
  443. intel_recv_pkts,
  444. ARRAY_SIZE(intel_recv_pkts));
  445. if (IS_ERR(intel->rx_skb)) {
  446. int err = PTR_ERR(intel->rx_skb);
  447. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  448. intel->rx_skb = NULL;
  449. return err;
  450. }
  451. return count;
  452. }
  453. static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  454. {
  455. struct intel_data *intel = hu->priv;
  456. BT_DBG("hu %p skb %p", hu, skb);
  457. skb_queue_tail(&intel->txq, skb);
  458. return 0;
  459. }
  460. static struct sk_buff *intel_dequeue(struct hci_uart *hu)
  461. {
  462. struct intel_data *intel = hu->priv;
  463. struct sk_buff *skb;
  464. skb = skb_dequeue(&intel->txq);
  465. if (!skb)
  466. return skb;
  467. if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
  468. (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
  469. struct hci_command_hdr *cmd = (void *)skb->data;
  470. __u16 opcode = le16_to_cpu(cmd->opcode);
  471. /* When the 0xfc01 command is issued to boot into
  472. * the operational firmware, it will actually not
  473. * send a command complete event. To keep the flow
  474. * control working inject that event here.
  475. */
  476. if (opcode == 0xfc01)
  477. inject_cmd_complete(hu->hdev, opcode);
  478. }
  479. /* Prepend skb with frame type */
  480. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  481. return skb;
  482. }
  483. static const struct hci_uart_proto intel_proto = {
  484. .id = HCI_UART_INTEL,
  485. .name = "Intel",
  486. .init_speed = 115200,
  487. .open = intel_open,
  488. .close = intel_close,
  489. .flush = intel_flush,
  490. .setup = intel_setup,
  491. .recv = intel_recv,
  492. .enqueue = intel_enqueue,
  493. .dequeue = intel_dequeue,
  494. };
  495. int __init intel_init(void)
  496. {
  497. return hci_uart_register_proto(&intel_proto);
  498. }
  499. int __exit intel_deinit(void)
  500. {
  501. return hci_uart_unregister_proto(&intel_proto);
  502. }