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>
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
42 #define STATE_BOOTLOADER 0
43 #define STATE_DOWNLOADING 1
44 #define STATE_FIRMWARE_LOADED 2
45 #define STATE_FIRMWARE_FAILED 3
46 #define STATE_BOOTING 4
49 struct list_head list;
50 struct platform_device *pdev;
51 struct gpio_desc *reset;
55 static LIST_HEAD(intel_device_list);
56 static DEFINE_SPINLOCK(intel_device_list_lock);
59 struct sk_buff *rx_skb;
60 struct sk_buff_head txq;
64 static u8 intel_convert_speed(unsigned int speed)
96 static int intel_wait_booting(struct hci_uart *hu)
98 struct intel_data *intel = hu->priv;
101 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
103 msecs_to_jiffies(1000));
106 BT_ERR("%s: Device boot interrupted", hu->hdev->name);
111 BT_ERR("%s: Device boot timeout", hu->hdev->name);
118 static irqreturn_t intel_irq(int irq, void *dev_id)
120 struct intel_device *idev = dev_id;
122 dev_info(&idev->pdev->dev, "hci_intel irq\n");
127 static int intel_set_power(struct hci_uart *hu, bool powered)
132 spin_lock(&intel_device_list_lock);
134 list_for_each(p, &intel_device_list) {
135 struct intel_device *idev = list_entry(p, struct intel_device,
138 /* tty device and pdev device should share the same parent
139 * which is the UART port.
141 if (hu->tty->dev->parent != idev->pdev->dev.parent)
149 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
150 hu, dev_name(&idev->pdev->dev), powered);
152 gpiod_set_value(idev->reset, powered);
157 if (powered && device_can_wakeup(&idev->pdev->dev)) {
158 err = devm_request_threaded_irq(&idev->pdev->dev,
162 "bt-host-wake", idev);
164 BT_ERR("hu %p, unable to allocate irq-%d",
169 device_wakeup_enable(&idev->pdev->dev);
170 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
171 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
172 device_wakeup_disable(&idev->pdev->dev);
176 spin_unlock(&intel_device_list_lock);
181 static int intel_open(struct hci_uart *hu)
183 struct intel_data *intel;
187 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
191 skb_queue_head_init(&intel->txq);
195 if (!intel_set_power(hu, true))
196 set_bit(STATE_BOOTING, &intel->flags);
201 static int intel_close(struct hci_uart *hu)
203 struct intel_data *intel = hu->priv;
207 intel_set_power(hu, false);
209 skb_queue_purge(&intel->txq);
210 kfree_skb(intel->rx_skb);
217 static int intel_flush(struct hci_uart *hu)
219 struct intel_data *intel = hu->priv;
223 skb_queue_purge(&intel->txq);
228 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
231 struct hci_event_hdr *hdr;
232 struct hci_ev_cmd_complete *evt;
234 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
238 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
239 hdr->evt = HCI_EV_CMD_COMPLETE;
240 hdr->plen = sizeof(*evt) + 1;
242 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
244 evt->opcode = cpu_to_le16(opcode);
246 *skb_put(skb, 1) = 0x00;
248 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
250 return hci_recv_frame(hdev, skb);
253 static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
255 struct intel_data *intel = hu->priv;
256 struct hci_dev *hdev = hu->hdev;
257 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
261 /* This can be the first command sent to the chip, check
262 * that the controller is ready.
264 err = intel_wait_booting(hu);
266 clear_bit(STATE_BOOTING, &intel->flags);
268 /* In case of timeout, try to continue anyway */
269 if (err && err != ETIMEDOUT)
272 BT_INFO("%s: Change controller speed to %d", hdev->name, speed);
274 speed_cmd[3] = intel_convert_speed(speed);
275 if (speed_cmd[3] == 0xff) {
276 BT_ERR("%s: Unsupported speed", hdev->name);
280 /* Device will not accept speed change if Intel version has not been
281 * previously requested.
283 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
285 BT_ERR("%s: Reading Intel version information failed (%ld)",
286 hdev->name, PTR_ERR(skb));
291 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
293 BT_ERR("%s: Failed to allocate memory for baudrate packet",
298 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
299 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
301 hci_uart_set_flow_control(hu, true);
303 skb_queue_tail(&intel->txq, skb);
304 hci_uart_tx_wakeup(hu);
306 /* wait 100ms to change baudrate on controller side */
309 hci_uart_set_baudrate(hu, speed);
310 hci_uart_set_flow_control(hu, false);
315 static int intel_setup(struct hci_uart *hu)
317 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
318 0x00, 0x08, 0x04, 0x00 };
319 struct intel_data *intel = hu->priv;
320 struct hci_dev *hdev = hu->hdev;
322 struct intel_version *ver;
323 struct intel_boot_params *params;
324 const struct firmware *fw;
328 ktime_t calltime, delta, rettime;
329 unsigned long long duration;
330 unsigned int init_speed, oper_speed;
331 int speed_change = 0;
334 BT_DBG("%s", hdev->name);
336 hu->hdev->set_bdaddr = btintel_set_bdaddr;
338 calltime = ktime_get();
341 init_speed = hu->init_speed;
343 init_speed = hu->proto->init_speed;
346 oper_speed = hu->oper_speed;
348 oper_speed = hu->proto->oper_speed;
350 if (oper_speed && init_speed && oper_speed != init_speed)
353 /* Check that the controller is ready */
354 err = intel_wait_booting(hu);
356 clear_bit(STATE_BOOTING, &intel->flags);
358 /* In case of timeout, try to continue anyway */
359 if (err && err != ETIMEDOUT)
362 set_bit(STATE_BOOTLOADER, &intel->flags);
364 /* Read the Intel version information to determine if the device
365 * is in bootloader mode or if it already has operational firmware
368 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
370 BT_ERR("%s: Reading Intel version information failed (%ld)",
371 hdev->name, PTR_ERR(skb));
375 if (skb->len != sizeof(*ver)) {
376 BT_ERR("%s: Intel version event size mismatch", hdev->name);
381 ver = (struct intel_version *)skb->data;
383 BT_ERR("%s: Intel version command failure (%02x)",
384 hdev->name, ver->status);
385 err = -bt_to_errno(ver->status);
390 /* The hardware platform number has a fixed value of 0x37 and
391 * for now only accept this single value.
393 if (ver->hw_platform != 0x37) {
394 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
395 hdev->name, ver->hw_platform);
400 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
401 * supported by this firmware loading method. This check has been
402 * put in place to ensure correct forward compatibility options
403 * when newer hardware variants come along.
405 if (ver->hw_variant != 0x0b) {
406 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
407 hdev->name, ver->hw_variant);
412 btintel_version_info(hdev, ver);
414 /* The firmware variant determines if the device is in bootloader
415 * mode or is running operational firmware. The value 0x06 identifies
416 * the bootloader and the value 0x23 identifies the operational
419 * When the operational firmware is already present, then only
420 * the check for valid Bluetooth device address is needed. This
421 * determines if the device will be added as configured or
422 * unconfigured controller.
424 * It is not possible to use the Secure Boot Parameters in this
425 * case since that command is only available in bootloader mode.
427 if (ver->fw_variant == 0x23) {
429 clear_bit(STATE_BOOTLOADER, &intel->flags);
430 btintel_check_bdaddr(hdev);
434 /* If the device is not in bootloader mode, then the only possible
435 * choice is to return an error and abort the device initialization.
437 if (ver->fw_variant != 0x06) {
438 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
439 hdev->name, ver->fw_variant);
446 /* Read the secure boot parameters to identify the operating
447 * details of the bootloader.
449 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
451 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
452 hdev->name, PTR_ERR(skb));
456 if (skb->len != sizeof(*params)) {
457 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
462 params = (struct intel_boot_params *)skb->data;
463 if (params->status) {
464 BT_ERR("%s: Intel boot parameters command failure (%02x)",
465 hdev->name, params->status);
466 err = -bt_to_errno(params->status);
471 BT_INFO("%s: Device revision is %u", hdev->name,
472 le16_to_cpu(params->dev_revid));
474 BT_INFO("%s: Secure boot is %s", hdev->name,
475 params->secure_boot ? "enabled" : "disabled");
477 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
478 params->min_fw_build_nn, params->min_fw_build_cw,
479 2000 + params->min_fw_build_yy);
481 /* It is required that every single firmware fragment is acknowledged
482 * with a command complete event. If the boot parameters indicate
483 * that this bootloader does not send them, then abort the setup.
485 if (params->limited_cce != 0x00) {
486 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
487 hdev->name, params->limited_cce);
492 /* If the OTP has no valid Bluetooth device address, then there will
493 * also be no valid address for the operational firmware.
495 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
496 BT_INFO("%s: No device address configured", hdev->name);
497 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
500 /* With this Intel bootloader only the hardware variant and device
501 * revision information are used to select the right firmware.
503 * Currently this bootloader support is limited to hardware variant
504 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
506 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
507 le16_to_cpu(params->dev_revid));
509 err = request_firmware(&fw, fwname, &hdev->dev);
511 BT_ERR("%s: Failed to load Intel firmware file (%d)",
517 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
521 if (fw->size < 644) {
522 BT_ERR("%s: Invalid size of firmware file (%zu)",
523 hdev->name, fw->size);
528 set_bit(STATE_DOWNLOADING, &intel->flags);
530 /* Start the firmware download transaction with the Init fragment
531 * represented by the 128 bytes of CSS header.
533 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
535 BT_ERR("%s: Failed to send firmware header (%d)",
540 /* Send the 256 bytes of public key information from the firmware
541 * as the PKey fragment.
543 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
545 BT_ERR("%s: Failed to send firmware public key (%d)",
550 /* Send the 256 bytes of signature information from the firmware
551 * as the Sign fragment.
553 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
555 BT_ERR("%s: Failed to send firmware signature (%d)",
560 fw_ptr = fw->data + 644;
563 while (fw_ptr - fw->data < fw->size) {
564 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
566 frag_len += sizeof(*cmd) + cmd->plen;
568 BT_DBG("%s: patching %td/%zu", hdev->name,
569 (fw_ptr - fw->data), fw->size);
571 /* The parameter length of the secure send command requires
572 * a 4 byte alignment. It happens so that the firmware file
573 * contains proper Intel_NOP commands to align the fragments
576 * Send set of commands with 4 byte alignment from the
577 * firmware data buffer as a single Data fragement.
582 /* Send each command from the firmware data buffer as
583 * a single Data fragment.
585 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
587 BT_ERR("%s: Failed to send firmware data (%d)",
596 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
598 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
600 /* Before switching the device into operational mode and with that
601 * booting the loaded firmware, wait for the bootloader notification
602 * that all fragments have been successfully received.
604 * When the event processing receives the notification, then the
605 * STATE_DOWNLOADING flag will be cleared.
607 * The firmware loading should not take longer than 5 seconds
608 * and thus just timeout if that happens and fail the setup
611 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
613 msecs_to_jiffies(5000));
615 BT_ERR("%s: Firmware loading interrupted", hdev->name);
621 BT_ERR("%s: Firmware loading timeout", hdev->name);
626 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
627 BT_ERR("%s: Firmware loading failed", hdev->name);
632 rettime = ktime_get();
633 delta = ktime_sub(rettime, calltime);
634 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
636 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
639 release_firmware(fw);
644 /* We need to restore the default speed before Intel reset */
646 err = intel_set_baudrate(hu, init_speed);
651 calltime = ktime_get();
653 set_bit(STATE_BOOTING, &intel->flags);
655 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
662 /* The bootloader will not indicate when the device is ready. This
663 * is done by the operational firmware sending bootup notification.
665 * Booting into operational firmware should not take longer than
666 * 1 second. However if that happens, then just fail the setup
667 * since something went wrong.
669 BT_INFO("%s: Waiting for device to boot", hdev->name);
671 err = intel_wait_booting(hu);
675 clear_bit(STATE_BOOTING, &intel->flags);
677 rettime = ktime_get();
678 delta = ktime_sub(rettime, calltime);
679 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
681 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
683 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
689 err = intel_set_baudrate(hu, oper_speed);
694 BT_INFO("%s: Setup complete", hdev->name);
696 clear_bit(STATE_BOOTLOADER, &intel->flags);
701 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
703 struct hci_uart *hu = hci_get_drvdata(hdev);
704 struct intel_data *intel = hu->priv;
705 struct hci_event_hdr *hdr;
707 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
708 !test_bit(STATE_BOOTING, &intel->flags))
711 hdr = (void *)skb->data;
713 /* When the firmware loading completes the device sends
714 * out a vendor specific event indicating the result of
715 * the firmware loading.
717 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
718 skb->data[2] == 0x06) {
719 if (skb->data[3] != 0x00)
720 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
722 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
723 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
724 smp_mb__after_atomic();
725 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
728 /* When switching to the operational firmware the device
729 * sends a vendor specific event indicating that the bootup
732 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
733 skb->data[2] == 0x02) {
734 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
735 smp_mb__after_atomic();
736 wake_up_bit(&intel->flags, STATE_BOOTING);
740 return hci_recv_frame(hdev, skb);
743 static const struct h4_recv_pkt intel_recv_pkts[] = {
744 { H4_RECV_ACL, .recv = hci_recv_frame },
745 { H4_RECV_SCO, .recv = hci_recv_frame },
746 { H4_RECV_EVENT, .recv = intel_recv_event },
749 static int intel_recv(struct hci_uart *hu, const void *data, int count)
751 struct intel_data *intel = hu->priv;
753 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
756 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
758 ARRAY_SIZE(intel_recv_pkts));
759 if (IS_ERR(intel->rx_skb)) {
760 int err = PTR_ERR(intel->rx_skb);
761 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
762 intel->rx_skb = NULL;
769 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
771 struct intel_data *intel = hu->priv;
773 BT_DBG("hu %p skb %p", hu, skb);
775 skb_queue_tail(&intel->txq, skb);
780 static struct sk_buff *intel_dequeue(struct hci_uart *hu)
782 struct intel_data *intel = hu->priv;
785 skb = skb_dequeue(&intel->txq);
789 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
790 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
791 struct hci_command_hdr *cmd = (void *)skb->data;
792 __u16 opcode = le16_to_cpu(cmd->opcode);
794 /* When the 0xfc01 command is issued to boot into
795 * the operational firmware, it will actually not
796 * send a command complete event. To keep the flow
797 * control working inject that event here.
799 if (opcode == 0xfc01)
800 inject_cmd_complete(hu->hdev, opcode);
803 /* Prepend skb with frame type */
804 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
809 static const struct hci_uart_proto intel_proto = {
810 .id = HCI_UART_INTEL,
812 .init_speed = 115200,
813 .oper_speed = 3000000,
815 .close = intel_close,
816 .flush = intel_flush,
817 .setup = intel_setup,
818 .set_baudrate = intel_set_baudrate,
820 .enqueue = intel_enqueue,
821 .dequeue = intel_dequeue,
825 static const struct acpi_device_id intel_acpi_match[] = {
829 MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
831 static int intel_acpi_probe(struct intel_device *idev)
833 const struct acpi_device_id *id;
835 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
842 static int intel_acpi_probe(struct intel_device *idev)
848 static int intel_probe(struct platform_device *pdev)
850 struct intel_device *idev;
852 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
858 if (ACPI_HANDLE(&pdev->dev)) {
859 int err = intel_acpi_probe(idev);
866 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
868 if (IS_ERR(idev->reset)) {
869 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
870 return PTR_ERR(idev->reset);
873 idev->irq = platform_get_irq(pdev, 0);
875 struct gpio_desc *host_wake;
877 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
879 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
881 if (IS_ERR(host_wake)) {
882 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
886 idev->irq = gpiod_to_irq(host_wake);
888 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
893 /* Only enable wake-up/irq when controller is powered */
894 device_set_wakeup_capable(&pdev->dev, true);
895 device_wakeup_disable(&pdev->dev);
898 platform_set_drvdata(pdev, idev);
900 /* Place this instance on the device list */
901 spin_lock(&intel_device_list_lock);
902 list_add_tail(&idev->list, &intel_device_list);
903 spin_unlock(&intel_device_list_lock);
905 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
906 desc_to_gpio(idev->reset), idev->irq);
911 static int intel_remove(struct platform_device *pdev)
913 struct intel_device *idev = platform_get_drvdata(pdev);
915 device_wakeup_disable(&pdev->dev);
917 spin_lock(&intel_device_list_lock);
918 list_del(&idev->list);
919 spin_unlock(&intel_device_list_lock);
921 dev_info(&pdev->dev, "unregistered.\n");
926 static struct platform_driver intel_driver = {
927 .probe = intel_probe,
928 .remove = intel_remove,
931 .acpi_match_table = ACPI_PTR(intel_acpi_match),
935 int __init intel_init(void)
937 platform_driver_register(&intel_driver);
939 return hci_uart_register_proto(&intel_proto);
942 int __exit intel_deinit(void)
944 platform_driver_unregister(&intel_driver);
946 return hci_uart_unregister_proto(&intel_proto);