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