summaryrefslogtreecommitdiff
path: root/halimpl/hal/phNxpNciHal.h
blob: 4be4320720af793731eaa837012f6fb4e7c3694d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
 * Copyright (C) 2015-2020 NXP Semiconductors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef _PHNXPNCIHAL_H_
#define _PHNXPNCIHAL_H_

#include "NxpMfcReader.h"
#include <hardware/nfc.h>
#include <phNxpNciHal_utils.h>
#include "NxpNfcCapability.h"
#include <vendor/nxp/hardware/nfc/2.0/types.h>
#include "DwpEseUpdater.h"
#ifdef ENABLE_ESE_CLIENT
#include "EseUpdateChecker.h"
#endif

using namespace std;

/********************* Definitions and structures *****************************/
#define MAX_RETRY_COUNT 5
#define NCI_MAX_DATA_LEN 300
#define NCI_POLL_DURATION 500
#define HAL_NFC_ENABLE_I2C_FRAGMENTATION_EVT 0x07
#define HAL_NFC_POST_MIN_INIT_CPLT_EVT  0x09
#define NXP_STAG_TIMEOUT_BUF_LEN 0x04 /*FIXME:TODO:remove*/
#define NXP_WIREDMODE_RESUME_TIMEOUT_LEN 0x04
#define NFCC_DECIDES 00
#define POWER_ALWAYS_ON 01
#define LINK_ALWAYS_ON 02
#undef P2P_PRIO_LOGIC_HAL_IMP

#define NCI_VERSION_2_0 0x20
#define NCI_VERSION_1_1 0x11
#define NCI_VERSION_1_0 0x10
#define NCI_VERSION_UNKNOWN 0x00
#define NQ_330_440_CHIPID "0x51"

/* Uncomment define ENABLE_ESE_CLIENT to
enable eSE client */
//#define ENABLE_ESE_CLIENT TRUE

/*Mem alloc with 8 byte alignment*/
#define size_align(sz) ((((sz)-1) | 7) + 1)
#define nxp_malloc(size) malloc(size_align((size)))

typedef void(phNxpNciHal_control_granted_callback_t)();

/*ROM CODE VERSION FW*/
#define FW_MOBILE_ROM_VERSION_PN551 0x10
#define FW_MOBILE_ROM_VERSION_PN553 0x11
#define FW_MOBILE_ROM_VERSION_PN548AD 0x10
#define FW_MOBILE_ROM_VERSION_PN547C2 0x08
#define FW_MOBILE_ROM_VERSION_PN557 0x12

#define FW_DBG_REASON_AVAILABLE     (0xA3)


/* NCI Data */
#define NCI_MT_CMD  0x20
#define NCI_MT_RSP  0x40
#define NCI_MT_NTF  0x60

#define CORE_RESET_TRIGGER_TYPE_CORE_RESET_CMD_RECEIVED 0x02
#define CORE_RESET_TRIGGER_TYPE_POWERED_ON              0x01
#define NCI_MSG_CORE_RESET           0x00
#define NCI_MSG_CORE_INIT            0x01
#define NCI_MT_MASK                  0xE0
#define NCI_OID_MASK                 0x3F

#define NXP_MAX_CONFIG_STRING_LEN 260
#define NCI_HEADER_SIZE 3

#define MAX_IOCTL_TRANSCEIVE_CMD_LEN 256
#define MAX_IOCTL_TRANSCEIVE_RESP_LEN 256

typedef struct nci_data {
  uint16_t len;
  uint8_t p_data[NCI_MAX_DATA_LEN];
} nci_data_t;

typedef enum {
  HAL_STATUS_CLOSE = 0,
  HAL_STATUS_OPEN,
  HAL_STATUS_MIN_OPEN
} phNxpNci_HalStatus;
typedef enum {
  GPIO_UNKNOWN = 0x00,
  GPIO_STORE = 0x01,
  GPIO_STORE_DONE = 0x02,
  GPIO_RESTORE = 0x10,
  GPIO_RESTORE_DONE = 0x20,
  GPIO_CLEAR = 0xFF
} phNxpNciHal_GpioInfoState;

