1 /******************************************************************************
2  *
3  *  Copyright 2015-2018,2020-2023 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #define LOG_TAG "NxpHal"
19 #include "NxpNfcCapability.h"
20 #include <phNxpLog.h>
21 
22 capability* capability::instance = NULL;
23 tNFC_chipType capability::chipType = pn81T;
24 tNfc_featureList nfcFL;
25 
capability()26 capability::capability() {}
27 
getInstance()28 capability* capability::getInstance() {
29   if (NULL == instance) {
30     instance = new capability();
31   }
32   return instance;
33 }
34 
processChipType(uint8_t * msg,uint16_t msg_len)35 tNFC_chipType capability::processChipType(uint8_t* msg, uint16_t msg_len) {
36   if ((msg != NULL) && (msg_len != 0)) {
37     if ((msg_len > 2) && msg[0] == NCI_CMD_RSP_SUCCESS_SW1 &&
38         msg[1] == NCI_CMD_RSP_SUCCESS_SW2) {
39       chipType = determineChipTypeFromNciRsp(msg, msg_len);
40     } else if (msg[0] == FW_DL_RSP_FIRST_BYTE) {
41       chipType = determineChipTypeFromDLRsp(msg, msg_len);
42     } else {
43       ALOGD("%s Wrong msg_len. Setting Default ChipType pn80T", __func__);
44       chipType = pn81T;
45     }
46   }
47   ALOGD("%s Product : %s", __func__, product[chipType]);
48   return chipType;
49 }
50 
determineChipTypeFromNciRsp(uint8_t * msg,uint16_t msg_len)51 tNFC_chipType capability::determineChipTypeFromNciRsp(uint8_t* msg,
52                                                       uint16_t msg_len) {
53   tNFC_chipType chip_type = pn81T;
54   if ((msg == NULL) || (msg_len < 3)) {
55     ALOGD("%s Wrong msg_len. Setting Default ChipType pn80T", __func__);
56     return chip_type;
57   }
58   if (GET_FW_ROM_VERSION_NCI_RESP(msg, msg_len) ==
59           FW_MOBILE_ROM_VERSION_PN557 &&
60       (GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
61            FW_MOBILE_MAJOR_NUMBER_PN557 ||
62        GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
63            FW_MOBILE_MAJOR_NUMBER_PN557_V2))
64     chip_type = pn557;
65   else if (GET_FW_ROM_VERSION_NCI_RESP(msg, msg_len) ==
66                FW_MOBILE_ROM_VERSION_PN553 &&
67            GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
68                FW_MOBILE_MAJOR_NUMBER_PN553)
69     chip_type = pn553;
70   else if (GET_FW_ROM_VERSION_NCI_RESP(msg, msg_len) ==
71                FW_MOBILE_ROM_VERSION_SN100U &&
72            GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
73                FW_MOBILE_MAJOR_NUMBER_SN100U)
74     chip_type = sn100u;
75   /* PN560 & SN220 have same rom & fw major version but chip type is different*/
76   else if (GET_FW_ROM_VERSION_NCI_RESP(msg, msg_len) ==
77                FW_MOBILE_ROM_VERSION_SN220U &&
78            GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
79                FW_MOBILE_MAJOR_NUMBER_SN220U) {
80     if ((msg_len >= 4) &&
81         (GET_HW_VERSION_NCI_RESP(msg, msg_len) == HW_PN560_V1 ||
82          GET_HW_VERSION_NCI_RESP(msg, msg_len) == HW_PN560_V2)) {
83       chip_type = pn560;
84     } else {
85       chip_type = sn220u;
86     }
87   } else if (GET_FW_ROM_VERSION_NCI_RESP(msg, msg_len) ==
88                  FW_MOBILE_ROM_VERSION_SN300U &&
89              GET_FW_MAJOR_VERSION_NCI_RESP(msg, msg_len) ==
90                  FW_MOBILE_MAJOR_NUMBER_SN300U) {
91     chip_type = sn300u;
92   }
93   return chip_type;
94 }
95 
determineChipTypeFromDLRsp(uint8_t * msg,uint16_t msg_len)96 tNFC_chipType capability::determineChipTypeFromDLRsp(uint8_t* msg,
97                                                      uint16_t msg_len) {
98   tNFC_chipType chip_type = pn81T;
99   if ((msg == NULL) || (msg_len < 3)) {
100     ALOGD("%s Wrong msg_len. Setting Default ChipType pn80T", __func__);
101     return chip_type;
102   }
103   /* PN560 & SN220 have same rom & fw major version but chip type is different*/
104   if (msg[offsetFwRomCodeVersion] == FW_MOBILE_ROM_VERSION_SN220U &&
105       msg[offsetFwMajorVersion] == FW_MOBILE_MAJOR_NUMBER_SN220U) {
106     if (msg[offsetDlRspChipType] == HW_PN560_V1 ||
107         msg[offsetDlRspChipType] == HW_PN560_V2) {
108       chip_type = pn560;
109     } else {
110       chip_type = sn220u;
111     }
112   } else if (msg[offsetFwRomCodeVersion] == FW_MOBILE_ROM_VERSION_SN100U &&
113              msg[offsetFwMajorVersion] == FW_MOBILE_MAJOR_NUMBER_SN100U)
114     chip_type = sn100u;
115   else if (msg[offsetFwRomCodeVersion] == FW_MOBILE_ROM_VERSION_PN557 &&
116            (msg[offsetFwMajorVersion_pn557] == FW_MOBILE_ROM_VERSION_PN557 ||
117             msg[offsetFwMajorVersion_pn557] == FW_MOBILE_MAJOR_NUMBER_PN557_V2))
118     chip_type = pn557;
119   else if (msg[offsetFwRomCodeVersion] == FW_MOBILE_ROM_VERSION_SN300U &&
120            (msg[offsetFwMajorVersion] == FW_MOBILE_MAJOR_NUMBER_SN300U ||
121             msg[offsetDlRspChipType] == HW_SN300U)) {
122     chip_type = sn300u;
123   }
124   return chip_type;
125 }
126 
getFWVersionInfo(uint8_t * msg,uint16_t msg_len)127 uint32_t capability::getFWVersionInfo(uint8_t* msg, uint16_t msg_len) {
128   uint32_t versionInfo = 0;
129   if ((msg != NULL) && (msg_len != 0)) {
130     if (msg[0] == 0x00) {
131       versionInfo = msg[offsetFwRomCodeVersion] << 16;
132       versionInfo |= msg[offsetFwMajorVersion] << 8;
133       versionInfo |= msg[offsetFwMinorVersion];
134     }
135   }
136   return versionInfo;
137 }
138