1 /*
2  * Copyright (C) 2010-2014 NXP Semiconductors
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_ControlCode_t;     /* Control code for IOCTL call */
112 
113 /*
114  * Enable / Disable Re-Transmission of Packets
115  *
116  * phTmlNfc_ConfigNciPktReTx
117  */
118 typedef enum {
119   phTmlNfc_e_EnableRetrans = 0x00, /*Enable retransmission of Nci packet */
120   phTmlNfc_e_DisableRetrans = 0x01 /*Disable retransmission of Nci packet */
121 } phTmlNfc_ConfigRetrans_t;        /* Configuration for Retransmission */
122 
123 /*
124  * Structure containing details related to read and write operations
125  *
126  */
127 typedef struct phTmlNfc_ReadWriteInfo {
128   volatile uint8_t bEnable; /*This flag shall decide whether to perform
129                                Write/Read operation */
130   uint8_t
131       bThreadBusy; /*Flag to indicate thread is busy on respective operation */
132   /* Transaction completion Callback function */
133   pphTmlNfc_TransactCompletionCb_t pThread_Callback;
134   void* pContext;        /*Context passed while invocation of operation */
135   uint8_t* pBuffer;      /*Buffer passed while invocation of operation */
136   uint16_t wLength;      /*Length of data read/written */
137   NFCSTATUS wWorkStatus; /*Status of the transaction performed */
138 } phTmlNfc_ReadWriteInfo_t;
139 
140 /*
141  *Base Context Structure containing members required for entire session
142  */
143 typedef struct phTmlNfc_Context {
144   pthread_t readerThread; /*Handle to the thread which handles write and read
145                              operations */
146   pthread_t writerThread;
147   volatile uint8_t
148       bThreadDone; /*Flag to decide whether to run or abort the thread */
149   phTmlNfc_ConfigRetrans_t
150       eConfig;             /*Retransmission of Nci Packet during timeout */
151   uint8_t bRetryCount;     /*Number of times retransmission shall happen */
152   uint8_t bWriteCbInvoked; /* Indicates whether write callback is invoked during
153                               retransmission */
154   uint32_t dwTimerId;      /* Timer used to retransmit nci packet */
155   phTmlNfc_ReadWriteInfo_t tReadInfo;  /*Pointer to Reader Thread Structure */
156   phTmlNfc_ReadWriteInfo_t tWriteInfo; /*Pointer to Writer Thread Structure */
157   void* pDevHandle;                    /* Pointer to Device Handle */
158   uintptr_t dwCallbackThreadId; /* Thread ID to which message to be posted */
159   uint8_t bEnableCrc;           /*Flag to validate/not CRC for input buffer */
160   sem_t rxSemaphore;
161   sem_t txSemaphore;      /* Lock/Aquire txRx Semaphore */
162   sem_t postMsgSemaphore; /* Semaphore to post message atomically by Reader &
163                              writer thread */
164   pthread_mutex_t readInfoUpdateMutex; /*Mutex to synchronize read Info update*/
165 } phTmlNfc_Context_t;
166 
167 /*
168  * TML Configuration exposed to upper layer.
169  */
170 typedef struct phTmlNfc_Config {
171   /* Port name connected to PN54X
172    *
173    * Platform specific canonical device name to which PN54X is connected.
174    *
175    * e.g. On Linux based systems this would be /dev/PN54X
176    */
177   int8_t* pDevName;
178   /* Callback Thread ID
179    *
180    * This is the thread ID on which the Reader & Writer thread posts message. */
181   uintptr_t dwGetMsgThreadId;
182   /* Communication speed between DH and PN54X
183    *
184    * This is the baudrate of the bus for communication between DH and PN54X */
185   uint32_t dwBaudRate;
186 } phTmlNfc_Config_t, *pphTmlNfc_Config_t; /* pointer to phTmlNfc_Config_t */
187 
188 /*
189  * TML Deferred Callback structure used to invoke Upper layer Callback function.
190  */
191 typedef struct {
192   /* Deferred callback function to be invoked */
193   pphTmlNfc_DeferFuncPointer_t pDef_call;
194   /* Source identifier
195    *
196    * Identifier of the source which posted the message
197    */
198   uint32_t dwMsgPostedThread;
199   /** Actual Message
200    *
201    * This is passed as a parameter passed to the deferred callback function
202    * pDef_call. */
203   void* pParams;
204 } phTmlNfc_DeferMsg_t; /* DeferMsg structure passed to User Thread */
205 
206 typedef enum {
207   I2C_FRAGMENATATION_DISABLED, /*i2c fragmentation_disabled           */
208   I2C_FRAGMENTATION_ENABLED    /*i2c_fragmentation_enabled          */
209 } phTmlNfc_i2cfragmentation_t;
210 /* Function declarations */
211 NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig);
212 NFCSTATUS phTmlNfc_Shutdown(void);
213 NFCSTATUS phTmlNfc_Shutdown_CleanUp();
214 void phTmlNfc_CleanUp(void);
215 NFCSTATUS phTmlNfc_Write(uint8_t* pBuffer, uint16_t wLength,
216                          pphTmlNfc_TransactCompletionCb_t pTmlWriteComplete,
217                          void* pContext);
218 NFCSTATUS phTmlNfc_Read(uint8_t* pBuffer, uint16_t wLength,
219                         pphTmlNfc_TransactCompletionCb_t pTmlReadComplete,
220                         void* pContext);
221 NFCSTATUS phTmlNfc_WriteAbort(void);
222 NFCSTATUS phTmlNfc_ReadAbort(void);
223 NFCSTATUS phTmlNfc_IoCtl(phTmlNfc_ControlCode_t eControlCode);
224 NFCSTATUS phTmlNfc_UpdateReadCompleteCallback (
225     pphTmlNfc_TransactCompletionCb_t pTmlReadComplete);
226 void phTmlNfc_DeferredCall(uintptr_t dwThreadId,
227                            phLibNfc_Message_t* ptWorkerMsg);
228 void phTmlNfc_ConfigNciPktReTx(phTmlNfc_ConfigRetrans_t eConfig,
229                                uint8_t bRetryCount);
230 void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t enable);
231 phTmlNfc_i2cfragmentation_t phTmlNfc_get_fragmentation_enabled();
232 #endif /*  PHTMLNFC_H  */
233