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
105/*************************************
106 * Generic device handling.
107 *************************************/
108
109
110/* Close an opened nfc device instance */
111static int nfc_close (hw_device_t *dev)
112{
113    int retval = 0;
114    free (dev);
115    retval = HaiTerminateLibrary ();
116    return retval;
117}
118
119
120static int nfc_open (const hw_module_t* module, const char* name, hw_device_t** device)
121{
122    ALOGD ("%s: enter; name=%s", __FUNCTION__, name);
123    int retval = 0; //0 is ok; -1 is error
124
125    if (strcmp (name, NFC_NCI_CONTROLLER) == 0)
126    {
127        bcm2079x_dev_t *dev = calloc (1, sizeof(bcm2079x_dev_t));
128
129        // Common hw_device_t fields
130        dev->nci_device.common.tag = HARDWARE_DEVICE_TAG;
131        dev->nci_device.common.version = 0x00010000; // [31:16] major, [15:0] minor
132        dev->nci_device.common.module = (struct hw_module_t*) module;
133        dev->nci_device.common.close = nfc_close;
134
135        // NCI HAL method pointers
136        dev->nci_device.open = hal_open;
137        dev->nci_device.write = hal_write;
138        dev->nci_device.core_initialized = hal_core_initialized;
139        dev->nci_device.pre_discover = hal_pre_discover;
140        dev->nci_device.close = hal_close;
141        dev->nci_device.control_granted = hal_control_granted;
142        dev->nci_device.power_cycle = hal_power_cycle;
143
144        // Copy in
145        *device = (hw_device_t*) dev;
146
147        retval = HaiInitializeLibrary (dev);
148    }
149    else
150    {
151        retval = -EINVAL;
152    }
153    ALOGD ("%s: exit %d", __FUNCTION__, retval);
154    return retval;
155}
156
157
158static struct hw_module_methods_t nfc_module_methods =
159{
160    .open = nfc_open,
161};
162
163
164struct nfc_nci_module_t HAL_MODULE_INFO_SYM =
165{
166    .common =
167    {
168        .tag = HARDWARE_MODULE_TAG, .module_api_version = 0x0100, // [15:8] major, [7:0] minor (1.0)
169        .hal_api_version = 0x00, // 0 is only valid value
170        .id = NFC_NCI_HARDWARE_MODULE_ID,
171        .name = "Default NFC NCI HW HAL",
172        .author = "The Android Open Source Project",
173        .methods = &nfc_module_methods,
174    },
175};
176