hci_intel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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/module.h>
  28. #include <linux/wait.h>
  29. #include <linux/tty.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/acpi.h>
  33. #include <net/bluetooth/bluetooth.h>
  34. #include <net/bluetooth/hci_core.h>
  35. #include "hci_uart.h"
  36. #include "btintel.h"
  37. #define STATE_BOOTLOADER 0
  38. #define STATE_DOWNLOADING 1
  39. #define STATE_FIRMWARE_LOADED 2
  40. #define STATE_FIRMWARE_FAILED 3
  41. #define STATE_BOOTING 4
  42. struct intel_device {
  43. struct list_head list;
  44. struct platform_device *pdev;
  45. struct gpio_desc *reset;
  46. };
  47. static LIST_HEAD(intel_device_list);
  48. static DEFINE_SPINLOCK(intel_device_list_lock);
  49. struct intel_data {
  50. struct sk_buff *rx_skb;
  51. struct sk_buff_head txq;
  52. unsigned long flags;
  53. };
  54. static u8 intel_convert_speed(unsigned int speed)
  55. {
  56. switch (speed) {
  57. case 9600:
  58. return 0x00;
  59. case 19200:
  60. return 0x01;
  61. case 38400:
  62. return 0x02;
  63. case 57600:
  64. return 0x03;
  65. case 115200:
  66. return 0x04;
  67. case 230400:
  68. return 0x05;
  69. case 460800:
  70. return 0x06;
  71. case 921600:
  72. return 0x07;
  73. case 1843200:
  74. return 0x08;
  75. case 3250000:
  76. return 0x09;
  77. case 2000000:
  78. return 0x0a;
  79. case 3000000:
  80. return 0x0b;
  81. default:
  82. return 0xff;
  83. }
  84. }
  85. static int intel_wait_booting(struct hci_uart *hu)
  86. {
  87. struct intel_data *intel = hu->priv;
  88. int err;
  89. err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
  90. TASK_INTERRUPTIBLE,
  91. msecs_to_jiffies(1000));
  92. if (err == 1) {
  93. BT_ERR("%s: Device boot interrupted", hu->hdev->name);
  94. return -EINTR;
  95. }
  96. if (err) {
  97. BT_ERR("%s: Device boot timeout", hu->hdev->name);
  98. return -ETIMEDOUT;
  99. }
  100. return err;
  101. }
  102. static int intel_set_power(struct hci_uart *hu, bool powered)
  103. {
  104. struct list_head *p;
  105. int err = -ENODEV;
  106. spin_lock(&intel_device_list_lock);
  107. list_for_each(p, &intel_device_list) {
  108. struct intel_device *idev = list_entry(p, struct intel_device,
  109. list);
  110. /* tty device and pdev device should share the same parent
  111. * which is the UART port.
  112. */
  113. if (hu->tty->dev->parent != idev->pdev->dev.parent)
  114. continue;
  115. if (!idev->reset) {
  116. err = -ENOTSUPP;
  117. break;
  118. }
  119. BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
  120. hu, dev_name(&idev->pdev->dev), powered);
  121. gpiod_set_value(idev->reset, powered);
  122. }
  123. spin_unlock(&intel_device_list_lock);
  124. return err;
  125. }
  126. static int intel_open(struct hci_uart *hu)
  127. {
  128. struct intel_data *intel;
  129. BT_DBG("hu %p", hu);
  130. intel = kzalloc(sizeof(*intel), GFP_KERNEL);
  131. if (!intel)
  132. return -ENOMEM;
  133. skb_queue_head_init(&intel->txq);
  134. hu->priv = intel;
  135. if (!intel_set_power(hu, true))
  136. set_bit(STATE_BOOTING, &intel->flags);
  137. return 0;
  138. }
  139. static int intel_close(struct hci_uart *hu)
  140. {
  141. struct intel_data *intel = hu->priv;
  142. BT_DBG("hu %p", hu);
  143. intel_set_power(hu, false);
  144. skb_queue_purge(&intel->txq);
  145. kfree_skb(intel->rx_skb);
  146. kfree(intel);
  147. hu->priv = NULL;
  148. return 0;
  149. }
  150. static int intel_flush(struct hci_uart *hu)
  151. {
  152. struct intel_data *intel = hu->priv;
  153. BT_DBG("hu %p", hu);
  154. skb_queue_purge(&intel->txq);
  155. return 0;
  156. }
  157. static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
  158. {
  159. struct sk_buff *skb;
  160. struct hci_event_hdr *hdr;
  161. struct hci_ev_cmd_complete *evt;
  162. skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
  163. if (!skb)
  164. return -ENOMEM;
  165. hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
  166. hdr->evt = HCI_EV_CMD_COMPLETE;
  167. hdr->plen = sizeof(*evt) + 1;
  168. evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
  169. evt->ncmd = 0x01;
  170. evt->opcode = cpu_to_le16(opcode);
  171. *skb_put(skb, 1) = 0x00;
  172. bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
  173. return hci_recv_frame(hdev, skb);
  174. }
  175. static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
  176. {
  177. struct intel_data *intel = hu->priv;
  178. struct hci_dev *hdev = hu->hdev;
  179. u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
  180. struct sk_buff *skb;
  181. int err;
  182. /* This can be the first command sent to the chip, check
  183. * that the controller is ready.
  184. */
  185. err = intel_wait_booting(hu);
  186. clear_bit(STATE_BOOTING, &intel->flags);
  187. /* In case of timeout, try to continue anyway */
  188. if (err && err != ETIMEDOUT)
  189. return err;
  190. BT_INFO("%s: Change controller speed to %d", hdev->name, speed);
  191. speed_cmd[3] = intel_convert_speed(speed);
  192. if (speed_cmd[3] == 0xff) {
  193. BT_ERR("%s: Unsupported speed", hdev->name);
  194. return -EINVAL;
  195. }
  196. /* Device will not accept speed change if Intel version has not been
  197. * previously requested.
  198. */
  199. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  200. if (IS_ERR(skb)) {
  201. BT_ERR("%s: Reading Intel version information failed (%ld)",
  202. hdev->name, PTR_ERR(skb));
  203. return PTR_ERR(skb);
  204. }
  205. kfree_skb(skb);
  206. skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
  207. if (!skb) {
  208. BT_ERR("%s: Failed to allocate memory for baudrate packet",
  209. hdev->name);
  210. return -ENOMEM;
  211. }
  212. memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
  213. bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
  214. hci_uart_set_flow_control(hu, true);
  215. skb_queue_tail(&intel->txq, skb);
  216. hci_uart_tx_wakeup(hu);
  217. /* wait 100ms to change baudrate on controller side */
  218. msleep(100);
  219. hci_uart_set_baudrate(hu, speed);
  220. hci_uart_set_flow_control(hu, false);
  221. return 0;
  222. }
  223. static int intel_setup(struct hci_uart *hu)
  224. {
  225. static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
  226. 0x00, 0x08, 0x04, 0x00 };
  227. struct intel_data *intel = hu->priv;
  228. struct hci_dev *hdev = hu->hdev;
  229. struct sk_buff *skb;
  230. struct intel_version *ver;
  231. struct intel_boot_params *params;
  232. const struct firmware *fw;
  233. const u8 *fw_ptr;
  234. char fwname[64];
  235. u32 frag_len;
  236. ktime_t calltime, delta, rettime;
  237. unsigned long long duration;
  238. unsigned int init_speed, oper_speed;
  239. int speed_change = 0;
  240. int err;
  241. BT_DBG("%s", hdev->name);
  242. hu->hdev->set_bdaddr = btintel_set_bdaddr;
  243. calltime = ktime_get();
  244. if (hu->init_speed)
  245. init_speed = hu->init_speed;
  246. else
  247. init_speed = hu->proto->init_speed;
  248. if (hu->oper_speed)
  249. oper_speed = hu->oper_speed;
  250. else
  251. oper_speed = hu->proto->oper_speed;
  252. if (oper_speed && init_speed && oper_speed != init_speed)
  253. speed_change = 1;
  254. /* Check that the controller is ready */
  255. err = intel_wait_booting(hu);
  256. clear_bit(STATE_BOOTING, &intel->flags);
  257. /* In case of timeout, try to continue anyway */
  258. if (err && err != ETIMEDOUT)
  259. return err;
  260. set_bit(STATE_BOOTLOADER, &intel->flags);
  261. /* Read the Intel version information to determine if the device
  262. * is in bootloader mode or if it already has operational firmware
  263. * loaded.
  264. */
  265. skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
  266. if (IS_ERR(skb)) {
  267. BT_ERR("%s: Reading Intel version information failed (%ld)",
  268. hdev->name, PTR_ERR(skb));
  269. return PTR_ERR(skb);
  270. }
  271. if (skb->len != sizeof(*ver)) {
  272. BT_ERR("%s: Intel version event size mismatch", hdev->name);
  273. kfree_skb(skb);
  274. return -EILSEQ;
  275. }
  276. ver = (struct intel_version *)skb->data;
  277. if (ver->status) {
  278. BT_ERR("%s: Intel version command failure (%02x)",
  279. hdev->name, ver->status);
  280. err = -bt_to_errno(ver->status);
  281. kfree_skb(skb);
  282. return err;
  283. }
  284. /* The hardware platform number has a fixed value of 0x37 and
  285. * for now only accept this single value.
  286. */
  287. if (ver->hw_platform != 0x37) {
  288. BT_ERR("%s: Unsupported Intel hardware platform (%u)",
  289. hdev->name, ver->hw_platform);
  290. kfree_skb(skb);
  291. return -EINVAL;
  292. }
  293. /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
  294. * supported by this firmware loading method. This check has been
  295. * put in place to ensure correct forward compatibility options
  296. * when newer hardware variants come along.
  297. */
  298. if (ver->hw_variant != 0x0b) {
  299. BT_ERR("%s: Unsupported Intel hardware variant (%u)",
  300. hdev->name, ver->hw_variant);
  301. kfree_skb(skb);
  302. return -EINVAL;
  303. }
  304. btintel_version_info(hdev, ver);
  305. /* The firmware variant determines if the device is in bootloader
  306. * mode or is running operational firmware. The value 0x06 identifies
  307. * the bootloader and the value 0x23 identifies the operational
  308. * firmware.
  309. *
  310. * When the operational firmware is already present, then only
  311. * the check for valid Bluetooth device address is needed. This
  312. * determines if the device will be added as configured or
  313. * unconfigured controller.
  314. *
  315. * It is not possible to use the Secure Boot Parameters in this
  316. * case since that command is only available in bootloader mode.
  317. */
  318. if (ver->fw_variant == 0x23) {
  319. kfree_skb(skb);
  320. clear_bit(STATE_BOOTLOADER, &intel->flags);
  321. btintel_check_bdaddr(hdev);
  322. return 0;
  323. }
  324. /* If the device is not in bootloader mode, then the only possible
  325. * choice is to return an error and abort the device initialization.
  326. */
  327. if (ver->fw_variant != 0x06) {
  328. BT_ERR("%s: Unsupported Intel firmware variant (%u)",
  329. hdev->name, ver->fw_variant);
  330. kfree_skb(skb);
  331. return -ENODEV;
  332. }
  333. kfree_skb(skb);
  334. /* Read the secure boot parameters to identify the operating
  335. * details of the bootloader.
  336. */
  337. skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
  338. if (IS_ERR(skb)) {
  339. BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
  340. hdev->name, PTR_ERR(skb));
  341. return PTR_ERR(skb);
  342. }
  343. if (skb->len != sizeof(*params)) {
  344. BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
  345. kfree_skb(skb);
  346. return -EILSEQ;
  347. }
  348. params = (struct intel_boot_params *)skb->data;
  349. if (params->status) {
  350. BT_ERR("%s: Intel boot parameters command failure (%02x)",
  351. hdev->name, params->status);
  352. err = -bt_to_errno(params->status);
  353. kfree_skb(skb);
  354. return err;
  355. }
  356. BT_INFO("%s: Device revision is %u", hdev->name,
  357. le16_to_cpu(params->dev_revid));
  358. BT_INFO("%s: Secure boot is %s", hdev->name,
  359. params->secure_boot ? "enabled" : "disabled");
  360. BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
  361. params->min_fw_build_nn, params->min_fw_build_cw,
  362. 2000 + params->min_fw_build_yy);
  363. /* It is required that every single firmware fragment is acknowledged
  364. * with a command complete event. If the boot parameters indicate
  365. * that this bootloader does not send them, then abort the setup.
  366. */
  367. if (params->limited_cce != 0x00) {
  368. BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
  369. hdev->name, params->limited_cce);
  370. kfree_skb(skb);
  371. return -EINVAL;
  372. }
  373. /* If the OTP has no valid Bluetooth device address, then there will
  374. * also be no valid address for the operational firmware.
  375. */
  376. if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
  377. BT_INFO("%s: No device address configured", hdev->name);
  378. set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
  379. }
  380. /* With this Intel bootloader only the hardware variant and device
  381. * revision information are used to select the right firmware.
  382. *
  383. * Currently this bootloader support is limited to hardware variant
  384. * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
  385. */
  386. snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
  387. le16_to_cpu(params->dev_revid));
  388. err = request_firmware(&fw, fwname, &hdev->dev);
  389. if (err < 0) {
  390. BT_ERR("%s: Failed to load Intel firmware file (%d)",
  391. hdev->name, err);
  392. kfree_skb(skb);
  393. return err;
  394. }
  395. BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
  396. kfree_skb(skb);
  397. if (fw->size < 644) {
  398. BT_ERR("%s: Invalid size of firmware file (%zu)",
  399. hdev->name, fw->size);
  400. err = -EBADF;
  401. goto done;
  402. }
  403. set_bit(STATE_DOWNLOADING, &intel->flags);
  404. /* Start the firmware download transaction with the Init fragment
  405. * represented by the 128 bytes of CSS header.
  406. */
  407. err = btintel_secure_send(hdev, 0x00, 128, fw->data);
  408. if (err < 0) {
  409. BT_ERR("%s: Failed to send firmware header (%d)",
  410. hdev->name, err);
  411. goto done;
  412. }
  413. /* Send the 256 bytes of public key information from the firmware
  414. * as the PKey fragment.
  415. */
  416. err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
  417. if (err < 0) {
  418. BT_ERR("%s: Failed to send firmware public key (%d)",
  419. hdev->name, err);
  420. goto done;
  421. }
  422. /* Send the 256 bytes of signature information from the firmware
  423. * as the Sign fragment.
  424. */
  425. err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
  426. if (err < 0) {
  427. BT_ERR("%s: Failed to send firmware signature (%d)",
  428. hdev->name, err);
  429. goto done;
  430. }
  431. fw_ptr = fw->data + 644;
  432. frag_len = 0;
  433. while (fw_ptr - fw->data < fw->size) {
  434. struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
  435. frag_len += sizeof(*cmd) + cmd->plen;
  436. BT_DBG("%s: patching %td/%zu", hdev->name,
  437. (fw_ptr - fw->data), fw->size);
  438. /* The parameter length of the secure send command requires
  439. * a 4 byte alignment. It happens so that the firmware file
  440. * contains proper Intel_NOP commands to align the fragments
  441. * as needed.
  442. *
  443. * Send set of commands with 4 byte alignment from the
  444. * firmware data buffer as a single Data fragement.
  445. */
  446. if (frag_len % 4)
  447. continue;
  448. /* Send each command from the firmware data buffer as
  449. * a single Data fragment.
  450. */
  451. err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
  452. if (err < 0) {
  453. BT_ERR("%s: Failed to send firmware data (%d)",
  454. hdev->name, err);
  455. goto done;
  456. }
  457. fw_ptr += frag_len;
  458. frag_len = 0;
  459. }
  460. set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
  461. BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
  462. /* Before switching the device into operational mode and with that
  463. * booting the loaded firmware, wait for the bootloader notification
  464. * that all fragments have been successfully received.
  465. *
  466. * When the event processing receives the notification, then the
  467. * STATE_DOWNLOADING flag will be cleared.
  468. *
  469. * The firmware loading should not take longer than 5 seconds
  470. * and thus just timeout if that happens and fail the setup
  471. * of this device.
  472. */
  473. err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
  474. TASK_INTERRUPTIBLE,
  475. msecs_to_jiffies(5000));
  476. if (err == 1) {
  477. BT_ERR("%s: Firmware loading interrupted", hdev->name);
  478. err = -EINTR;
  479. goto done;
  480. }
  481. if (err) {
  482. BT_ERR("%s: Firmware loading timeout", hdev->name);
  483. err = -ETIMEDOUT;
  484. goto done;
  485. }
  486. if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
  487. BT_ERR("%s: Firmware loading failed", hdev->name);
  488. err = -ENOEXEC;
  489. goto done;
  490. }
  491. rettime = ktime_get();
  492. delta = ktime_sub(rettime, calltime);
  493. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  494. BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
  495. done:
  496. release_firmware(fw);
  497. if (err < 0)
  498. return err;
  499. /* We need to restore the default speed before Intel reset */
  500. if (speed_change) {
  501. err = intel_set_baudrate(hu, init_speed);
  502. if (err)
  503. return err;
  504. }
  505. calltime = ktime_get();
  506. set_bit(STATE_BOOTING, &intel->flags);
  507. skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
  508. HCI_INIT_TIMEOUT);
  509. if (IS_ERR(skb))
  510. return PTR_ERR(skb);
  511. kfree_skb(skb);
  512. /* The bootloader will not indicate when the device is ready. This
  513. * is done by the operational firmware sending bootup notification.
  514. *
  515. * Booting into operational firmware should not take longer than
  516. * 1 second. However if that happens, then just fail the setup
  517. * since something went wrong.
  518. */
  519. BT_INFO("%s: Waiting for device to boot", hdev->name);
  520. err = intel_wait_booting(hu);
  521. if (err)
  522. return err;
  523. clear_bit(STATE_BOOTING, &intel->flags);
  524. rettime = ktime_get();
  525. delta = ktime_sub(rettime, calltime);
  526. duration = (unsigned long long) ktime_to_ns(delta) >> 10;
  527. BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
  528. skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
  529. if (IS_ERR(skb))
  530. return PTR_ERR(skb);
  531. kfree_skb(skb);
  532. if (speed_change) {
  533. err = intel_set_baudrate(hu, oper_speed);
  534. if (err)
  535. return err;
  536. }
  537. BT_INFO("%s: Setup complete", hdev->name);
  538. clear_bit(STATE_BOOTLOADER, &intel->flags);
  539. return 0;
  540. }
  541. static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
  542. {
  543. struct hci_uart *hu = hci_get_drvdata(hdev);
  544. struct intel_data *intel = hu->priv;
  545. struct hci_event_hdr *hdr;
  546. if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
  547. !test_bit(STATE_BOOTING, &intel->flags))
  548. goto recv;
  549. hdr = (void *)skb->data;
  550. /* When the firmware loading completes the device sends
  551. * out a vendor specific event indicating the result of
  552. * the firmware loading.
  553. */
  554. if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
  555. skb->data[2] == 0x06) {
  556. if (skb->data[3] != 0x00)
  557. set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
  558. if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
  559. test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
  560. smp_mb__after_atomic();
  561. wake_up_bit(&intel->flags, STATE_DOWNLOADING);
  562. }
  563. /* When switching to the operational firmware the device
  564. * sends a vendor specific event indicating that the bootup
  565. * completed.
  566. */
  567. } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
  568. skb->data[2] == 0x02) {
  569. if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
  570. smp_mb__after_atomic();
  571. wake_up_bit(&intel->flags, STATE_BOOTING);
  572. }
  573. }
  574. recv:
  575. return hci_recv_frame(hdev, skb);
  576. }
  577. static const struct h4_recv_pkt intel_recv_pkts[] = {
  578. { H4_RECV_ACL, .recv = hci_recv_frame },
  579. { H4_RECV_SCO, .recv = hci_recv_frame },
  580. { H4_RECV_EVENT, .recv = intel_recv_event },
  581. };
  582. static int intel_recv(struct hci_uart *hu, const void *data, int count)
  583. {
  584. struct intel_data *intel = hu->priv;
  585. if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
  586. return -EUNATCH;
  587. intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
  588. intel_recv_pkts,
  589. ARRAY_SIZE(intel_recv_pkts));
  590. if (IS_ERR(intel->rx_skb)) {
  591. int err = PTR_ERR(intel->rx_skb);
  592. BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
  593. intel->rx_skb = NULL;
  594. return err;
  595. }
  596. return count;
  597. }
  598. static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
  599. {
  600. struct intel_data *intel = hu->priv;
  601. BT_DBG("hu %p skb %p", hu, skb);
  602. skb_queue_tail(&intel->txq, skb);
  603. return 0;
  604. }
  605. static struct sk_buff *intel_dequeue(struct hci_uart *hu)
  606. {
  607. struct intel_data *intel = hu->priv;
  608. struct sk_buff *skb;
  609. skb = skb_dequeue(&intel->txq);
  610. if (!skb)
  611. return skb;
  612. if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
  613. (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
  614. struct hci_command_hdr *cmd = (void *)skb->data;
  615. __u16 opcode = le16_to_cpu(cmd->opcode);
  616. /* When the 0xfc01 command is issued to boot into
  617. * the operational firmware, it will actually not
  618. * send a command complete event. To keep the flow
  619. * control working inject that event here.
  620. */
  621. if (opcode == 0xfc01)
  622. inject_cmd_complete(hu->hdev, opcode);
  623. }
  624. /* Prepend skb with frame type */
  625. memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
  626. return skb;
  627. }
  628. static const struct hci_uart_proto intel_proto = {
  629. .id = HCI_UART_INTEL,
  630. .name = "Intel",
  631. .init_speed = 115200,
  632. .oper_speed = 3000000,
  633. .open = intel_open,
  634. .close = intel_close,
  635. .flush = intel_flush,
  636. .setup = intel_setup,
  637. .set_baudrate = intel_set_baudrate,
  638. .recv = intel_recv,
  639. .enqueue = intel_enqueue,
  640. .dequeue = intel_dequeue,
  641. };
  642. #ifdef CONFIG_ACPI
  643. static const struct acpi_device_id intel_acpi_match[] = {
  644. { "INT33E1", 0 },
  645. { },
  646. };
  647. MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
  648. static int intel_acpi_probe(struct intel_device *idev)
  649. {
  650. const struct acpi_device_id *id;
  651. id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
  652. if (!id)
  653. return -ENODEV;
  654. return 0;
  655. }
  656. #else
  657. static int intel_acpi_probe(struct intel_device *idev)
  658. {
  659. return -ENODEV;
  660. }
  661. #endif
  662. static int intel_probe(struct platform_device *pdev)
  663. {
  664. struct intel_device *idev;
  665. idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
  666. if (!idev)
  667. return -ENOMEM;
  668. idev->pdev = pdev;
  669. if (ACPI_HANDLE(&pdev->dev)) {
  670. int err = intel_acpi_probe(idev);
  671. if (err)
  672. return err;
  673. } else {
  674. return -ENODEV;
  675. }
  676. idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
  677. GPIOD_OUT_LOW);
  678. if (IS_ERR(idev->reset)) {
  679. dev_err(&pdev->dev, "Unable to retrieve gpio\n");
  680. return PTR_ERR(idev->reset);
  681. }
  682. platform_set_drvdata(pdev, idev);
  683. /* Place this instance on the device list */
  684. spin_lock(&intel_device_list_lock);
  685. list_add_tail(&idev->list, &intel_device_list);
  686. spin_unlock(&intel_device_list_lock);
  687. dev_info(&pdev->dev, "registered.\n");
  688. return 0;
  689. }
  690. static int intel_remove(struct platform_device *pdev)
  691. {
  692. struct intel_device *idev = platform_get_drvdata(pdev);
  693. spin_lock(&intel_device_list_lock);
  694. list_del(&idev->list);
  695. spin_unlock(&intel_device_list_lock);
  696. dev_info(&pdev->dev, "unregistered.\n");
  697. return 0;
  698. }
  699. static struct platform_driver intel_driver = {
  700. .probe = intel_probe,
  701. .remove = intel_remove,
  702. .driver = {
  703. .name = "hci_intel",
  704. .acpi_match_table = ACPI_PTR(intel_acpi_match),
  705. },
  706. };
  707. int __init intel_init(void)
  708. {
  709. platform_driver_register(&intel_driver);
  710. return hci_uart_register_proto(&intel_proto);
  711. }
  712. int __exit intel_deinit(void)
  713. {
  714. platform_driver_unregister(&intel_driver);
  715. return hci_uart_unregister_proto(&intel_proto);
  716. }