enum NxpNfcHalStatus {
    /** In case of an error, HCI network needs to be re-initialized */
    HAL_NFC_STATUS_RESTART = 0x30,
    HAL_NFC_HCI_NV_RESET = 0x40,
    HAL_NFC_CONFIG_ESE_LINK_COMPLETE = 0x50
};

typedef struct phNxpNciGpioInfo {
  phNxpNciHal_GpioInfoState state;
  uint8_t values[2];
} phNxpNciGpioInfo_t;

#ifdef ENABLE_ESE_CLIENT
extern ESE_UPDATE_STATE eseUpdateSpi;
extern ESE_UPDATE_STATE eseUpdateDwp;
#endif

/* Macros to enable and disable extensions */
#define HAL_ENABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 1)
#define HAL_DISABLE_EXT() (nxpncihal_ctrl.hal_ext_enabled = 0)

typedef struct phNxpNciInfo {
  uint8_t   nci_version;
  bool_t    wait_for_ntf;
}phNxpNciInfo_t;
/* NCI Control structure */
typedef struct phNxpNciHal_Control {
  phNxpNci_HalStatus halStatus; /* Indicate if hal is open or closed */
  pthread_t client_thread;      /* Integration thread handle */
  uint8_t thread_running;       /* Thread running if set to 1, else set to 0 */
  phLibNfc_sConfig_t gDrvCfg;   /* Driver config data */

  /* Rx data */
  uint8_t* p_rx_data;
  uint16_t rx_data_len;

  /* Rx data */
  uint8_t* p_rx_ese_data;
  uint16_t rx_ese_data_len;
  /* libnfc-nci callbacks */
  nfc_stack_callback_t* p_nfc_stack_cback;
  nfc_stack_data_callback_t* p_nfc_stack_data_cback;

  /* control granted callback */
  phNxpNciHal_control_granted_callback_t* p_control_granted_cback;

  /* HAL open status */
  bool_t hal_open_status;

  bool_t is_wait_for_ce_ntf;

  /* HAL extensions */
  uint8_t hal_ext_enabled;

  /* Waiting semaphore */
  phNxpNciHal_Sem_t ext_cb_data;
  sem_t syncSpiNfc;

  uint16_t cmd_len;
  uint8_t p_cmd_data[NCI_MAX_DATA_LEN];
  uint16_t rsp_len;
  uint8_t p_rsp_data[NCI_MAX_DATA_LEN];

  /* retry count used to force download */
  uint16_t retry_cnt;
  uint8_t read_retry_cnt;
  phNxpNciInfo_t nci_info;
  uint8_t hal_boot_mode;
  tNFC_chipType chipType;
  /* to store and restore gpio values */
  phNxpNciGpioInfo_t phNxpNciGpioInfo;
  bool bIsForceFwDwnld;
  bool isHciCfgEvtRequested;
} phNxpNciHal_Control_t;

typedef struct {
  uint8_t fw_update_reqd;
  uint8_t rf_update_reqd;
} phNxpNciHal_FwRfupdateInfo_t;

typedef struct phNxpNciClock {
  bool_t isClockSet;
  uint8_t p_rx_data[20];
} phNxpNciClock_t;

typedef struct phNxpNciRfSetting {
  bool_t isGetRfSetting;
  uint8_t p_rx_data[20];
} phNxpNciRfSetting_t;

/*set config management*/

#define TOTAL_DURATION 0x00
#define ATR_REQ_GEN_BYTES_POLL 0x29
#define ATR_REQ_GEN_BYTES_LIS 0x61
#define LEN_WT 0x60

/*Whenever a new get cfg need to be sent,
 * array must be updated with defined config type*/
static const uint8_t get_cfg_arr[] = {TOTAL_DURATION, ATR_REQ_GEN_BYTES_POLL,
                                      ATR_REQ_GEN_BYTES_LIS, LEN_WT};

