11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
216e3887fSMarcel Holtmann /*
316e3887fSMarcel Holtmann *
416e3887fSMarcel Holtmann * Bluetooth HCI UART driver for Intel devices
516e3887fSMarcel Holtmann *
616e3887fSMarcel Holtmann * Copyright (C) 2015 Intel Corporation
716e3887fSMarcel Holtmann */
816e3887fSMarcel Holtmann
916e3887fSMarcel Holtmann #include <linux/kernel.h>
1016e3887fSMarcel Holtmann #include <linux/errno.h>
1116e3887fSMarcel Holtmann #include <linux/skbuff.h>
12ca93cee5SLoic Poulain #include <linux/firmware.h>
131ab1f239SLoic Poulain #include <linux/module.h>
14ca93cee5SLoic Poulain #include <linux/wait.h>
151ab1f239SLoic Poulain #include <linux/tty.h>
161ab1f239SLoic Poulain #include <linux/platform_device.h>
171ab1f239SLoic Poulain #include <linux/gpio/consumer.h>
181ab1f239SLoic Poulain #include <linux/acpi.h>
19765ea3abSLoic Poulain #include <linux/interrupt.h>
2074cdad37SLoic Poulain #include <linux/pm_runtime.h>
2116e3887fSMarcel Holtmann
2216e3887fSMarcel Holtmann #include <net/bluetooth/bluetooth.h>
2316e3887fSMarcel Holtmann #include <net/bluetooth/hci_core.h>
2416e3887fSMarcel Holtmann
2516e3887fSMarcel Holtmann #include "hci_uart.h"
26ca93cee5SLoic Poulain #include "btintel.h"
27ca93cee5SLoic Poulain
28ca93cee5SLoic Poulain #define STATE_BOOTLOADER 0
29ca93cee5SLoic Poulain #define STATE_DOWNLOADING 1
30ca93cee5SLoic Poulain #define STATE_FIRMWARE_LOADED 2
31ca93cee5SLoic Poulain #define STATE_FIRMWARE_FAILED 3
32ca93cee5SLoic Poulain #define STATE_BOOTING 4
33b98469f4SLoic Poulain #define STATE_LPM_ENABLED 5
34b98469f4SLoic Poulain #define STATE_TX_ACTIVE 6
3589436546SLoic Poulain #define STATE_SUSPENDED 7
3689436546SLoic Poulain #define STATE_LPM_TRANSACTION 8
37b98469f4SLoic Poulain
3889436546SLoic Poulain #define HCI_LPM_WAKE_PKT 0xf0
39b98469f4SLoic Poulain #define HCI_LPM_PKT 0xf1
40b98469f4SLoic Poulain #define HCI_LPM_MAX_SIZE 10
41b98469f4SLoic Poulain #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
42b98469f4SLoic Poulain
43b98469f4SLoic Poulain #define LPM_OP_TX_NOTIFY 0x00
4489436546SLoic Poulain #define LPM_OP_SUSPEND_ACK 0x02
4589436546SLoic Poulain #define LPM_OP_RESUME_ACK 0x03
46b98469f4SLoic Poulain
4774cdad37SLoic Poulain #define LPM_SUSPEND_DELAY_MS 1000
4874cdad37SLoic Poulain
49b98469f4SLoic Poulain struct hci_lpm_pkt {
50b98469f4SLoic Poulain __u8 opcode;
51b98469f4SLoic Poulain __u8 dlen;
527c369483SGustavo A. R. Silva __u8 data[];
53b98469f4SLoic Poulain } __packed;
54ca93cee5SLoic Poulain
551ab1f239SLoic Poulain struct intel_device {
561ab1f239SLoic Poulain struct list_head list;
571ab1f239SLoic Poulain struct platform_device *pdev;
581ab1f239SLoic Poulain struct gpio_desc *reset;
59aa6802dfSLoic Poulain struct hci_uart *hu;
60aa6802dfSLoic Poulain struct mutex hu_lock;
61765ea3abSLoic Poulain int irq;
621ab1f239SLoic Poulain };
631ab1f239SLoic Poulain
641ab1f239SLoic Poulain static LIST_HEAD(intel_device_list);
6567c8bde0SLoic Poulain static DEFINE_MUTEX(intel_device_list_lock);
661ab1f239SLoic Poulain
67ca93cee5SLoic Poulain struct intel_data {
68ca93cee5SLoic Poulain struct sk_buff *rx_skb;
69ca93cee5SLoic Poulain struct sk_buff_head txq;
7074cdad37SLoic Poulain struct work_struct busy_work;
7174cdad37SLoic Poulain struct hci_uart *hu;
72ca93cee5SLoic Poulain unsigned long flags;
73ca93cee5SLoic Poulain };
74ca93cee5SLoic Poulain
intel_convert_speed(unsigned int speed)75ff289559SLoic Poulain static u8 intel_convert_speed(unsigned int speed)
76ff289559SLoic Poulain {
77ff289559SLoic Poulain switch (speed) {
78ff289559SLoic Poulain case 9600:
79ff289559SLoic Poulain return 0x00;
80ff289559SLoic Poulain case 19200:
81ff289559SLoic Poulain return 0x01;
82ff289559SLoic Poulain case 38400:
83ff289559SLoic Poulain return 0x02;
84ff289559SLoic Poulain case 57600:
85ff289559SLoic Poulain return 0x03;
86ff289559SLoic Poulain case 115200:
87ff289559SLoic Poulain return 0x04;
88ff289559SLoic Poulain case 230400:
89ff289559SLoic Poulain return 0x05;
90ff289559SLoic Poulain case 460800:
91ff289559SLoic Poulain return 0x06;
92ff289559SLoic Poulain case 921600:
93ff289559SLoic Poulain return 0x07;
94ff289559SLoic Poulain case 1843200:
95ff289559SLoic Poulain return 0x08;
96ff289559SLoic Poulain case 3250000:
97ff289559SLoic Poulain return 0x09;
98ff289559SLoic Poulain case 2000000:
99ff289559SLoic Poulain return 0x0a;
100ff289559SLoic Poulain case 3000000:
101ff289559SLoic Poulain return 0x0b;
102ff289559SLoic Poulain default:
103ff289559SLoic Poulain return 0xff;
104ff289559SLoic Poulain }
105ff289559SLoic Poulain }
106ff289559SLoic Poulain
intel_wait_booting(struct hci_uart * hu)1071ab1f239SLoic Poulain static int intel_wait_booting(struct hci_uart *hu)
1081ab1f239SLoic Poulain {
1091ab1f239SLoic Poulain struct intel_data *intel = hu->priv;
1101ab1f239SLoic Poulain int err;
1111ab1f239SLoic Poulain
1121ab1f239SLoic Poulain err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
1131ab1f239SLoic Poulain TASK_INTERRUPTIBLE,
1141ab1f239SLoic Poulain msecs_to_jiffies(1000));
1151ab1f239SLoic Poulain
116f0a70a04SBart Van Assche if (err == -EINTR) {
117f44e78a5SLoic Poulain bt_dev_err(hu->hdev, "Device boot interrupted");
1181ab1f239SLoic Poulain return -EINTR;
1191ab1f239SLoic Poulain }
1201ab1f239SLoic Poulain
1211ab1f239SLoic Poulain if (err) {
122f44e78a5SLoic Poulain bt_dev_err(hu->hdev, "Device boot timeout");
1231ab1f239SLoic Poulain return -ETIMEDOUT;
1241ab1f239SLoic Poulain }
1251ab1f239SLoic Poulain
1261ab1f239SLoic Poulain return err;
1271ab1f239SLoic Poulain }
1281ab1f239SLoic Poulain
129a9cb0fe4SLoic Poulain #ifdef CONFIG_PM
intel_wait_lpm_transaction(struct hci_uart * hu)13089436546SLoic Poulain static int intel_wait_lpm_transaction(struct hci_uart *hu)
13189436546SLoic Poulain {
13289436546SLoic Poulain struct intel_data *intel = hu->priv;
13389436546SLoic Poulain int err;
13489436546SLoic Poulain
13589436546SLoic Poulain err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
13689436546SLoic Poulain TASK_INTERRUPTIBLE,
13789436546SLoic Poulain msecs_to_jiffies(1000));
13889436546SLoic Poulain
139f0a70a04SBart Van Assche if (err == -EINTR) {
14089436546SLoic Poulain bt_dev_err(hu->hdev, "LPM transaction interrupted");
14189436546SLoic Poulain return -EINTR;
14289436546SLoic Poulain }
14389436546SLoic Poulain
14489436546SLoic Poulain if (err) {
14589436546SLoic Poulain bt_dev_err(hu->hdev, "LPM transaction timeout");
14689436546SLoic Poulain return -ETIMEDOUT;
14789436546SLoic Poulain }
14889436546SLoic Poulain
14989436546SLoic Poulain return err;
15089436546SLoic Poulain }
15189436546SLoic Poulain
intel_lpm_suspend(struct hci_uart * hu)15289436546SLoic Poulain static int intel_lpm_suspend(struct hci_uart *hu)
15389436546SLoic Poulain {
15489436546SLoic Poulain static const u8 suspend[] = { 0x01, 0x01, 0x01 };
15589436546SLoic Poulain struct intel_data *intel = hu->priv;
15689436546SLoic Poulain struct sk_buff *skb;
15789436546SLoic Poulain
15889436546SLoic Poulain if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
15989436546SLoic Poulain test_bit(STATE_SUSPENDED, &intel->flags))
16089436546SLoic Poulain return 0;
16189436546SLoic Poulain
16289436546SLoic Poulain if (test_bit(STATE_TX_ACTIVE, &intel->flags))
16389436546SLoic Poulain return -EAGAIN;
16489436546SLoic Poulain
16589436546SLoic Poulain bt_dev_dbg(hu->hdev, "Suspending");
16689436546SLoic Poulain
16789436546SLoic Poulain skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
16889436546SLoic Poulain if (!skb) {
16989436546SLoic Poulain bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
17089436546SLoic Poulain return -ENOMEM;
17189436546SLoic Poulain }
17289436546SLoic Poulain
17359ae1d12SJohannes Berg skb_put_data(skb, suspend, sizeof(suspend));
174618e8bc2SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_LPM_PKT;
17589436546SLoic Poulain
17689436546SLoic Poulain set_bit(STATE_LPM_TRANSACTION, &intel->flags);
17789436546SLoic Poulain
17830e945fbSLoic Poulain /* LPM flow is a priority, enqueue packet at list head */
17930e945fbSLoic Poulain skb_queue_head(&intel->txq, skb);
18089436546SLoic Poulain hci_uart_tx_wakeup(hu);
18189436546SLoic Poulain
18289436546SLoic Poulain intel_wait_lpm_transaction(hu);
18389436546SLoic Poulain /* Even in case of failure, continue and test the suspended flag */
18489436546SLoic Poulain
18589436546SLoic Poulain clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
18689436546SLoic Poulain
18789436546SLoic Poulain if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
18889436546SLoic Poulain bt_dev_err(hu->hdev, "Device suspend error");
18989436546SLoic Poulain return -EINVAL;
19089436546SLoic Poulain }
19189436546SLoic Poulain
19289436546SLoic Poulain bt_dev_dbg(hu->hdev, "Suspended");
19389436546SLoic Poulain
19489436546SLoic Poulain hci_uart_set_flow_control(hu, true);
19589436546SLoic Poulain
19689436546SLoic Poulain return 0;
19789436546SLoic Poulain }
19889436546SLoic Poulain
intel_lpm_resume(struct hci_uart * hu)19989436546SLoic Poulain static int intel_lpm_resume(struct hci_uart *hu)
20089436546SLoic Poulain {
20189436546SLoic Poulain struct intel_data *intel = hu->priv;
20289436546SLoic Poulain struct sk_buff *skb;
20389436546SLoic Poulain
20489436546SLoic Poulain if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
20589436546SLoic Poulain !test_bit(STATE_SUSPENDED, &intel->flags))
20689436546SLoic Poulain return 0;
20789436546SLoic Poulain
20889436546SLoic Poulain bt_dev_dbg(hu->hdev, "Resuming");
20989436546SLoic Poulain
21089436546SLoic Poulain hci_uart_set_flow_control(hu, false);
21189436546SLoic Poulain
21289436546SLoic Poulain skb = bt_skb_alloc(0, GFP_KERNEL);
21389436546SLoic Poulain if (!skb) {
21489436546SLoic Poulain bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
21589436546SLoic Poulain return -ENOMEM;
21689436546SLoic Poulain }
21789436546SLoic Poulain
218618e8bc2SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
21989436546SLoic Poulain
22089436546SLoic Poulain set_bit(STATE_LPM_TRANSACTION, &intel->flags);
22189436546SLoic Poulain
22230e945fbSLoic Poulain /* LPM flow is a priority, enqueue packet at list head */
22330e945fbSLoic Poulain skb_queue_head(&intel->txq, skb);
22489436546SLoic Poulain hci_uart_tx_wakeup(hu);
22589436546SLoic Poulain
22689436546SLoic Poulain intel_wait_lpm_transaction(hu);
22789436546SLoic Poulain /* Even in case of failure, continue and test the suspended flag */
22889436546SLoic Poulain
22989436546SLoic Poulain clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
23089436546SLoic Poulain
23189436546SLoic Poulain if (test_bit(STATE_SUSPENDED, &intel->flags)) {
23289436546SLoic Poulain bt_dev_err(hu->hdev, "Device resume error");
23389436546SLoic Poulain return -EINVAL;
23489436546SLoic Poulain }
23589436546SLoic Poulain
23689436546SLoic Poulain bt_dev_dbg(hu->hdev, "Resumed");
23789436546SLoic Poulain
23889436546SLoic Poulain return 0;
23989436546SLoic Poulain }
240a9cb0fe4SLoic Poulain #endif /* CONFIG_PM */
24189436546SLoic Poulain
intel_lpm_host_wake(struct hci_uart * hu)24289436546SLoic Poulain static int intel_lpm_host_wake(struct hci_uart *hu)
24389436546SLoic Poulain {
24489436546SLoic Poulain static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
24589436546SLoic Poulain struct intel_data *intel = hu->priv;
24689436546SLoic Poulain struct sk_buff *skb;
24789436546SLoic Poulain
24889436546SLoic Poulain hci_uart_set_flow_control(hu, false);
24989436546SLoic Poulain
25089436546SLoic Poulain clear_bit(STATE_SUSPENDED, &intel->flags);
25189436546SLoic Poulain
25289436546SLoic Poulain skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
25389436546SLoic Poulain if (!skb) {
25489436546SLoic Poulain bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
25589436546SLoic Poulain return -ENOMEM;
25689436546SLoic Poulain }
25789436546SLoic Poulain
25859ae1d12SJohannes Berg skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
259618e8bc2SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_LPM_PKT;
26089436546SLoic Poulain
26130e945fbSLoic Poulain /* LPM flow is a priority, enqueue packet at list head */
26230e945fbSLoic Poulain skb_queue_head(&intel->txq, skb);
26389436546SLoic Poulain hci_uart_tx_wakeup(hu);
26489436546SLoic Poulain
26589436546SLoic Poulain bt_dev_dbg(hu->hdev, "Resumed by controller");
26689436546SLoic Poulain
26789436546SLoic Poulain return 0;
26889436546SLoic Poulain }
26989436546SLoic Poulain
intel_irq(int irq,void * dev_id)270765ea3abSLoic Poulain static irqreturn_t intel_irq(int irq, void *dev_id)
271765ea3abSLoic Poulain {
272765ea3abSLoic Poulain struct intel_device *idev = dev_id;
273765ea3abSLoic Poulain
274765ea3abSLoic Poulain dev_info(&idev->pdev->dev, "hci_intel irq\n");
275765ea3abSLoic Poulain
276aa6802dfSLoic Poulain mutex_lock(&idev->hu_lock);
277aa6802dfSLoic Poulain if (idev->hu)
278aa6802dfSLoic Poulain intel_lpm_host_wake(idev->hu);
279aa6802dfSLoic Poulain mutex_unlock(&idev->hu_lock);
280aa6802dfSLoic Poulain
28174cdad37SLoic Poulain /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
28274cdad37SLoic Poulain pm_runtime_get(&idev->pdev->dev);
28374cdad37SLoic Poulain pm_runtime_mark_last_busy(&idev->pdev->dev);
28474cdad37SLoic Poulain pm_runtime_put_autosuspend(&idev->pdev->dev);
28574cdad37SLoic Poulain
286765ea3abSLoic Poulain return IRQ_HANDLED;
287765ea3abSLoic Poulain }
288765ea3abSLoic Poulain
intel_set_power(struct hci_uart * hu,bool powered)2891ab1f239SLoic Poulain static int intel_set_power(struct hci_uart *hu, bool powered)
2901ab1f239SLoic Poulain {
2917dbbd692SAndy Shevchenko struct intel_device *idev;
2921ab1f239SLoic Poulain int err = -ENODEV;
2931ab1f239SLoic Poulain
294dcb9cfaaSJohan Hovold if (!hu->tty->dev)
295dcb9cfaaSJohan Hovold return err;
296dcb9cfaaSJohan Hovold
29767c8bde0SLoic Poulain mutex_lock(&intel_device_list_lock);
2981ab1f239SLoic Poulain
2997dbbd692SAndy Shevchenko list_for_each_entry(idev, &intel_device_list, list) {
3001ab1f239SLoic Poulain /* tty device and pdev device should share the same parent
3011ab1f239SLoic Poulain * which is the UART port.
3021ab1f239SLoic Poulain */
3031ab1f239SLoic Poulain if (hu->tty->dev->parent != idev->pdev->dev.parent)
3041ab1f239SLoic Poulain continue;
3051ab1f239SLoic Poulain
3061ab1f239SLoic Poulain if (!idev->reset) {
3071ab1f239SLoic Poulain err = -ENOTSUPP;
3081ab1f239SLoic Poulain break;
3091ab1f239SLoic Poulain }
3101ab1f239SLoic Poulain
3111ab1f239SLoic Poulain BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
3121ab1f239SLoic Poulain hu, dev_name(&idev->pdev->dev), powered);
3131ab1f239SLoic Poulain
3141ab1f239SLoic Poulain gpiod_set_value(idev->reset, powered);
315765ea3abSLoic Poulain
316aa6802dfSLoic Poulain /* Provide to idev a hu reference which is used to run LPM
317aa6802dfSLoic Poulain * transactions (lpm suspend/resume) from PM callbacks.
318aa6802dfSLoic Poulain * hu needs to be protected against concurrent removing during
319aa6802dfSLoic Poulain * these PM ops.
320aa6802dfSLoic Poulain */
321aa6802dfSLoic Poulain mutex_lock(&idev->hu_lock);
322aa6802dfSLoic Poulain idev->hu = powered ? hu : NULL;
323aa6802dfSLoic Poulain mutex_unlock(&idev->hu_lock);
324aa6802dfSLoic Poulain
325765ea3abSLoic Poulain if (idev->irq < 0)
326765ea3abSLoic Poulain break;
327765ea3abSLoic Poulain
328765ea3abSLoic Poulain if (powered && device_can_wakeup(&idev->pdev->dev)) {
329765ea3abSLoic Poulain err = devm_request_threaded_irq(&idev->pdev->dev,
330765ea3abSLoic Poulain idev->irq, NULL,
331765ea3abSLoic Poulain intel_irq,
332765ea3abSLoic Poulain IRQF_ONESHOT,
333765ea3abSLoic Poulain "bt-host-wake", idev);
334765ea3abSLoic Poulain if (err) {
335765ea3abSLoic Poulain BT_ERR("hu %p, unable to allocate irq-%d",
336765ea3abSLoic Poulain hu, idev->irq);
337765ea3abSLoic Poulain break;
338765ea3abSLoic Poulain }
339765ea3abSLoic Poulain
340765ea3abSLoic Poulain device_wakeup_enable(&idev->pdev->dev);
34174cdad37SLoic Poulain
34274cdad37SLoic Poulain pm_runtime_set_active(&idev->pdev->dev);
34374cdad37SLoic Poulain pm_runtime_use_autosuspend(&idev->pdev->dev);
34474cdad37SLoic Poulain pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
34574cdad37SLoic Poulain LPM_SUSPEND_DELAY_MS);
34674cdad37SLoic Poulain pm_runtime_enable(&idev->pdev->dev);
347765ea3abSLoic Poulain } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
348765ea3abSLoic Poulain devm_free_irq(&idev->pdev->dev, idev->irq, idev);
349765ea3abSLoic Poulain device_wakeup_disable(&idev->pdev->dev);
35074cdad37SLoic Poulain
35174cdad37SLoic Poulain pm_runtime_disable(&idev->pdev->dev);
352765ea3abSLoic Poulain }
3531ab1f239SLoic Poulain }
3541ab1f239SLoic Poulain
35567c8bde0SLoic Poulain mutex_unlock(&intel_device_list_lock);
3561ab1f239SLoic Poulain
3571ab1f239SLoic Poulain return err;
3581ab1f239SLoic Poulain }
3591ab1f239SLoic Poulain
intel_busy_work(struct work_struct * work)36074cdad37SLoic Poulain static void intel_busy_work(struct work_struct *work)
36174cdad37SLoic Poulain {
36274cdad37SLoic Poulain struct intel_data *intel = container_of(work, struct intel_data,
36374cdad37SLoic Poulain busy_work);
3647dbbd692SAndy Shevchenko struct intel_device *idev;
36574cdad37SLoic Poulain
366dcb9cfaaSJohan Hovold if (!intel->hu->tty->dev)
367dcb9cfaaSJohan Hovold return;
368dcb9cfaaSJohan Hovold
36974cdad37SLoic Poulain /* Link is busy, delay the suspend */
37074cdad37SLoic Poulain mutex_lock(&intel_device_list_lock);
3717dbbd692SAndy Shevchenko list_for_each_entry(idev, &intel_device_list, list) {
37274cdad37SLoic Poulain if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
37374cdad37SLoic Poulain pm_runtime_get(&idev->pdev->dev);
37474cdad37SLoic Poulain pm_runtime_mark_last_busy(&idev->pdev->dev);
37574cdad37SLoic Poulain pm_runtime_put_autosuspend(&idev->pdev->dev);
37674cdad37SLoic Poulain break;
37774cdad37SLoic Poulain }
37874cdad37SLoic Poulain }
37974cdad37SLoic Poulain mutex_unlock(&intel_device_list_lock);
38074cdad37SLoic Poulain }
38174cdad37SLoic Poulain
intel_open(struct hci_uart * hu)382ca93cee5SLoic Poulain static int intel_open(struct hci_uart *hu)
383ca93cee5SLoic Poulain {
384ca93cee5SLoic Poulain struct intel_data *intel;
385ca93cee5SLoic Poulain
386ca93cee5SLoic Poulain BT_DBG("hu %p", hu);
387ca93cee5SLoic Poulain
388b36a1552SVladis Dronov if (!hci_uart_has_flow_control(hu))
389b36a1552SVladis Dronov return -EOPNOTSUPP;
390b36a1552SVladis Dronov
391ca93cee5SLoic Poulain intel = kzalloc(sizeof(*intel), GFP_KERNEL);
392ca93cee5SLoic Poulain if (!intel)
393ca93cee5SLoic Poulain return -ENOMEM;
394ca93cee5SLoic Poulain
395ca93cee5SLoic Poulain skb_queue_head_init(&intel->txq);
39674cdad37SLoic Poulain INIT_WORK(&intel->busy_work, intel_busy_work);
39774cdad37SLoic Poulain
39874cdad37SLoic Poulain intel->hu = hu;
399ca93cee5SLoic Poulain
400ca93cee5SLoic Poulain hu->priv = intel;
4011ab1f239SLoic Poulain
4021ab1f239SLoic Poulain if (!intel_set_power(hu, true))
4031ab1f239SLoic Poulain set_bit(STATE_BOOTING, &intel->flags);
4041ab1f239SLoic Poulain
405ca93cee5SLoic Poulain return 0;
406ca93cee5SLoic Poulain }
407ca93cee5SLoic Poulain
intel_close(struct hci_uart * hu)408ca93cee5SLoic Poulain static int intel_close(struct hci_uart *hu)
409ca93cee5SLoic Poulain {
410ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
411ca93cee5SLoic Poulain
412ca93cee5SLoic Poulain BT_DBG("hu %p", hu);
413ca93cee5SLoic Poulain
41474cdad37SLoic Poulain cancel_work_sync(&intel->busy_work);
41574cdad37SLoic Poulain
4161ab1f239SLoic Poulain intel_set_power(hu, false);
4171ab1f239SLoic Poulain
418ca93cee5SLoic Poulain skb_queue_purge(&intel->txq);
419ca93cee5SLoic Poulain kfree_skb(intel->rx_skb);
420ca93cee5SLoic Poulain kfree(intel);
421ca93cee5SLoic Poulain
422ca93cee5SLoic Poulain hu->priv = NULL;
423ca93cee5SLoic Poulain return 0;
424ca93cee5SLoic Poulain }
425ca93cee5SLoic Poulain
intel_flush(struct hci_uart * hu)426ca93cee5SLoic Poulain static int intel_flush(struct hci_uart *hu)
427ca93cee5SLoic Poulain {
428ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
429ca93cee5SLoic Poulain
430ca93cee5SLoic Poulain BT_DBG("hu %p", hu);
431ca93cee5SLoic Poulain
432ca93cee5SLoic Poulain skb_queue_purge(&intel->txq);
433ca93cee5SLoic Poulain
434ca93cee5SLoic Poulain return 0;
435ca93cee5SLoic Poulain }
436ca93cee5SLoic Poulain
inject_cmd_complete(struct hci_dev * hdev,__u16 opcode)437ca93cee5SLoic Poulain static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
438ca93cee5SLoic Poulain {
439ca93cee5SLoic Poulain struct sk_buff *skb;
440ca93cee5SLoic Poulain struct hci_event_hdr *hdr;
441ca93cee5SLoic Poulain struct hci_ev_cmd_complete *evt;
442ca93cee5SLoic Poulain
443f6ebfc24SJia-Ju Bai skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
444ca93cee5SLoic Poulain if (!skb)
445ca93cee5SLoic Poulain return -ENOMEM;
446ca93cee5SLoic Poulain
4474df864c1SJohannes Berg hdr = skb_put(skb, sizeof(*hdr));
448ca93cee5SLoic Poulain hdr->evt = HCI_EV_CMD_COMPLETE;
449ca93cee5SLoic Poulain hdr->plen = sizeof(*evt) + 1;
450ca93cee5SLoic Poulain
4514df864c1SJohannes Berg evt = skb_put(skb, sizeof(*evt));
452ca93cee5SLoic Poulain evt->ncmd = 0x01;
453ca93cee5SLoic Poulain evt->opcode = cpu_to_le16(opcode);
454ca93cee5SLoic Poulain
455634fef61SJohannes Berg skb_put_u8(skb, 0x00);
456ca93cee5SLoic Poulain
457618e8bc2SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
458ca93cee5SLoic Poulain
459ca93cee5SLoic Poulain return hci_recv_frame(hdev, skb);
460ca93cee5SLoic Poulain }
461ca93cee5SLoic Poulain
intel_set_baudrate(struct hci_uart * hu,unsigned int speed)462ff289559SLoic Poulain static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
463ff289559SLoic Poulain {
464ff289559SLoic Poulain struct intel_data *intel = hu->priv;
465ff289559SLoic Poulain struct hci_dev *hdev = hu->hdev;
466ff289559SLoic Poulain u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
467ff289559SLoic Poulain struct sk_buff *skb;
4681ab1f239SLoic Poulain int err;
4691ab1f239SLoic Poulain
4701ab1f239SLoic Poulain /* This can be the first command sent to the chip, check
4711ab1f239SLoic Poulain * that the controller is ready.
4721ab1f239SLoic Poulain */
4731ab1f239SLoic Poulain err = intel_wait_booting(hu);
4741ab1f239SLoic Poulain
4751ab1f239SLoic Poulain clear_bit(STATE_BOOTING, &intel->flags);
4761ab1f239SLoic Poulain
4771ab1f239SLoic Poulain /* In case of timeout, try to continue anyway */
4782be1149eSAnton Protopopov if (err && err != -ETIMEDOUT)
4791ab1f239SLoic Poulain return err;
480ff289559SLoic Poulain
481f44e78a5SLoic Poulain bt_dev_info(hdev, "Change controller speed to %d", speed);
482ff289559SLoic Poulain
483ff289559SLoic Poulain speed_cmd[3] = intel_convert_speed(speed);
484ff289559SLoic Poulain if (speed_cmd[3] == 0xff) {
485f44e78a5SLoic Poulain bt_dev_err(hdev, "Unsupported speed");
486ff289559SLoic Poulain return -EINVAL;
487ff289559SLoic Poulain }
488ff289559SLoic Poulain
489ff289559SLoic Poulain /* Device will not accept speed change if Intel version has not been
490ff289559SLoic Poulain * previously requested.
491ff289559SLoic Poulain */
492a0c38245SLoic Poulain skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
493ff289559SLoic Poulain if (IS_ERR(skb)) {
494f44e78a5SLoic Poulain bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
495f44e78a5SLoic Poulain PTR_ERR(skb));
496ff289559SLoic Poulain return PTR_ERR(skb);
497ff289559SLoic Poulain }
498ff289559SLoic Poulain kfree_skb(skb);
499ff289559SLoic Poulain
500ff289559SLoic Poulain skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
501ff289559SLoic Poulain if (!skb) {
502f44e78a5SLoic Poulain bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
503ff289559SLoic Poulain return -ENOMEM;
504ff289559SLoic Poulain }
505ff289559SLoic Poulain
50659ae1d12SJohannes Berg skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
507618e8bc2SMarcel Holtmann hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
508ff289559SLoic Poulain
509ff289559SLoic Poulain hci_uart_set_flow_control(hu, true);
510ff289559SLoic Poulain
511ff289559SLoic Poulain skb_queue_tail(&intel->txq, skb);
512ff289559SLoic Poulain hci_uart_tx_wakeup(hu);
513ff289559SLoic Poulain
514ff289559SLoic Poulain /* wait 100ms to change baudrate on controller side */
515ff289559SLoic Poulain msleep(100);
516ff289559SLoic Poulain
517ff289559SLoic Poulain hci_uart_set_baudrate(hu, speed);
518ff289559SLoic Poulain hci_uart_set_flow_control(hu, false);
519ff289559SLoic Poulain
520ff289559SLoic Poulain return 0;
521ff289559SLoic Poulain }
522ff289559SLoic Poulain
intel_setup(struct hci_uart * hu)523ca93cee5SLoic Poulain static int intel_setup(struct hci_uart *hu)
524ca93cee5SLoic Poulain {
525ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
526ca93cee5SLoic Poulain struct hci_dev *hdev = hu->hdev;
527ca93cee5SLoic Poulain struct sk_buff *skb;
5286c483de1SLoic Poulain struct intel_version ver;
529faf174d2STedd Ho-Jeong An struct intel_boot_params params;
5307dbbd692SAndy Shevchenko struct intel_device *idev;
531ca93cee5SLoic Poulain const struct firmware *fw;
532ca93cee5SLoic Poulain char fwname[64];
533e5889af6STedd Ho-Jeong An u32 boot_param;
534ca93cee5SLoic Poulain ktime_t calltime, delta, rettime;
535ca93cee5SLoic Poulain unsigned long long duration;
536ff289559SLoic Poulain unsigned int init_speed, oper_speed;
537ff289559SLoic Poulain int speed_change = 0;
538ca93cee5SLoic Poulain int err;
539ca93cee5SLoic Poulain
540*62f7de37SUri Arev bt_dev_dbg(hdev, "");
541ca93cee5SLoic Poulain
5426d2e50d2SMarcel Holtmann hu->hdev->set_diag = btintel_set_diag;
54335ab8150SMarcel Holtmann hu->hdev->set_bdaddr = btintel_set_bdaddr;
54435ab8150SMarcel Holtmann
54504d729b8STedd Ho-Jeong An /* Set the default boot parameter to 0x0 and it is updated to
54604d729b8STedd Ho-Jeong An * SKU specific boot parameter after reading Intel_Write_Boot_Params
54704d729b8STedd Ho-Jeong An * command while downloading the firmware.
54804d729b8STedd Ho-Jeong An */
54904d729b8STedd Ho-Jeong An boot_param = 0x00000000;
550e5889af6STedd Ho-Jeong An
551ca93cee5SLoic Poulain calltime = ktime_get();
552ca93cee5SLoic Poulain
553ff289559SLoic Poulain if (hu->init_speed)
554ff289559SLoic Poulain init_speed = hu->init_speed;
555ff289559SLoic Poulain else
556ff289559SLoic Poulain init_speed = hu->proto->init_speed;
557ff289559SLoic Poulain
558ff289559SLoic Poulain if (hu->oper_speed)
559ff289559SLoic Poulain oper_speed = hu->oper_speed;
560ff289559SLoic Poulain else
561ff289559SLoic Poulain oper_speed = hu->proto->oper_speed;
562ff289559SLoic Poulain
563ff289559SLoic Poulain if (oper_speed && init_speed && oper_speed != init_speed)
564ff289559SLoic Poulain speed_change = 1;
565ff289559SLoic Poulain
5661ab1f239SLoic Poulain /* Check that the controller is ready */
5671ab1f239SLoic Poulain err = intel_wait_booting(hu);
5681ab1f239SLoic Poulain
5691ab1f239SLoic Poulain clear_bit(STATE_BOOTING, &intel->flags);
5701ab1f239SLoic Poulain
5711ab1f239SLoic Poulain /* In case of timeout, try to continue anyway */
5722be1149eSAnton Protopopov if (err && err != -ETIMEDOUT)
5731ab1f239SLoic Poulain return err;
5741ab1f239SLoic Poulain
575ca93cee5SLoic Poulain set_bit(STATE_BOOTLOADER, &intel->flags);
576ca93cee5SLoic Poulain
577ca93cee5SLoic Poulain /* Read the Intel version information to determine if the device
578ca93cee5SLoic Poulain * is in bootloader mode or if it already has operational firmware
579ca93cee5SLoic Poulain * loaded.
580ca93cee5SLoic Poulain */
5816c483de1SLoic Poulain err = btintel_read_version(hdev, &ver);
5826c483de1SLoic Poulain if (err)
583ca93cee5SLoic Poulain return err;
584ca93cee5SLoic Poulain
585ca93cee5SLoic Poulain /* The hardware platform number has a fixed value of 0x37 and
586ca93cee5SLoic Poulain * for now only accept this single value.
587ca93cee5SLoic Poulain */
5886c483de1SLoic Poulain if (ver.hw_platform != 0x37) {
589f44e78a5SLoic Poulain bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
5906c483de1SLoic Poulain ver.hw_platform);
591ca93cee5SLoic Poulain return -EINVAL;
592ca93cee5SLoic Poulain }
593ca93cee5SLoic Poulain
5949268834bSTedd Ho-Jeong An /* Check for supported iBT hardware variants of this firmware
5959268834bSTedd Ho-Jeong An * loading method.
5969268834bSTedd Ho-Jeong An *
5979268834bSTedd Ho-Jeong An * This check has been put in place to ensure correct forward
5989268834bSTedd Ho-Jeong An * compatibility options when newer hardware variants come along.
599ca93cee5SLoic Poulain */
6009268834bSTedd Ho-Jeong An switch (ver.hw_variant) {
6019268834bSTedd Ho-Jeong An case 0x0b: /* LnP */
6029268834bSTedd Ho-Jeong An case 0x0c: /* WsP */
6036c7bb7ebSTedd Ho-Jeong An case 0x12: /* ThP */
6049268834bSTedd Ho-Jeong An break;
6059268834bSTedd Ho-Jeong An default:
606f44e78a5SLoic Poulain bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
6076c483de1SLoic Poulain ver.hw_variant);
608ca93cee5SLoic Poulain return -EINVAL;
609ca93cee5SLoic Poulain }
610ca93cee5SLoic Poulain
6116c483de1SLoic Poulain btintel_version_info(hdev, &ver);
612ca93cee5SLoic Poulain
613ca93cee5SLoic Poulain /* The firmware variant determines if the device is in bootloader
614ca93cee5SLoic Poulain * mode or is running operational firmware. The value 0x06 identifies
615ca93cee5SLoic Poulain * the bootloader and the value 0x23 identifies the operational
616ca93cee5SLoic Poulain * firmware.
617ca93cee5SLoic Poulain *
618ca93cee5SLoic Poulain * When the operational firmware is already present, then only
619ca93cee5SLoic Poulain * the check for valid Bluetooth device address is needed. This
620ca93cee5SLoic Poulain * determines if the device will be added as configured or
621ca93cee5SLoic Poulain * unconfigured controller.
622ca93cee5SLoic Poulain *
623ca93cee5SLoic Poulain * It is not possible to use the Secure Boot Parameters in this
624ca93cee5SLoic Poulain * case since that command is only available in bootloader mode.
625ca93cee5SLoic Poulain */
6266c483de1SLoic Poulain if (ver.fw_variant == 0x23) {
627ca93cee5SLoic Poulain clear_bit(STATE_BOOTLOADER, &intel->flags);
628ca93cee5SLoic Poulain btintel_check_bdaddr(hdev);
629ca93cee5SLoic Poulain return 0;
630ca93cee5SLoic Poulain }
631ca93cee5SLoic Poulain
632ca93cee5SLoic Poulain /* If the device is not in bootloader mode, then the only possible
633ca93cee5SLoic Poulain * choice is to return an error and abort the device initialization.
634ca93cee5SLoic Poulain */
6356c483de1SLoic Poulain if (ver.fw_variant != 0x06) {
636f44e78a5SLoic Poulain bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
6376c483de1SLoic Poulain ver.fw_variant);
638ca93cee5SLoic Poulain return -ENODEV;
639ca93cee5SLoic Poulain }
640ca93cee5SLoic Poulain
641ca93cee5SLoic Poulain /* Read the secure boot parameters to identify the operating
642ca93cee5SLoic Poulain * details of the bootloader.
643ca93cee5SLoic Poulain */
644faf174d2STedd Ho-Jeong An err = btintel_read_boot_params(hdev, ¶ms);
645faf174d2STedd Ho-Jeong An if (err)
646ca93cee5SLoic Poulain return err;
647ca93cee5SLoic Poulain
648ca93cee5SLoic Poulain /* It is required that every single firmware fragment is acknowledged
649ca93cee5SLoic Poulain * with a command complete event. If the boot parameters indicate
650ca93cee5SLoic Poulain * that this bootloader does not send them, then abort the setup.
651ca93cee5SLoic Poulain */
652faf174d2STedd Ho-Jeong An if (params.limited_cce != 0x00) {
653f44e78a5SLoic Poulain bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
654faf174d2STedd Ho-Jeong An params.limited_cce);
655ca93cee5SLoic Poulain return -EINVAL;
656ca93cee5SLoic Poulain }
657ca93cee5SLoic Poulain
658ca93cee5SLoic Poulain /* If the OTP has no valid Bluetooth device address, then there will
659ca93cee5SLoic Poulain * also be no valid address for the operational firmware.
660ca93cee5SLoic Poulain */
661faf174d2STedd Ho-Jeong An if (!bacmp(¶ms.otp_bdaddr, BDADDR_ANY)) {
662f44e78a5SLoic Poulain bt_dev_info(hdev, "No device address configured");
663ca93cee5SLoic Poulain set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
664ca93cee5SLoic Poulain }
665ca93cee5SLoic Poulain
666ca93cee5SLoic Poulain /* With this Intel bootloader only the hardware variant and device
667965651c1STedd Ho-Jeong An * revision information are used to select the right firmware for SfP
668965651c1STedd Ho-Jeong An * and WsP.
669ca93cee5SLoic Poulain *
670b7da6a69STedd Ho-Jeong An * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
671b7da6a69STedd Ho-Jeong An *
672b7da6a69STedd Ho-Jeong An * Currently the supported hardware variants are:
673b7da6a69STedd Ho-Jeong An * 11 (0x0b) for iBT 3.0 (LnP/SfP)
674965651c1STedd Ho-Jeong An * 12 (0x0c) for iBT 3.5 (WsP)
675965651c1STedd Ho-Jeong An *
676965651c1STedd Ho-Jeong An * For ThP/JfP and for future SKU's, the FW name varies based on HW
677965651c1STedd Ho-Jeong An * variant, HW revision and FW revision, as these are dependent on CNVi
678965651c1STedd Ho-Jeong An * and RF Combination.
679965651c1STedd Ho-Jeong An *
680965651c1STedd Ho-Jeong An * 18 (0x12) for iBT3.5 (ThP/JfP)
681965651c1STedd Ho-Jeong An *
682965651c1STedd Ho-Jeong An * The firmware file name for these will be
683965651c1STedd Ho-Jeong An * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
684965651c1STedd Ho-Jeong An *
685ca93cee5SLoic Poulain */
686965651c1STedd Ho-Jeong An switch (ver.hw_variant) {
687965651c1STedd Ho-Jeong An case 0x0b: /* SfP */
688965651c1STedd Ho-Jeong An case 0x0c: /* WsP */
689b7da6a69STedd Ho-Jeong An snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
6900cc62cafSAndy Shevchenko ver.hw_variant, le16_to_cpu(params.dev_revid));
691965651c1STedd Ho-Jeong An break;
692965651c1STedd Ho-Jeong An case 0x12: /* ThP */
693965651c1STedd Ho-Jeong An snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
6940cc62cafSAndy Shevchenko ver.hw_variant, ver.hw_revision, ver.fw_revision);
695965651c1STedd Ho-Jeong An break;
696965651c1STedd Ho-Jeong An default:
697965651c1STedd Ho-Jeong An bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
698965651c1STedd Ho-Jeong An ver.hw_variant);
699965651c1STedd Ho-Jeong An return -EINVAL;
700965651c1STedd Ho-Jeong An }
701ca93cee5SLoic Poulain
702ca93cee5SLoic Poulain err = request_firmware(&fw, fwname, &hdev->dev);
703ca93cee5SLoic Poulain if (err < 0) {
704f44e78a5SLoic Poulain bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
705f44e78a5SLoic Poulain err);
706ca93cee5SLoic Poulain return err;
707ca93cee5SLoic Poulain }
708ca93cee5SLoic Poulain
709f44e78a5SLoic Poulain bt_dev_info(hdev, "Found device firmware: %s", fwname);
710ca93cee5SLoic Poulain
7111cfbabddSLoic Poulain /* Save the DDC file name for later */
712965651c1STedd Ho-Jeong An switch (ver.hw_variant) {
713965651c1STedd Ho-Jeong An case 0x0b: /* SfP */
714965651c1STedd Ho-Jeong An case 0x0c: /* WsP */
715b7da6a69STedd Ho-Jeong An snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
7160cc62cafSAndy Shevchenko ver.hw_variant, le16_to_cpu(params.dev_revid));
717965651c1STedd Ho-Jeong An break;
718965651c1STedd Ho-Jeong An case 0x12: /* ThP */
719965651c1STedd Ho-Jeong An snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
7200cc62cafSAndy Shevchenko ver.hw_variant, ver.hw_revision, ver.fw_revision);
721965651c1STedd Ho-Jeong An break;
722965651c1STedd Ho-Jeong An default:
723965651c1STedd Ho-Jeong An bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
724965651c1STedd Ho-Jeong An ver.hw_variant);
725965651c1STedd Ho-Jeong An return -EINVAL;
726965651c1STedd Ho-Jeong An }
7271cfbabddSLoic Poulain
728ca93cee5SLoic Poulain if (fw->size < 644) {
729f44e78a5SLoic Poulain bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
730f44e78a5SLoic Poulain fw->size);
731ca93cee5SLoic Poulain err = -EBADF;
732ca93cee5SLoic Poulain goto done;
733ca93cee5SLoic Poulain }
734ca93cee5SLoic Poulain
735ca93cee5SLoic Poulain set_bit(STATE_DOWNLOADING, &intel->flags);
736ca93cee5SLoic Poulain
737fbbe83c5STedd Ho-Jeong An /* Start firmware downloading and get boot parameter */
738ac056546SLuiz Augusto von Dentz err = btintel_download_firmware(hdev, &ver, fw, &boot_param);
739fbbe83c5STedd Ho-Jeong An if (err < 0)
740ca93cee5SLoic Poulain goto done;
741ca93cee5SLoic Poulain
742ca93cee5SLoic Poulain set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
743ca93cee5SLoic Poulain
744f44e78a5SLoic Poulain bt_dev_info(hdev, "Waiting for firmware download to complete");
745ca93cee5SLoic Poulain
746ca93cee5SLoic Poulain /* Before switching the device into operational mode and with that
747ca93cee5SLoic Poulain * booting the loaded firmware, wait for the bootloader notification
748ca93cee5SLoic Poulain * that all fragments have been successfully received.
749ca93cee5SLoic Poulain *
750ca93cee5SLoic Poulain * When the event processing receives the notification, then the
751ca93cee5SLoic Poulain * STATE_DOWNLOADING flag will be cleared.
752ca93cee5SLoic Poulain *
753ca93cee5SLoic Poulain * The firmware loading should not take longer than 5 seconds
754ca93cee5SLoic Poulain * and thus just timeout if that happens and fail the setup
755ca93cee5SLoic Poulain * of this device.
756ca93cee5SLoic Poulain */
757ca93cee5SLoic Poulain err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
758ca93cee5SLoic Poulain TASK_INTERRUPTIBLE,
759ca93cee5SLoic Poulain msecs_to_jiffies(5000));
760f0a70a04SBart Van Assche if (err == -EINTR) {
761f44e78a5SLoic Poulain bt_dev_err(hdev, "Firmware loading interrupted");
762ca93cee5SLoic Poulain err = -EINTR;
763ca93cee5SLoic Poulain goto done;
764ca93cee5SLoic Poulain }
765ca93cee5SLoic Poulain
766ca93cee5SLoic Poulain if (err) {
767f44e78a5SLoic Poulain bt_dev_err(hdev, "Firmware loading timeout");
768ca93cee5SLoic Poulain err = -ETIMEDOUT;
769ca93cee5SLoic Poulain goto done;
770ca93cee5SLoic Poulain }
771ca93cee5SLoic Poulain
772ca93cee5SLoic Poulain if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
773f44e78a5SLoic Poulain bt_dev_err(hdev, "Firmware loading failed");
774ca93cee5SLoic Poulain err = -ENOEXEC;
775ca93cee5SLoic Poulain goto done;
776ca93cee5SLoic Poulain }
777ca93cee5SLoic Poulain
778ca93cee5SLoic Poulain rettime = ktime_get();
779ca93cee5SLoic Poulain delta = ktime_sub(rettime, calltime);
780ca93cee5SLoic Poulain duration = (unsigned long long)ktime_to_ns(delta) >> 10;
781ca93cee5SLoic Poulain
782f44e78a5SLoic Poulain bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
783ca93cee5SLoic Poulain
784ca93cee5SLoic Poulain done:
785ca93cee5SLoic Poulain release_firmware(fw);
786ca93cee5SLoic Poulain
787ac056546SLuiz Augusto von Dentz /* Check if there was an error and if is not -EALREADY which means the
788ac056546SLuiz Augusto von Dentz * firmware has already been loaded.
789ac056546SLuiz Augusto von Dentz */
790ac056546SLuiz Augusto von Dentz if (err < 0 && err != -EALREADY)
791ca93cee5SLoic Poulain return err;
792ca93cee5SLoic Poulain
793ff289559SLoic Poulain /* We need to restore the default speed before Intel reset */
794ff289559SLoic Poulain if (speed_change) {
795ff289559SLoic Poulain err = intel_set_baudrate(hu, init_speed);
796ff289559SLoic Poulain if (err)
797ff289559SLoic Poulain return err;
798ff289559SLoic Poulain }
799ff289559SLoic Poulain
800ca93cee5SLoic Poulain calltime = ktime_get();
801ca93cee5SLoic Poulain
802ca93cee5SLoic Poulain set_bit(STATE_BOOTING, &intel->flags);
803ca93cee5SLoic Poulain
804e5889af6STedd Ho-Jeong An err = btintel_send_intel_reset(hdev, boot_param);
805e5889af6STedd Ho-Jeong An if (err)
806e5889af6STedd Ho-Jeong An return err;
807ca93cee5SLoic Poulain
808ca93cee5SLoic Poulain /* The bootloader will not indicate when the device is ready. This
809ca93cee5SLoic Poulain * is done by the operational firmware sending bootup notification.
810ca93cee5SLoic Poulain *
811ca93cee5SLoic Poulain * Booting into operational firmware should not take longer than
812ca93cee5SLoic Poulain * 1 second. However if that happens, then just fail the setup
813ca93cee5SLoic Poulain * since something went wrong.
814ca93cee5SLoic Poulain */
815f44e78a5SLoic Poulain bt_dev_info(hdev, "Waiting for device to boot");
816ca93cee5SLoic Poulain
8171ab1f239SLoic Poulain err = intel_wait_booting(hu);
8181ab1f239SLoic Poulain if (err)
8191ab1f239SLoic Poulain return err;
820ca93cee5SLoic Poulain
8211ab1f239SLoic Poulain clear_bit(STATE_BOOTING, &intel->flags);
822ca93cee5SLoic Poulain
823ca93cee5SLoic Poulain rettime = ktime_get();
824ca93cee5SLoic Poulain delta = ktime_sub(rettime, calltime);
825ca93cee5SLoic Poulain duration = (unsigned long long)ktime_to_ns(delta) >> 10;
826ca93cee5SLoic Poulain
827f44e78a5SLoic Poulain bt_dev_info(hdev, "Device booted in %llu usecs", duration);
828ca93cee5SLoic Poulain
82931eff267SLoic Poulain /* Enable LPM if matching pdev with wakeup enabled, set TX active
83031eff267SLoic Poulain * until further LPM TX notification.
83131eff267SLoic Poulain */
83267c8bde0SLoic Poulain mutex_lock(&intel_device_list_lock);
8337dbbd692SAndy Shevchenko list_for_each_entry(idev, &intel_device_list, list) {
834dcb9cfaaSJohan Hovold if (!hu->tty->dev)
835dcb9cfaaSJohan Hovold break;
8367dbbd692SAndy Shevchenko if (hu->tty->dev->parent == idev->pdev->dev.parent) {
8377dbbd692SAndy Shevchenko if (device_may_wakeup(&idev->pdev->dev)) {
83831eff267SLoic Poulain set_bit(STATE_LPM_ENABLED, &intel->flags);
83931eff267SLoic Poulain set_bit(STATE_TX_ACTIVE, &intel->flags);
84031eff267SLoic Poulain }
841b98469f4SLoic Poulain break;
842b98469f4SLoic Poulain }
843b98469f4SLoic Poulain }
84467c8bde0SLoic Poulain mutex_unlock(&intel_device_list_lock);
845b98469f4SLoic Poulain
8461cfbabddSLoic Poulain /* Ignore errors, device can work without DDC parameters */
8471cfbabddSLoic Poulain btintel_load_ddc_config(hdev, fwname);
8481cfbabddSLoic Poulain
849ff289559SLoic Poulain skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
850ff289559SLoic Poulain if (IS_ERR(skb))
851ff289559SLoic Poulain return PTR_ERR(skb);
852ff289559SLoic Poulain kfree_skb(skb);
853ff289559SLoic Poulain
854ff289559SLoic Poulain if (speed_change) {
855ff289559SLoic Poulain err = intel_set_baudrate(hu, oper_speed);
856ff289559SLoic Poulain if (err)
857ff289559SLoic Poulain return err;
858ff289559SLoic Poulain }
859ff289559SLoic Poulain
860f44e78a5SLoic Poulain bt_dev_info(hdev, "Setup complete");
861ff289559SLoic Poulain
862ca93cee5SLoic Poulain clear_bit(STATE_BOOTLOADER, &intel->flags);
863ca93cee5SLoic Poulain
864ca93cee5SLoic Poulain return 0;
865ca93cee5SLoic Poulain }
866ca93cee5SLoic Poulain
intel_recv_event(struct hci_dev * hdev,struct sk_buff * skb)867ca93cee5SLoic Poulain static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
868ca93cee5SLoic Poulain {
869ca93cee5SLoic Poulain struct hci_uart *hu = hci_get_drvdata(hdev);
870ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
871ca93cee5SLoic Poulain struct hci_event_hdr *hdr;
872ca93cee5SLoic Poulain
8731ab1f239SLoic Poulain if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
8741ab1f239SLoic Poulain !test_bit(STATE_BOOTING, &intel->flags))
875ca93cee5SLoic Poulain goto recv;
876ca93cee5SLoic Poulain
877ca93cee5SLoic Poulain hdr = (void *)skb->data;
878ca93cee5SLoic Poulain
879ca93cee5SLoic Poulain /* When the firmware loading completes the device sends
880ca93cee5SLoic Poulain * out a vendor specific event indicating the result of
881ca93cee5SLoic Poulain * the firmware loading.
882ca93cee5SLoic Poulain */
883ca93cee5SLoic Poulain if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
884ca93cee5SLoic Poulain skb->data[2] == 0x06) {
885ca93cee5SLoic Poulain if (skb->data[3] != 0x00)
886ca93cee5SLoic Poulain set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
887ca93cee5SLoic Poulain
888ca93cee5SLoic Poulain if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
889dff6d593SAndrea Parri test_bit(STATE_FIRMWARE_LOADED, &intel->flags))
890ca93cee5SLoic Poulain wake_up_bit(&intel->flags, STATE_DOWNLOADING);
891ca93cee5SLoic Poulain
892ca93cee5SLoic Poulain /* When switching to the operational firmware the device
893ca93cee5SLoic Poulain * sends a vendor specific event indicating that the bootup
894ca93cee5SLoic Poulain * completed.
895ca93cee5SLoic Poulain */
896ca93cee5SLoic Poulain } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
897ca93cee5SLoic Poulain skb->data[2] == 0x02) {
898dff6d593SAndrea Parri if (test_and_clear_bit(STATE_BOOTING, &intel->flags))
899ca93cee5SLoic Poulain wake_up_bit(&intel->flags, STATE_BOOTING);
900ca93cee5SLoic Poulain }
901ca93cee5SLoic Poulain recv:
902ca93cee5SLoic Poulain return hci_recv_frame(hdev, skb);
903ca93cee5SLoic Poulain }
904ca93cee5SLoic Poulain
intel_recv_lpm_notify(struct hci_dev * hdev,int value)905b98469f4SLoic Poulain static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
906b98469f4SLoic Poulain {
907b98469f4SLoic Poulain struct hci_uart *hu = hci_get_drvdata(hdev);
908b98469f4SLoic Poulain struct intel_data *intel = hu->priv;
909b98469f4SLoic Poulain
910f44e78a5SLoic Poulain bt_dev_dbg(hdev, "TX idle notification (%d)", value);
911b98469f4SLoic Poulain
91274cdad37SLoic Poulain if (value) {
913b98469f4SLoic Poulain set_bit(STATE_TX_ACTIVE, &intel->flags);
91474cdad37SLoic Poulain schedule_work(&intel->busy_work);
91574cdad37SLoic Poulain } else {
916b98469f4SLoic Poulain clear_bit(STATE_TX_ACTIVE, &intel->flags);
917b98469f4SLoic Poulain }
91874cdad37SLoic Poulain }
919b98469f4SLoic Poulain
intel_recv_lpm(struct hci_dev * hdev,struct sk_buff * skb)920b98469f4SLoic Poulain static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
921b98469f4SLoic Poulain {
922b98469f4SLoic Poulain struct hci_lpm_pkt *lpm = (void *)skb->data;
92389436546SLoic Poulain struct hci_uart *hu = hci_get_drvdata(hdev);
92489436546SLoic Poulain struct intel_data *intel = hu->priv;
925b98469f4SLoic Poulain
926b98469f4SLoic Poulain switch (lpm->opcode) {
927b98469f4SLoic Poulain case LPM_OP_TX_NOTIFY:
9281b197574SLoic Poulain if (lpm->dlen < 1) {
9291b197574SLoic Poulain bt_dev_err(hu->hdev, "Invalid LPM notification packet");
9301b197574SLoic Poulain break;
9311b197574SLoic Poulain }
932b98469f4SLoic Poulain intel_recv_lpm_notify(hdev, lpm->data[0]);
933b98469f4SLoic Poulain break;
93489436546SLoic Poulain case LPM_OP_SUSPEND_ACK:
93589436546SLoic Poulain set_bit(STATE_SUSPENDED, &intel->flags);
936dff6d593SAndrea Parri if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
93789436546SLoic Poulain wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
93889436546SLoic Poulain break;
93989436546SLoic Poulain case LPM_OP_RESUME_ACK:
94089436546SLoic Poulain clear_bit(STATE_SUSPENDED, &intel->flags);
941dff6d593SAndrea Parri if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
94289436546SLoic Poulain wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
94389436546SLoic Poulain break;
944b98469f4SLoic Poulain default:
945f44e78a5SLoic Poulain bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
946b98469f4SLoic Poulain break;
947b98469f4SLoic Poulain }
948b98469f4SLoic Poulain
949b98469f4SLoic Poulain kfree_skb(skb);
950b98469f4SLoic Poulain
951b98469f4SLoic Poulain return 0;
952b98469f4SLoic Poulain }
953b98469f4SLoic Poulain
954b98469f4SLoic Poulain #define INTEL_RECV_LPM \
955b98469f4SLoic Poulain .type = HCI_LPM_PKT, \
956b98469f4SLoic Poulain .hlen = HCI_LPM_HDR_SIZE, \
957b98469f4SLoic Poulain .loff = 1, \
958b98469f4SLoic Poulain .lsize = 1, \
959b98469f4SLoic Poulain .maxlen = HCI_LPM_MAX_SIZE
960b98469f4SLoic Poulain
961ca93cee5SLoic Poulain static const struct h4_recv_pkt intel_recv_pkts[] = {
962ca93cee5SLoic Poulain { H4_RECV_ACL, .recv = hci_recv_frame },
963ca93cee5SLoic Poulain { H4_RECV_SCO, .recv = hci_recv_frame },
964ca93cee5SLoic Poulain { H4_RECV_EVENT, .recv = intel_recv_event },
965b98469f4SLoic Poulain { INTEL_RECV_LPM, .recv = intel_recv_lpm },
966ca93cee5SLoic Poulain };
967ca93cee5SLoic Poulain
intel_recv(struct hci_uart * hu,const void * data,int count)968ca93cee5SLoic Poulain static int intel_recv(struct hci_uart *hu, const void *data, int count)
969ca93cee5SLoic Poulain {
970ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
971ca93cee5SLoic Poulain
972ca93cee5SLoic Poulain if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
973ca93cee5SLoic Poulain return -EUNATCH;
974ca93cee5SLoic Poulain
975ca93cee5SLoic Poulain intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
976ca93cee5SLoic Poulain intel_recv_pkts,
977ca93cee5SLoic Poulain ARRAY_SIZE(intel_recv_pkts));
978ca93cee5SLoic Poulain if (IS_ERR(intel->rx_skb)) {
979ca93cee5SLoic Poulain int err = PTR_ERR(intel->rx_skb);
980*62f7de37SUri Arev
981f44e78a5SLoic Poulain bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
982ca93cee5SLoic Poulain intel->rx_skb = NULL;
983ca93cee5SLoic Poulain return err;
984ca93cee5SLoic Poulain }
985ca93cee5SLoic Poulain
986ca93cee5SLoic Poulain return count;
987ca93cee5SLoic Poulain }
988ca93cee5SLoic Poulain
intel_enqueue(struct hci_uart * hu,struct sk_buff * skb)989ca93cee5SLoic Poulain static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
990ca93cee5SLoic Poulain {
991ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
9927dbbd692SAndy Shevchenko struct intel_device *idev;
993ca93cee5SLoic Poulain
994ca93cee5SLoic Poulain BT_DBG("hu %p skb %p", hu, skb);
995ca93cee5SLoic Poulain
996dcb9cfaaSJohan Hovold if (!hu->tty->dev)
997dcb9cfaaSJohan Hovold goto out_enqueue;
998dcb9cfaaSJohan Hovold
99974cdad37SLoic Poulain /* Be sure our controller is resumed and potential LPM transaction
100074cdad37SLoic Poulain * completed before enqueuing any packet.
100174cdad37SLoic Poulain */
100274cdad37SLoic Poulain mutex_lock(&intel_device_list_lock);
10037dbbd692SAndy Shevchenko list_for_each_entry(idev, &intel_device_list, list) {
100474cdad37SLoic Poulain if (hu->tty->dev->parent == idev->pdev->dev.parent) {
100574cdad37SLoic Poulain pm_runtime_get_sync(&idev->pdev->dev);
100674cdad37SLoic Poulain pm_runtime_mark_last_busy(&idev->pdev->dev);
100774cdad37SLoic Poulain pm_runtime_put_autosuspend(&idev->pdev->dev);
100874cdad37SLoic Poulain break;
100974cdad37SLoic Poulain }
101074cdad37SLoic Poulain }
101174cdad37SLoic Poulain mutex_unlock(&intel_device_list_lock);
1012dcb9cfaaSJohan Hovold out_enqueue:
1013ca93cee5SLoic Poulain skb_queue_tail(&intel->txq, skb);
1014ca93cee5SLoic Poulain
1015ca93cee5SLoic Poulain return 0;
1016ca93cee5SLoic Poulain }
1017ca93cee5SLoic Poulain
intel_dequeue(struct hci_uart * hu)1018ca93cee5SLoic Poulain static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1019ca93cee5SLoic Poulain {
1020ca93cee5SLoic Poulain struct intel_data *intel = hu->priv;
1021ca93cee5SLoic Poulain struct sk_buff *skb;
1022ca93cee5SLoic Poulain
1023ca93cee5SLoic Poulain skb = skb_dequeue(&intel->txq);
1024ca93cee5SLoic Poulain if (!skb)
1025ca93cee5SLoic Poulain return skb;
1026ca93cee5SLoic Poulain
1027ca93cee5SLoic Poulain if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1028618e8bc2SMarcel Holtmann (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
1029ca93cee5SLoic Poulain struct hci_command_hdr *cmd = (void *)skb->data;
1030ca93cee5SLoic Poulain __u16 opcode = le16_to_cpu(cmd->opcode);
1031ca93cee5SLoic Poulain
1032ca93cee5SLoic Poulain /* When the 0xfc01 command is issued to boot into
1033ca93cee5SLoic Poulain * the operational firmware, it will actually not
1034ca93cee5SLoic Poulain * send a command complete event. To keep the flow
1035ca93cee5SLoic Poulain * control working inject that event here.
1036ca93cee5SLoic Poulain */
1037ca93cee5SLoic Poulain if (opcode == 0xfc01)
1038ca93cee5SLoic Poulain inject_cmd_complete(hu->hdev, opcode);
1039ca93cee5SLoic Poulain }
1040ca93cee5SLoic Poulain
1041ca93cee5SLoic Poulain /* Prepend skb with frame type */
1042618e8bc2SMarcel Holtmann memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
1043ca93cee5SLoic Poulain
1044ca93cee5SLoic Poulain return skb;
1045ca93cee5SLoic Poulain }
1046ca93cee5SLoic Poulain
1047ca93cee5SLoic Poulain static const struct hci_uart_proto intel_proto = {
1048ca93cee5SLoic Poulain .id = HCI_UART_INTEL,
1049ca93cee5SLoic Poulain .name = "Intel",
1050aee61f7aSMarcel Holtmann .manufacturer = 2,
1051ca93cee5SLoic Poulain .init_speed = 115200,
1052ff289559SLoic Poulain .oper_speed = 3000000,
1053ca93cee5SLoic Poulain .open = intel_open,
1054ca93cee5SLoic Poulain .close = intel_close,
1055ca93cee5SLoic Poulain .flush = intel_flush,
1056ca93cee5SLoic Poulain .setup = intel_setup,
1057ff289559SLoic Poulain .set_baudrate = intel_set_baudrate,
1058ca93cee5SLoic Poulain .recv = intel_recv,
1059ca93cee5SLoic Poulain .enqueue = intel_enqueue,
1060ca93cee5SLoic Poulain .dequeue = intel_dequeue,
1061ca93cee5SLoic Poulain };
1062ca93cee5SLoic Poulain
10631ab1f239SLoic Poulain #ifdef CONFIG_ACPI
10641ab1f239SLoic Poulain static const struct acpi_device_id intel_acpi_match[] = {
10651ab1f239SLoic Poulain { "INT33E1", 0 },
1066c6db0143SAndy Shevchenko { "INT33E3", 0 },
1067c6db0143SAndy Shevchenko { }
10681ab1f239SLoic Poulain };
10691ab1f239SLoic Poulain MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
10701ab1f239SLoic Poulain #endif
10711ab1f239SLoic Poulain
107274cdad37SLoic Poulain #ifdef CONFIG_PM
intel_suspend_device(struct device * dev)1073f7552473SLoic Poulain static int intel_suspend_device(struct device *dev)
1074aa6802dfSLoic Poulain {
1075aa6802dfSLoic Poulain struct intel_device *idev = dev_get_drvdata(dev);
1076aa6802dfSLoic Poulain
1077aa6802dfSLoic Poulain mutex_lock(&idev->hu_lock);
1078aa6802dfSLoic Poulain if (idev->hu)
1079aa6802dfSLoic Poulain intel_lpm_suspend(idev->hu);
1080aa6802dfSLoic Poulain mutex_unlock(&idev->hu_lock);
1081aa6802dfSLoic Poulain
1082aa6802dfSLoic Poulain return 0;
1083aa6802dfSLoic Poulain }
1084aa6802dfSLoic Poulain
intel_resume_device(struct device * dev)1085f7552473SLoic Poulain static int intel_resume_device(struct device *dev)
1086aa6802dfSLoic Poulain {
1087aa6802dfSLoic Poulain struct intel_device *idev = dev_get_drvdata(dev);
1088aa6802dfSLoic Poulain
1089aa6802dfSLoic Poulain mutex_lock(&idev->hu_lock);
1090aa6802dfSLoic Poulain if (idev->hu)
1091aa6802dfSLoic Poulain intel_lpm_resume(idev->hu);
1092aa6802dfSLoic Poulain mutex_unlock(&idev->hu_lock);
1093aa6802dfSLoic Poulain
1094aa6802dfSLoic Poulain return 0;
1095aa6802dfSLoic Poulain }
1096aa6802dfSLoic Poulain #endif
1097aa6802dfSLoic Poulain
1098f7552473SLoic Poulain #ifdef CONFIG_PM_SLEEP
intel_suspend(struct device * dev)1099f7552473SLoic Poulain static int intel_suspend(struct device *dev)
1100f7552473SLoic Poulain {
1101f7552473SLoic Poulain struct intel_device *idev = dev_get_drvdata(dev);
1102f7552473SLoic Poulain
1103f7552473SLoic Poulain if (device_may_wakeup(dev))
1104f7552473SLoic Poulain enable_irq_wake(idev->irq);
1105f7552473SLoic Poulain
1106f7552473SLoic Poulain return intel_suspend_device(dev);
1107f7552473SLoic Poulain }
1108f7552473SLoic Poulain
intel_resume(struct device * dev)1109f7552473SLoic Poulain static int intel_resume(struct device *dev)
1110f7552473SLoic Poulain {
1111f7552473SLoic Poulain struct intel_device *idev = dev_get_drvdata(dev);
1112f7552473SLoic Poulain
1113f7552473SLoic Poulain if (device_may_wakeup(dev))
1114f7552473SLoic Poulain disable_irq_wake(idev->irq);
1115f7552473SLoic Poulain
1116f7552473SLoic Poulain return intel_resume_device(dev);
1117f7552473SLoic Poulain }
1118f7552473SLoic Poulain #endif
1119f7552473SLoic Poulain
1120aa6802dfSLoic Poulain static const struct dev_pm_ops intel_pm_ops = {
1121aa6802dfSLoic Poulain SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1122f7552473SLoic Poulain SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
1123aa6802dfSLoic Poulain };
1124aa6802dfSLoic Poulain
11254a59d433SAndy Shevchenko static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
11264a59d433SAndy Shevchenko static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
11274a59d433SAndy Shevchenko
11284a59d433SAndy Shevchenko static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
1129c6db0143SAndy Shevchenko { "reset-gpios", &reset_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
1130c6db0143SAndy Shevchenko { "host-wake-gpios", &host_wake_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
1131c6db0143SAndy Shevchenko { }
11324a59d433SAndy Shevchenko };
11334a59d433SAndy Shevchenko
intel_probe(struct platform_device * pdev)11341ab1f239SLoic Poulain static int intel_probe(struct platform_device *pdev)
11351ab1f239SLoic Poulain {
11361ab1f239SLoic Poulain struct intel_device *idev;
11374a59d433SAndy Shevchenko int ret;
11381ab1f239SLoic Poulain
11391ab1f239SLoic Poulain idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
11401ab1f239SLoic Poulain if (!idev)
11411ab1f239SLoic Poulain return -ENOMEM;
11421ab1f239SLoic Poulain
1143aa6802dfSLoic Poulain mutex_init(&idev->hu_lock);
1144aa6802dfSLoic Poulain
11451ab1f239SLoic Poulain idev->pdev = pdev;
11461ab1f239SLoic Poulain
11474a59d433SAndy Shevchenko ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
11484a59d433SAndy Shevchenko if (ret)
11494a59d433SAndy Shevchenko dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
11504a59d433SAndy Shevchenko
115132b9ccbcSLoic Poulain idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
11521ab1f239SLoic Poulain if (IS_ERR(idev->reset)) {
11531ab1f239SLoic Poulain dev_err(&pdev->dev, "Unable to retrieve gpio\n");
11541ab1f239SLoic Poulain return PTR_ERR(idev->reset);
11551ab1f239SLoic Poulain }
11561ab1f239SLoic Poulain
1157765ea3abSLoic Poulain idev->irq = platform_get_irq(pdev, 0);
1158765ea3abSLoic Poulain if (idev->irq < 0) {
1159765ea3abSLoic Poulain struct gpio_desc *host_wake;
1160765ea3abSLoic Poulain
1161765ea3abSLoic Poulain dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1162765ea3abSLoic Poulain
116332b9ccbcSLoic Poulain host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
1164765ea3abSLoic Poulain if (IS_ERR(host_wake)) {
1165765ea3abSLoic Poulain dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1166765ea3abSLoic Poulain goto no_irq;
1167765ea3abSLoic Poulain }
1168765ea3abSLoic Poulain
1169765ea3abSLoic Poulain idev->irq = gpiod_to_irq(host_wake);
1170765ea3abSLoic Poulain if (idev->irq < 0) {
1171765ea3abSLoic Poulain dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1172765ea3abSLoic Poulain goto no_irq;
1173765ea3abSLoic Poulain }
1174765ea3abSLoic Poulain }
1175765ea3abSLoic Poulain
1176765ea3abSLoic Poulain /* Only enable wake-up/irq when controller is powered */
1177765ea3abSLoic Poulain device_set_wakeup_capable(&pdev->dev, true);
1178765ea3abSLoic Poulain device_wakeup_disable(&pdev->dev);
1179765ea3abSLoic Poulain
1180765ea3abSLoic Poulain no_irq:
11811ab1f239SLoic Poulain platform_set_drvdata(pdev, idev);
11821ab1f239SLoic Poulain
11831ab1f239SLoic Poulain /* Place this instance on the device list */
118467c8bde0SLoic Poulain mutex_lock(&intel_device_list_lock);
11851ab1f239SLoic Poulain list_add_tail(&idev->list, &intel_device_list);
118667c8bde0SLoic Poulain mutex_unlock(&intel_device_list_lock);
11871ab1f239SLoic Poulain
1188765ea3abSLoic Poulain dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1189765ea3abSLoic Poulain desc_to_gpio(idev->reset), idev->irq);
11901ab1f239SLoic Poulain
11911ab1f239SLoic Poulain return 0;
11921ab1f239SLoic Poulain }
11931ab1f239SLoic Poulain
intel_remove(struct platform_device * pdev)11942efbac88SUwe Kleine-König static void intel_remove(struct platform_device *pdev)
11951ab1f239SLoic Poulain {
11961ab1f239SLoic Poulain struct intel_device *idev = platform_get_drvdata(pdev);
11971ab1f239SLoic Poulain
1198765ea3abSLoic Poulain device_wakeup_disable(&pdev->dev);
1199765ea3abSLoic Poulain
120067c8bde0SLoic Poulain mutex_lock(&intel_device_list_lock);
12011ab1f239SLoic Poulain list_del(&idev->list);
120267c8bde0SLoic Poulain mutex_unlock(&intel_device_list_lock);
12031ab1f239SLoic Poulain
12041ab1f239SLoic Poulain dev_info(&pdev->dev, "unregistered.\n");
12051ab1f239SLoic Poulain }
12061ab1f239SLoic Poulain
12071ab1f239SLoic Poulain static struct platform_driver intel_driver = {
12081ab1f239SLoic Poulain .probe = intel_probe,
12092efbac88SUwe Kleine-König .remove_new = intel_remove,
12101ab1f239SLoic Poulain .driver = {
12111ab1f239SLoic Poulain .name = "hci_intel",
12121ab1f239SLoic Poulain .acpi_match_table = ACPI_PTR(intel_acpi_match),
1213aa6802dfSLoic Poulain .pm = &intel_pm_ops,
12141ab1f239SLoic Poulain },
12151ab1f239SLoic Poulain };
12161ab1f239SLoic Poulain
intel_init(void)1217ca93cee5SLoic Poulain int __init intel_init(void)
1218ca93cee5SLoic Poulain {
1219ab2d2a98SJiasheng Jiang int err;
1220ab2d2a98SJiasheng Jiang
1221ab2d2a98SJiasheng Jiang err = platform_driver_register(&intel_driver);
1222ab2d2a98SJiasheng Jiang if (err)
1223ab2d2a98SJiasheng Jiang return err;
12241ab1f239SLoic Poulain
1225ca93cee5SLoic Poulain return hci_uart_register_proto(&intel_proto);
1226ca93cee5SLoic Poulain }
1227ca93cee5SLoic Poulain
intel_deinit(void)1228ca93cee5SLoic Poulain int __exit intel_deinit(void)
1229ca93cee5SLoic Poulain {
12301ab1f239SLoic Poulain platform_driver_unregister(&intel_driver);
12311ab1f239SLoic Poulain
1232ca93cee5SLoic Poulain return hci_uart_unregister_proto(&intel_proto);
1233ca93cee5SLoic Poulain }
1234