3 * Bluetooth HCI UART driver 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/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/skbuff.h>
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/wait.h>
30 #include <linux/tty.h>
31 #include <linux/platform_device.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/acpi.h>
34 #include <linux/interrupt.h>
35 #include <linux/pm_runtime.h>
37 #include <net/bluetooth/bluetooth.h>
38 #include <net/bluetooth/hci_core.h>
43 #define STATE_BOOTLOADER 0
44 #define STATE_DOWNLOADING 1
45 #define STATE_FIRMWARE_LOADED 2
46 #define STATE_FIRMWARE_FAILED 3
47 #define STATE_BOOTING 4
48 #define STATE_LPM_ENABLED 5
49 #define STATE_TX_ACTIVE 6
50 #define STATE_SUSPENDED 7
51 #define STATE_LPM_TRANSACTION 8
53 #define HCI_LPM_WAKE_PKT 0xf0
54 #define HCI_LPM_PKT 0xf1
55 #define HCI_LPM_MAX_SIZE 10
56 #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
58 #define LPM_OP_TX_NOTIFY 0x00
59 #define LPM_OP_SUSPEND_ACK 0x02
60 #define LPM_OP_RESUME_ACK 0x03
62 #define LPM_SUSPEND_DELAY_MS 1000
71 struct list_head list;
72 struct platform_device *pdev;
73 struct gpio_desc *reset;
79 static LIST_HEAD(intel_device_list);
80 static DEFINE_MUTEX(intel_device_list_lock);
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
85 struct work_struct busy_work;
90 static u8 intel_convert_speed(unsigned int speed)
122 static int intel_wait_booting(struct hci_uart *hu)
124 struct intel_data *intel = hu->priv;
127 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
129 msecs_to_jiffies(1000));
132 bt_dev_err(hu->hdev, "Device boot interrupted");
137 bt_dev_err(hu->hdev, "Device boot timeout");
145 static int intel_wait_lpm_transaction(struct hci_uart *hu)
147 struct intel_data *intel = hu->priv;
150 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
152 msecs_to_jiffies(1000));
155 bt_dev_err(hu->hdev, "LPM transaction interrupted");
160 bt_dev_err(hu->hdev, "LPM transaction timeout");
167 static int intel_lpm_suspend(struct hci_uart *hu)
169 static const u8 suspend[] = { 0x01, 0x01, 0x01 };
170 struct intel_data *intel = hu->priv;
173 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
174 test_bit(STATE_SUSPENDED, &intel->flags))
177 if (test_bit(STATE_TX_ACTIVE, &intel->flags))
180 bt_dev_dbg(hu->hdev, "Suspending");
182 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
184 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
188 memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend));
189 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
191 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
193 skb_queue_tail(&intel->txq, skb);
194 hci_uart_tx_wakeup(hu);
196 intel_wait_lpm_transaction(hu);
197 /* Even in case of failure, continue and test the suspended flag */
199 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
201 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
202 bt_dev_err(hu->hdev, "Device suspend error");
206 bt_dev_dbg(hu->hdev, "Suspended");
208 hci_uart_set_flow_control(hu, true);
213 static int intel_lpm_resume(struct hci_uart *hu)
215 struct intel_data *intel = hu->priv;
218 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
219 !test_bit(STATE_SUSPENDED, &intel->flags))
222 bt_dev_dbg(hu->hdev, "Resuming");
224 hci_uart_set_flow_control(hu, false);
226 skb = bt_skb_alloc(0, GFP_KERNEL);
228 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
232 bt_cb(skb)->pkt_type = HCI_LPM_WAKE_PKT;
234 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
236 skb_queue_tail(&intel->txq, skb);
237 hci_uart_tx_wakeup(hu);
239 intel_wait_lpm_transaction(hu);
240 /* Even in case of failure, continue and test the suspended flag */
242 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
244 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
245 bt_dev_err(hu->hdev, "Device resume error");
249 bt_dev_dbg(hu->hdev, "Resumed");
253 #endif /* CONFIG_PM */
255 static int intel_lpm_host_wake(struct hci_uart *hu)
257 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
258 struct intel_data *intel = hu->priv;
261 hci_uart_set_flow_control(hu, false);
263 clear_bit(STATE_SUSPENDED, &intel->flags);
265 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
267 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
271 memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack,
272 sizeof(lpm_resume_ack));
273 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
275 skb_queue_tail(&intel->txq, skb);
276 hci_uart_tx_wakeup(hu);
278 bt_dev_dbg(hu->hdev, "Resumed by controller");
283 static irqreturn_t intel_irq(int irq, void *dev_id)
285 struct intel_device *idev = dev_id;
287 dev_info(&idev->pdev->dev, "hci_intel irq\n");
289 mutex_lock(&idev->hu_lock);
291 intel_lpm_host_wake(idev->hu);
292 mutex_unlock(&idev->hu_lock);
294 /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
295 pm_runtime_get(&idev->pdev->dev);
296 pm_runtime_mark_last_busy(&idev->pdev->dev);
297 pm_runtime_put_autosuspend(&idev->pdev->dev);
302 static int intel_set_power(struct hci_uart *hu, bool powered)
307 mutex_lock(&intel_device_list_lock);
309 list_for_each(p, &intel_device_list) {
310 struct intel_device *idev = list_entry(p, struct intel_device,
313 /* tty device and pdev device should share the same parent
314 * which is the UART port.
316 if (hu->tty->dev->parent != idev->pdev->dev.parent)
324 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
325 hu, dev_name(&idev->pdev->dev), powered);
327 gpiod_set_value(idev->reset, powered);
329 /* Provide to idev a hu reference which is used to run LPM
330 * transactions (lpm suspend/resume) from PM callbacks.
331 * hu needs to be protected against concurrent removing during
334 mutex_lock(&idev->hu_lock);
335 idev->hu = powered ? hu : NULL;
336 mutex_unlock(&idev->hu_lock);
341 if (powered && device_can_wakeup(&idev->pdev->dev)) {
342 err = devm_request_threaded_irq(&idev->pdev->dev,
346 "bt-host-wake", idev);
348 BT_ERR("hu %p, unable to allocate irq-%d",
353 device_wakeup_enable(&idev->pdev->dev);
355 pm_runtime_set_active(&idev->pdev->dev);
356 pm_runtime_use_autosuspend(&idev->pdev->dev);
357 pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
358 LPM_SUSPEND_DELAY_MS);
359 pm_runtime_enable(&idev->pdev->dev);
360 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
361 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
362 device_wakeup_disable(&idev->pdev->dev);
364 pm_runtime_disable(&idev->pdev->dev);
368 mutex_unlock(&intel_device_list_lock);
373 static void intel_busy_work(struct work_struct *work)
376 struct intel_data *intel = container_of(work, struct intel_data,
379 /* Link is busy, delay the suspend */
380 mutex_lock(&intel_device_list_lock);
381 list_for_each(p, &intel_device_list) {
382 struct intel_device *idev = list_entry(p, struct intel_device,
385 if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
386 pm_runtime_get(&idev->pdev->dev);
387 pm_runtime_mark_last_busy(&idev->pdev->dev);
388 pm_runtime_put_autosuspend(&idev->pdev->dev);
392 mutex_unlock(&intel_device_list_lock);
395 static int intel_open(struct hci_uart *hu)
397 struct intel_data *intel;
401 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
405 skb_queue_head_init(&intel->txq);
406 INIT_WORK(&intel->busy_work, intel_busy_work);
412 if (!intel_set_power(hu, true))
413 set_bit(STATE_BOOTING, &intel->flags);
418 static int intel_close(struct hci_uart *hu)
420 struct intel_data *intel = hu->priv;
424 cancel_work_sync(&intel->busy_work);
426 intel_set_power(hu, false);
428 skb_queue_purge(&intel->txq);
429 kfree_skb(intel->rx_skb);
436 static int intel_flush(struct hci_uart *hu)
438 struct intel_data *intel = hu->priv;
442 skb_queue_purge(&intel->txq);
447 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
450 struct hci_event_hdr *hdr;
451 struct hci_ev_cmd_complete *evt;
453 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
457 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
458 hdr->evt = HCI_EV_CMD_COMPLETE;
459 hdr->plen = sizeof(*evt) + 1;
461 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
463 evt->opcode = cpu_to_le16(opcode);
465 *skb_put(skb, 1) = 0x00;
467 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
469 return hci_recv_frame(hdev, skb);
472 static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
474 struct intel_data *intel = hu->priv;
475 struct hci_dev *hdev = hu->hdev;
476 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
480 /* This can be the first command sent to the chip, check
481 * that the controller is ready.
483 err = intel_wait_booting(hu);
485 clear_bit(STATE_BOOTING, &intel->flags);
487 /* In case of timeout, try to continue anyway */
488 if (err && err != ETIMEDOUT)
491 bt_dev_info(hdev, "Change controller speed to %d", speed);
493 speed_cmd[3] = intel_convert_speed(speed);
494 if (speed_cmd[3] == 0xff) {
495 bt_dev_err(hdev, "Unsupported speed");
499 /* Device will not accept speed change if Intel version has not been
500 * previously requested.
502 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
504 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
510 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
512 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
516 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
517 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
519 hci_uart_set_flow_control(hu, true);
521 skb_queue_tail(&intel->txq, skb);
522 hci_uart_tx_wakeup(hu);
524 /* wait 100ms to change baudrate on controller side */
527 hci_uart_set_baudrate(hu, speed);
528 hci_uart_set_flow_control(hu, false);
533 static int intel_setup(struct hci_uart *hu)
535 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
536 0x00, 0x08, 0x04, 0x00 };
537 static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
538 struct intel_data *intel = hu->priv;
539 struct intel_device *idev = NULL;
540 struct hci_dev *hdev = hu->hdev;
542 struct intel_version *ver;
543 struct intel_boot_params *params;
545 const struct firmware *fw;
549 ktime_t calltime, delta, rettime;
550 unsigned long long duration;
551 unsigned int init_speed, oper_speed;
552 int speed_change = 0;
555 bt_dev_dbg(hdev, "start intel_setup");
557 hu->hdev->set_bdaddr = btintel_set_bdaddr;
559 calltime = ktime_get();
562 init_speed = hu->init_speed;
564 init_speed = hu->proto->init_speed;
567 oper_speed = hu->oper_speed;
569 oper_speed = hu->proto->oper_speed;
571 if (oper_speed && init_speed && oper_speed != init_speed)
574 /* Check that the controller is ready */
575 err = intel_wait_booting(hu);
577 clear_bit(STATE_BOOTING, &intel->flags);
579 /* In case of timeout, try to continue anyway */
580 if (err && err != ETIMEDOUT)
583 set_bit(STATE_BOOTLOADER, &intel->flags);
585 /* Read the Intel version information to determine if the device
586 * is in bootloader mode or if it already has operational firmware
589 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
591 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
596 if (skb->len != sizeof(*ver)) {
597 bt_dev_err(hdev, "Intel version event size mismatch");
602 ver = (struct intel_version *)skb->data;
604 bt_dev_err(hdev, "Intel version command failure (%02x)",
606 err = -bt_to_errno(ver->status);
611 /* The hardware platform number has a fixed value of 0x37 and
612 * for now only accept this single value.
614 if (ver->hw_platform != 0x37) {
615 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
621 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
622 * supported by this firmware loading method. This check has been
623 * put in place to ensure correct forward compatibility options
624 * when newer hardware variants come along.
626 if (ver->hw_variant != 0x0b) {
627 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
633 btintel_version_info(hdev, ver);
635 /* The firmware variant determines if the device is in bootloader
636 * mode or is running operational firmware. The value 0x06 identifies
637 * the bootloader and the value 0x23 identifies the operational
640 * When the operational firmware is already present, then only
641 * the check for valid Bluetooth device address is needed. This
642 * determines if the device will be added as configured or
643 * unconfigured controller.
645 * It is not possible to use the Secure Boot Parameters in this
646 * case since that command is only available in bootloader mode.
648 if (ver->fw_variant == 0x23) {
650 clear_bit(STATE_BOOTLOADER, &intel->flags);
651 btintel_check_bdaddr(hdev);
655 /* If the device is not in bootloader mode, then the only possible
656 * choice is to return an error and abort the device initialization.
658 if (ver->fw_variant != 0x06) {
659 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
667 /* Read the secure boot parameters to identify the operating
668 * details of the bootloader.
670 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
672 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
677 if (skb->len != sizeof(*params)) {
678 bt_dev_err(hdev, "Intel boot parameters size mismatch");
683 params = (struct intel_boot_params *)skb->data;
684 if (params->status) {
685 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
687 err = -bt_to_errno(params->status);
692 bt_dev_info(hdev, "Device revision is %u",
693 le16_to_cpu(params->dev_revid));
695 bt_dev_info(hdev, "Secure boot is %s",
696 params->secure_boot ? "enabled" : "disabled");
698 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
699 params->min_fw_build_nn, params->min_fw_build_cw,
700 2000 + params->min_fw_build_yy);
702 /* It is required that every single firmware fragment is acknowledged
703 * with a command complete event. If the boot parameters indicate
704 * that this bootloader does not send them, then abort the setup.
706 if (params->limited_cce != 0x00) {
707 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
708 params->limited_cce);
713 /* If the OTP has no valid Bluetooth device address, then there will
714 * also be no valid address for the operational firmware.
716 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
717 bt_dev_info(hdev, "No device address configured");
718 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
721 /* With this Intel bootloader only the hardware variant and device
722 * revision information are used to select the right firmware.
724 * Currently this bootloader support is limited to hardware variant
725 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
727 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
728 le16_to_cpu(params->dev_revid));
730 err = request_firmware(&fw, fwname, &hdev->dev);
732 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
738 bt_dev_info(hdev, "Found device firmware: %s", fwname);
742 if (fw->size < 644) {
743 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
749 set_bit(STATE_DOWNLOADING, &intel->flags);
751 /* Start the firmware download transaction with the Init fragment
752 * represented by the 128 bytes of CSS header.
754 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
756 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
760 /* Send the 256 bytes of public key information from the firmware
761 * as the PKey fragment.
763 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
765 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
770 /* Send the 256 bytes of signature information from the firmware
771 * as the Sign fragment.
773 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
775 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
780 fw_ptr = fw->data + 644;
783 while (fw_ptr - fw->data < fw->size) {
784 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
786 frag_len += sizeof(*cmd) + cmd->plen;
788 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
791 /* The parameter length of the secure send command requires
792 * a 4 byte alignment. It happens so that the firmware file
793 * contains proper Intel_NOP commands to align the fragments
796 * Send set of commands with 4 byte alignment from the
797 * firmware data buffer as a single Data fragement.
802 /* Send each command from the firmware data buffer as
803 * a single Data fragment.
805 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
807 bt_dev_err(hdev, "Failed to send firmware data (%d)",
816 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
818 bt_dev_info(hdev, "Waiting for firmware download to complete");
820 /* Before switching the device into operational mode and with that
821 * booting the loaded firmware, wait for the bootloader notification
822 * that all fragments have been successfully received.
824 * When the event processing receives the notification, then the
825 * STATE_DOWNLOADING flag will be cleared.
827 * The firmware loading should not take longer than 5 seconds
828 * and thus just timeout if that happens and fail the setup
831 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
833 msecs_to_jiffies(5000));
835 bt_dev_err(hdev, "Firmware loading interrupted");
841 bt_dev_err(hdev, "Firmware loading timeout");
846 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
847 bt_dev_err(hdev, "Firmware loading failed");
852 rettime = ktime_get();
853 delta = ktime_sub(rettime, calltime);
854 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
856 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
859 release_firmware(fw);
864 /* We need to restore the default speed before Intel reset */
866 err = intel_set_baudrate(hu, init_speed);
871 calltime = ktime_get();
873 set_bit(STATE_BOOTING, &intel->flags);
875 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
882 /* The bootloader will not indicate when the device is ready. This
883 * is done by the operational firmware sending bootup notification.
885 * Booting into operational firmware should not take longer than
886 * 1 second. However if that happens, then just fail the setup
887 * since something went wrong.
889 bt_dev_info(hdev, "Waiting for device to boot");
891 err = intel_wait_booting(hu);
895 clear_bit(STATE_BOOTING, &intel->flags);
897 rettime = ktime_get();
898 delta = ktime_sub(rettime, calltime);
899 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
901 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
903 /* Enable LPM if matching pdev with wakeup enabled */
904 mutex_lock(&intel_device_list_lock);
905 list_for_each(p, &intel_device_list) {
906 struct intel_device *dev = list_entry(p, struct intel_device,
908 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
909 if (device_may_wakeup(&dev->pdev->dev))
914 mutex_unlock(&intel_device_list_lock);
919 bt_dev_info(hdev, "Enabling LPM");
921 skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
924 bt_dev_err(hdev, "Failed to enable LPM");
929 set_bit(STATE_LPM_ENABLED, &intel->flags);
932 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
938 err = intel_set_baudrate(hu, oper_speed);
943 bt_dev_info(hdev, "Setup complete");
945 clear_bit(STATE_BOOTLOADER, &intel->flags);
950 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
952 struct hci_uart *hu = hci_get_drvdata(hdev);
953 struct intel_data *intel = hu->priv;
954 struct hci_event_hdr *hdr;
956 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
957 !test_bit(STATE_BOOTING, &intel->flags))
960 hdr = (void *)skb->data;
962 /* When the firmware loading completes the device sends
963 * out a vendor specific event indicating the result of
964 * the firmware loading.
966 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
967 skb->data[2] == 0x06) {
968 if (skb->data[3] != 0x00)
969 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
971 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
972 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
973 smp_mb__after_atomic();
974 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
977 /* When switching to the operational firmware the device
978 * sends a vendor specific event indicating that the bootup
981 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
982 skb->data[2] == 0x02) {
983 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
984 smp_mb__after_atomic();
985 wake_up_bit(&intel->flags, STATE_BOOTING);
989 return hci_recv_frame(hdev, skb);
992 static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
994 struct hci_uart *hu = hci_get_drvdata(hdev);
995 struct intel_data *intel = hu->priv;
997 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
1000 set_bit(STATE_TX_ACTIVE, &intel->flags);
1001 schedule_work(&intel->busy_work);
1003 clear_bit(STATE_TX_ACTIVE, &intel->flags);
1007 static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1009 struct hci_lpm_pkt *lpm = (void *)skb->data;
1010 struct hci_uart *hu = hci_get_drvdata(hdev);
1011 struct intel_data *intel = hu->priv;
1013 switch (lpm->opcode) {
1014 case LPM_OP_TX_NOTIFY:
1015 if (lpm->dlen < 1) {
1016 bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1019 intel_recv_lpm_notify(hdev, lpm->data[0]);
1021 case LPM_OP_SUSPEND_ACK:
1022 set_bit(STATE_SUSPENDED, &intel->flags);
1023 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1024 smp_mb__after_atomic();
1025 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1028 case LPM_OP_RESUME_ACK:
1029 clear_bit(STATE_SUSPENDED, &intel->flags);
1030 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1031 smp_mb__after_atomic();
1032 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1036 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
1045 #define INTEL_RECV_LPM \
1046 .type = HCI_LPM_PKT, \
1047 .hlen = HCI_LPM_HDR_SIZE, \
1050 .maxlen = HCI_LPM_MAX_SIZE
1052 static const struct h4_recv_pkt intel_recv_pkts[] = {
1053 { H4_RECV_ACL, .recv = hci_recv_frame },
1054 { H4_RECV_SCO, .recv = hci_recv_frame },
1055 { H4_RECV_EVENT, .recv = intel_recv_event },
1056 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
1059 static int intel_recv(struct hci_uart *hu, const void *data, int count)
1061 struct intel_data *intel = hu->priv;
1063 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1066 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1068 ARRAY_SIZE(intel_recv_pkts));
1069 if (IS_ERR(intel->rx_skb)) {
1070 int err = PTR_ERR(intel->rx_skb);
1071 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1072 intel->rx_skb = NULL;
1079 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1081 struct intel_data *intel = hu->priv;
1082 struct list_head *p;
1084 BT_DBG("hu %p skb %p", hu, skb);
1086 /* Be sure our controller is resumed and potential LPM transaction
1087 * completed before enqueuing any packet.
1089 mutex_lock(&intel_device_list_lock);
1090 list_for_each(p, &intel_device_list) {
1091 struct intel_device *idev = list_entry(p, struct intel_device,
1094 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1095 pm_runtime_get_sync(&idev->pdev->dev);
1096 pm_runtime_mark_last_busy(&idev->pdev->dev);
1097 pm_runtime_put_autosuspend(&idev->pdev->dev);
1101 mutex_unlock(&intel_device_list_lock);
1103 skb_queue_tail(&intel->txq, skb);
1108 static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1110 struct intel_data *intel = hu->priv;
1111 struct sk_buff *skb;
1113 skb = skb_dequeue(&intel->txq);
1117 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1118 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1119 struct hci_command_hdr *cmd = (void *)skb->data;
1120 __u16 opcode = le16_to_cpu(cmd->opcode);
1122 /* When the 0xfc01 command is issued to boot into
1123 * the operational firmware, it will actually not
1124 * send a command complete event. To keep the flow
1125 * control working inject that event here.
1127 if (opcode == 0xfc01)
1128 inject_cmd_complete(hu->hdev, opcode);
1131 /* Prepend skb with frame type */
1132 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1137 static const struct hci_uart_proto intel_proto = {
1138 .id = HCI_UART_INTEL,
1140 .init_speed = 115200,
1141 .oper_speed = 3000000,
1143 .close = intel_close,
1144 .flush = intel_flush,
1145 .setup = intel_setup,
1146 .set_baudrate = intel_set_baudrate,
1148 .enqueue = intel_enqueue,
1149 .dequeue = intel_dequeue,
1153 static const struct acpi_device_id intel_acpi_match[] = {
1157 MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1159 static int intel_acpi_probe(struct intel_device *idev)
1161 const struct acpi_device_id *id;
1163 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1170 static int intel_acpi_probe(struct intel_device *idev)
1177 static int intel_suspend(struct device *dev)
1179 struct intel_device *idev = dev_get_drvdata(dev);
1181 dev_dbg(dev, "intel_suspend");
1183 mutex_lock(&idev->hu_lock);
1185 intel_lpm_suspend(idev->hu);
1186 mutex_unlock(&idev->hu_lock);
1191 static int intel_resume(struct device *dev)
1193 struct intel_device *idev = dev_get_drvdata(dev);
1195 dev_dbg(dev, "intel_resume");
1197 mutex_lock(&idev->hu_lock);
1199 intel_lpm_resume(idev->hu);
1200 mutex_unlock(&idev->hu_lock);
1206 static const struct dev_pm_ops intel_pm_ops = {
1207 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1208 SET_RUNTIME_PM_OPS(intel_suspend, intel_resume, NULL)
1211 static int intel_probe(struct platform_device *pdev)
1213 struct intel_device *idev;
1215 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1219 mutex_init(&idev->hu_lock);
1223 if (ACPI_HANDLE(&pdev->dev)) {
1224 int err = intel_acpi_probe(idev);
1231 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1233 if (IS_ERR(idev->reset)) {
1234 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1235 return PTR_ERR(idev->reset);
1238 idev->irq = platform_get_irq(pdev, 0);
1239 if (idev->irq < 0) {
1240 struct gpio_desc *host_wake;
1242 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1244 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1246 if (IS_ERR(host_wake)) {
1247 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1251 idev->irq = gpiod_to_irq(host_wake);
1252 if (idev->irq < 0) {
1253 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1258 /* Only enable wake-up/irq when controller is powered */
1259 device_set_wakeup_capable(&pdev->dev, true);
1260 device_wakeup_disable(&pdev->dev);
1263 platform_set_drvdata(pdev, idev);
1265 /* Place this instance on the device list */
1266 mutex_lock(&intel_device_list_lock);
1267 list_add_tail(&idev->list, &intel_device_list);
1268 mutex_unlock(&intel_device_list_lock);
1270 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1271 desc_to_gpio(idev->reset), idev->irq);
1276 static int intel_remove(struct platform_device *pdev)
1278 struct intel_device *idev = platform_get_drvdata(pdev);
1280 device_wakeup_disable(&pdev->dev);
1282 mutex_lock(&intel_device_list_lock);
1283 list_del(&idev->list);
1284 mutex_unlock(&intel_device_list_lock);
1286 dev_info(&pdev->dev, "unregistered.\n");
1291 static struct platform_driver intel_driver = {
1292 .probe = intel_probe,
1293 .remove = intel_remove,
1295 .name = "hci_intel",
1296 .acpi_match_table = ACPI_PTR(intel_acpi_match),
1297 .pm = &intel_pm_ops,
1301 int __init intel_init(void)
1303 platform_driver_register(&intel_driver);
1305 return hci_uart_register_proto(&intel_proto);
1308 int __exit intel_deinit(void)
1310 platform_driver_unregister(&intel_driver);
1312 return hci_uart_unregister_proto(&intel_proto);