1 /******************************************************************************
2  *
3  *  Copyright 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 #pragma once
20 #include <NfccTransport.h>
21 
22 #define PN544_MAGIC 0xE9
23 /*
24  * PN544 power control via ioctl
25  * PN544_SET_PWR(0): power off
26  * PN544_SET_PWR(1): power on
27  * PN544_SET_PWR(2): reset and power on with firmware download enabled
28  */
29 #define PN544_SET_PWR _IOW(PN544_MAGIC, 0x01, long)
30 /*
31  * 1. SPI Request NFCC to enable p61 power, only in param
32  *   Only for SPI
33  *   level 1 = Enable power
34  *   level 0 = Disable power
35  * 2. NFC Request the eSE cold reset, only with MODE_ESE_COLD_RESET
36  */
37 #define ESE_SET_PWR _IOW(PN544_MAGIC, 0x02, long)
38 
39 /*
40  * SPI or DWP can call this ioctl to get the current
41  * power state of P61
42  */
43 #define ESE_GET_PWR _IOR(PN544_MAGIC, 0x03, long)
44 
45 /*
46  * get platform interface type(i2c or i3c) for common MW
47  * return 0 - i2c, 1 - i3c
48  */
49 #define P544_GET_PLATFORM_INTERFACE _IO(PN544_MAGIC, 0x04)
50 /*
51  * get boot state
52  * return unknown, fw dwl, fw teared, nci
53  */
54 #define P544_GET_NFC_STATE _IO(PN544_MAGIC, 0x05)
55 
56 /* NFC HAL can call this ioctl to get the current IRQ state */
57 #define PN544_GET_IRQ_STATE _IOW(PN544_MAGIC, 0x06, long)
58 
59 extern phTmlNfc_i2cfragmentation_t fragmentation_enabled;
60 
61 class NfccI2cTransport : public NfccTransport {
62  private:
63   bool_t bFwDnldFlag = false;
64   sem_t mTxRxSemaphore;
65   /*****************************************************************************
66    **
67    ** Function         SemTimedWait
68    **
69    ** Description      Timed sem_wait for avoiding i2c_read & write overlap
70    **
71    ** Parameters       none
72    **
73    ** Returns          Sem_wait return status
74    ****************************************************************************/
75   int SemTimedWait();
76 
77   /*****************************************************************************
78    **
79    ** Function         SemPost
80    **
81    ** Description      sem_post 2c_read / write
82    **
83    ** Parameters       none
84    **
85    ** Returns          none
86    ****************************************************************************/
87   void SemPost();
88 
89   int Flushdata(void* pDevHandle, uint8_t* pBuffer, int numRead);
90 
91  public:
92   /*****************************************************************************
93   **
94   ** Function         Close
95   **
96   ** Description      Closes PN54X device
97   **
98   ** Parameters       pDevHandle - device handle
99   **
100   ** Returns          None
101   **
102   *****************************************************************************/
103   void Close(void* pDevHandle);
104 
105   /*****************************************************************************
106    **
107    ** Function         OpenAndConfigure
108    **
109    ** Description      Open and configure PN54X device
110    **
111    ** Parameters       pConfig     - hardware information
112    **                  pLinkHandle - device handle
113    **
114    ** Returns          NFC status:
115    **                  NFCSTATUS_SUCCESS - open_and_configure operation success
116    **                  NFCSTATUS_INVALID_DEVICE - device open operation failure
117    **
118    ****************************************************************************/
119   NFCSTATUS OpenAndConfigure(pphTmlNfc_Config_t pConfig, void** pLinkHandle);
120 
121   /*****************************************************************************
122    **
123    ** Function         Read
124    **
125    ** Description      Reads requested number of bytes from PN54X device into
126    *given
127    **                  buffer
128    **
129    ** Parameters       pDevHandle       - valid device handle
130    **                  pBuffer          - buffer for read data
131    **                  nNbBytesToRead   - number of bytes requested to be read
132    **
133    ** Returns          numRead   - number of successfully read bytes
134    **                  -1        - read operation failure
135    **
136    ****************************************************************************/
137   int Read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead);
138 
139   /*****************************************************************************
140   **
141   ** Function         Write
142   **
143   ** Description      Writes requested number of bytes from given buffer into
144   **                  PN54X device
145   **
146   ** Parameters       pDevHandle       - valid device handle
147   **                  pBuffer          - buffer for read data
148   **                  nNbBytesToWrite  - number of bytes requested to be written
149   **
150   ** Returns          numWrote   - number of successfully written bytes
151   **                  -1         - write operation failure
152   **
153   *****************************************************************************/
154   int Write(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToWrite);
155 
156   /*****************************************************************************
157    **
158    ** Function         Reset
159    **
160    ** Description      Reset PN54X device, using VEN pin
161    **
162    ** Parameters       pDevHandle     - valid device handle
163    **                  level          - reset level
164    **
165    ** Returns           0   - reset operation success
166    **                  -1   - reset operation failure
167    **
168    ****************************************************************************/
169   int NfccReset(void* pDevHandle, NfccResetType eType);
170 
171   /*****************************************************************************
172    **
173    ** Function         EseReset
174    **
175    ** Description      Request NFCC to reset the eSE
176    **
177    ** Parameters       pDevHandle     - valid device handle
178    **                  eType          - EseResetType
179    **
180    ** Returns           0   - reset operation success
181    **                  else - reset operation failure
182    **
183    ****************************************************************************/
184   int EseReset(void* pDevHandle, EseResetType eType);
185 
186   /*****************************************************************************
187    **
188    ** Function         EseGetPower
189    **
190    ** Description      Request NFCC to reset the eSE
191    **
192    ** Parameters       pDevHandle     - valid device handle
193    **                  level          - reset level
194    **
195    ** Returns           0   - reset operation success
196    **                  else - reset operation failure
197    **
198    ****************************************************************************/
199   int EseGetPower(void* pDevHandle, long level);
200 
201   /*****************************************************************************
202    **
203    ** Function         GetPlatform
204    **
205    ** Description      Get platform interface type (i2c or i3c) for common mw
206    **
207    ** Parameters       pDevHandle     - valid device handle
208    **
209    ** Returns           0   - i2c
210    **                   1   - i3c
211    **
212    ****************************************************************************/
213   int GetPlatform(void* pDevHandle);
214 
215   /*****************************************************************************
216   **
217   ** Function         GetNfcState
218   **
219   ** Description      Get Nfc state
220   **
221   ** Parameters       pDevHandle     - valid device handle
222   ** Returns           0   - unknown
223   **                   1   - FW DWL
224   **                   2   - NCI
225   **
226   *****************************************************************************/
227   int GetNfcState(void* pDevHandle);
228 
229   /*****************************************************************************
230    **
231    ** Function         EnableFwDnldMode
232    **
233    ** Description      updates the state to Download mode
234    **
235    ** Parameters       True/False
236    **
237    ** Returns          None
238    ****************************************************************************/
239   void EnableFwDnldMode(bool mode);
240 
241   /*****************************************************************************
242    **
243    ** Function         IsFwDnldModeEnabled
244    **
245    ** Description      Returns the current mode
246    **
247    ** Parameters       none
248    **
249    ** Returns           Current mode download/NCI
250    ****************************************************************************/
251   bool_t IsFwDnldModeEnabled(void);
252 
253   /*******************************************************************************
254    **
255    ** Function         GetIrqState
256    **
257    ** Description      Get state of IRQ GPIO
258    **
259    ** Parameters       pDevHandle - valid device handle
260    **
261    ** Returns          The state of IRQ line i.e. +ve if read is pending else
262    *Zer0.
263    **                  In the case of IOCTL error, it returns -ve value.
264    **
265    *******************************************************************************/
266   int GetIrqState(void* pDevHandle);
267 };
268