1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * Bluetooth HCI UART driver for Intel devices 5 * 6 * Copyright (C) 2015 Intel Corporation 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/errno.h> 11 #include <linux/skbuff.h> 12 #include <linux/firmware.h> 13 #include <linux/module.h> 14 #include <linux/wait.h> 15 #include <linux/tty.h> 16 #include <linux/platform_device.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/acpi.h> 19 #include <linux/interrupt.h> 20 #include <linux/pm_runtime.h> 21 22 #include <net/bluetooth/bluetooth.h> 23 #include <net/bluetooth/hci_core.h> 24 25 #include "hci_uart.h" 26 #include "btintel.h" 27 28 #define STATE_BOOTLOADER 0 29 #define STATE_DOWNLOADING 1 30 #define STATE_FIRMWARE_LOADED 2 31 #define STATE_FIRMWARE_FAILED 3 32 #define STATE_BOOTING 4 33 #define STATE_LPM_ENABLED 5 34 #define STATE_TX_ACTIVE 6 35 #define STATE_SUSPENDED 7 36 #define STATE_LPM_TRANSACTION 8 37 38 #define HCI_LPM_WAKE_PKT 0xf0 39 #define HCI_LPM_PKT 0xf1 40 #define HCI_LPM_MAX_SIZE 10 41 #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE 42 43 #define LPM_OP_TX_NOTIFY 0x00 44 #define LPM_OP_SUSPEND_ACK 0x02 45 #define LPM_OP_RESUME_ACK 0x03 46 47 #define LPM_SUSPEND_DELAY_MS 1000 48 49 struct hci_lpm_pkt { 50 __u8 opcode; 51 __u8 dlen; 52 __u8 data[]; 53 } __packed; 54 55 struct intel_device { 56 struct list_head list; 57 struct platform_device *pdev; 58 struct gpio_desc *reset; 59 struct hci_uart *hu; 60 struct mutex hu_lock; 61 int irq; 62 }; 63 64 static LIST_HEAD(intel_device_list); 65 static DEFINE_MUTEX(intel_device_list_lock); 66 67 struct intel_data { 68 struct sk_buff *rx_skb; 69 struct sk_buff_head txq; 70 struct work_struct busy_work; 71 struct hci_uart *hu; 72 unsigned long flags; 73 }; 74 75 static u8 intel_convert_speed(unsigned int speed) 76 { 77 switch (speed) { 78 case 9600: 79 return 0x00; 80 case 19200: 81 return 0x01; 82 case 38400: 83 return 0x02; 84 case 57600: 85 return 0x03; 86 case 115200: 87 return 0x04; 88 case 230400: 89 return 0x05; 90 case 460800: 91 return 0x06; 92 case 921600: 93 return 0x07; 94 case 1843200: 95 return 0x08; 96 case 3250000: 97 return 0x09; 98 case 2000000: 99 return 0x0a; 100 case 3000000: 101 return 0x0b; 102 default: 103 return 0xff; 104 } 105 } 106 107 static int intel_wait_booting(struct hci_uart *hu) 108 { 109 struct intel_data *intel = hu->priv; 110 int err; 111 112 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING, 113 TASK_INTERRUPTIBLE, 114 msecs_to_jiffies(1000)); 115 116 if (err == -EINTR) { 117 bt_dev_err(hu->hdev, "Device boot interrupted"); 118 return -EINTR; 119 } 120 121 if (err) { 122 bt_dev_err(hu->hdev, "Device boot timeout"); 123 return -ETIMEDOUT; 124 } 125 126 return err; 127 } 128 129 static int intel_wait_lpm_transaction(struct hci_uart *hu) 130 { 131 struct intel_data *intel = hu->priv; 132 int err; 133 134 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION, 135 TASK_INTERRUPTIBLE, 136 msecs_to_jiffies(1000)); 137 138 if (err == -EINTR) { 139 bt_dev_err(hu->hdev, "LPM transaction interrupted"); 140 return -EINTR; 141 } 142 143 if (err) { 144 bt_dev_err(hu->hdev, "LPM transaction timeout"); 145 return -ETIMEDOUT; 146 } 147 148 return err; 149 } 150 151 static int intel_lpm_suspend(struct hci_uart *hu) 152 { 153 static const u8 suspend[] = { 0x01, 0x01, 0x01 }; 154 struct intel_data *intel = hu->priv; 155 struct sk_buff *skb; 156 157 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) || 158 test_bit(STATE_SUSPENDED, &intel->flags)) 159 return 0; 160 161 if (test_bit(STATE_TX_ACTIVE, &intel->flags)) 162 return -EAGAIN; 163 164 bt_dev_dbg(hu->hdev, "Suspending"); 165 166 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL); 167 if (!skb) { 168 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); 169 return -ENOMEM; 170 } 171 172 skb_put_data(skb, suspend, sizeof(suspend)); 173 hci_skb_pkt_type(skb) = HCI_LPM_PKT; 174 175 set_bit(STATE_LPM_TRANSACTION, &intel->flags); 176 177 /* LPM flow is a priority, enqueue packet at list head */ 178 skb_queue_head(&intel->txq, skb); 179 hci_uart_tx_wakeup(hu); 180 181 intel_wait_lpm_transaction(hu); 182 /* Even in case of failure, continue and test the suspended flag */ 183 184 clear_bit(STATE_LPM_TRANSACTION, &intel->flags); 185 186 if (!test_bit(STATE_SUSPENDED, &intel->flags)) { 187 bt_dev_err(hu->hdev, "Device suspend error"); 188 return -EINVAL; 189 } 190 191 bt_dev_dbg(hu->hdev, "Suspended"); 192 193 hci_uart_set_flow_control(hu, true); 194 195 return 0; 196 } 197 198 static int intel_lpm_resume(struct hci_uart *hu) 199 { 200 struct intel_data *intel = hu->priv; 201 struct sk_buff *skb; 202 203 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) || 204 !test_bit(STATE_SUSPENDED, &intel->flags)) 205 return 0; 206 207 bt_dev_dbg(hu->hdev, "Resuming"); 208 209 hci_uart_set_flow_control(hu, false); 210 211 skb = bt_skb_alloc(0, GFP_KERNEL); 212 if (!skb) { 213 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); 214 return -ENOMEM; 215 } 216 217 hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT; 218 219 set_bit(STATE_LPM_TRANSACTION, &intel->flags); 220 221 /* LPM flow is a priority, enqueue packet at list head */ 222 skb_queue_head(&intel->txq, skb); 223 hci_uart_tx_wakeup(hu); 224 225 intel_wait_lpm_transaction(hu); 226 /* Even in case of failure, continue and test the suspended flag */ 227 228 clear_bit(STATE_LPM_TRANSACTION, &intel->flags); 229 230 if (test_bit(STATE_SUSPENDED, &intel->flags)) { 231 bt_dev_err(hu->hdev, "Device resume error"); 232 return -EINVAL; 233 } 234 235 bt_dev_dbg(hu->hdev, "Resumed"); 236 237 return 0; 238 } 239 240 static int intel_lpm_host_wake(struct hci_uart *hu) 241 { 242 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 }; 243 struct intel_data *intel = hu->priv; 244 struct sk_buff *skb; 245 246 hci_uart_set_flow_control(hu, false); 247 248 clear_bit(STATE_SUSPENDED, &intel->flags); 249 250 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL); 251 if (!skb) { 252 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet"); 253 return -ENOMEM; 254 } 255 256 skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack)); 257 hci_skb_pkt_type(skb) = HCI_LPM_PKT; 258 259 /* LPM flow is a priority, enqueue packet at list head */ 260 skb_queue_head(&intel->txq, skb); 261 hci_uart_tx_wakeup(hu); 262 263 bt_dev_dbg(hu->hdev, "Resumed by controller"); 264 265 return 0; 266 } 267 268 static irqreturn_t intel_irq(int irq, void *dev_id) 269 { 270 struct intel_device *idev = dev_id; 271 272 dev_info(&idev->pdev->dev, "hci_intel irq\n"); 273 274 mutex_lock(&idev->hu_lock); 275 if (idev->hu) 276 intel_lpm_host_wake(idev->hu); 277 mutex_unlock(&idev->hu_lock); 278 279 /* Host/Controller are now LPM resumed, trigger a new delayed suspend */ 280 pm_runtime_get(&idev->pdev->dev); 281 pm_runtime_put_autosuspend(&idev->pdev->dev); 282 283 return IRQ_HANDLED; 284 } 285 286 static int intel_set_power(struct hci_uart *hu, bool powered) 287 { 288 struct intel_device *idev; 289 int err = -ENODEV; 290 291 if (!hu->tty->dev) 292 return err; 293 294 mutex_lock(&intel_device_list_lock); 295 296 list_for_each_entry(idev, &intel_device_list, list) { 297 /* tty device and pdev device should share the same parent 298 * which is the UART port. 299 */ 300 if (hu->tty->dev->parent != idev->pdev->dev.parent) 301 continue; 302 303 if (!idev->reset) { 304 err = -ENOTSUPP; 305 break; 306 } 307 308 BT_INFO("hu %p, Switching compatible pm device (%s) to %u", 309 hu, dev_name(&idev->pdev->dev), powered); 310 311 gpiod_set_value(idev->reset, powered); 312 313 /* Provide to idev a hu reference which is used to run LPM 314 * transactions (lpm suspend/resume) from PM callbacks. 315 * hu needs to be protected against concurrent removing during 316 * these PM ops. 317 */ 318 mutex_lock(&idev->hu_lock); 319 idev->hu = powered ? hu : NULL; 320 mutex_unlock(&idev->hu_lock); 321 322 if (idev->irq < 0) 323 break; 324 325 if (powered && device_can_wakeup(&idev->pdev->dev)) { 326 err = devm_request_threaded_irq(&idev->pdev->dev, 327 idev->irq, NULL, 328 intel_irq, 329 IRQF_ONESHOT, 330 "bt-host-wake", idev); 331 if (err) { 332 BT_ERR("hu %p, unable to allocate irq-%d", 333 hu, idev->irq); 334 break; 335 } 336 337 device_wakeup_enable(&idev->pdev->dev); 338 339 pm_runtime_set_active(&idev->pdev->dev); 340 pm_runtime_use_autosuspend(&idev->pdev->dev); 341 pm_runtime_set_autosuspend_delay(&idev->pdev->dev, 342 LPM_SUSPEND_DELAY_MS); 343 pm_runtime_enable(&idev->pdev->dev); 344 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) { 345 devm_free_irq(&idev->pdev->dev, idev->irq, idev); 346 device_wakeup_disable(&idev->pdev->dev); 347 348 pm_runtime_dont_use_autosuspend(&idev->pdev->dev); 349 pm_runtime_disable(&idev->pdev->dev); 350 } 351 } 352 353 mutex_unlock(&intel_device_list_lock); 354 355 return err; 356 } 357 358 static void intel_busy_work(struct work_struct *work) 359 { 360 struct intel_data *intel = container_of(work, struct intel_data, 361 busy_work); 362 struct intel_device *idev; 363 364 if (!intel->hu->tty->dev) 365 return; 366 367 /* Link is busy, delay the suspend */ 368 mutex_lock(&intel_device_list_lock); 369 list_for_each_entry(idev, &intel_device_list, list) { 370 if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) { 371 pm_runtime_get(&idev->pdev->dev); 372 pm_runtime_put_autosuspend(&idev->pdev->dev); 373 break; 374 } 375 } 376 mutex_unlock(&intel_device_list_lock); 377 } 378 379 static int intel_open(struct hci_uart *hu) 380 { 381 struct intel_data *intel; 382 383 BT_DBG("hu %p", hu); 384 385 if (!hci_uart_has_flow_control(hu)) 386 return -EOPNOTSUPP; 387 388 intel = kzalloc_obj(*intel); 389 if (!intel) 390 return -ENOMEM; 391 392 skb_queue_head_init(&intel->txq); 393 INIT_WORK(&intel->busy_work, intel_busy_work); 394 395 intel->hu = hu; 396 397 hu->priv = intel; 398 399 if (!intel_set_power(hu, true)) 400 set_bit(STATE_BOOTING, &intel->flags); 401 402 return 0; 403 } 404 405 static int intel_close(struct hci_uart *hu) 406 { 407 struct intel_data *intel = hu->priv; 408 409 BT_DBG("hu %p", hu); 410 411 cancel_work_sync(&intel->busy_work); 412 413 intel_set_power(hu, false); 414 415 skb_queue_purge(&intel->txq); 416 kfree_skb(intel->rx_skb); 417 kfree(intel); 418 419 hu->priv = NULL; 420 return 0; 421 } 422 423 static int intel_flush(struct hci_uart *hu) 424 { 425 struct intel_data *intel = hu->priv; 426 427 BT_DBG("hu %p", hu); 428 429 skb_queue_purge(&intel->txq); 430 431 return 0; 432 } 433 434 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode) 435 { 436 struct sk_buff *skb; 437 struct hci_event_hdr *hdr; 438 struct hci_ev_cmd_complete *evt; 439 440 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL); 441 if (!skb) 442 return -ENOMEM; 443 444 hdr = skb_put(skb, sizeof(*hdr)); 445 hdr->evt = HCI_EV_CMD_COMPLETE; 446 hdr->plen = sizeof(*evt) + 1; 447 448 evt = skb_put(skb, sizeof(*evt)); 449 evt->ncmd = 0x01; 450 evt->opcode = cpu_to_le16(opcode); 451 452 skb_put_u8(skb, 0x00); 453 454 hci_skb_pkt_type(skb) = HCI_EVENT_PKT; 455 456 return hci_recv_frame(hdev, skb); 457 } 458 459 static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed) 460 { 461 struct intel_data *intel = hu->priv; 462 struct hci_dev *hdev = hu->hdev; 463 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 }; 464 struct sk_buff *skb; 465 int err; 466 467 /* This can be the first command sent to the chip, check 468 * that the controller is ready. 469 */ 470 err = intel_wait_booting(hu); 471 472 clear_bit(STATE_BOOTING, &intel->flags); 473 474 /* In case of timeout, try to continue anyway */ 475 if (err && err != -ETIMEDOUT) 476 return err; 477 478 bt_dev_info(hdev, "Change controller speed to %d", speed); 479 480 speed_cmd[3] = intel_convert_speed(speed); 481 if (speed_cmd[3] == 0xff) { 482 bt_dev_err(hdev, "Unsupported speed"); 483 return -EINVAL; 484 } 485 486 /* Device will not accept speed change if Intel version has not been 487 * previously requested. 488 */ 489 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT); 490 if (IS_ERR(skb)) { 491 bt_dev_err(hdev, "Reading Intel version information failed (%ld)", 492 PTR_ERR(skb)); 493 return PTR_ERR(skb); 494 } 495 kfree_skb(skb); 496 497 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL); 498 if (!skb) { 499 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet"); 500 return -ENOMEM; 501 } 502 503 skb_put_data(skb, speed_cmd, sizeof(speed_cmd)); 504 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; 505 506 hci_uart_set_flow_control(hu, true); 507 508 skb_queue_tail(&intel->txq, skb); 509 hci_uart_tx_wakeup(hu); 510 511 /* wait 100ms to change baudrate on controller side */ 512 msleep(100); 513 514 hci_uart_set_baudrate(hu, speed); 515 hci_uart_set_flow_control(hu, false); 516 517 return 0; 518 } 519 520 static int intel_setup(struct hci_uart *hu) 521 { 522 struct intel_data *intel = hu->priv; 523 struct hci_dev *hdev = hu->hdev; 524 struct sk_buff *skb; 525 struct intel_version ver; 526 struct intel_boot_params params; 527 struct intel_device *idev; 528 const struct firmware *fw; 529 char fwname[64]; 530 u32 boot_param; 531 ktime_t calltime, delta, rettime; 532 unsigned long long duration; 533 unsigned int init_speed, oper_speed; 534 int speed_change = 0; 535 int err; 536 537 bt_dev_dbg(hdev, ""); 538 539 hu->hdev->set_diag = btintel_set_diag; 540 hu->hdev->set_bdaddr = btintel_set_bdaddr; 541 542 /* Set the default boot parameter to 0x0 and it is updated to 543 * SKU specific boot parameter after reading Intel_Write_Boot_Params 544 * command while downloading the firmware. 545 */ 546 boot_param = 0x00000000; 547 548 calltime = ktime_get(); 549 550 if (hu->init_speed) 551 init_speed = hu->init_speed; 552 else 553 init_speed = hu->proto->init_speed; 554 555 if (hu->oper_speed) 556 oper_speed = hu->oper_speed; 557 else 558 oper_speed = hu->proto->oper_speed; 559 560 if (oper_speed && init_speed && oper_speed != init_speed) 561 speed_change = 1; 562 563 /* Check that the controller is ready */ 564 err = intel_wait_booting(hu); 565 566 clear_bit(STATE_BOOTING, &intel->flags); 567 568 /* In case of timeout, try to continue anyway */ 569 if (err && err != -ETIMEDOUT) 570 return err; 571 572 set_bit(STATE_BOOTLOADER, &intel->flags); 573 574 /* Read the Intel version information to determine if the device 575 * is in bootloader mode or if it already has operational firmware 576 * loaded. 577 */ 578 err = btintel_read_version(hdev, &ver); 579 if (err) 580 return err; 581 582 /* The hardware platform number has a fixed value of 0x37 and 583 * for now only accept this single value. 584 */ 585 if (ver.hw_platform != 0x37) { 586 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)", 587 ver.hw_platform); 588 return -EINVAL; 589 } 590 591 /* Check for supported iBT hardware variants of this firmware 592 * loading method. 593 * 594 * This check has been put in place to ensure correct forward 595 * compatibility options when newer hardware variants come along. 596 */ 597 switch (ver.hw_variant) { 598 case 0x0b: /* LnP */ 599 case 0x0c: /* WsP */ 600 case 0x12: /* ThP */ 601 break; 602 default: 603 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", 604 ver.hw_variant); 605 return -EINVAL; 606 } 607 608 btintel_version_info(hdev, &ver); 609 610 /* The firmware variant determines if the device is in bootloader 611 * mode or is running operational firmware. The value 0x06 identifies 612 * the bootloader and the value 0x23 identifies the operational 613 * firmware. 614 * 615 * When the operational firmware is already present, then only 616 * the check for valid Bluetooth device address is needed. This 617 * determines if the device will be added as configured or 618 * unconfigured controller. 619 * 620 * It is not possible to use the Secure Boot Parameters in this 621 * case since that command is only available in bootloader mode. 622 */ 623 if (ver.fw_variant == 0x23) { 624 clear_bit(STATE_BOOTLOADER, &intel->flags); 625 btintel_check_bdaddr(hdev); 626 return 0; 627 } 628 629 /* If the device is not in bootloader mode, then the only possible 630 * choice is to return an error and abort the device initialization. 631 */ 632 if (ver.fw_variant != 0x06) { 633 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)", 634 ver.fw_variant); 635 return -ENODEV; 636 } 637 638 /* Read the secure boot parameters to identify the operating 639 * details of the bootloader. 640 */ 641 err = btintel_read_boot_params(hdev, ¶ms); 642 if (err) 643 return err; 644 645 /* It is required that every single firmware fragment is acknowledged 646 * with a command complete event. If the boot parameters indicate 647 * that this bootloader does not send them, then abort the setup. 648 */ 649 if (params.limited_cce != 0x00) { 650 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)", 651 params.limited_cce); 652 return -EINVAL; 653 } 654 655 /* If the OTP has no valid Bluetooth device address, then there will 656 * also be no valid address for the operational firmware. 657 */ 658 if (!bacmp(¶ms.otp_bdaddr, BDADDR_ANY)) { 659 bt_dev_info(hdev, "No device address configured"); 660 hci_set_quirk(hdev, HCI_QUIRK_INVALID_BDADDR); 661 } 662 663 /* With this Intel bootloader only the hardware variant and device 664 * revision information are used to select the right firmware for SfP 665 * and WsP. 666 * 667 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi. 668 * 669 * Currently the supported hardware variants are: 670 * 11 (0x0b) for iBT 3.0 (LnP/SfP) 671 * 12 (0x0c) for iBT 3.5 (WsP) 672 * 673 * For ThP/JfP and for future SKU's, the FW name varies based on HW 674 * variant, HW revision and FW revision, as these are dependent on CNVi 675 * and RF Combination. 676 * 677 * 18 (0x12) for iBT3.5 (ThP/JfP) 678 * 679 * The firmware file name for these will be 680 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi. 681 * 682 */ 683 switch (ver.hw_variant) { 684 case 0x0b: /* SfP */ 685 case 0x0c: /* WsP */ 686 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi", 687 ver.hw_variant, le16_to_cpu(params.dev_revid)); 688 break; 689 case 0x12: /* ThP */ 690 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi", 691 ver.hw_variant, ver.hw_revision, ver.fw_revision); 692 break; 693 default: 694 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", 695 ver.hw_variant); 696 return -EINVAL; 697 } 698 699 err = request_firmware(&fw, fwname, &hdev->dev); 700 if (err < 0) { 701 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", 702 err); 703 return err; 704 } 705 706 bt_dev_info(hdev, "Found device firmware: %s", fwname); 707 708 /* Save the DDC file name for later */ 709 switch (ver.hw_variant) { 710 case 0x0b: /* SfP */ 711 case 0x0c: /* WsP */ 712 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc", 713 ver.hw_variant, le16_to_cpu(params.dev_revid)); 714 break; 715 case 0x12: /* ThP */ 716 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc", 717 ver.hw_variant, ver.hw_revision, ver.fw_revision); 718 break; 719 default: 720 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)", 721 ver.hw_variant); 722 return -EINVAL; 723 } 724 725 if (fw->size < 644) { 726 bt_dev_err(hdev, "Invalid size of firmware file (%zu)", 727 fw->size); 728 err = -EBADF; 729 goto done; 730 } 731 732 set_bit(STATE_DOWNLOADING, &intel->flags); 733 734 /* Start firmware downloading and get boot parameter */ 735 err = btintel_download_firmware(hdev, &ver, fw, &boot_param); 736 if (err < 0) 737 goto done; 738 739 set_bit(STATE_FIRMWARE_LOADED, &intel->flags); 740 741 bt_dev_info(hdev, "Waiting for firmware download to complete"); 742 743 /* Before switching the device into operational mode and with that 744 * booting the loaded firmware, wait for the bootloader notification 745 * that all fragments have been successfully received. 746 * 747 * When the event processing receives the notification, then the 748 * STATE_DOWNLOADING flag will be cleared. 749 * 750 * The firmware loading should not take longer than 5 seconds 751 * and thus just timeout if that happens and fail the setup 752 * of this device. 753 */ 754 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING, 755 TASK_INTERRUPTIBLE, 756 msecs_to_jiffies(5000)); 757 if (err == -EINTR) { 758 bt_dev_err(hdev, "Firmware loading interrupted"); 759 err = -EINTR; 760 goto done; 761 } 762 763 if (err) { 764 bt_dev_err(hdev, "Firmware loading timeout"); 765 err = -ETIMEDOUT; 766 goto done; 767 } 768 769 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) { 770 bt_dev_err(hdev, "Firmware loading failed"); 771 err = -ENOEXEC; 772 goto done; 773 } 774 775 rettime = ktime_get(); 776 delta = ktime_sub(rettime, calltime); 777 duration = (unsigned long long)ktime_to_ns(delta) >> 10; 778 779 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration); 780 781 done: 782 release_firmware(fw); 783 784 /* Check if there was an error and if is not -EALREADY which means the 785 * firmware has already been loaded. 786 */ 787 if (err < 0 && err != -EALREADY) 788 return err; 789 790 /* We need to restore the default speed before Intel reset */ 791 if (speed_change) { 792 err = intel_set_baudrate(hu, init_speed); 793 if (err) 794 return err; 795 } 796 797 calltime = ktime_get(); 798 799 set_bit(STATE_BOOTING, &intel->flags); 800 801 err = btintel_send_intel_reset(hdev, boot_param); 802 if (err) 803 return err; 804 805 /* The bootloader will not indicate when the device is ready. This 806 * is done by the operational firmware sending bootup notification. 807 * 808 * Booting into operational firmware should not take longer than 809 * 1 second. However if that happens, then just fail the setup 810 * since something went wrong. 811 */ 812 bt_dev_info(hdev, "Waiting for device to boot"); 813 814 err = intel_wait_booting(hu); 815 if (err) 816 return err; 817 818 clear_bit(STATE_BOOTING, &intel->flags); 819 820 rettime = ktime_get(); 821 delta = ktime_sub(rettime, calltime); 822 duration = (unsigned long long)ktime_to_ns(delta) >> 10; 823 824 bt_dev_info(hdev, "Device booted in %llu usecs", duration); 825 826 /* Enable LPM if matching pdev with wakeup enabled, set TX active 827 * until further LPM TX notification. 828 */ 829 mutex_lock(&intel_device_list_lock); 830 list_for_each_entry(idev, &intel_device_list, list) { 831 if (!hu->tty->dev) 832 break; 833 if (hu->tty->dev->parent == idev->pdev->dev.parent) { 834 if (device_may_wakeup(&idev->pdev->dev)) { 835 set_bit(STATE_LPM_ENABLED, &intel->flags); 836 set_bit(STATE_TX_ACTIVE, &intel->flags); 837 } 838 break; 839 } 840 } 841 mutex_unlock(&intel_device_list_lock); 842 843 /* Ignore errors, device can work without DDC parameters */ 844 btintel_load_ddc_config(hdev, fwname); 845 846 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT); 847 if (IS_ERR(skb)) 848 return PTR_ERR(skb); 849 kfree_skb(skb); 850 851 if (speed_change) { 852 err = intel_set_baudrate(hu, oper_speed); 853 if (err) 854 return err; 855 } 856 857 bt_dev_info(hdev, "Setup complete"); 858 859 clear_bit(STATE_BOOTLOADER, &intel->flags); 860 861 return 0; 862 } 863 864 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb) 865 { 866 struct hci_uart *hu = hci_get_drvdata(hdev); 867 struct intel_data *intel = hu->priv; 868 struct hci_event_hdr *hdr; 869 870 if (!test_bit(STATE_BOOTLOADER, &intel->flags) && 871 !test_bit(STATE_BOOTING, &intel->flags)) 872 goto recv; 873 874 hdr = (void *)skb->data; 875 876 /* When the firmware loading completes the device sends 877 * out a vendor specific event indicating the result of 878 * the firmware loading. 879 */ 880 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 && 881 skb->data[2] == 0x06) { 882 if (skb->data[3] != 0x00) 883 set_bit(STATE_FIRMWARE_FAILED, &intel->flags); 884 885 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) && 886 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) 887 wake_up_bit(&intel->flags, STATE_DOWNLOADING); 888 889 /* When switching to the operational firmware the device 890 * sends a vendor specific event indicating that the bootup 891 * completed. 892 */ 893 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 && 894 skb->data[2] == 0x02) { 895 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) 896 wake_up_bit(&intel->flags, STATE_BOOTING); 897 } 898 recv: 899 return hci_recv_frame(hdev, skb); 900 } 901 902 static void intel_recv_lpm_notify(struct hci_dev *hdev, int value) 903 { 904 struct hci_uart *hu = hci_get_drvdata(hdev); 905 struct intel_data *intel = hu->priv; 906 907 bt_dev_dbg(hdev, "TX idle notification (%d)", value); 908 909 if (value) { 910 set_bit(STATE_TX_ACTIVE, &intel->flags); 911 schedule_work(&intel->busy_work); 912 } else { 913 clear_bit(STATE_TX_ACTIVE, &intel->flags); 914 } 915 } 916 917 static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb) 918 { 919 struct hci_lpm_pkt *lpm = (void *)skb->data; 920 struct hci_uart *hu = hci_get_drvdata(hdev); 921 struct intel_data *intel = hu->priv; 922 923 switch (lpm->opcode) { 924 case LPM_OP_TX_NOTIFY: 925 if (lpm->dlen < 1) { 926 bt_dev_err(hu->hdev, "Invalid LPM notification packet"); 927 break; 928 } 929 intel_recv_lpm_notify(hdev, lpm->data[0]); 930 break; 931 case LPM_OP_SUSPEND_ACK: 932 set_bit(STATE_SUSPENDED, &intel->flags); 933 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) 934 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION); 935 break; 936 case LPM_OP_RESUME_ACK: 937 clear_bit(STATE_SUSPENDED, &intel->flags); 938 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) 939 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION); 940 break; 941 default: 942 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode); 943 break; 944 } 945 946 kfree_skb(skb); 947 948 return 0; 949 } 950 951 #define INTEL_RECV_LPM \ 952 .type = HCI_LPM_PKT, \ 953 .hlen = HCI_LPM_HDR_SIZE, \ 954 .loff = 1, \ 955 .lsize = 1, \ 956 .maxlen = HCI_LPM_MAX_SIZE 957 958 static const struct h4_recv_pkt intel_recv_pkts[] = { 959 { H4_RECV_ACL, .recv = hci_recv_frame }, 960 { H4_RECV_SCO, .recv = hci_recv_frame }, 961 { H4_RECV_EVENT, .recv = intel_recv_event }, 962 { INTEL_RECV_LPM, .recv = intel_recv_lpm }, 963 }; 964 965 static int intel_recv(struct hci_uart *hu, const void *data, int count) 966 { 967 struct intel_data *intel = hu->priv; 968 969 if (!test_bit(HCI_UART_REGISTERED, &hu->flags)) 970 return -EUNATCH; 971 972 intel->rx_skb = h4_recv_buf(hu, intel->rx_skb, data, count, 973 intel_recv_pkts, 974 ARRAY_SIZE(intel_recv_pkts)); 975 if (IS_ERR(intel->rx_skb)) { 976 int err = PTR_ERR(intel->rx_skb); 977 978 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err); 979 intel->rx_skb = NULL; 980 return err; 981 } 982 983 return count; 984 } 985 986 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb) 987 { 988 struct intel_data *intel = hu->priv; 989 struct intel_device *idev; 990 991 BT_DBG("hu %p skb %p", hu, skb); 992 993 if (!hu->tty->dev) 994 goto out_enqueue; 995 996 /* Be sure our controller is resumed and potential LPM transaction 997 * completed before enqueuing any packet. 998 */ 999 mutex_lock(&intel_device_list_lock); 1000 list_for_each_entry(idev, &intel_device_list, list) { 1001 if (hu->tty->dev->parent == idev->pdev->dev.parent) { 1002 pm_runtime_get_sync(&idev->pdev->dev); 1003 pm_runtime_put_autosuspend(&idev->pdev->dev); 1004 break; 1005 } 1006 } 1007 mutex_unlock(&intel_device_list_lock); 1008 out_enqueue: 1009 skb_queue_tail(&intel->txq, skb); 1010 1011 return 0; 1012 } 1013 1014 static struct sk_buff *intel_dequeue(struct hci_uart *hu) 1015 { 1016 struct intel_data *intel = hu->priv; 1017 struct sk_buff *skb; 1018 1019 skb = skb_dequeue(&intel->txq); 1020 if (!skb) 1021 return skb; 1022 1023 if (test_bit(STATE_BOOTLOADER, &intel->flags) && 1024 (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) { 1025 struct hci_command_hdr *cmd = (void *)skb->data; 1026 __u16 opcode = le16_to_cpu(cmd->opcode); 1027 1028 /* When the BTINTEL_HCI_OP_RESET command is issued to boot into 1029 * the operational firmware, it will actually not send a command 1030 * complete event. To keep the flow control working inject that 1031 * event here. 1032 */ 1033 if (opcode == BTINTEL_HCI_OP_RESET) 1034 inject_cmd_complete(hu->hdev, opcode); 1035 } 1036 1037 /* Prepend skb with frame type */ 1038 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); 1039 1040 return skb; 1041 } 1042 1043 static const struct hci_uart_proto intel_proto = { 1044 .id = HCI_UART_INTEL, 1045 .name = "Intel", 1046 .manufacturer = 2, 1047 .init_speed = 115200, 1048 .oper_speed = 3000000, 1049 .open = intel_open, 1050 .close = intel_close, 1051 .flush = intel_flush, 1052 .setup = intel_setup, 1053 .set_baudrate = intel_set_baudrate, 1054 .recv = intel_recv, 1055 .enqueue = intel_enqueue, 1056 .dequeue = intel_dequeue, 1057 }; 1058 1059 #ifdef CONFIG_ACPI 1060 static const struct acpi_device_id intel_acpi_match[] = { 1061 { .id = "INT33E1" }, 1062 { .id = "INT33E3" }, 1063 { } 1064 }; 1065 MODULE_DEVICE_TABLE(acpi, intel_acpi_match); 1066 #endif 1067 1068 static int intel_suspend_device(struct device *dev) 1069 { 1070 struct intel_device *idev = dev_get_drvdata(dev); 1071 1072 mutex_lock(&idev->hu_lock); 1073 if (idev->hu) 1074 intel_lpm_suspend(idev->hu); 1075 mutex_unlock(&idev->hu_lock); 1076 1077 return 0; 1078 } 1079 1080 static int intel_resume_device(struct device *dev) 1081 { 1082 struct intel_device *idev = dev_get_drvdata(dev); 1083 1084 mutex_lock(&idev->hu_lock); 1085 if (idev->hu) 1086 intel_lpm_resume(idev->hu); 1087 mutex_unlock(&idev->hu_lock); 1088 1089 return 0; 1090 } 1091 1092 static int __maybe_unused intel_suspend(struct device *dev) 1093 { 1094 struct intel_device *idev = dev_get_drvdata(dev); 1095 1096 if (device_may_wakeup(dev)) 1097 enable_irq_wake(idev->irq); 1098 1099 return intel_suspend_device(dev); 1100 } 1101 1102 static int __maybe_unused intel_resume(struct device *dev) 1103 { 1104 struct intel_device *idev = dev_get_drvdata(dev); 1105 1106 if (device_may_wakeup(dev)) 1107 disable_irq_wake(idev->irq); 1108 1109 return intel_resume_device(dev); 1110 } 1111 1112 static const struct dev_pm_ops intel_pm_ops = { 1113 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume) 1114 SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL) 1115 }; 1116 1117 static const struct acpi_gpio_params reset_gpios = { 0, 0, false }; 1118 static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false }; 1119 1120 static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = { 1121 { "reset-gpios", &reset_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO }, 1122 { "host-wake-gpios", &host_wake_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO }, 1123 { } 1124 }; 1125 1126 static int intel_probe(struct platform_device *pdev) 1127 { 1128 struct intel_device *idev; 1129 int ret; 1130 1131 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL); 1132 if (!idev) 1133 return -ENOMEM; 1134 1135 mutex_init(&idev->hu_lock); 1136 1137 idev->pdev = pdev; 1138 1139 ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios); 1140 if (ret) 1141 dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n"); 1142 1143 idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW); 1144 if (IS_ERR(idev->reset)) { 1145 dev_err(&pdev->dev, "Unable to retrieve gpio\n"); 1146 return PTR_ERR(idev->reset); 1147 } 1148 1149 idev->irq = platform_get_irq(pdev, 0); 1150 if (idev->irq < 0) { 1151 struct gpio_desc *host_wake; 1152 1153 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n"); 1154 1155 host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN); 1156 if (IS_ERR(host_wake)) { 1157 dev_err(&pdev->dev, "Unable to retrieve IRQ\n"); 1158 goto no_irq; 1159 } 1160 1161 idev->irq = gpiod_to_irq(host_wake); 1162 if (idev->irq < 0) { 1163 dev_err(&pdev->dev, "No corresponding irq for gpio\n"); 1164 goto no_irq; 1165 } 1166 } 1167 1168 /* Only enable wake-up/irq when controller is powered */ 1169 device_set_wakeup_capable(&pdev->dev, true); 1170 device_wakeup_disable(&pdev->dev); 1171 1172 no_irq: 1173 platform_set_drvdata(pdev, idev); 1174 1175 /* Place this instance on the device list */ 1176 mutex_lock(&intel_device_list_lock); 1177 list_add_tail(&idev->list, &intel_device_list); 1178 mutex_unlock(&intel_device_list_lock); 1179 1180 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n", 1181 desc_to_gpio(idev->reset), idev->irq); 1182 1183 return 0; 1184 } 1185 1186 static void intel_remove(struct platform_device *pdev) 1187 { 1188 struct intel_device *idev = platform_get_drvdata(pdev); 1189 1190 device_wakeup_disable(&pdev->dev); 1191 1192 mutex_lock(&intel_device_list_lock); 1193 list_del(&idev->list); 1194 mutex_unlock(&intel_device_list_lock); 1195 1196 dev_info(&pdev->dev, "unregistered.\n"); 1197 } 1198 1199 static struct platform_driver intel_driver = { 1200 .probe = intel_probe, 1201 .remove = intel_remove, 1202 .driver = { 1203 .name = "hci_intel", 1204 .acpi_match_table = ACPI_PTR(intel_acpi_match), 1205 .pm = &intel_pm_ops, 1206 }, 1207 }; 1208 1209 int __init intel_init(void) 1210 { 1211 int err; 1212 1213 err = platform_driver_register(&intel_driver); 1214 if (err) 1215 return err; 1216 1217 return hci_uart_register_proto(&intel_proto); 1218 } 1219 1220 int __exit intel_deinit(void) 1221 { 1222 platform_driver_unregister(&intel_driver); 1223 1224 return hci_uart_unregister_proto(&intel_proto); 1225 } 1226