1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2014 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  this file contains the NCI transport internal definitions and functions.
22  *
23  ******************************************************************************/
24 
25 #ifndef NFC_HAL_INT_H
26 #define NFC_HAL_INT_H
27 
28 #include "gki.h"
29 #include "nci_defs.h"
30 #include "nfc_brcm_defs.h"
31 #include "nfc_hal_api.h"
32 #include "nfc_hal_int_api.h"
33 #include "nfc_hal_target.h"
34 
35 using android::base::StringPrintf;
36 
37 /* NFC HAL Task Timer events */
38 #ifndef NFC_HAL_QUICK_TIMER_EVT_MASK
39 #define NFC_HAL_QUICK_TIMER_EVT_MASK (TIMER_0_EVT_MASK)
40 #endif
41 
42 #ifndef NFC_HAL_QUICK_TIMER_ID
43 #define NFC_HAL_QUICK_TIMER_ID (TIMER_0)
44 #endif
45 
46 typedef uint8_t tNFC_HAL_WAIT_RSP;
47 
48 #if (NFC_HAL_HCI_INCLUDED == TRUE)
49 
50 #endif
51 
52 /* NFC HAL transport configuration */
53 typedef struct {
54   bool shared_transport; /* TRUE if using shared HCI/NCI transport */
55   uint8_t userial_baud;
56   uint8_t userial_fc;
57 } tNFC_HAL_TRANS_CFG;
58 
59 #ifdef TESTER
60 /* For Insight, ncit_cfg is runtime-configurable */
61 #define NFC_HAL_TRANS_CFG_QUALIFIER
62 #else
63 /* For all other platforms, ncit_cfg is constant */
64 #define NFC_HAL_TRANS_CFG_QUALIFIER const
65 #endif
66 extern NFC_HAL_TRANS_CFG_QUALIFIER tNFC_HAL_TRANS_CFG nfc_hal_trans_cfg;
67 
68 /****************************************************************************
69 ** Internal constants and definitions
70 ****************************************************************************/
71 
72 typedef uint8_t tNFC_HAL_NCI_RAS;
73 
74 typedef uint8_t tNFC_HAL_POWER_MODE;
75 
76 #define NFC_HAL_SAVED_HDR_SIZE (2)
77 #define NFC_HAL_SAVED_CMD_SIZE (2)
78 
79 #ifndef NFC_HAL_DEBUG
80 #define NFC_HAL_DEBUG TRUE
81 #endif
82 
83 #if (NFC_HAL_DEBUG == TRUE)
84 extern bool nfc_debug_enabled;
85 extern const char* const nfc_hal_init_state_str[];
86 #define NFC_HAL_SET_INIT_STATE(state)                                        \
87   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf(                          \
88       "init state: %d->%d(%s)", nfc_hal_cb.dev_cb.initializing_state, state, \
89       nfc_hal_init_state_str[state]);                                        \
90   nfc_hal_cb.dev_cb.initializing_state = state;
91 #else
92 #define NFC_HAL_SET_INIT_STATE(state) \
93   nfc_hal_cb.dev_cb.initializing_state = state;
94 #endif
95 
96 typedef uint8_t tNFC_HAL_INIT_STATE;
97 
98 typedef uint8_t tNFC_HAL_DM_CONFIG;
99 
100 /* callback function prototype */
101 typedef struct {
102   uint16_t opcode;
103   uint16_t param_len;
104   uint8_t* p_param_buf;
105 } tNFC_HAL_BTVSC_CPLT;
106 
107 #if (NFC_HAL_HCI_INCLUDED == TRUE)
108 
109 /* data type for NFC_HAL_HCI_RSP_NV_READ_EVT */
110 typedef struct {
111   NFC_HDR hdr;
112   uint8_t block;
113   uint16_t size;
114   tHAL_NFC_STATUS status;
115 } tNFC_HAL_HCI_RSP_NV_READ_EVT;
116 
117 /* data type for NFC_HAL_HCI_RSP_NV_WRITE_EVT */
118 typedef struct {
119   NFC_HDR hdr;
120   tHAL_NFC_STATUS status;
121 } tNFC_HAL_HCI_RSP_NV_WRITE_EVT;
122 
123 /* union of all event data types */
124 typedef union {
125   NFC_HDR hdr;
126   /* Internal events */
127   tNFC_HAL_HCI_RSP_NV_READ_EVT nv_read;
128   tNFC_HAL_HCI_RSP_NV_WRITE_EVT nv_write;
129 } tNFC_HAL_HCI_EVENT_DATA;
130 
131 #endif
132 /*****************************************************************************
133 ** Control block for NFC HAL
134 *****************************************************************************/
135 
136 /* Information about current patch in NVM */
137 typedef struct {
138   uint16_t project_id; /* Current project_id of patch in nvm       */
139   uint16_t ver_major;  /* Current major version of patch in nvm    */
140   uint16_t ver_minor;  /* Current minor version of patch in nvm    */
141   uint16_t fpm_size;   /* Current size of FPM patch in nvm         */
142   uint16_t lpm_size;   /* Current size of LPM patch in nvm         */
143   uint8_t flags;       /* See NFC_HAL_NVM_FLAGS_* flag definitions */
144   uint8_t nvm_type;    /* Current NVM Type - UICC/EEPROM           */
145   uint8_t chip_ver[NFC_HAL_PRM_MAX_CHIP_VER_LEN]; /* patch chip version       */
146 } tNFC_HAL_NVM;
147 
148 /* Patch for I2C fix */
149 typedef struct {
150   uint8_t* p_patch;      /* patch for i2c fix                */
151   uint32_t prei2c_delay; /* the dealy after preI2C patch */
152   uint16_t len;          /* i2c patch length                 */
153 } tNFC_HAL_PRM_I2C_FIX_CB;
154 
155 /* Control block for NCI transport */
156 typedef struct {
157   uint8_t nci_ctrl_size; /* Max size for NCI messages */
158   uint8_t rcv_state;     /* current rx state */
159   uint16_t rcv_len; /* bytes remaining to be received in current rx state     */
160   NFC_HDR* p_rcv_msg;  /* buffer to receive NCI message */
161   NFC_HDR* p_frag_msg; /* fragmented NCI message; waiting for last fragment */
162   NFC_HDR*
163       p_pend_cmd; /* pending NCI message; waiting for NFCC state to be free */
164   tNFC_HAL_NCI_RAS nci_ras; /* nci reassembly error status */
165   TIMER_LIST_ENT
166   nci_wait_rsp_timer; /* Timer for waiting for nci command response */
167   tNFC_HAL_WAIT_RSP nci_wait_rsp; /* nci wait response flag */
168   uint8_t
169       last_hdr[NFC_HAL_SAVED_HDR_SIZE]; /* part of last NCI command header */
170   uint8_t
171       last_cmd[NFC_HAL_SAVED_CMD_SIZE]; /* part of last NCI command payload */
172   void* p_vsc_cback; /* the callback function for last VSC command */
173 } tNFC_HAL_NCIT_CB;
174 
175 /* Control block for device initialization */
176 typedef struct {
177   tNFC_HAL_INIT_STATE initializing_state; /* state of initializing NFCC */
178 
179   uint32_t brcm_hw_id; /* BRCM NFCC HW ID                          */
180   tNFC_HAL_DM_CONFIG next_dm_config; /* next config in post initialization */
181   uint8_t next_startup_vsc; /* next start-up VSC offset in post init    */
182 
183   tNFC_HAL_POWER_MODE power_mode; /* NFCC power mode                          */
184   uint8_t snooze_mode;            /* current snooze mode                      */
185   uint8_t new_snooze_mode;        /* next snooze mode after receiving cmpl    */
186   uint8_t nfc_wake_active_mode;   /* NFC_HAL_LP_ACTIVE_LOW/HIGH               */
187   TIMER_LIST_ENT lp_timer;        /* timer for low power mode                 */
188 
189   tHAL_NFC_STATUS_CBACK*
190       p_prop_cback; /* callback to notify complete of proprietary update */
191 } tNFC_HAL_DEV_CB;
192 
193 #if (NFC_HAL_HCI_INCLUDED == TRUE)
194 
195 /* data members for NFC_HAL-HCI */
196 typedef struct {
197   TIMER_LIST_ENT
198   hci_timer; /* Timer to avoid indefinitely waiting for response */
199   uint8_t*
200       p_hci_netwk_info_buf; /* Buffer for reading HCI Network information */
201   uint8_t* p_hci_netwk_dh_info_buf; /* Buffer for reading HCI Network DH
202                                        information */
203   uint8_t hci_netwk_config_block;  /* Rsp awaiting for hci network configuration
204                                       block */
205   bool b_wait_hcp_conn_create_rsp; /* Waiting for hcp connection create response
206                                       */
207   bool clear_all_pipes_to_uicc1;   /* UICC1 was restarted for patch download */
208   bool update_session_id; /* Next response from NFCC is to Get Session id cmd */
209   bool hci_fw_workaround; /* HAL HCI Workaround need */
210   bool hci_fw_validate_netwk_cmd; /* Flag to indicate if hci network ntf to
211                                      validate */
212   uint8_t hcp_conn_id;            /* NCI Connection id for HCP */
213   uint8_t dh_session_id[1];       /* Byte 0 of DH Session ID */
214 } tNFC_HAL_HCI_CB;
215 
216 #endif
217 
218 typedef uint8_t tNFC_HAL_FLAGS;
219 
220 typedef struct {
221   tHAL_NFC_CBACK* p_stack_cback;     /* Callback for HAL event notification  */
222   tHAL_NFC_DATA_CBACK* p_data_cback; /* Callback for data event notification  */
223 
224   TIMER_LIST_Q quick_timer_queue; /* timer list queue                 */
225   TIMER_LIST_ENT timer;           /* timer for NCI transport task     */
226 
227   tNFC_HAL_NCIT_CB ncit_cb; /* NCI transport */
228   tNFC_HAL_DEV_CB dev_cb;   /* device initialization */
229   tNFC_HAL_NVM nvm_cb;      /* Information about current patch in NVM */
230 
231   /* Patchram control block */
232   tNFC_HAL_PRM_CB prm;
233   tNFC_HAL_PRM_I2C_FIX_CB prm_i2c;
234 
235 #if (NFC_HAL_HCI_INCLUDED == TRUE)
236   /* data members for NFC_HAL-HCI */
237   tNFC_HAL_HCI_CB hci_cb;
238 #endif
239 
240   uint8_t pre_discover_done; /* TRUE, when the prediscover config is complete */
241   tNFC_HAL_FLAGS hal_flags;
242   uint8_t pre_set_mem_idx;
243 
244   uint8_t max_rf_credits; /* NFC Max RF data credits */
245   uint8_t max_ee;         /* NFC Max number of NFCEE supported by NFCC */
246 } tNFC_HAL_CB;
247 
248 /* Global NCI data */
249 extern tNFC_HAL_CB nfc_hal_cb;
250 
251 extern uint8_t* p_nfc_hal_pre_discover_cfg;
252 
253 /****************************************************************************
254 ** Internal nfc functions
255 ****************************************************************************/
256 
257 #if (NFC_HAL_HCI_INCLUDED == TRUE)
258 /* nfc_hal_hci.c */
259 void nfc_hal_hci_enable(void);
260 void nfc_hal_hci_evt_hdlr(tNFC_HAL_HCI_EVENT_DATA* p_evt_data);
261 
262 void nfc_hal_hci_handle_build_info(uint8_t chipverlen, uint8_t* p_chipverstr);
263 #else
264 #define nfc_hal_hci_enable() NFC_HAL_SET_INIT_STATE(NFC_HAL_INIT_STATE_IDLE);
265 #define nfc_hal_hci_handle_build_info(p, a)
266 #define nfc_hal_hci_evt_hdlr(p) ;
267 #endif
268 
269 /* Define default NCI protocol trace function (if protocol tracing is enabled)
270  */
271 #if (NFC_HAL_TRACE_PROTOCOL == TRUE)
272 #if !defined(DISP_NCI)
273 #define DISP_NCI (DispNci)
274 void DispNci(uint8_t* p, uint16_t len, bool is_recv);
275 #endif /* DISP_NCI */
276 
277 #endif /* NFC_HAL_TRACE_PROTOCOL */
278 
279 #endif /* NFC_HAL_INT_H */
280