Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 1 | /* |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 2 | * Copyright (C) 2011, 2012 The Android Open Source Project |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 17 | #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 Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 29 | |
| 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 Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 57 | |
| 58 | /* |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 59 | * nfc_nci_module_t should contain module-specific parameters |
| 60 | */ |
| 61 | typedef struct nfc_nci_module_t { |
Stewart Miles | b253bcc | 2014-05-12 15:00:13 -0700 | [diff] [blame^] | 62 | /** |
| 63 | * Common methods of the NFC NCI module. This *must* be the first member of |
| 64 | * nfc_nci_module_t as users of this structure will cast a hw_module_t to |
| 65 | * nfc_nci_module_t pointer in contexts where it's known the hw_module_t references a |
| 66 | * nfc_nci_module_t. |
| 67 | */ |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 68 | struct hw_module_t common; |
| 69 | } nfc_nci_module_t; |
| 70 | |
| 71 | /* |
| 72 | * HAL events that can be passed back to the stack |
| 73 | */ |
| 74 | typedef uint8_t nfc_event_t; |
| 75 | |
| 76 | enum { |
| 77 | HAL_NFC_OPEN_CPLT_EVT = 0x00, |
| 78 | HAL_NFC_CLOSE_CPLT_EVT = 0x01, |
| 79 | HAL_NFC_POST_INIT_CPLT_EVT = 0x02, |
Martijn Coenen | 442752a | 2012-09-30 11:06:22 -0700 | [diff] [blame] | 80 | HAL_NFC_PRE_DISCOVER_CPLT_EVT = 0x03, |
| 81 | HAL_NFC_REQUEST_CONTROL_EVT = 0x04, |
| 82 | HAL_NFC_RELEASE_CONTROL_EVT = 0x05, |
| 83 | HAL_NFC_ERROR_EVT = 0x06 |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Allowed status return values for each of the HAL methods |
| 88 | */ |
| 89 | typedef uint8_t nfc_status_t; |
| 90 | |
| 91 | enum { |
| 92 | HAL_NFC_STATUS_OK = 0x00, |
| 93 | HAL_NFC_STATUS_FAILED = 0x01, |
| 94 | HAL_NFC_STATUS_ERR_TRANSPORT = 0x02, |
| 95 | HAL_NFC_STATUS_ERR_CMD_TIMEOUT = 0x03, |
| 96 | HAL_NFC_STATUS_REFUSED = 0x04 |
| 97 | }; |
| 98 | |
| 99 | /* |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 100 | * The callback passed in from the NFC stack that the HAL |
| 101 | * can use to pass events back to the stack. |
| 102 | */ |
Martijn Coenen | 442752a | 2012-09-30 11:06:22 -0700 | [diff] [blame] | 103 | typedef void (nfc_stack_callback_t) (nfc_event_t event, nfc_status_t event_status); |
| 104 | |
| 105 | /* |
| 106 | * The callback passed in from the NFC stack that the HAL |
| 107 | * can use to pass incomming data to the stack. |
| 108 | */ |
| 109 | typedef void (nfc_stack_data_callback_t) (uint16_t data_len, uint8_t* p_data); |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 110 | |
| 111 | /* nfc_nci_device_t starts with a hw_device_t struct, |
| 112 | * followed by device-specific methods and members. |
| 113 | * |
| 114 | * All methods in the NCI HAL are asynchronous. |
| 115 | */ |
| 116 | typedef struct nfc_nci_device { |
Stewart Miles | b253bcc | 2014-05-12 15:00:13 -0700 | [diff] [blame^] | 117 | /** |
| 118 | * Common methods of the NFC NCI device. This *must* be the first member of |
| 119 | * nfc_nci_device_t as users of this structure will cast a hw_device_t to |
| 120 | * nfc_nci_device_t pointer in contexts where it's known the hw_device_t references a |
| 121 | * nfc_nci_device_t. |
| 122 | */ |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 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 | */ |
Martijn Coenen | 442752a | 2012-09-30 11:06:22 -0700 | [diff] [blame] | 138 | int (*open)(const struct nfc_nci_device *p_dev, nfc_stack_callback_t *p_cback, |
| 139 | nfc_stack_data_callback_t *p_data_cback); |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * (*write)() Performs an NCI write. |
| 143 | * |
| 144 | * This method may queue writes and return immediately. The only |
| 145 | * requirement is that the writes are executed in order. |
| 146 | */ |
| 147 | int (*write)(const struct nfc_nci_device *p_dev, uint16_t data_len, const uint8_t *p_data); |
| 148 | |
| 149 | /* |
| 150 | * (*core_initialized)() is called after the CORE_INIT_RSP is received from the NFCC. |
| 151 | * At this time, the HAL can do any chip-specific configuration. |
| 152 | * |
| 153 | * If core_initialized() returns 0, the NCI stack will wait for a HAL_NFC_POST_INIT_CPLT_EVT |
| 154 | * before continuing. |
| 155 | * |
| 156 | * If core_initialized() returns any other value, the NCI stack will continue |
| 157 | * immediately. |
| 158 | */ |
| 159 | int (*core_initialized)(const struct nfc_nci_device *p_dev, uint8_t* p_core_init_rsp_params); |
| 160 | |
| 161 | /* |
| 162 | * (*pre_discover)() Is called every time before starting RF discovery. |
| 163 | * It is a good place to do vendor-specific configuration that must be |
| 164 | * performed every time RF discovery is about to be started. |
| 165 | * |
| 166 | * If pre_discover() returns 0, the NCI stack will wait for a HAL_NFC_PRE_DISCOVER_CPLT_EVT |
| 167 | * before continuing. |
| 168 | * |
| 169 | * If pre_discover() returns any other value, the NCI stack will start |
| 170 | * RF discovery immediately. |
| 171 | */ |
| 172 | int (*pre_discover)(const struct nfc_nci_device *p_dev); |
| 173 | |
| 174 | /* |
| 175 | * (*close)() Closed the NFC controller. Should free all resources. |
| 176 | */ |
| 177 | int (*close)(const struct nfc_nci_device *p_dev); |
| 178 | |
| 179 | /* |
| 180 | * (*control_granted)() Grant HAL the exclusive control to send NCI commands. |
| 181 | * Called in response to HAL_REQUEST_CONTROL_EVT. |
| 182 | * Must only be called when there are no NCI commands pending. |
| 183 | * HAL_RELEASE_CONTROL_EVT will notify when HAL no longer needs exclusive control. |
| 184 | */ |
| 185 | int (*control_granted)(const struct nfc_nci_device *p_dev); |
| 186 | |
| 187 | /* |
| 188 | * (*power_cycle)() Restart controller by power cyle; |
| 189 | * HAL_OPEN_CPLT_EVT will notify when operation is complete. |
| 190 | */ |
| 191 | int (*power_cycle)(const struct nfc_nci_device *p_dev); |
| 192 | } nfc_nci_device_t; |
| 193 | |
| 194 | /* |
| 195 | * Convenience methods that the NFC stack can use to open |
| 196 | * and close an NCI device |
| 197 | */ |
| 198 | static inline int nfc_nci_open(const struct hw_module_t* module, |
| 199 | nfc_nci_device_t** dev) { |
| 200 | return module->methods->open(module, NFC_NCI_CONTROLLER, |
| 201 | (struct hw_device_t**) dev); |
| 202 | } |
| 203 | |
| 204 | static inline int nfc_nci_close(nfc_nci_device_t* dev) { |
| 205 | return dev->common.close(&dev->common); |
| 206 | } |
| 207 | /* |
| 208 | * End NFC NCI HAL |
| 209 | */ |
| 210 | |
| 211 | /* |
| 212 | * This is a limited NFC HAL for NXP PN544-based devices. |
| 213 | * This HAL as Android is moving to |
| 214 | * an NCI-based NFC stack. |
| 215 | * |
| 216 | * All NCI-based NFC controllers should use the NFC-NCI |
| 217 | * HAL instead. |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 218 | * Begin PN544 specific HAL |
| 219 | */ |
Martijn Coenen | 1c970f1 | 2012-09-12 17:59:39 -0400 | [diff] [blame] | 220 | #define NFC_HARDWARE_MODULE_ID "nfc" |
| 221 | |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 222 | #define NFC_PN544_CONTROLLER "pn544" |
| 223 | |
| 224 | typedef struct nfc_module_t { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Common methods of the NFC NXP PN544 module. This *must* be the first member of |
| 227 | * nfc_module_t as users of this structure will cast a hw_module_t to |
Stewart Miles | b253bcc | 2014-05-12 15:00:13 -0700 | [diff] [blame^] | 228 | * nfc_module_t pointer in contexts where it's known the hw_module_t references a |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 229 | * nfc_module_t. |
| 230 | */ |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 231 | struct hw_module_t common; |
| 232 | } nfc_module_t; |
| 233 | |
| 234 | /* |
| 235 | * PN544 linktypes. |
| 236 | * UART |
| 237 | * I2C |
| 238 | * USB (uses UART DAL) |
| 239 | */ |
| 240 | typedef enum { |
| 241 | PN544_LINK_TYPE_UART, |
| 242 | PN544_LINK_TYPE_I2C, |
| 243 | PN544_LINK_TYPE_USB, |
| 244 | PN544_LINK_TYPE_INVALID, |
| 245 | } nfc_pn544_linktype; |
| 246 | |
| 247 | typedef struct { |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Common methods of the NFC NXP PN544 device. This *must* be the first member of |
| 250 | * nfc_pn544_device_t as users of this structure will cast a hw_device_t to |
Stewart Miles | b253bcc | 2014-05-12 15:00:13 -0700 | [diff] [blame^] | 251 | * nfc_pn544_device_t pointer in contexts where it's known the hw_device_t references a |
Stewart Miles | 84d3549 | 2014-05-01 09:03:27 -0700 | [diff] [blame] | 252 | * nfc_pn544_device_t. |
| 253 | */ |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 254 | struct hw_device_t common; |
| 255 | |
| 256 | /* The number of EEPROM registers to write */ |
| 257 | uint32_t num_eeprom_settings; |
| 258 | |
| 259 | /* The actual EEPROM settings |
| 260 | * For PN544, each EEPROM setting is a 4-byte entry, |
| 261 | * of the format [0x00, addr_msb, addr_lsb, value]. |
| 262 | */ |
| 263 | uint8_t* eeprom_settings; |
| 264 | |
| 265 | /* The link type to which the PN544 is connected */ |
| 266 | nfc_pn544_linktype linktype; |
| 267 | |
| 268 | /* The device node to which the PN544 is connected */ |
| 269 | const char* device_node; |
| 270 | |
| 271 | /* On Crespo we had an I2C issue that would cause us to sometimes read |
| 272 | * the I2C slave address (0x57) over the bus. libnfc contains |
| 273 | * a hack to ignore this byte and try to read the length byte |
| 274 | * again. |
| 275 | * Set to 0 to disable the workaround, 1 to enable it. |
| 276 | */ |
| 277 | uint8_t enable_i2c_workaround; |
Rakesh Goyal | 4cbd62c | 2012-01-27 18:13:29 +0530 | [diff] [blame] | 278 | /* I2C slave address. Multiple I2C addresses are |
| 279 | * possible for PN544 module. Configure address according to |
| 280 | * board design. |
| 281 | */ |
| 282 | uint8_t i2c_device_address; |
Martijn Coenen | 44ae5b2 | 2011-11-02 16:09:37 -0700 | [diff] [blame] | 283 | } nfc_pn544_device_t; |
| 284 | |
| 285 | static inline int nfc_pn544_open(const struct hw_module_t* module, |
| 286 | nfc_pn544_device_t** dev) { |
| 287 | return module->methods->open(module, NFC_PN544_CONTROLLER, |
| 288 | (struct hw_device_t**) dev); |
| 289 | } |
| 290 | |
| 291 | static inline int nfc_pn544_close(nfc_pn544_device_t* dev) { |
| 292 | return dev->common.close(&dev->common); |
| 293 | } |
| 294 | /* |
| 295 | * End PN544 specific HAL |
| 296 | */ |
| 297 | |
| 298 | __END_DECLS |
| 299 | |
| 300 | #endif // ANDROID_NFC_HAL_INTERFACE_H |