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