xref: /linux/drivers/bluetooth/hci_intel.c (revision 965651c16b9e6212f781d5b619ab78bd82bf54ce)
116e3887fSMarcel Holtmann /*
216e3887fSMarcel Holtmann  *
316e3887fSMarcel Holtmann  *  Bluetooth HCI UART driver for Intel devices
416e3887fSMarcel Holtmann  *
516e3887fSMarcel Holtmann  *  Copyright (C) 2015  Intel Corporation
616e3887fSMarcel Holtmann  *
716e3887fSMarcel Holtmann  *
816e3887fSMarcel Holtmann  *  This program is free software; you can redistribute it and/or modify
916e3887fSMarcel Holtmann  *  it under the terms of the GNU General Public License as published by
1016e3887fSMarcel Holtmann  *  the Free Software Foundation; either version 2 of the License, or
1116e3887fSMarcel Holtmann  *  (at your option) any later version.
1216e3887fSMarcel Holtmann  *
1316e3887fSMarcel Holtmann  *  This program is distributed in the hope that it will be useful,
1416e3887fSMarcel Holtmann  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1516e3887fSMarcel Holtmann  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1616e3887fSMarcel Holtmann  *  GNU General Public License for more details.
1716e3887fSMarcel Holtmann  *
1816e3887fSMarcel Holtmann  *  You should have received a copy of the GNU General Public License
1916e3887fSMarcel Holtmann  *  along with this program; if not, write to the Free Software
2016e3887fSMarcel Holtmann  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2116e3887fSMarcel Holtmann  *
2216e3887fSMarcel Holtmann  */
2316e3887fSMarcel Holtmann 
2416e3887fSMarcel Holtmann #include <linux/kernel.h>
2516e3887fSMarcel Holtmann #include <linux/errno.h>
2616e3887fSMarcel Holtmann #include <linux/skbuff.h>
27ca93cee5SLoic Poulain #include <linux/firmware.h>
281ab1f239SLoic Poulain #include <linux/module.h>
29ca93cee5SLoic Poulain #include <linux/wait.h>
301ab1f239SLoic Poulain #include <linux/tty.h>
311ab1f239SLoic Poulain #include <linux/platform_device.h>
321ab1f239SLoic Poulain #include <linux/gpio/consumer.h>
331ab1f239SLoic Poulain #include <linux/acpi.h>
34765ea3abSLoic Poulain #include <linux/interrupt.h>
3574cdad37SLoic Poulain #include <linux/pm_runtime.h>
3616e3887fSMarcel Holtmann 
3716e3887fSMarcel Holtmann #include <net/bluetooth/bluetooth.h>
3816e3887fSMarcel Holtmann #include <net/bluetooth/hci_core.h>
3916e3887fSMarcel Holtmann 
4016e3887fSMarcel Holtmann #include "hci_uart.h"
41ca93cee5SLoic Poulain #include "btintel.h"
42ca93cee5SLoic Poulain 
43ca93cee5SLoic Poulain #define STATE_BOOTLOADER	0
44ca93cee5SLoic Poulain #define STATE_DOWNLOADING	1
45ca93cee5SLoic Poulain #define STATE_FIRMWARE_LOADED	2
46ca93cee5SLoic Poulain #define STATE_FIRMWARE_FAILED	3
47ca93cee5SLoic Poulain #define STATE_BOOTING		4
48b98469f4SLoic Poulain #define STATE_LPM_ENABLED	5
49b98469f4SLoic Poulain #define STATE_TX_ACTIVE		6
5089436546SLoic Poulain #define STATE_SUSPENDED		7
5189436546SLoic Poulain #define STATE_LPM_TRANSACTION	8
52b98469f4SLoic Poulain 
5389436546SLoic Poulain #define HCI_LPM_WAKE_PKT 0xf0
54b98469f4SLoic Poulain #define HCI_LPM_PKT 0xf1
55b98469f4SLoic Poulain #define HCI_LPM_MAX_SIZE 10
56b98469f4SLoic Poulain #define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
57b98469f4SLoic Poulain 
58b98469f4SLoic Poulain #define LPM_OP_TX_NOTIFY 0x00
5989436546SLoic Poulain #define LPM_OP_SUSPEND_ACK 0x02
6089436546SLoic Poulain #define LPM_OP_RESUME_ACK 0x03
61b98469f4SLoic Poulain 
6274cdad37SLoic Poulain #define LPM_SUSPEND_DELAY_MS 1000
6374cdad37SLoic Poulain 
64b98469f4SLoic Poulain struct hci_lpm_pkt {
65b98469f4SLoic Poulain 	__u8 opcode;
66b98469f4SLoic Poulain 	__u8 dlen;
67b98469f4SLoic Poulain 	__u8 data[0];
68b98469f4SLoic Poulain } __packed;
69ca93cee5SLoic Poulain 
701ab1f239SLoic Poulain struct intel_device {
711ab1f239SLoic Poulain 	struct list_head list;
721ab1f239SLoic Poulain 	struct platform_device *pdev;
731ab1f239SLoic Poulain 	struct gpio_desc *reset;
74aa6802dfSLoic Poulain 	struct hci_uart *hu;
75aa6802dfSLoic Poulain 	struct mutex hu_lock;
76765ea3abSLoic Poulain 	int irq;
771ab1f239SLoic Poulain };
781ab1f239SLoic Poulain 
791ab1f239SLoic Poulain static LIST_HEAD(intel_device_list);
8067c8bde0SLoic Poulain static DEFINE_MUTEX(intel_device_list_lock);
811ab1f239SLoic Poulain 
82ca93cee5SLoic Poulain struct intel_data {
83ca93cee5SLoic Poulain 	struct sk_buff *rx_skb;
84ca93cee5SLoic Poulain 	struct sk_buff_head txq;
8574cdad37SLoic Poulain 	struct work_struct busy_work;
8674cdad37SLoic Poulain 	struct hci_uart *hu;
87ca93cee5SLoic Poulain 	unsigned long flags;
88ca93cee5SLoic Poulain };
89ca93cee5SLoic Poulain 
90ff289559SLoic Poulain static u8 intel_convert_speed(unsigned int speed)
91ff289559SLoic Poulain {
92ff289559SLoic Poulain 	switch (speed) {
93ff289559SLoic Poulain 	case 9600:
94ff289559SLoic Poulain 		return 0x00;
95ff289559SLoic Poulain 	case 19200:
96ff289559SLoic Poulain 		return 0x01;
97ff289559SLoic Poulain 	case 38400:
98ff289559SLoic Poulain 		return 0x02;
99ff289559SLoic Poulain 	case 57600:
100ff289559SLoic Poulain 		return 0x03;
101ff289559SLoic Poulain 	case 115200:
102ff289559SLoic Poulain 		return 0x04;
103ff289559SLoic Poulain 	case 230400:
104ff289559SLoic Poulain 		return 0x05;
105ff289559SLoic Poulain 	case 460800:
106ff289559SLoic Poulain 		return 0x06;
107ff289559SLoic Poulain 	case 921600:
108ff289559SLoic Poulain 		return 0x07;
109ff289559SLoic Poulain 	case 1843200:
110ff289559SLoic Poulain 		return 0x08;
111ff289559SLoic Poulain 	case 3250000:
112ff289559SLoic Poulain 		return 0x09;
113ff289559SLoic Poulain 	case 2000000:
114ff289559SLoic Poulain 		return 0x0a;
115ff289559SLoic Poulain 	case 3000000:
116ff289559SLoic Poulain 		return 0x0b;
117ff289559SLoic Poulain 	default:
118ff289559SLoic Poulain 		return 0xff;
119ff289559SLoic Poulain 	}
120ff289559SLoic Poulain }
121ff289559SLoic Poulain 
1221ab1f239SLoic Poulain static int intel_wait_booting(struct hci_uart *hu)
1231ab1f239SLoic Poulain {
1241ab1f239SLoic Poulain 	struct intel_data *intel = hu->priv;
1251ab1f239SLoic Poulain 	int err;
1261ab1f239SLoic Poulain 
1271ab1f239SLoic Poulain 	err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
1281ab1f239SLoic Poulain 				  TASK_INTERRUPTIBLE,
1291ab1f239SLoic Poulain 				  msecs_to_jiffies(1000));
1301ab1f239SLoic Poulain 
131f0a70a04SBart Van Assche 	if (err == -EINTR) {
132f44e78a5SLoic Poulain 		bt_dev_err(hu->hdev, "Device boot interrupted");
1331ab1f239SLoic Poulain 		return -EINTR;
1341ab1f239SLoic Poulain 	}
1351ab1f239SLoic Poulain 
1361ab1f239SLoic Poulain 	if (err) {
137f44e78a5SLoic Poulain 		bt_dev_err(hu->hdev, "Device boot timeout");
1381ab1f239SLoic Poulain 		return -ETIMEDOUT;
1391ab1f239SLoic Poulain 	}
1401ab1f239SLoic Poulain 
1411ab1f239SLoic Poulain 	return err;
1421ab1f239SLoic Poulain }
1431ab1f239SLoic Poulain 
144a9cb0fe4SLoic Poulain #ifdef CONFIG_PM
14589436546SLoic Poulain static int intel_wait_lpm_transaction(struct hci_uart *hu)
14689436546SLoic Poulain {
14789436546SLoic Poulain 	struct intel_data *intel = hu->priv;
14889436546SLoic Poulain 	int err;
14989436546SLoic Poulain 
15089436546SLoic Poulain 	err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
15189436546SLoic Poulain 				  TASK_INTERRUPTIBLE,
15289436546SLoic Poulain 				  msecs_to_jiffies(1000));
15389436546SLoic Poulain 
154f0a70a04SBart Van Assche 	if (err == -EINTR) {
15589436546SLoic Poulain 		bt_dev_err(hu->hdev, "LPM transaction interrupted");
15689436546SLoic Poulain 		return -EINTR;
15789436546SLoic Poulain 	}
15889436546SLoic Poulain 
15989436546SLoic Poulain 	if (err) {
16089436546SLoic Poulain 		bt_dev_err(hu->hdev, "LPM transaction timeout");
16189436546SLoic Poulain 		return -ETIMEDOUT;
16289436546SLoic Poulain 	}
16389436546SLoic Poulain 
16489436546SLoic Poulain 	return err;
16589436546SLoic Poulain }
16689436546SLoic Poulain 
16789436546SLoic Poulain static int intel_lpm_suspend(struct hci_uart *hu)
16889436546SLoic Poulain {
16989436546SLoic Poulain 	static const u8 suspend[] = { 0x01, 0x01, 0x01 };
17089436546SLoic Poulain 	struct intel_data *intel = hu->priv;
17189436546SLoic Poulain 	struct sk_buff *skb;
17289436546SLoic Poulain 
17389436546SLoic Poulain 	if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
17489436546SLoic Poulain 	    test_bit(STATE_SUSPENDED, &intel->flags))
17589436546SLoic Poulain 		return 0;
17689436546SLoic Poulain 
17789436546SLoic Poulain 	if (test_bit(STATE_TX_ACTIVE, &intel->flags))
17889436546SLoic Poulain 		return -EAGAIN;
17989436546SLoic Poulain 
18089436546SLoic Poulain 	bt_dev_dbg(hu->hdev, "Suspending");
18189436546SLoic Poulain 
18289436546SLoic Poulain 	skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
18389436546SLoic Poulain 	if (!skb) {
18489436546SLoic Poulain 		bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
18589436546SLoic Poulain 		return -ENOMEM;
18689436546SLoic Poulain 	}
18789436546SLoic Poulain 
18859ae1d12SJohannes Berg 	skb_put_data(skb, suspend, sizeof(suspend));
189618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_LPM_PKT;
19089436546SLoic Poulain 
19189436546SLoic Poulain 	set_bit(STATE_LPM_TRANSACTION, &intel->flags);
19289436546SLoic Poulain 
19330e945fbSLoic Poulain 	/* LPM flow is a priority, enqueue packet at list head */
19430e945fbSLoic Poulain 	skb_queue_head(&intel->txq, skb);
19589436546SLoic Poulain 	hci_uart_tx_wakeup(hu);
19689436546SLoic Poulain 
19789436546SLoic Poulain 	intel_wait_lpm_transaction(hu);
19889436546SLoic Poulain 	/* Even in case of failure, continue and test the suspended flag */
19989436546SLoic Poulain 
20089436546SLoic Poulain 	clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
20189436546SLoic Poulain 
20289436546SLoic Poulain 	if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
20389436546SLoic Poulain 		bt_dev_err(hu->hdev, "Device suspend error");
20489436546SLoic Poulain 		return -EINVAL;
20589436546SLoic Poulain 	}
20689436546SLoic Poulain 
20789436546SLoic Poulain 	bt_dev_dbg(hu->hdev, "Suspended");
20889436546SLoic Poulain 
20989436546SLoic Poulain 	hci_uart_set_flow_control(hu, true);
21089436546SLoic Poulain 
21189436546SLoic Poulain 	return 0;
21289436546SLoic Poulain }
21389436546SLoic Poulain 
21489436546SLoic Poulain static int intel_lpm_resume(struct hci_uart *hu)
21589436546SLoic Poulain {
21689436546SLoic Poulain 	struct intel_data *intel = hu->priv;
21789436546SLoic Poulain 	struct sk_buff *skb;
21889436546SLoic Poulain 
21989436546SLoic Poulain 	if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
22089436546SLoic Poulain 	    !test_bit(STATE_SUSPENDED, &intel->flags))
22189436546SLoic Poulain 		return 0;
22289436546SLoic Poulain 
22389436546SLoic Poulain 	bt_dev_dbg(hu->hdev, "Resuming");
22489436546SLoic Poulain 
22589436546SLoic Poulain 	hci_uart_set_flow_control(hu, false);
22689436546SLoic Poulain 
22789436546SLoic Poulain 	skb = bt_skb_alloc(0, GFP_KERNEL);
22889436546SLoic Poulain 	if (!skb) {
22989436546SLoic Poulain 		bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
23089436546SLoic Poulain 		return -ENOMEM;
23189436546SLoic Poulain 	}
23289436546SLoic Poulain 
233618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
23489436546SLoic Poulain 
23589436546SLoic Poulain 	set_bit(STATE_LPM_TRANSACTION, &intel->flags);
23689436546SLoic Poulain 
23730e945fbSLoic Poulain 	/* LPM flow is a priority, enqueue packet at list head */
23830e945fbSLoic Poulain 	skb_queue_head(&intel->txq, skb);
23989436546SLoic Poulain 	hci_uart_tx_wakeup(hu);
24089436546SLoic Poulain 
24189436546SLoic Poulain 	intel_wait_lpm_transaction(hu);
24289436546SLoic Poulain 	/* Even in case of failure, continue and test the suspended flag */
24389436546SLoic Poulain 
24489436546SLoic Poulain 	clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
24589436546SLoic Poulain 
24689436546SLoic Poulain 	if (test_bit(STATE_SUSPENDED, &intel->flags)) {
24789436546SLoic Poulain 		bt_dev_err(hu->hdev, "Device resume error");
24889436546SLoic Poulain 		return -EINVAL;
24989436546SLoic Poulain 	}
25089436546SLoic Poulain 
25189436546SLoic Poulain 	bt_dev_dbg(hu->hdev, "Resumed");
25289436546SLoic Poulain 
25389436546SLoic Poulain 	return 0;
25489436546SLoic Poulain }
255a9cb0fe4SLoic Poulain #endif /* CONFIG_PM */
25689436546SLoic Poulain 
25789436546SLoic Poulain static int intel_lpm_host_wake(struct hci_uart *hu)
25889436546SLoic Poulain {
25989436546SLoic Poulain 	static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
26089436546SLoic Poulain 	struct intel_data *intel = hu->priv;
26189436546SLoic Poulain 	struct sk_buff *skb;
26289436546SLoic Poulain 
26389436546SLoic Poulain 	hci_uart_set_flow_control(hu, false);
26489436546SLoic Poulain 
26589436546SLoic Poulain 	clear_bit(STATE_SUSPENDED, &intel->flags);
26689436546SLoic Poulain 
26789436546SLoic Poulain 	skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
26889436546SLoic Poulain 	if (!skb) {
26989436546SLoic Poulain 		bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
27089436546SLoic Poulain 		return -ENOMEM;
27189436546SLoic Poulain 	}
27289436546SLoic Poulain 
27359ae1d12SJohannes Berg 	skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
274618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_LPM_PKT;
27589436546SLoic Poulain 
27630e945fbSLoic Poulain 	/* LPM flow is a priority, enqueue packet at list head */
27730e945fbSLoic Poulain 	skb_queue_head(&intel->txq, skb);
27889436546SLoic Poulain 	hci_uart_tx_wakeup(hu);
27989436546SLoic Poulain 
28089436546SLoic Poulain 	bt_dev_dbg(hu->hdev, "Resumed by controller");
28189436546SLoic Poulain 
28289436546SLoic Poulain 	return 0;
28389436546SLoic Poulain }
28489436546SLoic Poulain 
285765ea3abSLoic Poulain static irqreturn_t intel_irq(int irq, void *dev_id)
286765ea3abSLoic Poulain {
287765ea3abSLoic Poulain 	struct intel_device *idev = dev_id;
288765ea3abSLoic Poulain 
289765ea3abSLoic Poulain 	dev_info(&idev->pdev->dev, "hci_intel irq\n");
290765ea3abSLoic Poulain 
291aa6802dfSLoic Poulain 	mutex_lock(&idev->hu_lock);
292aa6802dfSLoic Poulain 	if (idev->hu)
293aa6802dfSLoic Poulain 		intel_lpm_host_wake(idev->hu);
294aa6802dfSLoic Poulain 	mutex_unlock(&idev->hu_lock);
295aa6802dfSLoic Poulain 
29674cdad37SLoic Poulain 	/* Host/Controller are now LPM resumed, trigger a new delayed suspend */
29774cdad37SLoic Poulain 	pm_runtime_get(&idev->pdev->dev);
29874cdad37SLoic Poulain 	pm_runtime_mark_last_busy(&idev->pdev->dev);
29974cdad37SLoic Poulain 	pm_runtime_put_autosuspend(&idev->pdev->dev);
30074cdad37SLoic Poulain 
301765ea3abSLoic Poulain 	return IRQ_HANDLED;
302765ea3abSLoic Poulain }
303765ea3abSLoic Poulain 
3041ab1f239SLoic Poulain static int intel_set_power(struct hci_uart *hu, bool powered)
3051ab1f239SLoic Poulain {
3061ab1f239SLoic Poulain 	struct list_head *p;
3071ab1f239SLoic Poulain 	int err = -ENODEV;
3081ab1f239SLoic Poulain 
309dcb9cfaaSJohan Hovold 	if (!hu->tty->dev)
310dcb9cfaaSJohan Hovold 		return err;
311dcb9cfaaSJohan Hovold 
31267c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
3131ab1f239SLoic Poulain 
3141ab1f239SLoic Poulain 	list_for_each(p, &intel_device_list) {
3151ab1f239SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
3161ab1f239SLoic Poulain 						       list);
3171ab1f239SLoic Poulain 
3181ab1f239SLoic Poulain 		/* tty device and pdev device should share the same parent
3191ab1f239SLoic Poulain 		 * which is the UART port.
3201ab1f239SLoic Poulain 		 */
3211ab1f239SLoic Poulain 		if (hu->tty->dev->parent != idev->pdev->dev.parent)
3221ab1f239SLoic Poulain 			continue;
3231ab1f239SLoic Poulain 
3241ab1f239SLoic Poulain 		if (!idev->reset) {
3251ab1f239SLoic Poulain 			err = -ENOTSUPP;
3261ab1f239SLoic Poulain 			break;
3271ab1f239SLoic Poulain 		}
3281ab1f239SLoic Poulain 
3291ab1f239SLoic Poulain 		BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
3301ab1f239SLoic Poulain 			hu, dev_name(&idev->pdev->dev), powered);
3311ab1f239SLoic Poulain 
3321ab1f239SLoic Poulain 		gpiod_set_value(idev->reset, powered);
333765ea3abSLoic Poulain 
334aa6802dfSLoic Poulain 		/* Provide to idev a hu reference which is used to run LPM
335aa6802dfSLoic Poulain 		 * transactions (lpm suspend/resume) from PM callbacks.
336aa6802dfSLoic Poulain 		 * hu needs to be protected against concurrent removing during
337aa6802dfSLoic Poulain 		 * these PM ops.
338aa6802dfSLoic Poulain 		 */
339aa6802dfSLoic Poulain 		mutex_lock(&idev->hu_lock);
340aa6802dfSLoic Poulain 		idev->hu = powered ? hu : NULL;
341aa6802dfSLoic Poulain 		mutex_unlock(&idev->hu_lock);
342aa6802dfSLoic Poulain 
343765ea3abSLoic Poulain 		if (idev->irq < 0)
344765ea3abSLoic Poulain 			break;
345765ea3abSLoic Poulain 
346765ea3abSLoic Poulain 		if (powered && device_can_wakeup(&idev->pdev->dev)) {
347765ea3abSLoic Poulain 			err = devm_request_threaded_irq(&idev->pdev->dev,
348765ea3abSLoic Poulain 							idev->irq, NULL,
349765ea3abSLoic Poulain 							intel_irq,
350765ea3abSLoic Poulain 							IRQF_ONESHOT,
351765ea3abSLoic Poulain 							"bt-host-wake", idev);
352765ea3abSLoic Poulain 			if (err) {
353765ea3abSLoic Poulain 				BT_ERR("hu %p, unable to allocate irq-%d",
354765ea3abSLoic Poulain 				       hu, idev->irq);
355765ea3abSLoic Poulain 				break;
356765ea3abSLoic Poulain 			}
357765ea3abSLoic Poulain 
358765ea3abSLoic Poulain 			device_wakeup_enable(&idev->pdev->dev);
35974cdad37SLoic Poulain 
36074cdad37SLoic Poulain 			pm_runtime_set_active(&idev->pdev->dev);
36174cdad37SLoic Poulain 			pm_runtime_use_autosuspend(&idev->pdev->dev);
36274cdad37SLoic Poulain 			pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
36374cdad37SLoic Poulain 							 LPM_SUSPEND_DELAY_MS);
36474cdad37SLoic Poulain 			pm_runtime_enable(&idev->pdev->dev);
365765ea3abSLoic Poulain 		} else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
366765ea3abSLoic Poulain 			devm_free_irq(&idev->pdev->dev, idev->irq, idev);
367765ea3abSLoic Poulain 			device_wakeup_disable(&idev->pdev->dev);
36874cdad37SLoic Poulain 
36974cdad37SLoic Poulain 			pm_runtime_disable(&idev->pdev->dev);
370765ea3abSLoic Poulain 		}
3711ab1f239SLoic Poulain 	}
3721ab1f239SLoic Poulain 
37367c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
3741ab1f239SLoic Poulain 
3751ab1f239SLoic Poulain 	return err;
3761ab1f239SLoic Poulain }
3771ab1f239SLoic Poulain 
37874cdad37SLoic Poulain static void intel_busy_work(struct work_struct *work)
37974cdad37SLoic Poulain {
38074cdad37SLoic Poulain 	struct list_head *p;
38174cdad37SLoic Poulain 	struct intel_data *intel = container_of(work, struct intel_data,
38274cdad37SLoic Poulain 						busy_work);
38374cdad37SLoic Poulain 
384dcb9cfaaSJohan Hovold 	if (!intel->hu->tty->dev)
385dcb9cfaaSJohan Hovold 		return;
386dcb9cfaaSJohan Hovold 
38774cdad37SLoic Poulain 	/* Link is busy, delay the suspend */
38874cdad37SLoic Poulain 	mutex_lock(&intel_device_list_lock);
38974cdad37SLoic Poulain 	list_for_each(p, &intel_device_list) {
39074cdad37SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
39174cdad37SLoic Poulain 						       list);
39274cdad37SLoic Poulain 
39374cdad37SLoic Poulain 		if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
39474cdad37SLoic Poulain 			pm_runtime_get(&idev->pdev->dev);
39574cdad37SLoic Poulain 			pm_runtime_mark_last_busy(&idev->pdev->dev);
39674cdad37SLoic Poulain 			pm_runtime_put_autosuspend(&idev->pdev->dev);
39774cdad37SLoic Poulain 			break;
39874cdad37SLoic Poulain 		}
39974cdad37SLoic Poulain 	}
40074cdad37SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
40174cdad37SLoic Poulain }
40274cdad37SLoic Poulain 
403ca93cee5SLoic Poulain static int intel_open(struct hci_uart *hu)
404ca93cee5SLoic Poulain {
405ca93cee5SLoic Poulain 	struct intel_data *intel;
406ca93cee5SLoic Poulain 
407ca93cee5SLoic Poulain 	BT_DBG("hu %p", hu);
408ca93cee5SLoic Poulain 
409ca93cee5SLoic Poulain 	intel = kzalloc(sizeof(*intel), GFP_KERNEL);
410ca93cee5SLoic Poulain 	if (!intel)
411ca93cee5SLoic Poulain 		return -ENOMEM;
412ca93cee5SLoic Poulain 
413ca93cee5SLoic Poulain 	skb_queue_head_init(&intel->txq);
41474cdad37SLoic Poulain 	INIT_WORK(&intel->busy_work, intel_busy_work);
41574cdad37SLoic Poulain 
41674cdad37SLoic Poulain 	intel->hu = hu;
417ca93cee5SLoic Poulain 
418ca93cee5SLoic Poulain 	hu->priv = intel;
4191ab1f239SLoic Poulain 
4201ab1f239SLoic Poulain 	if (!intel_set_power(hu, true))
4211ab1f239SLoic Poulain 		set_bit(STATE_BOOTING, &intel->flags);
4221ab1f239SLoic Poulain 
423ca93cee5SLoic Poulain 	return 0;
424ca93cee5SLoic Poulain }
425ca93cee5SLoic Poulain 
426ca93cee5SLoic Poulain static int intel_close(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 
43274cdad37SLoic Poulain 	cancel_work_sync(&intel->busy_work);
43374cdad37SLoic Poulain 
4341ab1f239SLoic Poulain 	intel_set_power(hu, false);
4351ab1f239SLoic Poulain 
436ca93cee5SLoic Poulain 	skb_queue_purge(&intel->txq);
437ca93cee5SLoic Poulain 	kfree_skb(intel->rx_skb);
438ca93cee5SLoic Poulain 	kfree(intel);
439ca93cee5SLoic Poulain 
440ca93cee5SLoic Poulain 	hu->priv = NULL;
441ca93cee5SLoic Poulain 	return 0;
442ca93cee5SLoic Poulain }
443ca93cee5SLoic Poulain 
444ca93cee5SLoic Poulain static int intel_flush(struct hci_uart *hu)
445ca93cee5SLoic Poulain {
446ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
447ca93cee5SLoic Poulain 
448ca93cee5SLoic Poulain 	BT_DBG("hu %p", hu);
449ca93cee5SLoic Poulain 
450ca93cee5SLoic Poulain 	skb_queue_purge(&intel->txq);
451ca93cee5SLoic Poulain 
452ca93cee5SLoic Poulain 	return 0;
453ca93cee5SLoic Poulain }
454ca93cee5SLoic Poulain 
455ca93cee5SLoic Poulain static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
456ca93cee5SLoic Poulain {
457ca93cee5SLoic Poulain 	struct sk_buff *skb;
458ca93cee5SLoic Poulain 	struct hci_event_hdr *hdr;
459ca93cee5SLoic Poulain 	struct hci_ev_cmd_complete *evt;
460ca93cee5SLoic Poulain 
461ca93cee5SLoic Poulain 	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
462ca93cee5SLoic Poulain 	if (!skb)
463ca93cee5SLoic Poulain 		return -ENOMEM;
464ca93cee5SLoic Poulain 
4654df864c1SJohannes Berg 	hdr = skb_put(skb, sizeof(*hdr));
466ca93cee5SLoic Poulain 	hdr->evt = HCI_EV_CMD_COMPLETE;
467ca93cee5SLoic Poulain 	hdr->plen = sizeof(*evt) + 1;
468ca93cee5SLoic Poulain 
4694df864c1SJohannes Berg 	evt = skb_put(skb, sizeof(*evt));
470ca93cee5SLoic Poulain 	evt->ncmd = 0x01;
471ca93cee5SLoic Poulain 	evt->opcode = cpu_to_le16(opcode);
472ca93cee5SLoic Poulain 
473634fef61SJohannes Berg 	skb_put_u8(skb, 0x00);
474ca93cee5SLoic Poulain 
475618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
476ca93cee5SLoic Poulain 
477ca93cee5SLoic Poulain 	return hci_recv_frame(hdev, skb);
478ca93cee5SLoic Poulain }
479ca93cee5SLoic Poulain 
480ff289559SLoic Poulain static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
481ff289559SLoic Poulain {
482ff289559SLoic Poulain 	struct intel_data *intel = hu->priv;
483ff289559SLoic Poulain 	struct hci_dev *hdev = hu->hdev;
484ff289559SLoic Poulain 	u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
485ff289559SLoic Poulain 	struct sk_buff *skb;
4861ab1f239SLoic Poulain 	int err;
4871ab1f239SLoic Poulain 
4881ab1f239SLoic Poulain 	/* This can be the first command sent to the chip, check
4891ab1f239SLoic Poulain 	 * that the controller is ready.
4901ab1f239SLoic Poulain 	 */
4911ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
4921ab1f239SLoic Poulain 
4931ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
4941ab1f239SLoic Poulain 
4951ab1f239SLoic Poulain 	/* In case of timeout, try to continue anyway */
4962be1149eSAnton Protopopov 	if (err && err != -ETIMEDOUT)
4971ab1f239SLoic Poulain 		return err;
498ff289559SLoic Poulain 
499f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Change controller speed to %d", speed);
500ff289559SLoic Poulain 
501ff289559SLoic Poulain 	speed_cmd[3] = intel_convert_speed(speed);
502ff289559SLoic Poulain 	if (speed_cmd[3] == 0xff) {
503f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported speed");
504ff289559SLoic Poulain 		return -EINVAL;
505ff289559SLoic Poulain 	}
506ff289559SLoic Poulain 
507ff289559SLoic Poulain 	/* Device will not accept speed change if Intel version has not been
508ff289559SLoic Poulain 	 * previously requested.
509ff289559SLoic Poulain 	 */
510a0c38245SLoic Poulain 	skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
511ff289559SLoic Poulain 	if (IS_ERR(skb)) {
512f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
513f44e78a5SLoic Poulain 			   PTR_ERR(skb));
514ff289559SLoic Poulain 		return PTR_ERR(skb);
515ff289559SLoic Poulain 	}
516ff289559SLoic Poulain 	kfree_skb(skb);
517ff289559SLoic Poulain 
518ff289559SLoic Poulain 	skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
519ff289559SLoic Poulain 	if (!skb) {
520f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
521ff289559SLoic Poulain 		return -ENOMEM;
522ff289559SLoic Poulain 	}
523ff289559SLoic Poulain 
52459ae1d12SJohannes Berg 	skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
525618e8bc2SMarcel Holtmann 	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
526ff289559SLoic Poulain 
527ff289559SLoic Poulain 	hci_uart_set_flow_control(hu, true);
528ff289559SLoic Poulain 
529ff289559SLoic Poulain 	skb_queue_tail(&intel->txq, skb);
530ff289559SLoic Poulain 	hci_uart_tx_wakeup(hu);
531ff289559SLoic Poulain 
532ff289559SLoic Poulain 	/* wait 100ms to change baudrate on controller side */
533ff289559SLoic Poulain 	msleep(100);
534ff289559SLoic Poulain 
535ff289559SLoic Poulain 	hci_uart_set_baudrate(hu, speed);
536ff289559SLoic Poulain 	hci_uart_set_flow_control(hu, false);
537ff289559SLoic Poulain 
538ff289559SLoic Poulain 	return 0;
539ff289559SLoic Poulain }
540ff289559SLoic Poulain 
541ca93cee5SLoic Poulain static int intel_setup(struct hci_uart *hu)
542ca93cee5SLoic Poulain {
543ca93cee5SLoic Poulain 	static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
544ca93cee5SLoic Poulain 					  0x00, 0x08, 0x04, 0x00 };
545ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
546ca93cee5SLoic Poulain 	struct hci_dev *hdev = hu->hdev;
547ca93cee5SLoic Poulain 	struct sk_buff *skb;
5486c483de1SLoic Poulain 	struct intel_version ver;
549ca93cee5SLoic Poulain 	struct intel_boot_params *params;
550b98469f4SLoic Poulain 	struct list_head *p;
551ca93cee5SLoic Poulain 	const struct firmware *fw;
552ca93cee5SLoic Poulain 	const u8 *fw_ptr;
553ca93cee5SLoic Poulain 	char fwname[64];
554ca93cee5SLoic Poulain 	u32 frag_len;
555ca93cee5SLoic Poulain 	ktime_t calltime, delta, rettime;
556ca93cee5SLoic Poulain 	unsigned long long duration;
557ff289559SLoic Poulain 	unsigned int init_speed, oper_speed;
558ff289559SLoic Poulain 	int speed_change = 0;
559ca93cee5SLoic Poulain 	int err;
560ca93cee5SLoic Poulain 
561f44e78a5SLoic Poulain 	bt_dev_dbg(hdev, "start intel_setup");
562ca93cee5SLoic Poulain 
5636d2e50d2SMarcel Holtmann 	hu->hdev->set_diag = btintel_set_diag;
56435ab8150SMarcel Holtmann 	hu->hdev->set_bdaddr = btintel_set_bdaddr;
56535ab8150SMarcel Holtmann 
566ca93cee5SLoic Poulain 	calltime = ktime_get();
567ca93cee5SLoic Poulain 
568ff289559SLoic Poulain 	if (hu->init_speed)
569ff289559SLoic Poulain 		init_speed = hu->init_speed;
570ff289559SLoic Poulain 	else
571ff289559SLoic Poulain 		init_speed = hu->proto->init_speed;
572ff289559SLoic Poulain 
573ff289559SLoic Poulain 	if (hu->oper_speed)
574ff289559SLoic Poulain 		oper_speed = hu->oper_speed;
575ff289559SLoic Poulain 	else
576ff289559SLoic Poulain 		oper_speed = hu->proto->oper_speed;
577ff289559SLoic Poulain 
578ff289559SLoic Poulain 	if (oper_speed && init_speed && oper_speed != init_speed)
579ff289559SLoic Poulain 		speed_change = 1;
580ff289559SLoic Poulain 
5811ab1f239SLoic Poulain 	/* Check that the controller is ready */
5821ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
5831ab1f239SLoic Poulain 
5841ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
5851ab1f239SLoic Poulain 
5861ab1f239SLoic Poulain 	/* In case of timeout, try to continue anyway */
5872be1149eSAnton Protopopov 	if (err && err != -ETIMEDOUT)
5881ab1f239SLoic Poulain 		return err;
5891ab1f239SLoic Poulain 
590ca93cee5SLoic Poulain 	set_bit(STATE_BOOTLOADER, &intel->flags);
591ca93cee5SLoic Poulain 
592ca93cee5SLoic Poulain 	/* Read the Intel version information to determine if the device
593ca93cee5SLoic Poulain 	 * is in bootloader mode or if it already has operational firmware
594ca93cee5SLoic Poulain 	 * loaded.
595ca93cee5SLoic Poulain 	 */
5966c483de1SLoic Poulain 	 err = btintel_read_version(hdev, &ver);
5976c483de1SLoic Poulain 	 if (err)
598ca93cee5SLoic Poulain 		return err;
599ca93cee5SLoic Poulain 
600ca93cee5SLoic Poulain 	/* The hardware platform number has a fixed value of 0x37 and
601ca93cee5SLoic Poulain 	 * for now only accept this single value.
602ca93cee5SLoic Poulain 	 */
6036c483de1SLoic Poulain 	if (ver.hw_platform != 0x37) {
604f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
6056c483de1SLoic Poulain 			   ver.hw_platform);
606ca93cee5SLoic Poulain 		return -EINVAL;
607ca93cee5SLoic Poulain 	}
608ca93cee5SLoic Poulain 
6099268834bSTedd Ho-Jeong An         /* Check for supported iBT hardware variants of this firmware
6109268834bSTedd Ho-Jeong An          * loading method.
6119268834bSTedd Ho-Jeong An          *
6129268834bSTedd Ho-Jeong An          * This check has been put in place to ensure correct forward
6139268834bSTedd Ho-Jeong An          * compatibility options when newer hardware variants come along.
614ca93cee5SLoic Poulain          */
6159268834bSTedd Ho-Jeong An 	switch (ver.hw_variant) {
6169268834bSTedd Ho-Jeong An 	case 0x0b:	/* LnP */
6179268834bSTedd Ho-Jeong An 	case 0x0c:	/* WsP */
6186c7bb7ebSTedd Ho-Jeong An 	case 0x12:	/* ThP */
6199268834bSTedd Ho-Jeong An 		break;
6209268834bSTedd Ho-Jeong An 	default:
621f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
6226c483de1SLoic Poulain 			   ver.hw_variant);
623ca93cee5SLoic Poulain 		return -EINVAL;
624ca93cee5SLoic Poulain 	}
625ca93cee5SLoic Poulain 
6266c483de1SLoic Poulain 	btintel_version_info(hdev, &ver);
627ca93cee5SLoic Poulain 
628ca93cee5SLoic Poulain 	/* The firmware variant determines if the device is in bootloader
629ca93cee5SLoic Poulain 	 * mode or is running operational firmware. The value 0x06 identifies
630ca93cee5SLoic Poulain 	 * the bootloader and the value 0x23 identifies the operational
631ca93cee5SLoic Poulain 	 * firmware.
632ca93cee5SLoic Poulain 	 *
633ca93cee5SLoic Poulain 	 * When the operational firmware is already present, then only
634ca93cee5SLoic Poulain 	 * the check for valid Bluetooth device address is needed. This
635ca93cee5SLoic Poulain 	 * determines if the device will be added as configured or
636ca93cee5SLoic Poulain 	 * unconfigured controller.
637ca93cee5SLoic Poulain 	 *
638ca93cee5SLoic Poulain 	 * It is not possible to use the Secure Boot Parameters in this
639ca93cee5SLoic Poulain 	 * case since that command is only available in bootloader mode.
640ca93cee5SLoic Poulain 	 */
6416c483de1SLoic Poulain 	if (ver.fw_variant == 0x23) {
642ca93cee5SLoic Poulain 		clear_bit(STATE_BOOTLOADER, &intel->flags);
643ca93cee5SLoic Poulain 		btintel_check_bdaddr(hdev);
644ca93cee5SLoic Poulain 		return 0;
645ca93cee5SLoic Poulain 	}
646ca93cee5SLoic Poulain 
647ca93cee5SLoic Poulain 	/* If the device is not in bootloader mode, then the only possible
648ca93cee5SLoic Poulain 	 * choice is to return an error and abort the device initialization.
649ca93cee5SLoic Poulain 	 */
6506c483de1SLoic Poulain 	if (ver.fw_variant != 0x06) {
651f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
6526c483de1SLoic Poulain 			   ver.fw_variant);
653ca93cee5SLoic Poulain 		return -ENODEV;
654ca93cee5SLoic Poulain 	}
655ca93cee5SLoic Poulain 
656ca93cee5SLoic Poulain 	/* Read the secure boot parameters to identify the operating
657ca93cee5SLoic Poulain 	 * details of the bootloader.
658ca93cee5SLoic Poulain 	 */
659a0c38245SLoic Poulain 	skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_CMD_TIMEOUT);
660ca93cee5SLoic Poulain 	if (IS_ERR(skb)) {
661f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
662f44e78a5SLoic Poulain 			   PTR_ERR(skb));
663ca93cee5SLoic Poulain 		return PTR_ERR(skb);
664ca93cee5SLoic Poulain 	}
665ca93cee5SLoic Poulain 
666ca93cee5SLoic Poulain 	if (skb->len != sizeof(*params)) {
667f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Intel boot parameters size mismatch");
668ca93cee5SLoic Poulain 		kfree_skb(skb);
669ca93cee5SLoic Poulain 		return -EILSEQ;
670ca93cee5SLoic Poulain 	}
671ca93cee5SLoic Poulain 
672ca93cee5SLoic Poulain 	params = (struct intel_boot_params *)skb->data;
673ca93cee5SLoic Poulain 	if (params->status) {
674f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
675f44e78a5SLoic Poulain 			   params->status);
676ca93cee5SLoic Poulain 		err = -bt_to_errno(params->status);
677ca93cee5SLoic Poulain 		kfree_skb(skb);
678ca93cee5SLoic Poulain 		return err;
679ca93cee5SLoic Poulain 	}
680ca93cee5SLoic Poulain 
681f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Device revision is %u",
682ca93cee5SLoic Poulain 		    le16_to_cpu(params->dev_revid));
683ca93cee5SLoic Poulain 
684f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Secure boot is %s",
685ca93cee5SLoic Poulain 		    params->secure_boot ? "enabled" : "disabled");
686ca93cee5SLoic Poulain 
687f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
688ca93cee5SLoic Poulain 		params->min_fw_build_nn, params->min_fw_build_cw,
689ca93cee5SLoic Poulain 		2000 + params->min_fw_build_yy);
690ca93cee5SLoic Poulain 
691ca93cee5SLoic Poulain 	/* It is required that every single firmware fragment is acknowledged
692ca93cee5SLoic Poulain 	 * with a command complete event. If the boot parameters indicate
693ca93cee5SLoic Poulain 	 * that this bootloader does not send them, then abort the setup.
694ca93cee5SLoic Poulain 	 */
695ca93cee5SLoic Poulain 	if (params->limited_cce != 0x00) {
696f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
697f44e78a5SLoic Poulain 			   params->limited_cce);
698ca93cee5SLoic Poulain 		kfree_skb(skb);
699ca93cee5SLoic Poulain 		return -EINVAL;
700ca93cee5SLoic Poulain 	}
701ca93cee5SLoic Poulain 
702ca93cee5SLoic Poulain 	/* If the OTP has no valid Bluetooth device address, then there will
703ca93cee5SLoic Poulain 	 * also be no valid address for the operational firmware.
704ca93cee5SLoic Poulain 	 */
705ca93cee5SLoic Poulain 	if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
706f44e78a5SLoic Poulain 		bt_dev_info(hdev, "No device address configured");
707ca93cee5SLoic Poulain 		set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
708ca93cee5SLoic Poulain 	}
709ca93cee5SLoic Poulain 
710ca93cee5SLoic Poulain 	/* With this Intel bootloader only the hardware variant and device
711*965651c1STedd Ho-Jeong An 	 * revision information are used to select the right firmware for SfP
712*965651c1STedd Ho-Jeong An 	 * and WsP.
713ca93cee5SLoic Poulain 	 *
714b7da6a69STedd Ho-Jeong An 	 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
715b7da6a69STedd Ho-Jeong An 	 *
716b7da6a69STedd Ho-Jeong An 	 * Currently the supported hardware variants are:
717b7da6a69STedd Ho-Jeong An 	 *   11 (0x0b) for iBT 3.0 (LnP/SfP)
718*965651c1STedd Ho-Jeong An 	 *   12 (0x0c) for iBT 3.5 (WsP)
719*965651c1STedd Ho-Jeong An 	 *
720*965651c1STedd Ho-Jeong An 	 * For ThP/JfP and for future SKU's, the FW name varies based on HW
721*965651c1STedd Ho-Jeong An 	 * variant, HW revision and FW revision, as these are dependent on CNVi
722*965651c1STedd Ho-Jeong An 	 * and RF Combination.
723*965651c1STedd Ho-Jeong An 	 *
724*965651c1STedd Ho-Jeong An 	 *   18 (0x12) for iBT3.5 (ThP/JfP)
725*965651c1STedd Ho-Jeong An 	 *
726*965651c1STedd Ho-Jeong An 	 * The firmware file name for these will be
727*965651c1STedd Ho-Jeong An 	 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
728*965651c1STedd Ho-Jeong An 	 *
729ca93cee5SLoic Poulain 	 */
730*965651c1STedd Ho-Jeong An 	switch (ver.hw_variant) {
731*965651c1STedd Ho-Jeong An 	case 0x0b:      /* SfP */
732*965651c1STedd Ho-Jeong An 	case 0x0c:      /* WsP */
733b7da6a69STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
734b7da6a69STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_variant),
735ca93cee5SLoic Poulain 			 le16_to_cpu(params->dev_revid));
736*965651c1STedd Ho-Jeong An 		break;
737*965651c1STedd Ho-Jeong An 	case 0x12:      /* ThP */
738*965651c1STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
739*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_variant),
740*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_revision),
741*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.fw_revision));
742*965651c1STedd Ho-Jeong An 		break;
743*965651c1STedd Ho-Jeong An 	default:
744*965651c1STedd Ho-Jeong An 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
745*965651c1STedd Ho-Jeong An 			   ver.hw_variant);
746*965651c1STedd Ho-Jeong An 		return -EINVAL;
747*965651c1STedd Ho-Jeong An 	}
748ca93cee5SLoic Poulain 
749ca93cee5SLoic Poulain 	err = request_firmware(&fw, fwname, &hdev->dev);
750ca93cee5SLoic Poulain 	if (err < 0) {
751f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
752f44e78a5SLoic Poulain 			   err);
753ca93cee5SLoic Poulain 		kfree_skb(skb);
754ca93cee5SLoic Poulain 		return err;
755ca93cee5SLoic Poulain 	}
756ca93cee5SLoic Poulain 
757f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Found device firmware: %s", fwname);
758ca93cee5SLoic Poulain 
7591cfbabddSLoic Poulain 	/* Save the DDC file name for later */
760*965651c1STedd Ho-Jeong An 	switch (ver.hw_variant) {
761*965651c1STedd Ho-Jeong An 	case 0x0b:      /* SfP */
762*965651c1STedd Ho-Jeong An 	case 0x0c:      /* WsP */
763b7da6a69STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
764b7da6a69STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_variant),
7651cfbabddSLoic Poulain 			 le16_to_cpu(params->dev_revid));
766*965651c1STedd Ho-Jeong An 		break;
767*965651c1STedd Ho-Jeong An 	case 0x12:      /* ThP */
768*965651c1STedd Ho-Jeong An 		snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
769*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_variant),
770*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.hw_revision),
771*965651c1STedd Ho-Jeong An 			 le16_to_cpu(ver.fw_revision));
772*965651c1STedd Ho-Jeong An 		break;
773*965651c1STedd Ho-Jeong An 	default:
774*965651c1STedd Ho-Jeong An 		bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
775*965651c1STedd Ho-Jeong An 			   ver.hw_variant);
776*965651c1STedd Ho-Jeong An 		return -EINVAL;
777*965651c1STedd Ho-Jeong An 	}
7781cfbabddSLoic Poulain 
779ca93cee5SLoic Poulain 	kfree_skb(skb);
780ca93cee5SLoic Poulain 
781ca93cee5SLoic Poulain 	if (fw->size < 644) {
782f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
783f44e78a5SLoic Poulain 			   fw->size);
784ca93cee5SLoic Poulain 		err = -EBADF;
785ca93cee5SLoic Poulain 		goto done;
786ca93cee5SLoic Poulain 	}
787ca93cee5SLoic Poulain 
788ca93cee5SLoic Poulain 	set_bit(STATE_DOWNLOADING, &intel->flags);
789ca93cee5SLoic Poulain 
790ca93cee5SLoic Poulain 	/* Start the firmware download transaction with the Init fragment
791ca93cee5SLoic Poulain 	 * represented by the 128 bytes of CSS header.
792ca93cee5SLoic Poulain 	 */
79309df123dSMarcel Holtmann 	err = btintel_secure_send(hdev, 0x00, 128, fw->data);
794ca93cee5SLoic Poulain 	if (err < 0) {
795f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
796ca93cee5SLoic Poulain 		goto done;
797ca93cee5SLoic Poulain 	}
798ca93cee5SLoic Poulain 
799ca93cee5SLoic Poulain 	/* Send the 256 bytes of public key information from the firmware
800ca93cee5SLoic Poulain 	 * as the PKey fragment.
801ca93cee5SLoic Poulain 	 */
80209df123dSMarcel Holtmann 	err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
803ca93cee5SLoic Poulain 	if (err < 0) {
804f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to send firmware public key (%d)",
805f44e78a5SLoic Poulain 			   err);
806ca93cee5SLoic Poulain 		goto done;
807ca93cee5SLoic Poulain 	}
808ca93cee5SLoic Poulain 
809ca93cee5SLoic Poulain 	/* Send the 256 bytes of signature information from the firmware
810ca93cee5SLoic Poulain 	 * as the Sign fragment.
811ca93cee5SLoic Poulain 	 */
81209df123dSMarcel Holtmann 	err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
813ca93cee5SLoic Poulain 	if (err < 0) {
814f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Failed to send firmware signature (%d)",
815f44e78a5SLoic Poulain 			   err);
816ca93cee5SLoic Poulain 		goto done;
817ca93cee5SLoic Poulain 	}
818ca93cee5SLoic Poulain 
819ca93cee5SLoic Poulain 	fw_ptr = fw->data + 644;
820ca93cee5SLoic Poulain 	frag_len = 0;
821ca93cee5SLoic Poulain 
822ca93cee5SLoic Poulain 	while (fw_ptr - fw->data < fw->size) {
823ca93cee5SLoic Poulain 		struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
824ca93cee5SLoic Poulain 
825ca93cee5SLoic Poulain 		frag_len += sizeof(*cmd) + cmd->plen;
826ca93cee5SLoic Poulain 
827f44e78a5SLoic Poulain 		bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
828f44e78a5SLoic Poulain 			   fw->size);
829ca93cee5SLoic Poulain 
830ca93cee5SLoic Poulain 		/* The parameter length of the secure send command requires
831ca93cee5SLoic Poulain 		 * a 4 byte alignment. It happens so that the firmware file
832ca93cee5SLoic Poulain 		 * contains proper Intel_NOP commands to align the fragments
833ca93cee5SLoic Poulain 		 * as needed.
834ca93cee5SLoic Poulain 		 *
835ca93cee5SLoic Poulain 		 * Send set of commands with 4 byte alignment from the
836ca93cee5SLoic Poulain 		 * firmware data buffer as a single Data fragement.
837ca93cee5SLoic Poulain 		 */
838ca93cee5SLoic Poulain 		if (frag_len % 4)
839ca93cee5SLoic Poulain 			continue;
840ca93cee5SLoic Poulain 
841ca93cee5SLoic Poulain 		/* Send each command from the firmware data buffer as
842ca93cee5SLoic Poulain 		 * a single Data fragment.
843ca93cee5SLoic Poulain 		 */
84409df123dSMarcel Holtmann 		err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
845ca93cee5SLoic Poulain 		if (err < 0) {
846f44e78a5SLoic Poulain 			bt_dev_err(hdev, "Failed to send firmware data (%d)",
847f44e78a5SLoic Poulain 				   err);
848ca93cee5SLoic Poulain 			goto done;
849ca93cee5SLoic Poulain 		}
850ca93cee5SLoic Poulain 
851ca93cee5SLoic Poulain 		fw_ptr += frag_len;
852ca93cee5SLoic Poulain 		frag_len = 0;
853ca93cee5SLoic Poulain 	}
854ca93cee5SLoic Poulain 
855ca93cee5SLoic Poulain 	set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
856ca93cee5SLoic Poulain 
857f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Waiting for firmware download to complete");
858ca93cee5SLoic Poulain 
859ca93cee5SLoic Poulain 	/* Before switching the device into operational mode and with that
860ca93cee5SLoic Poulain 	 * booting the loaded firmware, wait for the bootloader notification
861ca93cee5SLoic Poulain 	 * that all fragments have been successfully received.
862ca93cee5SLoic Poulain 	 *
863ca93cee5SLoic Poulain 	 * When the event processing receives the notification, then the
864ca93cee5SLoic Poulain 	 * STATE_DOWNLOADING flag will be cleared.
865ca93cee5SLoic Poulain 	 *
866ca93cee5SLoic Poulain 	 * The firmware loading should not take longer than 5 seconds
867ca93cee5SLoic Poulain 	 * and thus just timeout if that happens and fail the setup
868ca93cee5SLoic Poulain 	 * of this device.
869ca93cee5SLoic Poulain 	 */
870ca93cee5SLoic Poulain 	err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
871ca93cee5SLoic Poulain 				  TASK_INTERRUPTIBLE,
872ca93cee5SLoic Poulain 				  msecs_to_jiffies(5000));
873f0a70a04SBart Van Assche 	if (err == -EINTR) {
874f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading interrupted");
875ca93cee5SLoic Poulain 		err = -EINTR;
876ca93cee5SLoic Poulain 		goto done;
877ca93cee5SLoic Poulain 	}
878ca93cee5SLoic Poulain 
879ca93cee5SLoic Poulain 	if (err) {
880f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading timeout");
881ca93cee5SLoic Poulain 		err = -ETIMEDOUT;
882ca93cee5SLoic Poulain 		goto done;
883ca93cee5SLoic Poulain 	}
884ca93cee5SLoic Poulain 
885ca93cee5SLoic Poulain 	if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
886f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Firmware loading failed");
887ca93cee5SLoic Poulain 		err = -ENOEXEC;
888ca93cee5SLoic Poulain 		goto done;
889ca93cee5SLoic Poulain 	}
890ca93cee5SLoic Poulain 
891ca93cee5SLoic Poulain 	rettime = ktime_get();
892ca93cee5SLoic Poulain 	delta = ktime_sub(rettime, calltime);
893ca93cee5SLoic Poulain 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
894ca93cee5SLoic Poulain 
895f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
896ca93cee5SLoic Poulain 
897ca93cee5SLoic Poulain done:
898ca93cee5SLoic Poulain 	release_firmware(fw);
899ca93cee5SLoic Poulain 
900ca93cee5SLoic Poulain 	if (err < 0)
901ca93cee5SLoic Poulain 		return err;
902ca93cee5SLoic Poulain 
903ff289559SLoic Poulain 	/* We need to restore the default speed before Intel reset */
904ff289559SLoic Poulain 	if (speed_change) {
905ff289559SLoic Poulain 		err = intel_set_baudrate(hu, init_speed);
906ff289559SLoic Poulain 		if (err)
907ff289559SLoic Poulain 			return err;
908ff289559SLoic Poulain 	}
909ff289559SLoic Poulain 
910ca93cee5SLoic Poulain 	calltime = ktime_get();
911ca93cee5SLoic Poulain 
912ca93cee5SLoic Poulain 	set_bit(STATE_BOOTING, &intel->flags);
913ca93cee5SLoic Poulain 
914ca93cee5SLoic Poulain 	skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
915a0c38245SLoic Poulain 			     HCI_CMD_TIMEOUT);
916ca93cee5SLoic Poulain 	if (IS_ERR(skb))
917ca93cee5SLoic Poulain 		return PTR_ERR(skb);
918ca93cee5SLoic Poulain 
919ca93cee5SLoic Poulain 	kfree_skb(skb);
920ca93cee5SLoic Poulain 
921ca93cee5SLoic Poulain 	/* The bootloader will not indicate when the device is ready. This
922ca93cee5SLoic Poulain 	 * is done by the operational firmware sending bootup notification.
923ca93cee5SLoic Poulain 	 *
924ca93cee5SLoic Poulain 	 * Booting into operational firmware should not take longer than
925ca93cee5SLoic Poulain 	 * 1 second. However if that happens, then just fail the setup
926ca93cee5SLoic Poulain 	 * since something went wrong.
927ca93cee5SLoic Poulain 	 */
928f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Waiting for device to boot");
929ca93cee5SLoic Poulain 
9301ab1f239SLoic Poulain 	err = intel_wait_booting(hu);
9311ab1f239SLoic Poulain 	if (err)
9321ab1f239SLoic Poulain 		return err;
933ca93cee5SLoic Poulain 
9341ab1f239SLoic Poulain 	clear_bit(STATE_BOOTING, &intel->flags);
935ca93cee5SLoic Poulain 
936ca93cee5SLoic Poulain 	rettime = ktime_get();
937ca93cee5SLoic Poulain 	delta = ktime_sub(rettime, calltime);
938ca93cee5SLoic Poulain 	duration = (unsigned long long) ktime_to_ns(delta) >> 10;
939ca93cee5SLoic Poulain 
940f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Device booted in %llu usecs", duration);
941ca93cee5SLoic Poulain 
94231eff267SLoic Poulain 	/* Enable LPM if matching pdev with wakeup enabled, set TX active
94331eff267SLoic Poulain 	 * until further LPM TX notification.
94431eff267SLoic Poulain 	 */
94567c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
946b98469f4SLoic Poulain 	list_for_each(p, &intel_device_list) {
947b98469f4SLoic Poulain 		struct intel_device *dev = list_entry(p, struct intel_device,
948b98469f4SLoic Poulain 						      list);
949dcb9cfaaSJohan Hovold 		if (!hu->tty->dev)
950dcb9cfaaSJohan Hovold 			break;
951b98469f4SLoic Poulain 		if (hu->tty->dev->parent == dev->pdev->dev.parent) {
95231eff267SLoic Poulain 			if (device_may_wakeup(&dev->pdev->dev)) {
95331eff267SLoic Poulain 				set_bit(STATE_LPM_ENABLED, &intel->flags);
95431eff267SLoic Poulain 				set_bit(STATE_TX_ACTIVE, &intel->flags);
95531eff267SLoic Poulain 			}
956b98469f4SLoic Poulain 			break;
957b98469f4SLoic Poulain 		}
958b98469f4SLoic Poulain 	}
95967c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
960b98469f4SLoic Poulain 
9611cfbabddSLoic Poulain 	/* Ignore errors, device can work without DDC parameters */
9621cfbabddSLoic Poulain 	btintel_load_ddc_config(hdev, fwname);
9631cfbabddSLoic Poulain 
964ff289559SLoic Poulain 	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
965ff289559SLoic Poulain 	if (IS_ERR(skb))
966ff289559SLoic Poulain 		return PTR_ERR(skb);
967ff289559SLoic Poulain 	kfree_skb(skb);
968ff289559SLoic Poulain 
969ff289559SLoic Poulain 	if (speed_change) {
970ff289559SLoic Poulain 		err = intel_set_baudrate(hu, oper_speed);
971ff289559SLoic Poulain 		if (err)
972ff289559SLoic Poulain 			return err;
973ff289559SLoic Poulain 	}
974ff289559SLoic Poulain 
975f44e78a5SLoic Poulain 	bt_dev_info(hdev, "Setup complete");
976ff289559SLoic Poulain 
977ca93cee5SLoic Poulain 	clear_bit(STATE_BOOTLOADER, &intel->flags);
978ca93cee5SLoic Poulain 
979ca93cee5SLoic Poulain 	return 0;
980ca93cee5SLoic Poulain }
981ca93cee5SLoic Poulain 
982ca93cee5SLoic Poulain static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
983ca93cee5SLoic Poulain {
984ca93cee5SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
985ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
986ca93cee5SLoic Poulain 	struct hci_event_hdr *hdr;
987ca93cee5SLoic Poulain 
9881ab1f239SLoic Poulain 	if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
9891ab1f239SLoic Poulain 	    !test_bit(STATE_BOOTING, &intel->flags))
990ca93cee5SLoic Poulain 		goto recv;
991ca93cee5SLoic Poulain 
992ca93cee5SLoic Poulain 	hdr = (void *)skb->data;
993ca93cee5SLoic Poulain 
994ca93cee5SLoic Poulain 	/* When the firmware loading completes the device sends
995ca93cee5SLoic Poulain 	 * out a vendor specific event indicating the result of
996ca93cee5SLoic Poulain 	 * the firmware loading.
997ca93cee5SLoic Poulain 	 */
998ca93cee5SLoic Poulain 	if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
999ca93cee5SLoic Poulain 	    skb->data[2] == 0x06) {
1000ca93cee5SLoic Poulain 		if (skb->data[3] != 0x00)
1001ca93cee5SLoic Poulain 			set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
1002ca93cee5SLoic Poulain 
1003ca93cee5SLoic Poulain 		if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
1004ca93cee5SLoic Poulain 		    test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
1005ca93cee5SLoic Poulain 			smp_mb__after_atomic();
1006ca93cee5SLoic Poulain 			wake_up_bit(&intel->flags, STATE_DOWNLOADING);
1007ca93cee5SLoic Poulain 		}
1008ca93cee5SLoic Poulain 
1009ca93cee5SLoic Poulain 	/* When switching to the operational firmware the device
1010ca93cee5SLoic Poulain 	 * sends a vendor specific event indicating that the bootup
1011ca93cee5SLoic Poulain 	 * completed.
1012ca93cee5SLoic Poulain 	 */
1013ca93cee5SLoic Poulain 	} else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1014ca93cee5SLoic Poulain 		   skb->data[2] == 0x02) {
1015ca93cee5SLoic Poulain 		if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
1016ca93cee5SLoic Poulain 			smp_mb__after_atomic();
1017ca93cee5SLoic Poulain 			wake_up_bit(&intel->flags, STATE_BOOTING);
1018ca93cee5SLoic Poulain 		}
1019ca93cee5SLoic Poulain 	}
1020ca93cee5SLoic Poulain recv:
1021ca93cee5SLoic Poulain 	return hci_recv_frame(hdev, skb);
1022ca93cee5SLoic Poulain }
1023ca93cee5SLoic Poulain 
1024b98469f4SLoic Poulain static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
1025b98469f4SLoic Poulain {
1026b98469f4SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
1027b98469f4SLoic Poulain 	struct intel_data *intel = hu->priv;
1028b98469f4SLoic Poulain 
1029f44e78a5SLoic Poulain 	bt_dev_dbg(hdev, "TX idle notification (%d)", value);
1030b98469f4SLoic Poulain 
103174cdad37SLoic Poulain 	if (value) {
1032b98469f4SLoic Poulain 		set_bit(STATE_TX_ACTIVE, &intel->flags);
103374cdad37SLoic Poulain 		schedule_work(&intel->busy_work);
103474cdad37SLoic Poulain 	} else {
1035b98469f4SLoic Poulain 		clear_bit(STATE_TX_ACTIVE, &intel->flags);
1036b98469f4SLoic Poulain 	}
103774cdad37SLoic Poulain }
1038b98469f4SLoic Poulain 
1039b98469f4SLoic Poulain static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1040b98469f4SLoic Poulain {
1041b98469f4SLoic Poulain 	struct hci_lpm_pkt *lpm = (void *)skb->data;
104289436546SLoic Poulain 	struct hci_uart *hu = hci_get_drvdata(hdev);
104389436546SLoic Poulain 	struct intel_data *intel = hu->priv;
1044b98469f4SLoic Poulain 
1045b98469f4SLoic Poulain 	switch (lpm->opcode) {
1046b98469f4SLoic Poulain 	case LPM_OP_TX_NOTIFY:
10471b197574SLoic Poulain 		if (lpm->dlen < 1) {
10481b197574SLoic Poulain 			bt_dev_err(hu->hdev, "Invalid LPM notification packet");
10491b197574SLoic Poulain 			break;
10501b197574SLoic Poulain 		}
1051b98469f4SLoic Poulain 		intel_recv_lpm_notify(hdev, lpm->data[0]);
1052b98469f4SLoic Poulain 		break;
105389436546SLoic Poulain 	case LPM_OP_SUSPEND_ACK:
105489436546SLoic Poulain 		set_bit(STATE_SUSPENDED, &intel->flags);
105589436546SLoic Poulain 		if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
105689436546SLoic Poulain 			smp_mb__after_atomic();
105789436546SLoic Poulain 			wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
105889436546SLoic Poulain 		}
105989436546SLoic Poulain 		break;
106089436546SLoic Poulain 	case LPM_OP_RESUME_ACK:
106189436546SLoic Poulain 		clear_bit(STATE_SUSPENDED, &intel->flags);
106289436546SLoic Poulain 		if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
106389436546SLoic Poulain 			smp_mb__after_atomic();
106489436546SLoic Poulain 			wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
106589436546SLoic Poulain 		}
106689436546SLoic Poulain 		break;
1067b98469f4SLoic Poulain 	default:
1068f44e78a5SLoic Poulain 		bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
1069b98469f4SLoic Poulain 		break;
1070b98469f4SLoic Poulain 	}
1071b98469f4SLoic Poulain 
1072b98469f4SLoic Poulain 	kfree_skb(skb);
1073b98469f4SLoic Poulain 
1074b98469f4SLoic Poulain 	return 0;
1075b98469f4SLoic Poulain }
1076b98469f4SLoic Poulain 
1077b98469f4SLoic Poulain #define INTEL_RECV_LPM \
1078b98469f4SLoic Poulain 	.type = HCI_LPM_PKT, \
1079b98469f4SLoic Poulain 	.hlen = HCI_LPM_HDR_SIZE, \
1080b98469f4SLoic Poulain 	.loff = 1, \
1081b98469f4SLoic Poulain 	.lsize = 1, \
1082b98469f4SLoic Poulain 	.maxlen = HCI_LPM_MAX_SIZE
1083b98469f4SLoic Poulain 
1084ca93cee5SLoic Poulain static const struct h4_recv_pkt intel_recv_pkts[] = {
1085ca93cee5SLoic Poulain 	{ H4_RECV_ACL,    .recv = hci_recv_frame   },
1086ca93cee5SLoic Poulain 	{ H4_RECV_SCO,    .recv = hci_recv_frame   },
1087ca93cee5SLoic Poulain 	{ H4_RECV_EVENT,  .recv = intel_recv_event },
1088b98469f4SLoic Poulain 	{ INTEL_RECV_LPM, .recv = intel_recv_lpm   },
1089ca93cee5SLoic Poulain };
1090ca93cee5SLoic Poulain 
1091ca93cee5SLoic Poulain static int intel_recv(struct hci_uart *hu, const void *data, int count)
1092ca93cee5SLoic Poulain {
1093ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
1094ca93cee5SLoic Poulain 
1095ca93cee5SLoic Poulain 	if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1096ca93cee5SLoic Poulain 		return -EUNATCH;
1097ca93cee5SLoic Poulain 
1098ca93cee5SLoic Poulain 	intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1099ca93cee5SLoic Poulain 				    intel_recv_pkts,
1100ca93cee5SLoic Poulain 				    ARRAY_SIZE(intel_recv_pkts));
1101ca93cee5SLoic Poulain 	if (IS_ERR(intel->rx_skb)) {
1102ca93cee5SLoic Poulain 		int err = PTR_ERR(intel->rx_skb);
1103f44e78a5SLoic Poulain 		bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1104ca93cee5SLoic Poulain 		intel->rx_skb = NULL;
1105ca93cee5SLoic Poulain 		return err;
1106ca93cee5SLoic Poulain 	}
1107ca93cee5SLoic Poulain 
1108ca93cee5SLoic Poulain 	return count;
1109ca93cee5SLoic Poulain }
1110ca93cee5SLoic Poulain 
1111ca93cee5SLoic Poulain static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1112ca93cee5SLoic Poulain {
1113ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
111474cdad37SLoic Poulain 	struct list_head *p;
1115ca93cee5SLoic Poulain 
1116ca93cee5SLoic Poulain 	BT_DBG("hu %p skb %p", hu, skb);
1117ca93cee5SLoic Poulain 
1118dcb9cfaaSJohan Hovold 	if (!hu->tty->dev)
1119dcb9cfaaSJohan Hovold 		goto out_enqueue;
1120dcb9cfaaSJohan Hovold 
112174cdad37SLoic Poulain 	/* Be sure our controller is resumed and potential LPM transaction
112274cdad37SLoic Poulain 	 * completed before enqueuing any packet.
112374cdad37SLoic Poulain 	 */
112474cdad37SLoic Poulain 	mutex_lock(&intel_device_list_lock);
112574cdad37SLoic Poulain 	list_for_each(p, &intel_device_list) {
112674cdad37SLoic Poulain 		struct intel_device *idev = list_entry(p, struct intel_device,
112774cdad37SLoic Poulain 						       list);
112874cdad37SLoic Poulain 
112974cdad37SLoic Poulain 		if (hu->tty->dev->parent == idev->pdev->dev.parent) {
113074cdad37SLoic Poulain 			pm_runtime_get_sync(&idev->pdev->dev);
113174cdad37SLoic Poulain 			pm_runtime_mark_last_busy(&idev->pdev->dev);
113274cdad37SLoic Poulain 			pm_runtime_put_autosuspend(&idev->pdev->dev);
113374cdad37SLoic Poulain 			break;
113474cdad37SLoic Poulain 		}
113574cdad37SLoic Poulain 	}
113674cdad37SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
1137dcb9cfaaSJohan Hovold out_enqueue:
1138ca93cee5SLoic Poulain 	skb_queue_tail(&intel->txq, skb);
1139ca93cee5SLoic Poulain 
1140ca93cee5SLoic Poulain 	return 0;
1141ca93cee5SLoic Poulain }
1142ca93cee5SLoic Poulain 
1143ca93cee5SLoic Poulain static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1144ca93cee5SLoic Poulain {
1145ca93cee5SLoic Poulain 	struct intel_data *intel = hu->priv;
1146ca93cee5SLoic Poulain 	struct sk_buff *skb;
1147ca93cee5SLoic Poulain 
1148ca93cee5SLoic Poulain 	skb = skb_dequeue(&intel->txq);
1149ca93cee5SLoic Poulain 	if (!skb)
1150ca93cee5SLoic Poulain 		return skb;
1151ca93cee5SLoic Poulain 
1152ca93cee5SLoic Poulain 	if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1153618e8bc2SMarcel Holtmann 	    (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
1154ca93cee5SLoic Poulain 		struct hci_command_hdr *cmd = (void *)skb->data;
1155ca93cee5SLoic Poulain 		__u16 opcode = le16_to_cpu(cmd->opcode);
1156ca93cee5SLoic Poulain 
1157ca93cee5SLoic Poulain 		/* When the 0xfc01 command is issued to boot into
1158ca93cee5SLoic Poulain 		 * the operational firmware, it will actually not
1159ca93cee5SLoic Poulain 		 * send a command complete event. To keep the flow
1160ca93cee5SLoic Poulain 		 * control working inject that event here.
1161ca93cee5SLoic Poulain 		 */
1162ca93cee5SLoic Poulain 		if (opcode == 0xfc01)
1163ca93cee5SLoic Poulain 			inject_cmd_complete(hu->hdev, opcode);
1164ca93cee5SLoic Poulain 	}
1165ca93cee5SLoic Poulain 
1166ca93cee5SLoic Poulain 	/* Prepend skb with frame type */
1167618e8bc2SMarcel Holtmann 	memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
1168ca93cee5SLoic Poulain 
1169ca93cee5SLoic Poulain 	return skb;
1170ca93cee5SLoic Poulain }
1171ca93cee5SLoic Poulain 
1172ca93cee5SLoic Poulain static const struct hci_uart_proto intel_proto = {
1173ca93cee5SLoic Poulain 	.id		= HCI_UART_INTEL,
1174ca93cee5SLoic Poulain 	.name		= "Intel",
1175aee61f7aSMarcel Holtmann 	.manufacturer	= 2,
1176ca93cee5SLoic Poulain 	.init_speed	= 115200,
1177ff289559SLoic Poulain 	.oper_speed	= 3000000,
1178ca93cee5SLoic Poulain 	.open		= intel_open,
1179ca93cee5SLoic Poulain 	.close		= intel_close,
1180ca93cee5SLoic Poulain 	.flush		= intel_flush,
1181ca93cee5SLoic Poulain 	.setup		= intel_setup,
1182ff289559SLoic Poulain 	.set_baudrate	= intel_set_baudrate,
1183ca93cee5SLoic Poulain 	.recv		= intel_recv,
1184ca93cee5SLoic Poulain 	.enqueue	= intel_enqueue,
1185ca93cee5SLoic Poulain 	.dequeue	= intel_dequeue,
1186ca93cee5SLoic Poulain };
1187ca93cee5SLoic Poulain 
11881ab1f239SLoic Poulain #ifdef CONFIG_ACPI
11891ab1f239SLoic Poulain static const struct acpi_device_id intel_acpi_match[] = {
11901ab1f239SLoic Poulain 	{ "INT33E1", 0 },
11911ab1f239SLoic Poulain 	{ },
11921ab1f239SLoic Poulain };
11931ab1f239SLoic Poulain MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
11941ab1f239SLoic Poulain #endif
11951ab1f239SLoic Poulain 
119674cdad37SLoic Poulain #ifdef CONFIG_PM
1197f7552473SLoic Poulain static int intel_suspend_device(struct device *dev)
1198aa6802dfSLoic Poulain {
1199aa6802dfSLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1200aa6802dfSLoic Poulain 
1201aa6802dfSLoic Poulain 	mutex_lock(&idev->hu_lock);
1202aa6802dfSLoic Poulain 	if (idev->hu)
1203aa6802dfSLoic Poulain 		intel_lpm_suspend(idev->hu);
1204aa6802dfSLoic Poulain 	mutex_unlock(&idev->hu_lock);
1205aa6802dfSLoic Poulain 
1206aa6802dfSLoic Poulain 	return 0;
1207aa6802dfSLoic Poulain }
1208aa6802dfSLoic Poulain 
1209f7552473SLoic Poulain static int intel_resume_device(struct device *dev)
1210aa6802dfSLoic Poulain {
1211aa6802dfSLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1212aa6802dfSLoic Poulain 
1213aa6802dfSLoic Poulain 	mutex_lock(&idev->hu_lock);
1214aa6802dfSLoic Poulain 	if (idev->hu)
1215aa6802dfSLoic Poulain 		intel_lpm_resume(idev->hu);
1216aa6802dfSLoic Poulain 	mutex_unlock(&idev->hu_lock);
1217aa6802dfSLoic Poulain 
1218aa6802dfSLoic Poulain 	return 0;
1219aa6802dfSLoic Poulain }
1220aa6802dfSLoic Poulain #endif
1221aa6802dfSLoic Poulain 
1222f7552473SLoic Poulain #ifdef CONFIG_PM_SLEEP
1223f7552473SLoic Poulain static int intel_suspend(struct device *dev)
1224f7552473SLoic Poulain {
1225f7552473SLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1226f7552473SLoic Poulain 
1227f7552473SLoic Poulain 	if (device_may_wakeup(dev))
1228f7552473SLoic Poulain 		enable_irq_wake(idev->irq);
1229f7552473SLoic Poulain 
1230f7552473SLoic Poulain 	return intel_suspend_device(dev);
1231f7552473SLoic Poulain }
1232f7552473SLoic Poulain 
1233f7552473SLoic Poulain static int intel_resume(struct device *dev)
1234f7552473SLoic Poulain {
1235f7552473SLoic Poulain 	struct intel_device *idev = dev_get_drvdata(dev);
1236f7552473SLoic Poulain 
1237f7552473SLoic Poulain 	if (device_may_wakeup(dev))
1238f7552473SLoic Poulain 		disable_irq_wake(idev->irq);
1239f7552473SLoic Poulain 
1240f7552473SLoic Poulain 	return intel_resume_device(dev);
1241f7552473SLoic Poulain }
1242f7552473SLoic Poulain #endif
1243f7552473SLoic Poulain 
1244aa6802dfSLoic Poulain static const struct dev_pm_ops intel_pm_ops = {
1245aa6802dfSLoic Poulain 	SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1246f7552473SLoic Poulain 	SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
1247aa6802dfSLoic Poulain };
1248aa6802dfSLoic Poulain 
12494a59d433SAndy Shevchenko static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
12504a59d433SAndy Shevchenko static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
12514a59d433SAndy Shevchenko 
12524a59d433SAndy Shevchenko static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
12534a59d433SAndy Shevchenko 	{ "reset-gpios", &reset_gpios, 1 },
12544a59d433SAndy Shevchenko 	{ "host-wake-gpios", &host_wake_gpios, 1 },
12554a59d433SAndy Shevchenko 	{ },
12564a59d433SAndy Shevchenko };
12574a59d433SAndy Shevchenko 
12581ab1f239SLoic Poulain static int intel_probe(struct platform_device *pdev)
12591ab1f239SLoic Poulain {
12601ab1f239SLoic Poulain 	struct intel_device *idev;
12614a59d433SAndy Shevchenko 	int ret;
12621ab1f239SLoic Poulain 
12631ab1f239SLoic Poulain 	idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
12641ab1f239SLoic Poulain 	if (!idev)
12651ab1f239SLoic Poulain 		return -ENOMEM;
12661ab1f239SLoic Poulain 
1267aa6802dfSLoic Poulain 	mutex_init(&idev->hu_lock);
1268aa6802dfSLoic Poulain 
12691ab1f239SLoic Poulain 	idev->pdev = pdev;
12701ab1f239SLoic Poulain 
12714a59d433SAndy Shevchenko 	ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
12724a59d433SAndy Shevchenko 	if (ret)
12734a59d433SAndy Shevchenko 		dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
12744a59d433SAndy Shevchenko 
127532b9ccbcSLoic Poulain 	idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
12761ab1f239SLoic Poulain 	if (IS_ERR(idev->reset)) {
12771ab1f239SLoic Poulain 		dev_err(&pdev->dev, "Unable to retrieve gpio\n");
12781ab1f239SLoic Poulain 		return PTR_ERR(idev->reset);
12791ab1f239SLoic Poulain 	}
12801ab1f239SLoic Poulain 
1281765ea3abSLoic Poulain 	idev->irq = platform_get_irq(pdev, 0);
1282765ea3abSLoic Poulain 	if (idev->irq < 0) {
1283765ea3abSLoic Poulain 		struct gpio_desc *host_wake;
1284765ea3abSLoic Poulain 
1285765ea3abSLoic Poulain 		dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1286765ea3abSLoic Poulain 
128732b9ccbcSLoic Poulain 		host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
1288765ea3abSLoic Poulain 		if (IS_ERR(host_wake)) {
1289765ea3abSLoic Poulain 			dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1290765ea3abSLoic Poulain 			goto no_irq;
1291765ea3abSLoic Poulain 		}
1292765ea3abSLoic Poulain 
1293765ea3abSLoic Poulain 		idev->irq = gpiod_to_irq(host_wake);
1294765ea3abSLoic Poulain 		if (idev->irq < 0) {
1295765ea3abSLoic Poulain 			dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1296765ea3abSLoic Poulain 			goto no_irq;
1297765ea3abSLoic Poulain 		}
1298765ea3abSLoic Poulain 	}
1299765ea3abSLoic Poulain 
1300765ea3abSLoic Poulain 	/* Only enable wake-up/irq when controller is powered */
1301765ea3abSLoic Poulain 	device_set_wakeup_capable(&pdev->dev, true);
1302765ea3abSLoic Poulain 	device_wakeup_disable(&pdev->dev);
1303765ea3abSLoic Poulain 
1304765ea3abSLoic Poulain no_irq:
13051ab1f239SLoic Poulain 	platform_set_drvdata(pdev, idev);
13061ab1f239SLoic Poulain 
13071ab1f239SLoic Poulain 	/* Place this instance on the device list */
130867c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
13091ab1f239SLoic Poulain 	list_add_tail(&idev->list, &intel_device_list);
131067c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
13111ab1f239SLoic Poulain 
1312765ea3abSLoic Poulain 	dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1313765ea3abSLoic Poulain 		 desc_to_gpio(idev->reset), idev->irq);
13141ab1f239SLoic Poulain 
13151ab1f239SLoic Poulain 	return 0;
13161ab1f239SLoic Poulain }
13171ab1f239SLoic Poulain 
13181ab1f239SLoic Poulain static int intel_remove(struct platform_device *pdev)
13191ab1f239SLoic Poulain {
13201ab1f239SLoic Poulain 	struct intel_device *idev = platform_get_drvdata(pdev);
13211ab1f239SLoic Poulain 
1322765ea3abSLoic Poulain 	device_wakeup_disable(&pdev->dev);
1323765ea3abSLoic Poulain 
132467c8bde0SLoic Poulain 	mutex_lock(&intel_device_list_lock);
13251ab1f239SLoic Poulain 	list_del(&idev->list);
132667c8bde0SLoic Poulain 	mutex_unlock(&intel_device_list_lock);
13271ab1f239SLoic Poulain 
13281ab1f239SLoic Poulain 	dev_info(&pdev->dev, "unregistered.\n");
13291ab1f239SLoic Poulain 
13301ab1f239SLoic Poulain 	return 0;
13311ab1f239SLoic Poulain }
13321ab1f239SLoic Poulain 
13331ab1f239SLoic Poulain static struct platform_driver intel_driver = {
13341ab1f239SLoic Poulain 	.probe = intel_probe,
13351ab1f239SLoic Poulain 	.remove = intel_remove,
13361ab1f239SLoic Poulain 	.driver = {
13371ab1f239SLoic Poulain 		.name = "hci_intel",
13381ab1f239SLoic Poulain 		.acpi_match_table = ACPI_PTR(intel_acpi_match),
1339aa6802dfSLoic Poulain 		.pm = &intel_pm_ops,
13401ab1f239SLoic Poulain 	},
13411ab1f239SLoic Poulain };
13421ab1f239SLoic Poulain 
1343ca93cee5SLoic Poulain int __init intel_init(void)
1344ca93cee5SLoic Poulain {
13451ab1f239SLoic Poulain 	platform_driver_register(&intel_driver);
13461ab1f239SLoic Poulain 
1347ca93cee5SLoic Poulain 	return hci_uart_register_proto(&intel_proto);
1348ca93cee5SLoic Poulain }
1349ca93cee5SLoic Poulain 
1350ca93cee5SLoic Poulain int __exit intel_deinit(void)
1351ca93cee5SLoic Poulain {
13521ab1f239SLoic Poulain 	platform_driver_unregister(&intel_driver);
13531ab1f239SLoic Poulain 
1354ca93cee5SLoic Poulain 	return hci_uart_unregister_proto(&intel_proto);
1355ca93cee5SLoic Poulain }
1356