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, reading
19 * and writing data into files provided by the driver interface.
20 *
21 * API listed here encompasses Transport Mapping Layer interfaces required to be mapped
22 * to different Interfaces and Platforms.
23 *
24 */
25
26#ifndef PHTMLNFC_H
27#define PHTMLNFC_H
28
29#include <phNfcCommon.h>
30
31/*
32 * Message posted by Reader thread upon
33 * completion of requested operation
34 */
35#define PH_TMLNFC_READ_MESSAGE              (0xAA)
36
37/*
38 * Message posted by Writer thread upon
39 * completion of requested operation
40 */
41#define PH_TMLNFC_WRITE_MESSAGE             (0x55)
42
43/*
44 * Value indicates to reset device
45 */
46#define PH_TMLNFC_RESETDEVICE               (0x00008001)
47
48/*
49***************************Globals,Structure and Enumeration ******************
50*/
51
52/*
53 * Transaction (Tx/Rx) completion information structure of TML
54 *
55 * This structure holds the completion callback information of the
56 * transaction passed from the TML layer to the Upper layer
57 * along with the completion callback.
58 *
59 * The value of field wStatus can be interpreted as:
60 *
61 *     - NFCSTATUS_SUCCESS                    Transaction performed successfully.
62 *     - NFCSTATUS_FAILED                     Failed to wait on Read/Write operation.
63 *     - NFCSTATUS_INSUFFICIENT_STORAGE       Not enough memory to store data in case of read.
64 *     - NFCSTATUS_BOARD_COMMUNICATION_ERROR  Failure to Read/Write from the file or timeout.
65 */
66
67typedef struct phTmlNfc_TransactInfo
68{
69    NFCSTATUS           wStatus;    /* Status of the Transaction Completion*/
70    uint8_t             *pBuff;     /* Response Data of the Transaction*/
71    uint16_t            wLength;    /* Data size of the Transaction*/
72}phTmlNfc_TransactInfo_t;           /* Instance of Transaction structure */
73
74/*
75 * TML transreceive completion callback to Upper Layer
76 *
77 * pContext - Context provided by upper layer
78 * pInfo    - Transaction info. See phTmlNfc_TransactInfo
79 */
80typedef void (*pphTmlNfc_TransactCompletionCb_t) (void *pContext, phTmlNfc_TransactInfo_t *pInfo);
81
82/*
83 * TML Deferred callback interface structure invoked by upper layer
84 *
85 * This could be used for read/write operations
86 *
87 * dwMsgPostedThread Message source identifier
88 * pParams Parameters for the deferred call processing
89 */
90typedef  void (*pphTmlNfc_DeferFuncPointer_t) (uint32_t dwMsgPostedThread,void *pParams);
91
92/*
93 * Enum definition contains  supported ioctl control codes.
94 *
95 * phTmlNfc_IoCtl
96 */
97typedef enum
98{
99    phTmlNfc_e_Invalid = 0,
100    phTmlNfc_e_ResetDevice = PH_TMLNFC_RESETDEVICE, /* Reset the device */
101    phTmlNfc_e_EnableDownloadMode, /* Do the hardware setting to enter into download mode */
102    phTmlNfc_e_EnableNormalMode /* Hardware setting for normal mode of operation */
103} phTmlNfc_ControlCode_t ;  /* Control code for IOCTL call */
104
105/*
106 * Enable / Disable Re-Transmission of Packets
107 *
108 * phTmlNfc_ConfigNciPktReTx
109 */
110typedef enum
111{
112    phTmlNfc_e_EnableRetrans = 0x00, /*Enable retransmission of Nci packet */
113    phTmlNfc_e_DisableRetrans = 0x01 /*Disable retransmission of Nci packet */
114} phTmlNfc_ConfigRetrans_t ;  /* Configuration for Retransmission */
115
116/*
117 * Structure containing details related to read and write operations
118 *
119 */
120typedef struct phTmlNfc_ReadWriteInfo
121{
122    volatile uint8_t bEnable; /*This flag shall decide whether to perform Write/Read operation */
123    uint8_t bThreadBusy; /*Flag to indicate thread is busy on respective operation */
124    /* Transaction completion Callback function */
125    pphTmlNfc_TransactCompletionCb_t pThread_Callback;
126    void *pContext; /*Context passed while invocation of operation */
127    uint8_t *pBuffer; /*Buffer passed while invocation of operation */
128    uint16_t wLength; /*Length of data read/written */
129    NFCSTATUS wWorkStatus; /*Status of the transaction performed */
130} phTmlNfc_ReadWriteInfo_t;
131
132/*
133 *Base Context Structure containing members required for entire session
134 */
135typedef struct phTmlNfc_Context
136{
137    pthread_t readerThread; /*Handle to the thread which handles write and read operations */
138    pthread_t writerThread;
139    volatile uint8_t bThreadDone; /*Flag to decide whether to run or abort the thread */
140    phTmlNfc_ConfigRetrans_t eConfig; /*Retransmission of Nci Packet during timeout */
141    uint8_t bRetryCount; /*Number of times retransmission shall happen */
142    uint8_t bWriteCbInvoked; /* Indicates whether write callback is invoked during retransmission */
143    uint32_t dwTimerId; /* Timer used to retransmit nci packet */
144    phTmlNfc_ReadWriteInfo_t tReadInfo; /*Pointer to Reader Thread Structure */
145    phTmlNfc_ReadWriteInfo_t tWriteInfo; /*Pointer to Writer Thread Structure */
146    void *pDevHandle; /* Pointer to Device Handle */
147    uintptr_t dwCallbackThreadId; /* Thread ID to which message to be posted */
148    uint8_t bEnableCrc; /*Flag to validate/not CRC for input buffer */
149    sem_t   rxSemaphore;
150    sem_t   txSemaphore; /* Lock/Aquire txRx Semaphore */
151    sem_t   postMsgSemaphore; /* Semaphore to post message atomically by Reader & writer thread */
152} phTmlNfc_Context_t;
153
154/*
155 * TML Configuration exposed to upper layer.
156 */
157typedef struct phTmlNfc_Config
158{
159    /* Port name connected to PN54X
160     *
161     * Platform specific canonical device name to which PN54X is connected.
162     *
163     * e.g. On Linux based systems this would be /dev/PN54X
164     */
165    int8_t *pDevName;
166    /* Callback Thread ID
167     *
168     * This is the thread ID on which the Reader & Writer thread posts message. */
169    uintptr_t dwGetMsgThreadId;
170    /* Communication speed between DH and PN54X
171     *
172     * This is the baudrate of the bus for communication between DH and PN54X */
173    uint32_t dwBaudRate;
174} phTmlNfc_Config_t,*pphTmlNfc_Config_t;    /* pointer to phTmlNfc_Config_t */
175
176/*
177 * TML Deferred Callback structure used to invoke Upper layer Callback function.
178 */
179typedef struct {
180    pphTmlNfc_DeferFuncPointer_t pDef_call; /*Deferred callback function to be invoked */
181    /* Source identifier
182     *
183     * Identifier of the source which posted the message
184     */
185    uint32_t dwMsgPostedThread;
186    /** Actual Message
187     *
188     * This is passed as a parameter passed to the deferred callback function pDef_call. */
189    void* pParams;
190} phTmlNfc_DeferMsg_t;                      /* DeferMsg structure passed to User Thread */
191
192typedef enum
193{
194    I2C_FRAGMENATATION_DISABLED,     /*i2c fragmentation_disabled           */
195    I2C_FRAGMENTATION_ENABLED      /*i2c_fragmentation_enabled          */
196} phTmlNfc_i2cfragmentation_t;
197/* Function declarations */
198NFCSTATUS phTmlNfc_Init(pphTmlNfc_Config_t pConfig);
199NFCSTATUS phTmlNfc_Shutdown(void);
200NFCSTATUS phTmlNfc_Write(uint8_t *pBuffer, uint16_t wLength, pphTmlNfc_TransactCompletionCb_t pTmlWriteComplete,  void *pContext);
201NFCSTATUS phTmlNfc_Read(uint8_t *pBuffer, uint16_t wLength, pphTmlNfc_TransactCompletionCb_t pTmlReadComplete,  void *pContext);
202NFCSTATUS phTmlNfc_WriteAbort(void);
203NFCSTATUS phTmlNfc_ReadAbort(void);
204NFCSTATUS phTmlNfc_IoCtl(phTmlNfc_ControlCode_t eControlCode);
205void phTmlNfc_DeferredCall(uintptr_t dwThreadId, phLibNfc_Message_t *ptWorkerMsg);
206void phTmlNfc_ConfigNciPktReTx( phTmlNfc_ConfigRetrans_t eConfig, uint8_t bRetryCount);
207void phTmlNfc_set_fragmentation_enabled(phTmlNfc_i2cfragmentation_t enable);
208phTmlNfc_i2cfragmentation_t phTmlNfc_get_fragmentation_enabled();
209#endif /*  PHTMLNFC_H  */
210