1/******************************************************************************
2 *
3 *  Copyright (C) 1999-2012 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#define LOG_TAG "NfcNciHal"
21#include "OverrideLog.h"
22#include <errno.h>
23#include <hardware/hardware.h>
24#include <hardware/nfc.h>
25#include "HalAdaptation.h"
26
27
28/*********************************
29 * NCI HAL method implementations.
30 *********************************/
31
32
33static int hal_open (const struct nfc_nci_device *p_dev, nfc_stack_callback_t *p_hal_cback, nfc_stack_data_callback_t *p_hal_data_callback)
34{
35    int retval = 0;
36    bcm2079x_dev_t *dev = (bcm2079x_dev_t*) p_dev;
37
38    retval = HaiOpen (dev, p_hal_cback, p_hal_data_callback);
39    return retval;
40}
41
42
43static int hal_write (const struct nfc_nci_device *p_dev,
44        uint16_t data_len, const uint8_t *p_data)
45{
46    int retval = 0;
47    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
48
49    retval = HaiWrite (dev, data_len, p_data);
50    return retval;
51}
52
53
54static int hal_core_initialized (const struct nfc_nci_device *p_dev,
55        uint8_t* p_core_init_rsp_params)
56{
57    int retval = 0;
58    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
59
60    retval = HaiCoreInitialized (dev, p_core_init_rsp_params);
61    return retval;
62}
63
64
65static int hal_pre_discover (const struct nfc_nci_device *p_dev)
66{
67    int retval = 0;
68    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
69
70    retval = HaiPreDiscover (dev);
71    return retval;
72}
73
74
75static int hal_close (const struct nfc_nci_device *p_dev)
76{
77    int retval = 0;
78    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
79
80    retval = HaiClose (dev);
81    return retval;
82}
83
84
85static int hal_control_granted (const struct nfc_nci_device *p_dev)
86{
87    int retval = 0;
88    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
89
90    retval = HaiControlGranted (dev);
91   return retval;
92}
93
94
95static int hal_power_cycle (const struct nfc_nci_device *p_dev)
96{
97    int retval = 0;
98    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
99
100    retval = HaiPowerCycle (dev);
101    return retval;
102}
103
104
105static int hal_get_max_nfcee (const struct nfc_nci_device *p_dev, uint8_t* maxNfcee)
106{
107    int retval = 0;
108    bcm2079x_dev_t* dev = (bcm2079x_dev_t*) p_dev;
109
110    retval = HaiGetMaxNfcee (dev, maxNfcee);
111    return retval;
112}
113
114
115/*************************************
116 * Generic device handling.
117 *************************************/
118
119
120/* Close an opened nfc device instance */
121static int nfc_close (hw_device_t *dev)
122{
123    int retval = 0;
124    free (dev);
125    retval = HaiTerminateLibrary ();
126    return retval;
127}
128
129
130static int nfc_open (const hw_module_t* module, const char* name, hw_device_t** device)
131{
132    ALOGD ("%s: enter; name=%s", __FUNCTION__, name);
133    int retval = 0; //0 is ok; -1 is error
134
135    if (strcmp (name, NFC_NCI_CONTROLLER) == 0)
136    {
137        bcm2079x_dev_t *dev = calloc (1, sizeof(bcm2079x_dev_t));
138
139        // Common hw_device_t fields
140        dev->nci_device.common.tag = HARDWARE_DEVICE_TAG;
141        dev->nci_device.common.version = 0x00010000; // [31:16] major, [15:0] minor
142        dev->nci_device.common.module = (struct hw_module_t*) module;
143        dev->nci_device.common.close = nfc_close;
144
145        // NCI HAL method pointers
146        dev->nci_device.open = hal_open;
147        dev->nci_device.write = hal_write;
148        dev->nci_device.core_initialized = hal_core_initialized;
149        dev->nci_device.pre_discover = hal_pre_discover;
150        dev->nci_device.close = hal_close;
151        dev->nci_device.control_granted = hal_control_granted;
152        dev->nci_device.power_cycle = hal_power_cycle;
153        //dev->nci_device.get_max_ee = hal_get_max_nfcee;
154
155
156        // Copy in
157        *device = (hw_device_t*) dev;
158
159        retval = HaiInitializeLibrary (dev);
160    }
161    else
162    {
163        retval = -EINVAL;
164    }
165    ALOGD ("%s: exit %d", __FUNCTION__, retval);
166    return retval;
167}
168
169
170static struct hw_module_methods_t nfc_module_methods =
171{
172    .open = nfc_open,
173};
174
175
176struct nfc_nci_module_t HAL_MODULE_INFO_SYM =
177{
178    .common =
179    {
180        .tag = HARDWARE_MODULE_TAG, .module_api_version = 0x0100, // [15:8] major, [7:0] minor (1.0)
181        .hal_api_version = 0x00, // 0 is only valid value
182        .id = NFC_NCI_BCM2079X_HARDWARE_MODULE_ID,
183        .name = "BCM2079x NFC NCI HW HAL",
184        .author = "Broadcom Corporation",
185        .methods = &nfc_module_methods,
186    },
187};
188