1 /*
2  * Copyright (C) 2010-2019 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef _PHNXPNCIHAL_H_
17 #define _PHNXPNCIHAL_H_
18 
19 #include "NxpMfcReader.h"
20 #include "NxpNfcCapability.h"
21 #include <hardware/nfc.h>
22 #include <phNxpNciHal_utils.h>
23 
24 /********************* Definitions and structures *****************************/
25 #define MAX_RETRY_COUNT 5
26 #define NCI_MAX_DATA_LEN 300
27 #define NCI_POLL_DURATION 500
28 #define HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT 0x07
29 #undef P2P_PRIO_LOGIC_HAL_IMP
30 #define NCI_VERSION_2_0 0x20
31 #define NCI_VERSION_1_1 0x11
32 #define NCI_VERSION_1_0 0x10
33 #define NCI_VERSION_UNKNOWN 0x00
34 typedef void(phNxpNciHal_control_granted_callback_t)();
35 
36 /*ROM CODE VERSION FW*/
37 #define FW_MOBILE_ROM_VERSION_PN551 0x10
38 #define FW_MOBILE_ROM_VERSION_PN553 0x11
39 #define FW_MOBILE_ROM_VERSION_PN557 0x12
40 /* NCI Data */
41 
42 #define NCI_MT_CMD 0x20
43 #define NCI_MT_RSP 0x40
44 #define NCI_MT_NTF 0x60
45 
46 #define CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED 0x02
47 #define CORE_RESET_TRIGGER_TYPE_POWERED_ON 0x01
48 #define NCI_MSG_CORE_RESET 0x00
49 #define NCI_MSG_CORE_INIT 0x01
50 #define NCI_MT_MASK 0xE0
51 #define NCI_OID_MASK 0x3F
52 
53 #define NXP_MAX_CONFIG_STRING_LEN 260
54 #define NCI_HEADER_SIZE 3
55 
56 typedef struct nci_data {
57   uint16_t len;
58   uint8_t p_data[NCI_MAX_DATA_LEN];
59 } nci_data_t;
60 
61 typedef enum {
62   HAL_STATUS_CLOSE = 0,
63   HAL_STATUS_OPEN,
64   HAL_STATUS_MIN_OPEN
65 } phNxpNci_HalStatus;
66 
67 typedef enum {
68   GPIO_UNKNOWN = 0x00,
69   GPIO_STORE = 0x01,
70   GPIO_STORE_DONE = 0x02,
71   GPIO_RESTORE = 0x10,
72   GPIO_RESTORE_DONE = 0x20,
73   GPIO_CLEAR = 0xFF
74 } phNxpNciHal_GpioInfoState;
75 
76 typedef struct phNxpNciGpioInfo {
77   phNxpNciHal_GpioInfoState state;
78   uint8_t values[2];
79 } phNxpNciGpioInfo_t;
80 
81 /* Macros to enable and disable extensions */
82 #define HAL_ENABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 1)
83 #define HAL_DISABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 0)
84 typedef struct phNxpNciInfo {
85   uint8_t nci_version;
86   bool_t wait_for_ntf;
87 } phNxpNciInfo_t;
88 /* NCI Control structure */
89 typedef struct phNxpNciHal_Control {
90   phNxpNci_HalStatus halStatus; /* Indicate if hal is open or closed */
91   pthread_t client_thread;      /* Integration thread handle */
92   uint8_t thread_running;       /* Thread running if set to 1, else set to 0 */
93   phLibNfc_sConfig_t gDrvCfg;   /* Driver config data */
94 
95   /* Rx data */
96   uint8_t* p_rx_data;
97   uint16_t rx_data_len;
98 
99   /* Rx data */
100   uint8_t* p_rx_ese_data;
101   uint16_t rx_ese_data_len;
102 
103   /* libnfc-nci callbacks */
104   nfc_stack_callback_t* p_nfc_stack_cback;
105   nfc_stack_data_callback_t* p_nfc_stack_data_cback;
106 
107   /* control granted callback */
108   phNxpNciHal_control_granted_callback_t* p_control_granted_cback;
109 
110   /* HAL open status */
111   bool_t hal_open_status;
112 
113   /* HAL extensions */
114   uint8_t hal_ext_enabled;
115 
116   /* Waiting semaphore */
117   phNxpNciHal_Sem_t ext_cb_data;
118   sem_t syncSpiNfc;
119 
120   uint16_t cmd_len;
121   uint8_t p_cmd_data[NCI_MAX_DATA_LEN];
122   uint16_t rsp_len;
123   uint8_t p_rsp_data[NCI_MAX_DATA_LEN];
124 
125   /* retry count used to force download */
126   uint16_t retry_cnt;
127   uint8_t read_retry_cnt;
128   phNxpNciInfo_t nci_info;
129 
130   /* to store and restore gpio values */
131   phNxpNciGpioInfo_t phNxpNciGpioInfo;
132   bool bIsForceFwDwnld;
133 } phNxpNciHal_Control_t;
134 
135 typedef struct phNxpNciClock {
136   bool_t isClockSet;
137   uint8_t p_rx_data[20];
138   bool_t issetConfig;
139 } phNxpNciClock_t;
140 
141 typedef struct phNxpNciRfSetting {
142   bool_t isGetRfSetting;
143   uint8_t p_rx_data[20];
144 } phNxpNciRfSetting_t;
145 
146 typedef struct phNxpNciMwEepromArea {
147   bool_t isGetEepromArea;
148   uint8_t p_rx_data[32];
149 } phNxpNciMwEepromArea_t;
150 
151 typedef enum {
152   NFC_FORUM_PROFILE,
153   EMV_CO_PROFILE,
154   INVALID_PROFILe
155 } phNxpNciProfile_t;
156 /* NXP Poll Profile control structure */
157 typedef struct phNxpNciProfile_Control {
158   phNxpNciProfile_t profile_type;
159   uint8_t bClkSrcVal; /* Holds the System clock source read from config file */
160   uint8_t
161       bClkFreqVal;  /* Holds the System clock frequency read from config file */
162   uint8_t bTimeout; /* Holds the Timeout Value */
163 } phNxpNciProfile_Control_t;
164 
165 /* Internal messages to handle callbacks */
166 #define NCI_HAL_OPEN_CPLT_MSG 0x411
167 #define NCI_HAL_CLOSE_CPLT_MSG 0x412
168 #define NCI_HAL_POST_INIT_CPLT_MSG 0x413
169 #define NCI_HAL_PRE_DISCOVER_CPLT_MSG 0x414
170 #define NCI_HAL_ERROR_MSG 0x415
171 #define NCI_HAL_HCI_NETWORK_RESET_MSG 0x416
172 #define NCI_HAL_RX_MSG 0xF01
173 
174 #define NCIHAL_CMD_CODE_LEN_BYTE_OFFSET (2U)
175 #define NCIHAL_CMD_CODE_BYTE_LEN (3U)
176 
177 /******************** NCI HAL exposed functions *******************************/
178 int phNxpNciHal_check_ncicmd_write_window(uint16_t cmd_len, uint8_t* p_cmd);
179 void phNxpNciHal_request_control(void);
180 void phNxpNciHal_release_control(void);
181 int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t* p_data);
182 /*******************************************************************************
183 **
184 ** Function         phNxpNciHal_configFeatureList
185 **
186 ** Description      Configures the featureList based on chip type
187 
188 ** Returns          none
189 *******************************************************************************/
190 void phNxpNciHal_configFeatureList(uint8_t* init_rsp, uint16_t rsp_len);
191 #endif /* _PHNXPNCIHAL_H_ */
192