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 | |
| 77 | *skb_put(skb, 1) = crc & 0xFF; |
| 78 | *skb_put(skb, 1) = (crc >> 8) & 0xFF; |
| 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 | |
| 179 | mutex_unlock(&ddev->cmd_lock); |
| 180 | |
| 181 | if (cmd->req) |
| 182 | print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1, |
| 183 | cmd->req->data, cmd->req->len, false); |
| 184 | |
| 185 | switch (cmd->type) { |
| 186 | case DIGITAL_CMD_IN_SEND: |
| 187 | rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout, |
| 188 | digital_send_cmd_complete, cmd); |
| 189 | break; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 190 | |
| 191 | case DIGITAL_CMD_TG_SEND: |
| 192 | rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout, |
| 193 | digital_send_cmd_complete, cmd); |
| 194 | break; |
| 195 | |
| 196 | case DIGITAL_CMD_TG_LISTEN: |
| 197 | rc = ddev->ops->tg_listen(ddev, cmd->timeout, |
| 198 | digital_send_cmd_complete, cmd); |
| 199 | break; |
| 200 | |
| 201 | case DIGITAL_CMD_TG_LISTEN_MDAA: |
| 202 | params = cmd->mdaa_params; |
| 203 | |
| 204 | rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout, |
| 205 | digital_send_cmd_complete, cmd); |
| 206 | break; |
| 207 | |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 208 | case DIGITAL_CMD_TG_LISTEN_MD: |
| 209 | rc = ddev->ops->tg_listen_md(ddev, cmd->timeout, |
| 210 | digital_send_cmd_complete, cmd); |
| 211 | break; |
| 212 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 213 | default: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 214 | pr_err("Unknown cmd type %d\n", cmd->type); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (!rc) |
| 219 | return; |
| 220 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 221 | pr_err("in_send_command returned err %d\n", rc); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 222 | |
| 223 | mutex_lock(&ddev->cmd_lock); |
| 224 | list_del(&cmd->queue); |
| 225 | mutex_unlock(&ddev->cmd_lock); |
| 226 | |
| 227 | kfree_skb(cmd->req); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 228 | kfree(cmd->mdaa_params); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 229 | kfree(cmd); |
| 230 | |
| 231 | schedule_work(&ddev->cmd_work); |
| 232 | } |
| 233 | |
| 234 | int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type, |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 235 | struct sk_buff *skb, struct digital_tg_mdaa_params *params, |
| 236 | u16 timeout, nfc_digital_cmd_complete_t cmd_cb, |
| 237 | void *cb_context) |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 238 | { |
| 239 | struct digital_cmd *cmd; |
| 240 | |
| 241 | cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL); |
| 242 | if (!cmd) |
| 243 | return -ENOMEM; |
| 244 | |
| 245 | cmd->type = cmd_type; |
| 246 | cmd->timeout = timeout; |
| 247 | cmd->req = skb; |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 248 | cmd->mdaa_params = params; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 249 | cmd->cmd_cb = cmd_cb; |
| 250 | cmd->cb_context = cb_context; |
| 251 | INIT_LIST_HEAD(&cmd->queue); |
| 252 | |
| 253 | mutex_lock(&ddev->cmd_lock); |
| 254 | list_add_tail(&cmd->queue, &ddev->cmd_queue); |
| 255 | mutex_unlock(&ddev->cmd_lock); |
| 256 | |
| 257 | schedule_work(&ddev->cmd_work); |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param) |
| 263 | { |
| 264 | int rc; |
| 265 | |
| 266 | rc = ddev->ops->in_configure_hw(ddev, type, param); |
| 267 | if (rc) |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 268 | pr_err("in_configure_hw failed: %d\n", rc); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 269 | |
| 270 | return rc; |
| 271 | } |
| 272 | |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 273 | int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param) |
| 274 | { |
| 275 | int rc; |
| 276 | |
| 277 | rc = ddev->ops->tg_configure_hw(ddev, type, param); |
| 278 | if (rc) |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 279 | pr_err("tg_configure_hw failed: %d\n", rc); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 280 | |
| 281 | return rc; |
| 282 | } |
| 283 | |
| 284 | static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 285 | { |
| 286 | struct digital_tg_mdaa_params *params; |
| 287 | |
| 288 | params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL); |
| 289 | if (!params) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | params->sens_res = DIGITAL_SENS_RES_NFC_DEP; |
| 293 | get_random_bytes(params->nfcid1, sizeof(params->nfcid1)); |
| 294 | params->sel_res = DIGITAL_SEL_RES_NFC_DEP; |
| 295 | |
| 296 | params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1; |
| 297 | params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2; |
| 298 | get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2); |
| 299 | params->sc = DIGITAL_SENSF_FELICA_SC; |
| 300 | |
| 301 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params, |
| 302 | 500, digital_tg_recv_atr_req, NULL); |
| 303 | } |
| 304 | |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 305 | static int digital_tg_listen_md(struct nfc_digital_dev *ddev, u8 rf_tech) |
| 306 | { |
| 307 | return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MD, NULL, NULL, 500, |
| 308 | digital_tg_recv_md_req, NULL); |
| 309 | } |
| 310 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 311 | int digital_target_found(struct nfc_digital_dev *ddev, |
| 312 | struct nfc_target *target, u8 protocol) |
| 313 | { |
| 314 | int rc; |
| 315 | u8 framing; |
| 316 | u8 rf_tech; |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 317 | u8 poll_tech_count; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 318 | int (*check_crc)(struct sk_buff *skb); |
| 319 | void (*add_crc)(struct sk_buff *skb); |
| 320 | |
| 321 | rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech; |
| 322 | |
| 323 | switch (protocol) { |
| 324 | case NFC_PROTO_JEWEL: |
| 325 | framing = NFC_DIGITAL_FRAMING_NFCA_T1T; |
| 326 | check_crc = digital_skb_check_crc_b; |
| 327 | add_crc = digital_skb_add_crc_b; |
| 328 | break; |
| 329 | |
| 330 | case NFC_PROTO_MIFARE: |
| 331 | framing = NFC_DIGITAL_FRAMING_NFCA_T2T; |
| 332 | check_crc = digital_skb_check_crc_a; |
| 333 | add_crc = digital_skb_add_crc_a; |
| 334 | break; |
| 335 | |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 336 | case NFC_PROTO_FELICA: |
| 337 | framing = NFC_DIGITAL_FRAMING_NFCF_T3T; |
| 338 | check_crc = digital_skb_check_crc_f; |
| 339 | add_crc = digital_skb_add_crc_f; |
| 340 | break; |
| 341 | |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 342 | case NFC_PROTO_NFC_DEP: |
| 343 | if (rf_tech == NFC_DIGITAL_RF_TECH_106A) { |
| 344 | framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP; |
| 345 | check_crc = digital_skb_check_crc_a; |
| 346 | add_crc = digital_skb_add_crc_a; |
| 347 | } else { |
| 348 | framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP; |
| 349 | check_crc = digital_skb_check_crc_f; |
| 350 | add_crc = digital_skb_add_crc_f; |
| 351 | } |
| 352 | break; |
| 353 | |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 354 | case NFC_PROTO_ISO15693: |
Mark A. Greer | ceeee42 | 2014-03-06 07:39:19 -0700 | [diff] [blame] | 355 | framing = NFC_DIGITAL_FRAMING_ISO15693_T5T; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 356 | check_crc = digital_skb_check_crc_b; |
| 357 | add_crc = digital_skb_add_crc_b; |
Thierry Escande | 564af14 | 2014-02-12 12:13:23 +0100 | [diff] [blame] | 358 | break; |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 359 | |
| 360 | case NFC_PROTO_ISO14443: |
| 361 | framing = NFC_DIGITAL_FRAMING_NFCA_T4T; |
| 362 | check_crc = digital_skb_check_crc_a; |
| 363 | add_crc = digital_skb_add_crc_a; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 364 | break; |
| 365 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 366 | case NFC_PROTO_ISO14443_B: |
| 367 | framing = NFC_DIGITAL_FRAMING_NFCB_T4T; |
| 368 | check_crc = digital_skb_check_crc_b; |
| 369 | add_crc = digital_skb_add_crc_b; |
| 370 | break; |
| 371 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 372 | default: |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 373 | pr_err("Invalid protocol %d\n", protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 374 | return -EINVAL; |
| 375 | } |
| 376 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 377 | pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 378 | |
| 379 | ddev->curr_rf_tech = rf_tech; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 380 | |
| 381 | if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) { |
| 382 | ddev->skb_add_crc = digital_skb_add_crc_none; |
| 383 | ddev->skb_check_crc = digital_skb_check_crc_none; |
| 384 | } else { |
| 385 | ddev->skb_add_crc = add_crc; |
| 386 | ddev->skb_check_crc = check_crc; |
| 387 | } |
| 388 | |
| 389 | rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing); |
| 390 | if (rc) |
| 391 | return rc; |
| 392 | |
| 393 | target->supported_protocols = (1 << protocol); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 394 | |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 395 | poll_tech_count = ddev->poll_tech_count; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 396 | ddev->poll_tech_count = 0; |
| 397 | |
Mark A. Greer | 0529a7a | 2014-07-02 09:03:49 -0700 | [diff] [blame] | 398 | rc = nfc_targets_found(ddev->nfc_dev, target, 1); |
| 399 | if (rc) { |
| 400 | ddev->poll_tech_count = poll_tech_count; |
| 401 | return rc; |
| 402 | } |
| 403 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 404 | return 0; |
| 405 | } |
| 406 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 407 | void digital_poll_next_tech(struct nfc_digital_dev *ddev) |
| 408 | { |
Thierry Escande | 9dc3370 | 2014-05-10 13:06:02 +0200 | [diff] [blame] | 409 | u8 rand_mod; |
| 410 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 411 | digital_switch_rf(ddev, 0); |
| 412 | |
| 413 | mutex_lock(&ddev->poll_lock); |
| 414 | |
| 415 | if (!ddev->poll_tech_count) { |
| 416 | mutex_unlock(&ddev->poll_lock); |
| 417 | return; |
| 418 | } |
| 419 | |
Thierry Escande | 9dc3370 | 2014-05-10 13:06:02 +0200 | [diff] [blame] | 420 | get_random_bytes(&rand_mod, sizeof(rand_mod)); |
| 421 | ddev->poll_tech_index = rand_mod % ddev->poll_tech_count; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 422 | |
| 423 | mutex_unlock(&ddev->poll_lock); |
| 424 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 425 | schedule_delayed_work(&ddev->poll_work, |
| 426 | msecs_to_jiffies(DIGITAL_POLL_INTERVAL)); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static void digital_wq_poll(struct work_struct *work) |
| 430 | { |
| 431 | int rc; |
| 432 | struct digital_poll_tech *poll_tech; |
| 433 | struct nfc_digital_dev *ddev = container_of(work, |
| 434 | struct nfc_digital_dev, |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 435 | poll_work.work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 436 | mutex_lock(&ddev->poll_lock); |
| 437 | |
| 438 | if (!ddev->poll_tech_count) { |
| 439 | mutex_unlock(&ddev->poll_lock); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | poll_tech = &ddev->poll_techs[ddev->poll_tech_index]; |
| 444 | |
| 445 | mutex_unlock(&ddev->poll_lock); |
| 446 | |
| 447 | rc = poll_tech->poll_func(ddev, poll_tech->rf_tech); |
| 448 | if (rc) |
| 449 | digital_poll_next_tech(ddev); |
| 450 | } |
| 451 | |
| 452 | static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech, |
| 453 | digital_poll_t poll_func) |
| 454 | { |
| 455 | struct digital_poll_tech *poll_tech; |
| 456 | |
| 457 | if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX) |
| 458 | return; |
| 459 | |
| 460 | poll_tech = &ddev->poll_techs[ddev->poll_tech_count++]; |
| 461 | |
| 462 | poll_tech->rf_tech = rf_tech; |
| 463 | poll_tech->poll_func = poll_func; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * start_poll operation |
| 468 | * |
| 469 | * For every supported protocol, the corresponding polling function is added |
| 470 | * to the table of polling technologies (ddev->poll_techs[]) using |
| 471 | * digital_add_poll_tech(). |
| 472 | * When a polling function fails (by timeout or protocol error) the next one is |
| 473 | * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work). |
| 474 | */ |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 475 | static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols, |
| 476 | __u32 tm_protocols) |
| 477 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 478 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 479 | u32 matching_im_protocols, matching_tm_protocols; |
| 480 | |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 481 | 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] | 482 | tm_protocols, ddev->protocols); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 483 | |
| 484 | matching_im_protocols = ddev->protocols & im_protocols; |
| 485 | matching_tm_protocols = ddev->protocols & tm_protocols; |
| 486 | |
| 487 | if (!matching_im_protocols && !matching_tm_protocols) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 488 | pr_err("Unknown protocol\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 489 | return -EINVAL; |
| 490 | } |
| 491 | |
| 492 | if (ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 493 | pr_err("Already polling\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 494 | return -EBUSY; |
| 495 | } |
| 496 | |
| 497 | if (ddev->curr_protocol) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 498 | pr_err("A target is already active\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 499 | return -EBUSY; |
| 500 | } |
| 501 | |
| 502 | ddev->poll_tech_count = 0; |
| 503 | ddev->poll_tech_index = 0; |
| 504 | |
| 505 | if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH) |
| 506 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, |
| 507 | digital_in_send_sens_req); |
| 508 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 509 | if (matching_im_protocols & DIGITAL_PROTO_NFCB_RF_TECH) |
| 510 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106B, |
| 511 | digital_in_send_sensb_req); |
| 512 | |
Axel Lin | 4f913d4 | 2014-02-22 10:16:11 +0800 | [diff] [blame] | 513 | if (matching_im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) { |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 514 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, |
| 515 | digital_in_send_sensf_req); |
| 516 | |
| 517 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, |
| 518 | digital_in_send_sensf_req); |
| 519 | } |
| 520 | |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 521 | if (matching_im_protocols & DIGITAL_PROTO_ISO15693_RF_TECH) |
| 522 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_ISO15693, |
| 523 | digital_in_send_iso15693_inv_req); |
| 524 | |
Axel Lin | 4f913d4 | 2014-02-22 10:16:11 +0800 | [diff] [blame] | 525 | if (matching_tm_protocols & NFC_PROTO_NFC_DEP_MASK) { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 526 | if (ddev->ops->tg_listen_mdaa) { |
| 527 | digital_add_poll_tech(ddev, 0, |
| 528 | digital_tg_listen_mdaa); |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 529 | } else if (ddev->ops->tg_listen_md) { |
| 530 | digital_add_poll_tech(ddev, 0, |
| 531 | digital_tg_listen_md); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 532 | } else { |
| 533 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A, |
| 534 | digital_tg_listen_nfca); |
| 535 | |
| 536 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F, |
| 537 | digital_tg_listen_nfcf); |
| 538 | |
| 539 | digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F, |
| 540 | digital_tg_listen_nfcf); |
| 541 | } |
| 542 | } |
| 543 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 544 | if (!ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 545 | pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n", |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 546 | matching_im_protocols, matching_tm_protocols); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 550 | schedule_delayed_work(&ddev->poll_work, 0); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 551 | |
| 552 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static void digital_stop_poll(struct nfc_dev *nfc_dev) |
| 556 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 557 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 558 | |
| 559 | mutex_lock(&ddev->poll_lock); |
| 560 | |
| 561 | if (!ddev->poll_tech_count) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 562 | pr_err("Polling operation was not running\n"); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 563 | mutex_unlock(&ddev->poll_lock); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | ddev->poll_tech_count = 0; |
| 568 | |
| 569 | mutex_unlock(&ddev->poll_lock); |
| 570 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 571 | cancel_delayed_work_sync(&ddev->poll_work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 572 | |
| 573 | digital_abort_cmd(ddev); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static int digital_dev_up(struct nfc_dev *nfc_dev) |
| 577 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 578 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 579 | |
| 580 | digital_switch_rf(ddev, 1); |
| 581 | |
| 582 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | static int digital_dev_down(struct nfc_dev *nfc_dev) |
| 586 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 587 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 588 | |
| 589 | digital_switch_rf(ddev, 0); |
| 590 | |
| 591 | return 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static int digital_dep_link_up(struct nfc_dev *nfc_dev, |
| 595 | struct nfc_target *target, |
| 596 | __u8 comm_mode, __u8 *gb, size_t gb_len) |
| 597 | { |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 598 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 599 | int rc; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 600 | |
Thierry Escande | 48e1044 | 2014-01-06 23:34:37 +0100 | [diff] [blame] | 601 | rc = digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len); |
| 602 | |
| 603 | if (!rc) |
| 604 | ddev->curr_protocol = NFC_PROTO_NFC_DEP; |
| 605 | |
| 606 | return rc; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static int digital_dep_link_down(struct nfc_dev *nfc_dev) |
| 610 | { |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 611 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 612 | |
| 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 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 649 | ddev->curr_protocol = 0; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb) |
| 653 | { |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 654 | struct nfc_digital_dev *ddev = nfc_get_drvdata(dev); |
| 655 | |
| 656 | return digital_tg_send_dep_res(ddev, skb); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 659 | static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg, |
| 660 | struct sk_buff *resp) |
| 661 | { |
| 662 | struct digital_data_exch *data_exch = arg; |
| 663 | int rc; |
| 664 | |
| 665 | if (IS_ERR(resp)) { |
| 666 | rc = PTR_ERR(resp); |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 667 | resp = NULL; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 668 | goto done; |
| 669 | } |
| 670 | |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 671 | if (ddev->curr_protocol == NFC_PROTO_MIFARE) { |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 672 | rc = digital_in_recv_mifare_res(resp); |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 673 | /* crc check is done in digital_in_recv_mifare_res() */ |
| 674 | goto done; |
| 675 | } |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 676 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 677 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
| 678 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 679 | rc = digital_in_iso_dep_pull_sod(ddev, resp); |
| 680 | if (rc) |
| 681 | goto done; |
| 682 | } |
| 683 | |
| 684 | rc = ddev->skb_check_crc(resp); |
| 685 | |
| 686 | done: |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 687 | if (rc) { |
| 688 | kfree_skb(resp); |
| 689 | resp = NULL; |
| 690 | } |
| 691 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 692 | data_exch->cb(data_exch->cb_context, resp, rc); |
| 693 | |
| 694 | kfree(data_exch); |
| 695 | } |
| 696 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 697 | static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target, |
| 698 | struct sk_buff *skb, data_exchange_cb_t cb, |
| 699 | void *cb_context) |
| 700 | { |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 701 | struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev); |
| 702 | struct digital_data_exch *data_exch; |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 703 | int rc; |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 704 | |
| 705 | data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL); |
| 706 | if (!data_exch) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 707 | pr_err("Failed to allocate data_exch struct\n"); |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 708 | return -ENOMEM; |
| 709 | } |
| 710 | |
| 711 | data_exch->cb = cb; |
| 712 | data_exch->cb_context = cb_context; |
| 713 | |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 714 | if (ddev->curr_protocol == NFC_PROTO_NFC_DEP) { |
| 715 | rc = digital_in_send_dep_req(ddev, target, skb, data_exch); |
| 716 | goto exit; |
| 717 | } |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 718 | |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 719 | if ((ddev->curr_protocol == NFC_PROTO_ISO14443) || |
| 720 | (ddev->curr_protocol == NFC_PROTO_ISO14443_B)) { |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 721 | rc = digital_in_iso_dep_push_sod(ddev, skb); |
| 722 | if (rc) |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 723 | goto exit; |
Thierry Escande | c813007 | 2014-01-27 00:31:32 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Thierry Escande | 2c66dae | 2013-09-19 17:55:27 +0200 | [diff] [blame] | 726 | ddev->skb_add_crc(skb); |
| 727 | |
Thierry Escande | 6ea7398 | 2014-02-12 14:27:51 +0100 | [diff] [blame] | 728 | rc = digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete, |
| 729 | data_exch); |
| 730 | |
| 731 | exit: |
| 732 | if (rc) |
| 733 | kfree(data_exch); |
| 734 | |
| 735 | return rc; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static struct nfc_ops digital_nfc_ops = { |
| 739 | .dev_up = digital_dev_up, |
| 740 | .dev_down = digital_dev_down, |
| 741 | .start_poll = digital_start_poll, |
| 742 | .stop_poll = digital_stop_poll, |
| 743 | .dep_link_up = digital_dep_link_up, |
| 744 | .dep_link_down = digital_dep_link_down, |
| 745 | .activate_target = digital_activate_target, |
| 746 | .deactivate_target = digital_deactivate_target, |
| 747 | .tm_send = digital_tg_send, |
| 748 | .im_transceive = digital_in_send, |
| 749 | }; |
| 750 | |
| 751 | struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops, |
| 752 | __u32 supported_protocols, |
| 753 | __u32 driver_capabilities, |
| 754 | int tx_headroom, int tx_tailroom) |
| 755 | { |
| 756 | struct nfc_digital_dev *ddev; |
| 757 | |
| 758 | if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen || |
| 759 | !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd || |
Mark A. Greer | bf30a67 | 2014-07-21 21:24:39 -0700 | [diff] [blame] | 760 | !ops->switch_rf || (ops->tg_listen_md && !ops->tg_get_rf_tech)) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 761 | return NULL; |
| 762 | |
| 763 | ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL); |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 764 | if (!ddev) |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 765 | return NULL; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 766 | |
| 767 | ddev->driver_capabilities = driver_capabilities; |
| 768 | ddev->ops = ops; |
| 769 | |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 770 | mutex_init(&ddev->cmd_lock); |
| 771 | INIT_LIST_HEAD(&ddev->cmd_queue); |
| 772 | |
| 773 | INIT_WORK(&ddev->cmd_work, digital_wq_cmd); |
| 774 | INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete); |
| 775 | |
| 776 | mutex_init(&ddev->poll_lock); |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 777 | INIT_DELAYED_WORK(&ddev->poll_work, digital_wq_poll); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 778 | |
| 779 | if (supported_protocols & NFC_PROTO_JEWEL_MASK) |
| 780 | ddev->protocols |= NFC_PROTO_JEWEL_MASK; |
| 781 | if (supported_protocols & NFC_PROTO_MIFARE_MASK) |
| 782 | ddev->protocols |= NFC_PROTO_MIFARE_MASK; |
Thierry Escande | 8c0695e4 | 2013-09-19 17:55:28 +0200 | [diff] [blame] | 783 | if (supported_protocols & NFC_PROTO_FELICA_MASK) |
| 784 | ddev->protocols |= NFC_PROTO_FELICA_MASK; |
Thierry Escande | 7d0911c | 2013-09-19 17:55:29 +0200 | [diff] [blame] | 785 | if (supported_protocols & NFC_PROTO_NFC_DEP_MASK) |
| 786 | ddev->protocols |= NFC_PROTO_NFC_DEP_MASK; |
Mark A. Greer | a381d48 | 2014-01-21 16:23:59 -0700 | [diff] [blame] | 787 | if (supported_protocols & NFC_PROTO_ISO15693_MASK) |
| 788 | ddev->protocols |= NFC_PROTO_ISO15693_MASK; |
Thierry Escande | 12e3d24 | 2014-01-27 00:31:31 +0100 | [diff] [blame] | 789 | if (supported_protocols & NFC_PROTO_ISO14443_MASK) |
| 790 | ddev->protocols |= NFC_PROTO_ISO14443_MASK; |
Mark A. Greer | 2473460 | 2014-03-31 17:36:38 -0700 | [diff] [blame] | 791 | if (supported_protocols & NFC_PROTO_ISO14443_B_MASK) |
| 792 | ddev->protocols |= NFC_PROTO_ISO14443_B_MASK; |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 793 | |
| 794 | ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN; |
| 795 | ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN; |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 796 | |
| 797 | ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols, |
| 798 | ddev->tx_headroom, |
| 799 | ddev->tx_tailroom); |
| 800 | if (!ddev->nfc_dev) { |
Samuel Ortiz | 2604253 | 2013-09-20 16:56:40 +0200 | [diff] [blame] | 801 | pr_err("nfc_allocate_device failed\n"); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 802 | goto free_dev; |
| 803 | } |
| 804 | |
| 805 | nfc_set_drvdata(ddev->nfc_dev, ddev); |
| 806 | |
| 807 | return ddev; |
| 808 | |
| 809 | free_dev: |
| 810 | kfree(ddev); |
| 811 | |
| 812 | return NULL; |
| 813 | } |
| 814 | EXPORT_SYMBOL(nfc_digital_allocate_device); |
| 815 | |
| 816 | void nfc_digital_free_device(struct nfc_digital_dev *ddev) |
| 817 | { |
| 818 | nfc_free_device(ddev->nfc_dev); |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 819 | kfree(ddev); |
| 820 | } |
| 821 | EXPORT_SYMBOL(nfc_digital_free_device); |
| 822 | |
| 823 | int nfc_digital_register_device(struct nfc_digital_dev *ddev) |
| 824 | { |
| 825 | return nfc_register_device(ddev->nfc_dev); |
| 826 | } |
| 827 | EXPORT_SYMBOL(nfc_digital_register_device); |
| 828 | |
| 829 | void nfc_digital_unregister_device(struct nfc_digital_dev *ddev) |
| 830 | { |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 831 | struct digital_cmd *cmd, *n; |
| 832 | |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 833 | nfc_unregister_device(ddev->nfc_dev); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 834 | |
| 835 | mutex_lock(&ddev->poll_lock); |
| 836 | ddev->poll_tech_count = 0; |
| 837 | mutex_unlock(&ddev->poll_lock); |
| 838 | |
Thierry Escande | 7854a44 | 2016-06-07 16:21:52 +0200 | [diff] [blame^] | 839 | cancel_delayed_work_sync(&ddev->poll_work); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 840 | cancel_work_sync(&ddev->cmd_work); |
| 841 | cancel_work_sync(&ddev->cmd_complete_work); |
| 842 | |
| 843 | list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) { |
| 844 | list_del(&cmd->queue); |
Thierry Escande | 1c7a4c2 | 2013-09-19 17:55:30 +0200 | [diff] [blame] | 845 | kfree(cmd->mdaa_params); |
Thierry Escande | 59ee236 | 2013-09-19 17:55:26 +0200 | [diff] [blame] | 846 | kfree(cmd); |
| 847 | } |
Thierry Escande | 4b10884eb | 2013-09-19 17:55:25 +0200 | [diff] [blame] | 848 | } |
| 849 | EXPORT_SYMBOL(nfc_digital_unregister_device); |
| 850 | |
| 851 | MODULE_LICENSE("GPL"); |