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