typedef enum {
  EEPROM_RF_CFG,
  EEPROM_FW_DWNLD,
  EEPROM_WIREDMODE_RESUME_TIMEOUT,
  EEPROM_ESE_SVDD_POWER,
  EEPROM_ESE_POWER_EXT_PMU,
  EEPROM_PROP_ROUTING,
  EEPROM_ESE_SESSION_ID,
  EEPROM_SWP1_INTF,
  EEPROM_SWP1A_INTF,
  EEPROM_SWP2_INTF,
  EEPROM_NDEF_INTF_CFG
} phNxpNci_EEPROM_request_type_t;

typedef enum {
  phNxpNciHalNfc_e_SetIdleMode, /* Set the current mode of operation to Idle*/
  phNxpNciHalNfc_e_SetWiredMode,  /* Set the current mode of operation to Wired*/
} phNxpNfcHalEseState;

typedef enum {
  phNxpNfcHciInitStatus_e_Complete = 0x00,
  phNxpNfcHciInitStatus_e_Start = 0x01
} phNxpNfcHciInitStatus;

typedef struct phNxpNci_Extn_Cmd{
  uint16_t cmd_len;
  uint8_t p_cmd[MAX_IOCTL_TRANSCEIVE_CMD_LEN];
}phNxpNci_Extn_Cmd_t;

typedef struct phNxpNci_Extn_Resp{
  uint32_t status;
  uint16_t rsp_len;
  uint8_t p_rsp[MAX_IOCTL_TRANSCEIVE_RESP_LEN];
}phNxpNci_Extn_Resp_t;

typedef struct phNxpNci_EEPROM_info {
  uint8_t request_mode;
  phNxpNci_EEPROM_request_type_t request_type;
  uint8_t update_mode;
  uint8_t* buffer;
  uint8_t bufflen;
} phNxpNci_EEPROM_info_t;

typedef struct phNxpNci_getCfg_info {
  bool_t isGetcfg;
  uint8_t total_duration[4];
  uint8_t total_duration_len;
  uint8_t atr_req_gen_bytes[48];
  uint8_t atr_req_gen_bytes_len;
  uint8_t atr_res_gen_bytes[48];
  uint8_t atr_res_gen_bytes_len;
  uint8_t pmid_wt[3];
  uint8_t pmid_wt_len;
} phNxpNci_getCfg_info_t;

typedef enum {
  NFC_FORUM_PROFILE,
  EMV_CO_PROFILE,
  INVALID_PROFILe
} phNxpNciProfile_t;

typedef enum {
  NFC_NORMAL_BOOT_MODE,
  NFC_FAST_BOOT_MODE,
  NFC_OSU_BOOT_MODE
} phNxpNciBootMode;
/* NXP Poll Profile control structure */
typedef struct phNxpNciProfile_Control {
  phNxpNciProfile_t profile_type;
  uint8_t bClkSrcVal; /* Holds the System clock source read from config file */
  uint8_t
      bClkFreqVal;  /* Holds the System clock frequency read from config file */
  uint8_t bTimeout; /* Holds the Timeout Value */
} phNxpNciProfile_Control_t;

struct phNxpNfcScrResetEmvcoCmd{
  uint64_t len;
  uint8_t cmd[10];
};

