1/*
2 * Copyright (C) 2010 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 * \file phLibNfc_Ioctl.c
19
20 * Project: NFC FRI 1.1
21 *
22 * $Date: Mon Mar  1 19:07:05 2010 $
23 * $Author: ing07385 $
24 * $Revision: 1.35 $
25 * $Aliases: NFC_FRI1.1_WK1008_SDK,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1007_SDK,NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $
26 *
27 */
28
29/*
30************************* Header Files ****************************************
31*/
32
33#include <phLibNfcStatus.h>
34#include <phLibNfc.h>
35#include <phHal4Nfc.h>
36#include <phOsalNfc.h>
37#include <phLibNfc_Internal.h>
38#include <phLibNfc_ndef_raw.h>
39#include <phLibNfc_initiator.h>
40#include <phLibNfc_discovery.h>
41#include <phLibNfc_ioctl.h>
42#include <phNfcStatus.h>
43
44/*
45*************************** Macro's  ****************************************
46*/
47
48#ifndef STATIC_DISABLE
49#define STATIC static
50#else
51#define STATIC
52#endif
53
54/*
55*************************** Global Variables **********************************
56*/
57
58STATIC phLibNfc_Ioctl_Cntx_t phLibNfc_Ioctl_Cntx;
59
60/*
61*************************** Static Function Declaration ***********************
62*/
63
64
65/* Response callback for Ioctl management */
66STATIC  void phLibNfc_Ioctl_Mgmt_CB(void            *context,
67                              phNfc_sData_t         *pOutData,
68                              NFCSTATUS             status );
69
70
71/** Response callback for UICC switch mode*/
72STATIC void phLibNfc_Switch_Swp_Mode_CB(
73                                void  *context,
74                                NFCSTATUS status
75                                );
76
77/*
78*************************** Function Definitions ******************************
79*/
80
81/**
82* The I/O Control function allows the caller to configure specific
83* functionality provided by the lower layer.Each feature is accessible
84* via a specific IOCTL Code.
85*/
86NFCSTATUS phLibNfc_Mgt_IoCtl    (void*                      pDalHandle,
87                                 uint16_t                   IoctlCode,
88                                 phNfc_sData_t*             pInParam,
89                                 phNfc_sData_t*             pOutParam,
90                                 pphLibNfc_IoctlCallback_t  pIoCtl_Rsp_cb,
91                                 void*                      pContext
92                                 )
93{
94    NFCSTATUS StatusCode=NFCSTATUS_INVALID_PARAMETER;
95
96
97    if((IoctlCode==0)||(NULL==pIoCtl_Rsp_cb) ||
98		(NULL==pContext) ||(NULL==pInParam)  ||
99		(NULL==pDalHandle))
100    {
101        StatusCode=NFCSTATUS_INVALID_PARAMETER;
102        return StatusCode;
103    }
104    if(IoctlCode!= NFC_FW_DOWNLOAD)
105    {
106        if(pOutParam == NULL)
107        {
108            StatusCode = NFCSTATUS_INVALID_PARAMETER;
109            return StatusCode;
110        }
111        if(( gpphLibContext == NULL) ||
112			(gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))
113        {
114            StatusCode = NFCSTATUS_NOT_INITIALISED;
115            return StatusCode;
116        }
117        else
118        {
119            if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
120            {
121                StatusCode = NFCSTATUS_SHUTDOWN;
122                return StatusCode;
123            }
124        }
125    }
126    phLibNfc_Ioctl_Cntx.CliRspCb =pIoCtl_Rsp_cb;
127    phLibNfc_Ioctl_Cntx.pCliCntx = pContext;
128    phLibNfc_Ioctl_Cntx.pOutParam = pOutParam;
129    phLibNfc_Ioctl_Cntx.IoctlCode = IoctlCode;
130    /* Process the IOCTL requests */
131    switch(IoctlCode)
132    {
133        case NFC_FW_DOWNLOAD:
134        {/* Set power status */
135            phLibNfc_Ioctl_Cntx.psHwReference = phOsalNfc_GetMemory((uint32_t)sizeof(phHal_sHwReference_t));
136            if(phLibNfc_Ioctl_Cntx.psHwReference == NULL)
137                return NFCSTATUS_FAILED;
138            (void)memset(phLibNfc_Ioctl_Cntx.psHwReference,0,sizeof(phHal_sHwReference_t));
139            phLibNfc_Ioctl_Cntx.psHwReference->p_board_driver = pDalHandle;
140
141            StatusCode = phHal4Nfc_Ioctl( phLibNfc_Ioctl_Cntx.psHwReference,
142                                          NFC_FW_DOWNLOAD,
143                                          pInParam,
144                                          pOutParam,
145                                          phLibNfc_Ioctl_Mgmt_CB,
146                                          &phLibNfc_Ioctl_Cntx );
147        }break;
148        case NFC_MEM_READ:
149        {
150           StatusCode = phHal4Nfc_Ioctl(gpphLibContext->psHwReference,
151                                         NFC_MEM_READ,
152                                         pInParam,
153                                         pOutParam,
154                                         phLibNfc_Ioctl_Mgmt_CB,
155                                         &phLibNfc_Ioctl_Cntx );
156
157        }break;
158        case NFC_MEM_WRITE:
159        {
160
161           StatusCode = phHal4Nfc_Ioctl( gpphLibContext->psHwReference,
162				                          NFC_MEM_WRITE,
163                                          pInParam,
164                                          pOutParam,
165                                          phLibNfc_Ioctl_Mgmt_CB,
166                                          &phLibNfc_Ioctl_Cntx );
167
168        }break;
169		case PHLIBNFC_ANTENNA_TEST:
170		{
171
172                StatusCode = phHal4Nfc_Ioctl( gpphLibContext->psHwReference,
173                                          PHLIBNFC_ANTENNA_TEST,
174                                          pInParam,
175                                          pOutParam,
176                                          phLibNfc_Ioctl_Mgmt_CB,
177                                          &phLibNfc_Ioctl_Cntx );
178
179		}break;
180		case PHLIBNFC_SWP_TEST:
181		{
182
183                StatusCode = phHal4Nfc_Ioctl( gpphLibContext->psHwReference,
184                                          PHLIBNFC_SWP_TEST,
185                                          pInParam,
186                                          pOutParam,
187                                          phLibNfc_Ioctl_Mgmt_CB,
188                                          &phLibNfc_Ioctl_Cntx );
189
190		}break;
191
192		case PHLIBNFC_PRBS_TEST:
193		{
194		        StatusCode = phHal4Nfc_Ioctl( gpphLibContext->psHwReference,
195                                          PHLIBNFC_PRBS_TEST,
196                                          pInParam,
197                                          pOutParam,
198                                          phLibNfc_Ioctl_Mgmt_CB,
199                                          &phLibNfc_Ioctl_Cntx );
200
201
202		}break;
203        case PHLIBNFC_SWITCH_SWP_MODE:
204		{
205            StatusCode = phHal4Nfc_Switch_Swp_Mode( gpphLibContext->psHwReference,
206                                          (phHal_eSWP_Mode_t)pInParam->buffer[0],
207                                          phLibNfc_Switch_Swp_Mode_CB,
208                                          &phLibNfc_Ioctl_Cntx
209                                          );
210
211
212		}break;
213        default :
214        {
215          /* don't do any thing*/
216        }break;
217
218    } /* End of IOCTL  switch */
219	if(StatusCode!=NFCSTATUS_PENDING)
220    {
221		StatusCode = NFCSTATUS_FAILED;
222    }
223	else
224	{
225		if(IoctlCode!= NFC_FW_DOWNLOAD)
226		{
227			gpphLibContext->status.GenCb_pending_status=TRUE;
228		}
229	}
230    return StatusCode;
231
232}   /* End of IOCTL handler function */
233
234
235
236STATIC  void phLibNfc_Ioctl_Mgmt_CB(void          *context,
237                              phNfc_sData_t *pOutData,
238                              NFCSTATUS      status )
239{
240    phLibNfc_Ioctl_Cntx_t *pIoctlCntx=NULL;
241    if(PHNFCSTATUS(status) == NFCSTATUS_FEATURE_NOT_SUPPORTED)
242    {
243        status = NFCSTATUS_FEATURE_NOT_SUPPORTED;
244    }
245    else if(PHNFCSTATUS(status)!=NFCSTATUS_SUCCESS)
246    {
247        status = NFCSTATUS_FAILED;
248    }
249	if(gpphLibContext!= NULL)
250	{
251		if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
252		{
253		    /*If shutdown called in between allow shutdown to happen*/
254			phLibNfc_Pending_Shutdown();
255			status = NFCSTATUS_SHUTDOWN;
256		}
257	}
258    pIoctlCntx= (phLibNfc_Ioctl_Cntx_t*)context;
259    if( pIoctlCntx !=NULL)
260    {
261        switch(pIoctlCntx->IoctlCode)
262        {
263            case NFC_FW_DOWNLOAD:
264            {
265                /*Release the hardware reference memory*/
266                phOsalNfc_FreeMemory(pIoctlCntx->psHwReference);
267            }break;
268            case NFC_MEM_READ:
269            {
270
271            }break;
272            case NFC_MEM_WRITE:
273            {
274
275            }break;
276
277			case PHLIBNFC_ANTENNA_TEST:
278            {
279
280            }break;
281			case PHLIBNFC_SWP_TEST:
282            {
283
284            }break;
285			case PHLIBNFC_PRBS_TEST:
286            {
287
288            }break;
289            default:
290            {
291            }
292        }
293        pIoctlCntx->CliRspCb(pIoctlCntx->pCliCntx,pOutData,status);
294		if(gpphLibContext!= NULL)
295		{
296			gpphLibContext->status.GenCb_pending_status=FALSE;
297		}
298    }
299}
300
301STATIC void phLibNfc_Switch_Swp_Mode_CB(
302                                void  *context,
303                                NFCSTATUS status
304                                )
305{
306    if(PHNFCSTATUS(status)!=NFCSTATUS_SUCCESS)
307    {
308        status = NFCSTATUS_FAILED;
309    }
310    if(gpphLibContext!= NULL)
311	{
312		if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)
313		{
314		    /*If shutdown called in between allow shutdown to happen*/
315			phLibNfc_Pending_Shutdown();
316			status = NFCSTATUS_SHUTDOWN;
317		}
318	}
319    if((NULL != context)&&(context == (void *)&phLibNfc_Ioctl_Cntx))
320    {
321        if(NULL != phLibNfc_Ioctl_Cntx.CliRspCb)
322        {
323            (*phLibNfc_Ioctl_Cntx.CliRspCb)(
324                phLibNfc_Ioctl_Cntx.pCliCntx,
325                phLibNfc_Ioctl_Cntx.pOutParam,
326                status
327                );
328        }
329    }
330    return;
331}
332
333