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 | |
Samuel Ortiz | c5da0e4 | 2013-09-20 09:05:48 +0200 | [diff] [blame] | 7 | #define pr_fmt(fmt) "digital: %s: " fmt, __func__ |
| 8 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | |
| 11 | #include "digital.h" |
| 12 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 13 | #define DIGITAL_PROTO_NFCA_RF_TECH \ |
Shikha Singh | ce2e56c | 2015-11-20 06:40:19 -0500 | [diff] [blame] | 14 | (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | \ |
| 15 | NFC_PROTO_NFC_DEP_MASK | NFC_PROTO_ISO14443_MASK) |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 16 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 17 | #define DIGITAL_PROTO_NFCB_RF_TECH NFC_PROTO_ISO14443_B_MASK |
| 18 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 19 | #define DIGITAL_PROTO_NFCF_RF_TECH \ |
| 20 | (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK) |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 21 | |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 22 | #define DIGITAL_PROTO_ISO15693_RF_TECH NFC_PROTO_ISO15693_MASK |
| 23 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 24 | /* Delay between each poll frame (ms) */ |
| 25 | #define DIGITAL_POLL_INTERVAL 10 |
| 26 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 27 | struct digital_cmd { |
| 28 | struct list_head queue; |
| 29 | |
| 30 | u8 type; |
| 31 | u8 pending; |
| 32 | |
| 33 | u16 timeout; |
| 34 | struct sk_buff *req; |
| 35 | struct sk_buff *resp; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 36 | struct digital_tg_mdaa_params *mdaa_params; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 37 | |
| 38 | nfc_digital_cmd_complete_t cmd_cb; |
| 39 | void *cb_context; |
| 40 | }; |
| 41 | |
| 42 | struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev, |
| 43 | unsigned int len) |
| 44 | { |
| 45 | struct sk_buff *skb; |
| 46 | |
| 47 | skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom, |
| 48 | GFP_KERNEL); |
| 49 | if (skb) |
| 50 | skb_reserve(skb, ddev->tx_headroom); |
| 51 | |
| 52 | return skb; |
| 53 | } |
| 54 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 55 | void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init, |
| 56 | u8 bitwise_inv, u8 msb_first) |
| 57 | { |
| 58 | u16 crc; |
| 59 | |
| 60 | crc = crc_func(init, skb->data, skb->len); |
| 61 | |
| 62 | if (bitwise_inv) |
| 63 | crc = ~crc; |
| 64 | |
| 65 | if (msb_first) |
| 66 | crc = __fswab16(crc); |
| 67 | |
Johannes Berg | 634fef6 | 2017-06-16 14:29:24 +0200 | [diff] [blame] | 68 | skb_put_u8(skb, crc & 0xFF); |
| 69 | skb_put_u8(skb, (crc >> 8) & 0xFF); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func, |
| 73 | u16 crc_init, u8 bitwise_inv, u8 msb_first) |
| 74 | { |
| 75 | int rc; |
| 76 | u16 crc; |
| 77 | |
| 78 | if (skb->len <= 2) |
| 79 | return -EIO; |
| 80 | |
| 81 | crc = crc_func(crc_init, skb->data, skb->len - 2); |
| 82 | |
| 83 | if (bitwise_inv) |
| 84 | crc = ~crc; |
| 85 | |
| 86 | if (msb_first) |
| 87 | crc = __swab16(crc); |
| 88 | |
| 89 | rc = (skb->data[skb->len - 2] - (crc & 0xFF)) + |
| 90 | (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF)); |
| 91 | |
| 92 | if (rc) |
| 93 | return -EIO; |
| 94 | |
| 95 | skb_trim(skb, skb->len - 2); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 100 | static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on) |
| 101 | { |
| 102 | ddev->ops->switch_rf(ddev, on); |
| 103 | } |
| 104 | |
| 105 | static inline void digital_abort_cmd(struct nfc_digital_dev *ddev) |
| 106 | { |
| 107 | ddev->ops->abort_cmd(ddev); |
| 108 | } |
| 109 | |
| 110 | static void digital_wq_cmd_complete(struct work_struct *work) |
| 111 | { |
| 112 | struct digital_cmd *cmd; |
| 113 | struct nfc_digital_dev *ddev = container_of(work, |
| 114 | struct nfc_digital_dev, |
| 115 | cmd_complete_work); |
| 116 | |
| 117 | mutex_lock(&ddev->cmd_lock); |
| 118 | |
| 119 | cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd, |
| 120 | queue); |
| 121 | if (!cmd) { |
| 122 | mutex_unlock(&ddev->cmd_lock); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | list_del(&cmd->queue); |
| 127 | |
| 128 | mutex_unlock(&ddev->cmd_lock); |
| 129 | |
| 130 | if (!IS_ERR(cmd->resp)) |
| 131 | print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1, |
| 132 | cmd->resp->data, cmd->resp->len, false); |
| 133 | |
| 134 | cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp); |
| 135 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 136 | kfree(cmd->mdaa_params); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 137 | kfree(cmd); |
| 138 | |
| 139 | schedule_work(&ddev->cmd_work); |
| 140 | } |
| 141 | |
| 142 | static void digital_send_cmd_complete(struct nfc_digital_dev *ddev, |
| 143 | void *arg, struct sk_buff *resp) |
| 144 | { |
| 145 | struct digital_cmd *cmd = arg; |
| 146 | |
| 147 | cmd->resp = resp; |
| 148 | |
| 149 | schedule_work(&ddev->cmd_complete_work); |
| 150 | } |
| 151 | |
| 152 | static void digital_wq_cmd(struct work_struct *work) |
| 153 | { |
| 154 | int rc; |
| 155 | struct digital_cmd *cmd; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 156 | struct digital_tg_mdaa_params *params; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 157 | struct nfc_digital_dev *ddev = container_of(work, |
| 158 | struct nfc_digital_dev, |
| 159 | cmd_work); |
| 160 | |
| 161 | mutex_lock(&ddev->cmd_lock); |
| 162 | |
| 163 | cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd, |
| 164 | queue); |
| 165 | if (!cmd || cmd->pending) { |
| 166 | mutex_unlock(&ddev->cmd_lock); |
| 167 | return; |
| 168 | } |
| 169 | |
Thierry Escande | af66df0 | 2016-06-16 20:24:44 +0200 | [diff] [blame] | 170 | cmd->pending = 1; |
| 171 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 172 | mutex_unlock(&ddev->cmd_lock); |
| 173 | |
| 174 | if (cmd->req) |
| 175 | print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 176 | cmd->req->data, cmd->req->len, false); |
| 177 | |
| 178 | switch (cmd->type) { |
| 179 | case DIGITAL_CMD_IN_SEND: |
| 180 | rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout, |
| 181 | digital_send_cmd_complete, cmd); |
| 182 | break; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 183 | |
| 184 | case DIGITAL_CMD_TG_SEND: |
| 185 | rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout, |
| 186 | digital_send_cmd_complete, cmd); |
| 187 | break; |
| 188 | |
| 189 | case DIGITAL_CMD_TG_LISTEN: |
| 190 | rc = ddev->ops->tg_listen(ddev, cmd->timeout, |
| 191 | digital_send_cmd_complete, cmd); |
| 192 | break; |
| 193 | |
| 194 | case DIGITAL_CMD_TG_LISTEN_MDAA: |
| 195 | params = cmd->mdaa_params; |
| 196 | |
| 197 | rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout, |
| 198 | digital_send_cmd_complete, cmd); |
| 199 | break; |
| 200 | |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 201 | case DIGITAL_CMD_TG_LISTEN_MD: |
| 202 | rc = ddev->ops->tg_listen_md(ddev, cmd->timeout, |
| 203 | digital_send_cmd_complete, cmd); |
| 204 | break; |
| 205 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 206 | default: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 207 | pr_err("Unknown cmd type %d\n", cmd->type); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (!rc) |
| 212 | return; |
| 213 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 214 | pr_err("in_send_command returned err %d\n", rc); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 215 | |
| 216 | mutex_lock(&ddev->cmd_lock); |
| 217 | list_del(&cmd->queue); |
| 218 | mutex_unlock(&ddev->cmd_lock); |
| 219 | |
| 220 | kfree_skb(cmd->req); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 221 | kfree(cmd->mdaa_params); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 222 | kfree(cmd); |
| 223 | |
| 224 | schedule_work(&ddev->cmd_work); |
| 225 | } |
| 226 | |
| 227 | int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type, |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 228 | struct sk_buff *skb, struct digital_tg_mdaa_params *params, |
| 229 | u16 timeout, nfc_digital_cmd_complete_t cmd_cb, |
| 230 | void *cb_context) |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 231 | { |
| 232 | struct digital_cmd *cmd; |
| 233 | |
Markus Elfring | ae72c99 | 2017-05-22 14:11:01 +0200 | [diff] [blame] | 234 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 235 | if (!cmd) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | cmd->type = cmd_type; |
| 239 | cmd->timeout = timeout; |
| 240 | cmd->req = skb; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 241 | cmd->mdaa_params = params; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 242 | cmd->cmd_cb = cmd_cb; |
| 243 | cmd->cb_context = cb_context; |
| 244 | INIT_LIST_HEAD(&cmd->queue); |
| 245 | |
| 246 | mutex_lock(&ddev->cmd_lock); |
| 247 | list_add_tail(&cmd->queue, &ddev->cmd_queue); |
| 248 | mutex_unlock(&ddev->cmd_lock); |
| 249 | |
| 250 | schedule_work(&ddev->cmd_work); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param) |
| 256 | { |
| 257 | int rc; |
| 258 | |
| 259 | rc = ddev->ops->in_configure_hw(ddev, type, param); |
| 260 | if (rc) |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 261 | pr_err("in_configure_hw failed: %d\n", rc); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 262 | |
| 263 | return rc; |
| 264 | } |
| 265 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 266 | int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param) |
| 267 | { |
| 268 | int rc; |
| 269 | |
| 270 | rc = ddev->ops->tg_configure_hw(ddev, type, param); |
| 271 | if (rc) |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 272 | pr_err("tg_configure_hw failed: %d\n", rc); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 273 | |
| 274 | return rc; |
| 275 | } |
| 276 | |
| 277 | static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 278 | { |
| 279 | struct digital_tg_mdaa_params *params; |
Ziyang Xuan | 7ab488d | 2021-10-13 15:50:12 +0800 | [diff] [blame] | 280 | int rc; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 281 | |
Markus Elfring | ae72c99 | 2017-05-22 14:11:01 +0200 | [diff] [blame] | 282 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 283 | if (!params) |
| 284 | return -ENOMEM; |
| 285 | |
| 286 | params->sens_res = DIGITAL_SENS_RES_NFC_DEP; |
| 287 | get_random_bytes(params->nfcid1, sizeof(params->nfcid1)); |
| 288 | params->sel_res = DIGITAL_SEL_RES_NFC_DEP; |
| 289 | |
| 290 | params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; |
| 291 | params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; |
| 292 | get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2); |
| 293 | params->sc = DIGITAL_SENSF_FELICA_SC; |
| 294 | |
Ziyang Xuan | 7ab488d | 2021-10-13 15:50:12 +0800 | [diff] [blame] | 295 | rc = digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params, |
| 296 | 500, digital_tg_recv_atr_req, NULL); |
| 297 | if (rc) |
| 298 | kfree(params); |
| 299 | |
| 300 | return rc; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 301 | } |
| 302 | |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 303 | static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 304 | { |
| 305 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500, |
| 306 | digital_tg_recv_md_req, NULL); |
| 307 | } |
| 308 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 309 | int digital_target_found(struct nfc_digital_dev *ddev, |
| 310 | struct nfc_target *target, u8 protocol) |
| 311 | { |
| 312 | int rc; |
| 313 | u8 framing; |
| 314 | u8 rf_tech; |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 315 | u8 poll_tech_count; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 316 | int (*check_crc)(struct sk_buff *skb); |
| 317 | void (*add_crc)(struct sk_buff *skb); |
| 318 | |
| 319 | rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech; |
| 320 | |
| 321 | switch (protocol) { |
| 322 | case NFC_PROTO_JEWEL: |
| 323 | framing = NFC_DIGITAL_FRAMING_NFCA_T1T; |
| 324 | check_crc = digital_skb_check_crc_b; |
| 325 | add_crc = digital_skb_add_crc_b; |
| 326 | break; |
| 327 | |
| 328 | case NFC_PROTO_MIFARE: |
| 329 | framing = NFC_DIGITAL_FRAMING_NFCA_T2T; |
| 330 | check_crc = digital_skb_check_crc_a; |
| 331 | add_crc = digital_skb_add_crc_a; |
| 332 | break; |
| 333 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 334 | case NFC_PROTO_FELICA: |
| 335 | framing = NFC_DIGITAL_FRAMING_NFCF_T3T; |
| 336 | check_crc = digital_skb_check_crc_f; |
| 337 | add_crc = digital_skb_add_crc_f; |
| 338 | break; |
| 339 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 340 | case NFC_PROTO_NFC_DEP: |
| 341 | if (rf_tech == NFC_DIGITAL_RF_TECH_106A) { |
| 342 | framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP; |
| 343 | check_crc = digital_skb_check_crc_a; |
| 344 | add_crc = digital_skb_add_crc_a; |
| 345 | } else { |
| 346 | framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP; |
| 347 | check_crc = digital_skb_check_crc_f; |
| 348 | add_crc = digital_skb_add_crc_f; |
| 349 | } |
| 350 | break; |
| 351 | |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 352 | case NFC_PROTO_ISO15693: |
Mark A. Greer | ceeee42 | 2014-03-06 07:39:19 -0700 | [diff] [blame] | 353 | framing = NFC_DIGITAL_FRAMING_ISO15693_T5T; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 354 | check_crc = digital_skb_check_crc_b; |
| 355 | add_crc = digital_skb_add_crc_b; |
Thierry Escande | 564af14 | 2014-02-12 12:13:23 +0100 | [diff] [blame] | 356 | break; |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 357 | |
| 358 | case NFC_PROTO_ISO14443: |
| 359 | framing = NFC_DIGITAL_FRAMING_NFCA_T4T; |
| 360 | check_crc = digital_skb_check_crc_a; |
| 361 | add_crc = digital_skb_add_crc_a; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 362 | break; |
| 363 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 364 | case NFC_PROTO_ISO14443_B: |
| 365 | framing = NFC_DIGITAL_FRAMING_NFCB_T4T; |
| 366 | check_crc = digital_skb_check_crc_b; |
| 367 | add_crc = digital_skb_add_crc_b; |
| 368 | break; |
| 369 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 370 | default: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 371 | pr_err("Invalid protocol %d\n", protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 372 | return -EINVAL; |
| 373 | } |
| 374 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 375 | pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 376 | |
| 377 | ddev->curr_rf_tech = rf_tech; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 378 | |
| 379 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { |
| 380 | ddev->skb_add_crc = digital_skb_add_crc_none; |
| 381 | ddev->skb_check_crc = digital_skb_check_crc_none; |
| 382 | } else { |
| 383 | ddev->skb_add_crc = add_crc; |
| 384 | ddev->skb_check_crc = check_crc; |
| 385 | } |
| 386 | |
| 387 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing); |
| 388 | if (rc) |
| 389 | return rc; |
| 390 | |
| 391 | target->supported_protocols = (1 << protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 392 | |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 393 | poll_tech_count = ddev->poll_tech_count; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 394 | ddev->poll_tech_count = 0; |
| 395 | |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 396 | rc = nfc_targets_found(ddev->nfc_dev, target, 1); |
| 397 | if (rc) { |
| 398 | ddev->poll_tech_count = poll_tech_count; |
| 399 | return rc; |
| 400 | } |
| 401 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 402 | return 0; |
| 403 | } |
| 404 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 405 | void digital_poll_next_tech(struct nfc_digital_dev *ddev) |
| 406 | { |
Thierry Escande | 9dc3370 | 2014-05-10 13:06:02 +0200 | [diff] [blame] | 407 | u8 rand_mod; |
| 408 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 409 | digital_switch_rf(ddev, 0); |
| 410 | |
| 411 | mutex_lock(&ddev->poll_lock); |
| 412 | |
| 413 | if (!ddev->poll_tech_count) { |
| 414 | mutex_unlock(&ddev->poll_lock); |
| 415 | return; |
| 416 | } |
| 417 | |
Thierry Escande | 9dc3370 | 2014-05-10 13:06:02 +0200 | [diff] [blame] | 418 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 419 | ddev->poll_tech_index = rand_mod % ddev->poll_tech_count; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 420 | |
| 421 | mutex_unlock(&ddev->poll_lock); |
| 422 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 423 | schedule_delayed_work(&ddev->poll_work, |
| 424 | msecs_to_jiffies(DIGITAL_POLL_INTERVAL)); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void digital_wq_poll(struct work_struct *work) |
| 428 | { |
| 429 | int rc; |
| 430 | struct digital_poll_tech *poll_tech; |
| 431 | struct nfc_digital_dev *ddev = container_of(work, |
| 432 | struct nfc_digital_dev, |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 433 | poll_work.work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 434 | mutex_lock(&ddev->poll_lock); |
| 435 | |
| 436 | if (!ddev->poll_tech_count) { |
| 437 | mutex_unlock(&ddev->poll_lock); |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | poll_tech = &ddev->poll_techs[ddev->poll_tech_index]; |
| 442 | |
| 443 | mutex_unlock(&ddev->poll_lock); |
| 444 | |
| 445 | rc = poll_tech->poll_func(ddev, poll_tech->rf_tech); |
| 446 | if (rc) |
| 447 | digital_poll_next_tech(ddev); |
| 448 | } |
| 449 | |
| 450 | static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech, |
| 451 | digital_poll_t poll_func) |
| 452 | { |
| 453 | struct digital_poll_tech *poll_tech; |
| 454 | |
| 455 | if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX) |
| 456 | return; |
| 457 | |
| 458 | poll_tech = &ddev->poll_techs[ddev->poll_tech_count++]; |
| 459 | |
| 460 | poll_tech->rf_tech = rf_tech; |
| 461 | poll_tech->poll_func = poll_func; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * start_poll operation |
| 466 | * |
| 467 | * For every supported protocol, the corresponding polling function is added |
| 468 | * to the table of polling technologies (ddev->poll_techs[]) using |
| 469 | * digital_add_poll_tech(). |
| 470 | * When a polling function fails (by timeout or protocol error) the next one is |
| 471 | * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work). |
| 472 | */ |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 473 | static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, |
| 474 | __u32 tm_protocols) |
| 475 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 476 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 477 | u32 matching_im_protocols, matching_tm_protocols; |
| 478 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 479 | pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols, |
Samuel Ortiz | c5da0e4 | 2013-09-20 09:05:48 +0200 | [diff] [blame] | 480 | tm_protocols, ddev->protocols); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 481 | |
| 482 | matching_im_protocols = ddev->protocols & im_protocols; |
| 483 | matching_tm_protocols = ddev->protocols & tm_protocols; |
| 484 | |
| 485 | if (!matching_im_protocols && !matching_tm_protocols) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 486 | pr_err("Unknown protocol\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 487 | return -EINVAL; |
| 488 | } |
| 489 | |
| 490 | if (ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 491 | pr_err("Already polling\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 492 | return -EBUSY; |
| 493 | } |
| 494 | |
| 495 | if (ddev->curr_protocol) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 496 | pr_err("A target is already active\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 497 | return -EBUSY; |
| 498 | } |
| 499 | |
| 500 | ddev->poll_tech_count = 0; |
| 501 | ddev->poll_tech_index = 0; |
| 502 | |
| 503 | if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH) |
| 504 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, |
| 505 | digital_in_send_sens_req); |
| 506 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 507 | if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH) |
| 508 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B, |
| 509 | digital_in_send_sensb_req); |
| 510 | |
Axel Lin | 4f913d4 | 2014-02-22 10:16:11 +0800 | [diff] [blame] | 511 | if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 512 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, |
| 513 | digital_in_send_sensf_req); |
| 514 | |
| 515 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, |
| 516 | digital_in_send_sensf_req); |
| 517 | } |
| 518 | |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 519 | if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH) |
| 520 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693, |
| 521 | digital_in_send_iso15693_inv_req); |
| 522 | |
Axel Lin | 4f913d4 | 2014-02-22 10:16:11 +0800 | [diff] [blame] | 523 | if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 524 | if (ddev->ops->tg_listen_mdaa) { |
| 525 | digital_add_poll_tech(ddev, 0, |
| 526 | digital_tg_listen_mdaa); |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 527 | } else if (ddev->ops->tg_listen_md) { |
| 528 | digital_add_poll_tech(ddev, 0, |
| 529 | digital_tg_listen_md); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 530 | } else { |
| 531 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, |
| 532 | digital_tg_listen_nfca); |
| 533 | |
| 534 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, |
| 535 | digital_tg_listen_nfcf); |
| 536 | |
| 537 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, |
| 538 | digital_tg_listen_nfcf); |
| 539 | } |
| 540 | } |
| 541 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 542 | if (!ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 543 | pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n", |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 544 | matching_im_protocols, matching_tm_protocols); |
| 545 | return -EINVAL; |
| 546 | } |
| 547 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 548 | schedule_delayed_work(&ddev->poll_work, 0); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 549 | |
| 550 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | static void digital_stop_poll(struct nfc_dev *nfc_dev) |
| 554 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 555 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 556 | |
| 557 | mutex_lock(&ddev->poll_lock); |
| 558 | |
| 559 | if (!ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 560 | pr_err("Polling operation was not running\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 561 | mutex_unlock(&ddev->poll_lock); |
| 562 | return; |
| 563 | } |
| 564 | |
| 565 | ddev->poll_tech_count = 0; |
| 566 | |
| 567 | mutex_unlock(&ddev->poll_lock); |
| 568 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 569 | cancel_delayed_work_sync(&ddev->poll_work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 570 | |
| 571 | digital_abort_cmd(ddev); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static int digital_dev_up(struct nfc_dev *nfc_dev) |
| 575 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 576 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 577 | |
| 578 | digital_switch_rf(ddev, 1); |
| 579 | |
| 580 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | static int digital_dev_down(struct nfc_dev *nfc_dev) |
| 584 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 585 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 586 | |
| 587 | digital_switch_rf(ddev, 0); |
| 588 | |
| 589 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static int digital_dep_link_up(struct nfc_dev *nfc_dev, |
| 593 | struct nfc_target *target, |
| 594 | __u8 comm_mode, __u8 *gb, size_t gb_len) |
| 595 | { |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 596 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 597 | int rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 598 | |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 599 | rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len); |
| 600 | |
| 601 | if (!rc) |
| 602 | ddev->curr_protocol = NFC_PROTO_NFC_DEP; |
| 603 | |
| 604 | return rc; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int digital_dep_link_down(struct nfc_dev *nfc_dev) |
| 608 | { |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 609 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 610 | |
Thierry Escande | 3cc952d | 2016-06-16 20:24:45 +0200 | [diff] [blame] | 611 | digital_abort_cmd(ddev); |
| 612 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 613 | ddev->curr_protocol = 0; |
| 614 | |
| 615 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int digital_activate_target(struct nfc_dev *nfc_dev, |
| 619 | struct nfc_target *target, __u32 protocol) |
| 620 | { |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 621 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 622 | |
| 623 | if (ddev->poll_tech_count) { |
| 624 | pr_err("Can't activate a target while polling\n"); |
| 625 | return -EBUSY; |
| 626 | } |
| 627 | |
| 628 | if (ddev->curr_protocol) { |
| 629 | pr_err("A target is already active\n"); |
| 630 | return -EBUSY; |
| 631 | } |
| 632 | |
| 633 | ddev->curr_protocol = protocol; |
| 634 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 635 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void digital_deactivate_target(struct nfc_dev *nfc_dev, |
Christophe Ricard | 96d4581 | 2015-10-25 22:54:43 +0100 | [diff] [blame] | 639 | struct nfc_target *target, |
| 640 | u8 mode) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 641 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 642 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 643 | |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 644 | if (!ddev->curr_protocol) { |
| 645 | pr_err("No active target\n"); |
| 646 | return; |
| 647 | } |
| 648 | |
Mark Greer | 72ad533 | 2017-06-15 20:34:21 -0700 | [diff] [blame] | 649 | digital_abort_cmd(ddev); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 650 | ddev->curr_protocol = 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb) |
| 654 | { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 655 | struct nfc_digital_dev *ddev = nfc_get_drvdata(dev); |
| 656 | |
| 657 | return digital_tg_send_dep_res(ddev, skb); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 660 | static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg, |
| 661 | struct sk_buff *resp) |
| 662 | { |
| 663 | struct digital_data_exch *data_exch = arg; |
| 664 | int rc; |
| 665 | |
| 666 | if (IS_ERR(resp)) { |
| 667 | rc = PTR_ERR(resp); |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 668 | resp = NULL; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 669 | goto done; |
| 670 | } |
| 671 | |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 672 | if (ddev->curr_protocol == NFC_PROTO_MIFARE) { |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 673 | rc = digital_in_recv_mifare_res(resp); |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 674 | /* crc check is done in digital_in_recv_mifare_res() */ |
| 675 | goto done; |
| 676 | } |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 677 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 678 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
| 679 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 680 | rc = digital_in_iso_dep_pull_sod(ddev, resp); |
| 681 | if (rc) |
| 682 | goto done; |
| 683 | } |
| 684 | |
| 685 | rc = ddev->skb_check_crc(resp); |
| 686 | |
| 687 | done: |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 688 | if (rc) { |
| 689 | kfree_skb(resp); |
| 690 | resp = NULL; |
| 691 | } |
| 692 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 693 | data_exch->cb(data_exch->cb_context, resp, rc); |
| 694 | |
| 695 | kfree(data_exch); |
| 696 | } |
| 697 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 698 | static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 699 | struct sk_buff *skb, data_exchange_cb_t cb, |
| 700 | void *cb_context) |
| 701 | { |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 702 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 703 | struct digital_data_exch *data_exch; |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 704 | int rc; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 705 | |
Markus Elfring | ae72c99 | 2017-05-22 14:11:01 +0200 | [diff] [blame] | 706 | data_exch = kzalloc(sizeof(*data_exch), GFP_KERNEL); |
Markus Elfring | dcfca27 | 2017-05-22 14:24:24 +0200 | [diff] [blame] | 707 | if (!data_exch) |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 708 | return -ENOMEM; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 709 | |
| 710 | data_exch->cb = cb; |
| 711 | data_exch->cb_context = cb_context; |
| 712 | |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 713 | if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) { |
| 714 | rc = digital_in_send_dep_req(ddev, target, skb, data_exch); |
| 715 | goto exit; |
| 716 | } |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 717 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 718 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
| 719 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 720 | rc = digital_in_iso_dep_push_sod(ddev, skb); |
| 721 | if (rc) |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 722 | goto exit; |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 725 | ddev->skb_add_crc(skb); |
| 726 | |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 727 | rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete, |
| 728 | data_exch); |
| 729 | |
| 730 | exit: |
| 731 | if (rc) |
| 732 | kfree(data_exch); |
| 733 | |
| 734 | return rc; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | static struct nfc_ops digital_nfc_ops = { |
| 738 | .dev_up = digital_dev_up, |
| 739 | .dev_down = digital_dev_down, |
| 740 | .start_poll = digital_start_poll, |
| 741 | .stop_poll = digital_stop_poll, |
| 742 | .dep_link_up = digital_dep_link_up, |
| 743 | .dep_link_down = digital_dep_link_down, |
| 744 | .activate_target = digital_activate_target, |
| 745 | .deactivate_target = digital_deactivate_target, |
| 746 | .tm_send = digital_tg_send, |
| 747 | .im_transceive = digital_in_send, |
| 748 | }; |
| 749 | |
| 750 | struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops, |
| 751 | __u32 supported_protocols, |
| 752 | __u32 driver_capabilities, |
| 753 | int tx_headroom, int tx_tailroom) |
| 754 | { |
| 755 | struct nfc_digital_dev *ddev; |
| 756 | |
| 757 | if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen || |
| 758 | !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd || |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 759 | !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech)) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 760 | return NULL; |
| 761 | |
Markus Elfring | ae72c99 | 2017-05-22 14:11:01 +0200 | [diff] [blame] | 762 | ddev = kzalloc(sizeof(*ddev), GFP_KERNEL); |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 763 | if (!ddev) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 764 | return NULL; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 765 | |
| 766 | ddev->driver_capabilities = driver_capabilities; |
| 767 | ddev->ops = ops; |
| 768 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 769 | mutex_init(&ddev->cmd_lock); |
| 770 | INIT_LIST_HEAD(&ddev->cmd_queue); |
| 771 | |
| 772 | INIT_WORK(&ddev->cmd_work, digital_wq_cmd); |
| 773 | INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete); |
| 774 | |
| 775 | mutex_init(&ddev->poll_lock); |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 776 | INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 777 | |
| 778 | if (supported_protocols & NFC_PROTO_JEWEL_MASK) |
| 779 | ddev->protocols |= NFC_PROTO_JEWEL_MASK; |
| 780 | if (supported_protocols & NFC_PROTO_MIFARE_MASK) |
| 781 | ddev->protocols |= NFC_PROTO_MIFARE_MASK; |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 782 | if (supported_protocols & NFC_PROTO_FELICA_MASK) |
| 783 | ddev->protocols |= NFC_PROTO_FELICA_MASK; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 784 | if (supported_protocols & NFC_PROTO_NFC_DEP_MASK) |
| 785 | ddev->protocols |= NFC_PROTO_NFC_DEP_MASK; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 786 | if (supported_protocols & NFC_PROTO_ISO15693_MASK) |
| 787 | ddev->protocols |= NFC_PROTO_ISO15693_MASK; |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 788 | if (supported_protocols & NFC_PROTO_ISO14443_MASK) |
| 789 | ddev->protocols |= NFC_PROTO_ISO14443_MASK; |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 790 | if (supported_protocols & NFC_PROTO_ISO14443_B_MASK) |
| 791 | ddev->protocols |= NFC_PROTO_ISO14443_B_MASK; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 792 | |
| 793 | ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; |
| 794 | ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 795 | |
| 796 | ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols, |
| 797 | ddev->tx_headroom, |
| 798 | ddev->tx_tailroom); |
| 799 | if (!ddev->nfc_dev) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 800 | pr_err("nfc_allocate_device failed\n"); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 801 | goto free_dev; |
| 802 | } |
| 803 | |
| 804 | nfc_set_drvdata(ddev->nfc_dev, ddev); |
| 805 | |
| 806 | return ddev; |
| 807 | |
| 808 | free_dev: |
| 809 | kfree(ddev); |
| 810 | |
| 811 | return NULL; |
| 812 | } |
| 813 | EXPORT_SYMBOL(nfc_digital_allocate_device); |
| 814 | |
| 815 | void nfc_digital_free_device(struct nfc_digital_dev *ddev) |
| 816 | { |
| 817 | nfc_free_device(ddev->nfc_dev); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 818 | kfree(ddev); |
| 819 | } |
| 820 | EXPORT_SYMBOL(nfc_digital_free_device); |
| 821 | |
| 822 | int nfc_digital_register_device(struct nfc_digital_dev *ddev) |
| 823 | { |
| 824 | return nfc_register_device(ddev->nfc_dev); |
| 825 | } |
| 826 | EXPORT_SYMBOL(nfc_digital_register_device); |
| 827 | |
| 828 | void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) |
| 829 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 830 | struct digital_cmd *cmd, *n; |
| 831 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 832 | nfc_unregister_device(ddev->nfc_dev); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 833 | |
| 834 | mutex_lock(&ddev->poll_lock); |
| 835 | ddev->poll_tech_count = 0; |
| 836 | mutex_unlock(&ddev->poll_lock); |
| 837 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame] | 838 | cancel_delayed_work_sync(&ddev->poll_work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 839 | cancel_work_sync(&ddev->cmd_work); |
| 840 | cancel_work_sync(&ddev->cmd_complete_work); |
| 841 | |
| 842 | list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) { |
| 843 | list_del(&cmd->queue); |
Thierry Escande | 82e5795 | 2016-06-16 20:24:43 +0200 | [diff] [blame] | 844 | |
| 845 | /* Call the command callback if any and pass it a ENODEV error. |
| 846 | * This gives a chance to the command issuer to free any |
| 847 | * allocated buffer. |
| 848 | */ |
| 849 | if (cmd->cmd_cb) |
| 850 | cmd->cmd_cb(ddev, cmd->cb_context, ERR_PTR(-ENODEV)); |
| 851 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 852 | kfree(cmd->mdaa_params); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 853 | kfree(cmd); |
| 854 | } |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 855 | } |
| 856 | EXPORT_SYMBOL(nfc_digital_unregister_device); |
| 857 | |
| 858 | MODULE_LICENSE("GPL"); |