1 /******************************************************************************
2 *
3 * Copyright 2018-2020 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
19 /*
20 * DAL spi port implementation for linux
21 *
22 * Project: Trusted ESE Linux
23 *
24 */
25 #define LOG_TAG "NxpEseHal"
26 #include <log/log.h>
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <sys/ioctl.h>
32 #include <unistd.h>
33
34 #include <EseTransportFactory.h>
35 #include <ese_config.h>
36 #include <phEseStatus.h>
37 #include <string.h>
38
39 /*!
40 * \brief Normal mode header length
41 */
42 #define NORMAL_MODE_HEADER_LEN 3
43 /*!
44 * \brief Normal mode header offset
45 */
46 #define NORMAL_MODE_LEN_OFFSET 2
47 /*!
48 * \brief Start of frame marker
49 */
50 #define SEND_PACKET_SOF 0x5A
51 /*!
52 * \brief To enable SPI interface for ESE communication
53 */
54 #define SPI_ENABLED 1
55
56 spTransport gpTransportObj;
57
58 /*******************************************************************************
59 **
60 ** Function phPalEse_close
61 **
62 ** Description Closes PN547 device
63 **
64 ** Parameters pDevHandle - device handle
65 **
66 ** Returns None
67 **
68 *******************************************************************************/
phPalEse_close(void * pDevHandle)69 void phPalEse_close(void* pDevHandle) {
70 if (NULL != pDevHandle) {
71 gpTransportObj->Close(pDevHandle);
72 }
73 gpTransportObj = NULL;
74 return;
75 }
76
77 /*******************************************************************************
78 **
79 ** Function phPalEse_open_and_configure
80 **
81 ** Description Open and configure ESE device
82 **
83 ** Parameters pConfig - hardware information
84 **
85 ** Returns ESE status:
86 ** ESESTATUS_SUCCESS - open_and_configure operation
87 *success
88 ** ESESTATUS_INVALID_DEVICE - device open operation failure
89 **
90 *******************************************************************************/
phPalEse_open_and_configure(pphPalEse_Config_t pConfig)91 ESESTATUS phPalEse_open_and_configure(pphPalEse_Config_t pConfig) {
92 ESESTATUS status = ESESTATUS_FAILED;
93 if (ESESTATUS_SUCCESS != phPalEse_ConfigTransport()) return ESESTATUS_FAILED;
94 status = gpTransportObj->OpenAndConfigure(pConfig);
95 return status;
96 }
97
98 /*******************************************************************************
99 **
100 ** Function phPalEse_ConfigTransport
101 **
102 ** Description Configure Transport channel based on transport type provided
103 ** in config file
104 **
105 ** Returns ESESTATUS_SUCCESS If transport channel is configured
106 ** ESESTATUS_FAILED If transport channel configuration failed
107 **
108 *******************************************************************************/
phPalEse_ConfigTransport()109 ESESTATUS phPalEse_ConfigTransport() {
110 unsigned long transportType = UNKNOWN;
111
112 transportType = EseConfig::getUnsigned(NAME_NXP_TRANSPORT, UNKNOWN);
113 ALOGD("phPalEse_ConfigTransport transport type %ld", transportType);
114 gpTransportObj = transportFactory.getTransport((transportIntf)transportType);
115 if (gpTransportObj == nullptr) {
116 return ESESTATUS_FAILED;
117 }
118 return ESESTATUS_SUCCESS;
119 }
120
121 /*******************************************************************************
122 **
123 ** Function phPalEse_read
124 **
125 ** Description Reads requested number of bytes from pn547 device into given
126 *buffer
127 **
128 ** Parameters pDevHandle - valid device handle
129 ** pBuffer - buffer for read data
130 ** nNbBytesToRead - number of bytes requested to be read
131 **
132 ** Returns numRead - number of successfully read bytes
133 ** -1 - read operation failure
134 **
135 *******************************************************************************/
phPalEse_read(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)136 int phPalEse_read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead) {
137 int ret = -1;
138 ret = gpTransportObj->Read(pDevHandle, pBuffer, nNbBytesToRead);
139 return ret;
140 }
141
142 /*******************************************************************************
143 **
144 ** Function phPalEse_write
145 **
146 ** Description Writes requested number of bytes from given buffer into
147 *pn547 device
148 **
149 ** Parameters pDevHandle - valid device handle
150 ** pBuffer - buffer for read data
151 ** nNbBytesToWrite - number of bytes requested to be written
152 **
153 ** Returns numWrote - number of successfully written bytes
154 ** -1 - write operation failure
155 **
156 *******************************************************************************/
phPalEse_write(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToWrite)157 int phPalEse_write(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToWrite) {
158 int numWrote = 0;
159
160 if (NULL == pDevHandle) {
161 return -1;
162 }
163 numWrote = gpTransportObj->Write(pDevHandle, pBuffer, nNbBytesToWrite);
164 return numWrote;
165 }
166
167 /*******************************************************************************
168 **
169 ** Function phPalEse_ioctl
170 **
171 ** Description Exposed ioctl by p61 spi driver
172 **
173 ** Parameters pDevHandle - valid device handle
174 ** level - reset level
175 **
176 ** Returns 0 - ioctl operation success
177 ** -1 - ioctl operation failure
178 **
179 *******************************************************************************/
phPalEse_ioctl(phPalEse_ControlCode_t eControlCode,void * pDevHandle,long level)180 ESESTATUS phPalEse_ioctl(phPalEse_ControlCode_t eControlCode, void* pDevHandle,
181 long level) {
182 ESESTATUS ret = ESESTATUS_FAILED;
183 ALOGD_IF(ese_debug_enabled, "phPalEse_spi_ioctl(), ioctl %x , level %lx",
184 eControlCode, level);
185 if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
186 if (NULL == pDevHandle) {
187 return ESESTATUS_IOCTL_FAILED;
188 }
189 }
190 if (pDevHandle == NULL) {
191 phPalEse_ConfigTransport();
192 }
193 ret = gpTransportObj->Ioctl(eControlCode, pDevHandle, level);
194 if (pDevHandle == NULL) {
195 phPalEse_close(pDevHandle);
196 }
197
198 return ret;
199 }
200
201 /*******************************************************************************
202 **
203 ** Function phPalEse_print_packet
204 **
205 ** Description Print packet
206 **
207 ** Returns None
208 **
209 *******************************************************************************/
phPalEse_print_packet(const char * pString,const uint8_t * p_data,uint16_t len)210 void phPalEse_print_packet(const char* pString, const uint8_t* p_data,
211 uint16_t len) {
212 uint32_t i;
213 char print_buffer[len * 3 + 1];
214
215 memset(print_buffer, 0, sizeof(print_buffer));
216 for (i = 0; i < len; i++) {
217 snprintf(&print_buffer[i * 2], 3, "%02X", p_data[i]);
218 }
219 if (0 == memcmp(pString, "SEND", 0x04)) {
220 ALOGD_IF(ese_debug_enabled, "NxpEseDataX len = %3d > %s", len,
221 print_buffer);
222 } else if (0 == memcmp(pString, "RECV", 0x04)) {
223 ALOGD_IF(ese_debug_enabled, "NxpEseDataR len = %3d > %s", len,
224 print_buffer);
225 }
226
227 return;
228 }
229
230 /*******************************************************************************
231 **
232 ** Function phPalEse_sleep
233 **
234 ** Description This function suspends execution of the calling thread for
235 ** (at least) usec microseconds
236 **
237 ** Returns None
238 **
239 *******************************************************************************/
phPalEse_sleep(long usec)240 void phPalEse_sleep(long usec) {
241 usleep(usec);
242 return;
243 }
244
245 /*******************************************************************************
246 **
247 ** Function phPalEse_memset
248 **
249 ** Description
250 **
251 ** Returns None
252 **
253 *******************************************************************************/
254
phPalEse_memset(void * buff,int val,size_t len)255 void* phPalEse_memset(void* buff, int val, size_t len) {
256 return memset(buff, val, len);
257 }
258
259 /*******************************************************************************
260 **
261 ** Function phPalEse_memcpy
262 **
263 ** Description
264 **
265 ** Returns None
266 **
267 *******************************************************************************/
268
phPalEse_memcpy(void * dest,const void * src,size_t len)269 void* phPalEse_memcpy(void* dest, const void* src, size_t len) {
270 return memcpy(dest, src, len);
271 }
272
273 /*******************************************************************************
274 **
275 ** Function phPalEse_memalloc
276 **
277 ** Description
278 **
279 ** Returns None
280 **
281 *******************************************************************************/
282
phPalEse_memalloc(uint32_t size)283 void* phPalEse_memalloc(uint32_t size) { return malloc(size); }
284
285 /*******************************************************************************
286 **
287 ** Function phPalEse_calloc
288 **
289 ** Description
290 **
291 ** Returns None
292 **
293 *******************************************************************************/
294
phPalEse_calloc(size_t datatype,size_t size)295 void* phPalEse_calloc(size_t datatype, size_t size) {
296 return calloc(datatype, size);
297 }
298
299 /*******************************************************************************
300 **
301 ** Function phPalEse_free
302 **
303 ** Description
304 **
305 ** Returns None
306 **
307 *******************************************************************************/
phPalEse_free(void * ptr)308 void phPalEse_free(void* ptr) {
309 if (ptr != NULL) {
310 free(ptr);
311 ptr = NULL;
312 }
313 return;
314 }
315