blob: 97586848ec97c2bd16ce192ff07ba840b2109f64 [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>
Marcel Holtmann16e38872015-04-04 16:13:02 -070036
37#include <net/bluetooth/bluetooth.h>
38#include <net/bluetooth/hci_core.h>
39
40#include "hci_uart.h"
Loic Poulainca93cee2015-07-01 12:20:26 +020041#include "btintel.h"
42
43#define STATE_BOOTLOADER 0
44#define STATE_DOWNLOADING 1
45#define STATE_FIRMWARE_LOADED 2
46#define STATE_FIRMWARE_FAILED 3
47#define STATE_BOOTING 4
Loic Poulainb98469f2015-08-29 13:38:19 +020048#define STATE_LPM_ENABLED 5
49#define STATE_TX_ACTIVE 6
Loic Poulain89436542015-09-02 12:04:11 +020050#define STATE_SUSPENDED 7
51#define STATE_LPM_TRANSACTION 8
Loic Poulainb98469f2015-08-29 13:38:19 +020052
Loic Poulain89436542015-09-02 12:04:11 +020053#define HCI_LPM_WAKE_PKT 0xf0
Loic Poulainb98469f2015-08-29 13:38:19 +020054#define HCI_LPM_PKT 0xf1
55#define HCI_LPM_MAX_SIZE 10
56#define HCI_LPM_HDR_SIZE HCI_EVENT_HDR_SIZE
57
58#define LPM_OP_TX_NOTIFY 0x00
Loic Poulain89436542015-09-02 12:04:11 +020059#define LPM_OP_SUSPEND_ACK 0x02
60#define LPM_OP_RESUME_ACK 0x03
Loic Poulainb98469f2015-08-29 13:38:19 +020061
Loic Poulain74cdad32015-09-02 12:04:13 +020062#define LPM_SUSPEND_DELAY_MS 1000
63
Loic Poulainb98469f2015-08-29 13:38:19 +020064struct hci_lpm_pkt {
65 __u8 opcode;
66 __u8 dlen;
67 __u8 data[0];
68} __packed;
Loic Poulainca93cee2015-07-01 12:20:26 +020069
Loic Poulain1ab1f232015-08-27 07:21:51 +020070struct intel_device {
71 struct list_head list;
72 struct platform_device *pdev;
73 struct gpio_desc *reset;
Loic Poulainaa6802d2015-09-02 12:04:12 +020074 struct hci_uart *hu;
75 struct mutex hu_lock;
Loic Poulain765ea3a2015-08-29 13:38:18 +020076 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020077};
78
79static LIST_HEAD(intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +020080static DEFINE_MUTEX(intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +020081
Loic Poulainca93cee2015-07-01 12:20:26 +020082struct intel_data {
83 struct sk_buff *rx_skb;
84 struct sk_buff_head txq;
Loic Poulain74cdad32015-09-02 12:04:13 +020085 struct work_struct busy_work;
86 struct hci_uart *hu;
Loic Poulainca93cee2015-07-01 12:20:26 +020087 unsigned long flags;
88};
89
Loic Poulainff289552015-08-25 17:55:44 +020090static u8 intel_convert_speed(unsigned int speed)
91{
92 switch (speed) {
93 case 9600:
94 return 0x00;
95 case 19200:
96 return 0x01;
97 case 38400:
98 return 0x02;
99 case 57600:
100 return 0x03;
101 case 115200:
102 return 0x04;
103 case 230400:
104 return 0x05;
105 case 460800:
106 return 0x06;
107 case 921600:
108 return 0x07;
109 case 1843200:
110 return 0x08;
111 case 3250000:
112 return 0x09;
113 case 2000000:
114 return 0x0a;
115 case 3000000:
116 return 0x0b;
117 default:
118 return 0xff;
119 }
120}
121
Loic Poulain1ab1f232015-08-27 07:21:51 +0200122static int intel_wait_booting(struct hci_uart *hu)
123{
124 struct intel_data *intel = hu->priv;
125 int err;
126
127 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
128 TASK_INTERRUPTIBLE,
129 msecs_to_jiffies(1000));
130
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700131 if (err == -EINTR) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200132 bt_dev_err(hu->hdev, "Device boot interrupted");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200133 return -EINTR;
134 }
135
136 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200137 bt_dev_err(hu->hdev, "Device boot timeout");
Loic Poulain1ab1f232015-08-27 07:21:51 +0200138 return -ETIMEDOUT;
139 }
140
141 return err;
142}
143
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200144#ifdef CONFIG_PM
Loic Poulain89436542015-09-02 12:04:11 +0200145static int intel_wait_lpm_transaction(struct hci_uart *hu)
146{
147 struct intel_data *intel = hu->priv;
148 int err;
149
150 err = wait_on_bit_timeout(&intel->flags, STATE_LPM_TRANSACTION,
151 TASK_INTERRUPTIBLE,
152 msecs_to_jiffies(1000));
153
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700154 if (err == -EINTR) {
Loic Poulain89436542015-09-02 12:04:11 +0200155 bt_dev_err(hu->hdev, "LPM transaction interrupted");
156 return -EINTR;
157 }
158
159 if (err) {
160 bt_dev_err(hu->hdev, "LPM transaction timeout");
161 return -ETIMEDOUT;
162 }
163
164 return err;
165}
166
167static int intel_lpm_suspend(struct hci_uart *hu)
168{
169 static const u8 suspend[] = { 0x01, 0x01, 0x01 };
170 struct intel_data *intel = hu->priv;
171 struct sk_buff *skb;
172
173 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
174 test_bit(STATE_SUSPENDED, &intel->flags))
175 return 0;
176
177 if (test_bit(STATE_TX_ACTIVE, &intel->flags))
178 return -EAGAIN;
179
180 bt_dev_dbg(hu->hdev, "Suspending");
181
182 skb = bt_skb_alloc(sizeof(suspend), GFP_KERNEL);
183 if (!skb) {
184 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
185 return -ENOMEM;
186 }
187
Johannes Berg59ae1d12017-06-16 14:29:20 +0200188 skb_put_data(skb, suspend, sizeof(suspend));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100189 hci_skb_pkt_type(skb) = HCI_LPM_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200190
191 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
192
Loic Poulain30e945f2015-09-09 19:08:02 +0200193 /* LPM flow is a priority, enqueue packet at list head */
194 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200195 hci_uart_tx_wakeup(hu);
196
197 intel_wait_lpm_transaction(hu);
198 /* Even in case of failure, continue and test the suspended flag */
199
200 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
201
202 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
203 bt_dev_err(hu->hdev, "Device suspend error");
204 return -EINVAL;
205 }
206
207 bt_dev_dbg(hu->hdev, "Suspended");
208
209 hci_uart_set_flow_control(hu, true);
210
211 return 0;
212}
213
214static int intel_lpm_resume(struct hci_uart *hu)
215{
216 struct intel_data *intel = hu->priv;
217 struct sk_buff *skb;
218
219 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
220 !test_bit(STATE_SUSPENDED, &intel->flags))
221 return 0;
222
223 bt_dev_dbg(hu->hdev, "Resuming");
224
225 hci_uart_set_flow_control(hu, false);
226
227 skb = bt_skb_alloc(0, GFP_KERNEL);
228 if (!skb) {
229 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
230 return -ENOMEM;
231 }
232
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100233 hci_skb_pkt_type(skb) = HCI_LPM_WAKE_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200234
235 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
236
Loic Poulain30e945f2015-09-09 19:08:02 +0200237 /* LPM flow is a priority, enqueue packet at list head */
238 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200239 hci_uart_tx_wakeup(hu);
240
241 intel_wait_lpm_transaction(hu);
242 /* Even in case of failure, continue and test the suspended flag */
243
244 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
245
246 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
247 bt_dev_err(hu->hdev, "Device resume error");
248 return -EINVAL;
249 }
250
251 bt_dev_dbg(hu->hdev, "Resumed");
252
253 return 0;
254}
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200255#endif /* CONFIG_PM */
Loic Poulain89436542015-09-02 12:04:11 +0200256
257static int intel_lpm_host_wake(struct hci_uart *hu)
258{
259 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
260 struct intel_data *intel = hu->priv;
261 struct sk_buff *skb;
262
263 hci_uart_set_flow_control(hu, false);
264
265 clear_bit(STATE_SUSPENDED, &intel->flags);
266
267 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
268 if (!skb) {
269 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
270 return -ENOMEM;
271 }
272
Johannes Berg59ae1d12017-06-16 14:29:20 +0200273 skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100274 hci_skb_pkt_type(skb) = HCI_LPM_PKT;
Loic Poulain89436542015-09-02 12:04:11 +0200275
Loic Poulain30e945f2015-09-09 19:08:02 +0200276 /* LPM flow is a priority, enqueue packet at list head */
277 skb_queue_head(&intel->txq, skb);
Loic Poulain89436542015-09-02 12:04:11 +0200278 hci_uart_tx_wakeup(hu);
279
280 bt_dev_dbg(hu->hdev, "Resumed by controller");
281
282 return 0;
283}
284
Loic Poulain765ea3a2015-08-29 13:38:18 +0200285static irqreturn_t intel_irq(int irq, void *dev_id)
286{
287 struct intel_device *idev = dev_id;
288
289 dev_info(&idev->pdev->dev, "hci_intel irq\n");
290
Loic Poulainaa6802d2015-09-02 12:04:12 +0200291 mutex_lock(&idev->hu_lock);
292 if (idev->hu)
293 intel_lpm_host_wake(idev->hu);
294 mutex_unlock(&idev->hu_lock);
295
Loic Poulain74cdad32015-09-02 12:04:13 +0200296 /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
297 pm_runtime_get(&idev->pdev->dev);
298 pm_runtime_mark_last_busy(&idev->pdev->dev);
299 pm_runtime_put_autosuspend(&idev->pdev->dev);
300
Loic Poulain765ea3a2015-08-29 13:38:18 +0200301 return IRQ_HANDLED;
302}
303
Loic Poulain1ab1f232015-08-27 07:21:51 +0200304static int intel_set_power(struct hci_uart *hu, bool powered)
305{
306 struct list_head *p;
307 int err = -ENODEV;
308
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200309 if (!hu->tty->dev)
310 return err;
311
Loic Poulain67c8bde2015-08-31 18:34:31 +0200312 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200313
314 list_for_each(p, &intel_device_list) {
315 struct intel_device *idev = list_entry(p, struct intel_device,
316 list);
317
318 /* tty device and pdev device should share the same parent
319 * which is the UART port.
320 */
321 if (hu->tty->dev->parent != idev->pdev->dev.parent)
322 continue;
323
324 if (!idev->reset) {
325 err = -ENOTSUPP;
326 break;
327 }
328
329 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
330 hu, dev_name(&idev->pdev->dev), powered);
331
332 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200333
Loic Poulainaa6802d2015-09-02 12:04:12 +0200334 /* Provide to idev a hu reference which is used to run LPM
335 * transactions (lpm suspend/resume) from PM callbacks.
336 * hu needs to be protected against concurrent removing during
337 * these PM ops.
338 */
339 mutex_lock(&idev->hu_lock);
340 idev->hu = powered ? hu : NULL;
341 mutex_unlock(&idev->hu_lock);
342
Loic Poulain765ea3a2015-08-29 13:38:18 +0200343 if (idev->irq < 0)
344 break;
345
346 if (powered && device_can_wakeup(&idev->pdev->dev)) {
347 err = devm_request_threaded_irq(&idev->pdev->dev,
348 idev->irq, NULL,
349 intel_irq,
350 IRQF_ONESHOT,
351 "bt-host-wake", idev);
352 if (err) {
353 BT_ERR("hu %p, unable to allocate irq-%d",
354 hu, idev->irq);
355 break;
356 }
357
358 device_wakeup_enable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200359
360 pm_runtime_set_active(&idev->pdev->dev);
361 pm_runtime_use_autosuspend(&idev->pdev->dev);
362 pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
363 LPM_SUSPEND_DELAY_MS);
364 pm_runtime_enable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200365 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
366 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
367 device_wakeup_disable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200368
369 pm_runtime_disable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200370 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200371 }
372
Loic Poulain67c8bde2015-08-31 18:34:31 +0200373 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200374
375 return err;
376}
377
Loic Poulain74cdad32015-09-02 12:04:13 +0200378static void intel_busy_work(struct work_struct *work)
379{
380 struct list_head *p;
381 struct intel_data *intel = container_of(work, struct intel_data,
382 busy_work);
383
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200384 if (!intel->hu->tty->dev)
385 return;
386
Loic Poulain74cdad32015-09-02 12:04:13 +0200387 /* Link is busy, delay the suspend */
388 mutex_lock(&intel_device_list_lock);
389 list_for_each(p, &intel_device_list) {
390 struct intel_device *idev = list_entry(p, struct intel_device,
391 list);
392
393 if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
394 pm_runtime_get(&idev->pdev->dev);
395 pm_runtime_mark_last_busy(&idev->pdev->dev);
396 pm_runtime_put_autosuspend(&idev->pdev->dev);
397 break;
398 }
399 }
400 mutex_unlock(&intel_device_list_lock);
401}
402
Loic Poulainca93cee2015-07-01 12:20:26 +0200403static int intel_open(struct hci_uart *hu)
404{
405 struct intel_data *intel;
406
407 BT_DBG("hu %p", hu);
408
409 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
410 if (!intel)
411 return -ENOMEM;
412
413 skb_queue_head_init(&intel->txq);
Loic Poulain74cdad32015-09-02 12:04:13 +0200414 INIT_WORK(&intel->busy_work, intel_busy_work);
415
416 intel->hu = hu;
Loic Poulainca93cee2015-07-01 12:20:26 +0200417
418 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200419
420 if (!intel_set_power(hu, true))
421 set_bit(STATE_BOOTING, &intel->flags);
422
Loic Poulainca93cee2015-07-01 12:20:26 +0200423 return 0;
424}
425
426static int intel_close(struct hci_uart *hu)
427{
428 struct intel_data *intel = hu->priv;
429
430 BT_DBG("hu %p", hu);
431
Loic Poulain74cdad32015-09-02 12:04:13 +0200432 cancel_work_sync(&intel->busy_work);
433
Loic Poulain1ab1f232015-08-27 07:21:51 +0200434 intel_set_power(hu, false);
435
Loic Poulainca93cee2015-07-01 12:20:26 +0200436 skb_queue_purge(&intel->txq);
437 kfree_skb(intel->rx_skb);
438 kfree(intel);
439
440 hu->priv = NULL;
441 return 0;
442}
443
444static int intel_flush(struct hci_uart *hu)
445{
446 struct intel_data *intel = hu->priv;
447
448 BT_DBG("hu %p", hu);
449
450 skb_queue_purge(&intel->txq);
451
452 return 0;
453}
454
455static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
456{
457 struct sk_buff *skb;
458 struct hci_event_hdr *hdr;
459 struct hci_ev_cmd_complete *evt;
460
461 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
462 if (!skb)
463 return -ENOMEM;
464
Johannes Berg4df864c2017-06-16 14:29:21 +0200465 hdr = skb_put(skb, sizeof(*hdr));
Loic Poulainca93cee2015-07-01 12:20:26 +0200466 hdr->evt = HCI_EV_CMD_COMPLETE;
467 hdr->plen = sizeof(*evt) + 1;
468
Johannes Berg4df864c2017-06-16 14:29:21 +0200469 evt = skb_put(skb, sizeof(*evt));
Loic Poulainca93cee2015-07-01 12:20:26 +0200470 evt->ncmd = 0x01;
471 evt->opcode = cpu_to_le16(opcode);
472
Johannes Berg634fef62017-06-16 14:29:24 +0200473 skb_put_u8(skb, 0x00);
Loic Poulainca93cee2015-07-01 12:20:26 +0200474
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100475 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Loic Poulainca93cee2015-07-01 12:20:26 +0200476
477 return hci_recv_frame(hdev, skb);
478}
479
Loic Poulainff289552015-08-25 17:55:44 +0200480static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
481{
482 struct intel_data *intel = hu->priv;
483 struct hci_dev *hdev = hu->hdev;
484 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
485 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200486 int err;
487
488 /* This can be the first command sent to the chip, check
489 * that the controller is ready.
490 */
491 err = intel_wait_booting(hu);
492
493 clear_bit(STATE_BOOTING, &intel->flags);
494
495 /* In case of timeout, try to continue anyway */
Anton Protopopov2be11492016-02-10 12:22:54 -0500496 if (err && err != -ETIMEDOUT)
Loic Poulain1ab1f232015-08-27 07:21:51 +0200497 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200498
Loic Poulainf44e78a2015-08-31 18:34:30 +0200499 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200500
501 speed_cmd[3] = intel_convert_speed(speed);
502 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200503 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200504 return -EINVAL;
505 }
506
507 /* Device will not accept speed change if Intel version has not been
508 * previously requested.
509 */
Loic Poulaina0c38242015-11-27 19:55:59 +0100510 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
Loic Poulainff289552015-08-25 17:55:44 +0200511 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200512 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
513 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200514 return PTR_ERR(skb);
515 }
516 kfree_skb(skb);
517
518 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
519 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200520 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200521 return -ENOMEM;
522 }
523
Johannes Berg59ae1d12017-06-16 14:29:20 +0200524 skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100525 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
Loic Poulainff289552015-08-25 17:55:44 +0200526
527 hci_uart_set_flow_control(hu, true);
528
529 skb_queue_tail(&intel->txq, skb);
530 hci_uart_tx_wakeup(hu);
531
532 /* wait 100ms to change baudrate on controller side */
533 msleep(100);
534
535 hci_uart_set_baudrate(hu, speed);
536 hci_uart_set_flow_control(hu, false);
537
538 return 0;
539}
540
Loic Poulainca93cee2015-07-01 12:20:26 +0200541static int intel_setup(struct hci_uart *hu)
542{
543 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
544 0x00, 0x08, 0x04, 0x00 };
545 struct intel_data *intel = hu->priv;
546 struct hci_dev *hdev = hu->hdev;
547 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +0100548 struct intel_version ver;
Loic Poulainca93cee2015-07-01 12:20:26 +0200549 struct intel_boot_params *params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200550 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200551 const struct firmware *fw;
552 const u8 *fw_ptr;
553 char fwname[64];
554 u32 frag_len;
555 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
Loic Poulainca93cee2015-07-01 12:20:26 +0200566 calltime = ktime_get();
567
Loic Poulainff289552015-08-25 17:55:44 +0200568 if (hu->init_speed)
569 init_speed = hu->init_speed;
570 else
571 init_speed = hu->proto->init_speed;
572
573 if (hu->oper_speed)
574 oper_speed = hu->oper_speed;
575 else
576 oper_speed = hu->proto->oper_speed;
577
578 if (oper_speed && init_speed && oper_speed != init_speed)
579 speed_change = 1;
580
Loic Poulain1ab1f232015-08-27 07:21:51 +0200581 /* Check that the controller is ready */
582 err = intel_wait_booting(hu);
583
584 clear_bit(STATE_BOOTING, &intel->flags);
585
586 /* In case of timeout, try to continue anyway */
Anton Protopopov2be11492016-02-10 12:22:54 -0500587 if (err && err != -ETIMEDOUT)
Loic Poulain1ab1f232015-08-27 07:21:51 +0200588 return err;
589
Loic Poulainca93cee2015-07-01 12:20:26 +0200590 set_bit(STATE_BOOTLOADER, &intel->flags);
591
592 /* Read the Intel version information to determine if the device
593 * is in bootloader mode or if it already has operational firmware
594 * loaded.
595 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100596 err = btintel_read_version(hdev, &ver);
597 if (err)
Loic Poulainca93cee2015-07-01 12:20:26 +0200598 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200599
600 /* The hardware platform number has a fixed value of 0x37 and
601 * for now only accept this single value.
602 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100603 if (ver.hw_platform != 0x37) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200604 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100605 ver.hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200606 return -EINVAL;
607 }
608
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -0800609 /* Check for supported iBT hardware variants of this firmware
610 * loading method.
611 *
612 * This check has been put in place to ensure correct forward
613 * compatibility options when newer hardware variants come along.
614 */
615 switch (ver.hw_variant) {
616 case 0x0b: /* LnP */
617 case 0x0c: /* WsP */
Tedd Ho-Jeong An6c7bb7e2017-03-06 15:38:35 -0800618 case 0x12: /* ThP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -0800619 break;
620 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +0200621 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100622 ver.hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200623 return -EINVAL;
624 }
625
Loic Poulain6c483de2015-12-06 16:18:34 +0100626 btintel_version_info(hdev, &ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200627
628 /* The firmware variant determines if the device is in bootloader
629 * mode or is running operational firmware. The value 0x06 identifies
630 * the bootloader and the value 0x23 identifies the operational
631 * firmware.
632 *
633 * When the operational firmware is already present, then only
634 * the check for valid Bluetooth device address is needed. This
635 * determines if the device will be added as configured or
636 * unconfigured controller.
637 *
638 * It is not possible to use the Secure Boot Parameters in this
639 * case since that command is only available in bootloader mode.
640 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100641 if (ver.fw_variant == 0x23) {
Loic Poulainca93cee2015-07-01 12:20:26 +0200642 clear_bit(STATE_BOOTLOADER, &intel->flags);
643 btintel_check_bdaddr(hdev);
644 return 0;
645 }
646
647 /* If the device is not in bootloader mode, then the only possible
648 * choice is to return an error and abort the device initialization.
649 */
Loic Poulain6c483de2015-12-06 16:18:34 +0100650 if (ver.fw_variant != 0x06) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200651 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +0100652 ver.fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200653 return -ENODEV;
654 }
655
Loic Poulainca93cee2015-07-01 12:20:26 +0200656 /* Read the secure boot parameters to identify the operating
657 * details of the bootloader.
658 */
Loic Poulaina0c38242015-11-27 19:55:59 +0100659 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_CMD_TIMEOUT);
Loic Poulainca93cee2015-07-01 12:20:26 +0200660 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200661 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
662 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200663 return PTR_ERR(skb);
664 }
665
666 if (skb->len != sizeof(*params)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200667 bt_dev_err(hdev, "Intel boot parameters size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200668 kfree_skb(skb);
669 return -EILSEQ;
670 }
671
672 params = (struct intel_boot_params *)skb->data;
673 if (params->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200674 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
675 params->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200676 err = -bt_to_errno(params->status);
677 kfree_skb(skb);
678 return err;
679 }
680
Loic Poulainf44e78a2015-08-31 18:34:30 +0200681 bt_dev_info(hdev, "Device revision is %u",
682 le16_to_cpu(params->dev_revid));
Loic Poulainca93cee2015-07-01 12:20:26 +0200683
Loic Poulainf44e78a2015-08-31 18:34:30 +0200684 bt_dev_info(hdev, "Secure boot is %s",
685 params->secure_boot ? "enabled" : "disabled");
Loic Poulainca93cee2015-07-01 12:20:26 +0200686
Loic Poulainf44e78a2015-08-31 18:34:30 +0200687 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
Loic Poulainca93cee2015-07-01 12:20:26 +0200688 params->min_fw_build_nn, params->min_fw_build_cw,
689 2000 + params->min_fw_build_yy);
690
691 /* It is required that every single firmware fragment is acknowledged
692 * with a command complete event. If the boot parameters indicate
693 * that this bootloader does not send them, then abort the setup.
694 */
695 if (params->limited_cce != 0x00) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200696 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
697 params->limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200698 kfree_skb(skb);
699 return -EINVAL;
700 }
701
702 /* If the OTP has no valid Bluetooth device address, then there will
703 * also be no valid address for the operational firmware.
704 */
705 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200706 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200707 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
708 }
709
710 /* With this Intel bootloader only the hardware variant and device
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800711 * revision information are used to select the right firmware for SfP
712 * and WsP.
Loic Poulainca93cee2015-07-01 12:20:26 +0200713 *
Tedd Ho-Jeong Anb7da6a62017-03-06 15:38:32 -0800714 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
715 *
716 * Currently the supported hardware variants are:
717 * 11 (0x0b) for iBT 3.0 (LnP/SfP)
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800718 * 12 (0x0c) for iBT 3.5 (WsP)
719 *
720 * For ThP/JfP and for future SKU's, the FW name varies based on HW
721 * variant, HW revision and FW revision, as these are dependent on CNVi
722 * and RF Combination.
723 *
724 * 18 (0x12) for iBT3.5 (ThP/JfP)
725 *
726 * The firmware file name for these will be
727 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
728 *
Loic Poulainca93cee2015-07-01 12:20:26 +0200729 */
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800730 switch (ver.hw_variant) {
731 case 0x0b: /* SfP */
732 case 0x0c: /* WsP */
733 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
734 le16_to_cpu(ver.hw_variant),
735 le16_to_cpu(params->dev_revid));
736 break;
737 case 0x12: /* ThP */
738 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
739 le16_to_cpu(ver.hw_variant),
740 le16_to_cpu(ver.hw_revision),
741 le16_to_cpu(ver.fw_revision));
742 break;
743 default:
744 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
745 ver.hw_variant);
746 return -EINVAL;
747 }
Loic Poulainca93cee2015-07-01 12:20:26 +0200748
749 err = request_firmware(&fw, fwname, &hdev->dev);
750 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200751 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
752 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200753 kfree_skb(skb);
754 return err;
755 }
756
Loic Poulainf44e78a2015-08-31 18:34:30 +0200757 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200758
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200759 /* Save the DDC file name for later */
Tedd Ho-Jeong An965651c12018-01-24 09:19:17 -0800760 switch (ver.hw_variant) {
761 case 0x0b: /* SfP */
762 case 0x0c: /* WsP */
763 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
764 le16_to_cpu(ver.hw_variant),
765 le16_to_cpu(params->dev_revid));
766 break;
767 case 0x12: /* ThP */
768 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
769 le16_to_cpu(ver.hw_variant),
770 le16_to_cpu(ver.hw_revision),
771 le16_to_cpu(ver.fw_revision));
772 break;
773 default:
774 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
775 ver.hw_variant);
776 return -EINVAL;
777 }
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200778
Loic Poulainca93cee2015-07-01 12:20:26 +0200779 kfree_skb(skb);
780
781 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200782 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
783 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200784 err = -EBADF;
785 goto done;
786 }
787
788 set_bit(STATE_DOWNLOADING, &intel->flags);
789
790 /* Start the firmware download transaction with the Init fragment
791 * represented by the 128 bytes of CSS header.
792 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200793 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200794 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200795 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200796 goto done;
797 }
798
799 /* Send the 256 bytes of public key information from the firmware
800 * as the PKey fragment.
801 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200802 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200803 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200804 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
805 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200806 goto done;
807 }
808
809 /* Send the 256 bytes of signature information from the firmware
810 * as the Sign fragment.
811 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200812 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200813 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200814 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
815 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200816 goto done;
817 }
818
819 fw_ptr = fw->data + 644;
820 frag_len = 0;
821
822 while (fw_ptr - fw->data < fw->size) {
823 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
824
825 frag_len += sizeof(*cmd) + cmd->plen;
826
Loic Poulainf44e78a2015-08-31 18:34:30 +0200827 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
828 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200829
830 /* The parameter length of the secure send command requires
831 * a 4 byte alignment. It happens so that the firmware file
832 * contains proper Intel_NOP commands to align the fragments
833 * as needed.
834 *
835 * Send set of commands with 4 byte alignment from the
836 * firmware data buffer as a single Data fragement.
837 */
838 if (frag_len % 4)
839 continue;
840
841 /* Send each command from the firmware data buffer as
842 * a single Data fragment.
843 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200844 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200845 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200846 bt_dev_err(hdev, "Failed to send firmware data (%d)",
847 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200848 goto done;
849 }
850
851 fw_ptr += frag_len;
852 frag_len = 0;
853 }
854
855 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
856
Loic Poulainf44e78a2015-08-31 18:34:30 +0200857 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200858
859 /* Before switching the device into operational mode and with that
860 * booting the loaded firmware, wait for the bootloader notification
861 * that all fragments have been successfully received.
862 *
863 * When the event processing receives the notification, then the
864 * STATE_DOWNLOADING flag will be cleared.
865 *
866 * The firmware loading should not take longer than 5 seconds
867 * and thus just timeout if that happens and fail the setup
868 * of this device.
869 */
870 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
871 TASK_INTERRUPTIBLE,
872 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -0700873 if (err == -EINTR) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200874 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200875 err = -EINTR;
876 goto done;
877 }
878
879 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200880 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200881 err = -ETIMEDOUT;
882 goto done;
883 }
884
885 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200886 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200887 err = -ENOEXEC;
888 goto done;
889 }
890
891 rettime = ktime_get();
892 delta = ktime_sub(rettime, calltime);
893 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
894
Loic Poulainf44e78a2015-08-31 18:34:30 +0200895 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200896
897done:
898 release_firmware(fw);
899
900 if (err < 0)
901 return err;
902
Loic Poulainff289552015-08-25 17:55:44 +0200903 /* We need to restore the default speed before Intel reset */
904 if (speed_change) {
905 err = intel_set_baudrate(hu, init_speed);
906 if (err)
907 return err;
908 }
909
Loic Poulainca93cee2015-07-01 12:20:26 +0200910 calltime = ktime_get();
911
912 set_bit(STATE_BOOTING, &intel->flags);
913
914 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
Loic Poulaina0c38242015-11-27 19:55:59 +0100915 HCI_CMD_TIMEOUT);
Loic Poulainca93cee2015-07-01 12:20:26 +0200916 if (IS_ERR(skb))
917 return PTR_ERR(skb);
918
919 kfree_skb(skb);
920
921 /* The bootloader will not indicate when the device is ready. This
922 * is done by the operational firmware sending bootup notification.
923 *
924 * Booting into operational firmware should not take longer than
925 * 1 second. However if that happens, then just fail the setup
926 * since something went wrong.
927 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200928 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200929
Loic Poulain1ab1f232015-08-27 07:21:51 +0200930 err = intel_wait_booting(hu);
931 if (err)
932 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200933
Loic Poulain1ab1f232015-08-27 07:21:51 +0200934 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200935
936 rettime = ktime_get();
937 delta = ktime_sub(rettime, calltime);
938 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
939
Loic Poulainf44e78a2015-08-31 18:34:30 +0200940 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200941
Loic Poulain31eff262016-07-11 21:55:36 +0200942 /* Enable LPM if matching pdev with wakeup enabled, set TX active
943 * until further LPM TX notification.
944 */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200945 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200946 list_for_each(p, &intel_device_list) {
947 struct intel_device *dev = list_entry(p, struct intel_device,
948 list);
Johan Hovolddcb9cfa2017-03-29 18:15:28 +0200949 if (!hu->tty->dev)
950 break;
Loic Poulainb98469f2015-08-29 13:38:19 +0200951 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
Loic Poulain31eff262016-07-11 21:55:36 +0200952 if (device_may_wakeup(&dev->pdev->dev)) {
953 set_bit(STATE_LPM_ENABLED, &intel->flags);
954 set_bit(STATE_TX_ACTIVE, &intel->flags);
955 }
Loic Poulainb98469f2015-08-29 13:38:19 +0200956 break;
957 }
958 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200959 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200960
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200961 /* Ignore errors, device can work without DDC parameters */
962 btintel_load_ddc_config(hdev, fwname);
963
Loic Poulainff289552015-08-25 17:55:44 +0200964 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
965 if (IS_ERR(skb))
966 return PTR_ERR(skb);
967 kfree_skb(skb);
968
969 if (speed_change) {
970 err = intel_set_baudrate(hu, oper_speed);
971 if (err)
972 return err;
973 }
974
Loic Poulainf44e78a2015-08-31 18:34:30 +0200975 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200976
Loic Poulainca93cee2015-07-01 12:20:26 +0200977 clear_bit(STATE_BOOTLOADER, &intel->flags);
978
979 return 0;
980}
981
982static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
983{
984 struct hci_uart *hu = hci_get_drvdata(hdev);
985 struct intel_data *intel = hu->priv;
986 struct hci_event_hdr *hdr;
987
Loic Poulain1ab1f232015-08-27 07:21:51 +0200988 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
989 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200990 goto recv;
991
992 hdr = (void *)skb->data;
993
994 /* When the firmware loading completes the device sends
995 * out a vendor specific event indicating the result of
996 * the firmware loading.
997 */
998 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
999 skb->data[2] == 0x06) {
1000 if (skb->data[3] != 0x00)
1001 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
1002
1003 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
1004 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
1005 smp_mb__after_atomic();
1006 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
1007 }
1008
1009 /* When switching to the operational firmware the device
1010 * sends a vendor specific event indicating that the bootup
1011 * completed.
1012 */
1013 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1014 skb->data[2] == 0x02) {
1015 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
1016 smp_mb__after_atomic();
1017 wake_up_bit(&intel->flags, STATE_BOOTING);
1018 }
1019 }
1020recv:
1021 return hci_recv_frame(hdev, skb);
1022}
1023
Loic Poulainb98469f2015-08-29 13:38:19 +02001024static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
1025{
1026 struct hci_uart *hu = hci_get_drvdata(hdev);
1027 struct intel_data *intel = hu->priv;
1028
Loic Poulainf44e78a2015-08-31 18:34:30 +02001029 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +02001030
Loic Poulain74cdad32015-09-02 12:04:13 +02001031 if (value) {
Loic Poulainb98469f2015-08-29 13:38:19 +02001032 set_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001033 schedule_work(&intel->busy_work);
1034 } else {
Loic Poulainb98469f2015-08-29 13:38:19 +02001035 clear_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001036 }
Loic Poulainb98469f2015-08-29 13:38:19 +02001037}
1038
1039static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1040{
1041 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +02001042 struct hci_uart *hu = hci_get_drvdata(hdev);
1043 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +02001044
1045 switch (lpm->opcode) {
1046 case LPM_OP_TX_NOTIFY:
Loic Poulain1b197572015-09-02 12:04:14 +02001047 if (lpm->dlen < 1) {
1048 bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1049 break;
1050 }
1051 intel_recv_lpm_notify(hdev, lpm->data[0]);
Loic Poulainb98469f2015-08-29 13:38:19 +02001052 break;
Loic Poulain89436542015-09-02 12:04:11 +02001053 case LPM_OP_SUSPEND_ACK:
1054 set_bit(STATE_SUSPENDED, &intel->flags);
1055 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1056 smp_mb__after_atomic();
1057 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1058 }
1059 break;
1060 case LPM_OP_RESUME_ACK:
1061 clear_bit(STATE_SUSPENDED, &intel->flags);
1062 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1063 smp_mb__after_atomic();
1064 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1065 }
1066 break;
Loic Poulainb98469f2015-08-29 13:38:19 +02001067 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +02001068 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +02001069 break;
1070 }
1071
1072 kfree_skb(skb);
1073
1074 return 0;
1075}
1076
1077#define INTEL_RECV_LPM \
1078 .type = HCI_LPM_PKT, \
1079 .hlen = HCI_LPM_HDR_SIZE, \
1080 .loff = 1, \
1081 .lsize = 1, \
1082 .maxlen = HCI_LPM_MAX_SIZE
1083
Loic Poulainca93cee2015-07-01 12:20:26 +02001084static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +02001085 { H4_RECV_ACL, .recv = hci_recv_frame },
1086 { H4_RECV_SCO, .recv = hci_recv_frame },
1087 { H4_RECV_EVENT, .recv = intel_recv_event },
1088 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +02001089};
1090
1091static int intel_recv(struct hci_uart *hu, const void *data, int count)
1092{
1093 struct intel_data *intel = hu->priv;
1094
1095 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1096 return -EUNATCH;
1097
1098 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1099 intel_recv_pkts,
1100 ARRAY_SIZE(intel_recv_pkts));
1101 if (IS_ERR(intel->rx_skb)) {
1102 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001103 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001104 intel->rx_skb = NULL;
1105 return err;
1106 }
1107
1108 return count;
1109}
1110
1111static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1112{
1113 struct intel_data *intel = hu->priv;
Loic Poulain74cdad32015-09-02 12:04:13 +02001114 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +02001115
1116 BT_DBG("hu %p skb %p", hu, skb);
1117
Johan Hovolddcb9cfa2017-03-29 18:15:28 +02001118 if (!hu->tty->dev)
1119 goto out_enqueue;
1120
Loic Poulain74cdad32015-09-02 12:04:13 +02001121 /* Be sure our controller is resumed and potential LPM transaction
1122 * completed before enqueuing any packet.
1123 */
1124 mutex_lock(&intel_device_list_lock);
1125 list_for_each(p, &intel_device_list) {
1126 struct intel_device *idev = list_entry(p, struct intel_device,
1127 list);
1128
1129 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1130 pm_runtime_get_sync(&idev->pdev->dev);
1131 pm_runtime_mark_last_busy(&idev->pdev->dev);
1132 pm_runtime_put_autosuspend(&idev->pdev->dev);
1133 break;
1134 }
1135 }
1136 mutex_unlock(&intel_device_list_lock);
Johan Hovolddcb9cfa2017-03-29 18:15:28 +02001137out_enqueue:
Loic Poulainca93cee2015-07-01 12:20:26 +02001138 skb_queue_tail(&intel->txq, skb);
1139
1140 return 0;
1141}
1142
1143static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1144{
1145 struct intel_data *intel = hu->priv;
1146 struct sk_buff *skb;
1147
1148 skb = skb_dequeue(&intel->txq);
1149 if (!skb)
1150 return skb;
1151
1152 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001153 (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)) {
Loic Poulainca93cee2015-07-01 12:20:26 +02001154 struct hci_command_hdr *cmd = (void *)skb->data;
1155 __u16 opcode = le16_to_cpu(cmd->opcode);
1156
1157 /* When the 0xfc01 command is issued to boot into
1158 * the operational firmware, it will actually not
1159 * send a command complete event. To keep the flow
1160 * control working inject that event here.
1161 */
1162 if (opcode == 0xfc01)
1163 inject_cmd_complete(hu->hdev, opcode);
1164 }
1165
1166 /* Prepend skb with frame type */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001167 memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
Loic Poulainca93cee2015-07-01 12:20:26 +02001168
1169 return skb;
1170}
1171
1172static const struct hci_uart_proto intel_proto = {
1173 .id = HCI_UART_INTEL,
1174 .name = "Intel",
Marcel Holtmannaee61f72015-10-20 21:30:45 +02001175 .manufacturer = 2,
Loic Poulainca93cee2015-07-01 12:20:26 +02001176 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001177 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001178 .open = intel_open,
1179 .close = intel_close,
1180 .flush = intel_flush,
1181 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001182 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001183 .recv = intel_recv,
1184 .enqueue = intel_enqueue,
1185 .dequeue = intel_dequeue,
1186};
1187
Loic Poulain1ab1f232015-08-27 07:21:51 +02001188#ifdef CONFIG_ACPI
1189static const struct acpi_device_id intel_acpi_match[] = {
1190 { "INT33E1", 0 },
1191 { },
1192};
1193MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001194#endif
1195
Loic Poulain74cdad32015-09-02 12:04:13 +02001196#ifdef CONFIG_PM
Loic Poulainf7552472015-09-09 21:04:15 +02001197static int intel_suspend_device(struct device *dev)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001198{
1199 struct intel_device *idev = dev_get_drvdata(dev);
1200
Loic Poulainaa6802d2015-09-02 12:04:12 +02001201 mutex_lock(&idev->hu_lock);
1202 if (idev->hu)
1203 intel_lpm_suspend(idev->hu);
1204 mutex_unlock(&idev->hu_lock);
1205
1206 return 0;
1207}
1208
Loic Poulainf7552472015-09-09 21:04:15 +02001209static int intel_resume_device(struct device *dev)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001210{
1211 struct intel_device *idev = dev_get_drvdata(dev);
1212
Loic Poulainaa6802d2015-09-02 12:04:12 +02001213 mutex_lock(&idev->hu_lock);
1214 if (idev->hu)
1215 intel_lpm_resume(idev->hu);
1216 mutex_unlock(&idev->hu_lock);
1217
1218 return 0;
1219}
1220#endif
1221
Loic Poulainf7552472015-09-09 21:04:15 +02001222#ifdef CONFIG_PM_SLEEP
1223static int intel_suspend(struct device *dev)
1224{
1225 struct intel_device *idev = dev_get_drvdata(dev);
1226
1227 if (device_may_wakeup(dev))
1228 enable_irq_wake(idev->irq);
1229
1230 return intel_suspend_device(dev);
1231}
1232
1233static int intel_resume(struct device *dev)
1234{
1235 struct intel_device *idev = dev_get_drvdata(dev);
1236
1237 if (device_may_wakeup(dev))
1238 disable_irq_wake(idev->irq);
1239
1240 return intel_resume_device(dev);
1241}
1242#endif
1243
Loic Poulainaa6802d2015-09-02 12:04:12 +02001244static const struct dev_pm_ops intel_pm_ops = {
1245 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Loic Poulainf7552472015-09-09 21:04:15 +02001246 SET_RUNTIME_PM_OPS(intel_suspend_device, intel_resume_device, NULL)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001247};
1248
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001249static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
1250static const struct acpi_gpio_params host_wake_gpios = { 1, 0, false };
1251
1252static const struct acpi_gpio_mapping acpi_hci_intel_gpios[] = {
1253 { "reset-gpios", &reset_gpios, 1 },
1254 { "host-wake-gpios", &host_wake_gpios, 1 },
1255 { },
1256};
1257
Loic Poulain1ab1f232015-08-27 07:21:51 +02001258static int intel_probe(struct platform_device *pdev)
1259{
1260 struct intel_device *idev;
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001261 int ret;
Loic Poulain1ab1f232015-08-27 07:21:51 +02001262
1263 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1264 if (!idev)
1265 return -ENOMEM;
1266
Loic Poulainaa6802d2015-09-02 12:04:12 +02001267 mutex_init(&idev->hu_lock);
1268
Loic Poulain1ab1f232015-08-27 07:21:51 +02001269 idev->pdev = pdev;
1270
Andy Shevchenko4a59d432017-06-06 17:47:18 +03001271 ret = devm_acpi_dev_add_driver_gpios(&pdev->dev, acpi_hci_intel_gpios);
1272 if (ret)
1273 dev_dbg(&pdev->dev, "Unable to add GPIO mapping table\n");
1274
Loic Poulain32b9ccb2016-04-28 18:48:25 +02001275 idev->reset = devm_gpiod_get(&pdev->dev, "reset", GPIOD_OUT_LOW);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001276 if (IS_ERR(idev->reset)) {
1277 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1278 return PTR_ERR(idev->reset);
1279 }
1280
Loic Poulain765ea3a2015-08-29 13:38:18 +02001281 idev->irq = platform_get_irq(pdev, 0);
1282 if (idev->irq < 0) {
1283 struct gpio_desc *host_wake;
1284
1285 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1286
Loic Poulain32b9ccb2016-04-28 18:48:25 +02001287 host_wake = devm_gpiod_get(&pdev->dev, "host-wake", GPIOD_IN);
Loic Poulain765ea3a2015-08-29 13:38:18 +02001288 if (IS_ERR(host_wake)) {
1289 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1290 goto no_irq;
1291 }
1292
1293 idev->irq = gpiod_to_irq(host_wake);
1294 if (idev->irq < 0) {
1295 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1296 goto no_irq;
1297 }
1298 }
1299
1300 /* Only enable wake-up/irq when controller is powered */
1301 device_set_wakeup_capable(&pdev->dev, true);
1302 device_wakeup_disable(&pdev->dev);
1303
1304no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001305 platform_set_drvdata(pdev, idev);
1306
1307 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001308 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001309 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001310 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001311
Loic Poulain765ea3a2015-08-29 13:38:18 +02001312 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1313 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001314
1315 return 0;
1316}
1317
1318static int intel_remove(struct platform_device *pdev)
1319{
1320 struct intel_device *idev = platform_get_drvdata(pdev);
1321
Loic Poulain765ea3a2015-08-29 13:38:18 +02001322 device_wakeup_disable(&pdev->dev);
1323
Loic Poulain67c8bde2015-08-31 18:34:31 +02001324 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001325 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001326 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001327
1328 dev_info(&pdev->dev, "unregistered.\n");
1329
1330 return 0;
1331}
1332
1333static struct platform_driver intel_driver = {
1334 .probe = intel_probe,
1335 .remove = intel_remove,
1336 .driver = {
1337 .name = "hci_intel",
1338 .acpi_match_table = ACPI_PTR(intel_acpi_match),
Loic Poulainaa6802d2015-09-02 12:04:12 +02001339 .pm = &intel_pm_ops,
Loic Poulain1ab1f232015-08-27 07:21:51 +02001340 },
1341};
1342
Loic Poulainca93cee2015-07-01 12:20:26 +02001343int __init intel_init(void)
1344{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001345 platform_driver_register(&intel_driver);
1346
Loic Poulainca93cee2015-07-01 12:20:26 +02001347 return hci_uart_register_proto(&intel_proto);
1348}
1349
1350int __exit intel_deinit(void)
1351{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001352 platform_driver_unregister(&intel_driver);
1353
Loic Poulainca93cee2015-07-01 12:20:26 +02001354 return hci_uart_unregister_proto(&intel_proto);
1355}