1 /*
2  *    Copyright (C) 2013 SAMSUNG S.LSI
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 #ifndef __NFC_SEC_HAL__
18 #define __NFC_SEC_HAL__
19 
20 #include <hardware/nfc.h>
21 
22 #include "hal_msg.h"
23 #include "osi.h"
24 
25 #include "android/hardware/nfc/1.2/INfc.h"
26 #include "android/hardware/nfc/1.2/types.h"
27 
28 using ::android::hardware::nfc::V1_1::NfcConfig;
29 
30 #ifndef __bool_true_false_are_defined
31 #define __bool_true_false_are_defined
32 typedef enum { false, true } bool;
33 #endif
34 
35 /***************************************
36  * DEVICE
37  ***************************************/
38 typedef enum {
39   NFC_DEV_MODE_OFF = 0,
40   NFC_DEV_MODE_ON,
41   NFC_DEV_MODE_BOOTLOADER,
42 } eNFC_DEV_MODE;
43 
44 /***************************************
45  * States
46  ***************************************/
47 typedef enum {
48   HAL_STATE_INIT,
49   HAL_STATE_DEINIT,
50   HAL_STATE_OPEN,
51   HAL_STATE_VS,
52   HAL_STATE_POSTINIT,
53   HAL_STATE_SERVICE,
54   HAL_STATE_GRANTED,
55   HAL_STATE_CLOSE,      /* VTS */
56   HAL_STATE_POWERCYCLE, /* VTS */
57 } eHAL_STATE;
58 
59 /* FW sub-state */
60 #define FW_DATA_PAYLOAD_MAX (256)
61 
62 /* VS sub-state */
63 typedef enum {
64   VS_INIT,
65   VS_W4_COMPLETE,
66 } eNFC_HAL_VS_STATE;
67 
68 /***************************************
69  * Structures
70  ***************************************/
71 /* VS related */
72 
73 typedef struct {
74   eNFC_HAL_VS_STATE state;
75 } tNFC_HAL_VS_INFO;
76 enum {
77   CFG_SLEEP_TIMEOUT = 0,
78   CFG_WAKEUP_DELAY,
79   CFG_NCI_PROP,
80   CFG_POWER_DRIVER,
81   CFG_TRANS_DRIVER,
82   CFG_TRACE_LEVEL,
83   CFG_DATA_TRACE,
84   CFG_FW_CLK_SPEED,
85 };
86 
87 static const char* cfg_name_table[] = {
88     "SLEEP_TIMEOUT", "WAKEUP_DELAY", "NCI_PROP",   "POWER_DRIVER",
89     "TRANS_DRIVER",  "TRACE_LEVEL",  "DATA_TRACE", "FW_CFG_CLK_SPEED",
90 };
91 
92 typedef struct {
93   uint32_t sleep_timeout;
94   uint32_t override_timeout;
95 } tNFC_HAL_CONFIG;
96 
97 /* Granted related */
98 #define HAL_GRANT_SEND_NEXT 0x00
99 #define HAL_GRANT_WAIT_READ 0x01
100 #define HAL_GRANT_FINISH 0x02
101 typedef uint8_t(tNFC_HAL_GRANT_CALLBACK)(tNFC_NCI_PKT* pkt);
102 
103 /* FLAGS */
104 #define HAL_FLAG_MASK_USING_TIMER 0x000F
105 #define HAL_FLAG_W4_CORE_RESET_RSP 0x0001
106 #define HAL_FLAG_W4_CORE_INIT_RSP 0x0002
107 
108 #define HAL_FLAG_PROP_RESET 0x0010
109 #define HAL_FLAG_CLK_SET 0x0020
110 #define HAL_FLAG_PROP_ONE_TIMER 0x0040
111 
112 #define HAL_FLAG_MASK_ALREADY 0x0F00
113 #define HAL_FLAG_ALREADY_RESET 0x0100
114 #define HAL_FLAG_ALREADY_INIT 0x0200
115 
116 #define HAL_FLAG_MASK_SYSTEM 0xF000
117 #define HAL_FLAG_FORCE_FW_UPDATE 0x1000
118 #define HAL_FLAG_NTF_TRNS_ERROR 0x2000
119 #define HAL_FLAG_RETRY_TRNS 0x4000
120 
121 /***************************************
122  * Main information(context block)
123  ***************************************/
124 typedef struct {
125   tNFC_HAL_CONFIG cfg;
126   eHAL_STATE state; /* HAL state */
127   tNFC_HAL_VS_INFO vs_info;
128 
129   tOSI_TASK_HANDLER msg_task; /* HAL main task */
130   tOSI_QUEUE_HANDLER msg_q;
131   nfc_stack_callback_t* stack_cback;     /* Callback for HAL event */
132   nfc_stack_data_callback_t* data_cback; /* Callback for data event */
133   tNFC_NCI_PKT* nci_last_pkt;            /* last sent package */
134   tNFC_NCI_PKT* nci_fragment_pkt;        /* Control msg flagmentation */
135   tOSI_TIMER_HANDLER nci_timer;          /* Timer for NCI message */
136   tOSI_TIMER_HANDLER sleep_timer;        /* Timer for NCI message */
137   int trans_dev;                         /* transport device */
138   int power_dev;                         /* power device */
139   tOSI_QUEUE_HANDLER nci_q;
140   tNFC_HAL_GRANT_CALLBACK* grant_cback;
141   unsigned int flag;
142   /* START [H16031401] */
143   uint8_t msg_event; /* receiving message event */
144                      /* END [H16031401] */
145 } tNFC_HAL_CB;
146 
147 /*************************************
148  * Global
149  *************************************/
150 extern tNFC_HAL_CB nfc_hal_info;
151 
152 /*************************************
153  * NFC HAL API prototype
154  *************************************/
155 int nfc_hal_init(void);
156 void nfc_hal_deinit(void);
157 int nfc_hal_open(nfc_stack_callback_t* p_cback,
158                  nfc_stack_data_callback_t* p_data_cback);
159 int nfc_hal_write(uint16_t data_len, const uint8_t* p_data);
160 int nfc_hal_core_initialized(uint8_t* p_core_init_rsp_params);
161 int nfc_hal_pre_discover();
162 int nfc_hal_close();
163 int nfc_hal_control_granted();
164 int nfc_hal_power_cycle();
165 // HIDL INfc 1.1
166 int nfc_hal_factory_reset(void);
167 int nfc_hal_closeForPowerOffCase(void);
168 void nfc_hal_getVendorConfig(NfcConfig& config);  // To-Do
169 
170 /*************************************
171  * NFC HAL functions.
172  *************************************/
173 bool nfc_stack_cback(nfc_event_t event, nfc_status_t event_status);
174 bool nfc_data_callback(tNFC_NCI_PKT* pkt);
175 
176 void nfc_hal_task(void);
177 
178 /* START - VTS */
179 void nfc_hal_power_sm(tNFC_HAL_MSG* msg);
180 /* END - VTS */
181 void nfc_hal_grant_sm(tNFC_HAL_MSG* msg);
182 void nfc_hal_service_sm(tNFC_HAL_MSG* msg);
183 void nfc_hal_vs_sm(tNFC_HAL_MSG* msg);
184 void nfc_hal_postinit_sm(tNFC_HAL_MSG* msg);
185 void nfc_hal_open_sm(tNFC_HAL_MSG* msg);
186 
187 void setSleepTimeout(int option, uint32_t timeout);
188 
189 const char* event_to_string(uint8_t event);
190 const char* state_to_string(eHAL_STATE state);
191 
192 // SM
193 void hal_update_sleep_timer(void);
194 int __send_to_device(uint8_t* data, size_t len);
195 
196 // FW
197 void fw_force_update(void* param);
198 int nfc_fw_send_data(uint8_t* data, int len);
199 int fw_read_payload(tNFC_HAL_MSG* msg);
200 
201 // NCI
202 int hal_nci_send(tNFC_NCI_PKT* pkt);
203 void hal_nci_send_reset(void);
204 void hal_nci_send_prop_fw_cfg(void);
205 void hal_nci_send_clearLmrt(void);
206 void nci_init_timeout(void* param);
207 bool nfc_hal_prehandler(tNFC_NCI_PKT* pkt);
208 int nci_read_payload(tNFC_HAL_MSG* msg);
209 
210 // TRACE
211 void sec_nci_analyzer(tNFC_NCI_PKT* pkt);
212 
213 #endif  // __NFC_SEC_HAL__
214