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