1 /*
2  * Copyright (C) 2010-2014 NXP Semiconductors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef _PHNXPNCIHAL_H_
17 #define _PHNXPNCIHAL_H_
18 
19 #include <hardware/nfc.h>
20 #include <phNxpNciHal_utils.h>
21 
22 /********************* Definitions and structures *****************************/
23 #define MAX_RETRY_COUNT 5
24 #define NCI_MAX_DATA_LEN 300
25 #define NCI_POLL_DURATION 500
26 #define HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT 0x07
27 #undef P2P_PRIO_LOGIC_HAL_IMP
28 
29 typedef void(phNxpNciHal_control_granted_callback_t)();
30 
31 /* NCI Data */
32 typedef struct nci_data {
33   uint16_t len;
34   uint8_t p_data[NCI_MAX_DATA_LEN];
35 } nci_data_t;
36 
37 typedef enum { HAL_STATUS_CLOSE = 0, HAL_STATUS_OPEN } phNxpNci_HalStatus;
38 
39 /* Macros to enable and disable extensions */
40 #define HAL_ENABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 1)
41 #define HAL_DISABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 0)
42 
43 /* NCI Control structure */
44 typedef struct phNxpNciHal_Control {
45   phNxpNci_HalStatus halStatus; /* Indicate if hal is open or closed */
46   pthread_t client_thread;      /* Integration thread handle */
47   uint8_t thread_running;       /* Thread running if set to 1, else set to 0 */
48   phLibNfc_sConfig_t gDrvCfg;   /* Driver config data */
49 
50   /* Rx data */
51   uint8_t* p_rx_data;
52   uint16_t rx_data_len;
53 
54   /* libnfc-nci callbacks */
55   nfc_stack_callback_t* p_nfc_stack_cback;
56   nfc_stack_data_callback_t* p_nfc_stack_data_cback;
57 
58   /* control granted callback */
59   phNxpNciHal_control_granted_callback_t* p_control_granted_cback;
60 
61   /* HAL open status */
62   bool_t hal_open_status;
63 
64   /* HAL extensions */
65   uint8_t hal_ext_enabled;
66 
67   /* Waiting semaphore */
68   phNxpNciHal_Sem_t ext_cb_data;
69 
70   uint16_t cmd_len;
71   uint8_t p_cmd_data[NCI_MAX_DATA_LEN];
72   uint16_t rsp_len;
73   uint8_t p_rsp_data[NCI_MAX_DATA_LEN];
74 
75   /* retry count used to force download */
76   uint16_t retry_cnt;
77   uint8_t read_retry_cnt;
78 } phNxpNciHal_Control_t;
79 
80 typedef struct phNxpNciClock {
81   bool_t isClockSet;
82   uint8_t p_rx_data[20];
83   bool_t issetConfig;
84 } phNxpNciClock_t;
85 
86 typedef struct phNxpNciRfSetting {
87   bool_t isGetRfSetting;
88   uint8_t p_rx_data[20];
89 } phNxpNciRfSetting_t;
90 
91 typedef struct phNxpNciMwEepromArea {
92   bool_t isGetEepromArea;
93   uint8_t p_rx_data[32];
94 } phNxpNciMwEepromArea_t;
95 
96 typedef enum {
97   NFC_FORUM_PROFILE,
98   EMV_CO_PROFILE,
99   INVALID_PROFILe
100 } phNxpNciProfile_t;
101 /* NXP Poll Profile control structure */
102 typedef struct phNxpNciProfile_Control {
103   phNxpNciProfile_t profile_type;
104   uint8_t bClkSrcVal; /* Holds the System clock source read from config file */
105   uint8_t
106       bClkFreqVal;  /* Holds the System clock frequency read from config file */
107   uint8_t bTimeout; /* Holds the Timeout Value */
108 } phNxpNciProfile_Control_t;
109 
110 /* Internal messages to handle callbacks */
111 #define NCI_HAL_OPEN_CPLT_MSG 0x411
112 #define NCI_HAL_CLOSE_CPLT_MSG 0x412
113 #define NCI_HAL_POST_INIT_CPLT_MSG 0x413
114 #define NCI_HAL_PRE_DISCOVER_CPLT_MSG 0x414
115 #define NCI_HAL_ERROR_MSG 0x415
116 #define NCI_HAL_RX_MSG 0xF01
117 
118 #define NCIHAL_CMD_CODE_LEN_BYTE_OFFSET (2U)
119 #define NCIHAL_CMD_CODE_BYTE_LEN (3U)
120 
121 /******************** NCI HAL exposed functions *******************************/
122 
123 void phNxpNciHal_request_control(void);
124 void phNxpNciHal_release_control(void);
125 int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t* p_data);
126 #if (NFC_NXP_CHIP_TYPE == PN548C2)
127 NFCSTATUS phNxpNciHal_core_reset_recovery();
128 void phNxpNciHal_discovery_cmd_ext(uint8_t* p_cmd_data, uint16_t cmd_len);
129 #endif
130 #endif /* _PHNXPNCIHAL_H_ */
131