blob: b17386b5792c45ffa592e826bbd684216516162f [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
131 if (err == 1) {
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
154 if (err == 1) {
155 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
188 memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend));
189 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
190
191 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
192
193 skb_queue_tail(&intel->txq, skb);
194 hci_uart_tx_wakeup(hu);
195
196 intel_wait_lpm_transaction(hu);
197 /* Even in case of failure, continue and test the suspended flag */
198
199 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
200
201 if (!test_bit(STATE_SUSPENDED, &intel->flags)) {
202 bt_dev_err(hu->hdev, "Device suspend error");
203 return -EINVAL;
204 }
205
206 bt_dev_dbg(hu->hdev, "Suspended");
207
208 hci_uart_set_flow_control(hu, true);
209
210 return 0;
211}
212
213static int intel_lpm_resume(struct hci_uart *hu)
214{
215 struct intel_data *intel = hu->priv;
216 struct sk_buff *skb;
217
218 if (!test_bit(STATE_LPM_ENABLED, &intel->flags) ||
219 !test_bit(STATE_SUSPENDED, &intel->flags))
220 return 0;
221
222 bt_dev_dbg(hu->hdev, "Resuming");
223
224 hci_uart_set_flow_control(hu, false);
225
226 skb = bt_skb_alloc(0, GFP_KERNEL);
227 if (!skb) {
228 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
229 return -ENOMEM;
230 }
231
232 bt_cb(skb)->pkt_type = HCI_LPM_WAKE_PKT;
233
234 set_bit(STATE_LPM_TRANSACTION, &intel->flags);
235
236 skb_queue_tail(&intel->txq, skb);
237 hci_uart_tx_wakeup(hu);
238
239 intel_wait_lpm_transaction(hu);
240 /* Even in case of failure, continue and test the suspended flag */
241
242 clear_bit(STATE_LPM_TRANSACTION, &intel->flags);
243
244 if (test_bit(STATE_SUSPENDED, &intel->flags)) {
245 bt_dev_err(hu->hdev, "Device resume error");
246 return -EINVAL;
247 }
248
249 bt_dev_dbg(hu->hdev, "Resumed");
250
251 return 0;
252}
Loic Poulaina9cb0fe2015-09-04 17:39:25 +0200253#endif /* CONFIG_PM */
Loic Poulain89436542015-09-02 12:04:11 +0200254
255static int intel_lpm_host_wake(struct hci_uart *hu)
256{
257 static const u8 lpm_resume_ack[] = { LPM_OP_RESUME_ACK, 0x00 };
258 struct intel_data *intel = hu->priv;
259 struct sk_buff *skb;
260
261 hci_uart_set_flow_control(hu, false);
262
263 clear_bit(STATE_SUSPENDED, &intel->flags);
264
265 skb = bt_skb_alloc(sizeof(lpm_resume_ack), GFP_KERNEL);
266 if (!skb) {
267 bt_dev_err(hu->hdev, "Failed to alloc memory for LPM packet");
268 return -ENOMEM;
269 }
270
271 memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack,
272 sizeof(lpm_resume_ack));
273 bt_cb(skb)->pkt_type = HCI_LPM_PKT;
274
275 skb_queue_tail(&intel->txq, skb);
276 hci_uart_tx_wakeup(hu);
277
278 bt_dev_dbg(hu->hdev, "Resumed by controller");
279
280 return 0;
281}
282
Loic Poulain765ea3a2015-08-29 13:38:18 +0200283static irqreturn_t intel_irq(int irq, void *dev_id)
284{
285 struct intel_device *idev = dev_id;
286
287 dev_info(&idev->pdev->dev, "hci_intel irq\n");
288
Loic Poulainaa6802d2015-09-02 12:04:12 +0200289 mutex_lock(&idev->hu_lock);
290 if (idev->hu)
291 intel_lpm_host_wake(idev->hu);
292 mutex_unlock(&idev->hu_lock);
293
Loic Poulain74cdad32015-09-02 12:04:13 +0200294 /* Host/Controller are now LPM resumed, trigger a new delayed suspend */
295 pm_runtime_get(&idev->pdev->dev);
296 pm_runtime_mark_last_busy(&idev->pdev->dev);
297 pm_runtime_put_autosuspend(&idev->pdev->dev);
298
Loic Poulain765ea3a2015-08-29 13:38:18 +0200299 return IRQ_HANDLED;
300}
301
Loic Poulain1ab1f232015-08-27 07:21:51 +0200302static int intel_set_power(struct hci_uart *hu, bool powered)
303{
304 struct list_head *p;
305 int err = -ENODEV;
306
Loic Poulain67c8bde2015-08-31 18:34:31 +0200307 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200308
309 list_for_each(p, &intel_device_list) {
310 struct intel_device *idev = list_entry(p, struct intel_device,
311 list);
312
313 /* tty device and pdev device should share the same parent
314 * which is the UART port.
315 */
316 if (hu->tty->dev->parent != idev->pdev->dev.parent)
317 continue;
318
319 if (!idev->reset) {
320 err = -ENOTSUPP;
321 break;
322 }
323
324 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
325 hu, dev_name(&idev->pdev->dev), powered);
326
327 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200328
Loic Poulainaa6802d2015-09-02 12:04:12 +0200329 /* Provide to idev a hu reference which is used to run LPM
330 * transactions (lpm suspend/resume) from PM callbacks.
331 * hu needs to be protected against concurrent removing during
332 * these PM ops.
333 */
334 mutex_lock(&idev->hu_lock);
335 idev->hu = powered ? hu : NULL;
336 mutex_unlock(&idev->hu_lock);
337
Loic Poulain765ea3a2015-08-29 13:38:18 +0200338 if (idev->irq < 0)
339 break;
340
341 if (powered && device_can_wakeup(&idev->pdev->dev)) {
342 err = devm_request_threaded_irq(&idev->pdev->dev,
343 idev->irq, NULL,
344 intel_irq,
345 IRQF_ONESHOT,
346 "bt-host-wake", idev);
347 if (err) {
348 BT_ERR("hu %p, unable to allocate irq-%d",
349 hu, idev->irq);
350 break;
351 }
352
353 device_wakeup_enable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200354
355 pm_runtime_set_active(&idev->pdev->dev);
356 pm_runtime_use_autosuspend(&idev->pdev->dev);
357 pm_runtime_set_autosuspend_delay(&idev->pdev->dev,
358 LPM_SUSPEND_DELAY_MS);
359 pm_runtime_enable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200360 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
361 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
362 device_wakeup_disable(&idev->pdev->dev);
Loic Poulain74cdad32015-09-02 12:04:13 +0200363
364 pm_runtime_disable(&idev->pdev->dev);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200365 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200366 }
367
Loic Poulain67c8bde2015-08-31 18:34:31 +0200368 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200369
370 return err;
371}
372
Loic Poulain74cdad32015-09-02 12:04:13 +0200373static void intel_busy_work(struct work_struct *work)
374{
375 struct list_head *p;
376 struct intel_data *intel = container_of(work, struct intel_data,
377 busy_work);
378
379 /* Link is busy, delay the suspend */
380 mutex_lock(&intel_device_list_lock);
381 list_for_each(p, &intel_device_list) {
382 struct intel_device *idev = list_entry(p, struct intel_device,
383 list);
384
385 if (intel->hu->tty->dev->parent == idev->pdev->dev.parent) {
386 pm_runtime_get(&idev->pdev->dev);
387 pm_runtime_mark_last_busy(&idev->pdev->dev);
388 pm_runtime_put_autosuspend(&idev->pdev->dev);
389 break;
390 }
391 }
392 mutex_unlock(&intel_device_list_lock);
393}
394
Loic Poulainca93cee2015-07-01 12:20:26 +0200395static int intel_open(struct hci_uart *hu)
396{
397 struct intel_data *intel;
398
399 BT_DBG("hu %p", hu);
400
401 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
402 if (!intel)
403 return -ENOMEM;
404
405 skb_queue_head_init(&intel->txq);
Loic Poulain74cdad32015-09-02 12:04:13 +0200406 INIT_WORK(&intel->busy_work, intel_busy_work);
407
408 intel->hu = hu;
Loic Poulainca93cee2015-07-01 12:20:26 +0200409
410 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200411
412 if (!intel_set_power(hu, true))
413 set_bit(STATE_BOOTING, &intel->flags);
414
Loic Poulainca93cee2015-07-01 12:20:26 +0200415 return 0;
416}
417
418static int intel_close(struct hci_uart *hu)
419{
420 struct intel_data *intel = hu->priv;
421
422 BT_DBG("hu %p", hu);
423
Loic Poulain74cdad32015-09-02 12:04:13 +0200424 cancel_work_sync(&intel->busy_work);
425
Loic Poulain1ab1f232015-08-27 07:21:51 +0200426 intel_set_power(hu, false);
427
Loic Poulainca93cee2015-07-01 12:20:26 +0200428 skb_queue_purge(&intel->txq);
429 kfree_skb(intel->rx_skb);
430 kfree(intel);
431
432 hu->priv = NULL;
433 return 0;
434}
435
436static int intel_flush(struct hci_uart *hu)
437{
438 struct intel_data *intel = hu->priv;
439
440 BT_DBG("hu %p", hu);
441
442 skb_queue_purge(&intel->txq);
443
444 return 0;
445}
446
447static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
448{
449 struct sk_buff *skb;
450 struct hci_event_hdr *hdr;
451 struct hci_ev_cmd_complete *evt;
452
453 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
454 if (!skb)
455 return -ENOMEM;
456
457 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
458 hdr->evt = HCI_EV_CMD_COMPLETE;
459 hdr->plen = sizeof(*evt) + 1;
460
461 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
462 evt->ncmd = 0x01;
463 evt->opcode = cpu_to_le16(opcode);
464
465 *skb_put(skb, 1) = 0x00;
466
467 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
468
469 return hci_recv_frame(hdev, skb);
470}
471
Loic Poulainff289552015-08-25 17:55:44 +0200472static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
473{
474 struct intel_data *intel = hu->priv;
475 struct hci_dev *hdev = hu->hdev;
476 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
477 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200478 int err;
479
480 /* This can be the first command sent to the chip, check
481 * that the controller is ready.
482 */
483 err = intel_wait_booting(hu);
484
485 clear_bit(STATE_BOOTING, &intel->flags);
486
487 /* In case of timeout, try to continue anyway */
488 if (err && err != ETIMEDOUT)
489 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200490
Loic Poulainf44e78a2015-08-31 18:34:30 +0200491 bt_dev_info(hdev, "Change controller speed to %d", speed);
Loic Poulainff289552015-08-25 17:55:44 +0200492
493 speed_cmd[3] = intel_convert_speed(speed);
494 if (speed_cmd[3] == 0xff) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200495 bt_dev_err(hdev, "Unsupported speed");
Loic Poulainff289552015-08-25 17:55:44 +0200496 return -EINVAL;
497 }
498
499 /* Device will not accept speed change if Intel version has not been
500 * previously requested.
501 */
502 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
503 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200504 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
505 PTR_ERR(skb));
Loic Poulainff289552015-08-25 17:55:44 +0200506 return PTR_ERR(skb);
507 }
508 kfree_skb(skb);
509
510 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
511 if (!skb) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200512 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
Loic Poulainff289552015-08-25 17:55:44 +0200513 return -ENOMEM;
514 }
515
516 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
517 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
518
519 hci_uart_set_flow_control(hu, true);
520
521 skb_queue_tail(&intel->txq, skb);
522 hci_uart_tx_wakeup(hu);
523
524 /* wait 100ms to change baudrate on controller side */
525 msleep(100);
526
527 hci_uart_set_baudrate(hu, speed);
528 hci_uart_set_flow_control(hu, false);
529
530 return 0;
531}
532
Loic Poulainca93cee2015-07-01 12:20:26 +0200533static int intel_setup(struct hci_uart *hu)
534{
535 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
536 0x00, 0x08, 0x04, 0x00 };
Loic Poulainb98469f2015-08-29 13:38:19 +0200537 static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
Loic Poulainca93cee2015-07-01 12:20:26 +0200538 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +0200539 struct intel_device *idev = NULL;
Loic Poulainca93cee2015-07-01 12:20:26 +0200540 struct hci_dev *hdev = hu->hdev;
541 struct sk_buff *skb;
542 struct intel_version *ver;
543 struct intel_boot_params *params;
Loic Poulainb98469f2015-08-29 13:38:19 +0200544 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +0200545 const struct firmware *fw;
546 const u8 *fw_ptr;
547 char fwname[64];
548 u32 frag_len;
549 ktime_t calltime, delta, rettime;
550 unsigned long long duration;
Loic Poulainff289552015-08-25 17:55:44 +0200551 unsigned int init_speed, oper_speed;
552 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200553 int err;
554
Loic Poulainf44e78a2015-08-31 18:34:30 +0200555 bt_dev_dbg(hdev, "start intel_setup");
Loic Poulainca93cee2015-07-01 12:20:26 +0200556
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200557 hu->hdev->set_bdaddr = btintel_set_bdaddr;
558
Loic Poulainca93cee2015-07-01 12:20:26 +0200559 calltime = ktime_get();
560
Loic Poulainff289552015-08-25 17:55:44 +0200561 if (hu->init_speed)
562 init_speed = hu->init_speed;
563 else
564 init_speed = hu->proto->init_speed;
565
566 if (hu->oper_speed)
567 oper_speed = hu->oper_speed;
568 else
569 oper_speed = hu->proto->oper_speed;
570
571 if (oper_speed && init_speed && oper_speed != init_speed)
572 speed_change = 1;
573
Loic Poulain1ab1f232015-08-27 07:21:51 +0200574 /* Check that the controller is ready */
575 err = intel_wait_booting(hu);
576
577 clear_bit(STATE_BOOTING, &intel->flags);
578
579 /* In case of timeout, try to continue anyway */
580 if (err && err != ETIMEDOUT)
581 return err;
582
Loic Poulainca93cee2015-07-01 12:20:26 +0200583 set_bit(STATE_BOOTLOADER, &intel->flags);
584
585 /* Read the Intel version information to determine if the device
586 * is in bootloader mode or if it already has operational firmware
587 * loaded.
588 */
589 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
590 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200591 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
592 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200593 return PTR_ERR(skb);
594 }
595
596 if (skb->len != sizeof(*ver)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200597 bt_dev_err(hdev, "Intel version event size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200598 kfree_skb(skb);
599 return -EILSEQ;
600 }
601
602 ver = (struct intel_version *)skb->data;
603 if (ver->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200604 bt_dev_err(hdev, "Intel version command failure (%02x)",
605 ver->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200606 err = -bt_to_errno(ver->status);
607 kfree_skb(skb);
608 return err;
609 }
610
611 /* The hardware platform number has a fixed value of 0x37 and
612 * for now only accept this single value.
613 */
614 if (ver->hw_platform != 0x37) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200615 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
616 ver->hw_platform);
Loic Poulainca93cee2015-07-01 12:20:26 +0200617 kfree_skb(skb);
618 return -EINVAL;
619 }
620
621 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
622 * supported by this firmware loading method. This check has been
623 * put in place to ensure correct forward compatibility options
624 * when newer hardware variants come along.
625 */
626 if (ver->hw_variant != 0x0b) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200627 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
628 ver->hw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200629 kfree_skb(skb);
630 return -EINVAL;
631 }
632
Marcel Holtmann7feb99e2015-07-05 15:02:07 +0200633 btintel_version_info(hdev, ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200634
635 /* The firmware variant determines if the device is in bootloader
636 * mode or is running operational firmware. The value 0x06 identifies
637 * the bootloader and the value 0x23 identifies the operational
638 * firmware.
639 *
640 * When the operational firmware is already present, then only
641 * the check for valid Bluetooth device address is needed. This
642 * determines if the device will be added as configured or
643 * unconfigured controller.
644 *
645 * It is not possible to use the Secure Boot Parameters in this
646 * case since that command is only available in bootloader mode.
647 */
648 if (ver->fw_variant == 0x23) {
649 kfree_skb(skb);
650 clear_bit(STATE_BOOTLOADER, &intel->flags);
651 btintel_check_bdaddr(hdev);
652 return 0;
653 }
654
655 /* If the device is not in bootloader mode, then the only possible
656 * choice is to return an error and abort the device initialization.
657 */
658 if (ver->fw_variant != 0x06) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200659 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
660 ver->fw_variant);
Loic Poulainca93cee2015-07-01 12:20:26 +0200661 kfree_skb(skb);
662 return -ENODEV;
663 }
664
665 kfree_skb(skb);
666
667 /* Read the secure boot parameters to identify the operating
668 * details of the bootloader.
669 */
670 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
671 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200672 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
673 PTR_ERR(skb));
Loic Poulainca93cee2015-07-01 12:20:26 +0200674 return PTR_ERR(skb);
675 }
676
677 if (skb->len != sizeof(*params)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200678 bt_dev_err(hdev, "Intel boot parameters size mismatch");
Loic Poulainca93cee2015-07-01 12:20:26 +0200679 kfree_skb(skb);
680 return -EILSEQ;
681 }
682
683 params = (struct intel_boot_params *)skb->data;
684 if (params->status) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200685 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
686 params->status);
Loic Poulainca93cee2015-07-01 12:20:26 +0200687 err = -bt_to_errno(params->status);
688 kfree_skb(skb);
689 return err;
690 }
691
Loic Poulainf44e78a2015-08-31 18:34:30 +0200692 bt_dev_info(hdev, "Device revision is %u",
693 le16_to_cpu(params->dev_revid));
Loic Poulainca93cee2015-07-01 12:20:26 +0200694
Loic Poulainf44e78a2015-08-31 18:34:30 +0200695 bt_dev_info(hdev, "Secure boot is %s",
696 params->secure_boot ? "enabled" : "disabled");
Loic Poulainca93cee2015-07-01 12:20:26 +0200697
Loic Poulainf44e78a2015-08-31 18:34:30 +0200698 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
Loic Poulainca93cee2015-07-01 12:20:26 +0200699 params->min_fw_build_nn, params->min_fw_build_cw,
700 2000 + params->min_fw_build_yy);
701
702 /* It is required that every single firmware fragment is acknowledged
703 * with a command complete event. If the boot parameters indicate
704 * that this bootloader does not send them, then abort the setup.
705 */
706 if (params->limited_cce != 0x00) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200707 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
708 params->limited_cce);
Loic Poulainca93cee2015-07-01 12:20:26 +0200709 kfree_skb(skb);
710 return -EINVAL;
711 }
712
713 /* If the OTP has no valid Bluetooth device address, then there will
714 * also be no valid address for the operational firmware.
715 */
716 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200717 bt_dev_info(hdev, "No device address configured");
Loic Poulainca93cee2015-07-01 12:20:26 +0200718 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
719 }
720
721 /* With this Intel bootloader only the hardware variant and device
722 * revision information are used to select the right firmware.
723 *
724 * Currently this bootloader support is limited to hardware variant
725 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
726 */
727 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
728 le16_to_cpu(params->dev_revid));
729
730 err = request_firmware(&fw, fwname, &hdev->dev);
731 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200732 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
733 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200734 kfree_skb(skb);
735 return err;
736 }
737
Loic Poulainf44e78a2015-08-31 18:34:30 +0200738 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Loic Poulainca93cee2015-07-01 12:20:26 +0200739
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200740 /* Save the DDC file name for later */
741 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
742 le16_to_cpu(params->dev_revid));
743
Loic Poulainca93cee2015-07-01 12:20:26 +0200744 kfree_skb(skb);
745
746 if (fw->size < 644) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200747 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
748 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200749 err = -EBADF;
750 goto done;
751 }
752
753 set_bit(STATE_DOWNLOADING, &intel->flags);
754
755 /* Start the firmware download transaction with the Init fragment
756 * represented by the 128 bytes of CSS header.
757 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200758 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200759 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200760 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200761 goto done;
762 }
763
764 /* Send the 256 bytes of public key information from the firmware
765 * as the PKey fragment.
766 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200767 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
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 public key (%d)",
770 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200771 goto done;
772 }
773
774 /* Send the 256 bytes of signature information from the firmware
775 * as the Sign fragment.
776 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200777 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200778 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200779 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
780 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200781 goto done;
782 }
783
784 fw_ptr = fw->data + 644;
785 frag_len = 0;
786
787 while (fw_ptr - fw->data < fw->size) {
788 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
789
790 frag_len += sizeof(*cmd) + cmd->plen;
791
Loic Poulainf44e78a2015-08-31 18:34:30 +0200792 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
793 fw->size);
Loic Poulainca93cee2015-07-01 12:20:26 +0200794
795 /* The parameter length of the secure send command requires
796 * a 4 byte alignment. It happens so that the firmware file
797 * contains proper Intel_NOP commands to align the fragments
798 * as needed.
799 *
800 * Send set of commands with 4 byte alignment from the
801 * firmware data buffer as a single Data fragement.
802 */
803 if (frag_len % 4)
804 continue;
805
806 /* Send each command from the firmware data buffer as
807 * a single Data fragment.
808 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200809 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200810 if (err < 0) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200811 bt_dev_err(hdev, "Failed to send firmware data (%d)",
812 err);
Loic Poulainca93cee2015-07-01 12:20:26 +0200813 goto done;
814 }
815
816 fw_ptr += frag_len;
817 frag_len = 0;
818 }
819
820 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
821
Loic Poulainf44e78a2015-08-31 18:34:30 +0200822 bt_dev_info(hdev, "Waiting for firmware download to complete");
Loic Poulainca93cee2015-07-01 12:20:26 +0200823
824 /* Before switching the device into operational mode and with that
825 * booting the loaded firmware, wait for the bootloader notification
826 * that all fragments have been successfully received.
827 *
828 * When the event processing receives the notification, then the
829 * STATE_DOWNLOADING flag will be cleared.
830 *
831 * The firmware loading should not take longer than 5 seconds
832 * and thus just timeout if that happens and fail the setup
833 * of this device.
834 */
835 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
836 TASK_INTERRUPTIBLE,
837 msecs_to_jiffies(5000));
838 if (err == 1) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200839 bt_dev_err(hdev, "Firmware loading interrupted");
Loic Poulainca93cee2015-07-01 12:20:26 +0200840 err = -EINTR;
841 goto done;
842 }
843
844 if (err) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200845 bt_dev_err(hdev, "Firmware loading timeout");
Loic Poulainca93cee2015-07-01 12:20:26 +0200846 err = -ETIMEDOUT;
847 goto done;
848 }
849
850 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200851 bt_dev_err(hdev, "Firmware loading failed");
Loic Poulainca93cee2015-07-01 12:20:26 +0200852 err = -ENOEXEC;
853 goto done;
854 }
855
856 rettime = ktime_get();
857 delta = ktime_sub(rettime, calltime);
858 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
859
Loic Poulainf44e78a2015-08-31 18:34:30 +0200860 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200861
862done:
863 release_firmware(fw);
864
865 if (err < 0)
866 return err;
867
Loic Poulainff289552015-08-25 17:55:44 +0200868 /* We need to restore the default speed before Intel reset */
869 if (speed_change) {
870 err = intel_set_baudrate(hu, init_speed);
871 if (err)
872 return err;
873 }
874
Loic Poulainca93cee2015-07-01 12:20:26 +0200875 calltime = ktime_get();
876
877 set_bit(STATE_BOOTING, &intel->flags);
878
879 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
880 HCI_INIT_TIMEOUT);
881 if (IS_ERR(skb))
882 return PTR_ERR(skb);
883
884 kfree_skb(skb);
885
886 /* The bootloader will not indicate when the device is ready. This
887 * is done by the operational firmware sending bootup notification.
888 *
889 * Booting into operational firmware should not take longer than
890 * 1 second. However if that happens, then just fail the setup
891 * since something went wrong.
892 */
Loic Poulainf44e78a2015-08-31 18:34:30 +0200893 bt_dev_info(hdev, "Waiting for device to boot");
Loic Poulainca93cee2015-07-01 12:20:26 +0200894
Loic Poulain1ab1f232015-08-27 07:21:51 +0200895 err = intel_wait_booting(hu);
896 if (err)
897 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200898
Loic Poulain1ab1f232015-08-27 07:21:51 +0200899 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200900
901 rettime = ktime_get();
902 delta = ktime_sub(rettime, calltime);
903 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
904
Loic Poulainf44e78a2015-08-31 18:34:30 +0200905 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Loic Poulainca93cee2015-07-01 12:20:26 +0200906
Loic Poulainb98469f2015-08-29 13:38:19 +0200907 /* Enable LPM if matching pdev with wakeup enabled */
Loic Poulain67c8bde2015-08-31 18:34:31 +0200908 mutex_lock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200909 list_for_each(p, &intel_device_list) {
910 struct intel_device *dev = list_entry(p, struct intel_device,
911 list);
912 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
913 if (device_may_wakeup(&dev->pdev->dev))
914 idev = dev;
915 break;
916 }
917 }
Loic Poulain67c8bde2015-08-31 18:34:31 +0200918 mutex_unlock(&intel_device_list_lock);
Loic Poulainb98469f2015-08-29 13:38:19 +0200919
920 if (!idev)
921 goto no_lpm;
922
Loic Poulainf44e78a2015-08-31 18:34:30 +0200923 bt_dev_info(hdev, "Enabling LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200924
925 skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
926 HCI_CMD_TIMEOUT);
927 if (IS_ERR(skb)) {
Loic Poulainf44e78a2015-08-31 18:34:30 +0200928 bt_dev_err(hdev, "Failed to enable LPM");
Loic Poulainb98469f2015-08-29 13:38:19 +0200929 goto no_lpm;
930 }
931 kfree_skb(skb);
932
933 set_bit(STATE_LPM_ENABLED, &intel->flags);
934
935no_lpm:
Loic Poulain1cfbabd2015-09-04 17:54:35 +0200936 /* Ignore errors, device can work without DDC parameters */
937 btintel_load_ddc_config(hdev, fwname);
938
Loic Poulainff289552015-08-25 17:55:44 +0200939 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
940 if (IS_ERR(skb))
941 return PTR_ERR(skb);
942 kfree_skb(skb);
943
944 if (speed_change) {
945 err = intel_set_baudrate(hu, oper_speed);
946 if (err)
947 return err;
948 }
949
Loic Poulainf44e78a2015-08-31 18:34:30 +0200950 bt_dev_info(hdev, "Setup complete");
Loic Poulainff289552015-08-25 17:55:44 +0200951
Loic Poulainca93cee2015-07-01 12:20:26 +0200952 clear_bit(STATE_BOOTLOADER, &intel->flags);
953
954 return 0;
955}
956
957static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
958{
959 struct hci_uart *hu = hci_get_drvdata(hdev);
960 struct intel_data *intel = hu->priv;
961 struct hci_event_hdr *hdr;
962
Loic Poulain1ab1f232015-08-27 07:21:51 +0200963 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
964 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200965 goto recv;
966
967 hdr = (void *)skb->data;
968
969 /* When the firmware loading completes the device sends
970 * out a vendor specific event indicating the result of
971 * the firmware loading.
972 */
973 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
974 skb->data[2] == 0x06) {
975 if (skb->data[3] != 0x00)
976 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
977
978 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
979 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
980 smp_mb__after_atomic();
981 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
982 }
983
984 /* When switching to the operational firmware the device
985 * sends a vendor specific event indicating that the bootup
986 * completed.
987 */
988 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
989 skb->data[2] == 0x02) {
990 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
991 smp_mb__after_atomic();
992 wake_up_bit(&intel->flags, STATE_BOOTING);
993 }
994 }
995recv:
996 return hci_recv_frame(hdev, skb);
997}
998
Loic Poulainb98469f2015-08-29 13:38:19 +0200999static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
1000{
1001 struct hci_uart *hu = hci_get_drvdata(hdev);
1002 struct intel_data *intel = hu->priv;
1003
Loic Poulainf44e78a2015-08-31 18:34:30 +02001004 bt_dev_dbg(hdev, "TX idle notification (%d)", value);
Loic Poulainb98469f2015-08-29 13:38:19 +02001005
Loic Poulain74cdad32015-09-02 12:04:13 +02001006 if (value) {
Loic Poulainb98469f2015-08-29 13:38:19 +02001007 set_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001008 schedule_work(&intel->busy_work);
1009 } else {
Loic Poulainb98469f2015-08-29 13:38:19 +02001010 clear_bit(STATE_TX_ACTIVE, &intel->flags);
Loic Poulain74cdad32015-09-02 12:04:13 +02001011 }
Loic Poulainb98469f2015-08-29 13:38:19 +02001012}
1013
1014static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1015{
1016 struct hci_lpm_pkt *lpm = (void *)skb->data;
Loic Poulain89436542015-09-02 12:04:11 +02001017 struct hci_uart *hu = hci_get_drvdata(hdev);
1018 struct intel_data *intel = hu->priv;
Loic Poulainb98469f2015-08-29 13:38:19 +02001019
1020 switch (lpm->opcode) {
1021 case LPM_OP_TX_NOTIFY:
Loic Poulain1b197572015-09-02 12:04:14 +02001022 if (lpm->dlen < 1) {
1023 bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1024 break;
1025 }
1026 intel_recv_lpm_notify(hdev, lpm->data[0]);
Loic Poulainb98469f2015-08-29 13:38:19 +02001027 break;
Loic Poulain89436542015-09-02 12:04:11 +02001028 case LPM_OP_SUSPEND_ACK:
1029 set_bit(STATE_SUSPENDED, &intel->flags);
1030 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1031 smp_mb__after_atomic();
1032 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1033 }
1034 break;
1035 case LPM_OP_RESUME_ACK:
1036 clear_bit(STATE_SUSPENDED, &intel->flags);
1037 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1038 smp_mb__after_atomic();
1039 wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1040 }
1041 break;
Loic Poulainb98469f2015-08-29 13:38:19 +02001042 default:
Loic Poulainf44e78a2015-08-31 18:34:30 +02001043 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
Loic Poulainb98469f2015-08-29 13:38:19 +02001044 break;
1045 }
1046
1047 kfree_skb(skb);
1048
1049 return 0;
1050}
1051
1052#define INTEL_RECV_LPM \
1053 .type = HCI_LPM_PKT, \
1054 .hlen = HCI_LPM_HDR_SIZE, \
1055 .loff = 1, \
1056 .lsize = 1, \
1057 .maxlen = HCI_LPM_MAX_SIZE
1058
Loic Poulainca93cee2015-07-01 12:20:26 +02001059static const struct h4_recv_pkt intel_recv_pkts[] = {
Loic Poulainb98469f2015-08-29 13:38:19 +02001060 { H4_RECV_ACL, .recv = hci_recv_frame },
1061 { H4_RECV_SCO, .recv = hci_recv_frame },
1062 { H4_RECV_EVENT, .recv = intel_recv_event },
1063 { INTEL_RECV_LPM, .recv = intel_recv_lpm },
Loic Poulainca93cee2015-07-01 12:20:26 +02001064};
1065
1066static int intel_recv(struct hci_uart *hu, const void *data, int count)
1067{
1068 struct intel_data *intel = hu->priv;
1069
1070 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1071 return -EUNATCH;
1072
1073 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1074 intel_recv_pkts,
1075 ARRAY_SIZE(intel_recv_pkts));
1076 if (IS_ERR(intel->rx_skb)) {
1077 int err = PTR_ERR(intel->rx_skb);
Loic Poulainf44e78a2015-08-31 18:34:30 +02001078 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
Loic Poulainca93cee2015-07-01 12:20:26 +02001079 intel->rx_skb = NULL;
1080 return err;
1081 }
1082
1083 return count;
1084}
1085
1086static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1087{
1088 struct intel_data *intel = hu->priv;
Loic Poulain74cdad32015-09-02 12:04:13 +02001089 struct list_head *p;
Loic Poulainca93cee2015-07-01 12:20:26 +02001090
1091 BT_DBG("hu %p skb %p", hu, skb);
1092
Loic Poulain74cdad32015-09-02 12:04:13 +02001093 /* Be sure our controller is resumed and potential LPM transaction
1094 * completed before enqueuing any packet.
1095 */
1096 mutex_lock(&intel_device_list_lock);
1097 list_for_each(p, &intel_device_list) {
1098 struct intel_device *idev = list_entry(p, struct intel_device,
1099 list);
1100
1101 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1102 pm_runtime_get_sync(&idev->pdev->dev);
1103 pm_runtime_mark_last_busy(&idev->pdev->dev);
1104 pm_runtime_put_autosuspend(&idev->pdev->dev);
1105 break;
1106 }
1107 }
1108 mutex_unlock(&intel_device_list_lock);
1109
Loic Poulainca93cee2015-07-01 12:20:26 +02001110 skb_queue_tail(&intel->txq, skb);
1111
1112 return 0;
1113}
1114
1115static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1116{
1117 struct intel_data *intel = hu->priv;
1118 struct sk_buff *skb;
1119
1120 skb = skb_dequeue(&intel->txq);
1121 if (!skb)
1122 return skb;
1123
1124 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1125 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1126 struct hci_command_hdr *cmd = (void *)skb->data;
1127 __u16 opcode = le16_to_cpu(cmd->opcode);
1128
1129 /* When the 0xfc01 command is issued to boot into
1130 * the operational firmware, it will actually not
1131 * send a command complete event. To keep the flow
1132 * control working inject that event here.
1133 */
1134 if (opcode == 0xfc01)
1135 inject_cmd_complete(hu->hdev, opcode);
1136 }
1137
1138 /* Prepend skb with frame type */
1139 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1140
1141 return skb;
1142}
1143
1144static const struct hci_uart_proto intel_proto = {
1145 .id = HCI_UART_INTEL,
1146 .name = "Intel",
1147 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +02001148 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +02001149 .open = intel_open,
1150 .close = intel_close,
1151 .flush = intel_flush,
1152 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +02001153 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +02001154 .recv = intel_recv,
1155 .enqueue = intel_enqueue,
1156 .dequeue = intel_dequeue,
1157};
1158
Loic Poulain1ab1f232015-08-27 07:21:51 +02001159#ifdef CONFIG_ACPI
1160static const struct acpi_device_id intel_acpi_match[] = {
1161 { "INT33E1", 0 },
1162 { },
1163};
1164MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1165
1166static int intel_acpi_probe(struct intel_device *idev)
1167{
1168 const struct acpi_device_id *id;
1169
1170 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1171 if (!id)
1172 return -ENODEV;
1173
1174 return 0;
1175}
1176#else
1177static int intel_acpi_probe(struct intel_device *idev)
1178{
1179 return -ENODEV;
1180}
1181#endif
1182
Loic Poulain74cdad32015-09-02 12:04:13 +02001183#ifdef CONFIG_PM
Loic Poulainaa6802d2015-09-02 12:04:12 +02001184static int intel_suspend(struct device *dev)
1185{
1186 struct intel_device *idev = dev_get_drvdata(dev);
1187
1188 dev_dbg(dev, "intel_suspend");
1189
1190 mutex_lock(&idev->hu_lock);
1191 if (idev->hu)
1192 intel_lpm_suspend(idev->hu);
1193 mutex_unlock(&idev->hu_lock);
1194
1195 return 0;
1196}
1197
1198static int intel_resume(struct device *dev)
1199{
1200 struct intel_device *idev = dev_get_drvdata(dev);
1201
1202 dev_dbg(dev, "intel_resume");
1203
1204 mutex_lock(&idev->hu_lock);
1205 if (idev->hu)
1206 intel_lpm_resume(idev->hu);
1207 mutex_unlock(&idev->hu_lock);
1208
1209 return 0;
1210}
1211#endif
1212
1213static const struct dev_pm_ops intel_pm_ops = {
1214 SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
Loic Poulain74cdad32015-09-02 12:04:13 +02001215 SET_RUNTIME_PM_OPS(intel_suspend, intel_resume, NULL)
Loic Poulainaa6802d2015-09-02 12:04:12 +02001216};
1217
Loic Poulain1ab1f232015-08-27 07:21:51 +02001218static int intel_probe(struct platform_device *pdev)
1219{
1220 struct intel_device *idev;
1221
1222 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1223 if (!idev)
1224 return -ENOMEM;
1225
Loic Poulainaa6802d2015-09-02 12:04:12 +02001226 mutex_init(&idev->hu_lock);
1227
Loic Poulain1ab1f232015-08-27 07:21:51 +02001228 idev->pdev = pdev;
1229
1230 if (ACPI_HANDLE(&pdev->dev)) {
1231 int err = intel_acpi_probe(idev);
1232 if (err)
1233 return err;
1234 } else {
1235 return -ENODEV;
1236 }
1237
1238 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1239 GPIOD_OUT_LOW);
1240 if (IS_ERR(idev->reset)) {
1241 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1242 return PTR_ERR(idev->reset);
1243 }
1244
Loic Poulain765ea3a2015-08-29 13:38:18 +02001245 idev->irq = platform_get_irq(pdev, 0);
1246 if (idev->irq < 0) {
1247 struct gpio_desc *host_wake;
1248
1249 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1250
1251 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1252 GPIOD_IN);
1253 if (IS_ERR(host_wake)) {
1254 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1255 goto no_irq;
1256 }
1257
1258 idev->irq = gpiod_to_irq(host_wake);
1259 if (idev->irq < 0) {
1260 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1261 goto no_irq;
1262 }
1263 }
1264
1265 /* Only enable wake-up/irq when controller is powered */
1266 device_set_wakeup_capable(&pdev->dev, true);
1267 device_wakeup_disable(&pdev->dev);
1268
1269no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +02001270 platform_set_drvdata(pdev, idev);
1271
1272 /* Place this instance on the device list */
Loic Poulain67c8bde2015-08-31 18:34:31 +02001273 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001274 list_add_tail(&idev->list, &intel_device_list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001275 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001276
Loic Poulain765ea3a2015-08-29 13:38:18 +02001277 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1278 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001279
1280 return 0;
1281}
1282
1283static int intel_remove(struct platform_device *pdev)
1284{
1285 struct intel_device *idev = platform_get_drvdata(pdev);
1286
Loic Poulain765ea3a2015-08-29 13:38:18 +02001287 device_wakeup_disable(&pdev->dev);
1288
Loic Poulain67c8bde2015-08-31 18:34:31 +02001289 mutex_lock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001290 list_del(&idev->list);
Loic Poulain67c8bde2015-08-31 18:34:31 +02001291 mutex_unlock(&intel_device_list_lock);
Loic Poulain1ab1f232015-08-27 07:21:51 +02001292
1293 dev_info(&pdev->dev, "unregistered.\n");
1294
1295 return 0;
1296}
1297
1298static struct platform_driver intel_driver = {
1299 .probe = intel_probe,
1300 .remove = intel_remove,
1301 .driver = {
1302 .name = "hci_intel",
1303 .acpi_match_table = ACPI_PTR(intel_acpi_match),
Loic Poulainaa6802d2015-09-02 12:04:12 +02001304 .pm = &intel_pm_ops,
Loic Poulain1ab1f232015-08-27 07:21:51 +02001305 },
1306};
1307
Loic Poulainca93cee2015-07-01 12:20:26 +02001308int __init intel_init(void)
1309{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001310 platform_driver_register(&intel_driver);
1311
Loic Poulainca93cee2015-07-01 12:20:26 +02001312 return hci_uart_register_proto(&intel_proto);
1313}
1314
1315int __exit intel_deinit(void)
1316{
Loic Poulain1ab1f232015-08-27 07:21:51 +02001317 platform_driver_unregister(&intel_driver);
1318
Loic Poulainca93cee2015-07-01 12:20:26 +02001319 return hci_uart_unregister_proto(&intel_proto);
1320}