Bluetooth: hci_uart: Add Intel address configuration support
[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/wait.h>
29
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32
33 #include "hci_uart.h"
34 #include "btintel.h"
35
36 #define STATE_BOOTLOADER        0
37 #define STATE_DOWNLOADING       1
38 #define STATE_FIRMWARE_LOADED   2
39 #define STATE_FIRMWARE_FAILED   3
40 #define STATE_BOOTING           4
41
42 struct intel_data {
43         struct sk_buff *rx_skb;
44         struct sk_buff_head txq;
45         unsigned long flags;
46 };
47
48 static int intel_open(struct hci_uart *hu)
49 {
50         struct intel_data *intel;
51
52         BT_DBG("hu %p", hu);
53
54         intel = kzalloc(sizeof(*intel), GFP_KERNEL);
55         if (!intel)
56                 return -ENOMEM;
57
58         skb_queue_head_init(&intel->txq);
59
60         hu->priv = intel;
61         return 0;
62 }
63
64 static int intel_close(struct hci_uart *hu)
65 {
66         struct intel_data *intel = hu->priv;
67
68         BT_DBG("hu %p", hu);
69
70         skb_queue_purge(&intel->txq);
71         kfree_skb(intel->rx_skb);
72         kfree(intel);
73
74         hu->priv = NULL;
75         return 0;
76 }
77
78 static int intel_flush(struct hci_uart *hu)
79 {
80         struct intel_data *intel = hu->priv;
81
82         BT_DBG("hu %p", hu);
83
84         skb_queue_purge(&intel->txq);
85
86         return 0;
87 }
88
89 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
90 {
91         struct sk_buff *skb;
92         struct hci_event_hdr *hdr;
93         struct hci_ev_cmd_complete *evt;
94
95         skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
96         if (!skb)
97                 return -ENOMEM;
98
99         hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
100         hdr->evt = HCI_EV_CMD_COMPLETE;
101         hdr->plen = sizeof(*evt) + 1;
102
103         evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
104         evt->ncmd = 0x01;
105         evt->opcode = cpu_to_le16(opcode);
106
107         *skb_put(skb, 1) = 0x00;
108
109         bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
110
111         return hci_recv_frame(hdev, skb);
112 }
113
114 static int intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
115                              u32 plen, const void *param)
116 {
117         while (plen > 0) {
118                 struct sk_buff *skb;
119                 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
120
121                 cmd_param[0] = fragment_type;
122                 memcpy(cmd_param + 1, param, fragment_len);
123
124                 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
125                                      cmd_param, HCI_INIT_TIMEOUT);
126                 if (IS_ERR(skb))
127                         return PTR_ERR(skb);
128
129                 kfree_skb(skb);
130
131                 plen -= fragment_len;
132                 param += fragment_len;
133         }
134
135         return 0;
136 }
137
138 static void intel_version_info(struct hci_dev *hdev,
139                                struct intel_version *ver)
140 {
141         const char *variant;
142
143         switch (ver->fw_variant) {
144         case 0x06:
145                 variant = "Bootloader";
146                 break;
147         case 0x23:
148                 variant = "Firmware";
149                 break;
150         default:
151                 return;
152         }
153
154         BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
155                 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
156                 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
157 }
158
159 static int intel_setup(struct hci_uart *hu)
160 {
161         static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
162                                           0x00, 0x08, 0x04, 0x00 };
163         struct intel_data *intel = hu->priv;
164         struct hci_dev *hdev = hu->hdev;
165         struct sk_buff *skb;
166         struct intel_version *ver;
167         struct intel_boot_params *params;
168         const struct firmware *fw;
169         const u8 *fw_ptr;
170         char fwname[64];
171         u32 frag_len;
172         ktime_t calltime, delta, rettime;
173         unsigned long long duration;
174         int err;
175
176         BT_DBG("%s", hdev->name);
177
178         hu->hdev->set_bdaddr = btintel_set_bdaddr;
179
180         calltime = ktime_get();
181
182         set_bit(STATE_BOOTLOADER, &intel->flags);
183
184         /* Read the Intel version information to determine if the device
185          * is in bootloader mode or if it already has operational firmware
186          * loaded.
187          */
188         skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
189         if (IS_ERR(skb)) {
190                 BT_ERR("%s: Reading Intel version information failed (%ld)",
191                        hdev->name, PTR_ERR(skb));
192                 return PTR_ERR(skb);
193         }
194
195         if (skb->len != sizeof(*ver)) {
196                 BT_ERR("%s: Intel version event size mismatch", hdev->name);
197                 kfree_skb(skb);
198                 return -EILSEQ;
199         }
200
201         ver = (struct intel_version *)skb->data;
202         if (ver->status) {
203                 BT_ERR("%s: Intel version command failure (%02x)",
204                        hdev->name, ver->status);
205                 err = -bt_to_errno(ver->status);
206                 kfree_skb(skb);
207                 return err;
208         }
209
210         /* The hardware platform number has a fixed value of 0x37 and
211          * for now only accept this single value.
212          */
213         if (ver->hw_platform != 0x37) {
214                 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
215                        hdev->name, ver->hw_platform);
216                 kfree_skb(skb);
217                 return -EINVAL;
218         }
219
220         /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
221          * supported by this firmware loading method. This check has been
222          * put in place to ensure correct forward compatibility options
223          * when newer hardware variants come along.
224          */
225         if (ver->hw_variant != 0x0b) {
226                 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
227                        hdev->name, ver->hw_variant);
228                 kfree_skb(skb);
229                 return -EINVAL;
230         }
231
232         intel_version_info(hdev, ver);
233
234         /* The firmware variant determines if the device is in bootloader
235          * mode or is running operational firmware. The value 0x06 identifies
236          * the bootloader and the value 0x23 identifies the operational
237          * firmware.
238          *
239          * When the operational firmware is already present, then only
240          * the check for valid Bluetooth device address is needed. This
241          * determines if the device will be added as configured or
242          * unconfigured controller.
243          *
244          * It is not possible to use the Secure Boot Parameters in this
245          * case since that command is only available in bootloader mode.
246          */
247         if (ver->fw_variant == 0x23) {
248                 kfree_skb(skb);
249                 clear_bit(STATE_BOOTLOADER, &intel->flags);
250                 btintel_check_bdaddr(hdev);
251                 return 0;
252         }
253
254         /* If the device is not in bootloader mode, then the only possible
255          * choice is to return an error and abort the device initialization.
256          */
257         if (ver->fw_variant != 0x06) {
258                 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
259                        hdev->name, ver->fw_variant);
260                 kfree_skb(skb);
261                 return -ENODEV;
262         }
263
264         kfree_skb(skb);
265
266         /* Read the secure boot parameters to identify the operating
267          * details of the bootloader.
268          */
269         skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
270         if (IS_ERR(skb)) {
271                 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
272                        hdev->name, PTR_ERR(skb));
273                 return PTR_ERR(skb);
274         }
275
276         if (skb->len != sizeof(*params)) {
277                 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
278                 kfree_skb(skb);
279                 return -EILSEQ;
280         }
281
282         params = (struct intel_boot_params *)skb->data;
283         if (params->status) {
284                 BT_ERR("%s: Intel boot parameters command failure (%02x)",
285                        hdev->name, params->status);
286                 err = -bt_to_errno(params->status);
287                 kfree_skb(skb);
288                 return err;
289         }
290
291         BT_INFO("%s: Device revision is %u", hdev->name,
292                 le16_to_cpu(params->dev_revid));
293
294         BT_INFO("%s: Secure boot is %s", hdev->name,
295                 params->secure_boot ? "enabled" : "disabled");
296
297         BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
298                 params->min_fw_build_nn, params->min_fw_build_cw,
299                 2000 + params->min_fw_build_yy);
300
301         /* It is required that every single firmware fragment is acknowledged
302          * with a command complete event. If the boot parameters indicate
303          * that this bootloader does not send them, then abort the setup.
304          */
305         if (params->limited_cce != 0x00) {
306                 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
307                        hdev->name, params->limited_cce);
308                 kfree_skb(skb);
309                 return -EINVAL;
310         }
311
312         /* If the OTP has no valid Bluetooth device address, then there will
313          * also be no valid address for the operational firmware.
314          */
315         if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
316                 BT_INFO("%s: No device address configured", hdev->name);
317                 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
318         }
319
320         /* With this Intel bootloader only the hardware variant and device
321          * revision information are used to select the right firmware.
322          *
323          * Currently this bootloader support is limited to hardware variant
324          * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
325          */
326         snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
327                  le16_to_cpu(params->dev_revid));
328
329         err = request_firmware(&fw, fwname, &hdev->dev);
330         if (err < 0) {
331                 BT_ERR("%s: Failed to load Intel firmware file (%d)",
332                        hdev->name, err);
333                 kfree_skb(skb);
334                 return err;
335         }
336
337         BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
338
339         kfree_skb(skb);
340
341         if (fw->size < 644) {
342                 BT_ERR("%s: Invalid size of firmware file (%zu)",
343                        hdev->name, fw->size);
344                 err = -EBADF;
345                 goto done;
346         }
347
348         set_bit(STATE_DOWNLOADING, &intel->flags);
349
350         /* Start the firmware download transaction with the Init fragment
351          * represented by the 128 bytes of CSS header.
352          */
353         err = intel_secure_send(hdev, 0x00, 128, fw->data);
354         if (err < 0) {
355                 BT_ERR("%s: Failed to send firmware header (%d)",
356                        hdev->name, err);
357                 goto done;
358         }
359
360         /* Send the 256 bytes of public key information from the firmware
361          * as the PKey fragment.
362          */
363         err = intel_secure_send(hdev, 0x03, 256, fw->data + 128);
364         if (err < 0) {
365                 BT_ERR("%s: Failed to send firmware public key (%d)",
366                        hdev->name, err);
367                 goto done;
368         }
369
370         /* Send the 256 bytes of signature information from the firmware
371          * as the Sign fragment.
372          */
373         err = intel_secure_send(hdev, 0x02, 256, fw->data + 388);
374         if (err < 0) {
375                 BT_ERR("%s: Failed to send firmware signature (%d)",
376                        hdev->name, err);
377                 goto done;
378         }
379
380         fw_ptr = fw->data + 644;
381         frag_len = 0;
382
383         while (fw_ptr - fw->data < fw->size) {
384                 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
385
386                 frag_len += sizeof(*cmd) + cmd->plen;
387
388                 BT_DBG("%s: patching %td/%zu", hdev->name,
389                        (fw_ptr - fw->data), fw->size);
390
391                 /* The parameter length of the secure send command requires
392                  * a 4 byte alignment. It happens so that the firmware file
393                  * contains proper Intel_NOP commands to align the fragments
394                  * as needed.
395                  *
396                  * Send set of commands with 4 byte alignment from the
397                  * firmware data buffer as a single Data fragement.
398                  */
399                 if (frag_len % 4)
400                         continue;
401
402                 /* Send each command from the firmware data buffer as
403                  * a single Data fragment.
404                  */
405                 err = intel_secure_send(hdev, 0x01, frag_len, fw_ptr);
406                 if (err < 0) {
407                         BT_ERR("%s: Failed to send firmware data (%d)",
408                                hdev->name, err);
409                         goto done;
410                 }
411
412                 fw_ptr += frag_len;
413                 frag_len = 0;
414         }
415
416         set_bit(STATE_FIRMWARE_LOADED, &intel->flags);
417
418         BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
419
420         /* Before switching the device into operational mode and with that
421          * booting the loaded firmware, wait for the bootloader notification
422          * that all fragments have been successfully received.
423          *
424          * When the event processing receives the notification, then the
425          * STATE_DOWNLOADING flag will be cleared.
426          *
427          * The firmware loading should not take longer than 5 seconds
428          * and thus just timeout if that happens and fail the setup
429          * of this device.
430          */
431         err = wait_on_bit_timeout(&intel->flags, STATE_DOWNLOADING,
432                                   TASK_INTERRUPTIBLE,
433                                   msecs_to_jiffies(5000));
434         if (err == 1) {
435                 BT_ERR("%s: Firmware loading interrupted", hdev->name);
436                 err = -EINTR;
437                 goto done;
438         }
439
440         if (err) {
441                 BT_ERR("%s: Firmware loading timeout", hdev->name);
442                 err = -ETIMEDOUT;
443                 goto done;
444         }
445
446         if (test_bit(STATE_FIRMWARE_FAILED, &intel->flags)) {
447                 BT_ERR("%s: Firmware loading failed", hdev->name);
448                 err = -ENOEXEC;
449                 goto done;
450         }
451
452         rettime = ktime_get();
453         delta = ktime_sub(rettime, calltime);
454         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
455
456         BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
457
458 done:
459         release_firmware(fw);
460
461         if (err < 0)
462                 return err;
463
464         calltime = ktime_get();
465
466         set_bit(STATE_BOOTING, &intel->flags);
467
468         skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
469                              HCI_INIT_TIMEOUT);
470         if (IS_ERR(skb))
471                 return PTR_ERR(skb);
472
473         kfree_skb(skb);
474
475         /* The bootloader will not indicate when the device is ready. This
476          * is done by the operational firmware sending bootup notification.
477          *
478          * Booting into operational firmware should not take longer than
479          * 1 second. However if that happens, then just fail the setup
480          * since something went wrong.
481          */
482         BT_INFO("%s: Waiting for device to boot", hdev->name);
483
484         err = wait_on_bit_timeout(&intel->flags, STATE_BOOTING,
485                                   TASK_INTERRUPTIBLE,
486                                   msecs_to_jiffies(1000));
487
488         if (err == 1) {
489                 BT_ERR("%s: Device boot interrupted", hdev->name);
490                 return -EINTR;
491         }
492
493         if (err) {
494                 BT_ERR("%s: Device boot timeout", hdev->name);
495                 return -ETIMEDOUT;
496         }
497
498         rettime = ktime_get();
499         delta = ktime_sub(rettime, calltime);
500         duration = (unsigned long long) ktime_to_ns(delta) >> 10;
501
502         BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
503
504         clear_bit(STATE_BOOTLOADER, &intel->flags);
505
506         return 0;
507 }
508
509 static int intel_recv_event(struct hci_dev *hdev, struct sk_buff *skb)
510 {
511         struct hci_uart *hu = hci_get_drvdata(hdev);
512         struct intel_data *intel = hu->priv;
513         struct hci_event_hdr *hdr;
514
515         if (!test_bit(STATE_BOOTLOADER, &intel->flags))
516                 goto recv;
517
518         hdr = (void *)skb->data;
519
520         /* When the firmware loading completes the device sends
521          * out a vendor specific event indicating the result of
522          * the firmware loading.
523          */
524         if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
525             skb->data[2] == 0x06) {
526                 if (skb->data[3] != 0x00)
527                         set_bit(STATE_FIRMWARE_FAILED, &intel->flags);
528
529                 if (test_and_clear_bit(STATE_DOWNLOADING, &intel->flags) &&
530                     test_bit(STATE_FIRMWARE_LOADED, &intel->flags)) {
531                         smp_mb__after_atomic();
532                         wake_up_bit(&intel->flags, STATE_DOWNLOADING);
533                 }
534
535         /* When switching to the operational firmware the device
536          * sends a vendor specific event indicating that the bootup
537          * completed.
538          */
539         } else if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
540                    skb->data[2] == 0x02) {
541                 if (test_and_clear_bit(STATE_BOOTING, &intel->flags)) {
542                         smp_mb__after_atomic();
543                         wake_up_bit(&intel->flags, STATE_BOOTING);
544                 }
545         }
546 recv:
547         return hci_recv_frame(hdev, skb);
548 }
549
550 static const struct h4_recv_pkt intel_recv_pkts[] = {
551         { H4_RECV_ACL,   .recv = hci_recv_frame },
552         { H4_RECV_SCO,   .recv = hci_recv_frame },
553         { H4_RECV_EVENT, .recv = intel_recv_event },
554 };
555
556 static int intel_recv(struct hci_uart *hu, const void *data, int count)
557 {
558         struct intel_data *intel = hu->priv;
559
560         if (!test_bit(HCI_UART_REGISTERED, &hu->flags))
561                 return -EUNATCH;
562
563         intel->rx_skb = h4_recv_buf(hu->hdev, intel->rx_skb, data, count,
564                                     intel_recv_pkts,
565                                     ARRAY_SIZE(intel_recv_pkts));
566         if (IS_ERR(intel->rx_skb)) {
567                 int err = PTR_ERR(intel->rx_skb);
568                 BT_ERR("%s: Frame reassembly failed (%d)", hu->hdev->name, err);
569                 intel->rx_skb = NULL;
570                 return err;
571         }
572
573         return count;
574 }
575
576 static int intel_enqueue(struct hci_uart *hu, struct sk_buff *skb)
577 {
578         struct intel_data *intel = hu->priv;
579
580         BT_DBG("hu %p skb %p", hu, skb);
581
582         skb_queue_tail(&intel->txq, skb);
583
584         return 0;
585 }
586
587 static struct sk_buff *intel_dequeue(struct hci_uart *hu)
588 {
589         struct intel_data *intel = hu->priv;
590         struct sk_buff *skb;
591
592         skb = skb_dequeue(&intel->txq);
593         if (!skb)
594                 return skb;
595
596         if (test_bit(STATE_BOOTLOADER, &intel->flags) &&
597             (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT)) {
598                 struct hci_command_hdr *cmd = (void *)skb->data;
599                 __u16 opcode = le16_to_cpu(cmd->opcode);
600
601                 /* When the 0xfc01 command is issued to boot into
602                  * the operational firmware, it will actually not
603                  * send a command complete event. To keep the flow
604                  * control working inject that event here.
605                  */
606                 if (opcode == 0xfc01)
607                         inject_cmd_complete(hu->hdev, opcode);
608         }
609
610         /* Prepend skb with frame type */
611         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
612
613         return skb;
614 }
615
616 static const struct hci_uart_proto intel_proto = {
617         .id             = HCI_UART_INTEL,
618         .name           = "Intel",
619         .init_speed     = 115200,
620         .open           = intel_open,
621         .close          = intel_close,
622         .flush          = intel_flush,
623         .setup          = intel_setup,
624         .recv           = intel_recv,
625         .enqueue        = intel_enqueue,
626         .dequeue        = intel_dequeue,
627 };
628
629 int __init intel_init(void)
630 {
631         return hci_uart_register_proto(&intel_proto);
632 }
633
634 int __exit intel_deinit(void)
635 {
636         return hci_uart_unregister_proto(&intel_proto);
637 }