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