1 /*
2  * Copyright 2010-2023 NXP
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 
17 /*
18  * Transport Mapping Layer header files containing APIs related to initializing,
19  * reading
20  * and writing data into files provided by the driver interface.
21  *
22  * API listed here encompasses Transport Mapping Layer interfaces required to be
23  * mapped
24  * to different Interfaces and Platforms.
25  *
26  */
27 
28 #ifndef PHTMLNFC_H
29 #define PHTMLNFC_H
30 
31 #include <phNfcCommon.h>
32 
33 /*
34  * Message posted by Reader thread upon
35  * completion of requested operation
36  */
37 #define PH_TMLNFC_READ_MESSAGE (0xAA)
38 
39 /*
40  * Message posted by Writer thread upon
41  * completion of requested operation
42  */
43 #define PH_TMLNFC_WRITE_MESSAGE (0x55)
44 
45 /*
46  * Value indicates to reset device
47  */
48 #define PH_TMLNFC_RESETDEVICE (0x00008001)
49 
50 /*
51  * The 4096 bytes fragment len is supported during SN300 FW DNLD.
52  * If this macro is not defined, then the fragment len will fallback to 554.
53  */
54 #define PH_TMLNFC_HDLL_4K_WRITE_SUPPORTED
55 /*
56  * Fragment Length for SNXXX and PN547
57  */
58 #define PH_TMLNFC_FRGMENT_SIZE_PN557 (0x102)
59 #define PH_TMLNFC_FRGMENT_SIZE_SNXXX (0x22A)
60 #ifdef PH_TMLNFC_HDLL_4K_WRITE_SUPPORTED
61 #define PH_TMLNFC_FRGMENT_SIZE_SN300 (0x1000)
62 #else
63 #define PH_TMLNFC_FRGMENT_SIZE_SN300 (0x22A)
64 #endif
65 /*
66 ***************************Globals,Structure and Enumeration ******************
67 */
68 
69 /*
70  * Transaction (Tx/Rx) completion information structure of TML
71  *
72  * This structure holds the completion callback information of the
73  * transaction passed from the TML layer to the Upper layer
74  * along with the completion callback.
75  *
76  * The value of field wStatus can be interpreted as:
77  *
78  *     - NFCSTATUS_SUCCESS                    Transaction performed
79  * successfully.
80  *     - NFCSTATUS_FAILED                     Failed to wait on Read/Write
81  * operation.
82  *     - NFCSTATUS_INSUFFICIENT_STORAGE       Not enough memory to store data in
83  * case of read.
84  *     - NFCSTATUS_BOARD_COMMUNICATION_ERROR  Failure to Read/Write from the
85  * file or timeout.
86  */
87 
88 typedef struct phTmlNfc_TransactInfo {
89   NFCSTATUS wStatus;       /* Status of the Transaction Completion*/
90   uint8_t* pBuff;          /* Response Data of the Transaction*/
91   uint16_t wLength;        /* Data size of the Transaction*/
92 } phTmlNfc_TransactInfo_t; /* Instance of Transaction structure */
93 
94 /*
95  * TML transreceive completion callback to Upper Layer
96  *
97  * pContext - Context provided by upper layer
98  * pInfo    - Transaction info. See phTmlNfc_TransactInfo
99  */
100 typedef void (*pphTmlNfc_TransactCompletionCb_t)(
101     void* pContext, phTmlNfc_TransactInfo_t* pInfo);
102 
103 /*
104  * TML Deferred callback interface structure invoked by upper layer
105  *
106  * This could be used for read/write operations
107  *
108  * dwMsgPostedThread Message source identifier
109  * pParams Parameters for the deferred call processing
110  */
111 typedef void (*pphTmlNfc_DeferFuncPointer_t)(uint32_t dwMsgPostedThread,
112                                              void* pParams);
113 
114 /*
115  * Enum definition contains  supported ioctl control codes.
116  *
117  * phTmlNfc_IoCtl
118  */
119 typedef enum {
120   phTmlNfc_e_Invalid = 0,
121   phTmlNfc_e_ResetDevice = PH_TMLNFC_RESETDEVICE, /* Reset the device */
122   phTmlNfc_e_EnableDownloadMode, /* Do the hardware setting to enter into
123                                     download mode */
124   phTmlNfc_e_EnableNormalMode, /* Hardware setting for normal mode of operation
125                                 */
126   phTmlNfc_e_EnableDownloadModeWithVenRst,
127   phTmlNfc_e_EnableVen, /* Enable Ven for PN557 chip*/
128   phTmlNfc_e_PowerReset = 5,
129   phTmlNfc_e_setFragmentSize,
130   phTmlNfc_e_SetNfcState,
131   phTmlNfc_e_ResetNfcState,
132   phTmlNfc_e_PullVenLow,
133   phTmlNfc_e_PullVenHigh,
134 } phTmlNfc_ControlCode_t; /* Control code for IOCTL call */
135 
136 /*
137  * Enable / Disable Re-Transmission of Packets
138  *
139  * phTmlNfc_ConfigNciPktReTx
140  */
141 typedef enum {
142   phTmlNfc_e_EnableRetrans = 0x00, /*Enable retransmission of Nci packet */
143   phTmlNfc_e_DisableRetrans = 0x01 /*Disable retransmission of Nci packet */
144 } phTmlNfc_ConfigRetrans_t;        /* Configuration for Retransmission */
145 
146 /*
147  * Structure containing details related to read and write operations
148  *
149  */
150 typedef struct phTmlNfc_ReadWriteInfo {
151   volatile uint8_t bEnable; /*This flag shall decide whether to perform
152                                Write/Read operation */
153   uint8_t
154       bThreadBusy; /*Flag to indicate thread is busy on respective operation */
155   /* Transaction completion Callback function */
156   pphTmlNfc_TransactCompletionCb_t pThread_Callback;
157   void* pContext;        /*Context passed while invocation of operation */
158   uint8_t* pBuffer;      /*Buffer passed while invocation of operation */
159   uint16_t wLength;      /*Length of data read/written */
160   NFCSTATUS wWorkStatus; /*Status of the transaction performed */
161 } phTmlNfc_ReadWriteInfo_t;
162 
163 /*
164  *Base Context Structure containing members required for entire session
165  */
166 typedef struct phTmlNfc_Context {
167   pthread_t readerThread; /*Handle to the thread which handles write and read
168                              operations */
169   pthread_t writerThread;
170   volatile uint8_t
171       bThreadDone; /*Flag to decide whether to run or abort the thread */
172   phTmlNfc_ConfigRetrans_t
173       eConfig;             /*Retransmission of Nci Packet during timeout */
174   uint8_t bRetryCount;     /*Number of times retransmission shall happen */
175   uint8_t bWriteCbInvoked; /* Indicates whether write callback is invoked during
176                               retransmission */
177   uint32_t dwTimerId;      /* Timer used to retransmit nci packet */
178   phTmlNfc_ReadWriteInfo_t tReadInfo;  /*Pointer to Reader Thread Structure */
179   phTmlNfc_ReadWriteInfo_t tWriteInfo; /*Pointer to Writer Thread Structure */
180   void* pDevHandle;                    /* Pointer to Device Handle */
181   uintptr_t dwCallbackThreadId; /* Thread ID to which message to be posted */
182   uint8_t bEnableCrc;           /*Flag to validate/not CRC for input buffer */
183   sem_t rxSemaphore;
184   sem_t txSemaphore;      /* Lock/Acquire txRx Semaphore */
185   sem_t postMsgSemaphore; /* Semaphore to post message atomically by Reader &
186                              writer thread */
187   pthread_cond_t wait_busy_condition; /*Condition to wait reader thread*/
188   pthread_mutex_t wait_busy_lock;     /*Condition lock to wait reader thread*/
189   volatile uint8_t wait_busy_flag;    /*Condition flag to wait reader thread*/
190   volatile uint8_t gWriterCbflag; /* flag to indicate write callback message is
191                                      pushed to queue*/
192   long nfc_service_pid; /*NFC Service PID to be used by driver to signal*/
193   uint16_t fragment_len;
194 } phTmlNfc_Context_t;
195 
196 /*
197  * TML Configuration exposed to upper layer.
198  */
199 typedef struct phTmlNfc_Config {
200   /* Port name connected to PN54X
201    *
202    * Platform specific canonical device name to which PN54X is connected.
203    *
204    * e.g. On Linux based systems this would be /dev/PN54X
205    */
206   int8_t* pDevName;
207   /* Callback Thread ID
208    *
209    * This is the thread ID on which the Reader & Writer thread posts message. */
210   uintptr_t dwGetMsgThreadId;
211   uint16_t fragment_len;
212 } phTmlNfc_Config_t, *pphTmlNfc_Config_t; /* pointer to phTmlNfc_Config_t */
213 
214 /*
215  * TML Deferred Callback structure used to invoke Upper layer Callback function.
216  */
217 typedef struct {
218   /* Deferred callback function to be invoked */
219   pphTmlNfc_DeferFuncPointer_t pDef_call;
220   /* Source identifier
221    *
222    * Identifier of the source which posted the message
223    */
224   uint32_t dwMsgPostedThread;
225   /** Actual Message
226    *
227    * This is passed as a parameter passed to the deferred callback function
228    * pDef_call. */
229   void* pParams;
230 } phTmlNfc_DeferMsg_t; /* DeferMsg structure passed to User Thread */
231 
232 typedef enum {
233   I2C_FRAGMENATATION_DISABLED, /*i2c fragmentation_disabled           */
234   I2C_FRAGMENTATION_ENABLED    /*i2c_fragmentation_enabled          */
235 } phTmlNfc_i2cfragmentation_t;
236 /* Function declarations */
237 NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig);
238 NFCSTATUS phTmlNfc_Shutdown(void);
239 NFCSTATUS phTmlNfc_Shutdown_CleanUp();
240 void phTmlNfc_CleanUp(void);
241 NFCSTATUS phTmlNfc_Write(uint8_t* pBuffer, uint16_t wLength,
242                          pphTmlNfc_TransactCompletionCb_t pTmlWriteComplete,
243                          void* pContext);
244 NFCSTATUS phTmlNfc_Read(uint8_t* pBuffer, uint16_t wLength,
245                         pphTmlNfc_TransactCompletionCb_t pTmlReadComplete,
246                         void* pContext);
247 NFCSTATUS phTmlNfc_WriteAbort(void);
248 NFCSTATUS phTmlNfc_ReadAbort(void);
249 NFCSTATUS phTmlNfc_IoCtl(phTmlNfc_ControlCode_t eControlCode);
250 void phTmlNfc_DeferredCall(uintptr_t dwThreadId,
251                            phLibNfc_Message_t* ptWorkerMsg);
252 void phTmlNfc_ConfigNciPktReTx(phTmlNfc_ConfigRetrans_t eConfig,
253                                uint8_t bRetryCount);
254 void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t enable);
255 NFCSTATUS phTmlNfc_ConfigTransport();
256 void phTmlNfc_EnableFwDnldMode(bool mode);
257 bool phTmlNfc_IsFwDnldModeEnabled(void);
258 #endif /*  PHTMLNFC_H  */
259