blob: cf7438512d21a9f7c6238956f7d4688c5d08640c [file] [log] [blame]
Marcel Holtmann16e38872015-04-04 16:13:02 -07001/*
2 *
3 * Bluetooth HCI UART driver for Intel devices
4 *
5 * Copyright (C) 2015 Intel Corporation
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/skbuff.h>
Loic Poulainca93cee2015-07-01 12:20:26 +020027#include <linux/firmware.h>
Loic Poulain1ab1f232015-08-27 07:21:51 +020028#include <linux/module.h>
Loic Poulainca93cee2015-07-01 12:20:26 +020029#include <linux/wait.h>
Loic Poulain1ab1f232015-08-27 07:21:51 +020030#include <linux/tty.h>
31#include <linux/platform_device.h>
32#include <linux/gpio/consumer.h>
33#include <linux/acpi.h>
Loic Poulain765ea3a2015-08-29 13:38:18 +020034#include <linux/interrupt.h>
Loic Poulain74cdad32015-09-02 12:04:13 +020035#include <linux/pm_runtime.h>
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -080036#include <asm/unaligned.h>
Marcel Holtmann16e38872015-04-04 16:13:02 -070037
38#include <net/bluetooth/bluetooth.h>
39#include <net/bluetooth/hci_core.h>
40
41#include "hci_uart.h"
Loic Poulainca93cee2015-07-01 12:20:26 +020042#include "btintel.h"
43
44#define STATE_BOOTLOADER 0
45#define STATE_DOWNLOADING 1
46#define STATE_FIRMWARE_LOADED 2
47#define STATE_FIRMWARE_FAILED 3
48#define STATE_BOOTING 4
Loic Poulainb98469f2015-08-29 13:38:19 +020049#define STATE_LPM_ENABLED 5
50#define STATE_TX_ACTIVE 6
Loic Poulain89436542015-09-02 12:04:11 +020051#define STATE_SUSPENDED 7
52#define STATE_LPM_TRANSACTION 8
Loic Poulainb98469f2015-08-29 13:38:19 +020053
Loic Poulain89436542015-09-02 12:04:11 +020054#define HCI_LPM_WAKE_PKT 0xf0
Loic Poulainb98469f2015-08-29 13:38:19 +020055#define HCI_LPM_PKT 0xf1
56#define HCI_LPM_MAX_SIZE 10
57#define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
58
59#define LPM_OP_TX_NOTIFY 0x00
Loic Poulain89436542015-09-02 12:04:11 +020060#define LPM_OP_SUSPEND_ACK 0x02
61#define LPM_OP_RESUME_ACK 0x03
Loic Poulainb98469f2015-08-29 13:38:19 +020062
Loic Poulain74cdad32015-09-02 12:04:13 +020063#define LPM_SUSPEND_DELAY_MS 1000
64
Loic Poulainb98469f2015-08-29 13:38:19 +020065struct hci_lpm_pkt {
66 __u8 opcode;
67 __u8 dlen;
68 __u8 data[0];
69} __packed;
Loic Poulainca93cee2015-07-01 12:20:26 +020070
Loic Poulain1ab1f232015-08-27 07:21:51 +020071struct intel_device {
72 struct list_head list;
73 struct platform_device *pdev;
74 struct gpio_desc *reset;
Loic Poulainaa6802d2015-09-02 12:04:12 +020075 struct hci_uart *hu;
76 struct mutex hu_lock;
Loic Poulain765ea3a2015-08-29 13:38:18 +020077 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020078};
79
80static LIST_HEAD(intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +020081static DEFINE_MUTEX(intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +020082
Loic Poulainca93cee2015-07-01 12:20:26 +020083struct intel_data {
84 struct sk_buff *rx_skb;
85 struct sk_buff_head txq;
Loic Poulain74cdad32015-09-02 12:04:13 +020086 struct work_struct busy_work;
87 struct hci_uart *hu;
Loic Poulainca93cee2015-07-01 12:20:26 +020088 unsigned long flags;
89};
90
Loic Poulainff289552015-08-25 17:55:44 +020091static u8 intel_convert_speed(unsigned int speed)
92{
93 switch (speed) {
94 case 9600:
95 return 0x00;
96 case 19200:
97 return 0x01;
98 case 38400:
99 return 0x02;
100 case 57600:
101 return 0x03;
102 case 115200:
103 return 0x04;
104 case 230400:
105 return 0x05;
106 case 460800:
107 return 0x06;
108 case 921600:
109 return 0x07;
110 case 1843200:
111 return 0x08;
112 case 3250000:
113 return 0x09;
114 case 2000000:
115 return 0x0a;
116 case 3000000:
117 return 0x0b;
118 default:
119 return 0xff;
120 }
121}
122
Loic Poulain1ab1f232015-08-27 07:21:51 +0200123static int intel_wait_booting(struct hci_uart *hu)
124{
125 struct intel_data *intel = hu->priv;
126 int err;
127
128 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
129 TASK_INTERRUPTIBLE,
130 msecs_to_jiffies(1000));
131
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700132 if (err == -EINTR) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200133 bt_dev_err(hu->hdev, "Device boot interrupted");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200134 return -EINTR;
135 }
136
137 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200138 bt_dev_err(hu->hdev, "Device boot timeout");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200139 return -ETIMEDOUT;
140 }
141
142 return err;
143}
144
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200145#ifdef CONFIG_PM
Loic Poulain89436542015-09-02 12:04:11 +0200146static int intel_wait_lpm_transaction(struct hci_uart *hu)
147{
148 struct intel_data *intel = hu->priv;
149 int err;
150
151 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
152 TASK_INTERRUPTIBLE,
153 msecs_to_jiffies(1000));
154
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700155 if (err == -EINTR) {
Loic Poulain89436542015-09-02 12:04:11 +0200156 bt_dev_err(hu->hdev, "LPM transaction interrupted");
157 return -EINTR;
158 }
159
160 if (err) {
161 bt_dev_err(hu->hdev, "LPM transaction timeout");
162 return -ETIMEDOUT;
163 }
164
165 return err;
166}
167
168static int intel_lpm_suspend(struct hci_uart *hu)
169{
170 static const u8 suspend[] = { 0x01, 0x01, 0x01 };
171 struct intel_data *intel = hu->priv;
172 struct sk_buff *skb;
173
174 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
175 test_bit(STATE_SUSPENDED, &intel->flags))
176 return 0;
177
178 if (test_bit(STATE_TX_ACTIVE, &intel->flags))
179 return -EAGAIN;
180
181 bt_dev_dbg(hu->hdev, "Suspending");
182
183 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
184 if (!skb) {
185 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
186 return -ENOMEM;
187 }
188
Johannes Berg59ae1d12017-06-16 14:29:20 +0200189 skb_put_data(skb, suspend, sizeof(suspend));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100190 hci_skb_pkt_type(skb) = HCI_LPM_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200191
192 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
193
Loic Poulain30e945f2015-09-09 19:08:02 +0200194 /* LPM flow is a priority, enqueue packet at list head */
195 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200196 hci_uart_tx_wakeup(hu);
197
198 intel_wait_lpm_transaction(hu);
199 /* Even in case of failure, continue and test the suspended flag */
200
201 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
202
203 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
204 bt_dev_err(hu->hdev, "Device suspend error");
205 return -EINVAL;
206 }
207
208 bt_dev_dbg(hu->hdev, "Suspended");
209
210 hci_uart_set_flow_control(hu, true);
211
212 return 0;
213}
214
215static int intel_lpm_resume(struct hci_uart *hu)
216{
217 struct intel_data *intel = hu->priv;
218 struct sk_buff *skb;
219
220 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
221 !test_bit(STATE_SUSPENDED, &intel->flags))
222 return 0;
223
224 bt_dev_dbg(hu->hdev, "Resuming");
225
226 hci_uart_set_flow_control(hu, false);
227
228 skb = bt_skb_alloc(0, GFP_KERNEL);
229 if (!skb) {
230 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
231 return -ENOMEM;
232 }
233
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100234 hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200235
236 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
237
Loic Poulain30e945f2015-09-09 19:08:02 +0200238 /* LPM flow is a priority, enqueue packet at list head */
239 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200240 hci_uart_tx_wakeup(hu);
241
242 intel_wait_lpm_transaction(hu);
243 /* Even in case of failure, continue and test the suspended flag */
244
245 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
246
247 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
248 bt_dev_err(hu->hdev, "Device resume error");
249 return -EINVAL;
250 }
251
252 bt_dev_dbg(hu->hdev, "Resumed");
253
254 return 0;
255}
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200256#endif /* CONFIG_PM */
Loic Poulain89436542015-09-02 12:04:11 +0200257
258static int intel_lpm_host_wake(struct hci_uart *hu)
259{
260 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
261 struct intel_data *intel = hu->priv;
262 struct sk_buff *skb;
263
264 hci_uart_set_flow_control(hu, false);
265
266 clear_bit(STATE_SUSPENDED, &intel->flags);
267
268 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
269 if (!skb) {
270 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
271 return -ENOMEM;
272 }
273
Johannes Berg59ae1d12017-06-16 14:29:20 +0200274 skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100275 hci_skb_pkt_type(skb) = HCI_LPM_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200276
Loic Poulain30e945f2015-09-09 19:08:02 +0200277 /* LPM flow is a priority, enqueue packet at list head */
278 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200279 hci_uart_tx_wakeup(hu);
280
281 bt_dev_dbg(hu->hdev, "Resumed by controller");
282
283 return 0;
284}
285
Loic Poulain765ea3a2015-08-29 13:38:18 +0200286static irqreturn_t intel_irq(int irq, void *dev_id)
287{
288 struct intel_device *idev = dev_id;
289
290 dev_info(&idev->pdev->dev, "hci_intel irq\n");
291
Loic Poulainaa6802d2015-09-02 12:04:12 +0200292 mutex_lock(&idev->hu_lock);
293 if (idev->hu)
294 intel_lpm_host_wake(idev->hu);
295 mutex_unlock(&idev->hu_lock);
296
Loic Poulain74cdad32015-09-02 12:04:13 +0200297 /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
298 pm_runtime_get(&idev->pdev->dev);
299 pm_runtime_mark_last_busy(&idev->pdev->dev);
300 pm_runtime_put_autosuspend(&idev->pdev->dev);
301
Loic Poulain765ea3a2015-08-29 13:38:18 +0200302 return IRQ_HANDLED;
303}
304
Loic Poulain1ab1f232015-08-27 07:21:51 +0200305static int intel_set_power(struct hci_uart *hu, bool powered)
306{
307 struct list_head *p;
308 int err = -ENODEV;
309
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200310 if (!hu->tty->dev)
311 return err;
312
Loic Poulain67c8bde2015-08-31 18:34:31 +0200313 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200314
315 list_for_each(p, &intel_device_list) {
316 struct intel_device *idev = list_entry(p, struct intel_device,
317 list);
318
319 /* tty device and pdev device should share the same parent
320 * which is the UART port.
321 */
322 if (hu->tty->dev->parent != idev->pdev->dev.parent)
323 continue;
324
325 if (!idev->reset) {
326 err = -ENOTSUPP;
327 break;
328 }
329
330 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
331 hu, dev_name(&idev->pdev->dev), powered);
332
333 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200334
Loic Poulainaa6802d2015-09-02 12:04:12 +0200335 /* Provide to idev a hu reference which is used to run LPM
336 * transactions (lpm suspend/resume) from PM callbacks.
337 * hu needs to be protected against concurrent removing during
338 * these PM ops.
339 */
340 mutex_lock(&idev->hu_lock);
341 idev->hu = powered ? hu : NULL;
342 mutex_unlock(&idev->hu_lock);
343
Loic Poulain765ea3a2015-08-29 13:38:18 +0200344 if (idev->irq < 0)
345 break;
346
347 if (powered && device_can_wakeup(&idev->pdev->dev)) {
348 err = devm_request_threaded_irq(&idev->pdev->dev,
349 idev->irq, NULL,
350 intel_irq,
351 IRQF_ONESHOT,
352 "bt-host-wake", idev);
353 if (err) {
354 BT_ERR("hu %p, unable to allocate irq-%d",
355 hu, idev->irq);
356 break;
357 }
358
359 device_wakeup_enable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200360
361 pm_runtime_set_active(&idev->pdev->dev);
362 pm_runtime_use_autosuspend(&idev->pdev->dev);
363 pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
364 LPM_SUSPEND_DELAY_MS);
365 pm_runtime_enable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200366 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
367 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
368 device_wakeup_disable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200369
370 pm_runtime_disable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200371 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200372 }
373
Loic Poulain67c8bde2015-08-31 18:34:31 +0200374 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200375
376 return err;
377}
378
Loic Poulain74cdad32015-09-02 12:04:13 +0200379static void intel_busy_work(struct work_struct *work)
380{
381 struct list_head *p;
382 struct intel_data *intel = container_of(work, struct intel_data,
383 busy_work);
384
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200385 if (!intel->hu->tty->dev)
386 return;
387
Loic Poulain74cdad32015-09-02 12:04:13 +0200388 /* Link is busy, delay the suspend */
389 mutex_lock(&intel_device_list_lock);
390 list_for_each(p, &intel_device_list) {
391 struct intel_device *idev = list_entry(p, struct intel_device,
392 list);
393
394 if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
395 pm_runtime_get(&idev->pdev->dev);
396 pm_runtime_mark_last_busy(&idev->pdev->dev);
397 pm_runtime_put_autosuspend(&idev->pdev->dev);
398 break;
399 }
400 }
401 mutex_unlock(&intel_device_list_lock);
402}
403
Loic Poulainca93cee2015-07-01 12:20:26 +0200404static int intel_open(struct hci_uart *hu)
405{
406 struct intel_data *intel;
407
408 BT_DBG("hu %p", hu);
409
410 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
411 if (!intel)
412 return -ENOMEM;
413
414 skb_queue_head_init(&intel->txq);
Loic Poulain74cdad32015-09-02 12:04:13 +0200415 INIT_WORK(&intel->busy_work, intel_busy_work);
416
417 intel->hu = hu;
Loic Poulainca93cee2015-07-01 12:20:26 +0200418
419 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200420
421 if (!intel_set_power(hu, true))
422 set_bit(STATE_BOOTING, &intel->flags);
423
Loic Poulainca93cee2015-07-01 12:20:26 +0200424 return 0;
425}
426
427static int intel_close(struct hci_uart *hu)
428{
429 struct intel_data *intel = hu->priv;
430
431 BT_DBG("hu %p", hu);
432
Loic Poulain74cdad32015-09-02 12:04:13 +0200433 cancel_work_sync(&intel->busy_work);
434
Loic Poulain1ab1f232015-08-27 07:21:51 +0200435 intel_set_power(hu, false);
436
Loic Poulainca93cee2015-07-01 12:20:26 +0200437 skb_queue_purge(&intel->txq);
438 kfree_skb(intel->rx_skb);
439 kfree(intel);
440
441 hu->priv = NULL;
442 return 0;
443}
444
445static int intel_flush(struct hci_uart *hu)
446{
447 struct intel_data *intel = hu->priv;
448
449 BT_DBG("hu %p", hu);
450
451 skb_queue_purge(&intel->txq);
452
453 return 0;
454}
455
456static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
457{
458 struct sk_buff *skb;
459 struct hci_event_hdr *hdr;
460 struct hci_ev_cmd_complete *evt;
461
462 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
463 if (!skb)
464 return -ENOMEM;
465
Johannes Berg4df864c2017-06-16 14:29:21 +0200466 hdr = skb_put(skb, sizeof(*hdr));
Loic Poulainca93cee2015-07-01 12:20:26 +0200467 hdr->evt = HCI_EV_CMD_COMPLETE;
468 hdr->plen = sizeof(*evt) + 1;
469
Johannes Berg4df864c2017-06-16 14:29:21 +0200470 evt = skb_put(skb, sizeof(*evt));
Loic Poulainca93cee2015-07-01 12:20:26 +0200471 evt->ncmd = 0x01;
472 evt->opcode = cpu_to_le16(opcode);
473
Johannes Berg634fef62017-06-16 14:29:24 +0200474 skb_put_u8(skb, 0x00);
Loic Poulainca93cee2015-07-01 12:20:26 +0200475
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100476 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Loic Poulainca93cee2015-07-01 12:20:26 +0200477
478 return hci_recv_frame(hdev, skb);
479}
480
Loic Poulainff289552015-08-25 17:55:44 +0200481static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
482{
483 struct intel_data *intel = hu->priv;
484 struct hci_dev *hdev = hu->hdev;
485 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
486 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200487 int err;
488
489 /* This can be the first command sent to the chip, check
490 * that the controller is ready.
491 */
492 err = intel_wait_booting(hu);
493
494 clear_bit(STATE_BOOTING, &intel->flags);
495
496 /* In case of timeout, try to continue anyway */
Anton Protopopov2be11492016-02-10 12:22:54 -0500497 if (err && err != -ETIMEDOUT)
Loic Poulain1ab1f232015-08-27 07:21:51 +0200498 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200499
Loic Poulainf44e78a2015-08-31 18:34:30 +0200500 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200501
502 speed_cmd[3] = intel_convert_speed(speed);
503 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200504 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200505 return -EINVAL;
506 }
507
508 /* Device will not accept speed change if Intel version has not been
509 * previously requested.
510 */
Loic Poulaina0c38242015-11-27 19:55:59 +0100511 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
Loic Poulainff289552015-08-25 17:55:44 +0200512 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200513 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
514 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200515 return PTR_ERR(skb);
516 }
517 kfree_skb(skb);
518
519 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
520 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200521 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200522 return -ENOMEM;
523 }
524
Johannes Berg59ae1d12017-06-16 14:29:20 +0200525 skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100526 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Loic Poulainff289552015-08-25 17:55:44 +0200527
528 hci_uart_set_flow_control(hu, true);
529
530 skb_queue_tail(&intel->txq, skb);
531 hci_uart_tx_wakeup(hu);
532
533 /* wait 100ms to change baudrate on controller side */
534 msleep(100);
535
536 hci_uart_set_baudrate(hu, speed);
537 hci_uart_set_flow_control(hu, false);
538
539 return 0;
540}
541
Loic Poulainca93cee2015-07-01 12:20:26 +0200542static int intel_setup(struct hci_uart *hu)
543{
Loic Poulainca93cee2015-07-01 12:20:26 +0200544 struct intel_data *intel = hu->priv;
545 struct hci_dev *hdev = hu->hdev;
546 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +0100547 struct intel_version ver;
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800548 struct intel_boot_params params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200549 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200550 const struct firmware *fw;
551 const u8 *fw_ptr;
552 char fwname[64];
553 u32 frag_len;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -0800554 u32 boot_param;
Loic Poulainca93cee2015-07-01 12:20:26 +0200555 ktime_t calltime, delta, rettime;
556 unsigned long long duration;
Loic Poulainff289552015-08-25 17:55:44 +0200557 unsigned int init_speed, oper_speed;
558 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200559 int err;
560
Loic Poulainf44e78a2015-08-31 18:34:30 +0200561 bt_dev_dbg(hdev, "start intel_setup");
Loic Poulainca93cee2015-07-01 12:20:26 +0200562
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +0200563 hu->hdev->set_diag = btintel_set_diag;
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200564 hu->hdev->set_bdaddr = btintel_set_bdaddr;
565
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -0800566 /* Set the default boot parameter to 0x0 and it is updated to
567 * SKU specific boot parameter after reading Intel_Write_Boot_Params
568 * command while downloading the firmware.
569 */
570 boot_param = 0x00000000;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -0800571
Loic Poulainca93cee2015-07-01 12:20:26 +0200572 calltime = ktime_get();
573
Loic Poulainff289552015-08-25 17:55:44 +0200574 if (hu->init_speed)
575 init_speed = hu->init_speed;
576 else
577 init_speed = hu->proto->init_speed;
578
579 if (hu->oper_speed)
580 oper_speed = hu->oper_speed;
581 else
582 oper_speed = hu->proto->oper_speed;
583
584 if (oper_speed && init_speed && oper_speed != init_speed)
585 speed_change = 1;
586
Loic Poulain1ab1f232015-08-27 07:21:51 +0200587 /* Check that the controller is ready */
588 err = intel_wait_booting(hu);
589
590 clear_bit(STATE_BOOTING, &intel->flags);
591
592 /* In case of timeout, try to continue anyway */
Anton Protopopov2be11492016-02-10 12:22:54 -0500593 if (err && err != -ETIMEDOUT)
Loic Poulain1ab1f232015-08-27 07:21:51 +0200594 return err;
595
Loic Poulainca93cee2015-07-01 12:20:26 +0200596 set_bit(STATE_BOOTLOADER, &intel->flags);
597
598 /* Read the Intel version information to determine if the device
599 * is in bootloader mode or if it already has operational firmware
600 * loaded.
601 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100602 err = btintel_read_version(hdev, &ver);
603 if (err)
Loic Poulainca93cee2015-07-01 12:20:26 +0200604 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200605
606 /* The hardware platform number has a fixed value of 0x37 and
607 * for now only accept this single value.
608 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100609 if (ver.hw_platform != 0x37) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200610 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100611 ver.hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200612 return -EINVAL;
613 }
614
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -0800615 /* Check for supported iBT hardware variants of this firmware
616 * loading method.
617 *
618 * This check has been put in place to ensure correct forward
619 * compatibility options when newer hardware variants come along.
620 */
621 switch (ver.hw_variant) {
622 case 0x0b: /* LnP */
623 case 0x0c: /* WsP */
Tedd Ho-Jeong An6c7bb7e2017-03-06 15:38:35 -0800624 case 0x12: /* ThP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -0800625 break;
626 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +0200627 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100628 ver.hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200629 return -EINVAL;
630 }
631
Loic Poulain6c483de2015-12-06 16:18:34 +0100632 btintel_version_info(hdev, &ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200633
634 /* The firmware variant determines if the device is in bootloader
635 * mode or is running operational firmware. The value 0x06 identifies
636 * the bootloader and the value 0x23 identifies the operational
637 * firmware.
638 *
639 * When the operational firmware is already present, then only
640 * the check for valid Bluetooth device address is needed. This
641 * determines if the device will be added as configured or
642 * unconfigured controller.
643 *
644 * It is not possible to use the Secure Boot Parameters in this
645 * case since that command is only available in bootloader mode.
646 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100647 if (ver.fw_variant == 0x23) {
Loic Poulainca93cee2015-07-01 12:20:26 +0200648 clear_bit(STATE_BOOTLOADER, &intel->flags);
649 btintel_check_bdaddr(hdev);
650 return 0;
651 }
652
653 /* If the device is not in bootloader mode, then the only possible
654 * choice is to return an error and abort the device initialization.
655 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100656 if (ver.fw_variant != 0x06) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200657 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100658 ver.fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200659 return -ENODEV;
660 }
661
Loic Poulainca93cee2015-07-01 12:20:26 +0200662 /* Read the secure boot parameters to identify the operating
663 * details of the bootloader.
664 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800665 err = btintel_read_boot_params(hdev, &params);
666 if (err)
Loic Poulainca93cee2015-07-01 12:20:26 +0200667 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200668
669 /* It is required that every single firmware fragment is acknowledged
670 * with a command complete event. If the boot parameters indicate
671 * that this bootloader does not send them, then abort the setup.
672 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800673 if (params.limited_cce != 0x00) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200674 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800675 params.limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200676 return -EINVAL;
677 }
678
679 /* If the OTP has no valid Bluetooth device address, then there will
680 * also be no valid address for the operational firmware.
681 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800682 if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200683 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200684 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
685 }
686
687 /* With this Intel bootloader only the hardware variant and device
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800688 * revision information are used to select the right firmware for SfP
689 * and WsP.
Loic Poulainca93cee2015-07-01 12:20:26 +0200690 *
Tedd Ho-Jeong Anb7da6a62017-03-06 15:38:32 -0800691 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
692 *
693 * Currently the supported hardware variants are:
694 * 11 (0x0b) for iBT 3.0 (LnP/SfP)
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800695 * 12 (0x0c) for iBT 3.5 (WsP)
696 *
697 * For ThP/JfP and for future SKU's, the FW name varies based on HW
698 * variant, HW revision and FW revision, as these are dependent on CNVi
699 * and RF Combination.
700 *
701 * 18 (0x12) for iBT3.5 (ThP/JfP)
702 *
703 * The firmware file name for these will be
704 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
705 *
Loic Poulainca93cee2015-07-01 12:20:26 +0200706 */
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800707 switch (ver.hw_variant) {
708 case 0x0b: /* SfP */
709 case 0x0c: /* WsP */
710 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
711 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800712 le16_to_cpu(params.dev_revid));
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800713 break;
714 case 0x12: /* ThP */
715 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
716 le16_to_cpu(ver.hw_variant),
717 le16_to_cpu(ver.hw_revision),
718 le16_to_cpu(ver.fw_revision));
719 break;
720 default:
721 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
722 ver.hw_variant);
723 return -EINVAL;
724 }
Loic Poulainca93cee2015-07-01 12:20:26 +0200725
726 err = request_firmware(&fw, fwname, &hdev->dev);
727 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200728 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
729 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200730 return err;
731 }
732
Loic Poulainf44e78a2015-08-31 18:34:30 +0200733 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200734
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200735 /* Save the DDC file name for later */
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800736 switch (ver.hw_variant) {
737 case 0x0b: /* SfP */
738 case 0x0c: /* WsP */
739 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
740 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -0800741 le16_to_cpu(params.dev_revid));
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800742 break;
743 case 0x12: /* ThP */
744 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
745 le16_to_cpu(ver.hw_variant),
746 le16_to_cpu(ver.hw_revision),
747 le16_to_cpu(ver.fw_revision));
748 break;
749 default:
750 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
751 ver.hw_variant);
752 return -EINVAL;
753 }
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200754
Loic Poulainca93cee2015-07-01 12:20:26 +0200755 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200756 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
757 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200758 err = -EBADF;
759 goto done;
760 }
761
762 set_bit(STATE_DOWNLOADING, &intel->flags);
763
764 /* Start the firmware download transaction with the Init fragment
765 * represented by the 128 bytes of CSS header.
766 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200767 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200768 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200769 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200770 goto done;
771 }
772
773 /* Send the 256 bytes of public key information from the firmware
774 * as the PKey fragment.
775 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200776 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200777 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200778 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
779 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200780 goto done;
781 }
782
783 /* Send the 256 bytes of signature information from the firmware
784 * as the Sign fragment.
785 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200786 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200787 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200788 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
789 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200790 goto done;
791 }
792
793 fw_ptr = fw->data + 644;
794 frag_len = 0;
795
796 while (fw_ptr - fw->data < fw->size) {
797 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
798
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -0800799 /* Each SKU has a different reset parameter to use in the
800 * HCI_Intel_Reset command and it is embedded in the firmware
801 * data. So, instead of using static value per SKU, check
802 * the firmware data and save it for later use.
803 */
804 if (cmd->opcode == 0xfc0e) {
805 /* The boot parameter is the first 32-bit value
806 * and rest of 3 octets are reserved.
807 */
808 boot_param = get_unaligned_le32(fw_ptr + sizeof(*cmd));
809
810 bt_dev_dbg(hdev, "boot_param=0x%x", boot_param);
811 }
812
Loic Poulainca93cee2015-07-01 12:20:26 +0200813 frag_len += sizeof(*cmd) + cmd->plen;
814
Loic Poulainf44e78a2015-08-31 18:34:30 +0200815 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
816 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200817
818 /* The parameter length of the secure send command requires
819 * a 4 byte alignment. It happens so that the firmware file
820 * contains proper Intel_NOP commands to align the fragments
821 * as needed.
822 *
823 * Send set of commands with 4 byte alignment from the
824 * firmware data buffer as a single Data fragement.
825 */
826 if (frag_len % 4)
827 continue;
828
829 /* Send each command from the firmware data buffer as
830 * a single Data fragment.
831 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200832 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200833 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200834 bt_dev_err(hdev, "Failed to send firmware data (%d)",
835 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200836 goto done;
837 }
838
839 fw_ptr += frag_len;
840 frag_len = 0;
841 }
842
843 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
844
Loic Poulainf44e78a2015-08-31 18:34:30 +0200845 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200846
847 /* Before switching the device into operational mode and with that
848 * booting the loaded firmware, wait for the bootloader notification
849 * that all fragments have been successfully received.
850 *
851 * When the event processing receives the notification, then the
852 * STATE_DOWNLOADING flag will be cleared.
853 *
854 * The firmware loading should not take longer than 5 seconds
855 * and thus just timeout if that happens and fail the setup
856 * of this device.
857 */
858 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
859 TASK_INTERRUPTIBLE,
860 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700861 if (err == -EINTR) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200862 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200863 err = -EINTR;
864 goto done;
865 }
866
867 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200868 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200869 err = -ETIMEDOUT;
870 goto done;
871 }
872
873 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200874 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200875 err = -ENOEXEC;
876 goto done;
877 }
878
879 rettime = ktime_get();
880 delta = ktime_sub(rettime, calltime);
881 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
882
Loic Poulainf44e78a2015-08-31 18:34:30 +0200883 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200884
885done:
886 release_firmware(fw);
887
888 if (err < 0)
889 return err;
890
Loic Poulainff289552015-08-25 17:55:44 +0200891 /* We need to restore the default speed before Intel reset */
892 if (speed_change) {
893 err = intel_set_baudrate(hu, init_speed);
894 if (err)
895 return err;
896 }
897
Loic Poulainca93cee2015-07-01 12:20:26 +0200898 calltime = ktime_get();
899
900 set_bit(STATE_BOOTING, &intel->flags);
901
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -0800902 err = btintel_send_intel_reset(hdev, boot_param);
903 if (err)
904 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200905
906 /* The bootloader will not indicate when the device is ready. This
907 * is done by the operational firmware sending bootup notification.
908 *
909 * Booting into operational firmware should not take longer than
910 * 1 second. However if that happens, then just fail the setup
911 * since something went wrong.
912 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200913 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200914
Loic Poulain1ab1f232015-08-27 07:21:51 +0200915 err = intel_wait_booting(hu);
916 if (err)
917 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200918
Loic Poulain1ab1f232015-08-27 07:21:51 +0200919 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200920
921 rettime = ktime_get();
922 delta = ktime_sub(rettime, calltime);
923 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
924
Loic Poulainf44e78a2015-08-31 18:34:30 +0200925 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200926
Loic Poulain31eff262016-07-11 21:55:36 +0200927 /* Enable LPM if matching pdev with wakeup enabled, set TX active
928 * until further LPM TX notification.
929 */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200930 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200931 list_for_each(p, &intel_device_list) {
932 struct intel_device *dev = list_entry(p, struct intel_device,
933 list);
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200934 if (!hu->tty->dev)
935 break;
Loic Poulainb98469f2015-08-29 13:38:19 +0200936 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
Loic Poulain31eff262016-07-11 21:55:36 +0200937 if (device_may_wakeup(&dev->pdev->dev)) {
938 set_bit(STATE_LPM_ENABLED, &intel->flags);
939 set_bit(STATE_TX_ACTIVE, &intel->flags);
940 }
Loic Poulainb98469f2015-08-29 13:38:19 +0200941 break;
942 }
943 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200944 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200945
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200946 /* Ignore errors, device can work without DDC parameters */
947 btintel_load_ddc_config(hdev, fwname);
948
Loic Poulainff289552015-08-25 17:55:44 +0200949 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
950 if (IS_ERR(skb))
951 return PTR_ERR(skb);
952 kfree_skb(skb);
953
954 if (speed_change) {
955 err = intel_set_baudrate(hu, oper_speed);
956 if (err)
957 return err;
958 }
959
Loic Poulainf44e78a2015-08-31 18:34:30 +0200960 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200961
Loic Poulainca93cee2015-07-01 12:20:26 +0200962 clear_bit(STATE_BOOTLOADER, &intel->flags);
963
964 return 0;
965}
966
967static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
968{
969 struct hci_uart *hu = hci_get_drvdata(hdev);
970 struct intel_data *intel = hu->priv;
971 struct hci_event_hdr *hdr;
972
Loic Poulain1ab1f232015-08-27 07:21:51 +0200973 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
974 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200975 goto recv;
976
977 hdr = (void *)skb->data;
978
979 /* When the firmware loading completes the device sends
980 * out a vendor specific event indicating the result of
981 * the firmware loading.
982 */
983 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
984 skb->data[2] == 0x06) {
985 if (skb->data[3] != 0x00)
986 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
987
988 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
989 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
990 smp_mb__after_atomic();
991 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
992 }
993
994 /* When switching to the operational firmware the device
995 * sends a vendor specific event indicating that the bootup
996 * completed.
997 */
998 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
999 skb->data[2] == 0x02) {
1000 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
1001 smp_mb__after_atomic();
1002 wake_up_bit(&intel->flags, STATE_BOOTING);
1003 }
1004 }
1005recv:
1006 return hci_recv_frame(hdev, skb);
1007}
1008
Loic Poulainb98469f2015-08-29 13:38:19 +02001009static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
1010{
1011 struct hci_uart *hu = hci_get_drvdata(hdev);
1012 struct intel_data *intel = hu->priv;
1013
Loic Poulainf44e78a2015-08-31 18:34:30 +02001014 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +02001015
Loic Poulain74cdad32015-09-02 12:04:13 +02001016 if (value) {
Loic Poulainb98469f2015-08-29 13:38:19 +02001017 set_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001018 schedule_work(&intel->busy_work);
1019 } else {
Loic Poulainb98469f2015-08-29 13:38:19 +02001020 clear_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001021 }
Loic Poulainb98469f2015-08-29 13:38:19 +02001022}
1023
1024static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1025{
1026 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +02001027 struct hci_uart *hu = hci_get_drvdata(hdev);
1028 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +02001029
1030 switch (lpm->opcode) {
1031 case LPM_OP_TX_NOTIFY:
Loic Poulain1b197572015-09-02 12:04:14 +02001032 if (lpm->dlen < 1) {
1033 bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1034 break;
1035 }
1036 intel_recv_lpm_notify(hdev, lpm->data[0]);
Loic Poulainb98469f2015-08-29 13:38:19 +02001037 break;
Loic Poulain89436542015-09-02 12:04:11 +02001038 case LPM_OP_SUSPEND_ACK:
1039 set_bit(STATE_SUSPENDED, &intel->flags);
1040 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1041 smp_mb__after_atomic();
1042 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1043 }
1044 break;
1045 case LPM_OP_RESUME_ACK:
1046 clear_bit(STATE_SUSPENDED, &intel->flags);
1047 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1048 smp_mb__after_atomic();
1049 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1050 }
1051 break;
Loic Poulainb98469f2015-08-29 13:38:19 +02001052 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +02001053 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +02001054 break;
1055 }
1056
1057 kfree_skb(skb);
1058
1059 return 0;
1060}
1061
1062#define INTEL_RECV_LPM \
1063 .type = HCI_LPM_PKT, \
1064 .hlen = HCI_LPM_HDR_SIZE, \
1065 .loff = 1, \
1066 .lsize = 1, \
1067 .maxlen = HCI_LPM_MAX_SIZE
1068
Loic Poulainca93cee2015-07-01 12:20:26 +02001069static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +02001070 { H4_RECV_ACL, .recv = hci_recv_frame },
1071 { H4_RECV_SCO, .recv = hci_recv_frame },
1072 { H4_RECV_EVENT, .recv = intel_recv_event },
1073 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +02001074};
1075
1076static int intel_recv(struct hci_uart *hu, const void *data, int count)
1077{
1078 struct intel_data *intel = hu->priv;
1079
1080 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1081 return -EUNATCH;
1082
1083 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1084 intel_recv_pkts,
1085 ARRAY_SIZE(intel_recv_pkts));
1086 if (IS_ERR(intel->rx_skb)) {
1087 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001088 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001089 intel->rx_skb = NULL;
1090 return err;
1091 }
1092
1093 return count;
1094}
1095
1096static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1097{
1098 struct intel_data *intel = hu->priv;
Loic Poulain74cdad32015-09-02 12:04:13 +02001099 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +02001100
1101 BT_DBG("hu %p skb %p", hu, skb);
1102
Johan Hovolddcb9cfa2017-03-29 18:15:28 +02001103 if (!hu->tty->dev)
1104 goto out_enqueue;
1105
Loic Poulain74cdad32015-09-02 12:04:13 +02001106 /* Be sure our controller is resumed and potential LPM transaction
1107 * completed before enqueuing any packet.
1108 */
1109 mutex_lock(&intel_device_list_lock);
1110 list_for_each(p, &intel_device_list) {
1111 struct intel_device *idev = list_entry(p, struct intel_device,
1112 list);
1113
1114 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1115 pm_runtime_get_sync(&idev->pdev->dev);
1116 pm_runtime_mark_last_busy(&idev->pdev->dev);
1117 pm_runtime_put_autosuspend(&idev->pdev->dev);
1118 break;
1119 }
1120 }
1121 mutex_unlock(&intel_device_list_lock);
Johan Hovolddcb9cfa2017-03-29 18:15:28 +02001122out_enqueue:
Loic Poulainca93cee2015-07-01 12:20:26 +02001123 skb_queue_tail(&intel->txq, skb);
1124
1125 return 0;
1126}
1127
1128static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1129{
1130 struct intel_data *intel = hu->priv;
1131 struct sk_buff *skb;
1132
1133 skb = skb_dequeue(&intel->txq);
1134 if (!skb)
1135 return skb;
1136
1137 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001138 (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
Loic Poulainca93cee2015-07-01 12:20:26 +02001139 struct hci_command_hdr *cmd = (void *)skb->data;
1140 __u16 opcode = le16_to_cpu(cmd->opcode);
1141
1142 /* When the 0xfc01 command is issued to boot into
1143 * the operational firmware, it will actually not
1144 * send a command complete event. To keep the flow
1145 * control working inject that event here.
1146 */
1147 if (opcode == 0xfc01)
1148 inject_cmd_complete(hu->hdev, opcode);
1149 }
1150
1151 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001152 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Loic Poulainca93cee2015-07-01 12:20:26 +02001153
1154 return skb;
1155}
1156
1157static const struct hci_uart_proto intel_proto = {
1158 .id = HCI_UART_INTEL,
1159 .name = "Intel",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001160 .manufacturer = 2,
Loic Poulainca93cee2015-07-01 12:20:26 +02001161 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001162 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001163 .open = intel_open,
1164 .close = intel_close,
1165 .flush = intel_flush,
1166 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001167 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001168 .recv = intel_recv,
1169 .enqueue = intel_enqueue,
1170 .dequeue = intel_dequeue,
1171};
1172
Loic Poulain1ab1f232015-08-27 07:21:51 +02001173#ifdef CONFIG_ACPI
1174static const struct acpi_device_id intel_acpi_match[] = {
1175 { "INT33E1", 0 },
1176 { },
1177};
1178MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001179#endif
1180
Loic Poulain74cdad32015-09-02 12:04:13 +02001181#ifdef CONFIG_PM
Loic Poulainf7552472015-09-09 21:04:15 +02001182static int intel_suspend_device(struct device *dev)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001183{
1184 struct intel_device *idev = dev_get_drvdata(dev);
1185
Loic Poulainaa6802d2015-09-02 12:04:12 +02001186 mutex_lock(&idev->hu_lock);
1187 if (idev->hu)
1188 intel_lpm_suspend(idev->hu);
1189 mutex_unlock(&idev->hu_lock);
1190
1191 return 0;
1192}
1193
Loic Poulainf7552472015-09-09 21:04:15 +02001194static int intel_resume_device(struct device *dev)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001195{
1196 struct intel_device *idev = dev_get_drvdata(dev);
1197
Loic Poulainaa6802d2015-09-02 12:04:12 +02001198 mutex_lock(&idev->hu_lock);
1199 if (idev->hu)
1200 intel_lpm_resume(idev->hu);
1201 mutex_unlock(&idev->hu_lock);
1202
1203 return 0;
1204}
1205#endif
1206
Loic Poulainf7552472015-09-09 21:04:15 +02001207#ifdef CONFIG_PM_SLEEP
1208static int intel_suspend(struct device *dev)
1209{
1210 struct intel_device *idev = dev_get_drvdata(dev);
1211
1212 if (device_may_wakeup(dev))
1213 enable_irq_wake(idev->irq);
1214
1215 return intel_suspend_device(dev);
1216}
1217
1218static int intel_resume(struct device *dev)
1219{
1220 struct intel_device *idev = dev_get_drvdata(dev);
1221
1222 if (device_may_wakeup(dev))
1223 disable_irq_wake(idev->irq);
1224
1225 return intel_resume_device(dev);
1226}
1227#endif
1228
Loic Poulainaa6802d2015-09-02 12:04:12 +02001229static const struct dev_pm_ops intel_pm_ops = {
1230 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Loic Poulainf7552472015-09-09 21:04:15 +02001231 SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001232};
1233
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001234static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
1235static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
1236
1237static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
1238 { "reset-gpios", &reset_gpios, 1 },
1239 { "host-wake-gpios", &host_wake_gpios, 1 },
1240 { },
1241};
1242
Loic Poulain1ab1f232015-08-27 07:21:51 +02001243static int intel_probe(struct platform_device *pdev)
1244{
1245 struct intel_device *idev;
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001246 int ret;
Loic Poulain1ab1f232015-08-27 07:21:51 +02001247
1248 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1249 if (!idev)
1250 return -ENOMEM;
1251
Loic Poulainaa6802d2015-09-02 12:04:12 +02001252 mutex_init(&idev->hu_lock);
1253
Loic Poulain1ab1f232015-08-27 07:21:51 +02001254 idev->pdev = pdev;
1255
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001256 ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
1257 if (ret)
1258 dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
1259
Loic Poulain32b9ccb2016-04-28 18:48:25 +02001260 idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001261 if (IS_ERR(idev->reset)) {
1262 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1263 return PTR_ERR(idev->reset);
1264 }
1265
Loic Poulain765ea3a2015-08-29 13:38:18 +02001266 idev->irq = platform_get_irq(pdev, 0);
1267 if (idev->irq < 0) {
1268 struct gpio_desc *host_wake;
1269
1270 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1271
Loic Poulain32b9ccb2016-04-28 18:48:25 +02001272 host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
Loic Poulain765ea3a2015-08-29 13:38:18 +02001273 if (IS_ERR(host_wake)) {
1274 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1275 goto no_irq;
1276 }
1277
1278 idev->irq = gpiod_to_irq(host_wake);
1279 if (idev->irq < 0) {
1280 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1281 goto no_irq;
1282 }
1283 }
1284
1285 /* Only enable wake-up/irq when controller is powered */
1286 device_set_wakeup_capable(&pdev->dev, true);
1287 device_wakeup_disable(&pdev->dev);
1288
1289no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001290 platform_set_drvdata(pdev, idev);
1291
1292 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001293 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001294 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001295 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001296
Loic Poulain765ea3a2015-08-29 13:38:18 +02001297 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1298 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001299
1300 return 0;
1301}
1302
1303static int intel_remove(struct platform_device *pdev)
1304{
1305 struct intel_device *idev = platform_get_drvdata(pdev);
1306
Loic Poulain765ea3a2015-08-29 13:38:18 +02001307 device_wakeup_disable(&pdev->dev);
1308
Loic Poulain67c8bde2015-08-31 18:34:31 +02001309 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001310 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001311 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001312
1313 dev_info(&pdev->dev, "unregistered.\n");
1314
1315 return 0;
1316}
1317
1318static struct platform_driver intel_driver = {
1319 .probe = intel_probe,
1320 .remove = intel_remove,
1321 .driver = {
1322 .name = "hci_intel",
1323 .acpi_match_table = ACPI_PTR(intel_acpi_match),
Loic Poulainaa6802d2015-09-02 12:04:12 +02001324 .pm = &intel_pm_ops,
Loic Poulain1ab1f232015-08-27 07:21:51 +02001325 },
1326};
1327
Loic Poulainca93cee2015-07-01 12:20:26 +02001328int __init intel_init(void)
1329{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001330 platform_driver_register(&intel_driver);
1331
Loic Poulainca93cee2015-07-01 12:20:26 +02001332 return hci_uart_register_proto(&intel_proto);
1333}
1334
1335int __exit intel_deinit(void)
1336{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001337 platform_driver_unregister(&intel_driver);
1338
Loic Poulainca93cee2015-07-01 12:20:26 +02001339 return hci_uart_unregister_proto(&intel_proto);
1340}