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