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