struct phNxpNfcHalConfig {
  uint8_t ese_listen_tech_mask;
  uint8_t default_nfcee_disc_timeout;
  uint8_t default_nfcee_timeout;
  uint8_t ese_wired_prt_mask;
  uint8_t uicc_wired_prt_mask;
  uint8_t wired_mode_rf_field_enable;
  uint8_t aid_block_route;
  uint8_t esePowerDhControl;
  uint8_t tagOpTimeout;
  uint8_t loaderServiceVersion;
  uint8_t defaultNfceeDiscTimeout;
  uint8_t dualUiccEnable;
  uint8_t ceRouteStrictDisable;
  uint32_t osDownloadTimeoutValue;
  uint8_t defaultAidRoute;
  uint8_t defaultAidPwrState;
  uint8_t defaultRoutePwrState;
  uint8_t defaultOffHostPwrState;
  uint8_t jcopDlAtBootEnable;
  uint8_t defaultNfceeTimeout;
  uint8_t nxpNfcChip;
  uint8_t coreScrnOffAutonomousEnable;
  uint8_t p61LsDefaultInterface;
  uint8_t p61JcopDefaultInterface;
  uint8_t agcDebugEnable;
  uint8_t felicaCltPowerState;
  uint32_t cmdRspTimeoutValue;
  uint8_t checkDefaultProtoSeId;
  uint8_t nfccPassiveListenTimeout;
  uint32_t nfccStandbyTimeout;
  uint32_t wmMaxWtxCount;
  uint32_t nfccRfFieldEventTimeout;
  uint8_t allowWiredInMifareDesfireClt;
  uint8_t dwpIntfResetEnable;
  uint8_t nxpLogHalLoglevel;
  uint8_t nxpLogExtnsLogLevel;
  uint8_t nxpLogTmlLogLevel;
  uint8_t nxpLogFwDnldLogLevel;
  uint8_t nxpLogNcixLogLevel;
  uint8_t nxpLogNcirLogLevel;
  uint8_t scrCfgFormat;
  uint8_t etsiReaderEnable;
  uint8_t techAbfRoute;
  uint8_t techAbfPwrState;
  uint8_t wTagSupport;
  uint8_t t4tNfceePwrState;
  phNxpNfcScrResetEmvcoCmd scrResetEmvco;
};
/* Internal messages to handle callbacks */
#define NCI_HAL_OPEN_CPLT_MSG 0x411
#define NCI_HAL_CLOSE_CPLT_MSG 0x412
#define NCI_HAL_POST_INIT_CPLT_MSG 0x413
#define NCI_HAL_PRE_DISCOVER_CPLT_MSG 0x414
#define NCI_HAL_ERROR_MSG 0x415
#define NCI_HAL_HCI_NETWORK_RESET_MSG 0x416
#define NCI_HAL_RX_MSG 0xF01
#define NCI_HAL_POST_MIN_INIT_CPLT_MSG 0xF02
#define NCIHAL_CMD_CODE_LEN_BYTE_OFFSET (2U)
#define NCIHAL_CMD_CODE_BYTE_LEN (3U)

/******************** NCI HAL exposed functions *******************************/

int phNxpNciHal_check_ncicmd_write_window(uint16_t cmd_len, uint8_t* p_cmd);
void phNxpNciHal_request_control(void);
void phNxpNciHal_release_control(void);
NFCSTATUS phNxpNciHal_send_get_cfgs();
int phNxpNciHal_write_unlocked(uint16_t data_len, const uint8_t* p_data);
NFCSTATUS request_EEPROM(phNxpNci_EEPROM_info_t* mEEPROM_info);
NFCSTATUS phNxpNciHal_send_nfcee_pwr_cntl_cmd(uint8_t type);
string phNxpNciHal_getSystemProperty(string key);
bool phNxpNciHal_setSystemProperty(string key, string value);
void seteSEClientState(uint8_t state);
void eSEClientUpdate_NFC_Thread();
bool phNxpNciHal_Abort();
bool getJcopUpdateRequired();
bool getLsUpdateRequired();

/*******************************************************************************
**
** Function         phNxpNciHal_configFeatureList
**
** Description      Configures the featureList based on chip type
**                  HW Version information number will provide chipType.
**                  HW Version can be obtained from CORE_INIT_RESPONSE(NCI 1.0)
**                  or CORE_RST_NTF(NCI 2.0)
**
** Parameters       CORE_INIT_RESPONSE/CORE_RST_NTF, len
**
** Returns          none
*******************************************************************************/
void phNxpNciHal_configFeatureList(uint8_t* init_rsp, uint16_t rsp_len);

/*******************************************************************************
**
** Function         phNxpNciHal_getChipType
**
** Description      Gets the chipType which is configured during bootup
**
** Parameters       none
**
** Returns          chipType
*******************************************************************************/
tNFC_chipType phNxpNciHal_getChipType();

/*******************************************************************************
**
** Function         phNxpNciHal_getNxpConfig
**
** Description      Read vendor configuration macro values
**
** Parameters       ioctl input/output struct.
**
** Returns          none
*******************************************************************************/
void phNxpNciHal_getNxpConfig(phNxpNfcHalConfig *pNxpNfcHalConfig);
/******************************************************************************
 * Function         phNxpNciHal_getNxpTransitConfig
 *
 * Description      This function overwrite libnfc-nxpTransit.conf file
 *                  with transitConfValue.
 *
 * Returns          status.
 *
 ******************************************************************************/
