2fac6babb4b18aea1b1b400d201d7fde75ea9b19
[linux-2.6-microblaze.git] / drivers / bluetooth / btintel.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *  Bluetooth support for Intel devices
5  *
6  *  Copyright (C) 2015  Intel Corporation
7  */
8
9 #include <linux/module.h>
10 #include <linux/firmware.h>
11 #include <linux/regmap.h>
12 #include <asm/unaligned.h>
13
14 #include <net/bluetooth/bluetooth.h>
15 #include <net/bluetooth/hci_core.h>
16
17 #include "btintel.h"
18
19 #define VERSION "0.1"
20
21 #define BDADDR_INTEL            (&(bdaddr_t){{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
22 #define RSA_HEADER_LEN          644
23 #define CSS_HEADER_OFFSET       8
24 #define ECDSA_OFFSET            644
25 #define ECDSA_HEADER_LEN        320
26
27 #define CMD_WRITE_BOOT_PARAMS   0xfc0e
28 struct cmd_write_boot_params {
29         u32 boot_addr;
30         u8  fw_build_num;
31         u8  fw_build_ww;
32         u8  fw_build_yy;
33 } __packed;
34
35 int btintel_check_bdaddr(struct hci_dev *hdev)
36 {
37         struct hci_rp_read_bd_addr *bda;
38         struct sk_buff *skb;
39
40         skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
41                              HCI_INIT_TIMEOUT);
42         if (IS_ERR(skb)) {
43                 int err = PTR_ERR(skb);
44                 bt_dev_err(hdev, "Reading Intel device address failed (%d)",
45                            err);
46                 return err;
47         }
48
49         if (skb->len != sizeof(*bda)) {
50                 bt_dev_err(hdev, "Intel device address length mismatch");
51                 kfree_skb(skb);
52                 return -EIO;
53         }
54
55         bda = (struct hci_rp_read_bd_addr *)skb->data;
56
57         /* For some Intel based controllers, the default Bluetooth device
58          * address 00:03:19:9E:8B:00 can be found. These controllers are
59          * fully operational, but have the danger of duplicate addresses
60          * and that in turn can cause problems with Bluetooth operation.
61          */
62         if (!bacmp(&bda->bdaddr, BDADDR_INTEL)) {
63                 bt_dev_err(hdev, "Found Intel default device address (%pMR)",
64                            &bda->bdaddr);
65                 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
66         }
67
68         kfree_skb(skb);
69
70         return 0;
71 }
72 EXPORT_SYMBOL_GPL(btintel_check_bdaddr);
73
74 int btintel_enter_mfg(struct hci_dev *hdev)
75 {
76         static const u8 param[] = { 0x01, 0x00 };
77         struct sk_buff *skb;
78
79         skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
80         if (IS_ERR(skb)) {
81                 bt_dev_err(hdev, "Entering manufacturer mode failed (%ld)",
82                            PTR_ERR(skb));
83                 return PTR_ERR(skb);
84         }
85         kfree_skb(skb);
86
87         return 0;
88 }
89 EXPORT_SYMBOL_GPL(btintel_enter_mfg);
90
91 int btintel_exit_mfg(struct hci_dev *hdev, bool reset, bool patched)
92 {
93         u8 param[] = { 0x00, 0x00 };
94         struct sk_buff *skb;
95
96         /* The 2nd command parameter specifies the manufacturing exit method:
97          * 0x00: Just disable the manufacturing mode (0x00).
98          * 0x01: Disable manufacturing mode and reset with patches deactivated.
99          * 0x02: Disable manufacturing mode and reset with patches activated.
100          */
101         if (reset)
102                 param[1] |= patched ? 0x02 : 0x01;
103
104         skb = __hci_cmd_sync(hdev, 0xfc11, 2, param, HCI_CMD_TIMEOUT);
105         if (IS_ERR(skb)) {
106                 bt_dev_err(hdev, "Exiting manufacturer mode failed (%ld)",
107                            PTR_ERR(skb));
108                 return PTR_ERR(skb);
109         }
110         kfree_skb(skb);
111
112         return 0;
113 }
114 EXPORT_SYMBOL_GPL(btintel_exit_mfg);
115
116 int btintel_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr)
117 {
118         struct sk_buff *skb;
119         int err;
120
121         skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
122         if (IS_ERR(skb)) {
123                 err = PTR_ERR(skb);
124                 bt_dev_err(hdev, "Changing Intel device address failed (%d)",
125                            err);
126                 return err;
127         }
128         kfree_skb(skb);
129
130         return 0;
131 }
132 EXPORT_SYMBOL_GPL(btintel_set_bdaddr);
133
134 int btintel_set_diag(struct hci_dev *hdev, bool enable)
135 {
136         struct sk_buff *skb;
137         u8 param[3];
138         int err;
139
140         if (enable) {
141                 param[0] = 0x03;
142                 param[1] = 0x03;
143                 param[2] = 0x03;
144         } else {
145                 param[0] = 0x00;
146                 param[1] = 0x00;
147                 param[2] = 0x00;
148         }
149
150         skb = __hci_cmd_sync(hdev, 0xfc43, 3, param, HCI_INIT_TIMEOUT);
151         if (IS_ERR(skb)) {
152                 err = PTR_ERR(skb);
153                 if (err == -ENODATA)
154                         goto done;
155                 bt_dev_err(hdev, "Changing Intel diagnostic mode failed (%d)",
156                            err);
157                 return err;
158         }
159         kfree_skb(skb);
160
161 done:
162         btintel_set_event_mask(hdev, enable);
163         return 0;
164 }
165 EXPORT_SYMBOL_GPL(btintel_set_diag);
166
167 int btintel_set_diag_mfg(struct hci_dev *hdev, bool enable)
168 {
169         int err, ret;
170
171         err = btintel_enter_mfg(hdev);
172         if (err)
173                 return err;
174
175         ret = btintel_set_diag(hdev, enable);
176
177         err = btintel_exit_mfg(hdev, false, false);
178         if (err)
179                 return err;
180
181         return ret;
182 }
183 EXPORT_SYMBOL_GPL(btintel_set_diag_mfg);
184
185 void btintel_hw_error(struct hci_dev *hdev, u8 code)
186 {
187         struct sk_buff *skb;
188         u8 type = 0x00;
189
190         bt_dev_err(hdev, "Hardware error 0x%2.2x", code);
191
192         skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
193         if (IS_ERR(skb)) {
194                 bt_dev_err(hdev, "Reset after hardware error failed (%ld)",
195                            PTR_ERR(skb));
196                 return;
197         }
198         kfree_skb(skb);
199
200         skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
201         if (IS_ERR(skb)) {
202                 bt_dev_err(hdev, "Retrieving Intel exception info failed (%ld)",
203                            PTR_ERR(skb));
204                 return;
205         }
206
207         if (skb->len != 13) {
208                 bt_dev_err(hdev, "Exception info size mismatch");
209                 kfree_skb(skb);
210                 return;
211         }
212
213         bt_dev_err(hdev, "Exception info %s", (char *)(skb->data + 1));
214
215         kfree_skb(skb);
216 }
217 EXPORT_SYMBOL_GPL(btintel_hw_error);
218
219 int btintel_version_info(struct hci_dev *hdev, struct intel_version *ver)
220 {
221         const char *variant;
222
223         /* The hardware platform number has a fixed value of 0x37 and
224          * for now only accept this single value.
225          */
226         if (ver->hw_platform != 0x37) {
227                 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
228                            ver->hw_platform);
229                 return -EINVAL;
230         }
231
232         /* Check for supported iBT hardware variants of this firmware
233          * loading method.
234          *
235          * This check has been put in place to ensure correct forward
236          * compatibility options when newer hardware variants come along.
237          */
238         switch (ver->hw_variant) {
239         case 0x07:      /* WP - Legacy ROM */
240         case 0x08:      /* StP - Legacy ROM */
241         case 0x0b:      /* SfP */
242         case 0x0c:      /* WsP */
243         case 0x11:      /* JfP */
244         case 0x12:      /* ThP */
245         case 0x13:      /* HrP */
246         case 0x14:      /* CcP */
247                 break;
248         default:
249                 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
250                            ver->hw_variant);
251                 return -EINVAL;
252         }
253
254         switch (ver->fw_variant) {
255         case 0x01:
256                 variant = "Legacy ROM 2.5";
257                 break;
258         case 0x06:
259                 variant = "Bootloader";
260                 break;
261         case 0x22:
262                 variant = "Legacy ROM 2.x";
263                 break;
264         case 0x23:
265                 variant = "Firmware";
266                 break;
267         default:
268                 bt_dev_err(hdev, "Unsupported firmware variant(%02x)", ver->fw_variant);
269                 return -EINVAL;
270         }
271
272         bt_dev_info(hdev, "%s revision %u.%u build %u week %u %u",
273                     variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
274                     ver->fw_build_num, ver->fw_build_ww,
275                     2000 + ver->fw_build_yy);
276
277         return 0;
278 }
279 EXPORT_SYMBOL_GPL(btintel_version_info);
280
281 int btintel_secure_send(struct hci_dev *hdev, u8 fragment_type, u32 plen,
282                         const void *param)
283 {
284         while (plen > 0) {
285                 struct sk_buff *skb;
286                 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
287
288                 cmd_param[0] = fragment_type;
289                 memcpy(cmd_param + 1, param, fragment_len);
290
291                 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
292                                      cmd_param, HCI_INIT_TIMEOUT);
293                 if (IS_ERR(skb))
294                         return PTR_ERR(skb);
295
296                 kfree_skb(skb);
297
298                 plen -= fragment_len;
299                 param += fragment_len;
300         }
301
302         return 0;
303 }
304 EXPORT_SYMBOL_GPL(btintel_secure_send);
305
306 int btintel_load_ddc_config(struct hci_dev *hdev, const char *ddc_name)
307 {
308         const struct firmware *fw;
309         struct sk_buff *skb;
310         const u8 *fw_ptr;
311         int err;
312
313         err = request_firmware_direct(&fw, ddc_name, &hdev->dev);
314         if (err < 0) {
315                 bt_dev_err(hdev, "Failed to load Intel DDC file %s (%d)",
316                            ddc_name, err);
317                 return err;
318         }
319
320         bt_dev_info(hdev, "Found Intel DDC parameters: %s", ddc_name);
321
322         fw_ptr = fw->data;
323
324         /* DDC file contains one or more DDC structure which has
325          * Length (1 byte), DDC ID (2 bytes), and DDC value (Length - 2).
326          */
327         while (fw->size > fw_ptr - fw->data) {
328                 u8 cmd_plen = fw_ptr[0] + sizeof(u8);
329
330                 skb = __hci_cmd_sync(hdev, 0xfc8b, cmd_plen, fw_ptr,
331                                      HCI_INIT_TIMEOUT);
332                 if (IS_ERR(skb)) {
333                         bt_dev_err(hdev, "Failed to send Intel_Write_DDC (%ld)",
334                                    PTR_ERR(skb));
335                         release_firmware(fw);
336                         return PTR_ERR(skb);
337                 }
338
339                 fw_ptr += cmd_plen;
340                 kfree_skb(skb);
341         }
342
343         release_firmware(fw);
344
345         bt_dev_info(hdev, "Applying Intel DDC parameters completed");
346
347         return 0;
348 }
349 EXPORT_SYMBOL_GPL(btintel_load_ddc_config);
350
351 int btintel_set_event_mask(struct hci_dev *hdev, bool debug)
352 {
353         u8 mask[8] = { 0x87, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
354         struct sk_buff *skb;
355         int err;
356
357         if (debug)
358                 mask[1] |= 0x62;
359
360         skb = __hci_cmd_sync(hdev, 0xfc52, 8, mask, HCI_INIT_TIMEOUT);
361         if (IS_ERR(skb)) {
362                 err = PTR_ERR(skb);
363                 bt_dev_err(hdev, "Setting Intel event mask failed (%d)", err);
364                 return err;
365         }
366         kfree_skb(skb);
367
368         return 0;
369 }
370 EXPORT_SYMBOL_GPL(btintel_set_event_mask);
371
372 int btintel_set_event_mask_mfg(struct hci_dev *hdev, bool debug)
373 {
374         int err, ret;
375
376         err = btintel_enter_mfg(hdev);
377         if (err)
378                 return err;
379
380         ret = btintel_set_event_mask(hdev, debug);
381
382         err = btintel_exit_mfg(hdev, false, false);
383         if (err)
384                 return err;
385
386         return ret;
387 }
388 EXPORT_SYMBOL_GPL(btintel_set_event_mask_mfg);
389
390 int btintel_read_version(struct hci_dev *hdev, struct intel_version *ver)
391 {
392         struct sk_buff *skb;
393
394         skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_CMD_TIMEOUT);
395         if (IS_ERR(skb)) {
396                 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
397                            PTR_ERR(skb));
398                 return PTR_ERR(skb);
399         }
400
401         if (skb->len != sizeof(*ver)) {
402                 bt_dev_err(hdev, "Intel version event size mismatch");
403                 kfree_skb(skb);
404                 return -EILSEQ;
405         }
406
407         memcpy(ver, skb->data, sizeof(*ver));
408
409         kfree_skb(skb);
410
411         return 0;
412 }
413 EXPORT_SYMBOL_GPL(btintel_read_version);
414
415 int btintel_version_info_tlv(struct hci_dev *hdev, struct intel_version_tlv *version)
416 {
417         const char *variant;
418
419         /* The hardware platform number has a fixed value of 0x37 and
420          * for now only accept this single value.
421          */
422         if (INTEL_HW_PLATFORM(version->cnvi_bt) != 0x37) {
423                 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
424                            INTEL_HW_PLATFORM(version->cnvi_bt));
425                 return -EINVAL;
426         }
427
428         /* Check for supported iBT hardware variants of this firmware
429          * loading method.
430          *
431          * This check has been put in place to ensure correct forward
432          * compatibility options when newer hardware variants come along.
433          */
434         switch (INTEL_HW_VARIANT(version->cnvi_bt)) {
435         case 0x17:      /* TyP */
436         case 0x18:      /* Slr */
437         case 0x19:      /* Slr-F */
438                 break;
439         default:
440                 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
441                            INTEL_HW_VARIANT(version->cnvi_bt));
442                 return -EINVAL;
443         }
444
445         switch (version->img_type) {
446         case 0x01:
447                 variant = "Bootloader";
448                 /* It is required that every single firmware fragment is acknowledged
449                  * with a command complete event. If the boot parameters indicate
450                  * that this bootloader does not send them, then abort the setup.
451                  */
452                 if (version->limited_cce != 0x00) {
453                         bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
454                                    version->limited_cce);
455                         return -EINVAL;
456                 }
457
458                 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
459                 if (version->sbe_type > 0x01) {
460                         bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
461                                    version->sbe_type);
462                         return -EINVAL;
463                 }
464
465                 bt_dev_info(hdev, "Device revision is %u", version->dev_rev_id);
466                 bt_dev_info(hdev, "Secure boot is %s",
467                             version->secure_boot ? "enabled" : "disabled");
468                 bt_dev_info(hdev, "OTP lock is %s",
469                             version->otp_lock ? "enabled" : "disabled");
470                 bt_dev_info(hdev, "API lock is %s",
471                             version->api_lock ? "enabled" : "disabled");
472                 bt_dev_info(hdev, "Debug lock is %s",
473                             version->debug_lock ? "enabled" : "disabled");
474                 bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
475                             version->min_fw_build_nn, version->min_fw_build_cw,
476                             2000 + version->min_fw_build_yy);
477                 break;
478         case 0x03:
479                 variant = "Firmware";
480                 break;
481         default:
482                 bt_dev_err(hdev, "Unsupported image type(%02x)", version->img_type);
483                 return -EINVAL;
484         }
485
486         bt_dev_info(hdev, "%s timestamp %u.%u buildtype %u build %u", variant,
487                     2000 + (version->timestamp >> 8), version->timestamp & 0xff,
488                     version->build_type, version->build_num);
489
490         return 0;
491 }
492 EXPORT_SYMBOL_GPL(btintel_version_info_tlv);
493
494 static int btintel_parse_version_tlv(struct hci_dev *hdev,
495                                      struct intel_version_tlv *version,
496                                      struct sk_buff *skb)
497 {
498         /* Consume Command Complete Status field */
499         skb_pull(skb, 1);
500
501         /* Event parameters contatin multiple TLVs. Read each of them
502          * and only keep the required data. Also, it use existing legacy
503          * version field like hw_platform, hw_variant, and fw_variant
504          * to keep the existing setup flow
505          */
506         while (skb->len) {
507                 struct intel_tlv *tlv;
508
509                 /* Make sure skb has a minimum length of the header */
510                 if (skb->len < sizeof(*tlv))
511                         return -EINVAL;
512
513                 tlv = (struct intel_tlv *)skb->data;
514
515                 /* Make sure skb has a enough data */
516                 if (skb->len < tlv->len + sizeof(*tlv))
517                         return -EINVAL;
518
519                 switch (tlv->type) {
520                 case INTEL_TLV_CNVI_TOP:
521                         version->cnvi_top = get_unaligned_le32(tlv->val);
522                         break;
523                 case INTEL_TLV_CNVR_TOP:
524                         version->cnvr_top = get_unaligned_le32(tlv->val);
525                         break;
526                 case INTEL_TLV_CNVI_BT:
527                         version->cnvi_bt = get_unaligned_le32(tlv->val);
528                         break;
529                 case INTEL_TLV_CNVR_BT:
530                         version->cnvr_bt = get_unaligned_le32(tlv->val);
531                         break;
532                 case INTEL_TLV_DEV_REV_ID:
533                         version->dev_rev_id = get_unaligned_le16(tlv->val);
534                         break;
535                 case INTEL_TLV_IMAGE_TYPE:
536                         version->img_type = tlv->val[0];
537                         break;
538                 case INTEL_TLV_TIME_STAMP:
539                         /* If image type is Operational firmware (0x03), then
540                          * running FW Calendar Week and Year information can
541                          * be extracted from Timestamp information
542                          */
543                         version->min_fw_build_cw = tlv->val[0];
544                         version->min_fw_build_yy = tlv->val[1];
545                         version->timestamp = get_unaligned_le16(tlv->val);
546                         break;
547                 case INTEL_TLV_BUILD_TYPE:
548                         version->build_type = tlv->val[0];
549                         break;
550                 case INTEL_TLV_BUILD_NUM:
551                         /* If image type is Operational firmware (0x03), then
552                          * running FW build number can be extracted from the
553                          * Build information
554                          */
555                         version->min_fw_build_nn = tlv->val[0];
556                         version->build_num = get_unaligned_le32(tlv->val);
557                         break;
558                 case INTEL_TLV_SECURE_BOOT:
559                         version->secure_boot = tlv->val[0];
560                         break;
561                 case INTEL_TLV_OTP_LOCK:
562                         version->otp_lock = tlv->val[0];
563                         break;
564                 case INTEL_TLV_API_LOCK:
565                         version->api_lock = tlv->val[0];
566                         break;
567                 case INTEL_TLV_DEBUG_LOCK:
568                         version->debug_lock = tlv->val[0];
569                         break;
570                 case INTEL_TLV_MIN_FW:
571                         version->min_fw_build_nn = tlv->val[0];
572                         version->min_fw_build_cw = tlv->val[1];
573                         version->min_fw_build_yy = tlv->val[2];
574                         break;
575                 case INTEL_TLV_LIMITED_CCE:
576                         version->limited_cce = tlv->val[0];
577                         break;
578                 case INTEL_TLV_SBE_TYPE:
579                         version->sbe_type = tlv->val[0];
580                         break;
581                 case INTEL_TLV_OTP_BDADDR:
582                         memcpy(&version->otp_bd_addr, tlv->val,
583                                                         sizeof(bdaddr_t));
584                         break;
585                 default:
586                         /* Ignore rest of information */
587                         break;
588                 }
589                 /* consume the current tlv and move to next*/
590                 skb_pull(skb, tlv->len + sizeof(*tlv));
591         }
592
593         return 0;
594 }
595
596 int btintel_read_version_tlv(struct hci_dev *hdev, struct intel_version_tlv *version)
597 {
598         struct sk_buff *skb;
599         const u8 param[1] = { 0xFF };
600
601         if (!version)
602                 return -EINVAL;
603
604         skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
605         if (IS_ERR(skb)) {
606                 bt_dev_err(hdev, "Reading Intel version information failed (%ld)",
607                            PTR_ERR(skb));
608                 return PTR_ERR(skb);
609         }
610
611         if (skb->data[0]) {
612                 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
613                            skb->data[0]);
614                 kfree_skb(skb);
615                 return -EIO;
616         }
617
618         /* Consume Command Complete Status field */
619         skb_pull(skb, 1);
620
621         /* Event parameters contatin multiple TLVs. Read each of them
622          * and only keep the required data. Also, it use existing legacy
623          * version field like hw_platform, hw_variant, and fw_variant
624          * to keep the existing setup flow
625          */
626         while (skb->len) {
627                 struct intel_tlv *tlv;
628
629                 tlv = (struct intel_tlv *)skb->data;
630                 switch (tlv->type) {
631                 case INTEL_TLV_CNVI_TOP:
632                         version->cnvi_top = get_unaligned_le32(tlv->val);
633                         break;
634                 case INTEL_TLV_CNVR_TOP:
635                         version->cnvr_top = get_unaligned_le32(tlv->val);
636                         break;
637                 case INTEL_TLV_CNVI_BT:
638                         version->cnvi_bt = get_unaligned_le32(tlv->val);
639                         break;
640                 case INTEL_TLV_CNVR_BT:
641                         version->cnvr_bt = get_unaligned_le32(tlv->val);
642                         break;
643                 case INTEL_TLV_DEV_REV_ID:
644                         version->dev_rev_id = get_unaligned_le16(tlv->val);
645                         break;
646                 case INTEL_TLV_IMAGE_TYPE:
647                         version->img_type = tlv->val[0];
648                         break;
649                 case INTEL_TLV_TIME_STAMP:
650                         /* If image type is Operational firmware (0x03), then
651                          * running FW Calendar Week and Year information can
652                          * be extracted from Timestamp information
653                          */
654                         version->min_fw_build_cw = tlv->val[0];
655                         version->min_fw_build_yy = tlv->val[1];
656                         version->timestamp = get_unaligned_le16(tlv->val);
657                         break;
658                 case INTEL_TLV_BUILD_TYPE:
659                         version->build_type = tlv->val[0];
660                         break;
661                 case INTEL_TLV_BUILD_NUM:
662                         /* If image type is Operational firmware (0x03), then
663                          * running FW build number can be extracted from the
664                          * Build information
665                          */
666                         version->min_fw_build_nn = tlv->val[0];
667                         version->build_num = get_unaligned_le32(tlv->val);
668                         break;
669                 case INTEL_TLV_SECURE_BOOT:
670                         version->secure_boot = tlv->val[0];
671                         break;
672                 case INTEL_TLV_OTP_LOCK:
673                         version->otp_lock = tlv->val[0];
674                         break;
675                 case INTEL_TLV_API_LOCK:
676                         version->api_lock = tlv->val[0];
677                         break;
678                 case INTEL_TLV_DEBUG_LOCK:
679                         version->debug_lock = tlv->val[0];
680                         break;
681                 case INTEL_TLV_MIN_FW:
682                         version->min_fw_build_nn = tlv->val[0];
683                         version->min_fw_build_cw = tlv->val[1];
684                         version->min_fw_build_yy = tlv->val[2];
685                         break;
686                 case INTEL_TLV_LIMITED_CCE:
687                         version->limited_cce = tlv->val[0];
688                         break;
689                 case INTEL_TLV_SBE_TYPE:
690                         version->sbe_type = tlv->val[0];
691                         break;
692                 case INTEL_TLV_OTP_BDADDR:
693                         memcpy(&version->otp_bd_addr, tlv->val, tlv->len);
694                         break;
695                 default:
696                         /* Ignore rest of information */
697                         break;
698                 }
699                 /* consume the current tlv and move to next*/
700                 skb_pull(skb, tlv->len + sizeof(*tlv));
701         }
702
703         kfree_skb(skb);
704         return 0;
705 }
706 EXPORT_SYMBOL_GPL(btintel_read_version_tlv);
707
708 /* ------- REGMAP IBT SUPPORT ------- */
709
710 #define IBT_REG_MODE_8BIT  0x00
711 #define IBT_REG_MODE_16BIT 0x01
712 #define IBT_REG_MODE_32BIT 0x02
713
714 struct regmap_ibt_context {
715         struct hci_dev *hdev;
716         __u16 op_write;
717         __u16 op_read;
718 };
719
720 struct ibt_cp_reg_access {
721         __le32  addr;
722         __u8    mode;
723         __u8    len;
724         __u8    data[];
725 } __packed;
726
727 struct ibt_rp_reg_access {
728         __u8    status;
729         __le32  addr;
730         __u8    data[];
731 } __packed;
732
733 static int regmap_ibt_read(void *context, const void *addr, size_t reg_size,
734                            void *val, size_t val_size)
735 {
736         struct regmap_ibt_context *ctx = context;
737         struct ibt_cp_reg_access cp;
738         struct ibt_rp_reg_access *rp;
739         struct sk_buff *skb;
740         int err = 0;
741
742         if (reg_size != sizeof(__le32))
743                 return -EINVAL;
744
745         switch (val_size) {
746         case 1:
747                 cp.mode = IBT_REG_MODE_8BIT;
748                 break;
749         case 2:
750                 cp.mode = IBT_REG_MODE_16BIT;
751                 break;
752         case 4:
753                 cp.mode = IBT_REG_MODE_32BIT;
754                 break;
755         default:
756                 return -EINVAL;
757         }
758
759         /* regmap provides a little-endian formatted addr */
760         cp.addr = *(__le32 *)addr;
761         cp.len = val_size;
762
763         bt_dev_dbg(ctx->hdev, "Register (0x%x) read", le32_to_cpu(cp.addr));
764
765         skb = hci_cmd_sync(ctx->hdev, ctx->op_read, sizeof(cp), &cp,
766                            HCI_CMD_TIMEOUT);
767         if (IS_ERR(skb)) {
768                 err = PTR_ERR(skb);
769                 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error (%d)",
770                            le32_to_cpu(cp.addr), err);
771                 return err;
772         }
773
774         if (skb->len != sizeof(*rp) + val_size) {
775                 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad len",
776                            le32_to_cpu(cp.addr));
777                 err = -EINVAL;
778                 goto done;
779         }
780
781         rp = (struct ibt_rp_reg_access *)skb->data;
782
783         if (rp->addr != cp.addr) {
784                 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) read error, bad addr",
785                            le32_to_cpu(rp->addr));
786                 err = -EINVAL;
787                 goto done;
788         }
789
790         memcpy(val, rp->data, val_size);
791
792 done:
793         kfree_skb(skb);
794         return err;
795 }
796
797 static int regmap_ibt_gather_write(void *context,
798                                    const void *addr, size_t reg_size,
799                                    const void *val, size_t val_size)
800 {
801         struct regmap_ibt_context *ctx = context;
802         struct ibt_cp_reg_access *cp;
803         struct sk_buff *skb;
804         int plen = sizeof(*cp) + val_size;
805         u8 mode;
806         int err = 0;
807
808         if (reg_size != sizeof(__le32))
809                 return -EINVAL;
810
811         switch (val_size) {
812         case 1:
813                 mode = IBT_REG_MODE_8BIT;
814                 break;
815         case 2:
816                 mode = IBT_REG_MODE_16BIT;
817                 break;
818         case 4:
819                 mode = IBT_REG_MODE_32BIT;
820                 break;
821         default:
822                 return -EINVAL;
823         }
824
825         cp = kmalloc(plen, GFP_KERNEL);
826         if (!cp)
827                 return -ENOMEM;
828
829         /* regmap provides a little-endian formatted addr/value */
830         cp->addr = *(__le32 *)addr;
831         cp->mode = mode;
832         cp->len = val_size;
833         memcpy(&cp->data, val, val_size);
834
835         bt_dev_dbg(ctx->hdev, "Register (0x%x) write", le32_to_cpu(cp->addr));
836
837         skb = hci_cmd_sync(ctx->hdev, ctx->op_write, plen, cp, HCI_CMD_TIMEOUT);
838         if (IS_ERR(skb)) {
839                 err = PTR_ERR(skb);
840                 bt_dev_err(ctx->hdev, "regmap: Register (0x%x) write error (%d)",
841                            le32_to_cpu(cp->addr), err);
842                 goto done;
843         }
844         kfree_skb(skb);
845
846 done:
847         kfree(cp);
848         return err;
849 }
850
851 static int regmap_ibt_write(void *context, const void *data, size_t count)
852 {
853         /* data contains register+value, since we only support 32bit addr,
854          * minimum data size is 4 bytes.
855          */
856         if (WARN_ONCE(count < 4, "Invalid register access"))
857                 return -EINVAL;
858
859         return regmap_ibt_gather_write(context, data, 4, data + 4, count - 4);
860 }
861
862 static void regmap_ibt_free_context(void *context)
863 {
864         kfree(context);
865 }
866
867 static struct regmap_bus regmap_ibt = {
868         .read = regmap_ibt_read,
869         .write = regmap_ibt_write,
870         .gather_write = regmap_ibt_gather_write,
871         .free_context = regmap_ibt_free_context,
872         .reg_format_endian_default = REGMAP_ENDIAN_LITTLE,
873         .val_format_endian_default = REGMAP_ENDIAN_LITTLE,
874 };
875
876 /* Config is the same for all register regions */
877 static const struct regmap_config regmap_ibt_cfg = {
878         .name      = "btintel_regmap",
879         .reg_bits  = 32,
880         .val_bits  = 32,
881 };
882
883 struct regmap *btintel_regmap_init(struct hci_dev *hdev, u16 opcode_read,
884                                    u16 opcode_write)
885 {
886         struct regmap_ibt_context *ctx;
887
888         bt_dev_info(hdev, "regmap: Init R%x-W%x region", opcode_read,
889                     opcode_write);
890
891         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
892         if (!ctx)
893                 return ERR_PTR(-ENOMEM);
894
895         ctx->op_read = opcode_read;
896         ctx->op_write = opcode_write;
897         ctx->hdev = hdev;
898
899         return regmap_init(&hdev->dev, &regmap_ibt, ctx, &regmap_ibt_cfg);
900 }
901 EXPORT_SYMBOL_GPL(btintel_regmap_init);
902
903 int btintel_send_intel_reset(struct hci_dev *hdev, u32 boot_param)
904 {
905         struct intel_reset params = { 0x00, 0x01, 0x00, 0x01, 0x00000000 };
906         struct sk_buff *skb;
907
908         params.boot_param = cpu_to_le32(boot_param);
909
910         skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params), &params,
911                              HCI_INIT_TIMEOUT);
912         if (IS_ERR(skb)) {
913                 bt_dev_err(hdev, "Failed to send Intel Reset command");
914                 return PTR_ERR(skb);
915         }
916
917         kfree_skb(skb);
918
919         return 0;
920 }
921 EXPORT_SYMBOL_GPL(btintel_send_intel_reset);
922
923 int btintel_read_boot_params(struct hci_dev *hdev,
924                              struct intel_boot_params *params)
925 {
926         struct sk_buff *skb;
927
928         skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
929         if (IS_ERR(skb)) {
930                 bt_dev_err(hdev, "Reading Intel boot parameters failed (%ld)",
931                            PTR_ERR(skb));
932                 return PTR_ERR(skb);
933         }
934
935         if (skb->len != sizeof(*params)) {
936                 bt_dev_err(hdev, "Intel boot parameters size mismatch");
937                 kfree_skb(skb);
938                 return -EILSEQ;
939         }
940
941         memcpy(params, skb->data, sizeof(*params));
942
943         kfree_skb(skb);
944
945         if (params->status) {
946                 bt_dev_err(hdev, "Intel boot parameters command failed (%02x)",
947                            params->status);
948                 return -bt_to_errno(params->status);
949         }
950
951         bt_dev_info(hdev, "Device revision is %u",
952                     le16_to_cpu(params->dev_revid));
953
954         bt_dev_info(hdev, "Secure boot is %s",
955                     params->secure_boot ? "enabled" : "disabled");
956
957         bt_dev_info(hdev, "OTP lock is %s",
958                     params->otp_lock ? "enabled" : "disabled");
959
960         bt_dev_info(hdev, "API lock is %s",
961                     params->api_lock ? "enabled" : "disabled");
962
963         bt_dev_info(hdev, "Debug lock is %s",
964                     params->debug_lock ? "enabled" : "disabled");
965
966         bt_dev_info(hdev, "Minimum firmware build %u week %u %u",
967                     params->min_fw_build_nn, params->min_fw_build_cw,
968                     2000 + params->min_fw_build_yy);
969
970         return 0;
971 }
972 EXPORT_SYMBOL_GPL(btintel_read_boot_params);
973
974 static int btintel_sfi_rsa_header_secure_send(struct hci_dev *hdev,
975                                               const struct firmware *fw)
976 {
977         int err;
978
979         /* Start the firmware download transaction with the Init fragment
980          * represented by the 128 bytes of CSS header.
981          */
982         err = btintel_secure_send(hdev, 0x00, 128, fw->data);
983         if (err < 0) {
984                 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
985                 goto done;
986         }
987
988         /* Send the 256 bytes of public key information from the firmware
989          * as the PKey fragment.
990          */
991         err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
992         if (err < 0) {
993                 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
994                 goto done;
995         }
996
997         /* Send the 256 bytes of signature information from the firmware
998          * as the Sign fragment.
999          */
1000         err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
1001         if (err < 0) {
1002                 bt_dev_err(hdev, "Failed to send firmware signature (%d)", err);
1003                 goto done;
1004         }
1005
1006 done:
1007         return err;
1008 }
1009
1010 static int btintel_sfi_ecdsa_header_secure_send(struct hci_dev *hdev,
1011                                                 const struct firmware *fw)
1012 {
1013         int err;
1014
1015         /* Start the firmware download transaction with the Init fragment
1016          * represented by the 128 bytes of CSS header.
1017          */
1018         err = btintel_secure_send(hdev, 0x00, 128, fw->data + 644);
1019         if (err < 0) {
1020                 bt_dev_err(hdev, "Failed to send firmware header (%d)", err);
1021                 return err;
1022         }
1023
1024         /* Send the 96 bytes of public key information from the firmware
1025          * as the PKey fragment.
1026          */
1027         err = btintel_secure_send(hdev, 0x03, 96, fw->data + 644 + 128);
1028         if (err < 0) {
1029                 bt_dev_err(hdev, "Failed to send firmware pkey (%d)", err);
1030                 return err;
1031         }
1032
1033         /* Send the 96 bytes of signature information from the firmware
1034          * as the Sign fragment
1035          */
1036         err = btintel_secure_send(hdev, 0x02, 96, fw->data + 644 + 224);
1037         if (err < 0) {
1038                 bt_dev_err(hdev, "Failed to send firmware signature (%d)",
1039                            err);
1040                 return err;
1041         }
1042         return 0;
1043 }
1044
1045 static int btintel_download_firmware_payload(struct hci_dev *hdev,
1046                                              const struct firmware *fw,
1047                                              size_t offset)
1048 {
1049         int err;
1050         const u8 *fw_ptr;
1051         u32 frag_len;
1052
1053         fw_ptr = fw->data + offset;
1054         frag_len = 0;
1055         err = -EINVAL;
1056
1057         while (fw_ptr - fw->data < fw->size) {
1058                 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
1059
1060                 frag_len += sizeof(*cmd) + cmd->plen;
1061
1062                 /* The parameter length of the secure send command requires
1063                  * a 4 byte alignment. It happens so that the firmware file
1064                  * contains proper Intel_NOP commands to align the fragments
1065                  * as needed.
1066                  *
1067                  * Send set of commands with 4 byte alignment from the
1068                  * firmware data buffer as a single Data fragement.
1069                  */
1070                 if (!(frag_len % 4)) {
1071                         err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
1072                         if (err < 0) {
1073                                 bt_dev_err(hdev,
1074                                            "Failed to send firmware data (%d)",
1075                                            err);
1076                                 goto done;
1077                         }
1078
1079                         fw_ptr += frag_len;
1080                         frag_len = 0;
1081                 }
1082         }
1083
1084 done:
1085         return err;
1086 }
1087
1088 static bool btintel_firmware_version(struct hci_dev *hdev,
1089                                      u8 num, u8 ww, u8 yy,
1090                                      const struct firmware *fw,
1091                                      u32 *boot_addr)
1092 {
1093         const u8 *fw_ptr;
1094
1095         fw_ptr = fw->data;
1096
1097         while (fw_ptr - fw->data < fw->size) {
1098                 struct hci_command_hdr *cmd = (void *)(fw_ptr);
1099
1100                 /* Each SKU has a different reset parameter to use in the
1101                  * HCI_Intel_Reset command and it is embedded in the firmware
1102                  * data. So, instead of using static value per SKU, check
1103                  * the firmware data and save it for later use.
1104                  */
1105                 if (le16_to_cpu(cmd->opcode) == CMD_WRITE_BOOT_PARAMS) {
1106                         struct cmd_write_boot_params *params;
1107
1108                         params = (void *)(fw_ptr + sizeof(*cmd));
1109
1110                         bt_dev_info(hdev, "Boot Address: 0x%x",
1111                                     le32_to_cpu(params->boot_addr));
1112
1113                         bt_dev_info(hdev, "Firmware Version: %u-%u.%u",
1114                                     params->fw_build_num, params->fw_build_ww,
1115                                     params->fw_build_yy);
1116
1117                         return (num == params->fw_build_num &&
1118                                 ww == params->fw_build_ww &&
1119                                 yy == params->fw_build_yy);
1120                 }
1121
1122                 fw_ptr += sizeof(*cmd) + cmd->plen;
1123         }
1124
1125         return false;
1126 }
1127
1128 int btintel_download_firmware(struct hci_dev *hdev,
1129                               struct intel_version *ver,
1130                               const struct firmware *fw,
1131                               u32 *boot_param)
1132 {
1133         int err;
1134
1135         /* SfP and WsP don't seem to update the firmware version on file
1136          * so version checking is currently not possible.
1137          */
1138         switch (ver->hw_variant) {
1139         case 0x0b:      /* SfP */
1140         case 0x0c:      /* WsP */
1141                 /* Skip version checking */
1142                 break;
1143         default:
1144                 /* Skip reading firmware file version in bootloader mode */
1145                 if (ver->fw_variant == 0x06)
1146                         break;
1147
1148                 /* Skip download if firmware has the same version */
1149                 if (btintel_firmware_version(hdev, ver->fw_build_num,
1150                                              ver->fw_build_ww, ver->fw_build_yy,
1151                                              fw, boot_param)) {
1152                         bt_dev_info(hdev, "Firmware already loaded");
1153                         /* Return -EALREADY to indicate that the firmware has
1154                          * already been loaded.
1155                          */
1156                         return -EALREADY;
1157                 }
1158         }
1159
1160         /* The firmware variant determines if the device is in bootloader
1161          * mode or is running operational firmware. The value 0x06 identifies
1162          * the bootloader and the value 0x23 identifies the operational
1163          * firmware.
1164          *
1165          * If the firmware version has changed that means it needs to be reset
1166          * to bootloader when operational so the new firmware can be loaded.
1167          */
1168         if (ver->fw_variant == 0x23)
1169                 return -EINVAL;
1170
1171         err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1172         if (err)
1173                 return err;
1174
1175         return btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1176 }
1177 EXPORT_SYMBOL_GPL(btintel_download_firmware);
1178
1179 int btintel_download_firmware_newgen(struct hci_dev *hdev,
1180                                      struct intel_version_tlv *ver,
1181                                      const struct firmware *fw, u32 *boot_param,
1182                                      u8 hw_variant, u8 sbe_type)
1183 {
1184         int err;
1185         u32 css_header_ver;
1186
1187         /* Skip reading firmware file version in bootloader mode */
1188         if (ver->img_type != 0x01) {
1189                 /* Skip download if firmware has the same version */
1190                 if (btintel_firmware_version(hdev, ver->min_fw_build_nn,
1191                                              ver->min_fw_build_cw,
1192                                              ver->min_fw_build_yy,
1193                                              fw, boot_param)) {
1194                         bt_dev_info(hdev, "Firmware already loaded");
1195                         /* Return -EALREADY to indicate that firmware has
1196                          * already been loaded.
1197                          */
1198                         return -EALREADY;
1199                 }
1200         }
1201
1202         /* The firmware variant determines if the device is in bootloader
1203          * mode or is running operational firmware. The value 0x01 identifies
1204          * the bootloader and the value 0x03 identifies the operational
1205          * firmware.
1206          *
1207          * If the firmware version has changed that means it needs to be reset
1208          * to bootloader when operational so the new firmware can be loaded.
1209          */
1210         if (ver->img_type == 0x03)
1211                 return -EINVAL;
1212
1213         /* iBT hardware variants 0x0b, 0x0c, 0x11, 0x12, 0x13, 0x14 support
1214          * only RSA secure boot engine. Hence, the corresponding sfi file will
1215          * have RSA header of 644 bytes followed by Command Buffer.
1216          *
1217          * iBT hardware variants 0x17, 0x18 onwards support both RSA and ECDSA
1218          * secure boot engine. As a result, the corresponding sfi file will
1219          * have RSA header of 644, ECDSA header of 320 bytes followed by
1220          * Command Buffer.
1221          *
1222          * CSS Header byte positions 0x08 to 0x0B represent the CSS Header
1223          * version: RSA(0x00010000) , ECDSA (0x00020000)
1224          */
1225         css_header_ver = get_unaligned_le32(fw->data + CSS_HEADER_OFFSET);
1226         if (css_header_ver != 0x00010000) {
1227                 bt_dev_err(hdev, "Invalid CSS Header version");
1228                 return -EINVAL;
1229         }
1230
1231         if (hw_variant <= 0x14) {
1232                 if (sbe_type != 0x00) {
1233                         bt_dev_err(hdev, "Invalid SBE type for hardware variant (%d)",
1234                                    hw_variant);
1235                         return -EINVAL;
1236                 }
1237
1238                 err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1239                 if (err)
1240                         return err;
1241
1242                 err = btintel_download_firmware_payload(hdev, fw, RSA_HEADER_LEN);
1243                 if (err)
1244                         return err;
1245         } else if (hw_variant >= 0x17) {
1246                 /* Check if CSS header for ECDSA follows the RSA header */
1247                 if (fw->data[ECDSA_OFFSET] != 0x06)
1248                         return -EINVAL;
1249
1250                 /* Check if the CSS Header version is ECDSA(0x00020000) */
1251                 css_header_ver = get_unaligned_le32(fw->data + ECDSA_OFFSET + CSS_HEADER_OFFSET);
1252                 if (css_header_ver != 0x00020000) {
1253                         bt_dev_err(hdev, "Invalid CSS Header version");
1254                         return -EINVAL;
1255                 }
1256
1257                 if (sbe_type == 0x00) {
1258                         err = btintel_sfi_rsa_header_secure_send(hdev, fw);
1259                         if (err)
1260                                 return err;
1261
1262                         err = btintel_download_firmware_payload(hdev, fw,
1263                                                                 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1264                         if (err)
1265                                 return err;
1266                 } else if (sbe_type == 0x01) {
1267                         err = btintel_sfi_ecdsa_header_secure_send(hdev, fw);
1268                         if (err)
1269                                 return err;
1270
1271                         err = btintel_download_firmware_payload(hdev, fw,
1272                                                                 RSA_HEADER_LEN + ECDSA_HEADER_LEN);
1273                         if (err)
1274                                 return err;
1275                 }
1276         }
1277         return 0;
1278 }
1279 EXPORT_SYMBOL_GPL(btintel_download_firmware_newgen);
1280
1281 void btintel_reset_to_bootloader(struct hci_dev *hdev)
1282 {
1283         struct intel_reset params;
1284         struct sk_buff *skb;
1285
1286         /* Send Intel Reset command. This will result in
1287          * re-enumeration of BT controller.
1288          *
1289          * Intel Reset parameter description:
1290          * reset_type :   0x00 (Soft reset),
1291          *                0x01 (Hard reset)
1292          * patch_enable : 0x00 (Do not enable),
1293          *                0x01 (Enable)
1294          * ddc_reload :   0x00 (Do not reload),
1295          *                0x01 (Reload)
1296          * boot_option:   0x00 (Current image),
1297          *                0x01 (Specified boot address)
1298          * boot_param:    Boot address
1299          *
1300          */
1301         params.reset_type = 0x01;
1302         params.patch_enable = 0x01;
1303         params.ddc_reload = 0x01;
1304         params.boot_option = 0x00;
1305         params.boot_param = cpu_to_le32(0x00000000);
1306
1307         skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(params),
1308                              &params, HCI_INIT_TIMEOUT);
1309         if (IS_ERR(skb)) {
1310                 bt_dev_err(hdev, "FW download error recovery failed (%ld)",
1311                            PTR_ERR(skb));
1312                 return;
1313         }
1314         bt_dev_info(hdev, "Intel reset sent to retry FW download");
1315         kfree_skb(skb);
1316
1317         /* Current Intel BT controllers(ThP/JfP) hold the USB reset
1318          * lines for 2ms when it receives Intel Reset in bootloader mode.
1319          * Whereas, the upcoming Intel BT controllers will hold USB reset
1320          * for 150ms. To keep the delay generic, 150ms is chosen here.
1321          */
1322         msleep(150);
1323 }
1324 EXPORT_SYMBOL_GPL(btintel_reset_to_bootloader);
1325
1326 int btintel_read_debug_features(struct hci_dev *hdev,
1327                                 struct intel_debug_features *features)
1328 {
1329         struct sk_buff *skb;
1330         u8 page_no = 1;
1331
1332         /* Intel controller supports two pages, each page is of 128-bit
1333          * feature bit mask. And each bit defines specific feature support
1334          */
1335         skb = __hci_cmd_sync(hdev, 0xfca6, sizeof(page_no), &page_no,
1336                              HCI_INIT_TIMEOUT);
1337         if (IS_ERR(skb)) {
1338                 bt_dev_err(hdev, "Reading supported features failed (%ld)",
1339                            PTR_ERR(skb));
1340                 return PTR_ERR(skb);
1341         }
1342
1343         if (skb->len != (sizeof(features->page1) + 3)) {
1344                 bt_dev_err(hdev, "Supported features event size mismatch");
1345                 kfree_skb(skb);
1346                 return -EILSEQ;
1347         }
1348
1349         memcpy(features->page1, skb->data + 3, sizeof(features->page1));
1350
1351         /* Read the supported features page2 if required in future.
1352          */
1353         kfree_skb(skb);
1354         return 0;
1355 }
1356 EXPORT_SYMBOL_GPL(btintel_read_debug_features);
1357
1358 int btintel_set_debug_features(struct hci_dev *hdev,
1359                                const struct intel_debug_features *features)
1360 {
1361         u8 mask[11] = { 0x0a, 0x92, 0x02, 0x07, 0x00, 0x00, 0x00, 0x00,
1362                         0x00, 0x00, 0x00 };
1363         struct sk_buff *skb;
1364
1365         if (!features)
1366                 return -EINVAL;
1367
1368         if (!(features->page1[0] & 0x3f)) {
1369                 bt_dev_info(hdev, "Telemetry exception format not supported");
1370                 return 0;
1371         }
1372
1373         skb = __hci_cmd_sync(hdev, 0xfc8b, 11, mask, HCI_INIT_TIMEOUT);
1374         if (IS_ERR(skb)) {
1375                 bt_dev_err(hdev, "Setting Intel telemetry ddc write event mask failed (%ld)",
1376                            PTR_ERR(skb));
1377                 return PTR_ERR(skb);
1378         }
1379
1380         kfree_skb(skb);
1381         return 0;
1382 }
1383 EXPORT_SYMBOL_GPL(btintel_set_debug_features);
1384
1385 static int btintel_setup_combined(struct hci_dev *hdev)
1386 {
1387         const u8 param[1] = { 0xFF };
1388         struct intel_version ver;
1389         struct intel_version_tlv ver_tlv;
1390         struct sk_buff *skb;
1391         int err;
1392
1393         BT_DBG("%s", hdev->name);
1394
1395         /* Starting from TyP device, the command parameter and response are
1396          * changed even though the OCF for HCI_Intel_Read_Version command
1397          * remains same. The legacy devices can handle even if the
1398          * command has a parameter and returns a correct version information.
1399          * So, it uses new format to support both legacy and new format.
1400          */
1401         skb = __hci_cmd_sync(hdev, 0xfc05, 1, param, HCI_CMD_TIMEOUT);
1402         if (IS_ERR(skb)) {
1403                 bt_dev_err(hdev, "Reading Intel version command failed (%ld)",
1404                            PTR_ERR(skb));
1405                 return PTR_ERR(skb);
1406         }
1407
1408         /* Check the status */
1409         if (skb->data[0]) {
1410                 bt_dev_err(hdev, "Intel Read Version command failed (%02x)",
1411                            skb->data[0]);
1412                 err = -EIO;
1413                 goto exit_error;
1414         }
1415
1416         /* For Legacy device, check the HW platform value and size */
1417         if (skb->len == sizeof(ver) && skb->data[1] == 0x37) {
1418                 bt_dev_dbg(hdev, "Read the legacy Intel version information");
1419
1420                 memcpy(&ver, skb->data, sizeof(ver));
1421
1422                 /* Display version information */
1423                 btintel_version_info(hdev, &ver);
1424
1425                 /* Check for supported iBT hardware variants of this firmware
1426                  * loading method.
1427                  *
1428                  * This check has been put in place to ensure correct forward
1429                  * compatibility options when newer hardware variants come
1430                  * along.
1431                  */
1432                 switch (ver.hw_variant) {
1433                 case 0x07:      /* WP */
1434                 case 0x08:      /* StP */
1435                         /* Legacy ROM product */
1436                         /* TODO: call setup routine for legacy rom product */
1437                         break;
1438                 case 0x0b:      /* SfP */
1439                 case 0x0c:      /* WsP */
1440                 case 0x11:      /* JfP */
1441                 case 0x12:      /* ThP */
1442                 case 0x13:      /* HrP */
1443                 case 0x14:      /* CcP */
1444                         /* TODO: call setup routine for bootloader product */
1445                         break;
1446                 default:
1447                         bt_dev_err(hdev, "Unsupported Intel hw variant (%u)",
1448                                    ver.hw_variant);
1449                         err = -EINVAL;
1450                 }
1451
1452                 goto exit_error;
1453         }
1454
1455         /* For TLV type device, parse the tlv data */
1456         err = btintel_parse_version_tlv(hdev, &ver_tlv, skb);
1457         if (err) {
1458                 bt_dev_err(hdev, "Failed to parse TLV version information");
1459                 goto exit_error;
1460         }
1461
1462         if (INTEL_HW_PLATFORM(ver_tlv.cnvi_bt) != 0x37) {
1463                 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
1464                            INTEL_HW_PLATFORM(ver_tlv.cnvi_bt));
1465                 err = -EINVAL;
1466                 goto exit_error;
1467         }
1468
1469         /* Display version information of TLV type */
1470         btintel_version_info_tlv(hdev, &ver_tlv);
1471
1472         /* TODO: Need to filter the device for new generation */
1473         /* TODO: call setup routine for tlv based bootloader product */
1474
1475 exit_error:
1476         kfree_skb(skb);
1477
1478         return err;
1479 }
1480
1481 static int btintel_shutdown_combined(struct hci_dev *hdev)
1482 {
1483         struct sk_buff *skb;
1484
1485         /* Send HCI Reset to the controller to stop any BT activity which
1486          * were triggered. This will help to save power and maintain the
1487          * sync b/w Host and controller
1488          */
1489         skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1490         if (IS_ERR(skb)) {
1491                 bt_dev_err(hdev, "HCI reset during shutdown failed");
1492                 return PTR_ERR(skb);
1493         }
1494         kfree_skb(skb);
1495
1496         return 0;
1497 }
1498
1499 int btintel_configure_setup(struct hci_dev *hdev)
1500 {
1501         /* TODO: Setup hdev callback here */
1502         hdev->manufacturer = 2;
1503         hdev->setup = btintel_setup_combined;
1504         hdev->shutdown = btintel_shutdown_combined;
1505
1506         return 0;
1507 }
1508 EXPORT_SYMBOL_GPL(btintel_configure_setup);
1509
1510 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1511 MODULE_DESCRIPTION("Bluetooth support for Intel devices ver " VERSION);
1512 MODULE_VERSION(VERSION);
1513 MODULE_LICENSE("GPL");
1514 MODULE_FIRMWARE("intel/ibt-11-5.sfi");
1515 MODULE_FIRMWARE("intel/ibt-11-5.ddc");
1516 MODULE_FIRMWARE("intel/ibt-12-16.sfi");
1517 MODULE_FIRMWARE("intel/ibt-12-16.ddc");