blob: 31410fb6d58c221c6e02c4100e604a682e5da7ac [file] [log] [blame]
Martijn Coenen44ae5b22011-11-02 16:09:37 -07001/*
Martijn Coenen1c970f12012-09-12 17:59:39 -04002 * Copyright (C) 2011, 2012 The Android Open Source Project
Martijn Coenen44ae5b22011-11-02 16:09:37 -07003 *
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
Martijn Coenen44ae5b22011-11-02 16:09:37 -070017#ifndef ANDROID_NFC_HAL_INTERFACE_H
18#define ANDROID_NFC_HAL_INTERFACE_H
19
20#include <stdint.h>
21#include <strings.h>
22#include <sys/cdefs.h>
23#include <sys/types.h>
24
25#include <hardware/hardware.h>
26
27__BEGIN_DECLS
28
Martijn Coenen1c970f12012-09-12 17:59:39 -040029
30/* NFC device HAL for NCI-based NFC controllers.
31 *
32 * This HAL allows NCI silicon vendors to make use
33 * of the core NCI stack in Android for their own silicon.
34 *
35 * The responibilities of the NCI HAL implementation
36 * are as follows:
37 *
38 * - Implement the transport to the NFC controller
39 * - Implement each of the HAL methods specified below as applicable to their silicon
40 * - Pass up received NCI messages from the controller to the stack
41 *
42 * A simplified timeline of NCI HAL method calls:
43 * 1) Core NCI stack calls open()
44 * 2) Core NCI stack executes CORE_RESET and CORE_INIT through calls to write()
45 * 3) Core NCI stack calls core_initialized() to allow HAL to do post-init configuration
46 * 4) Core NCI stack calls pre_discover() to allow HAL to prepare for RF discovery
47 * 5) Core NCI stack starts discovery through calls to write()
48 * 6) Core NCI stack stops discovery through calls to write() (e.g. screen turns off)
49 * 7) Core NCI stack calls pre_discover() to prepare for RF discovery (e.g. screen turned back on)
50 * 8) Core NCI stack starts discovery through calls to write()
51 * ...
52 * ...
53 * 9) Core NCI stack calls close()
54 */
55#define NFC_NCI_HARDWARE_MODULE_ID "nfc_nci"
56#define NFC_NCI_CONTROLLER "nci"
Martijn Coenen44ae5b22011-11-02 16:09:37 -070057
58/*
Martijn Coenen1c970f12012-09-12 17:59:39 -040059 * nfc_nci_module_t should contain module-specific parameters
60 */
61typedef struct nfc_nci_module_t {
62 struct hw_module_t common;
63} nfc_nci_module_t;
64
65/*
66 * HAL events that can be passed back to the stack
67 */
68typedef uint8_t nfc_event_t;
69
70enum {
71 HAL_NFC_OPEN_CPLT_EVT = 0x00,
72 HAL_NFC_CLOSE_CPLT_EVT = 0x01,
73 HAL_NFC_POST_INIT_CPLT_EVT = 0x02,
74 HAL_NFC_NCI_RX_EVT = 0x03,
75 HAL_NFC_PRE_DISCOVER_CPLT_EVT = 0x04,
76 HAL_NFC_REQUEST_CONTROL_EVT = 0x05,
77 HAL_NFC_RELEASE_CONTROL_EVT = 0x06,
78 HAL_NFC_ERROR_EVT = 0x07
79};
80
81/*
82 * Allowed status return values for each of the HAL methods
83 */
84typedef uint8_t nfc_status_t;
85
86enum {
87 HAL_NFC_STATUS_OK = 0x00,
88 HAL_NFC_STATUS_FAILED = 0x01,
89 HAL_NFC_STATUS_ERR_TRANSPORT = 0x02,
90 HAL_NFC_STATUS_ERR_CMD_TIMEOUT = 0x03,
91 HAL_NFC_STATUS_REFUSED = 0x04
92};
93
94/*
95 * nfc_rx_data
96 * Struct used to pass received NCI packets up to the stack
97 */
98typedef struct nfc_rx_data {
99 uint16_t len;
100 uint8_t *p_data;
101} nfc_rx_data_t;
102
103/*
104 */
105typedef union
106{
107 nfc_status_t status;
108 nfc_rx_data_t nci_rx;
109} nfc_event_data_t;
110
111/*
112 * The callback passed in from the NFC stack that the HAL
113 * can use to pass events back to the stack.
114 */
115typedef void (nfc_stack_callback_t) (nfc_event_t event, nfc_event_data_t* p_data);
116
117/* nfc_nci_device_t starts with a hw_device_t struct,
118 * followed by device-specific methods and members.
119 *
120 * All methods in the NCI HAL are asynchronous.
121 */
122typedef struct nfc_nci_device {
123 struct hw_device_t common;
124 /*
125 * (*open)() Opens the NFC controller device and performs initialization.
126 * This may include patch download and other vendor-specific initialization.
127 *
128 * If open completes successfully, the controller should be ready to perform
129 * NCI initialization - ie accept CORE_RESET and subsequent commands through
130 * the write() call.
131 *
132 * If open() returns 0, the NCI stack will wait for a HAL_NFC_OPEN_CPLT_EVT
133 * before continuing.
134 *
135 * If open() returns any other value, the NCI stack will stop.
136 *
137 */
138 int (*open)(const struct nfc_nci_device *p_dev, nfc_stack_callback_t *p_cback);
139
140 /*
141 * (*write)() Performs an NCI write.
142 *
143 * This method may queue writes and return immediately. The only
144 * requirement is that the writes are executed in order.
145 */
146 int (*write)(const struct nfc_nci_device *p_dev, uint16_t data_len, const uint8_t *p_data);
147
148 /*
149 * (*core_initialized)() is called after the CORE_INIT_RSP is received from the NFCC.
150 * At this time, the HAL can do any chip-specific configuration.
151 *
152 * If core_initialized() returns 0, the NCI stack will wait for a HAL_NFC_POST_INIT_CPLT_EVT
153 * before continuing.
154 *
155 * If core_initialized() returns any other value, the NCI stack will continue
156 * immediately.
157 */
158 int (*core_initialized)(const struct nfc_nci_device *p_dev, uint8_t* p_core_init_rsp_params);
159
160 /*
161 * (*pre_discover)() Is called every time before starting RF discovery.
162 * It is a good place to do vendor-specific configuration that must be
163 * performed every time RF discovery is about to be started.
164 *
165 * If pre_discover() returns 0, the NCI stack will wait for a HAL_NFC_PRE_DISCOVER_CPLT_EVT
166 * before continuing.
167 *
168 * If pre_discover() returns any other value, the NCI stack will start
169 * RF discovery immediately.
170 */
171 int (*pre_discover)(const struct nfc_nci_device *p_dev);
172
173 /*
174 * (*close)() Closed the NFC controller. Should free all resources.
175 */
176 int (*close)(const struct nfc_nci_device *p_dev);
177
178 /*
179 * (*control_granted)() Grant HAL the exclusive control to send NCI commands.
180 * Called in response to HAL_REQUEST_CONTROL_EVT.
181 * Must only be called when there are no NCI commands pending.
182 * HAL_RELEASE_CONTROL_EVT will notify when HAL no longer needs exclusive control.
183 */
184 int (*control_granted)(const struct nfc_nci_device *p_dev);
185
186 /*
187 * (*power_cycle)() Restart controller by power cyle;
188 * HAL_OPEN_CPLT_EVT will notify when operation is complete.
189 */
190 int (*power_cycle)(const struct nfc_nci_device *p_dev);
191} nfc_nci_device_t;
192
193/*
194 * Convenience methods that the NFC stack can use to open
195 * and close an NCI device
196 */
197static inline int nfc_nci_open(const struct hw_module_t* module,
198 nfc_nci_device_t** dev) {
199 return module->methods->open(module, NFC_NCI_CONTROLLER,
200 (struct hw_device_t**) dev);
201}
202
203static inline int nfc_nci_close(nfc_nci_device_t* dev) {
204 return dev->common.close(&dev->common);
205}
206/*
207 * End NFC NCI HAL
208 */
209
210/*
211 * This is a limited NFC HAL for NXP PN544-based devices.
212 * This HAL as Android is moving to
213 * an NCI-based NFC stack.
214 *
215 * All NCI-based NFC controllers should use the NFC-NCI
216 * HAL instead.
Martijn Coenen44ae5b22011-11-02 16:09:37 -0700217 * Begin PN544 specific HAL
218 */
Martijn Coenen1c970f12012-09-12 17:59:39 -0400219#define NFC_HARDWARE_MODULE_ID "nfc"
220
Martijn Coenen44ae5b22011-11-02 16:09:37 -0700221#define NFC_PN544_CONTROLLER "pn544"
222
223typedef struct nfc_module_t {
224 struct hw_module_t common;
225} nfc_module_t;
226
227/*
228 * PN544 linktypes.
229 * UART
230 * I2C
231 * USB (uses UART DAL)
232 */
233typedef enum {
234 PN544_LINK_TYPE_UART,
235 PN544_LINK_TYPE_I2C,
236 PN544_LINK_TYPE_USB,
237 PN544_LINK_TYPE_INVALID,
238} nfc_pn544_linktype;
239
240typedef struct {
241 struct hw_device_t common;
242
243 /* The number of EEPROM registers to write */
244 uint32_t num_eeprom_settings;
245
246 /* The actual EEPROM settings
247 * For PN544, each EEPROM setting is a 4-byte entry,
248 * of the format [0x00, addr_msb, addr_lsb, value].
249 */
250 uint8_t* eeprom_settings;
251
252 /* The link type to which the PN544 is connected */
253 nfc_pn544_linktype linktype;
254
255 /* The device node to which the PN544 is connected */
256 const char* device_node;
257
258 /* On Crespo we had an I2C issue that would cause us to sometimes read
259 * the I2C slave address (0x57) over the bus. libnfc contains
260 * a hack to ignore this byte and try to read the length byte
261 * again.
262 * Set to 0 to disable the workaround, 1 to enable it.
263 */
264 uint8_t enable_i2c_workaround;
Rakesh Goyal4cbd62c2012-01-27 18:13:29 +0530265 /* I2C slave address. Multiple I2C addresses are
266 * possible for PN544 module. Configure address according to
267 * board design.
268 */
269 uint8_t i2c_device_address;
Martijn Coenen44ae5b22011-11-02 16:09:37 -0700270} nfc_pn544_device_t;
271
272static inline int nfc_pn544_open(const struct hw_module_t* module,
273 nfc_pn544_device_t** dev) {
274 return module->methods->open(module, NFC_PN544_CONTROLLER,
275 (struct hw_device_t**) dev);
276}
277
278static inline int nfc_pn544_close(nfc_pn544_device_t* dev) {
279 return dev->common.close(&dev->common);
280}
281/*
282 * End PN544 specific HAL
283 */
284
285__END_DECLS
286
287#endif // ANDROID_NFC_HAL_INTERFACE_H