2 * Copyright (C) 2010 NXP Semiconductors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * =========================================================================== *
21 * \file phHciNfc_SWP.c *
22 * \brief HCI SWP gate Management Routines. *
25 * Project: NFC-FRI-1.1 *
27 * $Date: Tue Aug 18 10:16:36 2009 $ *
28 * $Author: ing04880 $ *
30 * $Aliases: NFC_FRI1.1_WK934_R31_1,NFC_FRI1.1_WK941_PREP1,NFC_FRI1.1_WK941_PREP2,NFC_FRI1.1_WK941_1,NFC_FRI1.1_WK943_R32_1,NFC_FRI1.1_WK949_PREP1,NFC_FRI1.1_WK943_R32_10,NFC_FRI1.1_WK943_R32_13,NFC_FRI1.1_WK943_R32_14,NFC_FRI1.1_WK1007_R33_1,NFC_FRI1.1_WK1007_R33_4,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1023_R35_1 $ * *
31 * =========================================================================== *
35 ***************************** Header File Inclusion ****************************
37 #include <phNfcCompId.h>
38 #include <phHciNfc_Pipe.h>
39 #include <phHciNfc_SWP.h>
40 #include <phOsalNfc.h>
41 #include <phHciNfc_Emulation.h>
42 #include <phHciNfc_DevMgmt.h>
44 ****************************** Macro Definitions *******************************
47 /* SWP Gate regsitry Settings */
48 /* set default mode mode as virtual mode */
49 #define NXP_SWP_DEFAULT_MODE_INDEX (0x01U)
50 /* Get the Status of the UICC Connection */
51 #define NXP_SWP_STATUS_INDEX (0x02U)
53 /* Configure the Secure Element Protected Mode */
54 #define NXP_SWP_PROTECTED_INDEX (0x03U)
56 /* Switch mode index */
57 #define NXP_EVT_SWP_SWITCH_MODE (0x03U)
59 /* Protected Event from the Host Controller */
60 #define NXP_EVT_SWP_PROTECTED (0x04U)
62 /****************** Structure and Enumeration ****************************/
64 /****************** Static Function Declaration **************************/
68 phHciNfc_Recv_SWP_Response(
81 phHciNfc_Send_SWP_Event(
82 phHciNfc_sContext_t *psHciContext,
91 phHciNfc_Recv_SWP_Event(
104 phHciNfc_SWP_InfoUpdate(
105 phHciNfc_sContext_t *psHciContext,
112 *************************** Function Definitions ***************************
116 phHciNfc_SWP_Get_PipeID(
117 phHciNfc_sContext_t *psHciContext,
121 NFCSTATUS status = NFCSTATUS_SUCCESS;
123 if( (NULL != psHciContext)
124 && ( NULL != ppipe_id )
125 && ( NULL != psHciContext->p_swp_info )
128 phHciNfc_SWP_Info_t *p_swp_info=NULL;
129 p_swp_info = (phHciNfc_SWP_Info_t *)
130 psHciContext->p_swp_info ;
131 *ppipe_id = p_swp_info->pipe_id ;
135 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
141 phHciNfc_SWP_Init_Resources(phHciNfc_sContext_t *psHciContext)
143 NFCSTATUS status = NFCSTATUS_SUCCESS;
144 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
146 if( NULL == psHciContext )
148 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
152 if(( NULL == psHciContext->p_swp_info ) &&
153 (phHciNfc_Allocate_Resource((void **)(&ps_swp_info),
154 sizeof(phHciNfc_SWP_Info_t))== NFCSTATUS_SUCCESS))
156 psHciContext->p_swp_info = ps_swp_info;
157 ps_swp_info->current_seq = SWP_INVALID_SEQUENCE;
158 ps_swp_info->next_seq = SWP_INVALID_SEQUENCE;
159 ps_swp_info->pipe_id = (uint8_t)HCI_UNKNOWN_PIPE_ID;
163 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INSUFFICIENT_RESOURCES);
172 phHciNfc_SWP_Update_PipeInfo(
173 phHciNfc_sContext_t *psHciContext,
175 phHciNfc_Pipe_Info_t *pPipeInfo
178 NFCSTATUS status = NFCSTATUS_SUCCESS;
180 if( NULL == psHciContext )
182 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
184 else if(NULL == psHciContext->p_swp_info)
186 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
190 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
191 ps_swp_info = (phHciNfc_SWP_Info_t *)
192 psHciContext->p_swp_info ;
193 /* Update the pipe_id of the SWP Gate obtained from HCI Response */
194 ps_swp_info->pipe_id = pipeID;
195 ps_swp_info->p_pipe_info = pPipeInfo;
196 if (NULL != pPipeInfo)
198 /* Update the Response Receive routine of the SWP Gate */
199 pPipeInfo->recv_resp = &phHciNfc_Recv_SWP_Response;
200 pPipeInfo->recv_event =&phHciNfc_Recv_SWP_Event;
209 phHciNfc_Recv_SWP_Response(
220 NFCSTATUS status = NFCSTATUS_SUCCESS;
221 phHciNfc_sContext_t *psHciContext =
222 (phHciNfc_sContext_t *)psContext ;
225 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pResponse)
228 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
230 else if(NULL == psHciContext->p_swp_info)
232 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
236 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
237 uint8_t prev_cmd = ANY_GET_PARAMETER;
239 ps_swp_info = (phHciNfc_SWP_Info_t *)
240 psHciContext->p_swp_info ;
241 if( NULL == ps_swp_info->p_pipe_info)
243 status = PHNFCSTVAL(CID_NFC_HCI,
244 NFCSTATUS_INVALID_HCI_INFORMATION);
248 prev_cmd = ps_swp_info->p_pipe_info->prev_msg ;
251 case ANY_GET_PARAMETER:
253 HCI_PRINT(" Getting the SWP Parameter \n");
254 status = phHciNfc_SWP_InfoUpdate(psHciContext,
255 ps_swp_info->p_pipe_info->reg_index,
256 &pResponse[HCP_HEADER_LEN],
257 (uint8_t)(length - HCP_HEADER_LEN));
261 case ANY_SET_PARAMETER:
263 HCI_PRINT("SWP Parameter Set \n");
268 HCI_PRINT("SWP gate open pipe complete\n");
273 HCI_PRINT("SWP close pipe complete\n");
278 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
282 if( NFCSTATUS_SUCCESS == status )
284 status = phHciNfc_EmuMgmt_Update_Seq(psHciContext,
286 ps_swp_info->p_pipe_info->prev_status = NFCSTATUS_SUCCESS;
287 ps_swp_info->current_seq = ps_swp_info->next_seq;
296 phHciNfc_SWP_Configure_Default(
302 NFCSTATUS status = NFCSTATUS_SUCCESS;
303 static uint8_t param = 0 ;
304 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
306 if((NULL == psHciContext)||(NULL == pHwRef))
308 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
310 else if(NULL == psHciContext->p_swp_info)
312 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
316 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
317 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
319 ps_swp_info = (phHciNfc_SWP_Info_t*)psHciContext->p_swp_info;
320 ps_pipe_info = ps_swp_info->p_pipe_info;
322 if(NULL == ps_pipe_info)
324 status = PHNFCSTVAL(CID_NFC_HCI,
325 NFCSTATUS_INVALID_HCI_INFORMATION);
329 ps_pipe_info->reg_index = NXP_SWP_DEFAULT_MODE_INDEX;
330 /* Enable/Disable SWP link */
331 param = (uint8_t)enable_type;
332 ps_pipe_info->param_info =(void*)¶m ;
333 ps_pipe_info->param_length = sizeof(param) ;
334 status = phHciNfc_Send_Generic_Cmd(psHciContext, pHwRef,
335 ps_swp_info->pipe_id,
336 (uint8_t)ANY_SET_PARAMETER);
345 phHciNfc_SWP_Get_Status(
350 NFCSTATUS status = NFCSTATUS_SUCCESS;
351 /* static uint8_t param = 0 ; */
352 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
354 if((NULL == psHciContext)||(NULL == pHwRef))
356 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
358 else if(NULL == psHciContext->p_swp_info)
360 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
364 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
365 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
367 ps_swp_info = (phHciNfc_SWP_Info_t*)psHciContext->p_swp_info;
368 ps_pipe_info = ps_swp_info->p_pipe_info;
370 if(NULL == ps_pipe_info)
372 status = PHNFCSTVAL(CID_NFC_HCI,
373 NFCSTATUS_INVALID_HCI_INFORMATION);
377 ps_pipe_info->reg_index = NXP_SWP_STATUS_INDEX;
378 status = phHciNfc_Send_Generic_Cmd(psHciContext, pHwRef,
379 ps_swp_info->pipe_id,
380 (uint8_t)ANY_GET_PARAMETER);
388 phHciNfc_SWP_Get_Bitrate(
393 NFCSTATUS status = NFCSTATUS_SUCCESS;
394 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)
397 if((NULL == psHciContext) || (NULL == pHwRef))
399 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
401 else if(NULL == psHciContext->p_swp_info)
403 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
407 phHciNfc_SWP_Info_t *ps_swp_info = NULL;
409 ps_swp_info = (phHciNfc_SWP_Info_t*)psHciContext->p_swp_info;
411 status = phHciNfc_DevMgmt_Get_Info(psHciContext, pHwRef,
412 NFC_ADDRESS_SWP_BITRATE, &(ps_swp_info->uicc_bitrate));
420 phHciNfc_SWP_Protection(
426 NFCSTATUS status = NFCSTATUS_SUCCESS;
427 static uint8_t param = 0 ;
428 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)psHciHandle);
430 if((NULL == psHciContext)||(NULL == pHwRef))
432 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
434 else if(NULL == psHciContext->p_swp_info)
436 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
440 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
441 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
443 ps_swp_info = (phHciNfc_SWP_Info_t*)psHciContext->p_swp_info;
444 ps_pipe_info = ps_swp_info->p_pipe_info;
446 if(NULL == ps_pipe_info)
448 status = PHNFCSTVAL(CID_NFC_HCI,
449 NFCSTATUS_INVALID_HCI_INFORMATION);
453 ps_pipe_info->reg_index = NXP_SWP_PROTECTED_INDEX;
454 /* Enable/Disable SWP Protection */
455 param = (uint8_t)mode;
456 ps_pipe_info->param_info =(void*)¶m ;
457 ps_pipe_info->param_length = sizeof(param) ;
458 status = phHciNfc_Send_Generic_Cmd(psHciContext, pHwRef,
459 ps_swp_info->pipe_id,
460 (uint8_t)ANY_SET_PARAMETER);
472 phHciNfc_SWP_InfoUpdate(
473 phHciNfc_sContext_t *psHciContext,
479 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
480 NFCSTATUS status = NFCSTATUS_SUCCESS;
482 ps_swp_info = (phHciNfc_SWP_Info_t *)
483 psHciContext->p_swp_info ;
485 /* To remove "warning (VS C4100) : unreferenced formal parameter" */
486 PHNFC_UNUSED_VARIABLE(reg_length);
489 case NXP_SWP_DEFAULT_MODE_INDEX:
491 HCI_PRINT_BUFFER("\tUICC Enable Register:",reg_value,reg_length);
494 /* Get the Status of the UICC Connection */
495 case NXP_SWP_STATUS_INDEX:
497 HCI_PRINT_BUFFER("\tUICC Connection Status:", reg_value, reg_length);
498 ps_swp_info->uicc_status = (phHciNfc_SWP_Status_t ) *reg_value ;
501 case NXP_SWP_PROTECTED_INDEX:
503 HCI_PRINT_BUFFER("\t UICC Card Emulation Rights :",reg_value,reg_length);
509 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_INFORMATION);
512 } /* End of switch(index) */
518 phHciNfc_SWP_Configure_Mode(
524 NFCSTATUS status = NFCSTATUS_SUCCESS;
525 static uint8_t param = 0;
526 phHciNfc_sContext_t *psHciContext = ((phHciNfc_sContext_t *)
529 if( (NULL == psHciContext)||(NULL == pHwRef))
531 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
533 else if ( NULL == psHciContext->p_swp_info )
535 status = PHNFCSTVAL(CID_NFC_HCI,
536 NFCSTATUS_INVALID_HCI_INFORMATION);
540 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
541 phHciNfc_Pipe_Info_t *ps_pipe_info=NULL;
543 ps_swp_info = (phHciNfc_SWP_Info_t*)psHciContext->p_swp_info;
545 ps_pipe_info = ps_swp_info->p_pipe_info;
546 if(NULL == ps_pipe_info)
548 status = PHNFCSTVAL(CID_NFC_HCI,
549 NFCSTATUS_INVALID_HCI_INFORMATION);
553 /* Switch the Mode of the SmartMx */
555 ps_pipe_info->param_info =(void*)¶m ;
556 ps_pipe_info->param_length = sizeof(param) ;
557 status = phHciNfc_Send_SWP_Event( psHciContext, pHwRef,
558 ps_swp_info->pipe_id,
559 NXP_EVT_SWP_SWITCH_MODE );
561 /* Send the Success Status as this is an event */
562 status = ((status == NFCSTATUS_PENDING)?
563 NFCSTATUS_SUCCESS : status);
565 }/* End of else part*/
572 phHciNfc_Recv_SWP_Event(
583 NFCSTATUS status = NFCSTATUS_SUCCESS;
584 phHciNfc_sContext_t *psHciContext =
585 (phHciNfc_sContext_t *)psContext ;
586 static phHal_sEventInfo_t EventInfo;
589 if( (NULL == psHciContext) || (NULL == pHwRef) || (NULL == pEvent)
592 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
594 else if(NULL == psHciContext->p_swp_info)
596 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_FEATURE_NOT_SUPPORTED);
600 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
602 ps_swp_info = (phHciNfc_SWP_Info_t *)
603 psHciContext->p_swp_info ;
604 if( NULL == ps_swp_info->p_pipe_info)
606 status = PHNFCSTVAL(CID_NFC_HCI,
607 NFCSTATUS_INVALID_HCI_INFORMATION);
611 phHciNfc_HCP_Packet_t *p_packet = NULL;
612 phHciNfc_HCP_Message_t *message = NULL;
613 uint8_t EventType = 0;
615 p_packet = (phHciNfc_HCP_Packet_t *)pEvent;
616 message = &(p_packet->msg.message);
617 /* Get the instruction bits from the Message Header */
618 EventType = (uint8_t) GET_BITS8( message->msg_header,
619 HCP_MSG_INSTRUCTION_OFFSET, HCP_MSG_INSTRUCTION_LEN);
621 EventInfo.eventHost = phHal_eHostController;
622 EventInfo.eventSource = phHal_ePICC_DevType;
623 /* Occurrence of the Protected events for reporting */
624 if (NXP_EVT_SWP_PROTECTED == EventType)
626 EventInfo.eventType = NFC_EVT_PROTECTED;
630 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_HCI_RESPONSE);
633 if (NFCSTATUS_SUCCESS == status )
635 phHciNfc_Notify_Event( psHciContext, pHwRef,
636 NFC_NOTIFY_EVENT, (void*)&EventInfo);
648 phHciNfc_Send_SWP_Event(
649 phHciNfc_sContext_t *psHciContext,
655 phHciNfc_HCP_Packet_t *hcp_packet = NULL;
656 phHciNfc_HCP_Message_t *hcp_message = NULL;
657 phHciNfc_Pipe_Info_t *p_pipe_info = NULL;
660 NFCSTATUS status = NFCSTATUS_SUCCESS;
662 p_pipe_info = (phHciNfc_Pipe_Info_t *)
663 psHciContext->p_pipe_list[pipe_id];
664 psHciContext->tx_total = 0 ;
665 length = (length + HCP_HEADER_LEN);
667 hcp_packet = (phHciNfc_HCP_Packet_t *) psHciContext->send_buffer;
668 /* Construct the HCP Frame */
669 phHciNfc_Build_HCPFrame(hcp_packet,HCP_CHAINBIT_DEFAULT,
671 HCP_MSG_TYPE_EVENT, event);
673 hcp_message = &(hcp_packet->msg.message);
675 phHciNfc_Append_HCPFrame((uint8_t *)hcp_message->payload,
677 (uint8_t *)p_pipe_info->param_info,
678 p_pipe_info->param_length);
679 length = (uint8_t)(length + i + p_pipe_info->param_length);
681 p_pipe_info->sent_msg_type = HCP_MSG_TYPE_EVENT ;
682 p_pipe_info->prev_msg = event ;
683 psHciContext->tx_total = length;
685 /* Send the Constructed HCP packet to the lower layer */
686 status = phHciNfc_Send_HCP( psHciContext, pHwRef );
687 if(NFCSTATUS_PENDING == status)
689 ((phHciNfc_SWP_Info_t *)psHciContext->p_swp_info)->current_seq =
690 ((phHciNfc_SWP_Info_t *)psHciContext->p_swp_info)->next_seq;
691 p_pipe_info->prev_status = status;
698 phHciNfc_SWP_Update_Sequence(
699 phHciNfc_sContext_t *psHciContext,
700 phHciNfc_eSeqType_t SWP_seq
703 NFCSTATUS status = NFCSTATUS_SUCCESS;
704 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
705 if( NULL == psHciContext )
707 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
709 else if( NULL == psHciContext->p_swp_info )
711 status = PHNFCSTVAL(CID_NFC_HCI,
712 NFCSTATUS_INVALID_HCI_INFORMATION);
716 ps_swp_info = (phHciNfc_SWP_Info_t *)
717 psHciContext->p_swp_info ;
723 ps_swp_info->current_seq = SWP_INVALID_SEQUENCE;
724 ps_swp_info->next_seq = SWP_INVALID_SEQUENCE ;
729 ps_swp_info->current_seq = ps_swp_info->next_seq;
734 ps_swp_info->current_seq = SWP_INVALID_SEQUENCE;
735 ps_swp_info->next_seq = SWP_INVALID_SEQUENCE;
740 ps_swp_info->current_seq = SWP_STATUS_SEQ;
741 ps_swp_info->next_seq = SWP_STATUS_SEQ;
754 phHciNfc_SWP_Config_Sequence(
755 phHciNfc_sContext_t *psHciContext,
757 phHal_sEmulationCfg_t *ps_emulation_cfg
760 NFCSTATUS status = NFCSTATUS_SUCCESS;
761 phHciNfc_SWP_Info_t *ps_swp_info=NULL;
763 if ((NULL == psHciContext) || (NULL == pHwRef) ||
764 (NULL == ps_emulation_cfg))
766 status = PHNFCSTVAL(CID_NFC_HCI, NFCSTATUS_INVALID_PARAMETER);
768 else if( NULL == psHciContext->p_swp_info )
770 status = PHNFCSTVAL(CID_NFC_HCI,
771 NFCSTATUS_INVALID_HCI_INFORMATION);
775 phHciNfc_Pipe_Info_t *ps_pipe_info = NULL;
776 phHal_sUiccEmuCfg_t *uicc_config =
777 &(ps_emulation_cfg->config.uiccEmuCfg);
779 ps_swp_info = (phHciNfc_SWP_Info_t *)psHciContext->p_swp_info;
780 ps_pipe_info = ps_swp_info->p_pipe_info;
782 if (NULL == ps_pipe_info)
784 status = PHNFCSTVAL(CID_NFC_HCI,
785 NFCSTATUS_INVALID_HCI_INFORMATION);
789 switch(ps_swp_info->current_seq)
791 case SWP_STATUS_SEQ :
793 status = phHciNfc_SWP_Configure_Default( psHciContext,
794 pHwRef, uicc_config->enableUicc );
796 if(status == NFCSTATUS_PENDING)
798 ps_swp_info->next_seq = SWP_STATUS_SEQ;
799 status = NFCSTATUS_SUCCESS;
805 status = phHciNfc_SWP_Configure_Mode( psHciContext,
806 pHwRef, UICC_SWITCH_MODE_DEFAULT );
807 /* UICC_SWITCH_MODE_ON */
808 if(status == NFCSTATUS_PENDING)
810 ps_swp_info->next_seq = SWP_STATUS_SEQ;
811 status = NFCSTATUS_SUCCESS;
817 status = PHNFCSTVAL(CID_NFC_HCI,
818 NFCSTATUS_INVALID_HCI_INFORMATION);