1/*
2 * Copyright (C) 2012-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
17#define LOG_TAG "NxpNfcNciHal"
18
19#include <errno.h>
20#include <hardware/hardware.h>
21#include <hardware/nfc.h>
22#include <log/log.h>
23#include <phNxpNciHal_Adaptation.h>
24#include <stdlib.h>
25#include <string.h>
26
27/*****************************************************************************
28 * NXP NCI HAL Function implementations.
29 *****************************************************************************/
30
31/*******************************************************************************
32**
33** Function         hal_open
34**
35** Description      It opens and initialzes the physical connection with NFCC.
36**
37** Returns          0 if successful
38**
39*******************************************************************************/
40static int hal_open(const struct nfc_nci_device* p_dev,
41                    nfc_stack_callback_t p_hal_cback,
42                    nfc_stack_data_callback_t* p_hal_data_callback) {
43  int retval = 0;
44
45  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
46  retval = phNxpNciHal_open(p_hal_cback, p_hal_data_callback);
47
48  return retval;
49}
50
51/*******************************************************************************
52**
53** Function         hal_write
54**
55** Description      Write the data to NFCC.
56**
57** Returns          Number of bytes successfully written to NFCC.
58**
59*******************************************************************************/
60static int hal_write(const struct nfc_nci_device* p_dev, uint16_t data_len,
61                     const uint8_t* p_data) {
62  int retval = 0;
63  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
64
65  retval = phNxpNciHal_write(data_len, p_data);
66  return retval;
67}
68
69/*******************************************************************************
70**
71** Function         hal_core_initialized
72**
73** Description      Notify NFCC after successful initialization of NFCC.
74**                  All proprietary settings can be done here.
75**
76** Returns          0 if successful
77**
78*******************************************************************************/
79static int hal_core_initialized(const struct nfc_nci_device* p_dev,
80                                uint8_t* p_core_init_rsp_params) {
81  int retval = 0;
82  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
83
84  retval = phNxpNciHal_core_initialized(p_core_init_rsp_params);
85  return retval;
86}
87
88/*******************************************************************************
89**
90** Function         hal_pre_discover
91**
92** Description      Notify NFCC before start discovery.
93**
94** Returns          0 if successful
95**
96*******************************************************************************/
97static int hal_pre_discover(const struct nfc_nci_device* p_dev) {
98  int retval = 0;
99  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
100
101  retval = phNxpNciHal_pre_discover();
102  return retval;
103}
104
105/*******************************************************************************
106**
107** Function         hal_close
108**
109** Description      Close the NFCC interface and free all resources.
110**
111** Returns          0 if successful
112**
113*******************************************************************************/
114static int hal_close(const struct nfc_nci_device* p_dev) {
115  int retval = 0;
116  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
117
118  retval = phNxpNciHal_close();
119  return retval;
120}
121
122/*******************************************************************************
123**
124** Function         hal_control_granted
125**
126** Description      Notify NFCC that control is granted to HAL.
127**
128** Returns          0 if successful
129**
130*******************************************************************************/
131static int hal_control_granted(const struct nfc_nci_device* p_dev) {
132  int retval = 0;
133  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
134
135  retval = phNxpNciHal_control_granted();
136  return retval;
137}
138
139/*******************************************************************************
140**
141** Function         hal_power_cycle
142**
143** Description      Notify power cycling has performed.
144**
145** Returns          0 if successful
146**
147*******************************************************************************/
148static int hal_power_cycle(const struct nfc_nci_device* p_dev) {
149  int retval = 0;
150  pn547_dev_t* dev = (pn547_dev_t*)p_dev;
151
152  retval = phNxpNciHal_power_cycle();
153  return retval;
154}
155
156/*************************************
157 * Generic device handling.
158 *************************************/
159
160/*******************************************************************************
161**
162** Function         nfc_close
163**
164** Description      Close the nfc device instance.
165**
166** Returns          0 if successful
167**
168*******************************************************************************/
169static int nfc_close(hw_device_t* dev) {
170  int retval = 0;
171  free(dev);
172  return retval;
173}
174
175/*******************************************************************************
176**
177** Function         nfc_open
178**
179** Description      Open the nfc device instance.
180**
181** Returns          0 if successful
182**
183*******************************************************************************/
184static int nfc_open(const hw_module_t* module, const char* name,
185                    hw_device_t** device) {
186  ALOGD("%s: enter; name=%s", __func__, name);
187  int retval = 0; /* 0 is ok; -1 is error */
188
189  if (strcmp(name, NFC_NCI_CONTROLLER) == 0) {
190    pn547_dev_t* dev = calloc(1, sizeof(pn547_dev_t));
191
192    /* Common hw_device_t fields */
193    dev->nci_device.common.tag = HARDWARE_DEVICE_TAG;
194    dev->nci_device.common.version =
195        0x00010000; /* [31:16] major, [15:0] minor */
196    dev->nci_device.common.module = (struct hw_module_t*)module;
197    dev->nci_device.common.close = nfc_close;
198
199    /* NCI HAL method pointers */
200    dev->nci_device.open = hal_open;
201    dev->nci_device.write = hal_write;
202    dev->nci_device.core_initialized = hal_core_initialized;
203    dev->nci_device.pre_discover = hal_pre_discover;
204    dev->nci_device.close = hal_close;
205    dev->nci_device.control_granted = hal_control_granted;
206    dev->nci_device.power_cycle = hal_power_cycle;
207
208    *device = (hw_device_t*)dev;
209  } else {
210    retval = -EINVAL;
211  }
212
213  ALOGD("%s: exit %d", __func__, retval);
214  return retval;
215}
216
217/* Android hardware module definition */
218static struct hw_module_methods_t nfc_module_methods = {
219    .open = nfc_open,
220};
221
222/* NFC module definition */
223struct nfc_nci_module_t HAL_MODULE_INFO_SYM = {
224    .common =
225        {
226            .tag = HARDWARE_MODULE_TAG,
227            .module_api_version = 0x0100, /* [15:8] major, [7:0] minor (1.0) */
228            .hal_api_version = 0x00,      /* 0 is only valid value */
229            .id = NFC_NCI_HARDWARE_MODULE_ID,
230            .name = "NXP PN54X NFC NCI HW HAL",
231            .author = "NXP Semiconductors",
232            .methods = &nfc_module_methods,
233        },
234};
235