215e8e9dd65f664174e728bef4a91c46a826f1a4
[firefly-linux-kernel-4.4.55.git] / drivers / bluetooth / hci_intel.c
1 /*
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>
27 #include <linux/firmware.h>
28 #include <linux/module.h>
29 #include <linux/wait.h>
30 #include <linux/tty.h>
31 #include <linux/platform_device.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/acpi.h>
34 #include <linux/interrupt.h>
35 #include <linux/pm_runtime.h>
36
37 #include <net/bluetooth/bluetooth.h>
38 #include <net/bluetooth/hci_core.h>
39
40 #include "hci_uart.h"
41 #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
48 #define STATE_LPM_ENABLED       5
49 #define STATE_TX_ACTIVE         6
50 #define STATE_SUSPENDED         7
51 #define STATE_LPM_TRANSACTION   8
52
53 #define HCI_LPM_WAKE_PKT 0xf0
54 #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
59 #define LPM_OP_SUSPEND_ACK 0x02
60 #define LPM_OP_RESUME_ACK 0x03
61
62 #define LPM_SUSPEND_DELAY_MS 1000
63
64 struct hci_lpm_pkt {
65         __u8 opcode;
66         __u8 dlen;
67         __u8 data[0];
68 } __packed;
69
70 struct intel_device {
71         struct list_head list;
72         struct platform_device *pdev;
73         struct gpio_desc *reset;
74         struct hci_uart *hu;
75         struct mutex hu_lock;
76         int irq;
77 };
78
79 static LIST_HEAD(intel_device_list);
80 static DEFINE_MUTEX(intel_device_list_lock);
81
82 struct intel_data {
83         struct sk_buff *rx_skb;
84         struct sk_buff_head txq;
85         struct work_struct busy_work;
86         struct hci_uart *hu;
87         unsigned long flags;
88 };
89
90 static 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
122 static 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) {
132                 bt_dev_err(hu->hdev, "Device boot interrupted");
133                 return -EINTR;
134         }
135
136         if (err) {
137                 bt_dev_err(hu->hdev, "Device boot timeout");
138                 return -ETIMEDOUT;
139         }
140
141         return err;
142 }
143
144 #ifdef CONFIG_PM
145 static 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
167 static 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
213 static 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 }
253 #endif /* CONFIG_PM */
254
255 static 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
283 static 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
289         mutex_lock(&idev->hu_lock);
290         if (idev->hu)
291                 intel_lpm_host_wake(idev->hu);
292         mutex_unlock(&idev->hu_lock);
293
294         /* 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
299         return IRQ_HANDLED;
300 }
301
302 static int intel_set_power(struct hci_uart *hu, bool powered)
303 {
304         struct list_head *p;
305         int err = -ENODEV;
306
307         mutex_lock(&intel_device_list_lock);
308
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);
328
329                 /* 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
338                 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);
354
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);
360                 } 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);
363
364                         pm_runtime_disable(&idev->pdev->dev);
365                 }
366         }
367
368         mutex_unlock(&intel_device_list_lock);
369
370         return err;
371 }
372
373 static 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
395 static 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);
406         INIT_WORK(&intel->busy_work, intel_busy_work);
407
408         intel->hu = hu;
409
410         hu->priv = intel;
411
412         if (!intel_set_power(hu, true))
413                 set_bit(STATE_BOOTING, &intel->flags);
414
415         return 0;
416 }
417
418 static int intel_close(struct hci_uart *hu)
419 {
420         struct intel_data *intel = hu->priv;
421
422         BT_DBG("hu %p", hu);
423
424         cancel_work_sync(&intel->busy_work);
425
426         intel_set_power(hu, false);
427
428         skb_queue_purge(&intel->txq);
429         kfree_skb(intel->rx_skb);
430         kfree(intel);
431
432         hu->priv = NULL;
433         return 0;
434 }
435
436 static 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
447 static 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
472 static 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;
478         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;
490
491         bt_dev_info(hdev, "Change controller speed to %d", speed);
492
493         speed_cmd[3] = intel_convert_speed(speed);
494         if (speed_cmd[3] == 0xff) {
495                 bt_dev_err(hdev, "Unsupported speed");
496                 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)) {
504                 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
505                            PTR_ERR(skb));
506                 return PTR_ERR(skb);
507         }
508         kfree_skb(skb);
509
510         skb = bt_skb_alloc(sizeof(speed_cmd), GFP_KERNEL);
511         if (!skb) {
512                 bt_dev_err(hdev, "Failed to alloc memory for baudrate packet");
513                 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
533 static int intel_setup(struct hci_uart *hu)
534 {
535         static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
536                                           0x00, 0x08, 0x04, 0x00 };
537         static const u8 lpm_param[] = { 0x03, 0x07, 0x01, 0x0b };
538         struct intel_data *intel = hu->priv;
539         struct intel_device *idev = NULL;
540         struct hci_dev *hdev = hu->hdev;
541         struct sk_buff *skb;
542         struct intel_version *ver;
543         struct intel_boot_params *params;
544         struct list_head *p;
545         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;
551         unsigned int init_speed, oper_speed;
552         int speed_change = 0;
553         int err;
554
555         bt_dev_dbg(hdev, "start intel_setup");
556
557         hu->hdev->set_bdaddr = btintel_set_bdaddr;
558
559         calltime = ktime_get();
560
561         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
574         /* 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
583         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)) {
591                 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
592                            PTR_ERR(skb));
593                 return PTR_ERR(skb);
594         }
595
596         if (skb->len != sizeof(*ver)) {
597                 bt_dev_err(hdev, "Intel version event size mismatch");
598                 kfree_skb(skb);
599                 return -EILSEQ;
600         }
601
602         ver = (struct intel_version *)skb->data;
603         if (ver->status) {
604                 bt_dev_err(hdev, "Intel version command failure (%02x)",
605                            ver->status);
606                 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) {
615                 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
616                            ver->hw_platform);
617                 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) {
627                 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
628                            ver->hw_variant);
629                 kfree_skb(skb);
630                 return -EINVAL;
631         }
632
633         btintel_version_info(hdev, ver);
634
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) {
659                 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
660                            ver->fw_variant);
661                 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)) {
672                 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
673                            PTR_ERR(skb));
674                 return PTR_ERR(skb);
675         }
676
677         if (skb->len != sizeof(*params)) {
678                 bt_dev_err(hdev, "Intel boot parameters size mismatch");
679                 kfree_skb(skb);
680                 return -EILSEQ;
681         }
682
683         params = (struct intel_boot_params *)skb->data;
684         if (params->status) {
685                 bt_dev_err(hdev, "Intel boot parameters command failure (%02x)",
686                            params->status);
687                 err = -bt_to_errno(params->status);
688                 kfree_skb(skb);
689                 return err;
690         }
691
692         bt_dev_info(hdev, "Device revision is %u",
693                     le16_to_cpu(params->dev_revid));
694
695         bt_dev_info(hdev, "Secure boot is %s",
696                     params->secure_boot ? "enabled" : "disabled");
697
698         bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
699                 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) {
707                 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
708                            params->limited_cce);
709                 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)) {
717                 bt_dev_info(hdev, "No device address configured");
718                 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) {
732                 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)",
733                            err);
734                 kfree_skb(skb);
735                 return err;
736         }
737
738         bt_dev_info(hdev, "Found device firmware: %s", fwname);
739
740         kfree_skb(skb);
741
742         if (fw->size < 644) {
743                 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
744                            fw->size);
745                 err = -EBADF;
746                 goto done;
747         }
748
749         set_bit(STATE_DOWNLOADING, &intel->flags);
750
751         /* Start the firmware download transaction with the Init fragment
752          * represented by the 128 bytes of CSS header.
753          */
754         err = btintel_secure_send(hdev, 0x00, 128, fw->data);
755         if (err < 0) {
756                 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
757                 goto done;
758         }
759
760         /* Send the 256 bytes of public key information from the firmware
761          * as the PKey fragment.
762          */
763         err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
764         if (err < 0) {
765                 bt_dev_err(hdev, "Failed to send firmware public key (%d)",
766                            err);
767                 goto done;
768         }
769
770         /* Send the 256 bytes of signature information from the firmware
771          * as the Sign fragment.
772          */
773         err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
774         if (err < 0) {
775                 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
776                            err);
777                 goto done;
778         }
779
780         fw_ptr = fw->data + 644;
781         frag_len = 0;
782
783         while (fw_ptr - fw->data < fw->size) {
784                 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
785
786                 frag_len += sizeof(*cmd) + cmd->plen;
787
788                 bt_dev_dbg(hdev, "Patching %td/%zu", (fw_ptr - fw->data),
789                            fw->size);
790
791                 /* The parameter length of the secure send command requires
792                  * a 4 byte alignment. It happens so that the firmware file
793                  * contains proper Intel_NOP commands to align the fragments
794                  * as needed.
795                  *
796                  * Send set of commands with 4 byte alignment from the
797                  * firmware data buffer as a single Data fragement.
798                  */
799                 if (frag_len % 4)
800                         continue;
801
802                 /* Send each command from the firmware data buffer as
803                  * a single Data fragment.
804                  */
805                 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
806                 if (err < 0) {
807                         bt_dev_err(hdev, "Failed to send firmware data (%d)",
808                                    err);
809                         goto done;
810                 }
811
812                 fw_ptr += frag_len;
813                 frag_len = 0;
814         }
815
816         set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
817
818         bt_dev_info(hdev, "Waiting for firmware download to complete");
819
820         /* Before switching the device into operational mode and with that
821          * booting the loaded firmware, wait for the bootloader notification
822          * that all fragments have been successfully received.
823          *
824          * When the event processing receives the notification, then the
825          * STATE_DOWNLOADING flag will be cleared.
826          *
827          * The firmware loading should not take longer than 5 seconds
828          * and thus just timeout if that happens and fail the setup
829          * of this device.
830          */
831         err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
832                                   TASK_INTERRUPTIBLE,
833                                   msecs_to_jiffies(5000));
834         if (err == 1) {
835                 bt_dev_err(hdev, "Firmware loading interrupted");
836                 err = -EINTR;
837                 goto done;
838         }
839
840         if (err) {
841                 bt_dev_err(hdev, "Firmware loading timeout");
842                 err = -ETIMEDOUT;
843                 goto done;
844         }
845
846         if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
847                 bt_dev_err(hdev, "Firmware loading failed");
848                 err = -ENOEXEC;
849                 goto done;
850         }
851
852         rettime = ktime_get();
853         delta = ktime_sub(rettime, calltime);
854         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
855
856         bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
857
858 done:
859         release_firmware(fw);
860
861         if (err < 0)
862                 return err;
863
864         /* We need to restore the default speed before Intel reset */
865         if (speed_change) {
866                 err = intel_set_baudrate(hu, init_speed);
867                 if (err)
868                         return err;
869         }
870
871         calltime = ktime_get();
872
873         set_bit(STATE_BOOTING, &intel->flags);
874
875         skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
876                              HCI_INIT_TIMEOUT);
877         if (IS_ERR(skb))
878                 return PTR_ERR(skb);
879
880         kfree_skb(skb);
881
882         /* The bootloader will not indicate when the device is ready. This
883          * is done by the operational firmware sending bootup notification.
884          *
885          * Booting into operational firmware should not take longer than
886          * 1 second. However if that happens, then just fail the setup
887          * since something went wrong.
888          */
889         bt_dev_info(hdev, "Waiting for device to boot");
890
891         err = intel_wait_booting(hu);
892         if (err)
893                 return err;
894
895         clear_bit(STATE_BOOTING, &intel->flags);
896
897         rettime = ktime_get();
898         delta = ktime_sub(rettime, calltime);
899         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
900
901         bt_dev_info(hdev, "Device booted in %llu usecs", duration);
902
903         /* Enable LPM if matching pdev with wakeup enabled */
904         mutex_lock(&intel_device_list_lock);
905         list_for_each(p, &intel_device_list) {
906                 struct intel_device *dev = list_entry(p, struct intel_device,
907                                                       list);
908                 if (hu->tty->dev->parent == dev->pdev->dev.parent) {
909                         if (device_may_wakeup(&dev->pdev->dev))
910                                 idev = dev;
911                         break;
912                 }
913         }
914         mutex_unlock(&intel_device_list_lock);
915
916         if (!idev)
917                 goto no_lpm;
918
919         bt_dev_info(hdev, "Enabling LPM");
920
921         skb = __hci_cmd_sync(hdev, 0xfc8b, sizeof(lpm_param), lpm_param,
922                              HCI_CMD_TIMEOUT);
923         if (IS_ERR(skb)) {
924                 bt_dev_err(hdev, "Failed to enable LPM");
925                 goto no_lpm;
926         }
927         kfree_skb(skb);
928
929         set_bit(STATE_LPM_ENABLED, &intel->flags);
930
931 no_lpm:
932         skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_CMD_TIMEOUT);
933         if (IS_ERR(skb))
934                 return PTR_ERR(skb);
935         kfree_skb(skb);
936
937         if (speed_change) {
938                 err = intel_set_baudrate(hu, oper_speed);
939                 if (err)
940                         return err;
941         }
942
943         bt_dev_info(hdev, "Setup complete");
944
945         clear_bit(STATE_BOOTLOADER, &intel->flags);
946
947         return 0;
948 }
949
950 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
951 {
952         struct hci_uart *hu = hci_get_drvdata(hdev);
953         struct intel_data *intel = hu->priv;
954         struct hci_event_hdr *hdr;
955
956         if (!test_bit(STATE_BOOTLOADER, &intel->flags) &&
957             !test_bit(STATE_BOOTING, &intel->flags))
958                 goto recv;
959
960         hdr = (void *)skb->data;
961
962         /* When the firmware loading completes the device sends
963          * out a vendor specific event indicating the result of
964          * the firmware loading.
965          */
966         if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
967             skb->data[2] == 0x06) {
968                 if (skb->data[3] != 0x00)
969                         set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
970
971                 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
972                     test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
973                         smp_mb__after_atomic();
974                         wake_up_bit(&intel->flags, STATE_DOWNLOADING);
975                 }
976
977         /* When switching to the operational firmware the device
978          * sends a vendor specific event indicating that the bootup
979          * completed.
980          */
981         } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
982                    skb->data[2] == 0x02) {
983                 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
984                         smp_mb__after_atomic();
985                         wake_up_bit(&intel->flags, STATE_BOOTING);
986                 }
987         }
988 recv:
989         return hci_recv_frame(hdev, skb);
990 }
991
992 static void intel_recv_lpm_notify(struct hci_dev *hdev, int value)
993 {
994         struct hci_uart *hu = hci_get_drvdata(hdev);
995         struct intel_data *intel = hu->priv;
996
997         bt_dev_dbg(hdev, "TX idle notification (%d)", value);
998
999         if (value) {
1000                 set_bit(STATE_TX_ACTIVE, &intel->flags);
1001                 schedule_work(&intel->busy_work);
1002         } else {
1003                 clear_bit(STATE_TX_ACTIVE, &intel->flags);
1004         }
1005 }
1006
1007 static int intel_recv_lpm(struct hci_dev *hdev, struct sk_buff *skb)
1008 {
1009         struct hci_lpm_pkt *lpm = (void *)skb->data;
1010         struct hci_uart *hu = hci_get_drvdata(hdev);
1011         struct intel_data *intel = hu->priv;
1012
1013         switch (lpm->opcode) {
1014         case LPM_OP_TX_NOTIFY:
1015                 if (lpm->dlen < 1) {
1016                         bt_dev_err(hu->hdev, "Invalid LPM notification packet");
1017                         break;
1018                 }
1019                 intel_recv_lpm_notify(hdev, lpm->data[0]);
1020                 break;
1021         case LPM_OP_SUSPEND_ACK:
1022                 set_bit(STATE_SUSPENDED, &intel->flags);
1023                 if (test_and_clear_bit(STATE_LPM_TRANSACTION, &intel->flags)) {
1024                         smp_mb__after_atomic();
1025                         wake_up_bit(&intel->flags, STATE_LPM_TRANSACTION);
1026                 }
1027                 break;
1028         case LPM_OP_RESUME_ACK:
1029                 clear_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         default:
1036                 bt_dev_err(hdev, "Unknown LPM opcode (%02x)", lpm->opcode);
1037                 break;
1038         }
1039
1040         kfree_skb(skb);
1041
1042         return 0;
1043 }
1044
1045 #define INTEL_RECV_LPM \
1046         .type = HCI_LPM_PKT, \
1047         .hlen = HCI_LPM_HDR_SIZE, \
1048         .loff = 1, \
1049         .lsize = 1, \
1050         .maxlen = HCI_LPM_MAX_SIZE
1051
1052 static const struct h4_recv_pkt intel_recv_pkts[] = {
1053         { H4_RECV_ACL,    .recv = hci_recv_frame   },
1054         { H4_RECV_SCO,    .recv = hci_recv_frame   },
1055         { H4_RECV_EVENT,  .recv = intel_recv_event },
1056         { INTEL_RECV_LPM, .recv = intel_recv_lpm   },
1057 };
1058
1059 static int intel_recv(struct hci_uart *hu, const void *data, int count)
1060 {
1061         struct intel_data *intel = hu->priv;
1062
1063         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
1064                 return -EUNATCH;
1065
1066         intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
1067                                     intel_recv_pkts,
1068                                     ARRAY_SIZE(intel_recv_pkts));
1069         if (IS_ERR(intel->rx_skb)) {
1070                 int err = PTR_ERR(intel->rx_skb);
1071                 bt_dev_err(hu->hdev, "Frame reassembly failed (%d)", err);
1072                 intel->rx_skb = NULL;
1073                 return err;
1074         }
1075
1076         return count;
1077 }
1078
1079 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
1080 {
1081         struct intel_data *intel = hu->priv;
1082         struct list_head *p;
1083
1084         BT_DBG("hu %p skb %p", hu, skb);
1085
1086         /* Be sure our controller is resumed and potential LPM transaction
1087          * completed before enqueuing any packet.
1088          */
1089         mutex_lock(&intel_device_list_lock);
1090         list_for_each(p, &intel_device_list) {
1091                 struct intel_device *idev = list_entry(p, struct intel_device,
1092                                                        list);
1093
1094                 if (hu->tty->dev->parent == idev->pdev->dev.parent) {
1095                         pm_runtime_get_sync(&idev->pdev->dev);
1096                         pm_runtime_mark_last_busy(&idev->pdev->dev);
1097                         pm_runtime_put_autosuspend(&idev->pdev->dev);
1098                         break;
1099                 }
1100         }
1101         mutex_unlock(&intel_device_list_lock);
1102
1103         skb_queue_tail(&intel->txq, skb);
1104
1105         return 0;
1106 }
1107
1108 static struct sk_buff *intel_dequeue(struct hci_uart *hu)
1109 {
1110         struct intel_data *intel = hu->priv;
1111         struct sk_buff *skb;
1112
1113         skb = skb_dequeue(&intel->txq);
1114         if (!skb)
1115                 return skb;
1116
1117         if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
1118             (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
1119                 struct hci_command_hdr *cmd = (void *)skb->data;
1120                 __u16 opcode = le16_to_cpu(cmd->opcode);
1121
1122                 /* When the 0xfc01 command is issued to boot into
1123                  * the operational firmware, it will actually not
1124                  * send a command complete event. To keep the flow
1125                  * control working inject that event here.
1126                  */
1127                 if (opcode == 0xfc01)
1128                         inject_cmd_complete(hu->hdev, opcode);
1129         }
1130
1131         /* Prepend skb with frame type */
1132         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
1133
1134         return skb;
1135 }
1136
1137 static const struct hci_uart_proto intel_proto = {
1138         .id             = HCI_UART_INTEL,
1139         .name           = "Intel",
1140         .init_speed     = 115200,
1141         .oper_speed     = 3000000,
1142         .open           = intel_open,
1143         .close          = intel_close,
1144         .flush          = intel_flush,
1145         .setup          = intel_setup,
1146         .set_baudrate   = intel_set_baudrate,
1147         .recv           = intel_recv,
1148         .enqueue        = intel_enqueue,
1149         .dequeue        = intel_dequeue,
1150 };
1151
1152 #ifdef CONFIG_ACPI
1153 static const struct acpi_device_id intel_acpi_match[] = {
1154         { "INT33E1", 0 },
1155         { },
1156 };
1157 MODULE_DEVICE_TABLE(acpi, intel_acpi_match);
1158
1159 static int intel_acpi_probe(struct intel_device *idev)
1160 {
1161         const struct acpi_device_id *id;
1162
1163         id = acpi_match_device(intel_acpi_match, &idev->pdev->dev);
1164         if (!id)
1165                 return -ENODEV;
1166
1167         return 0;
1168 }
1169 #else
1170 static int intel_acpi_probe(struct intel_device *idev)
1171 {
1172         return -ENODEV;
1173 }
1174 #endif
1175
1176 #ifdef CONFIG_PM
1177 static int intel_suspend(struct device *dev)
1178 {
1179         struct intel_device *idev = dev_get_drvdata(dev);
1180
1181         dev_dbg(dev, "intel_suspend");
1182
1183         mutex_lock(&idev->hu_lock);
1184         if (idev->hu)
1185                 intel_lpm_suspend(idev->hu);
1186         mutex_unlock(&idev->hu_lock);
1187
1188         return 0;
1189 }
1190
1191 static int intel_resume(struct device *dev)
1192 {
1193         struct intel_device *idev = dev_get_drvdata(dev);
1194
1195         dev_dbg(dev, "intel_resume");
1196
1197         mutex_lock(&idev->hu_lock);
1198         if (idev->hu)
1199                 intel_lpm_resume(idev->hu);
1200         mutex_unlock(&idev->hu_lock);
1201
1202         return 0;
1203 }
1204 #endif
1205
1206 static const struct dev_pm_ops intel_pm_ops = {
1207         SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
1208         SET_RUNTIME_PM_OPS(intel_suspend, intel_resume, NULL)
1209 };
1210
1211 static int intel_probe(struct platform_device *pdev)
1212 {
1213         struct intel_device *idev;
1214
1215         idev = devm_kzalloc(&pdev->dev, sizeof(*idev), GFP_KERNEL);
1216         if (!idev)
1217                 return -ENOMEM;
1218
1219         mutex_init(&idev->hu_lock);
1220
1221         idev->pdev = pdev;
1222
1223         if (ACPI_HANDLE(&pdev->dev)) {
1224                 int err = intel_acpi_probe(idev);
1225                 if (err)
1226                         return err;
1227         } else {
1228                 return -ENODEV;
1229         }
1230
1231         idev->reset = devm_gpiod_get_optional(&pdev->dev, "reset",
1232                                               GPIOD_OUT_LOW);
1233         if (IS_ERR(idev->reset)) {
1234                 dev_err(&pdev->dev, "Unable to retrieve gpio\n");
1235                 return PTR_ERR(idev->reset);
1236         }
1237
1238         idev->irq = platform_get_irq(pdev, 0);
1239         if (idev->irq < 0) {
1240                 struct gpio_desc *host_wake;
1241
1242                 dev_err(&pdev->dev, "No IRQ, falling back to gpio-irq\n");
1243
1244                 host_wake = devm_gpiod_get_optional(&pdev->dev, "host-wake",
1245                                                     GPIOD_IN);
1246                 if (IS_ERR(host_wake)) {
1247                         dev_err(&pdev->dev, "Unable to retrieve IRQ\n");
1248                         goto no_irq;
1249                 }
1250
1251                 idev->irq = gpiod_to_irq(host_wake);
1252                 if (idev->irq < 0) {
1253                         dev_err(&pdev->dev, "No corresponding irq for gpio\n");
1254                         goto no_irq;
1255                 }
1256         }
1257
1258         /* Only enable wake-up/irq when controller is powered */
1259         device_set_wakeup_capable(&pdev->dev, true);
1260         device_wakeup_disable(&pdev->dev);
1261
1262 no_irq:
1263         platform_set_drvdata(pdev, idev);
1264
1265         /* Place this instance on the device list */
1266         mutex_lock(&intel_device_list_lock);
1267         list_add_tail(&idev->list, &intel_device_list);
1268         mutex_unlock(&intel_device_list_lock);
1269
1270         dev_info(&pdev->dev, "registered, gpio(%d)/irq(%d).\n",
1271                  desc_to_gpio(idev->reset), idev->irq);
1272
1273         return 0;
1274 }
1275
1276 static int intel_remove(struct platform_device *pdev)
1277 {
1278         struct intel_device *idev = platform_get_drvdata(pdev);
1279
1280         device_wakeup_disable(&pdev->dev);
1281
1282         mutex_lock(&intel_device_list_lock);
1283         list_del(&idev->list);
1284         mutex_unlock(&intel_device_list_lock);
1285
1286         dev_info(&pdev->dev, "unregistered.\n");
1287
1288         return 0;
1289 }
1290
1291 static struct platform_driver intel_driver = {
1292         .probe = intel_probe,
1293         .remove = intel_remove,
1294         .driver = {
1295                 .name = "hci_intel",
1296                 .acpi_match_table = ACPI_PTR(intel_acpi_match),
1297                 .pm = &intel_pm_ops,
1298         },
1299 };
1300
1301 int __init intel_init(void)
1302 {
1303         platform_driver_register(&intel_driver);
1304
1305         return hci_uart_register_proto(&intel_proto);
1306 }
1307
1308 int __exit intel_deinit(void)
1309 {
1310         platform_driver_unregister(&intel_driver);
1311
1312         return hci_uart_unregister_proto(&intel_proto);
1313 }