xref: /linux/drivers/bluetooth/hci_intel.c (revision 0cc62caf4c8f376f8a846271083379667cf74739)
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 
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 
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
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 
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 
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 
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 
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 
2891ab1f239SLoic Poulain static int intel_set_power(struct hci_uart *hu, bool powered)
2901ab1f239SLoic Poulain {
2911ab1f239SLoic Poulain 	struct list_head *p;
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 
2991ab1f239SLoic Poulain 	list_for_each(p, &intel_device_list) {
3001ab1f239SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
3011ab1f239SLoic Poulain 						       list);
3021ab1f239SLoic Poulain 
3031ab1f239SLoic Poulain 		/* tty device and pdev device should share the same parent
3041ab1f239SLoic Poulain 		 * which is the UART port.
3051ab1f239SLoic Poulain 		 */
3061ab1f239SLoic Poulain 		if (hu->tty->dev->parent != idev->pdev->dev.parent)
3071ab1f239SLoic Poulain 			continue;
3081ab1f239SLoic Poulain 
3091ab1f239SLoic Poulain 		if (!idev->reset) {
3101ab1f239SLoic Poulain 			err = -ENOTSUPP;
3111ab1f239SLoic Poulain 			break;
3121ab1f239SLoic Poulain 		}
3131ab1f239SLoic Poulain 
3141ab1f239SLoic Poulain 		BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
3151ab1f239SLoic Poulain 			hu, dev_name(&idev->pdev->dev), powered);
3161ab1f239SLoic Poulain 
3171ab1f239SLoic Poulain 		gpiod_set_value(idev->reset, powered);
318765ea3abSLoic Poulain 
319aa6802dfSLoic Poulain 		/* Provide to idev a hu reference which is used to run LPM
320aa6802dfSLoic Poulain 		 * transactions (lpm suspend/resume) from PM callbacks.
321aa6802dfSLoic Poulain 		 * hu needs to be protected against concurrent removing during
322aa6802dfSLoic Poulain 		 * these PM ops.
323aa6802dfSLoic Poulain 		 */
324aa6802dfSLoic Poulain 		mutex_lock(&idev->hu_lock);
325aa6802dfSLoic Poulain 		idev->hu = powered ? hu : NULL;
326aa6802dfSLoic Poulain 		mutex_unlock(&idev->hu_lock);
327aa6802dfSLoic Poulain 
328765ea3abSLoic Poulain 		if (idev->irq < 0)
329765ea3abSLoic Poulain 			break;
330765ea3abSLoic Poulain 
331765ea3abSLoic Poulain 		if (powered && device_can_wakeup(&idev->pdev->dev)) {
332765ea3abSLoic Poulain 			err = devm_request_threaded_irq(&idev->pdev->dev,
333765ea3abSLoic Poulain 							idev->irq, NULL,
334765ea3abSLoic Poulain 							intel_irq,
335765ea3abSLoic Poulain 							IRQF_ONESHOT,
336765ea3abSLoic Poulain 							"bt-host-wake", idev);
337765ea3abSLoic Poulain 			if (err) {
338765ea3abSLoic Poulain 				BT_ERR("hu %p, unable to allocate irq-%d",
339765ea3abSLoic Poulain 				       hu, idev->irq);
340765ea3abSLoic Poulain 				break;
341765ea3abSLoic Poulain 			}
342765ea3abSLoic Poulain 
343765ea3abSLoic Poulain 			device_wakeup_enable(&idev->pdev->dev);
34474cdad37SLoic Poulain 
34574cdad37SLoic Poulain 			pm_runtime_set_active(&idev->pdev->dev);
34674cdad37SLoic Poulain 			pm_runtime_use_autosuspend(&idev->pdev->dev);
34774cdad37SLoic Poulain 			pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
34874cdad37SLoic Poulain 							 LPM_SUSPEND_DELAY_MS);
34974cdad37SLoic Poulain 			pm_runtime_enable(&idev->pdev->dev);
350765ea3abSLoic Poulain 		} else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
351765ea3abSLoic Poulain 			devm_free_irq(&idev->pdev->dev, idev->irq, idev);
352765ea3abSLoic Poulain 			device_wakeup_disable(&idev->pdev->dev);
35374cdad37SLoic Poulain 
35474cdad37SLoic Poulain 			pm_runtime_disable(&idev->pdev->dev);
355765ea3abSLoic Poulain 		}
3561ab1f239SLoic Poulain 	}
3571ab1f239SLoic Poulain 
35867c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
3591ab1f239SLoic Poulain 
3601ab1f239SLoic Poulain 	return err;
3611ab1f239SLoic Poulain }
3621ab1f239SLoic Poulain 
36374cdad37SLoic Poulain static void intel_busy_work(struct work_struct *work)
36474cdad37SLoic Poulain {
36574cdad37SLoic Poulain 	struct list_head *p;
36674cdad37SLoic Poulain 	struct intel_data *intel = container_of(work, struct intel_data,
36774cdad37SLoic Poulain 						busy_work);
36874cdad37SLoic Poulain 
369dcb9cfaaSJohan Hovold 	if (!intel->hu->tty->dev)
370dcb9cfaaSJohan Hovold 		return;
371dcb9cfaaSJohan Hovold 
37274cdad37SLoic Poulain 	/* Link is busy, delay the suspend */
37374cdad37SLoic Poulain 	mutex_lock(&intel_device_list_lock);
37474cdad37SLoic Poulain 	list_for_each(p, &intel_device_list) {
37574cdad37SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
37674cdad37SLoic Poulain 						       list);
37774cdad37SLoic Poulain 
37874cdad37SLoic Poulain 		if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
37974cdad37SLoic Poulain 			pm_runtime_get(&idev->pdev->dev);
38074cdad37SLoic Poulain 			pm_runtime_mark_last_busy(&idev->pdev->dev);
38174cdad37SLoic Poulain 			pm_runtime_put_autosuspend(&idev->pdev->dev);
38274cdad37SLoic Poulain 			break;
38374cdad37SLoic Poulain 		}
38474cdad37SLoic Poulain 	}
38574cdad37SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
38674cdad37SLoic Poulain }
38774cdad37SLoic Poulain 
388ca93cee5SLoic Poulain static int intel_open(struct hci_uart *hu)
389ca93cee5SLoic Poulain {
390ca93cee5SLoic Poulain 	struct intel_data *intel;
391ca93cee5SLoic Poulain 
392ca93cee5SLoic Poulain 	BT_DBG("hu %p", hu);
393ca93cee5SLoic Poulain 
394b36a1552SVladis Dronov 	if (!hci_uart_has_flow_control(hu))
395b36a1552SVladis Dronov 		return -EOPNOTSUPP;
396b36a1552SVladis Dronov 
397ca93cee5SLoic Poulain 	intel = kzalloc(sizeof(*intel), GFP_KERNEL);
398ca93cee5SLoic Poulain 	if (!intel)
399ca93cee5SLoic Poulain 		return -ENOMEM;
400ca93cee5SLoic Poulain 
401ca93cee5SLoic Poulain 	skb_queue_head_init(&intel->txq);
40274cdad37SLoic Poulain 	INIT_WORK(&intel->busy_work, intel_busy_work);
40374cdad37SLoic Poulain 
40474cdad37SLoic Poulain 	intel->hu = hu;
405ca93cee5SLoic Poulain 
406ca93cee5SLoic Poulain 	hu->priv = intel;
4071ab1f239SLoic Poulain 
4081ab1f239SLoic Poulain 	if (!intel_set_power(hu, true))
4091ab1f239SLoic Poulain 		set_bit(STATE_BOOTING, &intel->flags);
4101ab1f239SLoic Poulain 
411ca93cee5SLoic Poulain 	return 0;
412ca93cee5SLoic Poulain }
413ca93cee5SLoic Poulain 
414ca93cee5SLoic Poulain static int intel_close(struct hci_uart *hu)
415ca93cee5SLoic Poulain {
416ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
417ca93cee5SLoic Poulain 
418ca93cee5SLoic Poulain 	BT_DBG("hu %p", hu);
419ca93cee5SLoic Poulain 
42074cdad37SLoic Poulain 	cancel_work_sync(&intel->busy_work);
42174cdad37SLoic Poulain 
4221ab1f239SLoic Poulain 	intel_set_power(hu, false);
4231ab1f239SLoic Poulain 
424ca93cee5SLoic Poulain 	skb_queue_purge(&intel->txq);
425ca93cee5SLoic Poulain 	kfree_skb(intel->rx_skb);
426ca93cee5SLoic Poulain 	kfree(intel);
427ca93cee5SLoic Poulain 
428ca93cee5SLoic Poulain 	hu->priv = NULL;
429ca93cee5SLoic Poulain 	return 0;
430ca93cee5SLoic Poulain }
431ca93cee5SLoic Poulain 
432ca93cee5SLoic Poulain static int intel_flush(struct hci_uart *hu)
433ca93cee5SLoic Poulain {
434ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
435ca93cee5SLoic Poulain 
436ca93cee5SLoic Poulain 	BT_DBG("hu %p", hu);
437ca93cee5SLoic Poulain 
438ca93cee5SLoic Poulain 	skb_queue_purge(&intel->txq);
439ca93cee5SLoic Poulain 
440ca93cee5SLoic Poulain 	return 0;
441ca93cee5SLoic Poulain }
442ca93cee5SLoic Poulain 
443ca93cee5SLoic Poulain static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
444ca93cee5SLoic Poulain {
445ca93cee5SLoic Poulain 	struct sk_buff *skb;
446ca93cee5SLoic Poulain 	struct hci_event_hdr *hdr;
447ca93cee5SLoic Poulain 	struct hci_ev_cmd_complete *evt;
448ca93cee5SLoic Poulain 
449f6ebfc24SJia-Ju Bai 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
450ca93cee5SLoic Poulain 	if (!skb)
451ca93cee5SLoic Poulain 		return -ENOMEM;
452ca93cee5SLoic Poulain 
4534df864c1SJohannes Berg 	hdr = skb_put(skb, sizeof(*hdr));
454ca93cee5SLoic Poulain 	hdr->evt = HCI_EV_CMD_COMPLETE;
455ca93cee5SLoic Poulain 	hdr->plen = sizeof(*evt) + 1;
456ca93cee5SLoic Poulain 
4574df864c1SJohannes Berg 	evt = skb_put(skb, sizeof(*evt));
458ca93cee5SLoic Poulain 	evt->ncmd = 0x01;
459ca93cee5SLoic Poulain 	evt->opcode = cpu_to_le16(opcode);
460ca93cee5SLoic Poulain 
461634fef61SJohannes Berg 	skb_put_u8(skb, 0x00);
462ca93cee5SLoic Poulain 
463618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
464ca93cee5SLoic Poulain 
465ca93cee5SLoic Poulain 	return hci_recv_frame(hdev, skb);
466ca93cee5SLoic Poulain }
467ca93cee5SLoic Poulain 
468ff289559SLoic Poulain static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
469ff289559SLoic Poulain {
470ff289559SLoic Poulain 	struct intel_data *intel = hu->priv;
471ff289559SLoic Poulain 	struct hci_dev *hdev = hu->hdev;
472ff289559SLoic Poulain 	u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
473ff289559SLoic Poulain 	struct sk_buff *skb;
4741ab1f239SLoic Poulain 	int err;
4751ab1f239SLoic Poulain 
4761ab1f239SLoic Poulain 	/* This can be the first command sent to the chip, check
4771ab1f239SLoic Poulain 	 * that the controller is ready.
4781ab1f239SLoic Poulain 	 */
4791ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
4801ab1f239SLoic Poulain 
4811ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
4821ab1f239SLoic Poulain 
4831ab1f239SLoic Poulain 	/* In case of timeout, try to continue anyway */
4842be1149eSAnton Protopopov 	if (err && err != -ETIMEDOUT)
4851ab1f239SLoic Poulain 		return err;
486ff289559SLoic Poulain 
487f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Change controller speed to %d", speed);
488ff289559SLoic Poulain 
489ff289559SLoic Poulain 	speed_cmd[3] = intel_convert_speed(speed);
490ff289559SLoic Poulain 	if (speed_cmd[3] == 0xff) {
491f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported speed");
492ff289559SLoic Poulain 		return -EINVAL;
493ff289559SLoic Poulain 	}
494ff289559SLoic Poulain 
495ff289559SLoic Poulain 	/* Device will not accept speed change if Intel version has not been
496ff289559SLoic Poulain 	 * previously requested.
497ff289559SLoic Poulain 	 */
498a0c38245SLoic Poulain 	skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
499ff289559SLoic Poulain 	if (IS_ERR(skb)) {
500f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
501f44e78a5SLoic Poulain 			   PTR_ERR(skb));
502ff289559SLoic Poulain 		return PTR_ERR(skb);
503ff289559SLoic Poulain 	}
504ff289559SLoic Poulain 	kfree_skb(skb);
505ff289559SLoic Poulain 
506ff289559SLoic Poulain 	skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
507ff289559SLoic Poulain 	if (!skb) {
508f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
509ff289559SLoic Poulain 		return -ENOMEM;
510ff289559SLoic Poulain 	}
511ff289559SLoic Poulain 
51259ae1d12SJohannes Berg 	skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
513618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
514ff289559SLoic Poulain 
515ff289559SLoic Poulain 	hci_uart_set_flow_control(hu, true);
516ff289559SLoic Poulain 
517ff289559SLoic Poulain 	skb_queue_tail(&intel->txq, skb);
518ff289559SLoic Poulain 	hci_uart_tx_wakeup(hu);
519ff289559SLoic Poulain 
520ff289559SLoic Poulain 	/* wait 100ms to change baudrate on controller side */
521ff289559SLoic Poulain 	msleep(100);
522ff289559SLoic Poulain 
523ff289559SLoic Poulain 	hci_uart_set_baudrate(hu, speed);
524ff289559SLoic Poulain 	hci_uart_set_flow_control(hu, false);
525ff289559SLoic Poulain 
526ff289559SLoic Poulain 	return 0;
527ff289559SLoic Poulain }
528ff289559SLoic Poulain 
529ca93cee5SLoic Poulain static int intel_setup(struct hci_uart *hu)
530ca93cee5SLoic Poulain {
531ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
532ca93cee5SLoic Poulain 	struct hci_dev *hdev = hu->hdev;
533ca93cee5SLoic Poulain 	struct sk_buff *skb;
5346c483de1SLoic Poulain 	struct intel_version ver;
535faf174d2STedd Ho-Jeong An 	struct intel_boot_params params;
536b98469f4SLoic Poulain 	struct list_head *p;
537ca93cee5SLoic Poulain 	const struct firmware *fw;
538ca93cee5SLoic Poulain 	char fwname[64];
539e5889af6STedd Ho-Jeong An 	u32 boot_param;
540ca93cee5SLoic Poulain 	ktime_t calltime, delta, rettime;
541ca93cee5SLoic Poulain 	unsigned long long duration;
542ff289559SLoic Poulain 	unsigned int init_speed, oper_speed;
543ff289559SLoic Poulain 	int speed_change = 0;
544ca93cee5SLoic Poulain 	int err;
545ca93cee5SLoic Poulain 
546f44e78a5SLoic Poulain 	bt_dev_dbg(hdev, "start intel_setup");
547ca93cee5SLoic Poulain 
5486d2e50d2SMarcel Holtmann 	hu->hdev->set_diag = btintel_set_diag;
54935ab8150SMarcel Holtmann 	hu->hdev->set_bdaddr = btintel_set_bdaddr;
55035ab8150SMarcel Holtmann 
55104d729b8STedd Ho-Jeong An 	/* Set the default boot parameter to 0x0 and it is updated to
55204d729b8STedd Ho-Jeong An 	 * SKU specific boot parameter after reading Intel_Write_Boot_Params
55304d729b8STedd Ho-Jeong An 	 * command while downloading the firmware.
55404d729b8STedd Ho-Jeong An 	 */
55504d729b8STedd Ho-Jeong An 	boot_param = 0x00000000;
556e5889af6STedd Ho-Jeong An 
557ca93cee5SLoic Poulain 	calltime = ktime_get();
558ca93cee5SLoic Poulain 
559ff289559SLoic Poulain 	if (hu->init_speed)
560ff289559SLoic Poulain 		init_speed = hu->init_speed;
561ff289559SLoic Poulain 	else
562ff289559SLoic Poulain 		init_speed = hu->proto->init_speed;
563ff289559SLoic Poulain 
564ff289559SLoic Poulain 	if (hu->oper_speed)
565ff289559SLoic Poulain 		oper_speed = hu->oper_speed;
566ff289559SLoic Poulain 	else
567ff289559SLoic Poulain 		oper_speed = hu->proto->oper_speed;
568ff289559SLoic Poulain 
569ff289559SLoic Poulain 	if (oper_speed && init_speed && oper_speed != init_speed)
570ff289559SLoic Poulain 		speed_change = 1;
571ff289559SLoic Poulain 
5721ab1f239SLoic Poulain 	/* Check that the controller is ready */
5731ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
5741ab1f239SLoic Poulain 
5751ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
5761ab1f239SLoic Poulain 
5771ab1f239SLoic Poulain 	/* In case of timeout, try to continue anyway */
5782be1149eSAnton Protopopov 	if (err && err != -ETIMEDOUT)
5791ab1f239SLoic Poulain 		return err;
5801ab1f239SLoic Poulain 
581ca93cee5SLoic Poulain 	set_bit(STATE_BOOTLOADER, &intel->flags);
582ca93cee5SLoic Poulain 
583ca93cee5SLoic Poulain 	/* Read the Intel version information to determine if the device
584ca93cee5SLoic Poulain 	 * is in bootloader mode or if it already has operational firmware
585ca93cee5SLoic Poulain 	 * loaded.
586ca93cee5SLoic Poulain 	 */
5876c483de1SLoic Poulain 	err = btintel_read_version(hdev, &ver);
5886c483de1SLoic Poulain 	if (err)
589ca93cee5SLoic Poulain 		return err;
590ca93cee5SLoic Poulain 
591ca93cee5SLoic Poulain 	/* The hardware platform number has a fixed value of 0x37 and
592ca93cee5SLoic Poulain 	 * for now only accept this single value.
593ca93cee5SLoic Poulain 	 */
5946c483de1SLoic Poulain 	if (ver.hw_platform != 0x37) {
595f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
5966c483de1SLoic Poulain 			   ver.hw_platform);
597ca93cee5SLoic Poulain 		return -EINVAL;
598ca93cee5SLoic Poulain 	}
599ca93cee5SLoic Poulain 
6009268834bSTedd Ho-Jeong An         /* Check for supported iBT hardware variants of this firmware
6019268834bSTedd Ho-Jeong An          * loading method.
6029268834bSTedd Ho-Jeong An          *
6039268834bSTedd Ho-Jeong An          * This check has been put in place to ensure correct forward
6049268834bSTedd Ho-Jeong An          * compatibility options when newer hardware variants come along.
605ca93cee5SLoic Poulain          */
6069268834bSTedd Ho-Jeong An 	switch (ver.hw_variant) {
6079268834bSTedd Ho-Jeong An 	case 0x0b:	/* LnP */
6089268834bSTedd Ho-Jeong An 	case 0x0c:	/* WsP */
6096c7bb7ebSTedd Ho-Jeong An 	case 0x12:	/* ThP */
6109268834bSTedd Ho-Jeong An 		break;
6119268834bSTedd Ho-Jeong An 	default:
612f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
6136c483de1SLoic Poulain 			   ver.hw_variant);
614ca93cee5SLoic Poulain 		return -EINVAL;
615ca93cee5SLoic Poulain 	}
616ca93cee5SLoic Poulain 
6176c483de1SLoic Poulain 	btintel_version_info(hdev, &ver);
618ca93cee5SLoic Poulain 
619ca93cee5SLoic Poulain 	/* The firmware variant determines if the device is in bootloader
620ca93cee5SLoic Poulain 	 * mode or is running operational firmware. The value 0x06 identifies
621ca93cee5SLoic Poulain 	 * the bootloader and the value 0x23 identifies the operational
622ca93cee5SLoic Poulain 	 * firmware.
623ca93cee5SLoic Poulain 	 *
624ca93cee5SLoic Poulain 	 * When the operational firmware is already present, then only
625ca93cee5SLoic Poulain 	 * the check for valid Bluetooth device address is needed. This
626ca93cee5SLoic Poulain 	 * determines if the device will be added as configured or
627ca93cee5SLoic Poulain 	 * unconfigured controller.
628ca93cee5SLoic Poulain 	 *
629ca93cee5SLoic Poulain 	 * It is not possible to use the Secure Boot Parameters in this
630ca93cee5SLoic Poulain 	 * case since that command is only available in bootloader mode.
631ca93cee5SLoic Poulain 	 */
6326c483de1SLoic Poulain 	if (ver.fw_variant == 0x23) {
633ca93cee5SLoic Poulain 		clear_bit(STATE_BOOTLOADER, &intel->flags);
634ca93cee5SLoic Poulain 		btintel_check_bdaddr(hdev);
635ca93cee5SLoic Poulain 		return 0;
636ca93cee5SLoic Poulain 	}
637ca93cee5SLoic Poulain 
638ca93cee5SLoic Poulain 	/* If the device is not in bootloader mode, then the only possible
639ca93cee5SLoic Poulain 	 * choice is to return an error and abort the device initialization.
640ca93cee5SLoic Poulain 	 */
6416c483de1SLoic Poulain 	if (ver.fw_variant != 0x06) {
642f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
6436c483de1SLoic Poulain 			   ver.fw_variant);
644ca93cee5SLoic Poulain 		return -ENODEV;
645ca93cee5SLoic Poulain 	}
646ca93cee5SLoic Poulain 
647ca93cee5SLoic Poulain 	/* Read the secure boot parameters to identify the operating
648ca93cee5SLoic Poulain 	 * details of the bootloader.
649ca93cee5SLoic Poulain 	 */
650faf174d2STedd Ho-Jeong An 	err = btintel_read_boot_params(hdev, &params);
651faf174d2STedd Ho-Jeong An 	if (err)
652ca93cee5SLoic Poulain 		return err;
653ca93cee5SLoic Poulain 
654ca93cee5SLoic Poulain 	/* It is required that every single firmware fragment is acknowledged
655ca93cee5SLoic Poulain 	 * with a command complete event. If the boot parameters indicate
656ca93cee5SLoic Poulain 	 * that this bootloader does not send them, then abort the setup.
657ca93cee5SLoic Poulain 	 */
658faf174d2STedd Ho-Jeong An 	if (params.limited_cce != 0x00) {
659f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
660faf174d2STedd Ho-Jeong An 			   params.limited_cce);
661ca93cee5SLoic Poulain 		return -EINVAL;
662ca93cee5SLoic Poulain 	}
663ca93cee5SLoic Poulain 
664ca93cee5SLoic Poulain 	/* If the OTP has no valid Bluetooth device address, then there will
665ca93cee5SLoic Poulain 	 * also be no valid address for the operational firmware.
666ca93cee5SLoic Poulain 	 */
667faf174d2STedd Ho-Jeong An 	if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
668f44e78a5SLoic Poulain 		bt_dev_info(hdev, "No device address configured");
669ca93cee5SLoic Poulain 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
670ca93cee5SLoic Poulain 	}
671ca93cee5SLoic Poulain 
672ca93cee5SLoic Poulain 	/* With this Intel bootloader only the hardware variant and device
673965651c1STedd Ho-Jeong An 	 * revision information are used to select the right firmware for SfP
674965651c1STedd Ho-Jeong An 	 * and WsP.
675ca93cee5SLoic Poulain 	 *
676b7da6a69STedd Ho-Jeong An 	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
677b7da6a69STedd Ho-Jeong An 	 *
678b7da6a69STedd Ho-Jeong An 	 * Currently the supported hardware variants are:
679b7da6a69STedd Ho-Jeong An 	 *   11 (0x0b) for iBT 3.0 (LnP/SfP)
680965651c1STedd Ho-Jeong An 	 *   12 (0x0c) for iBT 3.5 (WsP)
681965651c1STedd Ho-Jeong An 	 *
682965651c1STedd Ho-Jeong An 	 * For ThP/JfP and for future SKU's, the FW name varies based on HW
683965651c1STedd Ho-Jeong An 	 * variant, HW revision and FW revision, as these are dependent on CNVi
684965651c1STedd Ho-Jeong An 	 * and RF Combination.
685965651c1STedd Ho-Jeong An 	 *
686965651c1STedd Ho-Jeong An 	 *   18 (0x12) for iBT3.5 (ThP/JfP)
687965651c1STedd Ho-Jeong An 	 *
688965651c1STedd Ho-Jeong An 	 * The firmware file name for these will be
689965651c1STedd Ho-Jeong An 	 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
690965651c1STedd Ho-Jeong An 	 *
691ca93cee5SLoic Poulain 	 */
692965651c1STedd Ho-Jeong An 	switch (ver.hw_variant) {
693965651c1STedd Ho-Jeong An 	case 0x0b:      /* SfP */
694965651c1STedd Ho-Jeong An 	case 0x0c:      /* WsP */
695b7da6a69STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
696*0cc62cafSAndy Shevchenko 			 ver.hw_variant, le16_to_cpu(params.dev_revid));
697965651c1STedd Ho-Jeong An 		break;
698965651c1STedd Ho-Jeong An 	case 0x12:      /* ThP */
699965651c1STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
700*0cc62cafSAndy Shevchenko 			 ver.hw_variant, ver.hw_revision, ver.fw_revision);
701965651c1STedd Ho-Jeong An 		break;
702965651c1STedd Ho-Jeong An 	default:
703965651c1STedd Ho-Jeong An 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
704965651c1STedd Ho-Jeong An 			   ver.hw_variant);
705965651c1STedd Ho-Jeong An 		return -EINVAL;
706965651c1STedd Ho-Jeong An 	}
707ca93cee5SLoic Poulain 
708ca93cee5SLoic Poulain 	err = request_firmware(&fw, fwname, &hdev->dev);
709ca93cee5SLoic Poulain 	if (err < 0) {
710f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
711f44e78a5SLoic Poulain 			   err);
712ca93cee5SLoic Poulain 		return err;
713ca93cee5SLoic Poulain 	}
714ca93cee5SLoic Poulain 
715f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Found device firmware: %s", fwname);
716ca93cee5SLoic Poulain 
7171cfbabddSLoic Poulain 	/* Save the DDC file name for later */
718965651c1STedd Ho-Jeong An 	switch (ver.hw_variant) {
719965651c1STedd Ho-Jeong An 	case 0x0b:      /* SfP */
720965651c1STedd Ho-Jeong An 	case 0x0c:      /* WsP */
721b7da6a69STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
722*0cc62cafSAndy Shevchenko 			 ver.hw_variant, le16_to_cpu(params.dev_revid));
723965651c1STedd Ho-Jeong An 		break;
724965651c1STedd Ho-Jeong An 	case 0x12:      /* ThP */
725965651c1STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
726*0cc62cafSAndy Shevchenko 			 ver.hw_variant, ver.hw_revision, ver.fw_revision);
727965651c1STedd Ho-Jeong An 		break;
728965651c1STedd Ho-Jeong An 	default:
729965651c1STedd Ho-Jeong An 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
730965651c1STedd Ho-Jeong An 			   ver.hw_variant);
731965651c1STedd Ho-Jeong An 		return -EINVAL;
732965651c1STedd Ho-Jeong An 	}
7331cfbabddSLoic Poulain 
734ca93cee5SLoic Poulain 	if (fw->size < 644) {
735f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
736f44e78a5SLoic Poulain 			   fw->size);
737ca93cee5SLoic Poulain 		err = -EBADF;
738ca93cee5SLoic Poulain 		goto done;
739ca93cee5SLoic Poulain 	}
740ca93cee5SLoic Poulain 
741ca93cee5SLoic Poulain 	set_bit(STATE_DOWNLOADING, &intel->flags);
742ca93cee5SLoic Poulain 
743fbbe83c5STedd Ho-Jeong An 	/* Start firmware downloading and get boot parameter */
744fbbe83c5STedd Ho-Jeong An 	err = btintel_download_firmware(hdev, fw, &boot_param);
745fbbe83c5STedd Ho-Jeong An 	if (err < 0)
746ca93cee5SLoic Poulain 		goto done;
747ca93cee5SLoic Poulain 
748ca93cee5SLoic Poulain 	set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
749ca93cee5SLoic Poulain 
750f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Waiting for firmware download to complete");
751ca93cee5SLoic Poulain 
752ca93cee5SLoic Poulain 	/* Before switching the device into operational mode and with that
753ca93cee5SLoic Poulain 	 * booting the loaded firmware, wait for the bootloader notification
754ca93cee5SLoic Poulain 	 * that all fragments have been successfully received.
755ca93cee5SLoic Poulain 	 *
756ca93cee5SLoic Poulain 	 * When the event processing receives the notification, then the
757ca93cee5SLoic Poulain 	 * STATE_DOWNLOADING flag will be cleared.
758ca93cee5SLoic Poulain 	 *
759ca93cee5SLoic Poulain 	 * The firmware loading should not take longer than 5 seconds
760ca93cee5SLoic Poulain 	 * and thus just timeout if that happens and fail the setup
761ca93cee5SLoic Poulain 	 * of this device.
762ca93cee5SLoic Poulain 	 */
763ca93cee5SLoic Poulain 	err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
764ca93cee5SLoic Poulain 				  TASK_INTERRUPTIBLE,
765ca93cee5SLoic Poulain 				  msecs_to_jiffies(5000));
766f0a70a04SBart Van Assche 	if (err == -EINTR) {
767f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading interrupted");
768ca93cee5SLoic Poulain 		err = -EINTR;
769ca93cee5SLoic Poulain 		goto done;
770ca93cee5SLoic Poulain 	}
771ca93cee5SLoic Poulain 
772ca93cee5SLoic Poulain 	if (err) {
773f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading timeout");
774ca93cee5SLoic Poulain 		err = -ETIMEDOUT;
775ca93cee5SLoic Poulain 		goto done;
776ca93cee5SLoic Poulain 	}
777ca93cee5SLoic Poulain 
778ca93cee5SLoic Poulain 	if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
779f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading failed");
780ca93cee5SLoic Poulain 		err = -ENOEXEC;
781ca93cee5SLoic Poulain 		goto done;
782ca93cee5SLoic Poulain 	}
783ca93cee5SLoic Poulain 
784ca93cee5SLoic Poulain 	rettime = ktime_get();
785ca93cee5SLoic Poulain 	delta = ktime_sub(rettime, calltime);
786ca93cee5SLoic Poulain 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
787ca93cee5SLoic Poulain 
788f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
789ca93cee5SLoic Poulain 
790ca93cee5SLoic Poulain done:
791ca93cee5SLoic Poulain 	release_firmware(fw);
792ca93cee5SLoic Poulain 
793ca93cee5SLoic Poulain 	if (err < 0)
794ca93cee5SLoic Poulain 		return err;
795ca93cee5SLoic Poulain 
796ff289559SLoic Poulain 	/* We need to restore the default speed before Intel reset */
797ff289559SLoic Poulain 	if (speed_change) {
798ff289559SLoic Poulain 		err = intel_set_baudrate(hu, init_speed);
799ff289559SLoic Poulain 		if (err)
800ff289559SLoic Poulain 			return err;
801ff289559SLoic Poulain 	}
802ff289559SLoic Poulain 
803ca93cee5SLoic Poulain 	calltime = ktime_get();
804ca93cee5SLoic Poulain 
805ca93cee5SLoic Poulain 	set_bit(STATE_BOOTING, &intel->flags);
806ca93cee5SLoic Poulain 
807e5889af6STedd Ho-Jeong An 	err = btintel_send_intel_reset(hdev, boot_param);
808e5889af6STedd Ho-Jeong An 	if (err)
809e5889af6STedd Ho-Jeong An 		return err;
810ca93cee5SLoic Poulain 
811ca93cee5SLoic Poulain 	/* The bootloader will not indicate when the device is ready. This
812ca93cee5SLoic Poulain 	 * is done by the operational firmware sending bootup notification.
813ca93cee5SLoic Poulain 	 *
814ca93cee5SLoic Poulain 	 * Booting into operational firmware should not take longer than
815ca93cee5SLoic Poulain 	 * 1 second. However if that happens, then just fail the setup
816ca93cee5SLoic Poulain 	 * since something went wrong.
817ca93cee5SLoic Poulain 	 */
818f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Waiting for device to boot");
819ca93cee5SLoic Poulain 
8201ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
8211ab1f239SLoic Poulain 	if (err)
8221ab1f239SLoic Poulain 		return err;
823ca93cee5SLoic Poulain 
8241ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
825ca93cee5SLoic Poulain 
826ca93cee5SLoic Poulain 	rettime = ktime_get();
827ca93cee5SLoic Poulain 	delta = ktime_sub(rettime, calltime);
828ca93cee5SLoic Poulain 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
829ca93cee5SLoic Poulain 
830f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Device booted in %llu usecs", duration);
831ca93cee5SLoic Poulain 
83231eff267SLoic Poulain 	/* Enable LPM if matching pdev with wakeup enabled, set TX active
83331eff267SLoic Poulain 	 * until further LPM TX notification.
83431eff267SLoic Poulain 	 */
83567c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
836b98469f4SLoic Poulain 	list_for_each(p, &intel_device_list) {
837b98469f4SLoic Poulain 		struct intel_device *dev = list_entry(p, struct intel_device,
838b98469f4SLoic Poulain 						      list);
839dcb9cfaaSJohan Hovold 		if (!hu->tty->dev)
840dcb9cfaaSJohan Hovold 			break;
841b98469f4SLoic Poulain 		if (hu->tty->dev->parent == dev->pdev->dev.parent) {
84231eff267SLoic Poulain 			if (device_may_wakeup(&dev->pdev->dev)) {
84331eff267SLoic Poulain 				set_bit(STATE_LPM_ENABLED, &intel->flags);
84431eff267SLoic Poulain 				set_bit(STATE_TX_ACTIVE, &intel->flags);
84531eff267SLoic Poulain 			}
846b98469f4SLoic Poulain 			break;
847b98469f4SLoic Poulain 		}
848b98469f4SLoic Poulain 	}
84967c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
850b98469f4SLoic Poulain 
8511cfbabddSLoic Poulain 	/* Ignore errors, device can work without DDC parameters */
8521cfbabddSLoic Poulain 	btintel_load_ddc_config(hdev, fwname);
8531cfbabddSLoic Poulain 
854ff289559SLoic Poulain 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
855ff289559SLoic Poulain 	if (IS_ERR(skb))
856ff289559SLoic Poulain 		return PTR_ERR(skb);
857ff289559SLoic Poulain 	kfree_skb(skb);
858ff289559SLoic Poulain 
859ff289559SLoic Poulain 	if (speed_change) {
860ff289559SLoic Poulain 		err = intel_set_baudrate(hu, oper_speed);
861ff289559SLoic Poulain 		if (err)
862ff289559SLoic Poulain 			return err;
863ff289559SLoic Poulain 	}
864ff289559SLoic Poulain 
865f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Setup complete");
866ff289559SLoic Poulain 
867ca93cee5SLoic Poulain 	clear_bit(STATE_BOOTLOADER, &intel->flags);
868ca93cee5SLoic Poulain 
869ca93cee5SLoic Poulain 	return 0;
870ca93cee5SLoic Poulain }
871ca93cee5SLoic Poulain 
872ca93cee5SLoic Poulain static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
873ca93cee5SLoic Poulain {
874ca93cee5SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
875ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
876ca93cee5SLoic Poulain 	struct hci_event_hdr *hdr;
877ca93cee5SLoic Poulain 
8781ab1f239SLoic Poulain 	if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
8791ab1f239SLoic Poulain 	    !test_bit(STATE_BOOTING, &intel->flags))
880ca93cee5SLoic Poulain 		goto recv;
881ca93cee5SLoic Poulain 
882ca93cee5SLoic Poulain 	hdr = (void *)skb->data;
883ca93cee5SLoic Poulain 
884ca93cee5SLoic Poulain 	/* When the firmware loading completes the device sends
885ca93cee5SLoic Poulain 	 * out a vendor specific event indicating the result of
886ca93cee5SLoic Poulain 	 * the firmware loading.
887ca93cee5SLoic Poulain 	 */
888ca93cee5SLoic Poulain 	if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
889ca93cee5SLoic Poulain 	    skb->data[2] == 0x06) {
890ca93cee5SLoic Poulain 		if (skb->data[3] != 0x00)
891ca93cee5SLoic Poulain 			set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
892ca93cee5SLoic Poulain 
893ca93cee5SLoic Poulain 		if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
894dff6d593SAndrea Parri 		    test_bit(STATE_FIRMWARE_LOADED, &intel->flags))
895ca93cee5SLoic Poulain 			wake_up_bit(&intel->flags, STATE_DOWNLOADING);
896ca93cee5SLoic Poulain 
897ca93cee5SLoic Poulain 	/* When switching to the operational firmware the device
898ca93cee5SLoic Poulain 	 * sends a vendor specific event indicating that the bootup
899ca93cee5SLoic Poulain 	 * completed.
900ca93cee5SLoic Poulain 	 */
901ca93cee5SLoic Poulain 	} else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
902ca93cee5SLoic Poulain 		   skb->data[2] == 0x02) {
903dff6d593SAndrea Parri 		if (test_and_clear_bit(STATE_BOOTING, &intel->flags))
904ca93cee5SLoic Poulain 			wake_up_bit(&intel->flags, STATE_BOOTING);
905ca93cee5SLoic Poulain 	}
906ca93cee5SLoic Poulain recv:
907ca93cee5SLoic Poulain 	return hci_recv_frame(hdev, skb);
908ca93cee5SLoic Poulain }
909ca93cee5SLoic Poulain 
910b98469f4SLoic Poulain static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
911b98469f4SLoic Poulain {
912b98469f4SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
913b98469f4SLoic Poulain 	struct intel_data *intel = hu->priv;
914b98469f4SLoic Poulain 
915f44e78a5SLoic Poulain 	bt_dev_dbg(hdev, "TX idle notification (%d)", value);
916b98469f4SLoic Poulain 
91774cdad37SLoic Poulain 	if (value) {
918b98469f4SLoic Poulain 		set_bit(STATE_TX_ACTIVE, &intel->flags);
91974cdad37SLoic Poulain 		schedule_work(&intel->busy_work);
92074cdad37SLoic Poulain 	} else {
921b98469f4SLoic Poulain 		clear_bit(STATE_TX_ACTIVE, &intel->flags);
922b98469f4SLoic Poulain 	}
92374cdad37SLoic Poulain }
924b98469f4SLoic Poulain 
925b98469f4SLoic Poulain static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
926b98469f4SLoic Poulain {
927b98469f4SLoic Poulain 	struct hci_lpm_pkt *lpm = (void *)skb->data;
92889436546SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
92989436546SLoic Poulain 	struct intel_data *intel = hu->priv;
930b98469f4SLoic Poulain 
931b98469f4SLoic Poulain 	switch (lpm->opcode) {
932b98469f4SLoic Poulain 	case LPM_OP_TX_NOTIFY:
9331b197574SLoic Poulain 		if (lpm->dlen < 1) {
9341b197574SLoic Poulain 			bt_dev_err(hu->hdev, "Invalid LPM notification packet");
9351b197574SLoic Poulain 			break;
9361b197574SLoic Poulain 		}
937b98469f4SLoic Poulain 		intel_recv_lpm_notify(hdev, lpm->data[0]);
938b98469f4SLoic Poulain 		break;
93989436546SLoic Poulain 	case LPM_OP_SUSPEND_ACK:
94089436546SLoic Poulain 		set_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;
94489436546SLoic Poulain 	case LPM_OP_RESUME_ACK:
94589436546SLoic Poulain 		clear_bit(STATE_SUSPENDED, &intel->flags);
946dff6d593SAndrea Parri 		if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags))
94789436546SLoic Poulain 			wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
94889436546SLoic Poulain 		break;
949b98469f4SLoic Poulain 	default:
950f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
951b98469f4SLoic Poulain 		break;
952b98469f4SLoic Poulain 	}
953b98469f4SLoic Poulain 
954b98469f4SLoic Poulain 	kfree_skb(skb);
955b98469f4SLoic Poulain 
956b98469f4SLoic Poulain 	return 0;
957b98469f4SLoic Poulain }
958b98469f4SLoic Poulain 
959b98469f4SLoic Poulain #define INTEL_RECV_LPM \
960b98469f4SLoic Poulain 	.type = HCI_LPM_PKT, \
961b98469f4SLoic Poulain 	.hlen = HCI_LPM_HDR_SIZE, \
962b98469f4SLoic Poulain 	.loff = 1, \
963b98469f4SLoic Poulain 	.lsize = 1, \
964b98469f4SLoic Poulain 	.maxlen = HCI_LPM_MAX_SIZE
965b98469f4SLoic Poulain 
966ca93cee5SLoic Poulain static const struct h4_recv_pkt intel_recv_pkts[] = {
967ca93cee5SLoic Poulain 	{ H4_RECV_ACL,    .recv = hci_recv_frame   },
968ca93cee5SLoic Poulain 	{ H4_RECV_SCO,    .recv = hci_recv_frame   },
969ca93cee5SLoic Poulain 	{ H4_RECV_EVENT,  .recv = intel_recv_event },
970b98469f4SLoic Poulain 	{ INTEL_RECV_LPM, .recv = intel_recv_lpm   },
971ca93cee5SLoic Poulain };
972ca93cee5SLoic Poulain 
973ca93cee5SLoic Poulain static int intel_recv(struct hci_uart *hu, const void *data, int count)
974ca93cee5SLoic Poulain {
975ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
976ca93cee5SLoic Poulain 
977ca93cee5SLoic Poulain 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
978ca93cee5SLoic Poulain 		return -EUNATCH;
979ca93cee5SLoic Poulain 
980ca93cee5SLoic Poulain 	intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
981ca93cee5SLoic Poulain 				    intel_recv_pkts,
982ca93cee5SLoic Poulain 				    ARRAY_SIZE(intel_recv_pkts));
983ca93cee5SLoic Poulain 	if (IS_ERR(intel->rx_skb)) {
984ca93cee5SLoic Poulain 		int err = PTR_ERR(intel->rx_skb);
985f44e78a5SLoic Poulain 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
986ca93cee5SLoic Poulain 		intel->rx_skb = NULL;
987ca93cee5SLoic Poulain 		return err;
988ca93cee5SLoic Poulain 	}
989ca93cee5SLoic Poulain 
990ca93cee5SLoic Poulain 	return count;
991ca93cee5SLoic Poulain }
992ca93cee5SLoic Poulain 
993ca93cee5SLoic Poulain static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
994ca93cee5SLoic Poulain {
995ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
99674cdad37SLoic Poulain 	struct list_head *p;
997ca93cee5SLoic Poulain 
998ca93cee5SLoic Poulain 	BT_DBG("hu %p skb %p", hu, skb);
999ca93cee5SLoic Poulain 
1000dcb9cfaaSJohan Hovold 	if (!hu->tty->dev)
1001dcb9cfaaSJohan Hovold 		goto out_enqueue;
1002dcb9cfaaSJohan Hovold 
100374cdad37SLoic Poulain 	/* Be sure our controller is resumed and potential LPM transaction
100474cdad37SLoic Poulain 	 * completed before enqueuing any packet.
100574cdad37SLoic Poulain 	 */
100674cdad37SLoic Poulain 	mutex_lock(&intel_device_list_lock);
100774cdad37SLoic Poulain 	list_for_each(p, &intel_device_list) {
100874cdad37SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
100974cdad37SLoic Poulain 						       list);
101074cdad37SLoic Poulain 
101174cdad37SLoic Poulain 		if (hu->tty->dev->parent == idev->pdev->dev.parent) {
101274cdad37SLoic Poulain 			pm_runtime_get_sync(&idev->pdev->dev);
101374cdad37SLoic Poulain 			pm_runtime_mark_last_busy(&idev->pdev->dev);
101474cdad37SLoic Poulain 			pm_runtime_put_autosuspend(&idev->pdev->dev);
101574cdad37SLoic Poulain 			break;
101674cdad37SLoic Poulain 		}
101774cdad37SLoic Poulain 	}
101874cdad37SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
1019dcb9cfaaSJohan Hovold out_enqueue:
1020ca93cee5SLoic Poulain 	skb_queue_tail(&intel->txq, skb);
1021ca93cee5SLoic Poulain 
1022ca93cee5SLoic Poulain 	return 0;
1023ca93cee5SLoic Poulain }
1024ca93cee5SLoic Poulain 
1025ca93cee5SLoic Poulain static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1026ca93cee5SLoic Poulain {
1027ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
1028ca93cee5SLoic Poulain 	struct sk_buff *skb;
1029ca93cee5SLoic Poulain 
1030ca93cee5SLoic Poulain 	skb = skb_dequeue(&intel->txq);
1031ca93cee5SLoic Poulain 	if (!skb)
1032ca93cee5SLoic Poulain 		return skb;
1033ca93cee5SLoic Poulain 
1034ca93cee5SLoic Poulain 	if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1035618e8bc2SMarcel Holtmann 	    (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
1036ca93cee5SLoic Poulain 		struct hci_command_hdr *cmd = (void *)skb->data;
1037ca93cee5SLoic Poulain 		__u16 opcode = le16_to_cpu(cmd->opcode);
1038ca93cee5SLoic Poulain 
1039ca93cee5SLoic Poulain 		/* When the 0xfc01 command is issued to boot into
1040ca93cee5SLoic Poulain 		 * the operational firmware, it will actually not
1041ca93cee5SLoic Poulain 		 * send a command complete event. To keep the flow
1042ca93cee5SLoic Poulain 		 * control working inject that event here.
1043ca93cee5SLoic Poulain 		 */
1044ca93cee5SLoic Poulain 		if (opcode == 0xfc01)
1045ca93cee5SLoic Poulain 			inject_cmd_complete(hu->hdev, opcode);
1046ca93cee5SLoic Poulain 	}
1047ca93cee5SLoic Poulain 
1048ca93cee5SLoic Poulain 	/* Prepend skb with frame type */
1049618e8bc2SMarcel Holtmann 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
1050ca93cee5SLoic Poulain 
1051ca93cee5SLoic Poulain 	return skb;
1052ca93cee5SLoic Poulain }
1053ca93cee5SLoic Poulain 
1054ca93cee5SLoic Poulain static const struct hci_uart_proto intel_proto = {
1055ca93cee5SLoic Poulain 	.id		= HCI_UART_INTEL,
1056ca93cee5SLoic Poulain 	.name		= "Intel",
1057aee61f7aSMarcel Holtmann 	.manufacturer	= 2,
1058ca93cee5SLoic Poulain 	.init_speed	= 115200,
1059ff289559SLoic Poulain 	.oper_speed	= 3000000,
1060ca93cee5SLoic Poulain 	.open		= intel_open,
1061ca93cee5SLoic Poulain 	.close		= intel_close,
1062ca93cee5SLoic Poulain 	.flush		= intel_flush,
1063ca93cee5SLoic Poulain 	.setup		= intel_setup,
1064ff289559SLoic Poulain 	.set_baudrate	= intel_set_baudrate,
1065ca93cee5SLoic Poulain 	.recv		= intel_recv,
1066ca93cee5SLoic Poulain 	.enqueue	= intel_enqueue,
1067ca93cee5SLoic Poulain 	.dequeue	= intel_dequeue,
1068ca93cee5SLoic Poulain };
1069ca93cee5SLoic Poulain 
10701ab1f239SLoic Poulain #ifdef CONFIG_ACPI
10711ab1f239SLoic Poulain static const struct acpi_device_id intel_acpi_match[] = {
10721ab1f239SLoic Poulain 	{ "INT33E1", 0 },
10731ab1f239SLoic Poulain 	{ },
10741ab1f239SLoic Poulain };
10751ab1f239SLoic Poulain MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
10761ab1f239SLoic Poulain #endif
10771ab1f239SLoic Poulain 
107874cdad37SLoic Poulain #ifdef CONFIG_PM
1079f7552473SLoic Poulain static int intel_suspend_device(struct device *dev)
1080aa6802dfSLoic Poulain {
1081aa6802dfSLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1082aa6802dfSLoic Poulain 
1083aa6802dfSLoic Poulain 	mutex_lock(&idev->hu_lock);
1084aa6802dfSLoic Poulain 	if (idev->hu)
1085aa6802dfSLoic Poulain 		intel_lpm_suspend(idev->hu);
1086aa6802dfSLoic Poulain 	mutex_unlock(&idev->hu_lock);
1087aa6802dfSLoic Poulain 
1088aa6802dfSLoic Poulain 	return 0;
1089aa6802dfSLoic Poulain }
1090aa6802dfSLoic Poulain 
1091f7552473SLoic Poulain static int intel_resume_device(struct device *dev)
1092aa6802dfSLoic Poulain {
1093aa6802dfSLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1094aa6802dfSLoic Poulain 
1095aa6802dfSLoic Poulain 	mutex_lock(&idev->hu_lock);
1096aa6802dfSLoic Poulain 	if (idev->hu)
1097aa6802dfSLoic Poulain 		intel_lpm_resume(idev->hu);
1098aa6802dfSLoic Poulain 	mutex_unlock(&idev->hu_lock);
1099aa6802dfSLoic Poulain 
1100aa6802dfSLoic Poulain 	return 0;
1101aa6802dfSLoic Poulain }
1102aa6802dfSLoic Poulain #endif
1103aa6802dfSLoic Poulain 
1104f7552473SLoic Poulain #ifdef CONFIG_PM_SLEEP
1105f7552473SLoic Poulain static int intel_suspend(struct device *dev)
1106f7552473SLoic Poulain {
1107f7552473SLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1108f7552473SLoic Poulain 
1109f7552473SLoic Poulain 	if (device_may_wakeup(dev))
1110f7552473SLoic Poulain 		enable_irq_wake(idev->irq);
1111f7552473SLoic Poulain 
1112f7552473SLoic Poulain 	return intel_suspend_device(dev);
1113f7552473SLoic Poulain }
1114f7552473SLoic Poulain 
1115f7552473SLoic Poulain static int intel_resume(struct device *dev)
1116f7552473SLoic Poulain {
1117f7552473SLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1118f7552473SLoic Poulain 
1119f7552473SLoic Poulain 	if (device_may_wakeup(dev))
1120f7552473SLoic Poulain 		disable_irq_wake(idev->irq);
1121f7552473SLoic Poulain 
1122f7552473SLoic Poulain 	return intel_resume_device(dev);
1123f7552473SLoic Poulain }
1124f7552473SLoic Poulain #endif
1125f7552473SLoic Poulain 
1126aa6802dfSLoic Poulain static const struct dev_pm_ops intel_pm_ops = {
1127aa6802dfSLoic Poulain 	SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1128f7552473SLoic Poulain 	SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
1129aa6802dfSLoic Poulain };
1130aa6802dfSLoic Poulain 
11314a59d433SAndy Shevchenko static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
11324a59d433SAndy Shevchenko static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
11334a59d433SAndy Shevchenko 
11344a59d433SAndy Shevchenko static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
11354a59d433SAndy Shevchenko 	{ "reset-gpios", &reset_gpios, 1 },
11364a59d433SAndy Shevchenko 	{ "host-wake-gpios", &host_wake_gpios, 1 },
11374a59d433SAndy Shevchenko 	{ },
11384a59d433SAndy Shevchenko };
11394a59d433SAndy Shevchenko 
11401ab1f239SLoic Poulain static int intel_probe(struct platform_device *pdev)
11411ab1f239SLoic Poulain {
11421ab1f239SLoic Poulain 	struct intel_device *idev;
11434a59d433SAndy Shevchenko 	int ret;
11441ab1f239SLoic Poulain 
11451ab1f239SLoic Poulain 	idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
11461ab1f239SLoic Poulain 	if (!idev)
11471ab1f239SLoic Poulain 		return -ENOMEM;
11481ab1f239SLoic Poulain 
1149aa6802dfSLoic Poulain 	mutex_init(&idev->hu_lock);
1150aa6802dfSLoic Poulain 
11511ab1f239SLoic Poulain 	idev->pdev = pdev;
11521ab1f239SLoic Poulain 
11534a59d433SAndy Shevchenko 	ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
11544a59d433SAndy Shevchenko 	if (ret)
11554a59d433SAndy Shevchenko 		dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
11564a59d433SAndy Shevchenko 
115732b9ccbcSLoic Poulain 	idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
11581ab1f239SLoic Poulain 	if (IS_ERR(idev->reset)) {
11591ab1f239SLoic Poulain 		dev_err(&pdev->dev, "Unable to retrieve gpio\n");
11601ab1f239SLoic Poulain 		return PTR_ERR(idev->reset);
11611ab1f239SLoic Poulain 	}
11621ab1f239SLoic Poulain 
1163765ea3abSLoic Poulain 	idev->irq = platform_get_irq(pdev, 0);
1164765ea3abSLoic Poulain 	if (idev->irq < 0) {
1165765ea3abSLoic Poulain 		struct gpio_desc *host_wake;
1166765ea3abSLoic Poulain 
1167765ea3abSLoic Poulain 		dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1168765ea3abSLoic Poulain 
116932b9ccbcSLoic Poulain 		host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
1170765ea3abSLoic Poulain 		if (IS_ERR(host_wake)) {
1171765ea3abSLoic Poulain 			dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1172765ea3abSLoic Poulain 			goto no_irq;
1173765ea3abSLoic Poulain 		}
1174765ea3abSLoic Poulain 
1175765ea3abSLoic Poulain 		idev->irq = gpiod_to_irq(host_wake);
1176765ea3abSLoic Poulain 		if (idev->irq < 0) {
1177765ea3abSLoic Poulain 			dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1178765ea3abSLoic Poulain 			goto no_irq;
1179765ea3abSLoic Poulain 		}
1180765ea3abSLoic Poulain 	}
1181765ea3abSLoic Poulain 
1182765ea3abSLoic Poulain 	/* Only enable wake-up/irq when controller is powered */
1183765ea3abSLoic Poulain 	device_set_wakeup_capable(&pdev->dev, true);
1184765ea3abSLoic Poulain 	device_wakeup_disable(&pdev->dev);
1185765ea3abSLoic Poulain 
1186765ea3abSLoic Poulain no_irq:
11871ab1f239SLoic Poulain 	platform_set_drvdata(pdev, idev);
11881ab1f239SLoic Poulain 
11891ab1f239SLoic Poulain 	/* Place this instance on the device list */
119067c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
11911ab1f239SLoic Poulain 	list_add_tail(&idev->list, &intel_device_list);
119267c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
11931ab1f239SLoic Poulain 
1194765ea3abSLoic Poulain 	dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1195765ea3abSLoic Poulain 		 desc_to_gpio(idev->reset), idev->irq);
11961ab1f239SLoic Poulain 
11971ab1f239SLoic Poulain 	return 0;
11981ab1f239SLoic Poulain }
11991ab1f239SLoic Poulain 
12001ab1f239SLoic Poulain static int intel_remove(struct platform_device *pdev)
12011ab1f239SLoic Poulain {
12021ab1f239SLoic Poulain 	struct intel_device *idev = platform_get_drvdata(pdev);
12031ab1f239SLoic Poulain 
1204765ea3abSLoic Poulain 	device_wakeup_disable(&pdev->dev);
1205765ea3abSLoic Poulain 
120667c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
12071ab1f239SLoic Poulain 	list_del(&idev->list);
120867c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
12091ab1f239SLoic Poulain 
12101ab1f239SLoic Poulain 	dev_info(&pdev->dev, "unregistered.\n");
12111ab1f239SLoic Poulain 
12121ab1f239SLoic Poulain 	return 0;
12131ab1f239SLoic Poulain }
12141ab1f239SLoic Poulain 
12151ab1f239SLoic Poulain static struct platform_driver intel_driver = {
12161ab1f239SLoic Poulain 	.probe = intel_probe,
12171ab1f239SLoic Poulain 	.remove = intel_remove,
12181ab1f239SLoic Poulain 	.driver = {
12191ab1f239SLoic Poulain 		.name = "hci_intel",
12201ab1f239SLoic Poulain 		.acpi_match_table = ACPI_PTR(intel_acpi_match),
1221aa6802dfSLoic Poulain 		.pm = &intel_pm_ops,
12221ab1f239SLoic Poulain 	},
12231ab1f239SLoic Poulain };
12241ab1f239SLoic Poulain 
1225ca93cee5SLoic Poulain int __init intel_init(void)
1226ca93cee5SLoic Poulain {
12271ab1f239SLoic Poulain 	platform_driver_register(&intel_driver);
12281ab1f239SLoic Poulain 
1229ca93cee5SLoic Poulain 	return hci_uart_register_proto(&intel_proto);
1230ca93cee5SLoic Poulain }
1231ca93cee5SLoic Poulain 
1232ca93cee5SLoic Poulain int __exit intel_deinit(void)
1233ca93cee5SLoic Poulain {
12341ab1f239SLoic Poulain 	platform_driver_unregister(&intel_driver);
12351ab1f239SLoic Poulain 
1236ca93cee5SLoic Poulain 	return hci_uart_unregister_proto(&intel_proto);
1237ca93cee5SLoic Poulain }
1238