blob: 92bd17eb0acfc66b71fa48498d00e75910b155a9 [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>
Marcel Holtmann16e38872015-04-04 16:13:02 -070035
36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h>
38
39#include "hci_uart.h"
Loic Poulainca93cee2015-07-01 12:20:26 +020040#include "btintel.h"
41
42#define STATE_BOOTLOADER 0
43#define STATE_DOWNLOADING 1
44#define STATE_FIRMWARE_LOADED 2
45#define STATE_FIRMWARE_FAILED 3
46#define STATE_BOOTING 4
47
Loic Poulain1ab1f232015-08-27 07:21:51 +020048struct intel_device {
49 struct list_head list;
50 struct platform_device *pdev;
51 struct gpio_desc *reset;
Loic Poulain765ea3a2015-08-29 13:38:18 +020052 int irq;
Loic Poulain1ab1f232015-08-27 07:21:51 +020053};
54
55static LIST_HEAD(intel_device_list);
56static DEFINE_SPINLOCK(intel_device_list_lock);
57
Loic Poulainca93cee2015-07-01 12:20:26 +020058struct intel_data {
59 struct sk_buff *rx_skb;
60 struct sk_buff_head txq;
61 unsigned long flags;
62};
63
Loic Poulainff289552015-08-25 17:55:44 +020064static u8 intel_convert_speed(unsigned int speed)
65{
66 switch (speed) {
67 case 9600:
68 return 0x00;
69 case 19200:
70 return 0x01;
71 case 38400:
72 return 0x02;
73 case 57600:
74 return 0x03;
75 case 115200:
76 return 0x04;
77 case 230400:
78 return 0x05;
79 case 460800:
80 return 0x06;
81 case 921600:
82 return 0x07;
83 case 1843200:
84 return 0x08;
85 case 3250000:
86 return 0x09;
87 case 2000000:
88 return 0x0a;
89 case 3000000:
90 return 0x0b;
91 default:
92 return 0xff;
93 }
94}
95
Loic Poulain1ab1f232015-08-27 07:21:51 +020096static int intel_wait_booting(struct hci_uart *hu)
97{
98 struct intel_data *intel = hu->priv;
99 int err;
100
101 err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
102 TASK_INTERRUPTIBLE,
103 msecs_to_jiffies(1000));
104
105 if (err == 1) {
106 BT_ERR("%s: Device boot interrupted", hu->hdev->name);
107 return -EINTR;
108 }
109
110 if (err) {
111 BT_ERR("%s: Device boot timeout", hu->hdev->name);
112 return -ETIMEDOUT;
113 }
114
115 return err;
116}
117
Loic Poulain765ea3a2015-08-29 13:38:18 +0200118static irqreturn_t intel_irq(int irq, void *dev_id)
119{
120 struct intel_device *idev = dev_id;
121
122 dev_info(&idev->pdev->dev, "hci_intel irq\n");
123
124 return IRQ_HANDLED;
125}
126
Loic Poulain1ab1f232015-08-27 07:21:51 +0200127static int intel_set_power(struct hci_uart *hu, bool powered)
128{
129 struct list_head *p;
130 int err = -ENODEV;
131
132 spin_lock(&intel_device_list_lock);
133
134 list_for_each(p, &intel_device_list) {
135 struct intel_device *idev = list_entry(p, struct intel_device,
136 list);
137
138 /* tty device and pdev device should share the same parent
139 * which is the UART port.
140 */
141 if (hu->tty->dev->parent != idev->pdev->dev.parent)
142 continue;
143
144 if (!idev->reset) {
145 err = -ENOTSUPP;
146 break;
147 }
148
149 BT_INFO("hu %p, Switching compatible pm device (%s) to %u",
150 hu, dev_name(&idev->pdev->dev), powered);
151
152 gpiod_set_value(idev->reset, powered);
Loic Poulain765ea3a2015-08-29 13:38:18 +0200153
154 if (idev->irq < 0)
155 break;
156
157 if (powered && device_can_wakeup(&idev->pdev->dev)) {
158 err = devm_request_threaded_irq(&idev->pdev->dev,
159 idev->irq, NULL,
160 intel_irq,
161 IRQF_ONESHOT,
162 "bt-host-wake", idev);
163 if (err) {
164 BT_ERR("hu %p, unable to allocate irq-%d",
165 hu, idev->irq);
166 break;
167 }
168
169 device_wakeup_enable(&idev->pdev->dev);
170 } else if (!powered && device_may_wakeup(&idev->pdev->dev)) {
171 devm_free_irq(&idev->pdev->dev, idev->irq, idev);
172 device_wakeup_disable(&idev->pdev->dev);
173 }
Loic Poulain1ab1f232015-08-27 07:21:51 +0200174 }
175
176 spin_unlock(&intel_device_list_lock);
177
178 return err;
179}
180
Loic Poulainca93cee2015-07-01 12:20:26 +0200181static int intel_open(struct hci_uart *hu)
182{
183 struct intel_data *intel;
184
185 BT_DBG("hu %p", hu);
186
187 intel = kzalloc(sizeof(*intel), GFP_KERNEL);
188 if (!intel)
189 return -ENOMEM;
190
191 skb_queue_head_init(&intel->txq);
192
193 hu->priv = intel;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200194
195 if (!intel_set_power(hu, true))
196 set_bit(STATE_BOOTING, &intel->flags);
197
Loic Poulainca93cee2015-07-01 12:20:26 +0200198 return 0;
199}
200
201static int intel_close(struct hci_uart *hu)
202{
203 struct intel_data *intel = hu->priv;
204
205 BT_DBG("hu %p", hu);
206
Loic Poulain1ab1f232015-08-27 07:21:51 +0200207 intel_set_power(hu, false);
208
Loic Poulainca93cee2015-07-01 12:20:26 +0200209 skb_queue_purge(&intel->txq);
210 kfree_skb(intel->rx_skb);
211 kfree(intel);
212
213 hu->priv = NULL;
214 return 0;
215}
216
217static int intel_flush(struct hci_uart *hu)
218{
219 struct intel_data *intel = hu->priv;
220
221 BT_DBG("hu %p", hu);
222
223 skb_queue_purge(&intel->txq);
224
225 return 0;
226}
227
228static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
229{
230 struct sk_buff *skb;
231 struct hci_event_hdr *hdr;
232 struct hci_ev_cmd_complete *evt;
233
234 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
235 if (!skb)
236 return -ENOMEM;
237
238 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
239 hdr->evt = HCI_EV_CMD_COMPLETE;
240 hdr->plen = sizeof(*evt) + 1;
241
242 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
243 evt->ncmd = 0x01;
244 evt->opcode = cpu_to_le16(opcode);
245
246 *skb_put(skb, 1) = 0x00;
247
248 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
249
250 return hci_recv_frame(hdev, skb);
251}
252
Loic Poulainff289552015-08-25 17:55:44 +0200253static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
254{
255 struct intel_data *intel = hu->priv;
256 struct hci_dev *hdev = hu->hdev;
257 u8 speed_cmd[] = { 0x06, 0xfc, 0x01, 0x00 };
258 struct sk_buff *skb;
Loic Poulain1ab1f232015-08-27 07:21:51 +0200259 int err;
260
261 /* This can be the first command sent to the chip, check
262 * that the controller is ready.
263 */
264 err = intel_wait_booting(hu);
265
266 clear_bit(STATE_BOOTING, &intel->flags);
267
268 /* In case of timeout, try to continue anyway */
269 if (err && err != ETIMEDOUT)
270 return err;
Loic Poulainff289552015-08-25 17:55:44 +0200271
272 BT_INFO("%s: Change controller speed to %d", hdev->name, speed);
273
274 speed_cmd[3] = intel_convert_speed(speed);
275 if (speed_cmd[3] == 0xff) {
276 BT_ERR("%s: Unsupported speed", hdev->name);
277 return -EINVAL;
278 }
279
280 /* Device will not accept speed change if Intel version has not been
281 * previously requested.
282 */
283 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
284 if (IS_ERR(skb)) {
285 BT_ERR("%s: Reading Intel version information failed (%ld)",
286 hdev->name, PTR_ERR(skb));
287 return PTR_ERR(skb);
288 }
289 kfree_skb(skb);
290
291 skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
292 if (!skb) {
293 BT_ERR("%s: Failed to allocate memory for baudrate packet",
294 hdev->name);
295 return -ENOMEM;
296 }
297
298 memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
299 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
300
301 hci_uart_set_flow_control(hu, true);
302
303 skb_queue_tail(&intel->txq, skb);
304 hci_uart_tx_wakeup(hu);
305
306 /* wait 100ms to change baudrate on controller side */
307 msleep(100);
308
309 hci_uart_set_baudrate(hu, speed);
310 hci_uart_set_flow_control(hu, false);
311
312 return 0;
313}
314
Loic Poulainca93cee2015-07-01 12:20:26 +0200315static int intel_setup(struct hci_uart *hu)
316{
317 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
318 0x00, 0x08, 0x04, 0x00 };
319 struct intel_data *intel = hu->priv;
320 struct hci_dev *hdev = hu->hdev;
321 struct sk_buff *skb;
322 struct intel_version *ver;
323 struct intel_boot_params *params;
324 const struct firmware *fw;
325 const u8 *fw_ptr;
326 char fwname[64];
327 u32 frag_len;
328 ktime_t calltime, delta, rettime;
329 unsigned long long duration;
Loic Poulainff289552015-08-25 17:55:44 +0200330 unsigned int init_speed, oper_speed;
331 int speed_change = 0;
Loic Poulainca93cee2015-07-01 12:20:26 +0200332 int err;
333
334 BT_DBG("%s", hdev->name);
335
Marcel Holtmann35ab8152015-07-05 14:37:40 +0200336 hu->hdev->set_bdaddr = btintel_set_bdaddr;
337
Loic Poulainca93cee2015-07-01 12:20:26 +0200338 calltime = ktime_get();
339
Loic Poulainff289552015-08-25 17:55:44 +0200340 if (hu->init_speed)
341 init_speed = hu->init_speed;
342 else
343 init_speed = hu->proto->init_speed;
344
345 if (hu->oper_speed)
346 oper_speed = hu->oper_speed;
347 else
348 oper_speed = hu->proto->oper_speed;
349
350 if (oper_speed && init_speed && oper_speed != init_speed)
351 speed_change = 1;
352
Loic Poulain1ab1f232015-08-27 07:21:51 +0200353 /* Check that the controller is ready */
354 err = intel_wait_booting(hu);
355
356 clear_bit(STATE_BOOTING, &intel->flags);
357
358 /* In case of timeout, try to continue anyway */
359 if (err && err != ETIMEDOUT)
360 return err;
361
Loic Poulainca93cee2015-07-01 12:20:26 +0200362 set_bit(STATE_BOOTLOADER, &intel->flags);
363
364 /* Read the Intel version information to determine if the device
365 * is in bootloader mode or if it already has operational firmware
366 * loaded.
367 */
368 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
369 if (IS_ERR(skb)) {
370 BT_ERR("%s: Reading Intel version information failed (%ld)",
371 hdev->name, PTR_ERR(skb));
372 return PTR_ERR(skb);
373 }
374
375 if (skb->len != sizeof(*ver)) {
376 BT_ERR("%s: Intel version event size mismatch", hdev->name);
377 kfree_skb(skb);
378 return -EILSEQ;
379 }
380
381 ver = (struct intel_version *)skb->data;
382 if (ver->status) {
383 BT_ERR("%s: Intel version command failure (%02x)",
384 hdev->name, ver->status);
385 err = -bt_to_errno(ver->status);
386 kfree_skb(skb);
387 return err;
388 }
389
390 /* The hardware platform number has a fixed value of 0x37 and
391 * for now only accept this single value.
392 */
393 if (ver->hw_platform != 0x37) {
394 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
395 hdev->name, ver->hw_platform);
396 kfree_skb(skb);
397 return -EINVAL;
398 }
399
400 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
401 * supported by this firmware loading method. This check has been
402 * put in place to ensure correct forward compatibility options
403 * when newer hardware variants come along.
404 */
405 if (ver->hw_variant != 0x0b) {
406 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
407 hdev->name, ver->hw_variant);
408 kfree_skb(skb);
409 return -EINVAL;
410 }
411
Marcel Holtmann7feb99e2015-07-05 15:02:07 +0200412 btintel_version_info(hdev, ver);
Loic Poulainca93cee2015-07-01 12:20:26 +0200413
414 /* The firmware variant determines if the device is in bootloader
415 * mode or is running operational firmware. The value 0x06 identifies
416 * the bootloader and the value 0x23 identifies the operational
417 * firmware.
418 *
419 * When the operational firmware is already present, then only
420 * the check for valid Bluetooth device address is needed. This
421 * determines if the device will be added as configured or
422 * unconfigured controller.
423 *
424 * It is not possible to use the Secure Boot Parameters in this
425 * case since that command is only available in bootloader mode.
426 */
427 if (ver->fw_variant == 0x23) {
428 kfree_skb(skb);
429 clear_bit(STATE_BOOTLOADER, &intel->flags);
430 btintel_check_bdaddr(hdev);
431 return 0;
432 }
433
434 /* If the device is not in bootloader mode, then the only possible
435 * choice is to return an error and abort the device initialization.
436 */
437 if (ver->fw_variant != 0x06) {
438 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
439 hdev->name, ver->fw_variant);
440 kfree_skb(skb);
441 return -ENODEV;
442 }
443
444 kfree_skb(skb);
445
446 /* Read the secure boot parameters to identify the operating
447 * details of the bootloader.
448 */
449 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
450 if (IS_ERR(skb)) {
451 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
452 hdev->name, PTR_ERR(skb));
453 return PTR_ERR(skb);
454 }
455
456 if (skb->len != sizeof(*params)) {
457 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
458 kfree_skb(skb);
459 return -EILSEQ;
460 }
461
462 params = (struct intel_boot_params *)skb->data;
463 if (params->status) {
464 BT_ERR("%s: Intel boot parameters command failure (%02x)",
465 hdev->name, params->status);
466 err = -bt_to_errno(params->status);
467 kfree_skb(skb);
468 return err;
469 }
470
471 BT_INFO("%s: Device revision is %u", hdev->name,
472 le16_to_cpu(params->dev_revid));
473
474 BT_INFO("%s: Secure boot is %s", hdev->name,
475 params->secure_boot ? "enabled" : "disabled");
476
477 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
478 params->min_fw_build_nn, params->min_fw_build_cw,
479 2000 + params->min_fw_build_yy);
480
481 /* It is required that every single firmware fragment is acknowledged
482 * with a command complete event. If the boot parameters indicate
483 * that this bootloader does not send them, then abort the setup.
484 */
485 if (params->limited_cce != 0x00) {
486 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
487 hdev->name, params->limited_cce);
488 kfree_skb(skb);
489 return -EINVAL;
490 }
491
492 /* If the OTP has no valid Bluetooth device address, then there will
493 * also be no valid address for the operational firmware.
494 */
495 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
496 BT_INFO("%s: No device address configured", hdev->name);
497 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
498 }
499
500 /* With this Intel bootloader only the hardware variant and device
501 * revision information are used to select the right firmware.
502 *
503 * Currently this bootloader support is limited to hardware variant
504 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
505 */
506 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
507 le16_to_cpu(params->dev_revid));
508
509 err = request_firmware(&fw, fwname, &hdev->dev);
510 if (err < 0) {
511 BT_ERR("%s: Failed to load Intel firmware file (%d)",
512 hdev->name, err);
513 kfree_skb(skb);
514 return err;
515 }
516
517 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
518
519 kfree_skb(skb);
520
521 if (fw->size < 644) {
522 BT_ERR("%s: Invalid size of firmware file (%zu)",
523 hdev->name, fw->size);
524 err = -EBADF;
525 goto done;
526 }
527
528 set_bit(STATE_DOWNLOADING, &intel->flags);
529
530 /* Start the firmware download transaction with the Init fragment
531 * represented by the 128 bytes of CSS header.
532 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200533 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Loic Poulainca93cee2015-07-01 12:20:26 +0200534 if (err < 0) {
535 BT_ERR("%s: Failed to send firmware header (%d)",
536 hdev->name, err);
537 goto done;
538 }
539
540 /* Send the 256 bytes of public key information from the firmware
541 * as the PKey fragment.
542 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200543 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Loic Poulainca93cee2015-07-01 12:20:26 +0200544 if (err < 0) {
545 BT_ERR("%s: Failed to send firmware public key (%d)",
546 hdev->name, err);
547 goto done;
548 }
549
550 /* Send the 256 bytes of signature information from the firmware
551 * as the Sign fragment.
552 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200553 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Loic Poulainca93cee2015-07-01 12:20:26 +0200554 if (err < 0) {
555 BT_ERR("%s: Failed to send firmware signature (%d)",
556 hdev->name, err);
557 goto done;
558 }
559
560 fw_ptr = fw->data + 644;
561 frag_len = 0;
562
563 while (fw_ptr - fw->data < fw->size) {
564 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
565
566 frag_len += sizeof(*cmd) + cmd->plen;
567
568 BT_DBG("%s: patching %td/%zu", hdev->name,
569 (fw_ptr - fw->data), fw->size);
570
571 /* The parameter length of the secure send command requires
572 * a 4 byte alignment. It happens so that the firmware file
573 * contains proper Intel_NOP commands to align the fragments
574 * as needed.
575 *
576 * Send set of commands with 4 byte alignment from the
577 * firmware data buffer as a single Data fragement.
578 */
579 if (frag_len % 4)
580 continue;
581
582 /* Send each command from the firmware data buffer as
583 * a single Data fragment.
584 */
Marcel Holtmann09df1232015-07-05 14:55:36 +0200585 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Loic Poulainca93cee2015-07-01 12:20:26 +0200586 if (err < 0) {
587 BT_ERR("%s: Failed to send firmware data (%d)",
588 hdev->name, err);
589 goto done;
590 }
591
592 fw_ptr += frag_len;
593 frag_len = 0;
594 }
595
596 set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
597
598 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
599
600 /* Before switching the device into operational mode and with that
601 * booting the loaded firmware, wait for the bootloader notification
602 * that all fragments have been successfully received.
603 *
604 * When the event processing receives the notification, then the
605 * STATE_DOWNLOADING flag will be cleared.
606 *
607 * The firmware loading should not take longer than 5 seconds
608 * and thus just timeout if that happens and fail the setup
609 * of this device.
610 */
611 err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
612 TASK_INTERRUPTIBLE,
613 msecs_to_jiffies(5000));
614 if (err == 1) {
615 BT_ERR("%s: Firmware loading interrupted", hdev->name);
616 err = -EINTR;
617 goto done;
618 }
619
620 if (err) {
621 BT_ERR("%s: Firmware loading timeout", hdev->name);
622 err = -ETIMEDOUT;
623 goto done;
624 }
625
626 if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
627 BT_ERR("%s: Firmware loading failed", hdev->name);
628 err = -ENOEXEC;
629 goto done;
630 }
631
632 rettime = ktime_get();
633 delta = ktime_sub(rettime, calltime);
634 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
635
636 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
637
638done:
639 release_firmware(fw);
640
641 if (err < 0)
642 return err;
643
Loic Poulainff289552015-08-25 17:55:44 +0200644 /* We need to restore the default speed before Intel reset */
645 if (speed_change) {
646 err = intel_set_baudrate(hu, init_speed);
647 if (err)
648 return err;
649 }
650
Loic Poulainca93cee2015-07-01 12:20:26 +0200651 calltime = ktime_get();
652
653 set_bit(STATE_BOOTING, &intel->flags);
654
655 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
656 HCI_INIT_TIMEOUT);
657 if (IS_ERR(skb))
658 return PTR_ERR(skb);
659
660 kfree_skb(skb);
661
662 /* The bootloader will not indicate when the device is ready. This
663 * is done by the operational firmware sending bootup notification.
664 *
665 * Booting into operational firmware should not take longer than
666 * 1 second. However if that happens, then just fail the setup
667 * since something went wrong.
668 */
669 BT_INFO("%s: Waiting for device to boot", hdev->name);
670
Loic Poulain1ab1f232015-08-27 07:21:51 +0200671 err = intel_wait_booting(hu);
672 if (err)
673 return err;
Loic Poulainca93cee2015-07-01 12:20:26 +0200674
Loic Poulain1ab1f232015-08-27 07:21:51 +0200675 clear_bit(STATE_BOOTING, &intel->flags);
Loic Poulainca93cee2015-07-01 12:20:26 +0200676
677 rettime = ktime_get();
678 delta = ktime_sub(rettime, calltime);
679 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
680
681 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
682
Loic Poulainff289552015-08-25 17:55:44 +0200683 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
684 if (IS_ERR(skb))
685 return PTR_ERR(skb);
686 kfree_skb(skb);
687
688 if (speed_change) {
689 err = intel_set_baudrate(hu, oper_speed);
690 if (err)
691 return err;
692 }
693
694 BT_INFO("%s: Setup complete", hdev->name);
695
Loic Poulainca93cee2015-07-01 12:20:26 +0200696 clear_bit(STATE_BOOTLOADER, &intel->flags);
697
698 return 0;
699}
700
701static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
702{
703 struct hci_uart *hu = hci_get_drvdata(hdev);
704 struct intel_data *intel = hu->priv;
705 struct hci_event_hdr *hdr;
706
Loic Poulain1ab1f232015-08-27 07:21:51 +0200707 if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
708 !test_bit(STATE_BOOTING, &intel->flags))
Loic Poulainca93cee2015-07-01 12:20:26 +0200709 goto recv;
710
711 hdr = (void *)skb->data;
712
713 /* When the firmware loading completes the device sends
714 * out a vendor specific event indicating the result of
715 * the firmware loading.
716 */
717 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
718 skb->data[2] == 0x06) {
719 if (skb->data[3] != 0x00)
720 set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
721
722 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
723 test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
724 smp_mb__after_atomic();
725 wake_up_bit(&intel->flags, STATE_DOWNLOADING);
726 }
727
728 /* When switching to the operational firmware the device
729 * sends a vendor specific event indicating that the bootup
730 * completed.
731 */
732 } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
733 skb->data[2] == 0x02) {
734 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
735 smp_mb__after_atomic();
736 wake_up_bit(&intel->flags, STATE_BOOTING);
737 }
738 }
739recv:
740 return hci_recv_frame(hdev, skb);
741}
742
743static const struct h4_recv_pkt intel_recv_pkts[] = {
744 { H4_RECV_ACL, .recv = hci_recv_frame },
745 { H4_RECV_SCO, .recv = hci_recv_frame },
746 { H4_RECV_EVENT, .recv = intel_recv_event },
747};
748
749static int intel_recv(struct hci_uart *hu, const void *data, int count)
750{
751 struct intel_data *intel = hu->priv;
752
753 if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
754 return -EUNATCH;
755
756 intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
757 intel_recv_pkts,
758 ARRAY_SIZE(intel_recv_pkts));
759 if (IS_ERR(intel->rx_skb)) {
760 int err = PTR_ERR(intel->rx_skb);
761 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
762 intel->rx_skb = NULL;
763 return err;
764 }
765
766 return count;
767}
768
769static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
770{
771 struct intel_data *intel = hu->priv;
772
773 BT_DBG("hu %p skb %p", hu, skb);
774
775 skb_queue_tail(&intel->txq, skb);
776
777 return 0;
778}
779
780static struct sk_buff *intel_dequeue(struct hci_uart *hu)
781{
782 struct intel_data *intel = hu->priv;
783 struct sk_buff *skb;
784
785 skb = skb_dequeue(&intel->txq);
786 if (!skb)
787 return skb;
788
789 if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
790 (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
791 struct hci_command_hdr *cmd = (void *)skb->data;
792 __u16 opcode = le16_to_cpu(cmd->opcode);
793
794 /* When the 0xfc01 command is issued to boot into
795 * the operational firmware, it will actually not
796 * send a command complete event. To keep the flow
797 * control working inject that event here.
798 */
799 if (opcode == 0xfc01)
800 inject_cmd_complete(hu->hdev, opcode);
801 }
802
803 /* Prepend skb with frame type */
804 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
805
806 return skb;
807}
808
809static const struct hci_uart_proto intel_proto = {
810 .id = HCI_UART_INTEL,
811 .name = "Intel",
812 .init_speed = 115200,
Loic Poulainff289552015-08-25 17:55:44 +0200813 .oper_speed = 3000000,
Loic Poulainca93cee2015-07-01 12:20:26 +0200814 .open = intel_open,
815 .close = intel_close,
816 .flush = intel_flush,
817 .setup = intel_setup,
Loic Poulainff289552015-08-25 17:55:44 +0200818 .set_baudrate = intel_set_baudrate,
Loic Poulainca93cee2015-07-01 12:20:26 +0200819 .recv = intel_recv,
820 .enqueue = intel_enqueue,
821 .dequeue = intel_dequeue,
822};
823
Loic Poulain1ab1f232015-08-27 07:21:51 +0200824#ifdef CONFIG_ACPI
825static const struct acpi_device_id intel_acpi_match[] = {
826 { "INT33E1", 0 },
827 { },
828};
829MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
830
831static int intel_acpi_probe(struct intel_device *idev)
832{
833 const struct acpi_device_id *id;
834
835 id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
836 if (!id)
837 return -ENODEV;
838
839 return 0;
840}
841#else
842static int intel_acpi_probe(struct intel_device *idev)
843{
844 return -ENODEV;
845}
846#endif
847
848static int intel_probe(struct platform_device *pdev)
849{
850 struct intel_device *idev;
851
852 idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
853 if (!idev)
854 return -ENOMEM;
855
856 idev->pdev = pdev;
857
858 if (ACPI_HANDLE(&pdev->dev)) {
859 int err = intel_acpi_probe(idev);
860 if (err)
861 return err;
862 } else {
863 return -ENODEV;
864 }
865
866 idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
867 GPIOD_OUT_LOW);
868 if (IS_ERR(idev->reset)) {
869 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
870 return PTR_ERR(idev->reset);
871 }
872
Loic Poulain765ea3a2015-08-29 13:38:18 +0200873 idev->irq = platform_get_irq(pdev, 0);
874 if (idev->irq < 0) {
875 struct gpio_desc *host_wake;
876
877 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
878
879 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
880 GPIOD_IN);
881 if (IS_ERR(host_wake)) {
882 dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
883 goto no_irq;
884 }
885
886 idev->irq = gpiod_to_irq(host_wake);
887 if (idev->irq < 0) {
888 dev_err(&pdev->dev, "No corresponding irq for gpio\n");
889 goto no_irq;
890 }
891 }
892
893 /* Only enable wake-up/irq when controller is powered */
894 device_set_wakeup_capable(&pdev->dev, true);
895 device_wakeup_disable(&pdev->dev);
896
897no_irq:
Loic Poulain1ab1f232015-08-27 07:21:51 +0200898 platform_set_drvdata(pdev, idev);
899
900 /* Place this instance on the device list */
901 spin_lock(&intel_device_list_lock);
902 list_add_tail(&idev->list, &intel_device_list);
903 spin_unlock(&intel_device_list_lock);
904
Loic Poulain765ea3a2015-08-29 13:38:18 +0200905 dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
906 desc_to_gpio(idev->reset), idev->irq);
Loic Poulain1ab1f232015-08-27 07:21:51 +0200907
908 return 0;
909}
910
911static int intel_remove(struct platform_device *pdev)
912{
913 struct intel_device *idev = platform_get_drvdata(pdev);
914
Loic Poulain765ea3a2015-08-29 13:38:18 +0200915 device_wakeup_disable(&pdev->dev);
916
Loic Poulain1ab1f232015-08-27 07:21:51 +0200917 spin_lock(&intel_device_list_lock);
918 list_del(&idev->list);
919 spin_unlock(&intel_device_list_lock);
920
921 dev_info(&pdev->dev, "unregistered.\n");
922
923 return 0;
924}
925
926static struct platform_driver intel_driver = {
927 .probe = intel_probe,
928 .remove = intel_remove,
929 .driver = {
930 .name = "hci_intel",
931 .acpi_match_table = ACPI_PTR(intel_acpi_match),
932 },
933};
934
Loic Poulainca93cee2015-07-01 12:20:26 +0200935int __init intel_init(void)
936{
Loic Poulain1ab1f232015-08-27 07:21:51 +0200937 platform_driver_register(&intel_driver);
938
Loic Poulainca93cee2015-07-01 12:20:26 +0200939 return hci_uart_register_proto(&intel_proto);
940}
941
942int __exit intel_deinit(void)
943{
Loic Poulain1ab1f232015-08-27 07:21:51 +0200944 platform_driver_unregister(&intel_driver);
945
Loic Poulainca93cee2015-07-01 12:20:26 +0200946 return hci_uart_unregister_proto(&intel_proto);
947}