bool phNxpNciHal_setNxpTransitConfig(char *transitConfValue);
#endif /* _PHNXPNCIHAL_H_ */

/******************************************************************************
 * Function         phNxpNciHal_setEseState
 *
 * Description      This function is called for to update ese state
 *
 * Returns          void.
 *
 ******************************************************************************/
NFCSTATUS phNxpNciHal_setEseState(phNxpNfcHalEseState eSEstate);

/*******************************************************************************
 **
 ** Function         phNxpNciHal_getchipType
 **
 ** Description      Gets the chipType from hal which is already configured
 **                  during init time.
 **
 ** Returns          chipType
 *******************************************************************************/
uint8_t phNxpHal_getchipType();

/*******************************************************************************
**
** Function         phNxpNciHal_setNfcServicePid
**
** Description      This function request to pn54x driver to
**                  update NFC service process ID for signalling.
**
** Returns          0 if api call success, else -1
**
*******************************************************************************/
uint16_t phNxpNciHal_setNfcServicePid(uint64_t pid);

/******************************************************************************
 * Function         phNxpNciHal_getEseState
 *
 * Description      This function is called for to get  ese state
 *
 * Returns          void.
 *
 ******************************************************************************/
NFCSTATUS phNxpNciHal_getEseState();

/******************************************************************************
 * Function         phNxpNciHal_ReleaseSVDDWait
 *
 * Description      This function release wait for svdd change
 *                  of P61.
 *
 * Returns          NFCSTATUS.
 *
 ******************************************************************************/
NFCSTATUS phNxpNciHal_ReleaseSVDDWait(uint32_t level);

/******************************************************************************
 * Function         phNxpNciHal_ReleaseDWPOnOffWait
 *
 * Description      This function release wait for DWP On/Off
 *                  of P73. output returned as Status
 * Returns          NFCSTATUS.
 *
 ******************************************************************************/
NFCSTATUS phNxpNciHal_ReleaseDWPOnOffWait(uint32_t level);

/******************************************************************************
 * Function         phNxpNciHal_getSPMStatus
 *
 * Description      This function gets the SPM state before FW download
 *
 * Returns          0 as success -1 as failed.
 *
 *******************************************************************************/
int phNxpNciHal_getSPMStatus(uint32_t level);

/******************************************************************************
 * Function         phNxpNciHal_hciInitUpdateState
 *
 * Description      This function Sends HCI Events to nfc HAL
 *
 * Returns          0 as success -1 as failed.
 *
 *******************************************************************************/
int32_t phNxpNciHal_hciInitUpdateState(phNxpNfcHciInitStatus HciStatus);

/******************************************************************************
 * Function         phNxpNciHal_hciInitUpdateStateComplete
 *
 * Description       This function posts event HAL_NFC_CONFIG_ESE_LINK_COMPLETE
 *
 * Returns          0 as success -1 as failed..
 *
 *******************************************************************************/
int32_t phNxpNciHal_hciInitUpdateStateComplete();

/******************************************************************************
 * Function         phNxpNciHal_GetCachedNfccConfig
 *
 * Description      This function receives the cached configinfo.
 *
 * Returns          void.
 *
 *******************************************************************************/
void phNxpNciHal_GetCachedNfccConfig(phNxpNci_getCfg_info_t *pGetCfg_info);

/*******************************************************************************
**
** Function         phNxpNciHal_resetEse
**
** Description      It shall be used to to reset eSE by proprietary command.
**
** Parameters       None
**
** Returns          status of eSE reset response
*******************************************************************************/
NFCSTATUS phNxpNciHal_resetEse();

/******************************************************************************
 * Function         phNxpNciHal_nciTransceive
 *
 * Description      This function does tarnsceive of nci command
 *
 * Returns          void.
 *
 *******************************************************************************/
void phNxpNciHal_nciTransceive(phNxpNci_Extn_Cmd_t *in, phNxpNci_Extn_Resp_t *out);