Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * NFC Digital Protocol stack |
| 4 | * Copyright (c) 2013, Intel Corporation. |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __NFC_DIGITAL_H |
| 8 | #define __NFC_DIGITAL_H |
| 9 | |
| 10 | #include <linux/skbuff.h> |
| 11 | #include <net/nfc/nfc.h> |
| 12 | |
| 13 | /** |
| 14 | * Configuration types for in_configure_hw and tg_configure_hw. |
| 15 | */ |
| 16 | enum { |
| 17 | NFC_DIGITAL_CONFIG_RF_TECH = 0, |
| 18 | NFC_DIGITAL_CONFIG_FRAMING, |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * RF technology values passed as param argument to in_configure_hw and |
| 23 | * tg_configure_hw for NFC_DIGITAL_CONFIG_RF_TECH configuration type. |
| 24 | */ |
| 25 | enum { |
| 26 | NFC_DIGITAL_RF_TECH_106A = 0, |
| 27 | NFC_DIGITAL_RF_TECH_212F, |
| 28 | NFC_DIGITAL_RF_TECH_424F, |
Mark A. Greer | e487e4d | 2014-01-14 17:52:09 -0700 | [diff] [blame] | 29 | NFC_DIGITAL_RF_TECH_ISO15693, |
Mark A. Greer | 51d98fa | 2014-03-31 17:36:37 -0700 | [diff] [blame] | 30 | NFC_DIGITAL_RF_TECH_106B, |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 31 | |
| 32 | NFC_DIGITAL_RF_TECH_LAST, |
| 33 | }; |
| 34 | |
| 35 | /** |
| 36 | * Framing configuration passed as param argument to in_configure_hw and |
| 37 | * tg_configure_hw for NFC_DIGITAL_CONFIG_FRAMING configuration type. |
| 38 | */ |
| 39 | enum { |
| 40 | NFC_DIGITAL_FRAMING_NFCA_SHORT = 0, |
| 41 | NFC_DIGITAL_FRAMING_NFCA_STANDARD, |
| 42 | NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A, |
Mark A. Greer | 55537c7e | 2014-07-02 10:16:15 -0700 | [diff] [blame] | 43 | NFC_DIGITAL_FRAMING_NFCA_ANTICOL_COMPLETE, |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 44 | |
| 45 | NFC_DIGITAL_FRAMING_NFCA_T1T, |
| 46 | NFC_DIGITAL_FRAMING_NFCA_T2T, |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 47 | NFC_DIGITAL_FRAMING_NFCA_T4T, |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 48 | NFC_DIGITAL_FRAMING_NFCA_NFC_DEP, |
| 49 | |
| 50 | NFC_DIGITAL_FRAMING_NFCF, |
| 51 | NFC_DIGITAL_FRAMING_NFCF_T3T, |
| 52 | NFC_DIGITAL_FRAMING_NFCF_NFC_DEP, |
| 53 | NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED, |
| 54 | |
Mark A. Greer | e487e4d | 2014-01-14 17:52:09 -0700 | [diff] [blame] | 55 | NFC_DIGITAL_FRAMING_ISO15693_INVENTORY, |
Mark A. Greer | ceeee42 | 2014-03-06 07:39:19 -0700 | [diff] [blame] | 56 | NFC_DIGITAL_FRAMING_ISO15693_T5T, |
Mark A. Greer | e487e4d | 2014-01-14 17:52:09 -0700 | [diff] [blame] | 57 | |
Mark A. Greer | 51d98fa | 2014-03-31 17:36:37 -0700 | [diff] [blame] | 58 | NFC_DIGITAL_FRAMING_NFCB, |
| 59 | NFC_DIGITAL_FRAMING_NFCB_T4T, |
| 60 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 61 | NFC_DIGITAL_FRAMING_LAST, |
| 62 | }; |
| 63 | |
| 64 | #define DIGITAL_MDAA_NFCID1_SIZE 3 |
| 65 | |
| 66 | struct digital_tg_mdaa_params { |
| 67 | u16 sens_res; |
| 68 | u8 nfcid1[DIGITAL_MDAA_NFCID1_SIZE]; |
| 69 | u8 sel_res; |
| 70 | |
| 71 | u8 nfcid2[NFC_NFCID2_MAXSIZE]; |
| 72 | u16 sc; |
| 73 | }; |
| 74 | |
| 75 | struct nfc_digital_dev; |
| 76 | |
| 77 | /** |
| 78 | * nfc_digital_cmd_complete_t - Definition of command result callback |
| 79 | * |
| 80 | * @ddev: nfc_digital_device ref |
| 81 | * @arg: user data |
| 82 | * @resp: response data |
| 83 | * |
| 84 | * resp pointer can be an error code and will be checked with IS_ERR() macro. |
| 85 | * The callback is responsible for freeing resp sk_buff. |
| 86 | */ |
| 87 | typedef void (*nfc_digital_cmd_complete_t)(struct nfc_digital_dev *ddev, |
| 88 | void *arg, struct sk_buff *resp); |
| 89 | |
| 90 | /** |
| 91 | * Device side NFC Digital operations |
| 92 | * |
| 93 | * Initiator mode: |
| 94 | * @in_configure_hw: Hardware configuration for RF technology and communication |
| 95 | * framing in initiator mode. This is a synchronous function. |
| 96 | * @in_send_cmd: Initiator mode data exchange using RF technology and framing |
| 97 | * previously set with in_configure_hw. The peer response is returned |
| 98 | * through callback cb. If an io error occurs or the peer didn't reply |
| 99 | * within the specified timeout (ms), the error code is passed back through |
| 100 | * the resp pointer. This is an asynchronous function. |
| 101 | * |
| 102 | * Target mode: Only NFC-DEP protocol is supported in target mode. |
| 103 | * @tg_configure_hw: Hardware configuration for RF technology and communication |
| 104 | * framing in target mode. This is a synchronous function. |
| 105 | * @tg_send_cmd: Target mode data exchange using RF technology and framing |
| 106 | * previously set with tg_configure_hw. The peer next command is returned |
| 107 | * through callback cb. If an io error occurs or the peer didn't reply |
| 108 | * within the specified timeout (ms), the error code is passed back through |
| 109 | * the resp pointer. This is an asynchronous function. |
| 110 | * @tg_listen: Put the device in listen mode waiting for data from the peer |
| 111 | * device. This is an asynchronous function. |
| 112 | * @tg_listen_mdaa: If supported, put the device in automatic listen mode with |
| 113 | * mode detection and automatic anti-collision. In this mode, the device |
| 114 | * automatically detects the RF technology and executes the anti-collision |
| 115 | * detection using the command responses specified in mdaa_params. The |
| 116 | * mdaa_params structure contains SENS_RES, NFCID1, and SEL_RES for 106A RF |
| 117 | * tech. NFCID2 and system code (sc) for 212F and 424F. The driver returns |
| 118 | * the NFC-DEP ATR_REQ command through cb. The digital stack deducts the RF |
| 119 | * tech by analyzing the SoD of the frame containing the ATR_REQ command. |
| 120 | * This is an asynchronous function. |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 121 | * @tg_listen_md: If supported, put the device in automatic listen mode with |
| 122 | * mode detection but without automatic anti-collision. In this mode, the |
| 123 | * device automatically detects the RF technology. What the actual |
| 124 | * RF technology is can be retrieved by calling @tg_get_rf_tech. |
| 125 | * The digital stack will then perform the appropriate anti-collision |
| 126 | * sequence. This is an asynchronous function. |
| 127 | * @tg_get_rf_tech: Required when @tg_listen_md is supported, unused otherwise. |
| 128 | * Return the RF Technology that was detected by the @tg_listen_md call. |
| 129 | * This is a synchronous function. |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 130 | * |
| 131 | * @switch_rf: Turns device radio on or off. The stack does not call explicitly |
| 132 | * switch_rf to turn the radio on. A call to in|tg_configure_hw must turn |
| 133 | * the device radio on. |
| 134 | * @abort_cmd: Discard the last sent command. |
Thierry Escande | 444fb98 | 2014-01-02 11:58:12 +0100 | [diff] [blame] | 135 | * |
| 136 | * Notes: Asynchronous functions have a timeout parameter. It is the driver |
| 137 | * responsibility to call the digital stack back through the |
| 138 | * nfc_digital_cmd_complete_t callback when no RF respsonse has been |
| 139 | * received within the specified time (in milliseconds). In that case the |
| 140 | * driver must set the resp sk_buff to ERR_PTR(-ETIMEDOUT). |
| 141 | * Since the digital stack serializes commands to be sent, it's mandatory |
| 142 | * for the driver to handle the timeout correctly. Otherwise the stack |
| 143 | * would not be able to send new commands, waiting for the reply of the |
| 144 | * current one. |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 145 | */ |
| 146 | struct nfc_digital_ops { |
| 147 | int (*in_configure_hw)(struct nfc_digital_dev *ddev, int type, |
| 148 | int param); |
| 149 | int (*in_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb, |
| 150 | u16 timeout, nfc_digital_cmd_complete_t cb, |
| 151 | void *arg); |
| 152 | |
| 153 | int (*tg_configure_hw)(struct nfc_digital_dev *ddev, int type, |
| 154 | int param); |
| 155 | int (*tg_send_cmd)(struct nfc_digital_dev *ddev, struct sk_buff *skb, |
| 156 | u16 timeout, nfc_digital_cmd_complete_t cb, |
| 157 | void *arg); |
| 158 | int (*tg_listen)(struct nfc_digital_dev *ddev, u16 timeout, |
| 159 | nfc_digital_cmd_complete_t cb, void *arg); |
| 160 | int (*tg_listen_mdaa)(struct nfc_digital_dev *ddev, |
| 161 | struct digital_tg_mdaa_params *mdaa_params, |
| 162 | u16 timeout, nfc_digital_cmd_complete_t cb, |
| 163 | void *arg); |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 164 | int (*tg_listen_md)(struct nfc_digital_dev *ddev, u16 timeout, |
| 165 | nfc_digital_cmd_complete_t cb, void *arg); |
| 166 | int (*tg_get_rf_tech)(struct nfc_digital_dev *ddev, u8 *rf_tech); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 167 | |
| 168 | int (*switch_rf)(struct nfc_digital_dev *ddev, bool on); |
| 169 | void (*abort_cmd)(struct nfc_digital_dev *ddev); |
| 170 | }; |
| 171 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 172 | #define NFC_DIGITAL_POLL_MODE_COUNT_MAX 6 /* 106A, 212F, and 424F in & tg */ |
| 173 | |
| 174 | typedef int (*digital_poll_t)(struct nfc_digital_dev *ddev, u8 rf_tech); |
| 175 | |
| 176 | struct digital_poll_tech { |
| 177 | u8 rf_tech; |
| 178 | digital_poll_t poll_func; |
| 179 | }; |
| 180 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 181 | /** |
| 182 | * Driver capabilities - bit mask made of the following values |
| 183 | * |
| 184 | * @NFC_DIGITAL_DRV_CAPS_IN_CRC: The driver handles CRC calculation in initiator |
| 185 | * mode. |
| 186 | * @NFC_DIGITAL_DRV_CAPS_TG_CRC: The driver handles CRC calculation in target |
| 187 | * mode. |
| 188 | */ |
| 189 | #define NFC_DIGITAL_DRV_CAPS_IN_CRC 0x0001 |
| 190 | #define NFC_DIGITAL_DRV_CAPS_TG_CRC 0x0002 |
| 191 | |
| 192 | struct nfc_digital_dev { |
| 193 | struct nfc_dev *nfc_dev; |
Krzysztof Kozlowski | 7186aac | 2021-07-24 23:49:28 +0200 | [diff] [blame] | 194 | const struct nfc_digital_ops *ops; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 195 | |
| 196 | u32 protocols; |
| 197 | |
| 198 | int tx_headroom; |
| 199 | int tx_tailroom; |
| 200 | |
| 201 | u32 driver_capabilities; |
| 202 | void *driver_data; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 203 | |
| 204 | struct digital_poll_tech poll_techs[NFC_DIGITAL_POLL_MODE_COUNT_MAX]; |
| 205 | u8 poll_tech_count; |
| 206 | u8 poll_tech_index; |
| 207 | struct mutex poll_lock; |
| 208 | |
| 209 | struct work_struct cmd_work; |
| 210 | struct work_struct cmd_complete_work; |
| 211 | struct list_head cmd_queue; |
| 212 | struct mutex cmd_lock; |
| 213 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 214 | struct delayed_work poll_work; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 215 | |
| 216 | u8 curr_protocol; |
| 217 | u8 curr_rf_tech; |
| 218 | u8 curr_nfc_dep_pni; |
Mark A. Greer | 05afedc | 2014-09-23 16:38:05 -0700 | [diff] [blame] | 219 | u8 did; |
Thierry Escande | 1a09c56 | 2016-07-08 15:52:45 +0200 | [diff] [blame] | 220 | u16 dep_rwt; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 221 | |
Mark A. Greer | b08147c | 2014-09-23 16:38:08 -0700 | [diff] [blame] | 222 | u8 local_payload_max; |
| 223 | u8 remote_payload_max; |
| 224 | |
Mark A. Greer | 3bd2a5b | 2014-09-23 16:38:09 -0700 | [diff] [blame] | 225 | struct sk_buff *chaining_skb; |
| 226 | struct digital_data_exch *data_exch; |
| 227 | |
Mark A. Greer | 384ab1d | 2014-09-23 16:38:13 -0700 | [diff] [blame] | 228 | int atn_count; |
Mark A. Greer | a80509c | 2014-09-23 16:38:11 -0700 | [diff] [blame] | 229 | int nack_count; |
| 230 | |
Mark A. Greer | 49dbb14 | 2014-09-23 16:38:12 -0700 | [diff] [blame] | 231 | struct sk_buff *saved_skb; |
Mark A. Greer | 49dbb14 | 2014-09-23 16:38:12 -0700 | [diff] [blame] | 232 | |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 233 | u16 target_fsc; |
| 234 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 235 | int (*skb_check_crc)(struct sk_buff *skb); |
| 236 | void (*skb_add_crc)(struct sk_buff *skb); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 237 | }; |
| 238 | |
Krzysztof Kozlowski | 7186aac | 2021-07-24 23:49:28 +0200 | [diff] [blame] | 239 | struct nfc_digital_dev *nfc_digital_allocate_device(const struct nfc_digital_ops *ops, |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 240 | __u32 supported_protocols, |
| 241 | __u32 driver_capabilities, |
| 242 | int tx_headroom, |
| 243 | int tx_tailroom); |
| 244 | void nfc_digital_free_device(struct nfc_digital_dev *ndev); |
| 245 | int nfc_digital_register_device(struct nfc_digital_dev *ndev); |
| 246 | void nfc_digital_unregister_device(struct nfc_digital_dev *ndev); |
| 247 | |
| 248 | static inline void nfc_digital_set_parent_dev(struct nfc_digital_dev *ndev, |
| 249 | struct device *dev) |
| 250 | { |
| 251 | nfc_set_parent_dev(ndev->nfc_dev, dev); |
| 252 | } |
| 253 | |
| 254 | static inline void nfc_digital_set_drvdata(struct nfc_digital_dev *dev, |
| 255 | void *data) |
| 256 | { |
| 257 | dev->driver_data = data; |
| 258 | } |
| 259 | |
| 260 | static inline void *nfc_digital_get_drvdata(struct nfc_digital_dev *dev) |
| 261 | { |
| 262 | return dev->driver_data; |
| 263 | } |
| 264 | |
| 265 | #endif /* __NFC_DIGITAL_H */ |