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 * \file  phHal4Nfc.c
18 * \brief Hal4Nfc source.
19 *
20 * Project: NFC-FRI 1.1
21 *
22 * $Date: Fri Jun 11 09:32:23 2010 $
23 * $Author: ing07385 $
24 * $Revision: 1.192 $
25 * $Aliases: NFC_FRI1.1_WK1023_R35_1 $
26 *
27 */
28
29/* ---------------------------Include files ---------------------------------*/
30
31#include <phHal4Nfc.h>
32#include <phHal4Nfc_Internal.h>
33#include <phOsalNfc.h>
34#include <phHciNfc.h>
35#include <phLlcNfc.h>
36#include <phDal4Nfc.h>
37#include <phDnldNfc.h>
38#include <phOsalNfc_Timer.h>
39
40/* ------------------------------- Macros -----------------------------------*/
41#ifndef HAL_UNIT_TEST
42#define STATIC static
43#else
44#define STATIC
45#endif/*#ifndef UNIT_TEST*/
46#define HAL4_LAYERS                 3
47#define LAYER_HCI                   2
48#define LAYER_LLC                   1
49#define LAYER_DAL                   0
50
51/* --------------------Structures and enumerations --------------------------*/
52
53phHal_sHwReference_t *gpphHal4Nfc_Hwref;
54
55static void phHal4Nfc_IoctlComplete(
56                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
57                                    void     *pInfo
58                                    );
59
60static void phHal4Nfc_LowerNotificationHandler(
61                                        void    *pContext,
62                                        void    *pHwRef,
63                                        uint8_t  type,
64                                        void     *pInfo
65                                        );
66static void phHal4Nfc_HandleEvent(
67                              phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
68                              void     *pInfo
69                              );
70
71static void phHal4Nfc_OpenComplete(
72                                   phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
73                                   void *pInfo
74                                   );
75
76static void phHal4Nfc_CloseComplete(
77                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
78                                    void *pInfo
79                                    );
80
81static void phHal4Nfc_DownloadComplete(
82                                void *pContext,
83                                void *pHwRef,
84                                uint8_t type,
85                                void *pInfo
86                                );
87
88static NFCSTATUS phHal4Nfc_Configure_Layers(
89                                phNfcLayer_sCfg_t       **pphLayer
90                                );
91
92
93/*Callback for Self tests*/
94static void phHal4Nfc_SelfTestComplete(
95                                       phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
96                                       void *pInfo
97                                       );
98
99/**
100 *  The open callback function to be called by the HCI when open (initializaion)
101 *  sequence is completed  or if there is an error in initialization.
102 *  It is passed as a parameter to HCI when calling HCI Init.
103 */
104
105static void phHal4Nfc_OpenComplete(
106                                   phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
107                                   void *pInfo
108                                   )
109{
110    NFCSTATUS status = ((phNfc_sCompletionInfo_t *)pInfo)->status;
111    pphHal4Nfc_GenCallback_t pUpper_OpenCb
112                                    = Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb;
113    void                   *pUpper_Context
114                                = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
115    if(status == NFCSTATUS_SUCCESS)
116    {
117        PHDBG_INFO("Hal4:Open Successful");
118#ifdef MERGE_SAK_SW1 /*Software Workaround*/
119        if(eHal4StateOpenAndReady == Hal4Ctxt->Hal4NextState)
120        {
121            status = phHciNfc_System_Configure (
122                                    Hal4Ctxt->psHciHandle,
123                                    (void *)gpphHal4Nfc_Hwref,
124                                    PH_HAL4NFC_TGT_MERGE_ADDRESS,
125                                    PH_HAL4NFC_TGT_MERGE_SAK /*config value*/
126                                    );
127        }
128        if(NFCSTATUS_PENDING != status)
129#endif/*#ifdef MERGE_SAK_SW1*/
130        {
131            /*Update State*/
132            Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
133            Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
134            Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb = NULL;
135            if(NULL != pUpper_OpenCb)
136            {
137                /*Upper layer's Open Cb*/
138                (*pUpper_OpenCb)(Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
139                    NFCSTATUS_SUCCESS
140                    );
141            }
142        }
143    }
144    else/*Open did not succeed.Go back to reset state*/
145    {
146        Hal4Ctxt->Hal4CurrentState = eHal4StateClosed;
147        Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
148        Hal4Ctxt->psHciHandle = NULL;
149        phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
150        Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
151        phOsalNfc_FreeMemory((void *)Hal4Ctxt);
152        gpphHal4Nfc_Hwref->hal_context = NULL;
153        gpphHal4Nfc_Hwref = NULL;
154        PHDBG_INFO("Hal4:Open Failed");
155        /*Call upper layer's Open Cb with error status*/
156        (*pUpper_OpenCb)(pUpper_Context,status);
157    }
158    return;
159}
160
161/**
162 *  The close callback function called by the HCI when close  sequence is
163 *  completed or if there is an error in closing.
164 *  It is passed as a parameter to HCI when calling HCI Release.
165 */
166static void phHal4Nfc_CloseComplete(
167                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
168                                    void *pInfo
169                                    )
170{
171    NFCSTATUS   status= ((phNfc_sCompletionInfo_t *)pInfo)->status;
172    pphHal4Nfc_GenCallback_t pUpper_CloseCb;
173    void                    *pUpper_Context;
174    uint8_t                 RemoteDevNumber = 0;
175    pUpper_CloseCb = Hal4Ctxt->sUpperLayerInfo.pUpperCloseCb;
176    pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
177    /*Update state*/
178    Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
179    Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
180    /*If Closed successfully*/
181    if(NFCSTATUS_SUCCESS == status)
182    {
183        Hal4Ctxt->psHciHandle = NULL;
184        /*Free all heap allocations*/
185        phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
186        Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
187        /*Free ADD context info*/
188        if(NULL != Hal4Ctxt->psADDCtxtInfo)
189        {
190            while(RemoteDevNumber < MAX_REMOTE_DEVICES)
191            {
192                if(NULL != Hal4Ctxt->rem_dev_list[RemoteDevNumber])
193                {
194                    phOsalNfc_FreeMemory((void *)
195                                    (Hal4Ctxt->rem_dev_list[RemoteDevNumber]));
196                    Hal4Ctxt->rem_dev_list[RemoteDevNumber] = NULL;
197                }
198                RemoteDevNumber++;
199            }
200            Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
201            phOsalNfc_FreeMemory(Hal4Ctxt->psADDCtxtInfo);
202        }/*if(NULL != Hal4Ctxt->psADDCtxtInfo)*/
203        /*Free Trcv context info*/
204        if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
205        {
206            if(NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
207            {
208                    phOsalNfc_FreeMemory(
209                        Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer
210                        );
211            }
212            if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
213                && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
214            {
215                phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
216            }
217            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
218        }/*if(NULL != Hal4Ctxt->psTrcvCtxtInfo)*/
219        /*Free Hal context and Hardware reference*/
220        gpphHal4Nfc_Hwref->hal_context = NULL;
221        gpphHal4Nfc_Hwref = NULL;
222        phOsalNfc_FreeMemory((void *)Hal4Ctxt);
223    }/* if(NFCSTATUS_SUCCESS == status)*/
224    /*Call Upper layer's Close Cb with status*/
225    (*pUpper_CloseCb)(pUpper_Context,status);
226    return;
227}
228
229
230/*
231* For configuring the various layers during the Initialization call
232*
233*
234*/
235static
236NFCSTATUS
237phHal4Nfc_Configure_Layers(
238                        phNfcLayer_sCfg_t       **pphLayer
239                        )
240{
241    uint8_t index = HAL4_LAYERS - 1;
242    uint8_t i = 0;
243    NFCSTATUS status = NFCSTATUS_SUCCESS ;
244    PHDBG_INFO("Hal4:Configuring layers");
245    *pphLayer = (phNfcLayer_sCfg_t *) phOsalNfc_GetMemory(
246                            sizeof(phNfcLayer_sCfg_t) * HAL4_LAYERS);
247
248    if( NULL == *pphLayer)
249    {
250        status = PHNFCSTVAL(CID_NFC_HAL,
251                    NFCSTATUS_INSUFFICIENT_RESOURCES);/*Memory allocation error*/
252    }
253    else
254    {
255
256        (void)memset((void *)*pphLayer,0,(
257                                sizeof(phNfcLayer_sCfg_t) * HAL4_LAYERS));
258
259        for(i=0 ; i < HAL4_LAYERS ;i++, index-- )
260        {
261            (*pphLayer + i)->layer_index = index;
262            switch(index)
263            {
264                case LAYER_HCI: /*Configure Hci*/
265                {
266                    (*pphLayer+i)->layer_name  =(uint8_t *) "Hci";
267                    (*pphLayer+i)->layer_registry  = NULL;
268                    (*pphLayer+i)->layer_next  =
269                                    (((phNfcLayer_sCfg_t *)*pphLayer) + i + 1);
270                    break;
271                }
272                case LAYER_LLC:/*Configure LLC*/
273                {
274                    (*pphLayer+i)->layer_registry  = phLlcNfc_Register;
275                    (*pphLayer+i)->layer_name  = (uint8_t *)"Llc";
276                    (*pphLayer+i)->layer_next  =
277                                    (((phNfcLayer_sCfg_t *)*pphLayer) + i + 1);
278                    break;
279                }
280                case LAYER_DAL: /*Configure the DAL*/
281                {
282                    (*pphLayer+i)->layer_registry  = phDal4Nfc_Register;
283                    (*pphLayer+i)->layer_name  = (uint8_t *)"Dal";
284                    (*pphLayer+i)->layer_next  = NULL ;
285                    break;
286                }
287                default:
288                    break;
289            } /* End of Switch */
290        }   /* End of For Loop */
291    }   /* End of NULL Check */
292
293    return status ;
294}
295
296
297
298#ifdef ANDROID
299
300#define LOG_TAG "NFC-HCI"
301
302#include <utils/Log.h>
303#include <dlfcn.h>
304
305const unsigned char *nxp_nfc_full_version;
306const unsigned char *nxp_nfc_fw;
307
308int dlopen_firmware() {
309    void *p;
310
311    void *handle = dlopen("/system/lib/libpn544_fw.so", RTLD_NOW);
312    if (handle == NULL) {
313        LOGE("Could not open libpn544.so");
314        return -1;
315    }
316
317    p = dlsym(handle, "nxp_nfc_full_version");
318    if (p == NULL) {
319        LOGE("Could not link nxp_nfc_full_version");
320        return -1;
321    }
322    nxp_nfc_full_version = (unsigned char *)p;
323
324    p = dlsym(handle, "nxp_nfc_fw");
325    if (p == NULL) {
326        LOGE("Could not link nxp_nfc_fw");
327        return -1;
328    }
329    nxp_nfc_fw = (unsigned char *)p;
330
331    return 0;
332}
333#endif
334
335/**
336 *  The open function called by the upper HAL when HAL4 is to be opened
337 *  (initialized).
338 *
339 */
340NFCSTATUS phHal4Nfc_Open(
341                         phHal_sHwReference_t       *psHwReference,
342                         phHal4Nfc_InitType_t        InitType,
343                         pphHal4Nfc_GenCallback_t    pOpenCallback,
344                         void                       *pContext
345                         )
346{
347    NFCSTATUS openRetVal = NFCSTATUS_SUCCESS;
348    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
349    phHciNfc_Init_t   eHciInitType = (phHciNfc_Init_t)InitType;
350    /*Set Default Clock settings once*/
351    static phHal_sHwConfig_t sHwConfig = {
352        {0},
353        NXP_DEFAULT_CLK_REQUEST,
354        NXP_DEFAULT_INPUT_CLK
355        };
356    /*NULL checks*/
357    if(NULL == psHwReference || NULL == pOpenCallback)
358    {
359        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
360        openRetVal = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
361    }
362    else if(NULL != gpphHal4Nfc_Hwref)
363    {
364        /*Hal4 context is open or open in progress ,return Ctxt already open*/
365        openRetVal =  PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_ALREADY_INITIALISED);
366    }
367    else/*Do an initialization*/
368    {
369#ifdef ANDROID
370        dlopen_firmware();
371#endif
372
373        /*If hal4 ctxt in Hwreference is NULL create a new context*/
374        if(NULL == ((phHal_sHwReference_t *)psHwReference)->hal_context)
375        {
376            Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)
377                                phOsalNfc_GetMemory((uint32_t)sizeof(
378                                                        phHal4Nfc_Hal4Ctxt_t)
379                                                        );
380            ((phHal_sHwReference_t *)psHwReference)->hal_context = Hal4Ctxt;
381        }
382        else/*Take context from Hw reference*/
383        {
384            Hal4Ctxt = ((phHal_sHwReference_t *)psHwReference)->hal_context;
385        }
386        if(NULL == Hal4Ctxt)
387        {
388            openRetVal = PHNFCSTVAL(CID_NFC_HAL,
389                        NFCSTATUS_INSUFFICIENT_RESOURCES);
390        }
391        else
392        {
393            (void)memset((void *)Hal4Ctxt,
394                        0,
395                        ((uint32_t)sizeof(phHal4Nfc_Hal4Ctxt_t)));
396            /* Configure layers if not configured */
397            if( NULL == Hal4Ctxt->pHal4Nfc_LayerCfg )
398            {
399                openRetVal = phHal4Nfc_Configure_Layers(
400                                                  &(Hal4Ctxt->pHal4Nfc_LayerCfg)
401                                                  );
402            }
403
404            if( openRetVal == NFCSTATUS_SUCCESS )
405            {
406                /*update Next state*/
407                Hal4Ctxt->Hal4NextState = (HCI_SELF_TEST == eHciInitType?
408                                eHal4StateSelfTestMode:eHal4StateOpenAndReady);
409                /*Store callback and context ,and set Default settings in Context*/
410                Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb = pOpenCallback;
411                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
412                Hal4Ctxt->sTgtConnectInfo.EmulationState = NFC_EVT_DEACTIVATED;
413                gpphHal4Nfc_Hwref = psHwReference;
414                PHDBG_INFO("Hal4:Calling Hci-Init");
415                openRetVal = phHciNfc_Initialise (
416                                        (void *)&Hal4Ctxt->psHciHandle,
417                                        psHwReference,
418                                        eHciInitType,
419                                        &sHwConfig,
420                                        (pphNfcIF_Notification_CB_t)
421                                            phHal4Nfc_LowerNotificationHandler,
422                                        (void *)Hal4Ctxt,
423                                        Hal4Ctxt->pHal4Nfc_LayerCfg
424                                        );
425                /*Hci Init did not succeed.free Resources and return*/
426                if( (openRetVal != NFCSTATUS_SUCCESS)
427                            && (PHNFCSTATUS (openRetVal) != NFCSTATUS_PENDING) )
428                {
429                    phOsalNfc_FreeMemory(Hal4Ctxt->pHal4Nfc_LayerCfg);
430                    phOsalNfc_FreeMemory(Hal4Ctxt);
431                    Hal4Ctxt = NULL;
432                }
433            }/*if( openRetVal == NFCSTATUS_SUCCESS )*/
434            else/*Free the context*/
435            {
436                phOsalNfc_FreeMemory(Hal4Ctxt);
437            }/*else*/
438        }
439    }
440    return openRetVal;
441}
442
443/**  The I/O Control function allows the caller to use (vendor-) specific
444*  functionality provided by the lower layer or by the hardware. */
445NFCSTATUS phHal4Nfc_Ioctl(
446                          phHal_sHwReference_t       *psHwReference,
447                          uint32_t                    IoctlCode,
448                          phNfc_sData_t              *pInParam,
449                          phNfc_sData_t              *pOutParam,
450                          pphHal4Nfc_IoctlCallback_t  pIoctlCallback,
451                          void                       *pContext
452                          )
453{
454    NFCSTATUS RetStatus = NFCSTATUS_FAILED;
455    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
456    uint32_t config_type = 0;
457    uint8_t ind = 0;
458    /*NULL checks*/
459    if((NULL == psHwReference)
460        || (NULL == pIoctlCallback)
461        )
462    {
463        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
464        RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
465    }
466    /*Only the Ioctls NFC_FW_DOWNLOAD_CHECK and NFC_FW_DOWNLOAD are allowed in
467      the uninitialized state of HAL*/
468    else if(NULL == psHwReference->hal_context)
469    {
470#ifdef FW_DOWNLOAD
471        if(NFC_FW_DOWNLOAD_CHECK == IoctlCode)
472        {
473            RetStatus = phDnldNfc_Run_Check(
474                psHwReference
475                );
476        }
477        else if((NFC_FW_DOWNLOAD == IoctlCode)
478            &&(NULL == gpphHal4Nfc_Hwref))/*Indicates current state is shutdown*/
479        {
480            Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)
481                phOsalNfc_GetMemory((uint32_t)sizeof(
482                                                phHal4Nfc_Hal4Ctxt_t)
483                                                );
484            if(NULL == Hal4Ctxt)
485            {
486                RetStatus = PHNFCSTVAL(CID_NFC_HAL,
487                    NFCSTATUS_INSUFFICIENT_RESOURCES);
488            }
489            else
490            {
491                ((phHal_sHwReference_t *)psHwReference)->hal_context
492                    = Hal4Ctxt;
493                (void)memset((void *)Hal4Ctxt,
494                                 0,
495                                   ((uint32_t)sizeof(phHal4Nfc_Hal4Ctxt_t)));
496                Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
497                Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb
498                    = pIoctlCallback;/*Register upper layer callback*/
499                Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam = pOutParam;
500                /*Upgrade the firmware*/
501                RetStatus = phDnldNfc_Upgrade (
502                        psHwReference,
503                        phHal4Nfc_DownloadComplete,
504                        Hal4Ctxt
505                        );
506                if(NFCSTATUS_SUCCESS == RetStatus)
507                {
508                    phOsalNfc_FreeMemory(Hal4Ctxt);
509                    ((phHal_sHwReference_t *)psHwReference)->hal_context = NULL;
510                }
511            }
512        }
513        else
514#endif/*NFC_FW_DOWNLOAD*/
515        {
516            RetStatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);
517        }
518    }
519    else/*Status is Initialised*/
520    {
521        /*Register upper layer context*/
522        Hal4Ctxt = psHwReference->hal_context;
523        Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam = pOutParam;
524        switch(IoctlCode)
525        {
526        /*Self test Ioctls*/
527        case DEVMGMT_ANTENNA_TEST:
528        case DEVMGMT_SWP_TEST:
529        case DEVMGMT_NFCWI_TEST:
530            if(eHal4StateSelfTestMode ==Hal4Ctxt->Hal4CurrentState)
531            {
532                RetStatus = phHciNfc_System_Test(
533                    Hal4Ctxt->psHciHandle,
534                    (void *)psHwReference,
535                    IoctlCode ,
536                    pInParam
537                    );
538            }
539            break;
540        /*PRBS Test*/
541        case DEVMGMT_PRBS_TEST:
542            RetStatus = phHciNfc_PRBS_Test(
543                Hal4Ctxt->psHciHandle,
544                (void *)psHwReference,
545                IoctlCode ,
546                pInParam
547                );
548            break;
549        /*To Set Antenna Power Level*/
550        case NFC_ANTENNA_CWG:
551            if(eHal4StateSelfTestMode ==Hal4Ctxt->Hal4CurrentState)
552            {
553                RetStatus = phHciNfc_System_Configure (
554                    Hal4Ctxt->psHciHandle,
555                    (void *)psHwReference,
556                    NFC_ANTENNA_CWG,
557                    pInParam->buffer[0] /**Set Power Level*/
558                    );
559
560            }
561            break;
562        /*Not allowed when Init is complete*/
563        case NFC_FW_DOWNLOAD_CHECK:
564        case NFC_FW_DOWNLOAD:
565            RetStatus = PHNFCSTVAL(CID_NFC_HAL,
566                NFCSTATUS_BUSY);
567            break;
568        /*Gpio read*/
569        case NFC_GPIO_READ:
570            /* if(eHal4StateSelfTestMode == Hal4Ctxt->Hal4CurrentState) */
571            {
572                RetStatus = phHciNfc_System_Get_Info(
573                    Hal4Ctxt->psHciHandle,
574                    (void *)psHwReference,
575                    IoctlCode ,
576                    pOutParam->buffer
577                    );
578            }
579            break;
580        /*Used to Read Memory/Registers .3 bytes of Array passed form the
581          address to read from in MSB first format.*/
582        case NFC_MEM_READ:
583            {
584                if((NULL != pInParam)
585                    && (pInParam->length == 3))
586                {
587                    for( ind = 0; ind < 3; ind++ )
588                    {
589                        config_type = ((config_type << BYTE_SIZE )
590                                        | (pInParam->buffer[ind] ));
591                    }
592                    RetStatus = phHciNfc_System_Get_Info(
593                        Hal4Ctxt->psHciHandle,
594                        (void *)psHwReference,
595                        config_type ,
596                        pOutParam->buffer
597                        );
598                }
599                else
600                {
601                    RetStatus = PHNFCSTVAL(CID_NFC_HAL,
602                        NFCSTATUS_INVALID_PARAMETER);
603                }
604            }
605            break;
606        /*Used to Write Memory/Registers .First 3 bytes of Array passed in MSB
607          first format form the address to write to.The 4th Byte is the 8 bit
608          value to be written to the address*/
609        case NFC_MEM_WRITE:
610            {
611                if((NULL != pInParam)
612                    && (pInParam->length == 4))
613                {
614                    for( ind = 0; ind < 3; ind++ )
615                    {
616                        config_type = ((config_type << BYTE_SIZE )
617                                        | (pInParam->buffer[ind] ));
618                    }
619                    RetStatus = phHciNfc_System_Configure (
620                        Hal4Ctxt->psHciHandle,
621                        (void *)psHwReference,
622                        config_type,
623                        pInParam->buffer[3] /*config value*/
624                        );
625                }
626                else
627                {
628                    RetStatus = PHNFCSTVAL(CID_NFC_HAL ,
629                        NFCSTATUS_INVALID_PARAMETER);
630                }
631            }
632            break;
633        default:
634            break;
635        }
636        if(NFCSTATUS_PENDING == RetStatus)/*Callback Pending*/
637        {
638            /*Register upper layer callback and context*/
639            Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
640            Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb= pIoctlCallback;
641            /*Store the Ioctl code*/
642            Hal4Ctxt->Ioctl_Type = IoctlCode;
643        }
644    }
645    return RetStatus;
646}
647
648
649/**
650 *  The close function called by the upper layer when HAL4 is to be closed
651 *  (shutdown).
652 */
653NFCSTATUS phHal4Nfc_Close(
654                          phHal_sHwReference_t *psHwReference,
655                          pphHal4Nfc_GenCallback_t pCloseCallback,
656                          void *pContext
657                          )
658{
659    NFCSTATUS closeRetVal = NFCSTATUS_SUCCESS;
660    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
661    /*NULL checks*/
662    if(NULL == psHwReference || NULL == pCloseCallback)
663    {
664        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
665        closeRetVal = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
666    }
667    else if((NULL == psHwReference->hal_context)
668                        || (((phHal4Nfc_Hal4Ctxt_t *)
669                                psHwReference->hal_context)->Hal4CurrentState
670                                               < eHal4StateSelfTestMode)
671                        || (((phHal4Nfc_Hal4Ctxt_t *)
672                                psHwReference->hal_context)->Hal4NextState
673                                               == eHal4StateClosed))
674    {
675        /*return already closed*/
676        closeRetVal= PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_NOT_INITIALISED);
677    }
678    else  /*Close the HAL*/
679    {
680        /*Get Hal4 context from Hw reference*/
681        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)((phHal_sHwReference_t *)
682                                               psHwReference)->hal_context;
683        /*Unregister Tag Listener*/
684        if(NULL != Hal4Ctxt->psADDCtxtInfo)
685        {
686            Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
687        }
688        /*store Callback and Context*/
689        Hal4Ctxt->sUpperLayerInfo.pUpperCloseCb = pCloseCallback;
690        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
691        /*Call Hci Release*/
692        PHDBG_INFO("Hal4:Calling Hci Release");
693        closeRetVal =(NFCSTATUS)phHciNfc_Release(
694                                    (void *)Hal4Ctxt->psHciHandle,
695                                    psHwReference,
696                                    (pphNfcIF_Notification_CB_t)
697                                    phHal4Nfc_LowerNotificationHandler,
698                                    (void *)Hal4Ctxt
699                                    );
700        /*Update Next state and exit*/
701        if( PHNFCSTATUS (closeRetVal) == NFCSTATUS_PENDING )
702        {
703            Hal4Ctxt->Hal4NextState = eHal4StateClosed;
704            Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
705        }
706        else
707        {
708
709        }
710    }
711    return closeRetVal;
712}
713
714/*Forcibly shutdown the HAl4.Frees all Resources in use by Hal4 before shutting
715  down*/
716void phHal4Nfc_Hal4Reset(
717                         phHal_sHwReference_t *pHwRef,
718                         void                 *pContext
719                         )
720{
721    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
722    NFCSTATUS             closeRetVal = NFCSTATUS_SUCCESS;
723    uint8_t               RemoteDevNumber = 0;
724    if(pHwRef ==NULL)
725    {
726        closeRetVal = PHNFCSTVAL(CID_NFC_HAL , NFCSTATUS_INVALID_PARAMETER);
727    }
728    else if(pHwRef->hal_context != NULL)
729    {
730        /*Get the Hal context*/
731        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pHwRef->hal_context;
732        /*store the upper layer context*/
733        Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt = pContext;
734        Hal4Ctxt->Hal4NextState = eHal4StateClosed;
735        Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
736        /*Call Hci Release*/
737        PHDBG_INFO("Hal4:Calling Hci Release");
738        closeRetVal =(NFCSTATUS)phHciNfc_Release(
739                                            (void *)Hal4Ctxt->psHciHandle,
740                                            pHwRef,
741                                            (pphNfcIF_Notification_CB_t)NULL,
742                                            (void *)Hal4Ctxt
743                                            );/*Clean up Hci*/
744        Hal4Ctxt->Hal4CurrentState = eHal4StateClosed;
745        phOsalNfc_FreeMemory((void *)Hal4Ctxt->pHal4Nfc_LayerCfg);
746        Hal4Ctxt->pHal4Nfc_LayerCfg = NULL;
747        /*Free ADD context*/
748        if(NULL != Hal4Ctxt->psADDCtxtInfo)
749        {
750            Hal4Ctxt->sUpperLayerInfo.pTagDiscoveryNotification = NULL;
751            while(RemoteDevNumber < MAX_REMOTE_DEVICES)
752            {
753                if(NULL != Hal4Ctxt->rem_dev_list[RemoteDevNumber])
754                {
755                    phOsalNfc_FreeMemory((void *)
756                            (Hal4Ctxt->rem_dev_list[RemoteDevNumber]));
757                    Hal4Ctxt->rem_dev_list[RemoteDevNumber] = NULL;
758                }
759                RemoteDevNumber++;
760            }
761            Hal4Ctxt->psADDCtxtInfo->nbr_of_devices = 0;
762            phOsalNfc_FreeMemory(Hal4Ctxt->psADDCtxtInfo);
763        }
764        /*Free Trcv context*/
765        if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
766        {
767            if(NULL != Hal4Ctxt->psTrcvCtxtInfo->sLowerRecvData.buffer)
768            {
769                phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo
770                                                    ->sLowerRecvData.buffer);
771            }
772            if((NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
773                && (NULL != Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData))
774            {
775                phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo->psUpperSendData);
776            }
777            phOsalNfc_FreeMemory(Hal4Ctxt->psTrcvCtxtInfo);
778        }
779        phOsalNfc_FreeMemory(Hal4Ctxt);/*Free the context*/
780        pHwRef->hal_context = NULL;
781        gpphHal4Nfc_Hwref = NULL;
782    }
783    else
784    {
785        /*Hal4 Context is already closed.Return Success*/
786    }
787    /*Reset Should always return Success*/
788    if(closeRetVal != NFCSTATUS_SUCCESS)
789    {
790        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
791    }
792    return;
793}
794
795/**
796 *  \if hal
797 *   \ingroup grp_hal_common
798 *  \else
799 *   \ingroup grp_mw_external_hal_funcs
800 *  \endif
801 *
802 *  Retrieves the capabilities of the device represented by the Hardware
803 *  Reference parameter.
804 *  The HW, SW versions, the MTU and other mandatory information are located
805 *  inside the pDevCapabilities parameter.
806 */
807NFCSTATUS phHal4Nfc_GetDeviceCapabilities(
808                            phHal_sHwReference_t          *psHwReference,
809                            phHal_sDeviceCapabilities_t   *psDevCapabilities,
810                            void                          *pContext
811                            )
812{
813    NFCSTATUS retstatus = NFCSTATUS_SUCCESS;
814    /*NULL checks*/
815    if(psDevCapabilities == NULL || psHwReference == NULL || pContext == NULL)
816    {
817        retstatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_INVALID_PARAMETER);
818    }
819    /*Check for Initialized state*/
820    else if((NULL == psHwReference->hal_context)
821                        || (((phHal4Nfc_Hal4Ctxt_t *)
822                                psHwReference->hal_context)->Hal4CurrentState
823                                               < eHal4StateOpenAndReady)
824                        || (((phHal4Nfc_Hal4Ctxt_t *)
825                                psHwReference->hal_context)->Hal4NextState
826                                               == eHal4StateClosed))
827    {
828        retstatus = PHNFCSTVAL(CID_NFC_HAL ,NFCSTATUS_NOT_INITIALISED);
829    }
830    else/*Provide Device capabilities and Version Info to the caller*/
831    {
832        (void)memcpy((void *)psDevCapabilities,
833            (void *)&(psHwReference->device_info),
834            sizeof(phHal_sDeviceCapabilities_t));
835        psDevCapabilities->ReaderSupProtocol.Felica         = TRUE;
836        psDevCapabilities->ReaderSupProtocol.ISO14443_4A    = TRUE;
837        psDevCapabilities->ReaderSupProtocol.ISO14443_4B    = TRUE;
838        psDevCapabilities->ReaderSupProtocol.ISO15693       = TRUE;
839        psDevCapabilities->ReaderSupProtocol.Jewel          = TRUE;
840        psDevCapabilities->ReaderSupProtocol.MifareStd      = TRUE;
841        psDevCapabilities->ReaderSupProtocol.MifareUL       = TRUE;
842        psDevCapabilities->ReaderSupProtocol.NFC            = TRUE;
843        psDevCapabilities->EmulationSupProtocol.Felica      = FALSE;
844        psDevCapabilities->EmulationSupProtocol.ISO14443_4A = FALSE;
845        psDevCapabilities->EmulationSupProtocol.ISO14443_4B = FALSE;
846        psDevCapabilities->EmulationSupProtocol.ISO15693    = FALSE;
847        psDevCapabilities->EmulationSupProtocol.Jewel       = FALSE;
848        psDevCapabilities->EmulationSupProtocol.MifareStd   = FALSE;
849        psDevCapabilities->EmulationSupProtocol.MifareUL    = FALSE;
850        psDevCapabilities->EmulationSupProtocol.NFC         = TRUE;
851        psDevCapabilities->hal_version = (
852                    (((PH_HAL4NFC_INTERFACE_VERSION << BYTE_SIZE)
853                      |(PH_HAL4NFC_INTERFACE_REVISION)<<BYTE_SIZE)
854                      |(PH_HAL4NFC_INTERFACE_PATCH)<<BYTE_SIZE)
855                      |PH_HAL4NFC_INTERAFECE_BUILD
856                      );
857    }
858    return retstatus;
859}
860
861/*
862 * Handles all notifications received from HCI layer.
863 *
864 */
865static void phHal4Nfc_LowerNotificationHandler(
866                                    void    *pContext,
867                                    void    *pHwRef,
868                                    uint8_t  type,
869                                    void     *pInfo
870                                    )
871{
872    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
873    if((NULL == pInfo) || (NULL == pHwRef)
874        || (NULL == pContext))
875    {
876        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
877    }
878    else
879    {
880        /*A copy of hardware reference is maintained in HAL for comparing passed
881          and returned context.Set to NULL after a Shutdown*/
882        if(NULL != gpphHal4Nfc_Hwref)/*Get context from Hw ref*/
883        {
884            Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)gpphHal4Nfc_Hwref->hal_context;
885            if(NFC_INVALID_RELEASE_TYPE == Hal4Ctxt->sTgtConnectInfo.ReleaseType)
886            {
887                Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
888                gpphHal4Nfc_Hwref = (phHal_sHwReference_t *)pHwRef;
889            }
890        }
891        else/*No Copy of Hw ref in HAL.Copy both Hwref and Hal context passed
892             by Hci*/
893        {
894            Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
895            gpphHal4Nfc_Hwref = (phHal_sHwReference_t *)pHwRef;
896        }
897        /*Check the type of notification received from Hci and handle it
898          accordingly*/
899        switch(type)
900        {
901            case NFC_NOTIFY_INIT_COMPLETED:
902            case NFC_NOTIFY_INIT_FAILED:
903                phHal4Nfc_OpenComplete(Hal4Ctxt,pInfo);
904                break;
905            case NFC_IO_SUCCESS:
906            case NFC_IO_ERROR:
907                phHal4Nfc_IoctlComplete(Hal4Ctxt,pInfo);
908                break;
909            case NFC_NOTIFY_RESULT:
910                phHal4Nfc_SelfTestComplete(Hal4Ctxt,pInfo);
911                break;
912            case NFC_NOTIFY_DEINIT_COMPLETED:
913            case NFC_NOTIFY_DEINIT_FAILED:
914                phHal4Nfc_CloseComplete(Hal4Ctxt,pInfo);
915                break;
916            case NFC_NOTIFY_POLL_ENABLED:
917            case NFC_NOTIFY_POLL_DISABLED:
918            case NFC_NOTIFY_POLL_RESTARTED:
919            case NFC_NOTIFY_CONFIG_ERROR:
920            case NFC_NOTIFY_CONFIG_SUCCESS:
921                phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
922                break;
923            case NFC_NOTIFY_TARGET_DISCOVERED:
924            case NFC_NOTIFY_DISCOVERY_ERROR:
925                phHal4Nfc_TargetDiscoveryComplete(Hal4Ctxt,pInfo);
926                break;
927            case NFC_NOTIFY_TARGET_REACTIVATED:
928                phHal4Nfc_ReactivationComplete(Hal4Ctxt,pInfo);
929                break;
930            case NFC_NOTIFY_EVENT:
931                PHDBG_INFO("Hal4:Calling Event callback");
932                phHal4Nfc_HandleEvent(Hal4Ctxt,pInfo);
933                break;
934            case NFC_NOTIFY_TARGET_CONNECTED:
935                PHDBG_INFO("Hal4:Calling Hal4 Connect complete");
936                phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
937                break;
938
939            case NFC_NOTIFY_TARGET_DISCONNECTED:
940            {
941                PHDBG_INFO("Hal4:Target Disconnected");
942                if(Hal4Ctxt->Hal4NextState == eHal4StatePresenceCheck)
943                {
944                    phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
945                }
946                else
947                {
948                    phHal4Nfc_DisconnectComplete(Hal4Ctxt,pInfo);
949                }
950                break;
951            }
952            case NFC_NOTIFY_TRANSCEIVE_COMPLETED:
953            case NFC_NOTIFY_TRANSCEIVE_ERROR    :
954                PHDBG_INFO("Hal4:Transceive Callback");
955                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
956                {
957#ifdef TRANSACTION_TIMER
958                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
959                        != PH_OSALNFC_INVALID_TIMER_ID)
960                    {
961                        phOsalNfc_Timer_Stop(
962                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
963                            );
964                        phOsalNfc_Timer_Delete(
965                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
966                            );
967                        Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
968                                    = PH_OSALNFC_INVALID_TIMER_ID;
969                    }
970#endif /*TRANSACTION_TIMER*/
971                    phHal4Nfc_TransceiveComplete(Hal4Ctxt,pInfo);
972                }
973                break;
974            case NFC_NOTIFY_SEND_COMPLETED   :
975                PHDBG_INFO("Hal4:NfcIp1 Send Callback");
976                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
977                {
978                    phHal4Nfc_SendCompleteHandler(Hal4Ctxt,pInfo);
979                }
980                break;
981            case NFC_NOTIFY_TRANSACTION  :
982                phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
983                break;
984            case NFC_NOTIFY_RECV_ERROR    :
985            case NFC_NOTIFY_RECV_EVENT    :
986                PHDBG_INFO("Hal4:Receive Event");
987                if(NULL != Hal4Ctxt->psTrcvCtxtInfo)
988                {
989                    if(Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
990                        != PH_OSALNFC_INVALID_TIMER_ID)
991                    {
992                        phOsalNfc_Timer_Stop(
993                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
994                            );
995                        phOsalNfc_Timer_Delete(
996                            Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
997                            );
998                        Hal4Ctxt->psTrcvCtxtInfo->TransactionTimerId
999                            = PH_OSALNFC_INVALID_TIMER_ID;
1000                    }
1001                }
1002                phHal4Nfc_RecvCompleteHandler(Hal4Ctxt,pInfo);
1003                break;
1004            case NFC_NOTIFY_TARGET_PRESENT:
1005                phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
1006                break;
1007            case NFC_NOTIFY_DEVICE_ERROR:
1008            {
1009                static phHal4Nfc_NotificationInfo_t uNotificationInfo;
1010                Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
1011                if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
1012                {
1013                    Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
1014                        Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
1015                        NFC_EVENT_NOTIFICATION,
1016                        uNotificationInfo,
1017                        NFCSTATUS_BOARD_COMMUNICATION_ERROR
1018                        );
1019                }
1020                else
1021                {
1022                    phOsalNfc_RaiseException(phOsalNfc_e_UnrecovFirmwareErr,1);
1023                }
1024                break;
1025            }
1026            case NFC_NOTIFY_CONNECT_FAILED:
1027            case NFC_NOTIFY_DISCONNECT_FAILED:
1028            /*Generic Error type received from Hci.Handle the error based on
1029              Hal4 next state and which past callback was Pending*/
1030            case NFC_NOTIFY_ERROR:
1031            {
1032                PHDBG_WARNING("Hal4:Error Notification from HCI");
1033                switch(Hal4Ctxt->Hal4NextState)
1034                {
1035                    case eHal4StateClosed:
1036                        phHal4Nfc_CloseComplete(Hal4Ctxt,pInfo);
1037                        break;
1038                    case eHal4StateSelfTestMode:
1039                        phHal4Nfc_SelfTestComplete(Hal4Ctxt,pInfo);
1040                        break;
1041                    case eHal4StateConfiguring:
1042                        phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
1043                        break;
1044                    case eHal4StateTargetDiscovered:
1045                    case eHal4StateTargetActivate:
1046                    {
1047                        if(NULL != Hal4Ctxt->sTgtConnectInfo.pUpperConnectCb)
1048                        {
1049                            if(NULL == Hal4Ctxt->sTgtConnectInfo.psConnectedDevice)
1050                            {
1051                                 phHal4Nfc_ConfigureComplete(Hal4Ctxt,pInfo,type);
1052                            }
1053                            else
1054                            {
1055                                phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
1056                            }
1057                        }
1058                        else
1059                        {
1060                            phHal4Nfc_TargetDiscoveryComplete(Hal4Ctxt,pInfo);
1061                        }
1062                        break;
1063                    }
1064                    case eHal4StateTargetConnected:
1065                        phHal4Nfc_ConnectComplete(Hal4Ctxt,pInfo);
1066                        break;
1067                    case eHal4StateOpenAndReady:
1068                        phHal4Nfc_DisconnectComplete(Hal4Ctxt,pInfo);
1069                        break;
1070                    case eHal4StatePresenceCheck:
1071                        phHal4Nfc_PresenceChkComplete(Hal4Ctxt,pInfo);
1072                        break;
1073                    default:
1074                        PHDBG_WARNING("Unknown Error notification");
1075                        break;
1076                }
1077                break;
1078            }/*End of switch(Hal4Ctxt->Hal4State)*/
1079            default:
1080                phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
1081                break;
1082        }/*End of switch(type)*/
1083    }
1084    return;
1085}
1086
1087
1088/*Event handler for HAL-HCI interface*/
1089static void phHal4Nfc_HandleEvent(
1090                       phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
1091                       void *pInfo
1092                       )
1093{
1094    phHal_sEventInfo_t *psEventInfo = (phHal_sEventInfo_t *)pInfo;
1095    static phNfc_sNotificationInfo_t sNotificationInfo;
1096    phHal4Nfc_NotificationInfo_t uNotificationInfo = {NULL};
1097    NFCSTATUS RetStatus = NFCSTATUS_FAILED;
1098    /*Check if Hal4 Close has already been called*/
1099    if(eHal4StateClosed != Hal4Ctxt->Hal4NextState)
1100    {
1101        switch(psEventInfo->eventType)
1102        {
1103        case NFC_EVT_ACTIVATED:/*Target Activated*/
1104        {
1105            if(psEventInfo->eventHost == phHal_eHostController)
1106            {
1107                switch(psEventInfo->eventSource)
1108                {
1109                    case phHal_eNfcIP1_Target:
1110                        phHal4Nfc_P2PActivateComplete(Hal4Ctxt,pInfo);
1111                        break;
1112                    case phHal_eISO14443_A_PICC:
1113                    case phHal_eISO14443_B_PICC:
1114                        sNotificationInfo.info = psEventInfo;
1115                        sNotificationInfo.status = NFCSTATUS_SUCCESS;
1116                        sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1117                        pInfo = &sNotificationInfo;
1118                        phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1119                        break;
1120                    default:
1121                        break;
1122                }
1123            }
1124        }
1125            break;
1126        case NFC_EVT_DEACTIVATED:/*Target Deactivated*/
1127        {
1128            if(psEventInfo->eventHost == phHal_eHostController)
1129            {
1130                switch(psEventInfo->eventSource)
1131                {
1132                case phHal_eNfcIP1_Target:
1133                    phHal4Nfc_HandleP2PDeActivate(Hal4Ctxt,pInfo);
1134                    break;
1135                case phHal_eISO14443_A_PICC:
1136                case phHal_eISO14443_B_PICC:
1137                    sNotificationInfo.info = psEventInfo;
1138                    sNotificationInfo.status = NFCSTATUS_SUCCESS;
1139                    sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1140                    pInfo = &sNotificationInfo;
1141                    phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1142                    break;
1143                default:
1144                    break;
1145                }
1146            }
1147        }
1148            break;
1149        /*Set Protection Event*/
1150        case NFC_EVT_PROTECTED:
1151        {
1152#ifdef IGNORE_EVT_PROTECTED
1153            /*Ignore_Event_Protected is set to false during Field Off event and
1154              Set protection Configuration.After a NFC_EVT_PROTECTED is received
1155              once all subsequent NFC_EVT_PROTECTED events are ignored*/
1156            if(FALSE == Hal4Ctxt->Ignore_Event_Protected)
1157            {
1158                Hal4Ctxt->Ignore_Event_Protected = TRUE;
1159#endif/*#ifdef IGNORE_EVT_PROTECTED*/
1160                sNotificationInfo.info = psEventInfo;
1161                sNotificationInfo.status = NFCSTATUS_SUCCESS;
1162                sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1163                pInfo = &sNotificationInfo;
1164                phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1165#ifdef IGNORE_EVT_PROTECTED
1166            }
1167#endif/*#ifdef IGNORE_EVT_PROTECTED*/
1168            break;
1169        }
1170        /*NFC_UICC_RDPHASES_DEACTIVATE_REQ*/
1171        case NFC_UICC_RDPHASES_DEACTIVATE_REQ:
1172        {
1173            if(NULL != gpphHal4Nfc_Hwref)
1174            {
1175                gpphHal4Nfc_Hwref->uicc_rdr_active = FALSE;
1176            }
1177            break;
1178        }
1179        case NFC_UICC_RDPHASES_ACTIVATE_REQ:
1180        {
1181            if(NULL != gpphHal4Nfc_Hwref)
1182            {
1183                gpphHal4Nfc_Hwref->uicc_rdr_active = TRUE;
1184            }
1185            /*If a NFC_UICC_RDPHASES_ACTIVATE_REQ is received before a configure
1186             discovery,then create a ADD context info*/
1187            if (NULL == Hal4Ctxt->psADDCtxtInfo)
1188            {
1189                Hal4Ctxt->psADDCtxtInfo= (pphHal4Nfc_ADDCtxtInfo_t)
1190                    phOsalNfc_GetMemory((uint32_t)
1191                    (sizeof(phHal4Nfc_ADDCtxtInfo_t)));
1192                if(NULL != Hal4Ctxt->psADDCtxtInfo)
1193                {
1194                    (void)memset(Hal4Ctxt->psADDCtxtInfo,0,
1195                        sizeof(phHal4Nfc_ADDCtxtInfo_t)
1196                        );
1197                }
1198            }
1199            if(NULL != Hal4Ctxt->psADDCtxtInfo)
1200            {
1201                Hal4Ctxt->psADDCtxtInfo->sADDCfg.PollDevInfo.PollEnabled
1202                                |= psEventInfo->eventInfo.rd_phases;
1203                /*Configure HCI Discovery*/
1204                RetStatus = phHciNfc_Config_Discovery(
1205                    (void *)Hal4Ctxt->psHciHandle,
1206                    gpphHal4Nfc_Hwref,
1207                    &(Hal4Ctxt->psADDCtxtInfo->sADDCfg)
1208                    );
1209                Hal4Ctxt->Hal4NextState = (NFCSTATUS_PENDING == RetStatus?
1210                                                eHal4StateConfiguring:
1211                                                Hal4Ctxt->Hal4NextState);
1212            }
1213            break;
1214        }
1215        /*Call Default Event handler for these Events*/
1216        case NFC_INFO_TXLDO_OVERCUR:
1217        case NFC_INFO_MEM_VIOLATION:
1218        case NFC_INFO_TEMP_OVERHEAT:
1219        {
1220            sNotificationInfo.info = psEventInfo;
1221            sNotificationInfo.status = NFCSTATUS_SUCCESS;
1222            sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1223            pInfo = &sNotificationInfo;
1224            PHDBG_INFO("Hal4:Exception events");
1225            if(NULL != Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler)
1226            {
1227                /*Pass on Event notification info from Hci to Upper layer*/
1228                uNotificationInfo.psEventInfo = psEventInfo;
1229                Hal4Ctxt->sUpperLayerInfo.pDefaultEventHandler(
1230                    Hal4Ctxt->sUpperLayerInfo.DefaultListenerCtxt,
1231                    sNotificationInfo.type,
1232                    uNotificationInfo,
1233                    NFCSTATUS_SUCCESS
1234                    );
1235            }
1236            break;
1237        }
1238        /*Call emulation Event handler fto handle these Events*/
1239        case NFC_EVT_TRANSACTION:
1240        case NFC_EVT_START_OF_TRANSACTION:
1241        case NFC_EVT_END_OF_TRANSACTION:
1242        case NFC_EVT_CONNECTIVITY:
1243        case NFC_EVT_OPERATION_ENDED:
1244            sNotificationInfo.info = psEventInfo;
1245            sNotificationInfo.status = NFCSTATUS_SUCCESS;
1246            sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1247            pInfo = &sNotificationInfo;
1248            PHDBG_INFO("Hal4:Event transaction\n");
1249            phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1250            break;
1251        case NFC_EVT_FIELD_ON:
1252            Hal4Ctxt->psEventInfo = sNotificationInfo.info = psEventInfo;
1253            sNotificationInfo.status = NFCSTATUS_SUCCESS;
1254            sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1255            pInfo = &sNotificationInfo;
1256            PHDBG_INFO("Hal4:Event Field ON\n");
1257            phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1258            break;
1259        case NFC_EVT_FIELD_OFF:
1260    #ifdef IGNORE_EVT_PROTECTED
1261            Hal4Ctxt->Ignore_Event_Protected = FALSE;
1262    #endif/*#ifdef IGNORE_EVT_PROTECTED*/
1263            Hal4Ctxt->psEventInfo = sNotificationInfo.info = psEventInfo;
1264            sNotificationInfo.status = NFCSTATUS_SUCCESS;
1265            sNotificationInfo.type = NFC_EVENT_NOTIFICATION;
1266            pInfo = &sNotificationInfo;
1267            PHDBG_INFO("Hal4:Event Field OFF\n");
1268            phHal4Nfc_HandleEmulationEvent(Hal4Ctxt,pInfo);
1269            break;
1270        default:
1271            PHDBG_WARNING("Hal4:Unhandled Event type received");
1272            break;
1273        }/*End of switch*/
1274    }/*if(eHal4StateClosed != Hal4Ctxt->Hal4NextState)*/
1275    return;
1276}
1277
1278
1279/*Callback handler for Self Test Ioctl completion*/
1280static void phHal4Nfc_SelfTestComplete(
1281                                       phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
1282                                       void *pInfo
1283                                       )
1284{
1285    NFCSTATUS status = NFCSTATUS_FAILED;
1286    phNfc_sData_t *SelfTestResults
1287        = (phNfc_sData_t *)(((phNfc_sCompletionInfo_t *)pInfo)->info);
1288    pphHal4Nfc_IoctlCallback_t pUpper_IoctlCb
1289        = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
1290    void  *pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
1291    /*check for Success*/
1292    if((SelfTestResults->length > 0) && (0 == SelfTestResults->buffer[0]))
1293    {
1294        status = NFCSTATUS_SUCCESS;
1295    }
1296    /*Copy response buffer and length*/
1297    (void)memcpy(Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->buffer,
1298                 SelfTestResults->buffer,
1299                 SelfTestResults->length);
1300    Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->length
1301                                        = SelfTestResults->length;
1302    /*Call registered Ioctl callback*/
1303    (*pUpper_IoctlCb)(
1304                pUpper_Context,
1305                Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam,
1306                status
1307                );
1308    return;
1309}
1310
1311
1312static void phHal4Nfc_IoctlComplete(
1313                                    phHal4Nfc_Hal4Ctxt_t  *Hal4Ctxt,
1314                                    void *pInfo
1315                                    )
1316{
1317    /*Copy status*/
1318    NFCSTATUS status = (((phNfc_sCompletionInfo_t *)pInfo)->status);
1319    pphHal4Nfc_IoctlCallback_t pUpper_IoctlCb
1320                                    = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
1321#ifdef MERGE_SAK_SW2
1322    pphHal4Nfc_GenCallback_t pConfigCallback =
1323        Hal4Ctxt->sUpperLayerInfo.pConfigCallback;
1324#endif/*#ifdef MERGE_SAK_SW2*/
1325    void  *pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
1326    Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb = NULL;
1327#ifdef MERGE_SAK_SW1 /*Software workaround 1*/
1328    if(eHal4StateOpenAndReady == Hal4Ctxt->Hal4NextState)
1329    {
1330        Hal4Ctxt->Hal4CurrentState = Hal4Ctxt->Hal4NextState;
1331        Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
1332        /*Upper layer's Open Cb*/
1333        (*Hal4Ctxt->sUpperLayerInfo.pUpperOpenCb)(
1334            Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,
1335            NFCSTATUS_SUCCESS
1336            );
1337    }
1338#endif/*#ifdef MERGE_SAK_SW1*/
1339#ifdef MERGE_SAK_SW2 /*Software workaround 2*/
1340    else if((eHal4StateConfiguring == Hal4Ctxt->Hal4NextState)
1341            &&(NULL != pConfigCallback))
1342    {
1343        Hal4Ctxt->sUpperLayerInfo.pConfigCallback = NULL;
1344        Hal4Ctxt->Hal4NextState = eHal4StateInvalid;
1345        (*pConfigCallback)(
1346            Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt,status
1347            );
1348    }
1349    else
1350#endif/*#ifdef MERGE_SAK_SW2*/
1351    {
1352        /*for NFC_MEM_READ and NFC_GPIO_READ ,provide one Byte Response*/
1353        if ((NFC_MEM_READ == Hal4Ctxt->Ioctl_Type)
1354            || (NFC_GPIO_READ == Hal4Ctxt->Ioctl_Type))
1355        {
1356            Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam->length
1357                = sizeof (uint8_t);
1358        }
1359         /*Call registered Ioctl callback*/
1360        if(NULL != pUpper_IoctlCb)
1361        {
1362            (*pUpper_IoctlCb)(
1363                pUpper_Context,
1364                Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam,
1365                status
1366                );
1367        }
1368    }
1369    return;
1370}
1371
1372#ifdef FW_DOWNLOAD
1373/**Callback handler for Download completion*/
1374STATIC void phHal4Nfc_DownloadComplete(
1375                                void *pContext,
1376                                void *pHwRef,
1377                                uint8_t type,
1378                                void *pInfo
1379                                )
1380{
1381    phHal4Nfc_Hal4Ctxt_t *Hal4Ctxt = NULL;
1382    NFCSTATUS status = NFCSTATUS_FAILED;
1383    pphHal4Nfc_IoctlCallback_t pUpper_DnldCb = NULL;
1384    phNfc_sData_t *pIoctlOutParam = NULL;
1385    phHal_sHwReference_t *psHwRef = NULL;
1386    void  *pUpper_Context = NULL;
1387    /*NULL checks*/
1388    if((NULL == pInfo) || (NULL == pHwRef) || (NULL == pContext))
1389    {
1390        phOsalNfc_RaiseException(phOsalNfc_e_PrecondFailed,1);
1391    }
1392    else
1393    {
1394        type = type;
1395        Hal4Ctxt = (phHal4Nfc_Hal4Ctxt_t *)pContext;
1396        /*Copy back stored context/callback for the upper layer*/
1397        pUpper_Context = Hal4Ctxt->sUpperLayerInfo.psUpperLayerCtxt;
1398        pIoctlOutParam = Hal4Ctxt->sUpperLayerInfo.pIoctlOutParam;
1399        pUpper_DnldCb = Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb;
1400        Hal4Ctxt->sUpperLayerInfo.pUpperIoctlCb = NULL;
1401        /*Copy download status*/
1402        status = (((phNfc_sCompletionInfo_t *)pInfo)->status);
1403        /*copy hw reference*/
1404        psHwRef = (phHal_sHwReference_t *)pHwRef;
1405        /*Free the temporary hal context used only for the sake of download*/
1406        phOsalNfc_FreeMemory(psHwRef->hal_context);
1407        psHwRef->hal_context = NULL;
1408        /*Call upper layer callback*/
1409        if(NULL != pUpper_DnldCb)
1410        {
1411            (*pUpper_DnldCb)(
1412                pUpper_Context,
1413                pIoctlOutParam,
1414                status
1415                );
1416        }
1417    }
1418    return;
1419}
1420#endif /*FW_DOWNLOAD*/
1421
1422