Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Bluetooth HCI UART driver for Intel devices
0005  *
0006  *  Copyright (C) 2015  Intel Corporation
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/errno.h>
0011 #include <linux/skbuff.h>
0012 #include <linux/firmware.h>
0013 #include <linux/module.h>
0014 #include <linux/wait.h>
0015 #include <linux/tty.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/gpio/consumer.h>
0018 #include <linux/acpi.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/pm_runtime.h>
0021 
0022 #include <net/bluetooth/bluetooth.h>
0023 #include <net/bluetooth/hci_core.h>
0024 
0025 #include "hci_uart.h"
0026 #include "btintel.h"
0027 
0028 #define STATE_BOOTLOADER    0
0029 #define STATE_DOWNLOADING   1
0030 #define STATE_FIRMWARE_LOADED   2
0031 #define STATE_FIRMWARE_FAILED   3
0032 #define STATE_BOOTING       4
0033 #define STATE_LPM_ENABLED   5
0034 #define STATE_TX_ACTIVE     6
0035 #define STATE_SUSPENDED     7
0036 #define STATE_LPM_TRANSACTION   8
0037 
0038 #define HCI_LPM_WAKE_PKT 0xf0
0039 #define HCI_LPM_PKT 0xf1
0040 #define HCI_LPM_MAX_SIZE 10
0041 #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
0042 
0043 #define LPM_OP_TX_NOTIFY 0x00
0044 #define LPM_OP_SUSPEND_ACK 0x02
0045 #define LPM_OP_RESUME_ACK 0x03
0046 
0047 #define LPM_SUSPEND_DELAY_MS 1000
0048 
0049 struct hci_lpm_pkt {
0050     __u8 opcode;
0051     __u8 dlen;
0052     __u8 data[];
0053 } __packed;
0054 
0055 struct intel_device {
0056     struct list_head list;
0057     struct platform_device *pdev;
0058     struct gpio_desc *reset;
0059     struct hci_uart *hu;
0060     struct mutex hu_lock;
0061     int irq;
0062 };
0063 
0064 static LIST_HEAD(intel_device_list);
0065 static DEFINE_MUTEX(intel_device_list_lock);
0066 
0067 struct intel_data {
0068     struct sk_buff *rx_skb;
0069     struct sk_buff_head txq;
0070     struct work_struct busy_work;
0071     struct hci_uart *hu;
0072     unsigned long flags;
0073 };
0074 
0075 static u8 intel_convert_speed(unsigned int speed)
0076 {
0077     switch (speed) {
0078     case 9600:
0079         return 0x00;
0080     case 19200:
0081         return 0x01;
0082     case 38400:
0083         return 0x02;
0084     case 57600:
0085         return 0x03;
0086     case 115200:
0087         return 0x04;
0088     case 230400:
0089         return 0x05;
0090     case 460800:
0091         return 0x06;
0092     case 921600:
0093         return 0x07;
0094     case 1843200:
0095         return 0x08;
0096     case 3250000:
0097         return 0x09;
0098     case 2000000:
0099         return 0x0a;
0100     case 3000000:
0101         return 0x0b;
0102     default:
0103         return 0xff;
0104     }
0105 }
0106 
0107 static int intel_wait_booting(struct hci_uart *hu)
0108 {
0109     struct intel_data *intel = hu->priv;
0110     int err;
0111 
0112     err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
0113                   TASK_INTERRUPTIBLE,
0114                   msecs_to_jiffies(1000));
0115 
0116     if (err == -EINTR) {
0117         bt_dev_err(hu->hdev, "Device boot interrupted");
0118         return -EINTR;
0119     }
0120 
0121     if (err) {
0122         bt_dev_err(hu->hdev, "Device boot timeout");
0123         return -ETIMEDOUT;
0124     }
0125 
0126     return err;
0127 }
0128 
0129 #ifdef CONFIG_PM
0130 static int intel_wait_lpm_transaction(struct hci_uart *hu)
0131 {
0132     struct intel_data *intel = hu->priv;
0133     int err;
0134 
0135     err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
0136                   TASK_INTERRUPTIBLE,
0137                   msecs_to_jiffies(1000));
0138 
0139     if (err == -EINTR) {
0140         bt_dev_err(hu->hdev, "LPM transaction interrupted");
0141         return -EINTR;
0142     }
0143 
0144     if (err) {
0145         bt_dev_err(hu->hdev, "LPM transaction timeout");
0146         return -ETIMEDOUT;
0147     }
0148 
0149     return err;
0150 }
0151 
0152 static int intel_lpm_suspend(struct hci_uart *hu)
0153 {
0154     static const u8 suspend[] = { 0x01, 0x01, 0x01 };
0155     struct intel_data *intel = hu->priv;
0156     struct sk_buff *skb;
0157 
0158     if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
0159         test_bit(STATE_SUSPENDED, &intel->flags))
0160         return 0;
0161 
0162     if (test_bit(STATE_TX_ACTIVE, &intel->flags))
0163         return -EAGAIN;
0164 
0165     bt_dev_dbg(hu->hdev, "Suspending");
0166 
0167     skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
0168     if (!skb) {
0169         bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
0170         return -ENOMEM;
0171     }
0172 
0173     skb_put_data(skb, suspend, sizeof(suspend));
0174     hci_skb_pkt_type(skb) = HCI_LPM_PKT;
0175 
0176     set_bit(STATE_LPM_TRANSACTION, &intel->flags);
0177 
0178     /* LPM flow is a priority, enqueue packet at list head */
0179     skb_queue_head(&intel->txq, skb);
0180     hci_uart_tx_wakeup(hu);
0181 
0182     intel_wait_lpm_transaction(hu);
0183     /* Even in case of failure, continue and test the suspended flag */
0184 
0185     clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
0186 
0187     if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
0188         bt_dev_err(hu->hdev, "Device suspend error");
0189         return -EINVAL;
0190     }
0191 
0192     bt_dev_dbg(hu->hdev, "Suspended");
0193 
0194     hci_uart_set_flow_control(hu, true);
0195 
0196     return 0;
0197 }
0198 
0199 static int intel_lpm_resume(struct hci_uart *hu)
0200 {
0201     struct intel_data *intel = hu->priv;
0202     struct sk_buff *skb;
0203 
0204     if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
0205         !test_bit(STATE_SUSPENDED, &intel->flags))
0206         return 0;
0207 
0208     bt_dev_dbg(hu->hdev, "Resuming");
0209 
0210     hci_uart_set_flow_control(hu, false);
0211 
0212     skb = bt_skb_alloc(0, GFP_KERNEL);
0213     if (!skb) {
0214         bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
0215         return -ENOMEM;
0216     }
0217 
0218     hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
0219 
0220     set_bit(STATE_LPM_TRANSACTION, &intel->flags);
0221 
0222     /* LPM flow is a priority, enqueue packet at list head */
0223     skb_queue_head(&intel->txq, skb);
0224     hci_uart_tx_wakeup(hu);
0225 
0226     intel_wait_lpm_transaction(hu);
0227     /* Even in case of failure, continue and test the suspended flag */
0228 
0229     clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
0230 
0231     if (test_bit(STATE_SUSPENDED, &intel->flags)) {
0232         bt_dev_err(hu->hdev, "Device resume error");
0233         return -EINVAL;
0234     }
0235 
0236     bt_dev_dbg(hu->hdev, "Resumed");
0237 
0238     return 0;
0239 }
0240 #endif /* CONFIG_PM */
0241 
0242 static int intel_lpm_host_wake(struct hci_uart *hu)
0243 {
0244     static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
0245     struct intel_data *intel = hu->priv;
0246     struct sk_buff *skb;
0247 
0248     hci_uart_set_flow_control(hu, false);
0249 
0250     clear_bit(STATE_SUSPENDED, &intel->flags);
0251 
0252     skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
0253     if (!skb) {
0254         bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
0255         return -ENOMEM;
0256     }
0257 
0258     skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
0259     hci_skb_pkt_type(skb) = HCI_LPM_PKT;
0260 
0261     /* LPM flow is a priority, enqueue packet at list head */
0262     skb_queue_head(&intel->txq, skb);
0263     hci_uart_tx_wakeup(hu);
0264 
0265     bt_dev_dbg(hu->hdev, "Resumed by controller");
0266 
0267     return 0;
0268 }
0269 
0270 static irqreturn_t intel_irq(int irq, void *dev_id)
0271 {
0272     struct intel_device *idev = dev_id;
0273 
0274     dev_info(&idev->pdev->dev, "hci_intel irq\n");
0275 
0276     mutex_lock(&idev->hu_lock);
0277     if (idev->hu)
0278         intel_lpm_host_wake(idev->hu);
0279     mutex_unlock(&idev->hu_lock);
0280 
0281     /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
0282     pm_runtime_get(&idev->pdev->dev);
0283     pm_runtime_mark_last_busy(&idev->pdev->dev);
0284     pm_runtime_put_autosuspend(&idev->pdev->dev);
0285 
0286     return IRQ_HANDLED;
0287 }
0288 
0289 static int intel_set_power(struct hci_uart *hu, bool powered)
0290 {
0291     struct intel_device *idev;
0292     int err = -ENODEV;
0293 
0294     if (!hu->tty->dev)
0295         return err;
0296 
0297     mutex_lock(&intel_device_list_lock);
0298 
0299     list_for_each_entry(idev, &intel_device_list, list) {
0300         /* tty device and pdev device should share the same parent
0301          * which is the UART port.
0302          */
0303         if (hu->tty->dev->parent != idev->pdev->dev.parent)
0304             continue;
0305 
0306         if (!idev->reset) {
0307             err = -ENOTSUPP;
0308             break;
0309         }
0310 
0311         BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
0312             hu, dev_name(&idev->pdev->dev), powered);
0313 
0314         gpiod_set_value(idev->reset, powered);
0315 
0316         /* Provide to idev a hu reference which is used to run LPM
0317          * transactions (lpm suspend/resume) from PM callbacks.
0318          * hu needs to be protected against concurrent removing during
0319          * these PM ops.
0320          */
0321         mutex_lock(&idev->hu_lock);
0322         idev->hu = powered ? hu : NULL;
0323         mutex_unlock(&idev->hu_lock);
0324 
0325         if (idev->irq < 0)
0326             break;
0327 
0328         if (powered && device_can_wakeup(&idev->pdev->dev)) {
0329             err = devm_request_threaded_irq(&idev->pdev->dev,
0330                             idev->irq, NULL,
0331                             intel_irq,
0332                             IRQF_ONESHOT,
0333                             "bt-host-wake", idev);
0334             if (err) {
0335                 BT_ERR("hu %p, unable to allocate irq-%d",
0336                        hu, idev->irq);
0337                 break;
0338             }
0339 
0340             device_wakeup_enable(&idev->pdev->dev);
0341 
0342             pm_runtime_set_active(&idev->pdev->dev);
0343             pm_runtime_use_autosuspend(&idev->pdev->dev);
0344             pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
0345                              LPM_SUSPEND_DELAY_MS);
0346             pm_runtime_enable(&idev->pdev->dev);
0347         } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
0348             devm_free_irq(&idev->pdev->dev, idev->irq, idev);
0349             device_wakeup_disable(&idev->pdev->dev);
0350 
0351             pm_runtime_disable(&idev->pdev->dev);
0352         }
0353     }
0354 
0355     mutex_unlock(&intel_device_list_lock);
0356 
0357     return err;
0358 }
0359 
0360 static void intel_busy_work(struct work_struct *work)
0361 {
0362     struct intel_data *intel = container_of(work, struct intel_data,
0363                         busy_work);
0364     struct intel_device *idev;
0365 
0366     if (!intel->hu->tty->dev)
0367         return;
0368 
0369     /* Link is busy, delay the suspend */
0370     mutex_lock(&intel_device_list_lock);
0371     list_for_each_entry(idev, &intel_device_list, list) {
0372         if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
0373             pm_runtime_get(&idev->pdev->dev);
0374             pm_runtime_mark_last_busy(&idev->pdev->dev);
0375             pm_runtime_put_autosuspend(&idev->pdev->dev);
0376             break;
0377         }
0378     }
0379     mutex_unlock(&intel_device_list_lock);
0380 }
0381 
0382 static int intel_open(struct hci_uart *hu)
0383 {
0384     struct intel_data *intel;
0385 
0386     BT_DBG("hu %p", hu);
0387 
0388     if (!hci_uart_has_flow_control(hu))
0389         return -EOPNOTSUPP;
0390 
0391     intel = kzalloc(sizeof(*intel), GFP_KERNEL);
0392     if (!intel)
0393         return -ENOMEM;
0394 
0395     skb_queue_head_init(&intel->txq);
0396     INIT_WORK(&intel->busy_work, intel_busy_work);
0397 
0398     intel->hu = hu;
0399 
0400     hu->priv = intel;
0401 
0402     if (!intel_set_power(hu, true))
0403         set_bit(STATE_BOOTING, &intel->flags);
0404 
0405     return 0;
0406 }
0407 
0408 static int intel_close(struct hci_uart *hu)
0409 {
0410     struct intel_data *intel = hu->priv;
0411 
0412     BT_DBG("hu %p", hu);
0413 
0414     cancel_work_sync(&intel->busy_work);
0415 
0416     intel_set_power(hu, false);
0417 
0418     skb_queue_purge(&intel->txq);
0419     kfree_skb(intel->rx_skb);
0420     kfree(intel);
0421 
0422     hu->priv = NULL;
0423     return 0;
0424 }
0425 
0426 static int intel_flush(struct hci_uart *hu)
0427 {
0428     struct intel_data *intel = hu->priv;
0429 
0430     BT_DBG("hu %p", hu);
0431 
0432     skb_queue_purge(&intel->txq);
0433 
0434     return 0;
0435 }
0436 
0437 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
0438 {
0439     struct sk_buff *skb;
0440     struct hci_event_hdr *hdr;
0441     struct hci_ev_cmd_complete *evt;
0442 
0443     skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
0444     if (!skb)
0445         return -ENOMEM;
0446 
0447     hdr = skb_put(skb, sizeof(*hdr));
0448     hdr->evt = HCI_EV_CMD_COMPLETE;
0449     hdr->plen = sizeof(*evt) + 1;
0450 
0451     evt = skb_put(skb, sizeof(*evt));
0452     evt->ncmd = 0x01;
0453     evt->opcode = cpu_to_le16(opcode);
0454 
0455     skb_put_u8(skb, 0x00);
0456 
0457     hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
0458 
0459     return hci_recv_frame(hdev, skb);
0460 }
0461 
0462 static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
0463 {
0464     struct intel_data *intel = hu->priv;
0465     struct hci_dev *hdev = hu->hdev;
0466     u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
0467     struct sk_buff *skb;
0468     int err;
0469 
0470     /* This can be the first command sent to the chip, check
0471      * that the controller is ready.
0472      */
0473     err = intel_wait_booting(hu);
0474 
0475     clear_bit(STATE_BOOTING, &intel->flags);
0476 
0477     /* In case of timeout, try to continue anyway */
0478     if (err && err != -ETIMEDOUT)
0479         return err;
0480 
0481     bt_dev_info(hdev, "Change controller speed to %d", speed);
0482 
0483     speed_cmd[3] = intel_convert_speed(speed);
0484     if (speed_cmd[3] == 0xff) {
0485         bt_dev_err(hdev, "Unsupported speed");
0486         return -EINVAL;
0487     }
0488 
0489     /* Device will not accept speed change if Intel version has not been
0490      * previously requested.
0491      */
0492     skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
0493     if (IS_ERR(skb)) {
0494         bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
0495                PTR_ERR(skb));
0496         return PTR_ERR(skb);
0497     }
0498     kfree_skb(skb);
0499 
0500     skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
0501     if (!skb) {
0502         bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
0503         return -ENOMEM;
0504     }
0505 
0506     skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
0507     hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
0508 
0509     hci_uart_set_flow_control(hu, true);
0510 
0511     skb_queue_tail(&intel->txq, skb);
0512     hci_uart_tx_wakeup(hu);
0513 
0514     /* wait 100ms to change baudrate on controller side */
0515     msleep(100);
0516 
0517     hci_uart_set_baudrate(hu, speed);
0518     hci_uart_set_flow_control(hu, false);
0519 
0520     return 0;
0521 }
0522 
0523 static int intel_setup(struct hci_uart *hu)
0524 {
0525     struct intel_data *intel = hu->priv;
0526     struct hci_dev *hdev = hu->hdev;
0527     struct sk_buff *skb;
0528     struct intel_version ver;
0529     struct intel_boot_params params;
0530     struct intel_device *idev;
0531     const struct firmware *fw;
0532     char fwname[64];
0533     u32 boot_param;
0534     ktime_t calltime, delta, rettime;
0535     unsigned long long duration;
0536     unsigned int init_speed, oper_speed;
0537     int speed_change = 0;
0538     int err;
0539 
0540     bt_dev_dbg(hdev, "start intel_setup");
0541 
0542     hu->hdev->set_diag = btintel_set_diag;
0543     hu->hdev->set_bdaddr = btintel_set_bdaddr;
0544 
0545     /* Set the default boot parameter to 0x0 and it is updated to
0546      * SKU specific boot parameter after reading Intel_Write_Boot_Params
0547      * command while downloading the firmware.
0548      */
0549     boot_param = 0x00000000;
0550 
0551     calltime = ktime_get();
0552 
0553     if (hu->init_speed)
0554         init_speed = hu->init_speed;
0555     else
0556         init_speed = hu->proto->init_speed;
0557 
0558     if (hu->oper_speed)
0559         oper_speed = hu->oper_speed;
0560     else
0561         oper_speed = hu->proto->oper_speed;
0562 
0563     if (oper_speed && init_speed && oper_speed != init_speed)
0564         speed_change = 1;
0565 
0566     /* Check that the controller is ready */
0567     err = intel_wait_booting(hu);
0568 
0569     clear_bit(STATE_BOOTING, &intel->flags);
0570 
0571     /* In case of timeout, try to continue anyway */
0572     if (err && err != -ETIMEDOUT)
0573         return err;
0574 
0575     set_bit(STATE_BOOTLOADER, &intel->flags);
0576 
0577     /* Read the Intel version information to determine if the device
0578      * is in bootloader mode or if it already has operational firmware
0579      * loaded.
0580      */
0581     err = btintel_read_version(hdev, &ver);
0582     if (err)
0583         return err;
0584 
0585     /* The hardware platform number has a fixed value of 0x37 and
0586      * for now only accept this single value.
0587      */
0588     if (ver.hw_platform != 0x37) {
0589         bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
0590                ver.hw_platform);
0591         return -EINVAL;
0592     }
0593 
0594         /* Check for supported iBT hardware variants of this firmware
0595          * loading method.
0596          *
0597          * This check has been put in place to ensure correct forward
0598          * compatibility options when newer hardware variants come along.
0599          */
0600     switch (ver.hw_variant) {
0601     case 0x0b:  /* LnP */
0602     case 0x0c:  /* WsP */
0603     case 0x12:  /* ThP */
0604         break;
0605     default:
0606         bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
0607                ver.hw_variant);
0608         return -EINVAL;
0609     }
0610 
0611     btintel_version_info(hdev, &ver);
0612 
0613     /* The firmware variant determines if the device is in bootloader
0614      * mode or is running operational firmware. The value 0x06 identifies
0615      * the bootloader and the value 0x23 identifies the operational
0616      * firmware.
0617      *
0618      * When the operational firmware is already present, then only
0619      * the check for valid Bluetooth device address is needed. This
0620      * determines if the device will be added as configured or
0621      * unconfigured controller.
0622      *
0623      * It is not possible to use the Secure Boot Parameters in this
0624      * case since that command is only available in bootloader mode.
0625      */
0626     if (ver.fw_variant == 0x23) {
0627         clear_bit(STATE_BOOTLOADER, &intel->flags);
0628         btintel_check_bdaddr(hdev);
0629         return 0;
0630     }
0631 
0632     /* If the device is not in bootloader mode, then the only possible
0633      * choice is to return an error and abort the device initialization.
0634      */
0635     if (ver.fw_variant != 0x06) {
0636         bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
0637                ver.fw_variant);
0638         return -ENODEV;
0639     }
0640 
0641     /* Read the secure boot parameters to identify the operating
0642      * details of the bootloader.
0643      */
0644     err = btintel_read_boot_params(hdev, &params);
0645     if (err)
0646         return err;
0647 
0648     /* It is required that every single firmware fragment is acknowledged
0649      * with a command complete event. If the boot parameters indicate
0650      * that this bootloader does not send them, then abort the setup.
0651      */
0652     if (params.limited_cce != 0x00) {
0653         bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
0654                params.limited_cce);
0655         return -EINVAL;
0656     }
0657 
0658     /* If the OTP has no valid Bluetooth device address, then there will
0659      * also be no valid address for the operational firmware.
0660      */
0661     if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
0662         bt_dev_info(hdev, "No device address configured");
0663         set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
0664     }
0665 
0666     /* With this Intel bootloader only the hardware variant and device
0667      * revision information are used to select the right firmware for SfP
0668      * and WsP.
0669      *
0670      * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
0671      *
0672      * Currently the supported hardware variants are:
0673      *   11 (0x0b) for iBT 3.0 (LnP/SfP)
0674      *   12 (0x0c) for iBT 3.5 (WsP)
0675      *
0676      * For ThP/JfP and for future SKU's, the FW name varies based on HW
0677      * variant, HW revision and FW revision, as these are dependent on CNVi
0678      * and RF Combination.
0679      *
0680      *   18 (0x12) for iBT3.5 (ThP/JfP)
0681      *
0682      * The firmware file name for these will be
0683      * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
0684      *
0685      */
0686     switch (ver.hw_variant) {
0687     case 0x0b:      /* SfP */
0688     case 0x0c:      /* WsP */
0689         snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
0690              ver.hw_variant, le16_to_cpu(params.dev_revid));
0691         break;
0692     case 0x12:      /* ThP */
0693         snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
0694              ver.hw_variant, ver.hw_revision, ver.fw_revision);
0695         break;
0696     default:
0697         bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
0698                ver.hw_variant);
0699         return -EINVAL;
0700     }
0701 
0702     err = request_firmware(&fw, fwname, &hdev->dev);
0703     if (err < 0) {
0704         bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
0705                err);
0706         return err;
0707     }
0708 
0709     bt_dev_info(hdev, "Found device firmware: %s", fwname);
0710 
0711     /* Save the DDC file name for later */
0712     switch (ver.hw_variant) {
0713     case 0x0b:      /* SfP */
0714     case 0x0c:      /* WsP */
0715         snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
0716              ver.hw_variant, le16_to_cpu(params.dev_revid));
0717         break;
0718     case 0x12:      /* ThP */
0719         snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
0720              ver.hw_variant, ver.hw_revision, ver.fw_revision);
0721         break;
0722     default:
0723         bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
0724                ver.hw_variant);
0725         return -EINVAL;
0726     }
0727 
0728     if (fw->size < 644) {
0729         bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
0730                fw->size);
0731         err = -EBADF;
0732         goto done;
0733     }
0734 
0735     set_bit(STATE_DOWNLOADING, &intel->flags);
0736 
0737     /* Start firmware downloading and get boot parameter */
0738     err = btintel_download_firmware(hdev, &ver, fw, &boot_param);
0739     if (err < 0)
0740         goto done;
0741 
0742     set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
0743 
0744     bt_dev_info(hdev, "Waiting for firmware download to complete");
0745 
0746     /* Before switching the device into operational mode and with that
0747      * booting the loaded firmware, wait for the bootloader notification
0748      * that all fragments have been successfully received.
0749      *
0750      * When the event processing receives the notification, then the
0751      * STATE_DOWNLOADING flag will be cleared.
0752      *
0753      * The firmware loading should not take longer than 5 seconds
0754      * and thus just timeout if that happens and fail the setup
0755      * of this device.
0756      */
0757     err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
0758                   TASK_INTERRUPTIBLE,
0759                   msecs_to_jiffies(5000));
0760     if (err == -EINTR) {
0761         bt_dev_err(hdev, "Firmware loading interrupted");
0762         err = -EINTR;
0763         goto done;
0764     }
0765 
0766     if (err) {
0767         bt_dev_err(hdev, "Firmware loading timeout");
0768         err = -ETIMEDOUT;
0769         goto done;
0770     }
0771 
0772     if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
0773         bt_dev_err(hdev, "Firmware loading failed");
0774         err = -ENOEXEC;
0775         goto done;
0776     }
0777 
0778     rettime = ktime_get();
0779     delta = ktime_sub(rettime, calltime);
0780     duration = (unsigned long long) ktime_to_ns(delta) >> 10;
0781 
0782     bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
0783 
0784 done:
0785     release_firmware(fw);
0786 
0787     /* Check if there was an error and if is not -EALREADY which means the
0788      * firmware has already been loaded.
0789      */
0790     if (err < 0 && err != -EALREADY)
0791         return err;
0792 
0793     /* We need to restore the default speed before Intel reset */
0794     if (speed_change) {
0795         err = intel_set_baudrate(hu, init_speed);
0796         if (err)
0797             return err;
0798     }
0799 
0800     calltime = ktime_get();
0801 
0802     set_bit(STATE_BOOTING, &intel->flags);
0803 
0804     err = btintel_send_intel_reset(hdev, boot_param);
0805     if (err)
0806         return err;
0807 
0808     /* The bootloader will not indicate when the device is ready. This
0809      * is done by the operational firmware sending bootup notification.
0810      *
0811      * Booting into operational firmware should not take longer than
0812      * 1 second. However if that happens, then just fail the setup
0813      * since something went wrong.
0814      */
0815     bt_dev_info(hdev, "Waiting for device to boot");
0816 
0817     err = intel_wait_booting(hu);
0818     if (err)
0819         return err;
0820 
0821     clear_bit(STATE_BOOTING, &intel->flags);
0822 
0823     rettime = ktime_get();
0824     delta = ktime_sub(rettime, calltime);
0825     duration = (unsigned long long) ktime_to_ns(delta) >> 10;
0826 
0827     bt_dev_info(hdev, "Device booted in %llu usecs", duration);
0828 
0829     /* Enable LPM if matching pdev with wakeup enabled, set TX active
0830      * until further LPM TX notification.
0831      */
0832     mutex_lock(&intel_device_list_lock);
0833     list_for_each_entry(idev, &intel_device_list, list) {
0834         if (!hu->tty->dev)
0835             break;
0836         if (hu->tty->dev->parent == idev->pdev->dev.parent) {
0837             if (device_may_wakeup(&idev->pdev->dev)) {
0838                 set_bit(STATE_LPM_ENABLED, &intel->flags);
0839                 set_bit(STATE_TX_ACTIVE, &intel->flags);
0840             }
0841             break;
0842         }
0843     }
0844     mutex_unlock(&intel_device_list_lock);
0845 
0846     /* Ignore errors, device can work without DDC parameters */
0847     btintel_load_ddc_config(hdev, fwname);
0848 
0849     skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
0850     if (IS_ERR(skb))
0851         return PTR_ERR(skb);
0852     kfree_skb(skb);
0853 
0854     if (speed_change) {
0855         err = intel_set_baudrate(hu, oper_speed);
0856         if (err)
0857             return err;
0858     }
0859 
0860     bt_dev_info(hdev, "Setup complete");
0861 
0862     clear_bit(STATE_BOOTLOADER, &intel->flags);
0863 
0864     return 0;
0865 }
0866 
0867 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
0868 {
0869     struct hci_uart *hu = hci_get_drvdata(hdev);
0870     struct intel_data *intel = hu->priv;
0871     struct hci_event_hdr *hdr;
0872 
0873     if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
0874         !test_bit(STATE_BOOTING, &intel->flags))
0875         goto recv;
0876 
0877     hdr = (void *)skb->data;
0878 
0879     /* When the firmware loading completes the device sends
0880      * out a vendor specific event indicating the result of
0881      * the firmware loading.
0882      */
0883     if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
0884         skb->data[2] == 0x06) {
0885         if (skb->data[3] != 0x00)
0886             set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
0887 
0888         if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
0889             test_bit(STATE_FIRMWARE_LOADED, &intel->flags))
0890             wake_up_bit(&intel->flags, STATE_DOWNLOADING);
0891 
0892     /* When switching to the operational firmware the device
0893      * sends a vendor specific event indicating that the bootup
0894      * completed.
0895      */
0896     } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
0897            skb->data[2] == 0x02) {
0898         if (test_and_clear_bit(STATE_BOOTING, &intel->flags))
0899             wake_up_bit(&intel->flags, STATE_BOOTING);
0900     }
0901 recv:
0902     return hci_recv_frame(hdev, skb);
0903 }
0904 
0905 static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
0906 {
0907     struct hci_uart *hu = hci_get_drvdata(hdev);
0908     struct intel_data *intel = hu->priv;
0909 
0910     bt_dev_dbg(hdev, "TX idle notification (%d)", value);
0911 
0912     if (value) {
0913         set_bit(STATE_TX_ACTIVE, &intel->flags);
0914         schedule_work(&intel->busy_work);
0915     } else {
0916         clear_bit(STATE_TX_ACTIVE, &intel->flags);
0917     }
0918 }
0919 
0920 static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
0921 {
0922     struct hci_lpm_pkt *lpm = (void *)skb->data;
0923     struct hci_uart *hu = hci_get_drvdata(hdev);
0924     struct intel_data *intel = hu->priv;
0925 
0926     switch (lpm->opcode) {
0927     case LPM_OP_TX_NOTIFY:
0928         if (lpm->dlen < 1) {
0929             bt_dev_err(hu->hdev, "Invalid LPM notification packet");
0930             break;
0931         }
0932         intel_recv_lpm_notify(hdev, lpm->data[0]);
0933         break;
0934     case LPM_OP_SUSPEND_ACK:
0935         set_bit(STATE_SUSPENDED, &intel->flags);
0936         if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
0937             wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
0938         break;
0939     case LPM_OP_RESUME_ACK:
0940         clear_bit(STATE_SUSPENDED, &intel->flags);
0941         if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
0942             wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
0943         break;
0944     default:
0945         bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
0946         break;
0947     }
0948 
0949     kfree_skb(skb);
0950 
0951     return 0;
0952 }
0953 
0954 #define INTEL_RECV_LPM \
0955     .type = HCI_LPM_PKT, \
0956     .hlen = HCI_LPM_HDR_SIZE, \
0957     .loff = 1, \
0958     .lsize = 1, \
0959     .maxlen = HCI_LPM_MAX_SIZE
0960 
0961 static const struct h4_recv_pkt intel_recv_pkts[] = {
0962     { H4_RECV_ACL,    .recv = hci_recv_frame   },
0963     { H4_RECV_SCO,    .recv = hci_recv_frame   },
0964     { H4_RECV_EVENT,  .recv = intel_recv_event },
0965     { INTEL_RECV_LPM, .recv = intel_recv_lpm   },
0966 };
0967 
0968 static int intel_recv(struct hci_uart *hu, const void *data, int count)
0969 {
0970     struct intel_data *intel = hu->priv;
0971 
0972     if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
0973         return -EUNATCH;
0974 
0975     intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
0976                     intel_recv_pkts,
0977                     ARRAY_SIZE(intel_recv_pkts));
0978     if (IS_ERR(intel->rx_skb)) {
0979         int err = PTR_ERR(intel->rx_skb);
0980         bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
0981         intel->rx_skb = NULL;
0982         return err;
0983     }
0984 
0985     return count;
0986 }
0987 
0988 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
0989 {
0990     struct intel_data *intel = hu->priv;
0991     struct intel_device *idev;
0992 
0993     BT_DBG("hu %p skb %p", hu, skb);
0994 
0995     if (!hu->tty->dev)
0996         goto out_enqueue;
0997 
0998     /* Be sure our controller is resumed and potential LPM transaction
0999      * completed before enqueuing any packet.
1000      */
1001     mutex_lock(&intel_device_list_lock);
1002     list_for_each_entry(idev, &intel_device_list, list) {
1003         if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1004             pm_runtime_get_sync(&idev->pdev->dev);
1005             pm_runtime_mark_last_busy(&idev->pdev->dev);
1006             pm_runtime_put_autosuspend(&idev->pdev->dev);
1007             break;
1008         }
1009     }
1010     mutex_unlock(&intel_device_list_lock);
1011 out_enqueue:
1012     skb_queue_tail(&intel->txq, skb);
1013 
1014     return 0;
1015 }
1016 
1017 static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1018 {
1019     struct intel_data *intel = hu->priv;
1020     struct sk_buff *skb;
1021 
1022     skb = skb_dequeue(&intel->txq);
1023     if (!skb)
1024         return skb;
1025 
1026     if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1027         (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
1028         struct hci_command_hdr *cmd = (void *)skb->data;
1029         __u16 opcode = le16_to_cpu(cmd->opcode);
1030 
1031         /* When the 0xfc01 command is issued to boot into
1032          * the operational firmware, it will actually not
1033          * send a command complete event. To keep the flow
1034          * control working inject that event here.
1035          */
1036         if (opcode == 0xfc01)
1037             inject_cmd_complete(hu->hdev, opcode);
1038     }
1039 
1040     /* Prepend skb with frame type */
1041     memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
1042 
1043     return skb;
1044 }
1045 
1046 static const struct hci_uart_proto intel_proto = {
1047     .id     = HCI_UART_INTEL,
1048     .name       = "Intel",
1049     .manufacturer   = 2,
1050     .init_speed = 115200,
1051     .oper_speed = 3000000,
1052     .open       = intel_open,
1053     .close      = intel_close,
1054     .flush      = intel_flush,
1055     .setup      = intel_setup,
1056     .set_baudrate   = intel_set_baudrate,
1057     .recv       = intel_recv,
1058     .enqueue    = intel_enqueue,
1059     .dequeue    = intel_dequeue,
1060 };
1061 
1062 #ifdef CONFIG_ACPI
1063 static const struct acpi_device_id intel_acpi_match[] = {
1064     { "INT33E1", 0 },
1065     { "INT33E3", 0 },
1066     { }
1067 };
1068 MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1069 #endif
1070 
1071 #ifdef CONFIG_PM
1072 static int intel_suspend_device(struct device *dev)
1073 {
1074     struct intel_device *idev = dev_get_drvdata(dev);
1075 
1076     mutex_lock(&idev->hu_lock);
1077     if (idev->hu)
1078         intel_lpm_suspend(idev->hu);
1079     mutex_unlock(&idev->hu_lock);
1080 
1081     return 0;
1082 }
1083 
1084 static int intel_resume_device(struct device *dev)
1085 {
1086     struct intel_device *idev = dev_get_drvdata(dev);
1087 
1088     mutex_lock(&idev->hu_lock);
1089     if (idev->hu)
1090         intel_lpm_resume(idev->hu);
1091     mutex_unlock(&idev->hu_lock);
1092 
1093     return 0;
1094 }
1095 #endif
1096 
1097 #ifdef CONFIG_PM_SLEEP
1098 static int intel_suspend(struct device *dev)
1099 {
1100     struct intel_device *idev = dev_get_drvdata(dev);
1101 
1102     if (device_may_wakeup(dev))
1103         enable_irq_wake(idev->irq);
1104 
1105     return intel_suspend_device(dev);
1106 }
1107 
1108 static int intel_resume(struct device *dev)
1109 {
1110     struct intel_device *idev = dev_get_drvdata(dev);
1111 
1112     if (device_may_wakeup(dev))
1113         disable_irq_wake(idev->irq);
1114 
1115     return intel_resume_device(dev);
1116 }
1117 #endif
1118 
1119 static const struct dev_pm_ops intel_pm_ops = {
1120     SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1121     SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
1122 };
1123 
1124 static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
1125 static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
1126 
1127 static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
1128     { "reset-gpios", &reset_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
1129     { "host-wake-gpios", &host_wake_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
1130     { }
1131 };
1132 
1133 static int intel_probe(struct platform_device *pdev)
1134 {
1135     struct intel_device *idev;
1136     int ret;
1137 
1138     idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1139     if (!idev)
1140         return -ENOMEM;
1141 
1142     mutex_init(&idev->hu_lock);
1143 
1144     idev->pdev = pdev;
1145 
1146     ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
1147     if (ret)
1148         dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
1149 
1150     idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
1151     if (IS_ERR(idev->reset)) {
1152         dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1153         return PTR_ERR(idev->reset);
1154     }
1155 
1156     idev->irq = platform_get_irq(pdev, 0);
1157     if (idev->irq < 0) {
1158         struct gpio_desc *host_wake;
1159 
1160         dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1161 
1162         host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
1163         if (IS_ERR(host_wake)) {
1164             dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1165             goto no_irq;
1166         }
1167 
1168         idev->irq = gpiod_to_irq(host_wake);
1169         if (idev->irq < 0) {
1170             dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1171             goto no_irq;
1172         }
1173     }
1174 
1175     /* Only enable wake-up/irq when controller is powered */
1176     device_set_wakeup_capable(&pdev->dev, true);
1177     device_wakeup_disable(&pdev->dev);
1178 
1179 no_irq:
1180     platform_set_drvdata(pdev, idev);
1181 
1182     /* Place this instance on the device list */
1183     mutex_lock(&intel_device_list_lock);
1184     list_add_tail(&idev->list, &intel_device_list);
1185     mutex_unlock(&intel_device_list_lock);
1186 
1187     dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1188          desc_to_gpio(idev->reset), idev->irq);
1189 
1190     return 0;
1191 }
1192 
1193 static int intel_remove(struct platform_device *pdev)
1194 {
1195     struct intel_device *idev = platform_get_drvdata(pdev);
1196 
1197     device_wakeup_disable(&pdev->dev);
1198 
1199     mutex_lock(&intel_device_list_lock);
1200     list_del(&idev->list);
1201     mutex_unlock(&intel_device_list_lock);
1202 
1203     dev_info(&pdev->dev, "unregistered.\n");
1204 
1205     return 0;
1206 }
1207 
1208 static struct platform_driver intel_driver = {
1209     .probe = intel_probe,
1210     .remove = intel_remove,
1211     .driver = {
1212         .name = "hci_intel",
1213         .acpi_match_table = ACPI_PTR(intel_acpi_match),
1214         .pm = &intel_pm_ops,
1215     },
1216 };
1217 
1218 int __init intel_init(void)
1219 {
1220     int err;
1221 
1222     err = platform_driver_register(&intel_driver);
1223     if (err)
1224         return err;
1225 
1226     return hci_uart_register_proto(&intel_proto);
1227 }
1228 
1229 int __exit intel_deinit(void)
1230 {
1231     platform_driver_unregister(&intel_driver);
1232 
1233     return hci_uart_unregister_proto(&intel_proto);
1234 }