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
73typedef 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 */
85typedef 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 */
96typedef 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 */
104typedef 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 */
118typedef 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 */
127typedef 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 */
143typedef 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} phTmlNfc_Context_t;
165
166/*
167 * TML Configuration exposed to upper layer.
168 */
169typedef struct phTmlNfc_Config {
170  /* Port name connected to PN54X
171   *
172   * Platform specific canonical device name to which PN54X is connected.
173   *
174   * e.g. On Linux based systems this would be /dev/PN54X
175   */
176  int8_t* pDevName;
177  /* Callback Thread ID
178   *
179   * This is the thread ID on which the Reader & Writer thread posts message. */
180  uintptr_t dwGetMsgThreadId;
181  /* Communication speed between DH and PN54X
182   *
183   * This is the baudrate of the bus for communication between DH and PN54X */
184  uint32_t dwBaudRate;
185} phTmlNfc_Config_t, *pphTmlNfc_Config_t; /* pointer to phTmlNfc_Config_t */
186
187/*
188 * TML Deferred Callback structure used to invoke Upper layer Callback function.
189 */
190typedef struct {
191  /* Deferred callback function to be invoked */
192  pphTmlNfc_DeferFuncPointer_t pDef_call;
193  /* Source identifier
194   *
195   * Identifier of the source which posted the message
196   */
197  uint32_t dwMsgPostedThread;
198  /** Actual Message
199   *
200   * This is passed as a parameter passed to the deferred callback function
201   * pDef_call. */
202  void* pParams;
203} phTmlNfc_DeferMsg_t; /* DeferMsg structure passed to User Thread */
204
205typedef enum {
206  I2C_FRAGMENATATION_DISABLED, /*i2c fragmentation_disabled           */
207  I2C_FRAGMENTATION_ENABLED    /*i2c_fragmentation_enabled          */
208} phTmlNfc_i2cfragmentation_t;
209/* Function declarations */
210NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig);
211NFCSTATUS phTmlNfc_Shutdown(void);
212NFCSTATUS phTmlNfc_Write(uint8_t* pBuffer, uint16_t wLength,
213                         pphTmlNfc_TransactCompletionCb_t pTmlWriteComplete,
214                         void* pContext);
215NFCSTATUS phTmlNfc_Read(uint8_t* pBuffer, uint16_t wLength,
216                        pphTmlNfc_TransactCompletionCb_t pTmlReadComplete,
217                        void* pContext);
218NFCSTATUS phTmlNfc_WriteAbort(void);
219NFCSTATUS phTmlNfc_ReadAbort(void);
220NFCSTATUS phTmlNfc_IoCtl(phTmlNfc_ControlCode_t eControlCode);
221void phTmlNfc_DeferredCall(uintptr_t dwThreadId,
222                           phLibNfc_Message_t* ptWorkerMsg);
223void phTmlNfc_ConfigNciPktReTx(phTmlNfc_ConfigRetrans_t eConfig,
224                               uint8_t bRetryCount);
225void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t enable);
226phTmlNfc_i2cfragmentation_t phTmlNfc_get_fragmentation_enabled();
227#endif /*  PHTMLNFC_H  */
228