1 /*
2  * Copyright (C) 2012 The Android Open Source Project
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 #include <errno.h>
17 #include <malloc.h>
18 #include <string.h>
19 
20 #include <cutils/log.h>
21 #include <hardware/hardware.h>
22 #include <hardware/nfc.h>
23 
24 
25 /*
26  * We want to silence the "unused argument" that gcc and clang give.
27  * Other compilers generating this warning will need to provide their
28  * custom attribute to silence this.
29  */
30 #if defined(__GNUC__) || defined(__clang__)
31 #define UNUSED_ARGUMENT __attribute((unused))
32 #else
33 #define UNUSED_ARGUMENT
34 #endif
35 
36 /*
37  * NCI HAL method implementations. These must be overriden
38  */
hal_open(const struct nfc_nci_device * dev UNUSED_ARGUMENT,nfc_stack_callback_t * p_cback UNUSED_ARGUMENT,nfc_stack_data_callback_t * p_data_cback UNUSED_ARGUMENT)39 static int hal_open(const struct nfc_nci_device *dev UNUSED_ARGUMENT,
40         nfc_stack_callback_t *p_cback UNUSED_ARGUMENT,
41         nfc_stack_data_callback_t *p_data_cback UNUSED_ARGUMENT) {
42     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
43     return 0;
44 }
45 
hal_write(const struct nfc_nci_device * dev UNUSED_ARGUMENT,uint16_t data_len UNUSED_ARGUMENT,const uint8_t * p_data UNUSED_ARGUMENT)46 static int hal_write(const struct nfc_nci_device *dev UNUSED_ARGUMENT,
47         uint16_t data_len UNUSED_ARGUMENT,
48         const uint8_t *p_data UNUSED_ARGUMENT) {
49     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
50     return 0;
51 }
52 
hal_core_initialized(const struct nfc_nci_device * dev UNUSED_ARGUMENT,uint8_t * p_core_init_rsp_params UNUSED_ARGUMENT)53 static int hal_core_initialized(
54         const struct nfc_nci_device *dev UNUSED_ARGUMENT,
55         uint8_t* p_core_init_rsp_params UNUSED_ARGUMENT) {
56     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
57     return 0;
58 }
59 
hal_pre_discover(const struct nfc_nci_device * dev UNUSED_ARGUMENT)60 static int hal_pre_discover(
61         const struct nfc_nci_device *dev UNUSED_ARGUMENT) {
62     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
63     return 0;
64 }
65 
hal_close(const struct nfc_nci_device * dev UNUSED_ARGUMENT)66 static int hal_close(const struct nfc_nci_device *dev UNUSED_ARGUMENT) {
67     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
68     return 0;
69 }
70 
hal_control_granted(const struct nfc_nci_device * p_dev UNUSED_ARGUMENT)71 static int hal_control_granted (
72         const struct nfc_nci_device *p_dev UNUSED_ARGUMENT)
73 {
74     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
75     return 0;
76 }
77 
78 
hal_power_cycle(const struct nfc_nci_device * p_dev UNUSED_ARGUMENT)79 static int hal_power_cycle (
80         const struct nfc_nci_device *p_dev UNUSED_ARGUMENT)
81 {
82     ALOGE("NFC-NCI HAL: %s", __FUNCTION__);
83     return 0;
84 }
85 
86 /*
87  * Generic device handling below - can generally be left unchanged.
88  */
89 /* Close an opened nfc device instance */
nfc_close(hw_device_t * dev)90 static int nfc_close(hw_device_t *dev) {
91     free(dev);
92     return 0;
93 }
94 
nfc_open(const hw_module_t * module,const char * name,hw_device_t ** device)95 static int nfc_open(const hw_module_t* module, const char* name,
96         hw_device_t** device) {
97     if (strcmp(name, NFC_NCI_CONTROLLER) == 0) {
98         nfc_nci_device_t *dev = calloc(1, sizeof(nfc_nci_device_t));
99 
100         dev->common.tag = HARDWARE_DEVICE_TAG;
101         dev->common.version = 0x00010000; // [31:16] major, [15:0] minor
102         dev->common.module = (struct hw_module_t*) module;
103         dev->common.close = nfc_close;
104 
105         // NCI HAL method pointers
106         dev->open = hal_open;
107         dev->write = hal_write;
108         dev->core_initialized = hal_core_initialized;
109         dev->pre_discover = hal_pre_discover;
110         dev->close = hal_close;
111         dev->control_granted = hal_control_granted;
112         dev->power_cycle = hal_power_cycle;
113 
114         *device = (hw_device_t*) dev;
115 
116         return 0;
117     } else {
118         return -EINVAL;
119     }
120 }
121 
122 
123 static struct hw_module_methods_t nfc_module_methods = {
124     .open = nfc_open,
125 };
126 
127 struct nfc_nci_module_t HAL_MODULE_INFO_SYM = {
128     .common = {
129         .tag = HARDWARE_MODULE_TAG,
130         .module_api_version = 0x0100, // [15:8] major, [7:0] minor (1.0)
131         .hal_api_version = 0x00, // 0 is only valid value
132         .id = NFC_NCI_HARDWARE_MODULE_ID,
133         .name = "Default NFC NCI HW HAL",
134         .author = "The Android Open Source Project",
135         .methods = &nfc_module_methods,
136     },
137 };
138