1 /******************************************************************************
2  *
3  *  Copyright 2018-2021 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  * \addtogroup eSe_PAL
21  * \brief PAL implementation
22  * @{ */
23 
24 #ifndef _PHNXPESE_PAL_H
25 #define _PHNXPESE_PAL_H
26 
27 /* Basic type definitions */
28 #include <errno.h>
29 #include <phEseStatus.h>
30 #include <phNxpEseFeatures.h>
31 #include <stdint.h>
32 /*!
33  * \brief Value indicates to reset device
34  */
35 #define PH_PALESE_RESETDEVICE (0x00008001)
36 
37 /*!
38  * \ingroup eSe_PAL
39  *
40  * \brief Enum definition contains supported ioctl control codes.
41  *
42  * phPalEse_IoCtl
43  */
44 typedef enum {
45   phPalEse_e_Invalid = 0,                         /*!< Invalid control code */
46   phPalEse_e_ResetDevice = PH_PALESE_RESETDEVICE, /*!< Reset the device */
47   phPalEse_e_EnableLog,      /*!< Enable the spi driver logs */
48   phPalEse_e_EnablePollMode, /*!< Enable the polling for SPI */
49   phPalEse_e_GetEseAccess,   /*!< get the bus access in specified timeout */
50   phPalEse_e_ChipRst,        /*!< eSE Chip reset using ISO RST pin*/
51   phPalEse_e_EnableThroughputMeasurement, /*!< Enable throughput measurement */
52   phPalEse_e_SetPowerScheme,              /*!< Set power scheme */
53   phPalEse_e_GetSPMStatus,                /*!< Get SPM(power mgt) status */
54   phPalEse_e_DisablePwrCntrl,
55   phPalEse_e_SetJcopDwnldState,    /*!< Set Jcop Download state */
56   phPalEse_e_SetClientUpdateState, /*!< Set Jcop Download state */
57   phPalEse_e_SetSecureMode,        /*!< Set the Trusted SE Mode */
58   phPalEse_e_ResetProtection,
59 } phPalEse_ControlCode_t; /*!< Control code for IOCTL call */
60 
61 /*!
62  * \ingroup eSe_PAL
63  *
64  * \brief PAL Configuration exposed to upper layer.
65  */
66 typedef struct phPalEse_Config {
67   int8_t* pDevName;
68   /*!< Port name connected to ESE
69    *
70    * Platform specific canonical device name to which ESE is connected.
71    *
72    * e.g. On Linux based systems this would be /dev/p73
73    */
74 
75   uint32_t dwBaudRate;
76   /*!< Communication speed between DH and ESE
77    *
78    * This is the baudrate of the bus for communication between DH and ESE
79    */
80 
81   void* pDevHandle;
82   /*!< Device handle output */
83 } phPalEse_Config_t, *pphPalEse_Config_t; /* pointer to phPalEse_Config_t */
84 
85 /* Function declarations */
86 /**
87  * \ingroup eSe_PAL
88  * \brief This function is used to close the ESE device
89  *
90  * \retval None
91  *
92  */
93 void phPalEse_close(void* pDevHandle);
94 
95 /**
96  * \ingroup eSe_PAL
97  * \brief Open and configure ESE device
98  *
99  * \param[in]       pConfig: Config to open the device
100  *
101  * \retval  ESESTATUS On Success ESESTATUS_SUCCESS else proper error code
102  *
103  */
104 ESESTATUS phPalEse_open_and_configure(pphPalEse_Config_t pConfig);
105 
106 /**
107  * \ingroup eSe_PAL
108  * \brief ConfigTransport to get SPI terminal
109  *
110  * \retval  ESESTATUS On Success ESESTATUS_SUCCESS else proper error code
111  *
112  */
113 ESESTATUS phPalEse_ConfigTransport();
114 
115 /**
116  * \ingroup eSe_PAL
117  * \brief Reads requested number of bytes from ESE into given buffer
118  *
119  * \param[in]    pDevHandle       - valid device handle
120  **\param[in]    pBuffer           - buffer for read data
121  **\param[in]    nNbBytesToRead    - number of bytes requested to be read
122  *
123  * \retval   numRead      - number of successfully read bytes.
124  * \retval      -1             - read operation failure
125  *
126  */
127 int phPalEse_read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead);
128 
129 /**
130  * \ingroup eSe_PAL
131  * \brief Writes requested number of bytes from given buffer into pn547 device
132  *
133  * \param[in]    pDevHandle                 - valid device handle
134  * \param[in]    pBuffer                    - buffer to write
135  * \param[in]    nNbBytesToWrite            - number of bytes to write
136  *
137  * \retval  numWrote   - number of successfully written bytes
138  * \retval      -1         - write operation failure
139  *
140  */
141 int phPalEse_write(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToWrite);
142 
143 /**
144  * \ingroup eSe_PAL
145  * \brief Exposed ioctl by ESE driver
146  *
147  * \param[in]    eControlCode       - phPalEse_ControlCode_t for the respective
148  *configs
149  * \param[in]    pDevHandle         - valid device handle
150  * \param[in]    level              - reset level
151  *
152  * \retval    0   - ioctl operation success
153  * \retval   -1  - ioctl operation failure
154  *
155  */
156 ESESTATUS phPalEse_ioctl(phPalEse_ControlCode_t eControlCode, void* pDevHandle,
157                          long level);
158 
159 /**
160  * \ingroup eSe_PAL
161  * \brief Print packet data
162  *
163  * \param[in]    pString            - String to be printed
164  * \param[in]    p_data             - data to be printed
165  * \param[in]    len                - Length of data to be printed
166  *
167  * \retval   void
168  *
169  */
170 void phPalEse_print_packet(const char* pString, const uint8_t* p_data,
171                            uint16_t len);
172 
173 /**
174  * \ingroup eSe_PAL
175  * \brief This function  suspends execution of the calling thread for
176  *                  (at least) usec microseconds
177  *
178  * \param[in]    usec                - number of micro seconds to sleep
179  *
180  * \retval   void
181  *
182  */
183 void phPalEse_sleep(long usec);
184 
185 /**
186  * \ingroup eSe_PAL
187  * \brief This function updates destination buffer with val
188  *                 data in len size
189  *
190  * \param[in]    buff                - Array to be updated
191  * \param[in]    val                 - value to be updated
192  * \param[in]    len                 - length of array to be updated
193  *
194  * \retval   void
195  *
196  */
197 void* phPalEse_memset(void* buff, int val, size_t len);
198 
199 /**
200  * \ingroup eSe_PAL
201  * \brief This function copies source buffer to  destination buffer
202  *                 data in len size
203  *
204  * \param[in]    dest                - Destination array to be updated
205  * \param[in]    src                 - Source array to be updated
206  * \param[in]    len                 - length of array to be updated
207  *
208  * \retval   void
209  *
210  */
211 void* phPalEse_memcpy(void* dest, const void* src, size_t len);
212 
213 /**
214  * \ingroup eSe_PAL
215  * \brief This is utility function for runtime heap memory allocation
216  *
217  * \param[in]    size                 - number of bytes to be allocated
218  *
219  * \retval   void
220  *
221  */
222 void* phPalEse_memalloc(uint32_t size);
223 
224 /**
225  * \ingroup eSe_PAL
226  * \brief This is utility function for runtime heap memory allocation
227  *
228  * \param[in]    dataType                 - type of data
229  * \param[in]    size                         - number of bytes to be allocated
230  * \retval   void
231  *
232  */
233 void* phPalEse_calloc(size_t dataType, size_t size);
234 
235 /**
236  * \ingroup eSe_PAL
237  * \brief This is utility function for freeeing heap memory allocated
238  *
239  * \param[in]    ptr                 - Address pointer to previous allocation
240  *
241  * \retval   void
242  *
243  */
244 void phPalEse_free(void* ptr);
245 
246 /** @} */
247 #endif /*  _PHNXPESE_PAL_H    */
248