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