Initialize Tizen 2.3
[adaptation/devices/nfc-plugin-nxp.git] / src / phHciNfc_RFReader.c
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 * =========================================================================== *
19 *                                                                             *
20 *                                                                             *
21 * \file  phHciNfc_RFReader.c                                                  *
22 * \brief HCI RF Reader Management Gate Routines.                              *
23 *                                                                             *
24 *                                                                             *
25 * Project: NFC-FRI-1.1                                                        *
26 *                                                                             *
27 * $Date: Wed Apr 21 12:21:15 2010 $                                           *
28 * $Author: ing07385 $                                                         *
29 * $Revision: 1.53 $                                                            *
30 * $Aliases: NFC_FRI1.1_WK1007_R33_6 $                                                                *
31 *                                                                             *
32 * =========================================================================== *
33 */
34
35 /*
36 ***************************** Header File Inclusion ****************************
37 */
38 #include <phNfcConfig.h>
39 #include <phNfcCompId.h>
40 #include <phHciNfc_Pipe.h>
41 #include <phHciNfc_RFReader.h>
42 #include <phHciNfc_RFReaderA.h>
43 #ifdef TYPE_B
44 #include <phHciNfc_RFReaderB.h>
45 #endif
46 #ifdef ENABLE_P2P
47 #include <phHciNfc_NfcIPMgmt.h>
48 #endif
49 #ifdef TYPE_FELICA
50 #include <phHciNfc_Felica.h>
51 #endif
52 #ifdef TYPE_JEWEL
53 #include <phHciNfc_Jewel.h>
54 #endif
55 #ifdef TYPE_ISO15693
56 #include <phHciNfc_ISO15693.h>
57 #endif /* #ifdef    TYPE_ISO15693 */
58 #include <phOsalNfc.h>
59
60 /*
61 ****************************** Macro Definitions *******************************
62 */
63
64 #define NFCIP_ACTIVATE_DELAY       0x05U
65
66 uint8_t nxp_nfc_isoxchg_timeout = NXP_ISO_XCHG_TIMEOUT;
67 /*
68 *************************** Structure and Enumeration ***************************
69 */
70
71
72 /** \defgroup grp_hci_nfc HCI Reader RF Management Component
73  *
74  *
75  */
76
77
78 typedef enum phHciNfc_ReaderMgmt_Seq{
79     READERA_PIPE_OPEN       = 0x00U,
80     READERB_PIPE_OPEN,
81     FELICA_PROP_PIPE_OPEN,
82     JEWEL_PROP_PIPE_OPEN,
83     ISO15693_PROP_PIPE_OPEN,
84     NFCIP1_INITIATOR_PIPE_OPEN,
85     NFCIP1_INITIATOR_MODE_CONFIG,
86     NFCIP1_INITIATOR_PSL1_CONFIG,
87     NFCIP1_INITIATOR_PSL2_CONFIG,
88     READERA_DISABLE_AUTO_ACTIVATE,
89
90
91     READERA_PIPE_CLOSE,
92     READERB_PIPE_CLOSE,
93     FELICA_PROP_PIPE_CLOSE,
94     JEWEL_PROP_PIPE_CLOSE,
95     ISO15693_PROP_PIPE_CLOSE,
96     NFCIP1_INITIATOR_PIPE_CLOSE,
97     END_READER_SEQUENCE
98 } phHciNfc_ReaderMgmt_Seq_t;
99
100 typedef struct phHciNfc_ReaderMgmt_Info{
101     phHciNfc_ReaderMgmt_Seq_t   rf_gate_cur_seq;
102     phHciNfc_ReaderMgmt_Seq_t   rf_gate_next_seq;
103 } phHciNfc_ReaderMgmt_Info_t;
104
105
106 /*
107 *************************** Static Function Declaration **************************
108 */
109 static
110 NFCSTATUS
111 phHciNfc_ReaderMgmt_End_Discovery(
112                                 phHciNfc_sContext_t     *psHciContext,
113                                 void                    *pHwRef,
114                                 uint8_t                 reader_pipe_id
115                              );
116
117 static
118 NFCSTATUS
119 phHciNfc_ReaderMgmt_Initiate_Discovery(
120                                 phHciNfc_sContext_t     *psHciContext,
121                                 void                    *pHwRef,
122                                 uint8_t                 reader_pipe_id
123                              );
124
125 /*
126 *************************** Function Definitions ***************************
127 */
128
129 #ifdef READER_INIT
130 /*!
131  * \brief Allocates the resources of RF Reader Managment Gate.
132  *
133  * This function Allocates the resources of the RF Reader Management
134  * gate Information Structure.
135  * 
136  */
137
138 NFCSTATUS
139 phHciNfc_ReaderMgmt_Init_Resources(
140                                 phHciNfc_sContext_t     *psHciContext
141                              )
142 {
143     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
144     phHciNfc_ReaderMgmt_Info_t  *p_reader_mgmt_info=NULL;
145     if( NULL == psHciContext )
146     {
147         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
148     }
149     else
150     {
151         if( ( NULL == psHciContext->p_reader_mgmt_info )
152             && (phHciNfc_Allocate_Resource((void **)(&p_reader_mgmt_info),
153                     sizeof(phHciNfc_ReaderMgmt_Info_t))== NFCSTATUS_SUCCESS)
154         )
155         {
156             psHciContext->p_reader_mgmt_info = p_reader_mgmt_info;
157             p_reader_mgmt_info->rf_gate_cur_seq = READERA_PIPE_OPEN;
158             p_reader_mgmt_info->rf_gate_next_seq = END_READER_SEQUENCE;
159         }
160         else
161         {
162             status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
163         }
164     }
165     return status;
166 }
167
168 #endif
169
170 /*!
171  * \brief Updates the Sequence of RF Reader Managment Gate.
172  *
173  * This function Resets/Updates the sequence of the RF Reader Management
174  * gate.
175  * 
176  */
177
178 NFCSTATUS
179 phHciNfc_ReaderMgmt_Update_Sequence(
180                                 phHciNfc_sContext_t     *psHciContext,
181                                 phHciNfc_eSeqType_t     reader_seq
182                              )
183 {
184     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
185     phHciNfc_ReaderMgmt_Info_t  *p_reader_mgmt_info = NULL;
186     if( NULL == psHciContext )
187     {
188         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
189     }
190     else
191     {
192         if( NULL == psHciContext->p_reader_mgmt_info )
193         {
194             status = PHNFCSTVAL(CID_NFC_HCI, 
195                         NFCSTATUS_INVALID_HCI_INFORMATION);
196         }
197         else
198         {
199             p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
200                                 psHciContext->p_reader_mgmt_info ;
201             switch(reader_seq)
202             {
203                 case RESET_SEQ:
204                 case INIT_SEQ:
205                 {
206                     p_reader_mgmt_info->rf_gate_cur_seq = READERA_PIPE_OPEN;
207                     p_reader_mgmt_info->rf_gate_next_seq = END_READER_SEQUENCE;
208                     break;
209                 }
210                 case UPDATE_SEQ:
211                 {
212                     p_reader_mgmt_info->rf_gate_cur_seq = 
213                                             p_reader_mgmt_info->rf_gate_next_seq;
214                     break;
215                 }
216                 case INFO_SEQ:
217                 {
218                     status = phHciNfc_ReaderA_Update_Info(psHciContext, 
219                                             HCI_READER_A_INFO_SEQ, NULL);
220 #if defined( TYPE_B )
221                     status = phHciNfc_ReaderB_Update_Info(psHciContext, 
222                                             HCI_READER_B_INFO_SEQ, NULL);
223 #endif /* end of #if defined(TYPE_B) */
224 #if defined( TYPE_FELICA )
225                     status = phHciNfc_Felica_Update_Info(psHciContext, 
226                                             HCI_FELICA_INFO_SEQ, NULL);
227 #endif /* end of #if defined(TYPE_FELICA) */
228 #if defined( TYPE_JEWEL )
229                     status = phHciNfc_Jewel_Update_Info(psHciContext, 
230                                             HCI_JEWEL_INFO_SEQ, NULL);
231 #endif /* end of #if defined(TYPE_JEWEL) */
232 #if defined( TYPE_ISO15693 )
233                     status = phHciNfc_ISO15693_Update_Info(psHciContext, 
234                                             HCI_ISO_15693_INFO_SEQ, NULL);
235 #endif /* end of #if defined(TYPE_ISO15693) */
236                     break;
237                 }
238                 case REL_SEQ:
239                 {
240                     p_reader_mgmt_info->rf_gate_cur_seq = READERA_PIPE_CLOSE;
241                     p_reader_mgmt_info->rf_gate_next_seq = END_READER_SEQUENCE;
242                     break;
243                 }
244                 default:
245                 {
246                     break;
247                 }
248             }
249         }
250     }
251
252     return status;
253
254 }
255
256
257 /*!
258  * \brief Initialisation of RF Reader Managment Gate.
259  *
260  * This function initialses the RF Reader Management gate and 
261  * populates the Reader Management Information Structure
262  * 
263  */
264
265 NFCSTATUS
266 phHciNfc_ReaderMgmt_Initialise(
267                                 phHciNfc_sContext_t     *psHciContext,
268                                 void                    *pHwRef
269                          )
270 {
271     NFCSTATUS                       status = NFCSTATUS_SUCCESS;
272     phHciNfc_Pipe_Info_t            *p_pipe_info = NULL;
273     phHciNfc_ReaderMgmt_Info_t      *p_reader_mgmt_info=NULL;
274
275     if( NULL == psHciContext )
276     {
277         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
278     }
279     else
280     {
281
282         if( ( NULL == psHciContext->p_reader_mgmt_info )
283             && (phHciNfc_Allocate_Resource((void **)(&p_reader_mgmt_info),
284                     sizeof(phHciNfc_ReaderMgmt_Info_t))== NFCSTATUS_SUCCESS)
285         )
286         {
287             psHciContext->p_reader_mgmt_info = p_reader_mgmt_info;
288             p_reader_mgmt_info->rf_gate_cur_seq = READERA_PIPE_OPEN;
289             p_reader_mgmt_info->rf_gate_next_seq = END_READER_SEQUENCE;
290         }
291         else
292         {
293             p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
294                                 psHciContext->p_reader_mgmt_info ;
295         }
296
297         if( NULL == psHciContext->p_reader_mgmt_info )
298         {
299             status = PHNFCSTVAL(CID_NFC_HCI, 
300                                 NFCSTATUS_INSUFFICIENT_RESOURCES);
301         }
302 #ifdef ESTABLISH_SESSION
303         else if( hciMode_Session == psHciContext->hci_mode )
304         {
305             status = NFCSTATUS_SUCCESS;
306         }
307 #endif
308         else
309         {
310             switch(p_reader_mgmt_info->rf_gate_cur_seq )
311             {
312                 /* Reader A pipe open sequence */
313                 case READERA_PIPE_OPEN:
314                 {
315                     p_pipe_info = ((phHciNfc_ReaderA_Info_t *) 
316                             psHciContext->p_reader_a_info)->p_pipe_info;
317                     if(NULL == p_pipe_info )
318                     {
319                         status = PHNFCSTVAL(CID_NFC_HCI, 
320                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
321                     }
322                     else
323                     {
324                         status = phHciNfc_Open_Pipe( psHciContext,
325                                                         pHwRef, p_pipe_info );
326                         if(status == NFCSTATUS_SUCCESS)
327                         {
328                             uint8_t rdr_enable = TRUE;
329                             status = phHciNfc_ReaderA_Update_Info( 
330                                     psHciContext, HCI_READER_A_ENABLE, 
331                                                             &rdr_enable);
332 #if defined( TYPE_B )  && defined ( ENABLE_AUTO_ACTIVATE )
333                             p_reader_mgmt_info->rf_gate_next_seq = 
334                                                     READERB_PIPE_OPEN;
335                             status = NFCSTATUS_PENDING;
336 /* end of #ifdef TYPE_B */
337 #elif !defined( ENABLE_AUTO_ACTIVATE )
338                             p_reader_mgmt_info->rf_gate_next_seq = 
339                                                         READERA_DISABLE_AUTO_ACTIVATE;
340                             status = NFCSTATUS_PENDING;
341 /* #ifdef ENABLE_AUTO_ACTIVATE */
342 #elif defined( ENABLE_P2P )
343                             p_reader_mgmt_info->rf_gate_next_seq = 
344                                                     NFCIP1_INITIATOR_PIPE_OPEN;
345                             status = NFCSTATUS_PENDING;
346 /* #ifdef ENABLE_P2P */
347 #else
348                             p_reader_mgmt_info->rf_gate_next_seq = 
349                                                 END_READER_SEQUENCE;
350                             /* status = NFCSTATUS_PENDING; */
351 #endif 
352                         }
353                     }
354                     break;
355                 }
356                 /* Reader A Auto Activate Disable */
357                 case READERA_DISABLE_AUTO_ACTIVATE:
358                 {
359                     uint8_t     activate_enable = FALSE;
360                     p_pipe_info = ((phHciNfc_ReaderA_Info_t *) 
361                             psHciContext->p_reader_a_info)->p_pipe_info;
362                     if(NULL == p_pipe_info )
363                     {
364                         status = PHNFCSTVAL(CID_NFC_HCI, 
365                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
366                     }
367                     else
368                     {
369
370                         status = phHciNfc_ReaderA_Auto_Activate( psHciContext,
371                                                         pHwRef, activate_enable );
372                         if(status == NFCSTATUS_SUCCESS)
373                         {
374 #if defined (TYPE_B)
375                             p_reader_mgmt_info->rf_gate_next_seq = 
376                                                     READERB_PIPE_OPEN;
377                             status = NFCSTATUS_PENDING;
378 /* end of #ifdef TYPE_B */
379 #elif defined(TYPE_FELICA)
380                             p_reader_mgmt_info->rf_gate_next_seq = 
381                                                         FELICA_PROP_PIPE_OPEN;
382                             status = NFCSTATUS_PENDING;
383 /* end of #elif defined(TYPE_FELICA) */
384 #elif defined(TYPE_JEWEL)
385                             p_reader_mgmt_info->rf_gate_next_seq = 
386                                                         JEWEL_PROP_PIPE_OPEN;
387                             status = NFCSTATUS_PENDING;
388 /* end of #elif defined(TYPE_JEWEL) */
389 #elif defined (TYPE_ISO15693)
390                             p_reader_mgmt_info->rf_gate_next_seq = 
391                                                     ISO15693_PROP_PIPE_OPEN;
392                             status = NFCSTATUS_PENDING;
393 /* end of #elif defined(TYPE_ISO15693) */
394 #elif defined(ENABLE_P2P)
395                             p_reader_mgmt_info->rf_gate_next_seq = 
396                                                     NFCIP1_INITIATOR_PIPE_OPEN;
397                             status = NFCSTATUS_PENDING;
398 /* end of #ifdef ENABLE_P2P */
399 #else
400                             p_reader_mgmt_info->rf_gate_next_seq = 
401                                                             END_READER_SEQUENCE;
402                             /* status = NFCSTATUS_PENDING; */
403 #endif /* #if !defined(ENABLE_P2P) && !defined(TYPE_B)*/
404                         }
405                     }
406                     break;
407                 }
408 #ifdef TYPE_B
409                 /* Reader B pipe open sequence */
410                 case READERB_PIPE_OPEN:
411                 {
412                     p_pipe_info = ((phHciNfc_ReaderB_Info_t *) 
413                             psHciContext->p_reader_b_info)->p_pipe_info;
414                     if(NULL == p_pipe_info )
415                     {
416                         status = PHNFCSTVAL(CID_NFC_HCI, 
417                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
418                     }
419                     else
420                     {
421                         status = phHciNfc_Open_Pipe( psHciContext,
422                                                         pHwRef, p_pipe_info );
423                         if(status == NFCSTATUS_SUCCESS)
424                         {
425 #if defined(TYPE_FELICA)
426                             p_reader_mgmt_info->rf_gate_next_seq = 
427                                                         FELICA_PROP_PIPE_OPEN;
428                             status = NFCSTATUS_PENDING;
429 /* end of #ifdef TYPE_FELICA */
430 #elif defined(TYPE_JEWEL)
431                             p_reader_mgmt_info->rf_gate_next_seq = 
432                                                         JEWEL_PROP_PIPE_OPEN;
433                             status = NFCSTATUS_PENDING;
434 /* end of #elif defined(TYPE_JEWEL) */
435 #elif defined (TYPE_ISO15693)
436                             p_reader_mgmt_info->rf_gate_next_seq = 
437                                                     ISO15693_PROP_PIPE_OPEN;
438                             status = NFCSTATUS_PENDING;
439 /* end of #elif defined(TYPE_ISO15693) */
440 #elif defined(ENABLE_P2P)               
441                             p_reader_mgmt_info->rf_gate_next_seq = 
442                                                     NFCIP1_INITIATOR_PIPE_OPEN;
443                             status = NFCSTATUS_PENDING;
444 /* end of #ifdef ENABLE_P2P */
445 #else
446                             p_reader_mgmt_info->rf_gate_next_seq = 
447                                                             END_READER_SEQUENCE;
448                             /* status = NFCSTATUS_PENDING; */
449 #endif /* #if !defined(ENABLE_P2P) && !defined(TYPE_FELICA)*/
450                         }
451                     }
452                     break;
453                 }
454 #endif /* #ifdef TYPE_B */
455 #ifdef TYPE_FELICA
456                 /* Felica Reader pipe open sequence */
457                 case FELICA_PROP_PIPE_OPEN:
458                 {
459                     p_pipe_info = ((phHciNfc_Felica_Info_t *) 
460                             psHciContext->p_felica_info)->p_pipe_info;
461                     if(NULL == p_pipe_info )
462                     {
463                         status = PHNFCSTVAL(CID_NFC_HCI, 
464                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
465                     }
466                     else
467                     {
468                         status = phHciNfc_Open_Pipe( psHciContext,
469                                                         pHwRef, p_pipe_info );
470                         if(status == NFCSTATUS_SUCCESS)
471                         {
472 #if defined(TYPE_JEWEL)
473                             p_reader_mgmt_info->rf_gate_next_seq = 
474                                                         JEWEL_PROP_PIPE_OPEN;
475                             status = NFCSTATUS_PENDING;
476 /* end of #if defined(TYPE_JEWEL) */
477 #elif defined (TYPE_ISO15693)
478                             p_reader_mgmt_info->rf_gate_next_seq = 
479                                                     ISO15693_PROP_PIPE_OPEN;
480                             status = NFCSTATUS_PENDING;
481 /* end of #elif defined(TYPE_ISO15693) */
482 #elif defined(ENABLE_P2P)               
483                             p_reader_mgmt_info->rf_gate_next_seq = 
484                                                     NFCIP1_INITIATOR_PIPE_OPEN;
485                             status = NFCSTATUS_PENDING;
486  /* end of #ifdef ENABLE_P2P */
487 #else
488                             p_reader_mgmt_info->rf_gate_next_seq = 
489                                                             END_READER_SEQUENCE;
490                             /* status = NFCSTATUS_PENDING; */
491 #endif /* #if !defined(ENABLE_P2P) */
492                         }
493                     }
494                     break;
495                 }
496 #endif
497 #ifdef TYPE_JEWEL
498                 /* Jewel Reader pipe open sequence */
499                 case JEWEL_PROP_PIPE_OPEN:
500                 {
501                     p_pipe_info = ((phHciNfc_Jewel_Info_t *) 
502                             psHciContext->p_jewel_info)->p_pipe_info;
503                     if(NULL == p_pipe_info )
504                     {
505                         status = PHNFCSTVAL(CID_NFC_HCI, 
506                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
507                     }
508                     else
509                     {
510                         status = phHciNfc_Open_Pipe( psHciContext,
511                                                         pHwRef, p_pipe_info );
512                         if(status == NFCSTATUS_SUCCESS)
513                         {
514 #if defined (TYPE_ISO15693)
515                             p_reader_mgmt_info->rf_gate_next_seq = 
516                                                     ISO15693_PROP_PIPE_OPEN;
517                             status = NFCSTATUS_PENDING;
518 /* end of #if defined(TYPE_ISO15693) */
519 #elif defined (ENABLE_P2P)
520                             p_reader_mgmt_info->rf_gate_next_seq = 
521                                                     NFCIP1_INITIATOR_PIPE_OPEN;
522                             status = NFCSTATUS_PENDING;
523  /* end of #ifdef ENABLE_P2P */
524 #else
525                             p_reader_mgmt_info->rf_gate_next_seq = 
526                                                             END_READER_SEQUENCE;
527                             /* status = NFCSTATUS_PENDING; */
528 #endif /* #if !defined(ENABLE_P2P) */
529                         }
530                     }
531                     break;
532                 }
533 #endif
534
535 #ifdef TYPE_ISO15693
536                 /* ISO15693 Reader pipe open sequence */
537                 case ISO15693_PROP_PIPE_OPEN:
538                 {
539                     p_pipe_info = ((phHciNfc_ISO15693_Info_t *) 
540                         psHciContext->p_iso_15693_info)->ps_15693_pipe_info;
541                     if(NULL == p_pipe_info )
542                     {
543                         status = PHNFCSTVAL(CID_NFC_HCI, 
544                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
545                     }
546                     else
547                     {
548                         status = phHciNfc_Open_Pipe( psHciContext,
549                                                         pHwRef, p_pipe_info );
550                         if(status == NFCSTATUS_SUCCESS)
551                         {
552 #ifdef ENABLE_P2P               
553                             p_reader_mgmt_info->rf_gate_next_seq = 
554                                                     NFCIP1_INITIATOR_PIPE_OPEN;
555                             status = NFCSTATUS_PENDING;
556  /* end of #ifdef ENABLE_P2P */
557 #else
558                             p_reader_mgmt_info->rf_gate_next_seq = 
559                                                             END_READER_SEQUENCE;
560                             /* status = NFCSTATUS_PENDING; */
561 #endif /* #if !defined(ENABLE_P2P) */
562                         }
563                     }
564                     break;
565                 }
566
567 #endif
568
569 #ifdef ENABLE_P2P               
570                 /* NFC-IP1 Initiator pipe open sequence */
571                 case NFCIP1_INITIATOR_PIPE_OPEN:
572                 {
573                     p_pipe_info = 
574                         ((phHciNfc_NfcIP_Info_t *)psHciContext->
575                                     p_nfcip_info)->p_init_pipe_info;
576                     if(NULL == p_pipe_info )
577                     {
578                         status = PHNFCSTVAL(CID_NFC_HCI, 
579                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
580                     }
581                     else
582                     {
583                         status = phHciNfc_Open_Pipe( psHciContext,
584                                                         pHwRef, p_pipe_info );
585                         if(status == NFCSTATUS_SUCCESS)
586                         {
587                             p_reader_mgmt_info->rf_gate_next_seq = 
588                                                 NFCIP1_INITIATOR_MODE_CONFIG;
589                             status = NFCSTATUS_PENDING;
590                         }
591                     }
592                     break;
593                 }
594                 case NFCIP1_INITIATOR_MODE_CONFIG:
595                 {
596                     uint8_t mode = DEFAULT_NFCIP_INITIATOR_MODE_SUPPORT;
597                     status = phHciNfc_NfcIP_SetMode( psHciContext, pHwRef,
598                                                 NFCIP_INITIATOR, mode);
599                     if(status == NFCSTATUS_PENDING )
600                     {
601                         p_reader_mgmt_info->rf_gate_next_seq = 
602                                             NFCIP1_INITIATOR_PSL1_CONFIG;
603                         /* status = NFCSTATUS_SUCCESS; */
604                     }
605                     break;
606                 }
607                 case NFCIP1_INITIATOR_PSL1_CONFIG:
608                 {
609                     uint8_t psl_config = NXP_NFCIP_PSL_BRS_DEFAULT;
610                     status = phHciNfc_NfcIP_SetPSL1( psHciContext, pHwRef,
611                                                 psl_config);
612                     if(status == NFCSTATUS_PENDING )
613                     {
614                         p_reader_mgmt_info->rf_gate_next_seq = 
615                                                         END_READER_SEQUENCE;
616                         status = NFCSTATUS_SUCCESS;
617                     }
618                     break;
619                 }
620 #endif
621                 default:
622                 {
623                     status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_SEQUENCE);
624                     break;
625                 }
626
627             }/* End of the Sequence Switch */
628
629         }/* End of the Reader Info Memory Check */
630
631     } /* End of Null Context Check */
632
633     return status;
634 }
635
636
637 /*!
638  * \brief Initiate the Discovery for the RF Reader .
639  *
640  * This function starts the Polling Loop and initiates the discovery 
641  * of the Target.
642  * 
643  */
644 static
645 NFCSTATUS
646 phHciNfc_ReaderMgmt_Initiate_Discovery(
647                                 phHciNfc_sContext_t     *psHciContext,
648                                 void                    *pHwRef,
649                                 uint8_t                 reader_pipe_id
650                              )
651 {
652     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
653
654     if( ( NULL == psHciContext )
655         || ( NULL == pHwRef )
656         || ( HCI_UNKNOWN_PIPE_ID == reader_pipe_id)
657         )
658     {
659         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
660     }
661     else
662     {
663
664         status = phHciNfc_Send_RFReader_Event ( psHciContext, pHwRef, 
665                     reader_pipe_id,(uint8_t) EVT_READER_REQUESTED );
666         status = ( (status == NFCSTATUS_PENDING)?
667                                 NFCSTATUS_SUCCESS : status);
668     }
669     return status;
670 }
671
672
673 /*!
674  * \brief End the Discovery of the RF Reader .
675  *
676  * This function stops the Polling Loop and ends the discovery 
677  * of the Target.
678  * 
679  */
680 static
681 NFCSTATUS
682 phHciNfc_ReaderMgmt_End_Discovery(
683                                 phHciNfc_sContext_t     *psHciContext,
684                                 void                    *pHwRef,
685                                 uint8_t                 reader_pipe_id
686                              )
687 {
688     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
689
690     if( ( NULL == psHciContext )
691         || ( NULL == pHwRef )
692         || ( HCI_UNKNOWN_PIPE_ID == reader_pipe_id)
693         )
694     {
695         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
696     }
697     else
698     {
699
700         status = phHciNfc_Send_RFReader_Event ( psHciContext, pHwRef, 
701                     reader_pipe_id,(uint8_t) EVT_END_OPERATION );
702         status = ( (status == NFCSTATUS_PENDING)?
703                                 NFCSTATUS_SUCCESS : status);
704     }
705     return status;
706 }
707
708
709 /*!
710  * \brief Enable the Discovery of RF Reader Managment Gate.
711  *
712  * This function Enable the discovery of the RF Reader Management
713  * gate.
714  * 
715  */
716
717
718 NFCSTATUS
719 phHciNfc_ReaderMgmt_Enable_Discovery(
720                                 phHciNfc_sContext_t     *psHciContext,
721                                 void                    *pHwRef
722                              )
723 {
724     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
725     phHciNfc_ReaderMgmt_Info_t  *p_reader_mgmt_info=NULL;
726     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
727     /* phHal_sADD_Cfg_t    *p_poll_config = (phHal_sADD_Cfg_t * )
728                                             psHciContext->p_config_params; */
729     PHNFC_UNUSED_VARIABLE(p_reader_mgmt_info);
730     if( NULL != psHciContext->p_reader_mgmt_info )
731     {
732         uint8_t rdr_enable = FALSE;
733         p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
734                             psHciContext->p_reader_mgmt_info ;
735 #ifdef TYPE_B
736         if ( (NULL != psHciContext->p_reader_b_info )
737              /*   && (FALSE == rdr_enable) */
738         )
739         {
740             /* Get the Reader B Pipe ID */
741             status = phHciNfc_ReaderB_Get_PipeID
742                                     (psHciContext, &reader_pipe_id);
743
744             if( NFCSTATUS_SUCCESS == status )
745             {
746                 rdr_enable = (uint8_t)TRUE;
747                 /* rdr_enable = (uint8_t)
748                         p_poll_config->PollDevInfo.PollCfgInfo.EnableIso14443B; */
749                 status =    phHciNfc_ReaderB_Update_Info(psHciContext, 
750                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
751             }
752         }
753 #endif
754 #ifdef TYPE_FELICA
755         if ( (NULL != psHciContext->p_felica_info )
756                /* && (FALSE == rdr_enable) */
757         )
758         {
759             /* Get the Reader F Pipe ID */
760             status = phHciNfc_Felica_Get_PipeID
761                                     (psHciContext, &reader_pipe_id);
762
763             if( NFCSTATUS_SUCCESS == status )
764             {
765                 rdr_enable = (uint8_t)TRUE;
766                /* rdr_enable = (uint8_t)
767                     ( p_poll_config->PollDevInfo.PollCfgInfo.EnableFelica212
768                         || p_poll_config->PollDevInfo.PollCfgInfo.EnableFelica424 ); */
769                 status =    phHciNfc_Felica_Update_Info(psHciContext, 
770                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
771             }
772         }
773 #endif
774 #ifdef TYPE_JEWEL
775         if ( (NULL != psHciContext->p_jewel_info )
776                /* && (FALSE == rdr_enable) */
777         )
778         {
779             /* Get the Reader F Pipe ID */
780             status = phHciNfc_Jewel_Get_PipeID
781                                     (psHciContext, &reader_pipe_id);
782
783             if( NFCSTATUS_SUCCESS == status )
784             {
785                 rdr_enable = (uint8_t)TRUE;
786                 status =    phHciNfc_Jewel_Update_Info(psHciContext, 
787                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
788             }
789         }
790 #endif /* #ifdef TYPE_JEWEL */
791 #if defined(TYPE_ISO15693)
792         if ( (NULL != psHciContext->p_iso_15693_info )
793                /* && (FALSE == rdr_enable) */
794         )
795         {
796             /* Get the Reader F Pipe ID */
797             status = phHciNfc_ISO15693_Get_PipeID
798                                     (psHciContext, &reader_pipe_id);
799
800             if( NFCSTATUS_SUCCESS == status )
801             {
802                 rdr_enable = (uint8_t)TRUE;
803                 status =    phHciNfc_ISO15693_Update_Info(psHciContext, 
804                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
805             }
806         }
807
808 /* end of #elif defined(TYPE_ISO15693) */
809 #endif
810
811         if(NULL != psHciContext->p_reader_a_info)
812         {
813             /* Get the Reader A Pipe ID */
814             status = phHciNfc_ReaderA_Get_PipeID
815                                     (psHciContext, &reader_pipe_id);
816
817             if( NFCSTATUS_SUCCESS == status )
818             {
819                 rdr_enable = (uint8_t)TRUE;
820                 status =    phHciNfc_ReaderA_Update_Info(psHciContext, 
821                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
822             }
823
824         }
825         if( ( NFCSTATUS_SUCCESS == status )
826             && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
827           )
828         {
829             status = phHciNfc_ReaderMgmt_Initiate_Discovery( psHciContext,
830                          pHwRef, reader_pipe_id);
831         }
832
833     }/* End of the Reader Info Memory Check */
834
835     return status;
836 }
837
838 /*!
839  * \brief Disable the Discovery of RF Reader Managment Gate.
840  *
841  * This function Disable the discovery of the RF Reader Management
842  * gate.
843  * 
844  */
845
846 NFCSTATUS
847 phHciNfc_ReaderMgmt_Disable_Discovery(
848                                 phHciNfc_sContext_t     *psHciContext,
849                                 void                    *pHwRef
850                              )
851 {
852     NFCSTATUS                   status = NFCSTATUS_FAILED;
853     phHciNfc_ReaderMgmt_Info_t  *p_reader_mgmt_info=NULL;
854     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
855     /* phHal_sADD_Cfg_t         *p_poll_config = (phHal_sADD_Cfg_t * )
856                                             psHciContext->p_config_params; */
857     PHNFC_UNUSED_VARIABLE(p_reader_mgmt_info);
858     if( NULL != psHciContext->p_reader_mgmt_info )
859     {
860         p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
861                             psHciContext->p_reader_mgmt_info ;
862         if(NULL != psHciContext->p_reader_a_info)
863         {
864             /* Get the Reader A Pipe ID */
865             status = phHciNfc_ReaderA_Get_PipeID
866                                     (psHciContext, &reader_pipe_id);
867 #if 0
868             if( NFCSTATUS_SUCCESS == status )
869             {
870                 uint8_t rdr_enable = (uint8_t) FALSE;
871                 status =    phHciNfc_ReaderA_Update_Info(psHciContext, 
872                                         HCI_RDR_ENABLE_TYPE, &rdr_enable);
873             }
874 #endif
875
876         }
877 #ifdef TYPE_B
878         else if((NULL != psHciContext->p_reader_b_info )
879             /* && (NFCSTATUS_SUCCESS != status) */
880             )
881         {
882             /* Get the Reader B Pipe ID */
883             status = phHciNfc_ReaderB_Get_PipeID
884                                     (psHciContext, &reader_pipe_id);
885         }
886 #endif
887 #ifdef TYPE_FELICA
888         else if((NULL != psHciContext->p_felica_info )
889             /* && (NFCSTATUS_SUCCESS != status) */
890             )
891         {
892             /* Get the Reader B Pipe ID */
893             status = phHciNfc_Felica_Get_PipeID
894                                     (psHciContext, &reader_pipe_id);
895         }
896 #endif
897 #ifdef TYPE_JEWEL
898         else if((NULL != psHciContext->p_jewel_info )
899             /* && (NFCSTATUS_SUCCESS != status) */
900             )
901         {
902             /* Get the Reader B Pipe ID */
903             status = phHciNfc_Jewel_Get_PipeID
904                                     (psHciContext, &reader_pipe_id);
905         }
906 #endif /* #ifdef TYPE_JEWEL */
907 #ifdef  TYPE_ISO15693
908         else if((NULL != psHciContext->p_iso_15693_info )
909             /* && (NFCSTATUS_SUCCESS != status) */
910             )
911         {
912             /* Get the Reader B Pipe ID */
913             status = phHciNfc_ISO15693_Get_PipeID
914                                     (psHciContext, &reader_pipe_id);
915         }               
916 #endif /* #ifdef    TYPE_ISO15693 */
917
918         else
919         {
920             status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_NOT_ALLOWED);
921         }
922
923         if( (NFCSTATUS_SUCCESS == status)
924             && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
925             )
926         {
927             status = phHciNfc_ReaderMgmt_End_Discovery( psHciContext,
928                          pHwRef, reader_pipe_id);
929         }
930
931     }/* End of the Reader Info Memory Check */
932
933     return status;
934 }
935
936
937
938
939 /*!
940 * \brief Updates the Sequence of RF Reader Managment Gate.
941 *
942 * This function Resets/Updates the sequence of the RF Reader Management
943 * gate.
944
945 */
946
947 NFCSTATUS
948 phHciNfc_ReaderMgmt_Info_Sequence(
949                                    phHciNfc_sContext_t      *psHciContext,
950                                    void                     *pHwRef
951                                )
952 {
953 #if defined(NXP_NFCIP_ACTIVATE_DELAY)
954     static uint8_t              nfc_atr_retry = 0;
955 #endif
956     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
957     phHciNfc_ReaderMgmt_Info_t  *p_reader_mgmt_info=NULL;
958 #if 0
959     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
960     phHal_eRemDevType_t         target_type = phHal_eUnknown_DevType;
961 #endif
962
963
964     PHNFC_UNUSED_VARIABLE(p_reader_mgmt_info);
965     if( NULL == psHciContext )
966     {
967         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
968     }
969     else
970     {
971         if( NULL != psHciContext->p_reader_mgmt_info )
972         {
973             p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
974                 psHciContext->p_reader_mgmt_info ;
975             switch( psHciContext->host_rf_type )
976             {
977
978                 case phHal_eISO14443_A_PCD:
979                 {
980                     /* If the Target Info is updated then the Target 
981                      * is connected.
982                      */
983                     if(NULL == psHciContext->p_target_info)
984                     {
985 #if defined(NXP_NFCIP_ACTIVATE_DELAY)
986                         nfc_atr_retry = 0;
987 #endif
988                         status = phHciNfc_ReaderA_Info_Sequence( 
989                                                     psHciContext, pHwRef );
990                     }
991                     else
992                     {
993                             status = phHciNfc_ReaderA_App_Data( 
994                                                 psHciContext, pHwRef );
995                             status = ((NFCSTATUS_PENDING == status )?
996                                         NFCSTATUS_SUCCESS : status);
997                     }
998                     break;
999                 }
1000 #ifdef ENABLE_P2P
1001                 case phHal_eNfcIP1_Initiator:
1002                 {
1003                     /* If the Target Info is updated then the Target 
1004                      * is connected.
1005                      */
1006 #ifdef NFCIP_CHECK
1007                     if(NULL == psHciContext->p_target_info)
1008 #endif
1009                     {
1010                         status = phHciNfc_NfcIP_Info_Sequence( 
1011                                   psHciContext, pHwRef
1012 #ifdef NOTIFY_REQD
1013                                     ,(NULL == psHciContext->p_target_info)
1014 #endif /* #ifdef NOTIFY_REQD */
1015                                            );
1016                     }
1017 #ifdef NFCIP_CHECK
1018                     else
1019                     {
1020                         status = phHciNfc_NfcIP_GetATRInfo( 
1021                                             psHciContext, pHwRef, NFCIP_INITIATOR );
1022 #if defined(NXP_NFCIP_ACTIVATE_DELAY)
1023                         if (
1024                             (NFCSTATUS_PENDING == status)
1025                             && ( NFCIP_ACTIVATE_DELAY <= nfc_atr_retry)
1026                             )
1027                         {
1028                             nfc_atr_retry = 0;
1029                             status = NFCSTATUS_SUCCESS;
1030                         }
1031                         else
1032                         {
1033                             nfc_atr_retry++;
1034                         }
1035 #else
1036                         status = ((NFCSTATUS_PENDING == status )?
1037                                         NFCSTATUS_SUCCESS : status);
1038 #endif
1039                     }
1040 #endif
1041                     break;
1042                 }
1043 #endif
1044 #ifdef TYPE_B
1045                 case phHal_eISO14443_B_PCD:
1046                 {
1047                     if(NULL == psHciContext->p_target_info)
1048                     {
1049                         status = phHciNfc_ReaderB_Info_Sequence( 
1050                                                     psHciContext, pHwRef );
1051                     }
1052                     break;
1053                 }
1054 #endif /* #ifdef TYPE_B */
1055 #ifdef TYPE_FELICA
1056                 case phHal_eFelica_PCD:
1057                 {
1058                     if(NULL == psHciContext->p_target_info)
1059                     {
1060 #if defined(NXP_NFCIP_ACTIVATE_DELAY)
1061                         nfc_atr_retry = 0;
1062 #endif
1063                         status = phHciNfc_Felica_Info_Sequence( 
1064                                                     psHciContext, pHwRef );
1065                     }
1066                     break;
1067                 }
1068 #endif /* #ifdef TYPE_FELICA */
1069 #ifdef TYPE_JEWEL
1070                 case phHal_eJewel_PCD:
1071                 {
1072                     if(NULL == psHciContext->p_target_info)
1073                     {
1074                         status = phHciNfc_Jewel_Info_Sequence( 
1075                                                     psHciContext, pHwRef );
1076                     }
1077                     break;
1078                 }
1079 #endif /* #ifdef TYPE_JEWEL */
1080 #if defined(TYPE_ISO15693)
1081                 case phHal_eISO15693_PCD:
1082                 {
1083                     if(NULL == psHciContext->p_target_info)
1084                     {
1085                         status = phHciNfc_ISO15693_Info_Sequence( 
1086                                                     psHciContext, pHwRef );
1087                     }
1088                     break;
1089                 }
1090 #endif
1091                 default:
1092                 {
1093                     break;
1094                 }
1095             }
1096
1097         }/* End of the Reader Info Memory Check */
1098
1099     } /* End of Null Context Check */
1100
1101     return status;
1102
1103 }
1104
1105
1106 /*!
1107  * \brief Connects the the selected tag via RF Reader Gates.
1108  *
1109  * This function connects the selected tags via RF Reader Gate.
1110  * This function uses the RF Reader gate based on the type of the
1111  * tag specified.
1112  */
1113
1114
1115 NFCSTATUS
1116 phHciNfc_ReaderMgmt_Select(
1117                                     phHciNfc_sContext_t     *psHciContext,
1118                                     void                    *pHwRef,
1119                                     phHal_eRemDevType_t     target_type
1120                 )
1121 {
1122     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1123     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1124
1125     if( (NULL == psHciContext) || (NULL == pHwRef) )
1126     {
1127       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1128     }
1129     else
1130     {
1131         switch (target_type)
1132         {
1133             case phHal_eMifare_PICC:
1134             case phHal_eISO14443_3A_PICC:
1135             {
1136                 /* Get the Reader A Pipe ID */
1137                 status = phHciNfc_ReaderA_Get_PipeID
1138                                         (psHciContext, &reader_pipe_id);
1139
1140                 if( (NFCSTATUS_SUCCESS == status)
1141                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1142                     )
1143                 {
1144                     status = phHciNfc_ReaderMgmt_Reactivate( 
1145                             psHciContext, pHwRef, target_type );
1146                 }
1147                 break;
1148             }
1149             case phHal_eISO14443_A_PICC:
1150             case phHal_eISO14443_4A_PICC:
1151             {
1152 #ifdef ENABLE_AUTO_ACTIVATE
1153                 /* Get the Reader A Pipe ID */
1154                 status = phHciNfc_ReaderA_Get_PipeID
1155                                         (psHciContext, &reader_pipe_id);
1156
1157                 if( (NFCSTATUS_SUCCESS == status)
1158                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1159                     )
1160                 {
1161                     status = phHciNfc_Send_RFReader_Command (psHciContext, 
1162                                     pHwRef, reader_pipe_id, NXP_WR_PRESCHECK );
1163                 }
1164 #else
1165                 status = phHciNfc_ReaderA_Cont_Activate(
1166                                                     psHciContext, pHwRef);
1167 #endif /* #ifdef ENABLE_AUTO_ACTIVATE */
1168                 break;
1169             }
1170 #ifdef TYPE_B
1171             case phHal_eISO14443_B_PICC:
1172             case phHal_eISO14443_4B_PICC:
1173             {
1174                 /* Get the Reader B Pipe ID */
1175                 status = phHciNfc_ReaderB_Get_PipeID
1176                                         (psHciContext, &reader_pipe_id);
1177
1178                 if( (NFCSTATUS_SUCCESS == status)
1179                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1180                     )
1181                 {
1182                     status = phHciNfc_Send_RFReader_Command (psHciContext, 
1183                                     pHwRef, reader_pipe_id, NXP_WR_PRESCHECK );
1184                     /* status = phHciNfc_ReaderA_Set_DataRateMax(
1185                                         psHciContext, pHwRef,  
1186                                         DATA_RATE_MAX_DEFAULT_VALUE ); */
1187                     /* status = phHciNfc_ReaderMgmt_Reactivate( 
1188                             psHciContext, pHwRef, target_type ); */
1189                 }
1190                 break;
1191             }
1192 #endif /* #ifdef TYPE_B */
1193 #ifdef TYPE_FELICA
1194             case phHal_eFelica_PICC:
1195             {
1196                 status = phHciNfc_Felica_Get_PipeID
1197                                         (psHciContext, &reader_pipe_id);
1198
1199                 if( (NFCSTATUS_SUCCESS == status)
1200                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1201                     )
1202                 {
1203                     /* Get the Reader Felica Pipe ID */
1204                     /* status = phHciNfc_ReaderA_Set_DataRateMax(
1205                                         psHciContext, pHwRef,  
1206                                         DATA_RATE_MAX_DEFAULT_VALUE ); */
1207                     status = phHciNfc_ReaderMgmt_Reactivate( 
1208                             psHciContext, pHwRef, target_type );
1209                 }
1210                 break;
1211             }
1212 #endif /* #ifdef TYPE_FELICA */
1213 #ifdef TYPE_JEWEL
1214             case phHal_eJewel_PICC:
1215             {
1216                 /* Get the Reader jewel Pipe ID */
1217                 status = phHciNfc_Jewel_Get_PipeID
1218                                         (psHciContext, &reader_pipe_id);
1219
1220                 if( (NFCSTATUS_SUCCESS == status)
1221                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1222                     )
1223                 {   
1224                     status = phHciNfc_Jewel_GetRID(
1225                                         psHciContext, pHwRef);
1226                 }
1227                 break;
1228             }
1229 #endif /* #ifdef TYPE_JEWEL */
1230 #ifdef  TYPE_ISO15693
1231             case phHal_eISO15693_PICC:
1232             {
1233                 /* Get the Reader ISO 15693 Pipe ID */
1234                 status = phHciNfc_ISO15693_Get_PipeID
1235                                         (psHciContext, &reader_pipe_id);
1236
1237                 if( (NFCSTATUS_SUCCESS == status)
1238                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1239                     )
1240                 {   
1241                     /* TODO */
1242                     status = phHciNfc_ReaderA_Set_DataRateMax(
1243                                         psHciContext, pHwRef,  
1244                                         DATA_RATE_MAX_DEFAULT_VALUE );
1245                 }
1246                 break;
1247             }
1248 #endif /* #ifdef    TYPE_ISO15693 */
1249 #ifdef ENABLE_P2P
1250             case phHal_eNfcIP1_Target:
1251             {
1252                 if ( (phHal_eISO14443_A_PCD ==
1253                             psHciContext->host_rf_type )
1254                    || (phHal_eFelica_PCD ==
1255                             psHciContext->host_rf_type )
1256                    )
1257                 {
1258                     status = phHciNfc_Initiator_Cont_Activate(
1259                                                     psHciContext, pHwRef);
1260                 }
1261                 else
1262                 {
1263                     status = phHciNfc_NfcIP_Presence_Check (psHciContext, pHwRef);
1264                 }
1265                 break;
1266             }
1267 #endif
1268 #if 0
1269             case phHal_eNfcIP1_Initiator:
1270             {
1271
1272                 break;
1273             }
1274 #endif
1275             default:
1276             {
1277                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1278                 break;
1279             }
1280
1281         } /* End of the tag_type Switch */
1282     }
1283
1284     return status;
1285 }
1286
1287 NFCSTATUS
1288 phHciNfc_ReaderMgmt_UICC_Dispatch(
1289                                     phHciNfc_sContext_t     *psHciContext,
1290                                     void                    *pHwRef,
1291                                     phHal_eRemDevType_t     target_type
1292                 )
1293 {
1294     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1295     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1296     phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
1297
1298
1299     if( (NULL == psHciContext) || (NULL == pHwRef) )
1300     {
1301       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1302     }
1303     else if (NULL == psHciContext->p_target_info)
1304     {
1305         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
1306     }
1307     else
1308     {
1309         switch (target_type)
1310         {
1311             case phHal_eISO14443_A_PICC:
1312             case phHal_eISO14443_4A_PICC:
1313             {
1314                 /* Get the Reader A Pipe ID */
1315                 status = phHciNfc_ReaderA_Get_PipeID(
1316                                     psHciContext, &reader_pipe_id);
1317                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1318                 p_pipe_info->param_info = &psHciContext->p_target_info->
1319                                     RemoteDevInfo.Iso14443A_Info.Uid;
1320                 p_pipe_info->param_length = psHciContext->p_target_info->
1321                                     RemoteDevInfo.Iso14443A_Info.UidLength;
1322
1323                 break;
1324             }
1325 #ifdef TYPE_B
1326             case phHal_eISO14443_B_PICC:
1327             case phHal_eISO14443_4B_PICC:
1328             {
1329                 /* Get the Reader B Pipe ID */
1330                 status = phHciNfc_ReaderB_Get_PipeID
1331                     (psHciContext, &reader_pipe_id);
1332
1333                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1334                 p_pipe_info->param_info = &psHciContext->p_target_info->
1335                         RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.Pupi;
1336                 p_pipe_info->param_length = PHHAL_PUPI_LENGTH;
1337                 break;
1338             }
1339 #endif /* #ifdef TYPE_B */
1340             case phHal_eMifare_PICC:
1341             case phHal_eISO14443_3A_PICC:
1342             default:
1343             {
1344                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1345                 break;
1346             }
1347
1348         } /* End of the tag_type Switch */
1349     }
1350     if( (NFCSTATUS_SUCCESS == status)
1351         && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1352         )
1353     {
1354         status = phHciNfc_Send_RFReader_Command (psHciContext, 
1355                         pHwRef, reader_pipe_id, NXP_WR_DISPATCH_TO_UICC );
1356     }
1357
1358     return status;
1359 }
1360
1361
1362 NFCSTATUS
1363 phHciNfc_ReaderMgmt_Reactivate(
1364                                     phHciNfc_sContext_t     *psHciContext,
1365                                     void                    *pHwRef,
1366                                     phHal_eRemDevType_t     target_type
1367                 )
1368 {
1369     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1370     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1371     phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
1372
1373
1374     if( (NULL == psHciContext) || (NULL == pHwRef) )
1375     {
1376       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1377     }
1378     else if (NULL == psHciContext->p_target_info)
1379     {
1380         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
1381     }
1382     else
1383     {
1384         switch (target_type)
1385         {
1386             case phHal_eISO14443_A_PICC:
1387             case phHal_eMifare_PICC:
1388             case phHal_eISO14443_4A_PICC:
1389             case phHal_eISO14443_3A_PICC:
1390             {
1391                 /* Get the Reader A Pipe ID */
1392                 status = phHciNfc_ReaderA_Get_PipeID(
1393                                     psHciContext, &reader_pipe_id);
1394                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1395                 p_pipe_info->param_info = &psHciContext->p_target_info->
1396                                     RemoteDevInfo.Iso14443A_Info.Uid;
1397                 p_pipe_info->param_length = psHciContext->p_target_info->
1398                                     RemoteDevInfo.Iso14443A_Info.UidLength;
1399
1400                 break;
1401             }
1402 #ifdef TYPE_B
1403             case phHal_eISO14443_B_PICC:
1404             case phHal_eISO14443_4B_PICC:
1405             {
1406                 /* Get the Reader B Pipe ID */
1407                 status = phHciNfc_ReaderB_Get_PipeID
1408                     (psHciContext, &reader_pipe_id);
1409
1410                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1411                 p_pipe_info->param_info = &psHciContext->p_target_info->
1412                         RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.Pupi;
1413                 p_pipe_info->param_length = PHHAL_PUPI_LENGTH;
1414                 break;
1415             }
1416 #endif /* #ifdef TYPE_B */
1417 #ifdef TYPE_FELICA
1418             case phHal_eFelica_PICC:
1419             {
1420                 /* Get the Felica Reader Pipe ID */
1421                 status = phHciNfc_Felica_Get_PipeID
1422                                         (psHciContext, &reader_pipe_id);
1423
1424                 if( (NFCSTATUS_SUCCESS == status)
1425                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1426                     )
1427                 {
1428                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1429                 p_pipe_info->param_info = &psHciContext->p_target_info->
1430                     RemoteDevInfo.Felica_Info.IDm;
1431                 p_pipe_info->param_length = PHHAL_FEL_ID_LEN;
1432                 }
1433                 break;
1434             }
1435 #endif /* #ifdef TYPE_FELICA */
1436 #ifdef ENABLE_P2P
1437             case phHal_eNfcIP1_Target:
1438             {
1439                 /* Get the Initiator Pipe ID */
1440                 status = phHciNfc_Initiator_Get_PipeID(
1441                                     psHciContext, &reader_pipe_id);
1442                 p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1443                 p_pipe_info->param_info = &psHciContext->p_target_info->
1444                                             RemoteDevInfo.NfcIP_Info.NFCID;
1445                 p_pipe_info->param_length = psHciContext->p_target_info->
1446                                         RemoteDevInfo.NfcIP_Info.NFCID_Length;
1447                 break;
1448             }
1449             case phHal_eNfcIP1_Initiator:
1450             {
1451                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1452                 break;
1453             }
1454 #endif /* #ifdef ENABLE_P2P */
1455             default:
1456             {
1457                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1458                 break;
1459             }
1460
1461         } /* End of the tag_type Switch */
1462     }
1463     if( (NFCSTATUS_SUCCESS == status)
1464         && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1465         )
1466     {
1467         status = phHciNfc_Send_RFReader_Command (psHciContext, 
1468                         pHwRef, reader_pipe_id, NXP_WR_ACTIVATE_ID );
1469     }
1470
1471     return status;
1472 }
1473
1474
1475 /*!
1476 * \brief Activates the next Remote Target in the field.
1477 *
1478 * This function selects and activates the next tag present in the field.
1479 */
1480
1481
1482 NFCSTATUS
1483 phHciNfc_ReaderMgmt_Activate_Next(
1484                                   phHciNfc_sContext_t       *psHciContext,
1485                                   void                  *pHwRef
1486                                   )
1487 {
1488     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1489     /* phHciNfc_Pipe_Info_t     *p_pipe_info = NULL; */
1490     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1491
1492     if( (NULL == psHciContext) || (NULL == pHwRef) )
1493     {
1494         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1495     }
1496     else
1497     {
1498         switch ( psHciContext->host_rf_type )
1499         {
1500             case phHal_eISO14443_A_PCD:
1501             {
1502                 /* Get the Reader A Pipe ID */
1503                 status = phHciNfc_ReaderA_Get_PipeID
1504                     (psHciContext, &reader_pipe_id);
1505
1506                 break;
1507             }
1508 #ifdef TYPE_B
1509             case phHal_eISO14443_B_PCD:
1510             {
1511                 /* Get the Reader B Pipe ID */
1512                 status = phHciNfc_ReaderB_Get_PipeID
1513                     (psHciContext, &reader_pipe_id);
1514
1515                 break;
1516             }
1517 #endif /* #ifdef TYPE_B */
1518 #ifdef TYPE_FELICA
1519             case phHal_eFelica_PCD:
1520             {
1521                 /* Get the Felica Reader Pipe ID */
1522                 status = phHciNfc_Felica_Get_PipeID
1523                                         (psHciContext, &reader_pipe_id);
1524
1525                 break;
1526             }
1527 #endif /* #ifdef TYPE_FELICA */
1528 #ifdef  TYPE_ISO15693
1529             case phHal_eISO15693_PCD:
1530             {
1531                 /* Get the ISO 15693 Reader Pipe ID */
1532                 status = phHciNfc_ISO15693_Get_PipeID
1533                                         (psHciContext, &reader_pipe_id);
1534
1535                 break;
1536             }
1537 #endif /* #ifdef TYPE_ISO15693 */
1538             default:
1539             {
1540                 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
1541                 break;
1542             }
1543
1544         } /* End of the reader_type Switch */
1545         if( (NFCSTATUS_SUCCESS == status)
1546             && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1547           )
1548         {
1549             status = phHciNfc_Send_RFReader_Command (psHciContext, 
1550                 pHwRef, reader_pipe_id, NXP_WR_ACTIVATE_NEXT );
1551         }
1552
1553     }
1554
1555     return status;
1556
1557 }
1558
1559 /*!
1560 * \brief Checks the presence of the Remote Target in the field.
1561 *
1562 * This function checks the presence of the tag present in the field.
1563 */
1564
1565
1566 NFCSTATUS
1567 phHciNfc_ReaderMgmt_Presence_Check(
1568                                   phHciNfc_sContext_t       *psHciContext,
1569                                   void                  *pHwRef
1570                                   )
1571 {
1572     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1573     /* phHciNfc_Pipe_Info_t     *p_pipe_info = NULL; */
1574     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1575     phHal_eRemDevType_t         target_type = phHal_eUnknown_DevType;
1576
1577     if( (NULL == psHciContext) || (NULL == pHwRef) )
1578     {
1579         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1580     }
1581     else
1582     {
1583         target_type = psHciContext->p_target_info->RemDevType;
1584         switch (target_type)
1585         {
1586             case phHal_eMifare_PICC:
1587             case phHal_eISO14443_3A_PICC:
1588             {
1589                 /* Get the Reader A Pipe ID */
1590                 status = phHciNfc_ReaderA_Get_PipeID
1591                     (psHciContext, &reader_pipe_id);
1592
1593                 if( (NFCSTATUS_SUCCESS == status)
1594                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1595                     )
1596                 {
1597                     status = phHciNfc_ReaderMgmt_Reactivate( 
1598                             psHciContext, pHwRef, target_type );
1599                 }
1600                 break;
1601             }
1602             case phHal_eISO14443_A_PICC:
1603             case phHal_eISO14443_4A_PICC:
1604             {
1605                 /* Get the Reader A Pipe ID */
1606                 status = phHciNfc_ReaderA_Get_PipeID
1607                     (psHciContext, &reader_pipe_id);
1608
1609                 if( (NFCSTATUS_SUCCESS == status)
1610                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1611                     )
1612                 {
1613                     status = phHciNfc_Send_RFReader_Command (psHciContext, 
1614                         pHwRef, reader_pipe_id, NXP_WR_PRESCHECK );
1615                 }
1616                 break;
1617             }
1618 #ifdef ENABLE_P2P
1619             case phHal_eNfcIP1_Target:
1620             {
1621                 status = phHciNfc_NfcIP_Presence_Check (psHciContext, pHwRef);
1622                 break;
1623             }
1624 #endif
1625 #ifdef TYPE_B
1626             case phHal_eISO14443_B_PICC:
1627             case phHal_eISO14443_4B_PICC:
1628             {
1629                 /* Get the Reader B Pipe ID */
1630                 status = phHciNfc_ReaderB_Get_PipeID
1631                     (psHciContext, &reader_pipe_id);
1632
1633                 if( (NFCSTATUS_SUCCESS == status)
1634                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1635                     )
1636                 {
1637                     status = phHciNfc_Send_RFReader_Command (psHciContext, 
1638                             pHwRef, reader_pipe_id, NXP_WR_PRESCHECK );
1639                 }
1640                 break;
1641             }
1642 #endif /* #ifdef TYPE_B */
1643 #ifdef TYPE_FELICA
1644             case phHal_eFelica_PICC:
1645             {
1646                 /* Get the Felica Reader Pipe ID */
1647                 status = phHciNfc_Felica_Get_PipeID
1648                                         (psHciContext, &reader_pipe_id);
1649
1650                 if( (NFCSTATUS_SUCCESS == status)
1651                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1652                     )
1653                 {
1654                     status = phHciNfc_Felica_Request_Mode(psHciContext, pHwRef);
1655                 }
1656                 break;
1657             }
1658 #endif /* #ifdef TYPE_FELICA */
1659 #ifdef TYPE_JEWEL
1660             case phHal_eJewel_PICC:
1661             {
1662                 /* Get the Jewel Reader Pipe ID */
1663                 status = phHciNfc_Jewel_Get_PipeID
1664                                         (psHciContext, &reader_pipe_id);
1665
1666                 if( (NFCSTATUS_SUCCESS == status)
1667                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1668                     )
1669                 {
1670                     /* status = PHNFCSTVAL(CID_NFC_HCI, 
1671                                     NFCSTATUS_FEATURE_NOT_SUPPORTED); */
1672                       status = phHciNfc_Jewel_GetRID(
1673                                         psHciContext, pHwRef);
1674                 }
1675                 break;
1676             }
1677 #endif /* #ifdef TYPE_JEWEL */
1678 #ifdef  TYPE_ISO15693
1679             case phHal_eISO15693_PICC:
1680             {
1681                 /* Get the Reader ISO 15693 Pipe ID */
1682                 status = phHciNfc_ISO15693_Get_PipeID
1683                                         (psHciContext, &reader_pipe_id);
1684             
1685                 if( (NFCSTATUS_SUCCESS == status)
1686                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1687                     )
1688                 {   
1689                     uint8_t cmd[11];
1690                     phHciNfc_Pipe_Info_t *p_pipe_info = NULL;
1691                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1692                     p_pipe_info->param_info = &cmd;
1693                     p_pipe_info->param_length = 11;
1694                     // masked inventory command:
1695                     // set #slots to 1 to use mask without padding,
1696                     // need to set inventory flag to enable setting #slots
1697                     cmd[0] = 0x04 | 0x20; // FLAG_INVENTORY | FLAG_SLOTS
1698                     cmd[1] = 0x01; // CMD_INVENTORY
1699                     cmd[2] = 64; // mask bit-length
1700                     memcpy(cmd + 3, &(psHciContext->p_target_info->RemoteDevInfo.Iso15693_Info.Uid), 8);
1701                     status = phHciNfc_Send_ISO15693_Command(
1702                         psHciContext,  pHwRef
1703                         ,reader_pipe_id, NXP_ISO15693_CMD );
1704                     
1705                 }
1706                 break;
1707             }
1708 #endif /* #ifdef    TYPE_ISO15693 */
1709             default:
1710             {
1711                 status = PHNFCSTVAL(CID_NFC_HCI, 
1712                                     NFCSTATUS_FEATURE_NOT_SUPPORTED);
1713                 break;
1714             }
1715
1716         } /* End of the tag_type Switch */
1717     }
1718
1719     return status;
1720
1721 }
1722
1723
1724 /*!
1725  * \brief Disconnects the the selected tag.
1726  *
1727  * This function disconnects the selected tags via RF Reader Gate.
1728  * This function uses the RF Reader gate based on the type of the
1729  * tag specified.
1730  */
1731
1732
1733 NFCSTATUS
1734 phHciNfc_ReaderMgmt_Deselect(
1735                                     phHciNfc_sContext_t     *psHciContext,
1736                                     void                    *pHwRef,
1737                                     phHal_eRemDevType_t     target_type,
1738                                     uint8_t                 re_poll
1739                 )
1740 {
1741     static  uint8_t             rls_param = FALSE;                  
1742     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1743     uint8_t                     reader_pipe_id = 
1744                                     (uint8_t) HCI_UNKNOWN_PIPE_ID;
1745     phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
1746     
1747
1748
1749     if( (NULL == psHciContext) 
1750         || (NULL == pHwRef) 
1751         )
1752     {
1753       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1754     }
1755     else
1756     {
1757         rls_param = re_poll;
1758
1759         switch (target_type)
1760         {
1761             case phHal_eMifare_PICC:
1762             case phHal_eISO14443_A_PICC:
1763             case phHal_eISO14443_3A_PICC:
1764             case phHal_eISO14443_4A_PICC:
1765             {
1766                 /* Get the Reader A Pipe ID */
1767                 status = phHciNfc_ReaderA_Get_PipeID
1768                                         (psHciContext, &reader_pipe_id);
1769                 break;
1770             }
1771 #ifdef TYPE_B
1772             case phHal_eISO14443_B_PICC:
1773             case phHal_eISO14443_4B_PICC:
1774             {
1775                 /* Get the Reader B Pipe ID */
1776                 status = phHciNfc_ReaderB_Get_PipeID
1777                     (psHciContext, &reader_pipe_id);
1778
1779                 break;
1780             }
1781 #endif /* #ifdef TYPE_B */
1782 #ifdef TYPE_FELICA
1783             case phHal_eFelica_PICC:
1784             {
1785                 /* Get the Felica Pipe ID */
1786                 status = phHciNfc_Felica_Get_PipeID
1787                     (psHciContext, &reader_pipe_id);
1788
1789                 break;
1790             }
1791 #endif /* #ifdef TYPE_FELICA */
1792 #ifdef TYPE_JEWEL
1793             case phHal_eJewel_PICC:
1794             {
1795                 /* Get the Jewel Pipe ID */
1796                 status = phHciNfc_Jewel_Get_PipeID
1797                     (psHciContext, &reader_pipe_id);
1798
1799                 break;
1800             }
1801 #endif /* #ifdef TYPE_JEWEL */
1802 #ifdef  TYPE_ISO15693
1803             case phHal_eISO15693_PICC:
1804             {
1805                 /* Get the ISO 15693 Pipe ID */
1806                 status = phHciNfc_ISO15693_Get_PipeID
1807                     (psHciContext, &reader_pipe_id);
1808
1809                 break;
1810             }
1811 #endif /* #ifdef    TYPE_ISO15693 */
1812 #ifdef ENABLE_P2P
1813             case phHal_eNfcIP1_Target:
1814             {
1815                 /* Get the Reader A Pipe ID */
1816                 status = phHciNfc_Initiator_Get_PipeID
1817                                         (psHciContext, &reader_pipe_id);
1818
1819                 break;
1820             }
1821 #endif
1822             default:
1823             {
1824                 status = PHNFCSTVAL(CID_NFC_HCI, 
1825                                             NFCSTATUS_FEATURE_NOT_SUPPORTED);
1826                 break;
1827             }
1828
1829         } /* End of the tag_type Switch */
1830         p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1831         if( (NFCSTATUS_SUCCESS == status)
1832             && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1833             && ( NULL != p_pipe_info ) )
1834         {
1835             if (TRUE == rls_param)
1836             {
1837                 p_pipe_info->param_info = &rls_param;
1838                 p_pipe_info->param_length = sizeof(rls_param);
1839             }
1840             status = phHciNfc_Send_RFReader_Event ( psHciContext, pHwRef, 
1841                         reader_pipe_id,(uint8_t) NXP_EVT_RELEASE_TARGET );
1842         }
1843     }
1844
1845     return status;
1846 }
1847
1848
1849 /*!
1850  * \brief Exchanges the data to/from the selected tags via RF Reader Gates.
1851  *
1852  * This function Exchanges the data to/from the selected tags 
1853  * via RF Reader Gates. This function uses the RF Reader gate based on the 
1854  * type of the selected tag and the type of the Reader gate specified.
1855  */
1856
1857
1858 NFCSTATUS
1859 phHciNfc_ReaderMgmt_Exchange_Data(
1860                                     phHciNfc_sContext_t     *psHciContext,
1861                                     void                    *pHwRef,
1862                                     phHciNfc_XchgInfo_t     *p_xchg_info
1863                 )
1864 {
1865     NFCSTATUS                   status = NFCSTATUS_SUCCESS;
1866     phHciNfc_Pipe_Info_t        *p_pipe_info = NULL;
1867     uint8_t                     reader_pipe_id = (uint8_t) HCI_UNKNOWN_PIPE_ID;
1868     phHal_eRemDevType_t         target_type = phHal_eUnknown_DevType;
1869
1870     if( (NULL == psHciContext) || (NULL == pHwRef) )
1871     {
1872       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
1873     }
1874     else if (NULL == psHciContext->p_xchg_info)
1875     {
1876         status = PHNFCSTVAL(CID_NFC_HCI,
1877                 NFCSTATUS_INVALID_HCI_INFORMATION);
1878
1879     }
1880     else
1881     {
1882         switch (psHciContext->host_rf_type)
1883         {
1884             case phHal_eISO14443_A_PCD:
1885             {
1886                 /* Get the Reader A Pipe ID */
1887                 status = phHciNfc_ReaderA_Get_PipeID
1888                                         (psHciContext, &reader_pipe_id);
1889
1890                 if( (NFCSTATUS_SUCCESS == status)
1891                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1892                     )
1893                 {
1894                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1895                     p_pipe_info->param_info = p_xchg_info->tx_buffer;
1896                     p_pipe_info->param_length = p_xchg_info->tx_length;
1897                     target_type = psHciContext->p_target_info->RemDevType;
1898                     switch (target_type)
1899                     {
1900                         case phHal_eMifare_PICC:
1901                         case phHal_eISO14443_3A_PICC:
1902                         {
1903                             if ((uint8_t)phHal_eMifareRaw == 
1904                                             p_xchg_info->params.tag_info.cmd_type)
1905                             {
1906                                 status = phHciNfc_Send_ReaderA_Command(
1907                                     psHciContext,  pHwRef
1908                                     ,reader_pipe_id, NXP_MIFARE_RAW );
1909                             } 
1910                             else
1911                             {
1912                                 status = phHciNfc_Send_ReaderA_Command(
1913                                     psHciContext, pHwRef,
1914                                     reader_pipe_id, NXP_MIFARE_CMD );
1915                             }
1916                             break;
1917                         }
1918                         case phHal_eISO14443_A_PICC:
1919                         case phHal_eISO14443_4A_PICC:
1920                         {
1921                             status = phHciNfc_Send_RFReader_Command(
1922                                         psHciContext, pHwRef,
1923                                         reader_pipe_id, WR_XCHGDATA );
1924                             break;
1925                         }
1926                         default:
1927                         {
1928                             status = PHNFCSTVAL(CID_NFC_HCI,
1929                                                 NFCSTATUS_FEATURE_NOT_SUPPORTED);
1930                             break;
1931                         }
1932                     } /* End of the tag_type Switch */
1933                 } /* End of Pipe ID Check */
1934                 break;
1935             }
1936 #ifdef TYPE_B
1937             case phHal_eISO14443_B_PCD:
1938             {
1939                 /* Get the Reader B Pipe ID */
1940                 status = phHciNfc_ReaderB_Get_PipeID
1941                                         (psHciContext, &reader_pipe_id);
1942
1943                 if( (NFCSTATUS_SUCCESS == status)
1944                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1945                     )
1946                 {
1947                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1948                     p_pipe_info->param_info = p_xchg_info->tx_buffer;
1949                     p_pipe_info->param_length = p_xchg_info->tx_length;
1950                     status = phHciNfc_Send_RFReader_Command(
1951                                 psHciContext, pHwRef,
1952                                 reader_pipe_id, WR_XCHGDATA );
1953                 }
1954                 break;
1955             }
1956 #endif /* #ifdef TYPE_B */
1957 #ifdef TYPE_FELICA
1958             case phHal_eFelica_PCD:
1959             {
1960                 /* Get the Felica Reader Pipe ID */
1961                 status = phHciNfc_Felica_Get_PipeID
1962                                         (psHciContext, &reader_pipe_id);
1963
1964                 if( (NFCSTATUS_SUCCESS == status)
1965                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1966                     )
1967                 {
1968                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
1969                     p_pipe_info->param_info = p_xchg_info->tx_buffer;
1970                     p_pipe_info->param_length = p_xchg_info->tx_length;
1971                     if ((uint8_t)phHal_eFelica_Raw == 
1972                                     p_xchg_info->params.tag_info.cmd_type)
1973                     {
1974                         status = phHciNfc_Send_Felica_Command(
1975                             psHciContext,  pHwRef
1976                             ,reader_pipe_id, NXP_FELICA_RAW );
1977                     } 
1978                     else
1979                     {
1980                         status = phHciNfc_Send_Felica_Command(
1981                             psHciContext, pHwRef,
1982                             reader_pipe_id, NXP_FELICA_CMD );
1983                     }
1984                 }
1985                 break;
1986             }
1987 #endif /* #ifdef TYPE_FELICA */
1988 #if defined(TYPE_ISO15693)
1989             case phHal_eISO15693_PCD:
1990             {
1991                 /* Get the ISO15693 Reader Pipe ID */
1992                 status = phHciNfc_ISO15693_Get_PipeID
1993                                         (psHciContext, &reader_pipe_id);
1994
1995                 if( (NFCSTATUS_SUCCESS == status)
1996                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
1997                     )
1998                 {
1999                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
2000                     p_pipe_info->param_info = p_xchg_info->tx_buffer;
2001                     p_pipe_info->param_length = p_xchg_info->tx_length;
2002                     if (((uint8_t)phHal_eIso15693_Cmd  == 
2003                                     p_xchg_info->params.tag_info.cmd_type)
2004 #if defined(SUPPORT_ISO15693_RAW)
2005                         || ((uint8_t) phHal_eIso15693_Raw == 
2006                                     p_xchg_info->params.tag_info.cmd_type)
2007 #endif
2008                      )
2009                     {
2010                         status = phHciNfc_Send_ISO15693_Command(
2011                             psHciContext,  pHwRef
2012                             ,reader_pipe_id, NXP_ISO15693_CMD );
2013                     } 
2014                     else
2015                     {
2016                         status = PHNFCSTVAL(CID_NFC_HCI,
2017                                     NFCSTATUS_INVALID_PARAMETER);
2018                     }
2019                 }
2020                 break;
2021             }
2022 #endif
2023 #ifdef TYPE_JEWEL
2024             case phHal_eJewel_PCD:
2025             {
2026                 /* Get the Jewel Reader Pipe ID */
2027                 status = phHciNfc_Jewel_Get_PipeID
2028                                         (psHciContext, &reader_pipe_id);
2029
2030                 if( (NFCSTATUS_SUCCESS == status)
2031                     && (reader_pipe_id != HCI_UNKNOWN_PIPE_ID )
2032                     )
2033                 {
2034                     uint8_t         transact_type = 0;
2035                     p_pipe_info = psHciContext->p_pipe_list[reader_pipe_id];
2036                     p_pipe_info->param_info = p_xchg_info->tx_buffer;
2037                     p_pipe_info->param_length = p_xchg_info->tx_length;
2038                     switch(p_xchg_info->params.tag_info.cmd_type)
2039                     {
2040                         case phHal_eJewel_Raw:
2041                         {
2042                             transact_type = NXP_JEWEL_RAW;
2043                             break;
2044                         }
2045                         case phHal_eJewel_Invalid:
2046                         default:
2047                         {
2048                             status = PHNFCSTVAL(CID_NFC_HCI,
2049                                                 NFCSTATUS_INVALID_PARAMETER);
2050                             break;
2051                         }
2052                     }
2053                     if(0 != transact_type)
2054                     {
2055                         status = phHciNfc_Send_Jewel_Command(
2056                                     psHciContext,  pHwRef, 
2057                                     reader_pipe_id, transact_type );
2058                     }
2059                 }
2060                 break;
2061             }
2062 #endif /* #ifdef TYPE_JEWEL */
2063             default:
2064             {
2065                 status = PHNFCSTVAL(CID_NFC_HCI, 
2066                                                 NFCSTATUS_FEATURE_NOT_SUPPORTED);
2067                 break;
2068             }
2069         }/* End of Reader Type Switch */
2070     }
2071
2072     return status;
2073 }
2074
2075
2076
2077 /*!
2078  * \brief Releases the resources allocated the RF Reader Management.
2079  *
2080  * This function Releases the resources allocated the RF Reader Management.
2081  */
2082
2083 NFCSTATUS
2084 phHciNfc_ReaderMgmt_Release(
2085                                 phHciNfc_sContext_t     *psHciContext,
2086                                 void                    *pHwRef
2087                              )
2088 {
2089     NFCSTATUS                           status = NFCSTATUS_SUCCESS;
2090     phHciNfc_Pipe_Info_t                *p_pipe_info = NULL;
2091     phHciNfc_ReaderMgmt_Info_t          *p_reader_mgmt_info=NULL;
2092
2093     if( (NULL == psHciContext) || (NULL == pHwRef) )
2094     {
2095       status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
2096     }
2097     else
2098     {
2099         if( NULL != psHciContext->p_reader_mgmt_info )
2100         {
2101             p_reader_mgmt_info = (phHciNfc_ReaderMgmt_Info_t *)
2102                                 psHciContext->p_reader_mgmt_info ;
2103             switch(p_reader_mgmt_info->rf_gate_cur_seq)
2104             {
2105                 /* Reader A pipe close sequence */
2106                 case READERA_PIPE_CLOSE:
2107                 {
2108                     p_pipe_info = ((phHciNfc_ReaderA_Info_t *) 
2109                             psHciContext->p_reader_a_info)->p_pipe_info;
2110
2111                     status = phHciNfc_Close_Pipe( psHciContext,
2112                                                     pHwRef, p_pipe_info );
2113                     if(status == NFCSTATUS_SUCCESS)
2114                     {
2115                         p_reader_mgmt_info->rf_gate_next_seq =
2116                                                     READERB_PIPE_CLOSE;
2117                         /* status = NFCSTATUS_PENDING; */
2118                     }
2119                     break;
2120                 }
2121 #ifdef TYPE_B
2122                 /* Reader B pipe close sequence */
2123                 case READERB_PIPE_CLOSE:
2124                 {
2125                     p_pipe_info = ((phHciNfc_ReaderB_Info_t *) 
2126                             psHciContext->p_reader_b_info)->p_pipe_info;
2127
2128                     status = phHciNfc_Close_Pipe( psHciContext,
2129                                                         pHwRef, p_pipe_info );
2130                     if(status == NFCSTATUS_SUCCESS)
2131                     {
2132                         p_reader_mgmt_info->rf_gate_next_seq =
2133                                                         FELICA_PROP_PIPE_CLOSE;
2134                         status = NFCSTATUS_PENDING;
2135                     }
2136                     break;
2137                 }
2138 #endif /* #ifdef TYPE_B */
2139 #ifdef TYPE_FELICA
2140                 /* Felica Reader pipe close sequence */
2141                 case FELICA_PROP_PIPE_CLOSE:
2142                 {
2143                     p_pipe_info = ((phHciNfc_Felica_Info_t *) 
2144                             psHciContext->p_felica_info)->p_pipe_info;
2145
2146                     status = phHciNfc_Close_Pipe( psHciContext,
2147                                                         pHwRef, p_pipe_info );
2148                     if(status == NFCSTATUS_SUCCESS)
2149                     {
2150                         p_reader_mgmt_info->rf_gate_next_seq =
2151                                                 NFCIP1_INITIATOR_PIPE_CLOSE;
2152                         /* status = NFCSTATUS_PENDING; */
2153                     }
2154                     break;
2155                 }
2156 #endif /* #ifdef TYPE_FELICA */
2157 #ifdef ENABLE_P2P
2158                 /* NFC-IP1 Initiator pipe Close sequence */
2159                 case NFCIP1_INITIATOR_PIPE_CLOSE:
2160                 {
2161                     p_pipe_info = 
2162                         ((phHciNfc_NfcIP_Info_t *)psHciContext->
2163                                     p_nfcip_info)->p_init_pipe_info;
2164                     if(NULL == p_pipe_info )
2165                     {
2166                         status = PHNFCSTVAL(CID_NFC_HCI, 
2167                                         NFCSTATUS_INVALID_HCI_SEQUENCE);
2168                     }
2169                     else
2170                     {
2171                         status = phHciNfc_Open_Pipe( psHciContext,
2172                                                         pHwRef, p_pipe_info );
2173                         if(status == NFCSTATUS_SUCCESS)
2174                         {
2175                             p_reader_mgmt_info->rf_gate_next_seq = READERA_PIPE_CLOSE;
2176                             status = NFCSTATUS_PENDING;
2177                         }
2178                     }
2179                     break;
2180                 }
2181 #endif /* #ifdef ENABLE_P2P */
2182                 default:
2183                 {
2184                     status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_SEQUENCE);
2185                     break;
2186                 }
2187
2188             }/* End of the Sequence Switch */
2189
2190         }/* End of the Reader Info Memory Check */
2191
2192     } /* End of Null Context Check */
2193
2194     return status;
2195 }
2196
2197
2198 /*!
2199  * \brief Sends the RF Reader HCI Events to the connected reader device.
2200  *
2201  * This function Sends the RF Reader HCI Event frames in the HCP packet format to the 
2202  * connected reader device.
2203  */
2204
2205  NFCSTATUS
2206  phHciNfc_Send_RFReader_Event (
2207                                 phHciNfc_sContext_t *psHciContext,
2208                                 void                *pHwRef,
2209                                 uint8_t             pipe_id,
2210                                 uint8_t             event
2211                     )
2212  {
2213     phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
2214     phHciNfc_HCP_Message_t  *hcp_message = NULL;
2215     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
2216     uint8_t                 length = 0;
2217     uint8_t                 i = 0;
2218     NFCSTATUS               status = NFCSTATUS_SUCCESS;
2219
2220     if( (NULL == psHciContext)
2221         || ( pipe_id > PHHCINFC_MAX_PIPE)
2222         ||(NULL == psHciContext->p_pipe_list[pipe_id])
2223       )
2224     {
2225         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
2226         HCI_DEBUG("%s: Invalid Arguments passed \n",
2227                                                 "phHciNfc_Send_RFReader_Event");
2228     }
2229     else
2230     {
2231         p_pipe_info = (phHciNfc_Pipe_Info_t *) 
2232                                 psHciContext->p_pipe_list[pipe_id];
2233         psHciContext->tx_total = 0 ;
2234         length +=  HCP_HEADER_LEN ;
2235         switch( event )
2236         {
2237             case EVT_READER_REQUESTED:
2238             case EVT_END_OPERATION:
2239             {
2240
2241                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
2242                 /* Construct the HCP Frame */
2243                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
2244                                         (uint8_t) pipe_id, HCP_MSG_TYPE_EVENT, event);
2245                 break;
2246             }
2247             case NXP_EVT_RELEASE_TARGET:
2248             {
2249                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
2250                 /* Construct the HCP Frame */
2251                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
2252                                         (uint8_t) pipe_id, HCP_MSG_TYPE_EVENT, event);
2253                 hcp_message = &(hcp_packet->msg.message);
2254                 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
2255                                             i, p_pipe_info->param_info,
2256                                             p_pipe_info->param_length);
2257                 length =(uint16_t)(length + i + p_pipe_info->param_length);
2258                 break;
2259             }
2260             default:
2261             {
2262                 status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED );
2263                 HCI_DEBUG("%s: Statement Should Not Occur \n",
2264                                             "phHciNfc_Send_RFReader_Event");
2265                 break;
2266             }
2267         }
2268         if( NFCSTATUS_SUCCESS == status )
2269         {
2270             p_pipe_info->sent_msg_type = HCP_MSG_TYPE_EVENT ;
2271             p_pipe_info->prev_msg = event ;
2272             psHciContext->tx_total = length;
2273
2274             /* Send the Constructed HCP packet to the lower layer */
2275             status = phHciNfc_Send_HCP( psHciContext, pHwRef );
2276             p_pipe_info->prev_status = NFCSTATUS_PENDING;
2277         }
2278     }
2279
2280     return status;
2281 }
2282
2283 /*!
2284  * \brief Sends the RF Reader HCI Additonal Commands to the connected 
2285  * reader device.
2286  *
2287  * This function Sends the RF Reader HCI Command frames in the HCP packet 
2288  * format to the connected reader device.
2289  */
2290
2291  NFCSTATUS
2292  phHciNfc_Send_RFReader_Command (
2293                                 phHciNfc_sContext_t *psHciContext,
2294                                 void                *pHwRef,
2295                                 uint8_t             pipe_id,
2296                                 uint8_t             cmd
2297                     )
2298  {
2299     phHciNfc_HCP_Packet_t   *hcp_packet = NULL;
2300     phHciNfc_HCP_Message_t  *hcp_message = NULL;
2301     phHciNfc_Pipe_Info_t    *p_pipe_info = NULL;
2302     uint8_t                 i = 0;
2303     uint16_t                 length=0;
2304     NFCSTATUS               status = NFCSTATUS_SUCCESS;
2305
2306     if( (NULL == psHciContext)
2307         || ( pipe_id > PHHCINFC_MAX_PIPE)
2308         ||(NULL == psHciContext->p_pipe_list[pipe_id])
2309       )
2310     {
2311         status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
2312         HCI_DEBUG("%s: Invalid Arguments passed \n",
2313                                                 "phHciNfc_Send_RFReader_Command");
2314     }
2315     else
2316     {
2317         p_pipe_info = (phHciNfc_Pipe_Info_t *) 
2318                                 psHciContext->p_pipe_list[pipe_id];
2319         psHciContext->tx_total = 0 ;
2320         length +=  HCP_HEADER_LEN ;
2321         switch( cmd )
2322         {
2323             case WR_XCHGDATA:
2324             {
2325                 
2326                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
2327                 /* Construct the HCP Frame */
2328                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
2329                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
2330                 hcp_message = &(hcp_packet->msg.message);
2331                 /* Frame Wait Timeout */
2332                 hcp_message->payload[i++] = nxp_nfc_isoxchg_timeout ;
2333                 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
2334                                             i, p_pipe_info->param_info,
2335                                             p_pipe_info->param_length);
2336                 length =(uint16_t)(length + i + p_pipe_info->param_length);
2337                 break;
2338             }
2339             case NXP_WR_PRESCHECK:
2340             case NXP_WR_ACTIVATE_NEXT:
2341             {
2342
2343                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
2344                 /* Construct the HCP Frame */
2345                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
2346                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
2347                 break;
2348             }
2349             case NXP_WR_DISPATCH_TO_UICC:
2350             case NXP_WR_ACTIVATE_ID:
2351             {
2352
2353                 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
2354                 /* Construct the HCP Frame */
2355                 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
2356                                         (uint8_t) pipe_id, HCP_MSG_TYPE_COMMAND, cmd);
2357                 hcp_message = &(hcp_packet->msg.message);
2358                 /* UID of the Card */
2359                 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
2360                                             i, p_pipe_info->param_info,
2361                                             p_pipe_info->param_length);
2362                 length =(uint16_t)(length + i + p_pipe_info->param_length);
2363                 break;
2364             }
2365             default:
2366             {
2367                 status = PHNFCSTVAL( CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED );
2368                 HCI_DEBUG("%s: Statement Should Not Occur \n",
2369                                                 "phHciNfc_Send_RFReader_Command");
2370                 break;
2371             }
2372         }
2373         if( NFCSTATUS_SUCCESS == status )
2374         {
2375             p_pipe_info->sent_msg_type = HCP_MSG_TYPE_COMMAND;
2376             p_pipe_info->prev_msg = cmd;
2377             psHciContext->tx_total = length;
2378             psHciContext->response_pending = TRUE ;
2379
2380             /* Send the Constructed HCP packet to the lower layer */
2381             status = phHciNfc_Send_HCP( psHciContext, pHwRef );
2382             p_pipe_info->prev_status = NFCSTATUS_PENDING;
2383         }
2384     }
2385
2386     return status;
2387 }
2388
2389
2390