linux/drivers/nfc/pn533/pn533.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Driver for NXP PN533 NFC Chip - core functions
   4 *
   5 * Copyright (C) 2011 Instituto Nokia de Tecnologia
   6 * Copyright (C) 2012-2013 Tieto Poland
   7 */
   8
   9#include <linux/device.h>
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/slab.h>
  13#include <linux/nfc.h>
  14#include <linux/netdevice.h>
  15#include <net/nfc/nfc.h>
  16#include "pn533.h"
  17
  18#define VERSION "0.3"
  19
  20/* How much time we spend listening for initiators */
  21#define PN533_LISTEN_TIME 2
  22/* Delay between each poll frame (ms) */
  23#define PN533_POLL_INTERVAL 10
  24
  25/* structs for pn533 commands */
  26
  27/* PN533_CMD_GET_FIRMWARE_VERSION */
  28struct pn533_fw_version {
  29        u8 ic;
  30        u8 ver;
  31        u8 rev;
  32        u8 support;
  33};
  34
  35/* PN533_CMD_RF_CONFIGURATION */
  36#define PN533_CFGITEM_RF_FIELD    0x01
  37#define PN533_CFGITEM_TIMING      0x02
  38#define PN533_CFGITEM_MAX_RETRIES 0x05
  39#define PN533_CFGITEM_PASORI      0x82
  40
  41#define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
  42#define PN533_CFGITEM_RF_FIELD_ON        0x1
  43#define PN533_CFGITEM_RF_FIELD_OFF       0x0
  44
  45#define PN533_CONFIG_TIMING_102 0xb
  46#define PN533_CONFIG_TIMING_204 0xc
  47#define PN533_CONFIG_TIMING_409 0xd
  48#define PN533_CONFIG_TIMING_819 0xe
  49
  50#define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
  51#define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
  52
  53struct pn533_config_max_retries {
  54        u8 mx_rty_atr;
  55        u8 mx_rty_psl;
  56        u8 mx_rty_passive_act;
  57} __packed;
  58
  59struct pn533_config_timing {
  60        u8 rfu;
  61        u8 atr_res_timeout;
  62        u8 dep_timeout;
  63} __packed;
  64
  65/* PN533_CMD_IN_LIST_PASSIVE_TARGET */
  66
  67/* felica commands opcode */
  68#define PN533_FELICA_OPC_SENSF_REQ 0
  69#define PN533_FELICA_OPC_SENSF_RES 1
  70/* felica SENSF_REQ parameters */
  71#define PN533_FELICA_SENSF_SC_ALL 0xFFFF
  72#define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
  73#define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
  74#define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
  75
  76/* type B initiator_data values */
  77#define PN533_TYPE_B_AFI_ALL_FAMILIES 0
  78#define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
  79#define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
  80
  81union pn533_cmd_poll_initdata {
  82        struct {
  83                u8 afi;
  84                u8 polling_method;
  85        } __packed type_b;
  86        struct {
  87                u8 opcode;
  88                __be16 sc;
  89                u8 rc;
  90                u8 tsn;
  91        } __packed felica;
  92};
  93
  94struct pn533_poll_modulations {
  95        struct {
  96                u8 maxtg;
  97                u8 brty;
  98                union pn533_cmd_poll_initdata initiator_data;
  99        } __packed data;
 100        u8 len;
 101};
 102
 103static const struct pn533_poll_modulations poll_mod[] = {
 104        [PN533_POLL_MOD_106KBPS_A] = {
 105                .data = {
 106                        .maxtg = 1,
 107                        .brty = 0,
 108                },
 109                .len = 2,
 110        },
 111        [PN533_POLL_MOD_212KBPS_FELICA] = {
 112                .data = {
 113                        .maxtg = 1,
 114                        .brty = 1,
 115                        .initiator_data.felica = {
 116                                .opcode = PN533_FELICA_OPC_SENSF_REQ,
 117                                .sc = PN533_FELICA_SENSF_SC_ALL,
 118                                .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
 119                                .tsn = 0x03,
 120                        },
 121                },
 122                .len = 7,
 123        },
 124        [PN533_POLL_MOD_424KBPS_FELICA] = {
 125                .data = {
 126                        .maxtg = 1,
 127                        .brty = 2,
 128                        .initiator_data.felica = {
 129                                .opcode = PN533_FELICA_OPC_SENSF_REQ,
 130                                .sc = PN533_FELICA_SENSF_SC_ALL,
 131                                .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
 132                                .tsn = 0x03,
 133                        },
 134                 },
 135                .len = 7,
 136        },
 137        [PN533_POLL_MOD_106KBPS_JEWEL] = {
 138                .data = {
 139                        .maxtg = 1,
 140                        .brty = 4,
 141                },
 142                .len = 2,
 143        },
 144        [PN533_POLL_MOD_847KBPS_B] = {
 145                .data = {
 146                        .maxtg = 1,
 147                        .brty = 8,
 148                        .initiator_data.type_b = {
 149                                .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
 150                                .polling_method =
 151                                        PN533_TYPE_B_POLL_METHOD_TIMESLOT,
 152                        },
 153                },
 154                .len = 3,
 155        },
 156        [PN533_LISTEN_MOD] = {
 157                .len = 0,
 158        },
 159};
 160
 161/* PN533_CMD_IN_ATR */
 162
 163struct pn533_cmd_activate_response {
 164        u8 status;
 165        u8 nfcid3t[10];
 166        u8 didt;
 167        u8 bst;
 168        u8 brt;
 169        u8 to;
 170        u8 ppt;
 171        /* optional */
 172        u8 gt[];
 173} __packed;
 174
 175struct pn533_cmd_jump_dep_response {
 176        u8 status;
 177        u8 tg;
 178        u8 nfcid3t[10];
 179        u8 didt;
 180        u8 bst;
 181        u8 brt;
 182        u8 to;
 183        u8 ppt;
 184        /* optional */
 185        u8 gt[];
 186} __packed;
 187
 188struct pn532_autopoll_resp {
 189        u8 type;
 190        u8 ln;
 191        u8 tg;
 192        u8 tgdata[];
 193};
 194
 195/* PN532_CMD_IN_AUTOPOLL */
 196#define PN532_AUTOPOLL_POLLNR_INFINITE  0xff
 197#define PN532_AUTOPOLL_PERIOD           0x03 /* in units of 150 ms */
 198
 199#define PN532_AUTOPOLL_TYPE_GENERIC_106         0x00
 200#define PN532_AUTOPOLL_TYPE_GENERIC_212         0x01
 201#define PN532_AUTOPOLL_TYPE_GENERIC_424         0x02
 202#define PN532_AUTOPOLL_TYPE_JEWEL               0x04
 203#define PN532_AUTOPOLL_TYPE_MIFARE              0x10
 204#define PN532_AUTOPOLL_TYPE_FELICA212           0x11
 205#define PN532_AUTOPOLL_TYPE_FELICA424           0x12
 206#define PN532_AUTOPOLL_TYPE_ISOA                0x20
 207#define PN532_AUTOPOLL_TYPE_ISOB                0x23
 208#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106     0x40
 209#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212     0x41
 210#define PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424     0x42
 211#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106      0x80
 212#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_212      0x81
 213#define PN532_AUTOPOLL_TYPE_DEP_ACTIVE_424      0x82
 214
 215/* PN533_TG_INIT_AS_TARGET */
 216#define PN533_INIT_TARGET_PASSIVE 0x1
 217#define PN533_INIT_TARGET_DEP 0x2
 218
 219#define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
 220#define PN533_INIT_TARGET_RESP_ACTIVE     0x1
 221#define PN533_INIT_TARGET_RESP_DEP        0x4
 222
 223/* The rule: value(high byte) + value(low byte) + checksum = 0 */
 224static inline u8 pn533_ext_checksum(u16 value)
 225{
 226        return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
 227}
 228
 229/* The rule: value + checksum = 0 */
 230static inline u8 pn533_std_checksum(u8 value)
 231{
 232        return ~value + 1;
 233}
 234
 235/* The rule: sum(data elements) + checksum = 0 */
 236static u8 pn533_std_data_checksum(u8 *data, int datalen)
 237{
 238        u8 sum = 0;
 239        int i;
 240
 241        for (i = 0; i < datalen; i++)
 242                sum += data[i];
 243
 244        return pn533_std_checksum(sum);
 245}
 246
 247static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
 248{
 249        struct pn533_std_frame *frame = _frame;
 250
 251        frame->preamble = 0;
 252        frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
 253        PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
 254        PN533_FRAME_CMD(frame) = cmd_code;
 255        frame->datalen = 2;
 256}
 257
 258static void pn533_std_tx_frame_finish(void *_frame)
 259{
 260        struct pn533_std_frame *frame = _frame;
 261
 262        frame->datalen_checksum = pn533_std_checksum(frame->datalen);
 263
 264        PN533_STD_FRAME_CHECKSUM(frame) =
 265                pn533_std_data_checksum(frame->data, frame->datalen);
 266
 267        PN533_STD_FRAME_POSTAMBLE(frame) = 0;
 268}
 269
 270static void pn533_std_tx_update_payload_len(void *_frame, int len)
 271{
 272        struct pn533_std_frame *frame = _frame;
 273
 274        frame->datalen += len;
 275}
 276
 277static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
 278{
 279        u8 checksum;
 280        struct pn533_std_frame *stdf = _frame;
 281
 282        if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
 283                return false;
 284
 285        if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
 286                /* Standard frame code */
 287                dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
 288
 289                checksum = pn533_std_checksum(stdf->datalen);
 290                if (checksum != stdf->datalen_checksum)
 291                        return false;
 292
 293                checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
 294                if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
 295                        return false;
 296        } else {
 297                /* Extended */
 298                struct pn533_ext_frame *eif = _frame;
 299
 300                dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
 301
 302                checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
 303                if (checksum != eif->datalen_checksum)
 304                        return false;
 305
 306                /* check data checksum */
 307                checksum = pn533_std_data_checksum(eif->data,
 308                                                   be16_to_cpu(eif->datalen));
 309                if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
 310                        return false;
 311        }
 312
 313        return true;
 314}
 315
 316bool pn533_rx_frame_is_ack(void *_frame)
 317{
 318        struct pn533_std_frame *frame = _frame;
 319
 320        if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
 321                return false;
 322
 323        if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
 324                return false;
 325
 326        return true;
 327}
 328EXPORT_SYMBOL_GPL(pn533_rx_frame_is_ack);
 329
 330static inline int pn533_std_rx_frame_size(void *frame)
 331{
 332        struct pn533_std_frame *f = frame;
 333
 334        /* check for Extended Information frame */
 335        if (PN533_STD_IS_EXTENDED(f)) {
 336                struct pn533_ext_frame *eif = frame;
 337
 338                return sizeof(struct pn533_ext_frame)
 339                        + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
 340        }
 341
 342        return sizeof(struct pn533_std_frame) + f->datalen +
 343               PN533_STD_FRAME_TAIL_LEN;
 344}
 345
 346static u8 pn533_std_get_cmd_code(void *frame)
 347{
 348        struct pn533_std_frame *f = frame;
 349        struct pn533_ext_frame *eif = frame;
 350
 351        if (PN533_STD_IS_EXTENDED(f))
 352                return PN533_FRAME_CMD(eif);
 353        else
 354                return PN533_FRAME_CMD(f);
 355}
 356
 357bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
 358{
 359        return (dev->ops->get_cmd_code(frame) ==
 360                                PN533_CMD_RESPONSE(dev->cmd->code));
 361}
 362EXPORT_SYMBOL_GPL(pn533_rx_frame_is_cmd_response);
 363
 364
 365static struct pn533_frame_ops pn533_std_frame_ops = {
 366        .tx_frame_init = pn533_std_tx_frame_init,
 367        .tx_frame_finish = pn533_std_tx_frame_finish,
 368        .tx_update_payload_len = pn533_std_tx_update_payload_len,
 369        .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
 370        .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
 371
 372        .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
 373        .rx_frame_size = pn533_std_rx_frame_size,
 374        .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
 375        .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
 376
 377        .max_payload_len =  PN533_STD_FRAME_MAX_PAYLOAD_LEN,
 378        .get_cmd_code = pn533_std_get_cmd_code,
 379};
 380
 381static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
 382                                  struct sk_buff *skb)
 383{
 384        /* payload is already there, just update datalen */
 385        int payload_len = skb->len;
 386        struct pn533_frame_ops *ops = dev->ops;
 387
 388
 389        skb_push(skb, ops->tx_header_len);
 390        skb_put(skb, ops->tx_tail_len);
 391
 392        ops->tx_frame_init(skb->data, cmd_code);
 393        ops->tx_update_payload_len(skb->data, payload_len);
 394        ops->tx_frame_finish(skb->data);
 395}
 396
 397static int pn533_send_async_complete(struct pn533 *dev)
 398{
 399        struct pn533_cmd *cmd = dev->cmd;
 400        struct sk_buff *resp;
 401        int status, rc = 0;
 402
 403        if (!cmd) {
 404                dev_dbg(dev->dev, "%s: cmd not set\n", __func__);
 405                goto done;
 406        }
 407
 408        dev_kfree_skb(cmd->req);
 409
 410        status = cmd->status;
 411        resp = cmd->resp;
 412
 413        if (status < 0) {
 414                rc = cmd->complete_cb(dev, cmd->complete_cb_context,
 415                                      ERR_PTR(status));
 416                dev_kfree_skb(resp);
 417                goto done;
 418        }
 419
 420        /* when no response is set we got interrupted */
 421        if (!resp)
 422                resp = ERR_PTR(-EINTR);
 423
 424        if (!IS_ERR(resp)) {
 425                skb_pull(resp, dev->ops->rx_header_len);
 426                skb_trim(resp, resp->len - dev->ops->rx_tail_len);
 427        }
 428
 429        rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
 430
 431done:
 432        kfree(cmd);
 433        dev->cmd = NULL;
 434        return rc;
 435}
 436
 437static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
 438                              struct sk_buff *req,
 439                              pn533_send_async_complete_t complete_cb,
 440                              void *complete_cb_context)
 441{
 442        struct pn533_cmd *cmd;
 443        int rc = 0;
 444
 445        dev_dbg(dev->dev, "Sending command 0x%x\n", cmd_code);
 446
 447        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 448        if (!cmd)
 449                return -ENOMEM;
 450
 451        cmd->code = cmd_code;
 452        cmd->req = req;
 453        cmd->complete_cb = complete_cb;
 454        cmd->complete_cb_context = complete_cb_context;
 455
 456        pn533_build_cmd_frame(dev, cmd_code, req);
 457
 458        mutex_lock(&dev->cmd_lock);
 459
 460        if (!dev->cmd_pending) {
 461                dev->cmd = cmd;
 462                rc = dev->phy_ops->send_frame(dev, req);
 463                if (rc) {
 464                        dev->cmd = NULL;
 465                        goto error;
 466                }
 467
 468                dev->cmd_pending = 1;
 469                goto unlock;
 470        }
 471
 472        dev_dbg(dev->dev, "%s Queueing command 0x%x\n",
 473                __func__, cmd_code);
 474
 475        INIT_LIST_HEAD(&cmd->queue);
 476        list_add_tail(&cmd->queue, &dev->cmd_queue);
 477
 478        goto unlock;
 479
 480error:
 481        kfree(cmd);
 482unlock:
 483        mutex_unlock(&dev->cmd_lock);
 484        return rc;
 485}
 486
 487static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
 488                                 struct sk_buff *req,
 489                                 pn533_send_async_complete_t complete_cb,
 490                                 void *complete_cb_context)
 491{
 492        return __pn533_send_async(dev, cmd_code, req, complete_cb,
 493                                complete_cb_context);
 494}
 495
 496static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
 497                                struct sk_buff *req,
 498                                pn533_send_async_complete_t complete_cb,
 499                                void *complete_cb_context)
 500{
 501        return __pn533_send_async(dev, cmd_code, req, complete_cb,
 502                                complete_cb_context);
 503}
 504
 505/*
 506 * pn533_send_cmd_direct_async
 507 *
 508 * The function sends a priority cmd directly to the chip omitting the cmd
 509 * queue. It's intended to be used by chaining mechanism of received responses
 510 * where the host has to request every single chunk of data before scheduling
 511 * next cmd from the queue.
 512 */
 513static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
 514                                       struct sk_buff *req,
 515                                       pn533_send_async_complete_t complete_cb,
 516                                       void *complete_cb_context)
 517{
 518        struct pn533_cmd *cmd;
 519        int rc;
 520
 521        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 522        if (!cmd)
 523                return -ENOMEM;
 524
 525        cmd->code = cmd_code;
 526        cmd->req = req;
 527        cmd->complete_cb = complete_cb;
 528        cmd->complete_cb_context = complete_cb_context;
 529
 530        pn533_build_cmd_frame(dev, cmd_code, req);
 531
 532        dev->cmd = cmd;
 533        rc = dev->phy_ops->send_frame(dev, req);
 534        if (rc < 0) {
 535                dev->cmd = NULL;
 536                kfree(cmd);
 537        }
 538
 539        return rc;
 540}
 541
 542static void pn533_wq_cmd_complete(struct work_struct *work)
 543{
 544        struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
 545        int rc;
 546
 547        rc = pn533_send_async_complete(dev);
 548        if (rc != -EINPROGRESS)
 549                queue_work(dev->wq, &dev->cmd_work);
 550}
 551
 552static void pn533_wq_cmd(struct work_struct *work)
 553{
 554        struct pn533 *dev = container_of(work, struct pn533, cmd_work);
 555        struct pn533_cmd *cmd;
 556        int rc;
 557
 558        mutex_lock(&dev->cmd_lock);
 559
 560        if (list_empty(&dev->cmd_queue)) {
 561                dev->cmd_pending = 0;
 562                mutex_unlock(&dev->cmd_lock);
 563                return;
 564        }
 565
 566        cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
 567
 568        list_del(&cmd->queue);
 569
 570        mutex_unlock(&dev->cmd_lock);
 571
 572        dev->cmd = cmd;
 573        rc = dev->phy_ops->send_frame(dev, cmd->req);
 574        if (rc < 0) {
 575                dev->cmd = NULL;
 576                dev_kfree_skb(cmd->req);
 577                kfree(cmd);
 578                return;
 579        }
 580
 581}
 582
 583struct pn533_sync_cmd_response {
 584        struct sk_buff *resp;
 585        struct completion done;
 586};
 587
 588static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
 589                                    struct sk_buff *resp)
 590{
 591        struct pn533_sync_cmd_response *arg = _arg;
 592
 593        arg->resp = resp;
 594        complete(&arg->done);
 595
 596        return 0;
 597}
 598
 599/*  pn533_send_cmd_sync
 600 *
 601 *  Please note the req parameter is freed inside the function to
 602 *  limit a number of return value interpretations by the caller.
 603 *
 604 *  1. negative in case of error during TX path -> req should be freed
 605 *
 606 *  2. negative in case of error during RX path -> req should not be freed
 607 *     as it's been already freed at the beginning of RX path by
 608 *     async_complete_cb.
 609 *
 610 *  3. valid pointer in case of successful RX path
 611 *
 612 *  A caller has to check a return value with IS_ERR macro. If the test pass,
 613 *  the returned pointer is valid.
 614 *
 615 */
 616static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
 617                                               struct sk_buff *req)
 618{
 619        int rc;
 620        struct pn533_sync_cmd_response arg;
 621
 622        init_completion(&arg.done);
 623
 624        rc = pn533_send_cmd_async(dev, cmd_code, req,
 625                                  pn533_send_sync_complete, &arg);
 626        if (rc) {
 627                dev_kfree_skb(req);
 628                return ERR_PTR(rc);
 629        }
 630
 631        wait_for_completion(&arg.done);
 632
 633        return arg.resp;
 634}
 635
 636static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
 637{
 638        struct sk_buff *skb;
 639
 640        skb = alloc_skb(dev->ops->tx_header_len +
 641                        size +
 642                        dev->ops->tx_tail_len, GFP_KERNEL);
 643
 644        if (skb)
 645                skb_reserve(skb, dev->ops->tx_header_len);
 646
 647        return skb;
 648}
 649
 650struct pn533_target_type_a {
 651        __be16 sens_res;
 652        u8 sel_res;
 653        u8 nfcid_len;
 654        u8 nfcid_data[];
 655} __packed;
 656
 657
 658#define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
 659#define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
 660#define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
 661
 662#define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
 663#define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
 664
 665#define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
 666#define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
 667
 668#define PN533_TYPE_A_SEL_PROT_MIFARE 0
 669#define PN533_TYPE_A_SEL_PROT_ISO14443 1
 670#define PN533_TYPE_A_SEL_PROT_DEP 2
 671#define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
 672
 673static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
 674                                                        int target_data_len)
 675{
 676        u8 ssd;
 677        u8 platconf;
 678
 679        if (target_data_len < sizeof(struct pn533_target_type_a))
 680                return false;
 681
 682        /*
 683         * The length check of nfcid[] and ats[] are not being performed because
 684         * the values are not being used
 685         */
 686
 687        /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
 688        ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
 689        platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
 690
 691        if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
 692             platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
 693            (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
 694             platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
 695                return false;
 696
 697        /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
 698        if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
 699                return false;
 700
 701        if (type_a->nfcid_len > NFC_NFCID1_MAXSIZE)
 702                return false;
 703
 704        return true;
 705}
 706
 707static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
 708                                                        int tgt_data_len)
 709{
 710        struct pn533_target_type_a *tgt_type_a;
 711
 712        tgt_type_a = (struct pn533_target_type_a *)tgt_data;
 713
 714        if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
 715                return -EPROTO;
 716
 717        switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
 718        case PN533_TYPE_A_SEL_PROT_MIFARE:
 719                nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
 720                break;
 721        case PN533_TYPE_A_SEL_PROT_ISO14443:
 722                nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
 723                break;
 724        case PN533_TYPE_A_SEL_PROT_DEP:
 725                nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
 726                break;
 727        case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
 728                nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
 729                                                        NFC_PROTO_NFC_DEP_MASK;
 730                break;
 731        }
 732
 733        nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
 734        nfc_tgt->sel_res = tgt_type_a->sel_res;
 735        nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
 736        memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
 737
 738        return 0;
 739}
 740
 741struct pn533_target_felica {
 742        u8 pol_res;
 743        u8 opcode;
 744        u8 nfcid2[NFC_NFCID2_MAXSIZE];
 745        u8 pad[8];
 746        /* optional */
 747        u8 syst_code[];
 748} __packed;
 749
 750#define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
 751#define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
 752
 753static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
 754                                                        int target_data_len)
 755{
 756        if (target_data_len < sizeof(struct pn533_target_felica))
 757                return false;
 758
 759        if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
 760                return false;
 761
 762        return true;
 763}
 764
 765static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
 766                                                        int tgt_data_len)
 767{
 768        struct pn533_target_felica *tgt_felica;
 769
 770        tgt_felica = (struct pn533_target_felica *)tgt_data;
 771
 772        if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
 773                return -EPROTO;
 774
 775        if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
 776            (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
 777                nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
 778        else
 779                nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
 780
 781        memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
 782        nfc_tgt->sensf_res_len = 9;
 783
 784        memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
 785        nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
 786
 787        return 0;
 788}
 789
 790struct pn533_target_jewel {
 791        __be16 sens_res;
 792        u8 jewelid[4];
 793} __packed;
 794
 795static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
 796                                                        int target_data_len)
 797{
 798        u8 ssd;
 799        u8 platconf;
 800
 801        if (target_data_len < sizeof(struct pn533_target_jewel))
 802                return false;
 803
 804        /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
 805        ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
 806        platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
 807
 808        if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
 809             platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
 810            (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
 811             platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
 812                return false;
 813
 814        return true;
 815}
 816
 817static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
 818                                                        int tgt_data_len)
 819{
 820        struct pn533_target_jewel *tgt_jewel;
 821
 822        tgt_jewel = (struct pn533_target_jewel *)tgt_data;
 823
 824        if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
 825                return -EPROTO;
 826
 827        nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
 828        nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
 829        nfc_tgt->nfcid1_len = 4;
 830        memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
 831
 832        return 0;
 833}
 834
 835struct pn533_type_b_prot_info {
 836        u8 bitrate;
 837        u8 fsci_type;
 838        u8 fwi_adc_fo;
 839} __packed;
 840
 841#define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
 842#define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
 843#define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
 844
 845struct pn533_type_b_sens_res {
 846        u8 opcode;
 847        u8 nfcid[4];
 848        u8 appdata[4];
 849        struct pn533_type_b_prot_info prot_info;
 850} __packed;
 851
 852#define PN533_TYPE_B_OPC_SENSB_RES 0x50
 853
 854struct pn533_target_type_b {
 855        struct pn533_type_b_sens_res sensb_res;
 856        u8 attrib_res_len;
 857        u8 attrib_res[];
 858} __packed;
 859
 860static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
 861                                                        int target_data_len)
 862{
 863        if (target_data_len < sizeof(struct pn533_target_type_b))
 864                return false;
 865
 866        if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
 867                return false;
 868
 869        if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
 870                                                PN533_TYPE_B_PROT_TYPE_RFU_MASK)
 871                return false;
 872
 873        return true;
 874}
 875
 876static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
 877                                                        int tgt_data_len)
 878{
 879        struct pn533_target_type_b *tgt_type_b;
 880
 881        tgt_type_b = (struct pn533_target_type_b *)tgt_data;
 882
 883        if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
 884                return -EPROTO;
 885
 886        nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
 887
 888        return 0;
 889}
 890
 891static void pn533_poll_reset_mod_list(struct pn533 *dev);
 892static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
 893                              int tgdata_len)
 894{
 895        struct nfc_target nfc_tgt;
 896        int rc;
 897
 898        dev_dbg(dev->dev, "%s: modulation=%d\n",
 899                __func__, dev->poll_mod_curr);
 900
 901        if (tg != 1)
 902                return -EPROTO;
 903
 904        memset(&nfc_tgt, 0, sizeof(struct nfc_target));
 905
 906        switch (dev->poll_mod_curr) {
 907        case PN533_POLL_MOD_106KBPS_A:
 908                rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
 909                break;
 910        case PN533_POLL_MOD_212KBPS_FELICA:
 911        case PN533_POLL_MOD_424KBPS_FELICA:
 912                rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
 913                break;
 914        case PN533_POLL_MOD_106KBPS_JEWEL:
 915                rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
 916                break;
 917        case PN533_POLL_MOD_847KBPS_B:
 918                rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
 919                break;
 920        default:
 921                nfc_err(dev->dev,
 922                        "Unknown current poll modulation\n");
 923                return -EPROTO;
 924        }
 925
 926        if (rc)
 927                return rc;
 928
 929        if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
 930                dev_dbg(dev->dev,
 931                        "The Tg found doesn't have the desired protocol\n");
 932                return -EAGAIN;
 933        }
 934
 935        dev_dbg(dev->dev,
 936                "Target found - supported protocols: 0x%x\n",
 937                nfc_tgt.supported_protocols);
 938
 939        dev->tgt_available_prots = nfc_tgt.supported_protocols;
 940
 941        pn533_poll_reset_mod_list(dev);
 942        nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
 943
 944        return 0;
 945}
 946
 947static inline void pn533_poll_next_mod(struct pn533 *dev)
 948{
 949        dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
 950}
 951
 952static void pn533_poll_reset_mod_list(struct pn533 *dev)
 953{
 954        dev->poll_mod_count = 0;
 955}
 956
 957static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
 958{
 959        dev->poll_mod_active[dev->poll_mod_count] =
 960                (struct pn533_poll_modulations *)&poll_mod[mod_index];
 961        dev->poll_mod_count++;
 962}
 963
 964static void pn533_poll_create_mod_list(struct pn533 *dev,
 965                                       u32 im_protocols, u32 tm_protocols)
 966{
 967        pn533_poll_reset_mod_list(dev);
 968
 969        if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
 970            (im_protocols & NFC_PROTO_ISO14443_MASK) ||
 971            (im_protocols & NFC_PROTO_NFC_DEP_MASK))
 972                pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
 973
 974        if (im_protocols & NFC_PROTO_FELICA_MASK ||
 975            im_protocols & NFC_PROTO_NFC_DEP_MASK) {
 976                pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
 977                pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
 978        }
 979
 980        if (im_protocols & NFC_PROTO_JEWEL_MASK)
 981                pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
 982
 983        if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
 984                pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
 985
 986        if (tm_protocols)
 987                pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
 988}
 989
 990static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
 991{
 992        u8 nbtg, tg, *tgdata;
 993        int rc, tgdata_len;
 994
 995        /* Toggle the DEP polling */
 996        if (dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK)
 997                dev->poll_dep = 1;
 998
 999        nbtg = resp->data[0];
1000        tg = resp->data[1];
1001        tgdata = &resp->data[2];
1002        tgdata_len = resp->len - 2;  /* nbtg + tg */
1003
1004        if (nbtg) {
1005                rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1006
1007                /* We must stop the poll after a valid target found */
1008                if (rc == 0)
1009                        return 0;
1010        }
1011
1012        return -EAGAIN;
1013}
1014
1015static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1016{
1017        struct sk_buff *skb;
1018        u8 *felica, *nfcid3;
1019
1020        u8 *gbytes = dev->gb;
1021        size_t gbytes_len = dev->gb_len;
1022
1023        u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1024                                0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1025                                0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1026                                0xff, 0xff}; /* System code */
1027
1028        u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1029                               0x0, 0x0, 0x0,
1030                               0x40}; /* SEL_RES for DEP */
1031
1032        unsigned int skb_len = 36 + /*
1033                                     * mode (1), mifare (6),
1034                                     * felica (18), nfcid3 (10), gb_len (1)
1035                                     */
1036                               gbytes_len +
1037                               1;  /* len Tk*/
1038
1039        skb = pn533_alloc_skb(dev, skb_len);
1040        if (!skb)
1041                return NULL;
1042
1043        /* DEP support only */
1044        skb_put_u8(skb, PN533_INIT_TARGET_DEP);
1045
1046        /* MIFARE params */
1047        skb_put_data(skb, mifare_params, 6);
1048
1049        /* Felica params */
1050        felica = skb_put_data(skb, felica_params, 18);
1051        get_random_bytes(felica + 2, 6);
1052
1053        /* NFCID3 */
1054        nfcid3 = skb_put_zero(skb, 10);
1055        memcpy(nfcid3, felica, 8);
1056
1057        /* General bytes */
1058        skb_put_u8(skb, gbytes_len);
1059
1060        skb_put_data(skb, gbytes, gbytes_len);
1061
1062        /* Len Tk */
1063        skb_put_u8(skb, 0);
1064
1065        return skb;
1066}
1067
1068static void pn533_wq_tm_mi_recv(struct work_struct *work);
1069static struct sk_buff *pn533_build_response(struct pn533 *dev);
1070
1071static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1072                                      struct sk_buff *resp)
1073{
1074        struct sk_buff *skb;
1075        u8 status, ret, mi;
1076        int rc;
1077
1078        dev_dbg(dev->dev, "%s\n", __func__);
1079
1080        if (IS_ERR(resp)) {
1081                skb_queue_purge(&dev->resp_q);
1082                return PTR_ERR(resp);
1083        }
1084
1085        status = resp->data[0];
1086
1087        ret = status & PN533_CMD_RET_MASK;
1088        mi = status & PN533_CMD_MI_MASK;
1089
1090        skb_pull(resp, sizeof(status));
1091
1092        if (ret != PN533_CMD_RET_SUCCESS) {
1093                rc = -EIO;
1094                goto error;
1095        }
1096
1097        skb_queue_tail(&dev->resp_q, resp);
1098
1099        if (mi) {
1100                queue_work(dev->wq, &dev->mi_tm_rx_work);
1101                return -EINPROGRESS;
1102        }
1103
1104        skb = pn533_build_response(dev);
1105        if (!skb) {
1106                rc = -EIO;
1107                goto error;
1108        }
1109
1110        return nfc_tm_data_received(dev->nfc_dev, skb);
1111
1112error:
1113        nfc_tm_deactivated(dev->nfc_dev);
1114        dev->tgt_mode = 0;
1115        skb_queue_purge(&dev->resp_q);
1116        dev_kfree_skb(resp);
1117
1118        return rc;
1119}
1120
1121static void pn533_wq_tm_mi_recv(struct work_struct *work)
1122{
1123        struct pn533 *dev = container_of(work, struct pn533, mi_tm_rx_work);
1124        struct sk_buff *skb;
1125        int rc;
1126
1127        dev_dbg(dev->dev, "%s\n", __func__);
1128
1129        skb = pn533_alloc_skb(dev, 0);
1130        if (!skb)
1131                return;
1132
1133        rc = pn533_send_cmd_direct_async(dev,
1134                                        PN533_CMD_TG_GET_DATA,
1135                                        skb,
1136                                        pn533_tm_get_data_complete,
1137                                        NULL);
1138
1139        if (rc < 0)
1140                dev_kfree_skb(skb);
1141}
1142
1143static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
1144                                  struct sk_buff *resp);
1145static void pn533_wq_tm_mi_send(struct work_struct *work)
1146{
1147        struct pn533 *dev = container_of(work, struct pn533, mi_tm_tx_work);
1148        struct sk_buff *skb;
1149        int rc;
1150
1151        dev_dbg(dev->dev, "%s\n", __func__);
1152
1153        /* Grab the first skb in the queue */
1154        skb = skb_dequeue(&dev->fragment_skb);
1155        if (skb == NULL) {      /* No more data */
1156                /* Reset the queue for future use */
1157                skb_queue_head_init(&dev->fragment_skb);
1158                goto error;
1159        }
1160
1161        /* last entry - remove MI bit */
1162        if (skb_queue_len(&dev->fragment_skb) == 0) {
1163                rc = pn533_send_cmd_direct_async(dev, PN533_CMD_TG_SET_DATA,
1164                                        skb, pn533_tm_send_complete, NULL);
1165        } else
1166                rc = pn533_send_cmd_direct_async(dev,
1167                                        PN533_CMD_TG_SET_META_DATA,
1168                                        skb, pn533_tm_send_complete, NULL);
1169
1170        if (rc == 0) /* success */
1171                return;
1172
1173        dev_err(dev->dev,
1174                "Error %d when trying to perform set meta data_exchange", rc);
1175
1176        dev_kfree_skb(skb);
1177
1178error:
1179        dev->phy_ops->send_ack(dev, GFP_KERNEL);
1180        queue_work(dev->wq, &dev->cmd_work);
1181}
1182
1183static void pn533_wq_tg_get_data(struct work_struct *work)
1184{
1185        struct pn533 *dev = container_of(work, struct pn533, tg_work);
1186        struct sk_buff *skb;
1187        int rc;
1188
1189        dev_dbg(dev->dev, "%s\n", __func__);
1190
1191        skb = pn533_alloc_skb(dev, 0);
1192        if (!skb)
1193                return;
1194
1195        rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1196                                   pn533_tm_get_data_complete, NULL);
1197
1198        if (rc < 0)
1199                dev_kfree_skb(skb);
1200}
1201
1202#define ATR_REQ_GB_OFFSET 17
1203static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1204{
1205        u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1206        size_t gb_len;
1207        int rc;
1208
1209        dev_dbg(dev->dev, "%s\n", __func__);
1210
1211        if (resp->len < ATR_REQ_GB_OFFSET + 1)
1212                return -EINVAL;
1213
1214        mode = resp->data[0];
1215        cmd = &resp->data[1];
1216
1217        dev_dbg(dev->dev, "Target mode 0x%x len %d\n",
1218                mode, resp->len);
1219
1220        if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1221            PN533_INIT_TARGET_RESP_ACTIVE)
1222                comm_mode = NFC_COMM_ACTIVE;
1223
1224        if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0)  /* Only DEP supported */
1225                return -EOPNOTSUPP;
1226
1227        gb = cmd + ATR_REQ_GB_OFFSET;
1228        gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1229
1230        rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1231                              comm_mode, gb, gb_len);
1232        if (rc < 0) {
1233                nfc_err(dev->dev,
1234                        "Error when signaling target activation\n");
1235                return rc;
1236        }
1237
1238        dev->tgt_mode = 1;
1239        queue_work(dev->wq, &dev->tg_work);
1240
1241        return 0;
1242}
1243
1244static void pn533_listen_mode_timer(struct timer_list *t)
1245{
1246        struct pn533 *dev = from_timer(dev, t, listen_timer);
1247
1248        dev_dbg(dev->dev, "Listen mode timeout\n");
1249
1250        dev->cancel_listen = 1;
1251
1252        pn533_poll_next_mod(dev);
1253
1254        queue_delayed_work(dev->wq, &dev->poll_work,
1255                           msecs_to_jiffies(PN533_POLL_INTERVAL));
1256}
1257
1258static int pn533_rf_complete(struct pn533 *dev, void *arg,
1259                             struct sk_buff *resp)
1260{
1261        int rc = 0;
1262
1263        dev_dbg(dev->dev, "%s\n", __func__);
1264
1265        if (IS_ERR(resp)) {
1266                rc = PTR_ERR(resp);
1267
1268                nfc_err(dev->dev, "RF setting error %d\n", rc);
1269
1270                return rc;
1271        }
1272
1273        queue_delayed_work(dev->wq, &dev->poll_work,
1274                           msecs_to_jiffies(PN533_POLL_INTERVAL));
1275
1276        dev_kfree_skb(resp);
1277        return rc;
1278}
1279
1280static void pn533_wq_rf(struct work_struct *work)
1281{
1282        struct pn533 *dev = container_of(work, struct pn533, rf_work);
1283        struct sk_buff *skb;
1284        int rc;
1285
1286        dev_dbg(dev->dev, "%s\n", __func__);
1287
1288        skb = pn533_alloc_skb(dev, 2);
1289        if (!skb)
1290                return;
1291
1292        skb_put_u8(skb, PN533_CFGITEM_RF_FIELD);
1293        skb_put_u8(skb, PN533_CFGITEM_RF_FIELD_AUTO_RFCA);
1294
1295        rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1296                                  pn533_rf_complete, NULL);
1297        if (rc < 0) {
1298                dev_kfree_skb(skb);
1299                nfc_err(dev->dev, "RF setting error %d\n", rc);
1300        }
1301}
1302
1303static int pn533_poll_dep_complete(struct pn533 *dev, void *arg,
1304                                   struct sk_buff *resp)
1305{
1306        struct pn533_cmd_jump_dep_response *rsp;
1307        struct nfc_target nfc_target;
1308        u8 target_gt_len;
1309        int rc;
1310
1311        if (IS_ERR(resp))
1312                return PTR_ERR(resp);
1313
1314        rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1315
1316        rc = rsp->status & PN533_CMD_RET_MASK;
1317        if (rc != PN533_CMD_RET_SUCCESS) {
1318                /* Not target found, turn radio off */
1319                queue_work(dev->wq, &dev->rf_work);
1320
1321                dev_kfree_skb(resp);
1322                return 0;
1323        }
1324
1325        dev_dbg(dev->dev, "Creating new target");
1326
1327        nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1328        nfc_target.nfcid1_len = 10;
1329        memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1330        rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1331        if (rc)
1332                goto error;
1333
1334        dev->tgt_available_prots = 0;
1335        dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1336
1337        /* ATR_RES general bytes are located at offset 17 */
1338        target_gt_len = resp->len - 17;
1339        rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1340                                          rsp->gt, target_gt_len);
1341        if (!rc) {
1342                rc = nfc_dep_link_is_up(dev->nfc_dev,
1343                                        dev->nfc_dev->targets[0].idx,
1344                                        0, NFC_RF_INITIATOR);
1345
1346                if (!rc)
1347                        pn533_poll_reset_mod_list(dev);
1348        }
1349error:
1350        dev_kfree_skb(resp);
1351        return rc;
1352}
1353
1354#define PASSIVE_DATA_LEN 5
1355static int pn533_poll_dep(struct nfc_dev *nfc_dev)
1356{
1357        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1358        struct sk_buff *skb;
1359        int rc, skb_len;
1360        u8 *next, nfcid3[NFC_NFCID3_MAXSIZE];
1361        u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1362
1363        dev_dbg(dev->dev, "%s", __func__);
1364
1365        if (!dev->gb) {
1366                dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1367
1368                if (!dev->gb || !dev->gb_len) {
1369                        dev->poll_dep = 0;
1370                        queue_work(dev->wq, &dev->rf_work);
1371                }
1372        }
1373
1374        skb_len = 3 + dev->gb_len; /* ActPass + BR + Next */
1375        skb_len += PASSIVE_DATA_LEN;
1376
1377        /* NFCID3 */
1378        skb_len += NFC_NFCID3_MAXSIZE;
1379        nfcid3[0] = 0x1;
1380        nfcid3[1] = 0xfe;
1381        get_random_bytes(nfcid3 + 2, 6);
1382
1383        skb = pn533_alloc_skb(dev, skb_len);
1384        if (!skb)
1385                return -ENOMEM;
1386
1387        skb_put_u8(skb, 0x01);  /* Active */
1388        skb_put_u8(skb, 0x02);  /* 424 kbps */
1389
1390        next = skb_put(skb, 1);  /* Next */
1391        *next = 0;
1392
1393        /* Copy passive data */
1394        skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
1395        *next |= 1;
1396
1397        /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
1398        skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
1399        *next |= 2;
1400
1401        skb_put_data(skb, dev->gb, dev->gb_len);
1402        *next |= 4; /* We have some Gi */
1403
1404        rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1405                                  pn533_poll_dep_complete, NULL);
1406
1407        if (rc < 0)
1408                dev_kfree_skb(skb);
1409
1410        return rc;
1411}
1412
1413static int pn533_autopoll_complete(struct pn533 *dev, void *arg,
1414                               struct sk_buff *resp)
1415{
1416        struct pn532_autopoll_resp *apr;
1417        struct nfc_target nfc_tgt;
1418        u8 nbtg;
1419        int rc;
1420
1421        if (IS_ERR(resp)) {
1422                rc = PTR_ERR(resp);
1423
1424                nfc_err(dev->dev, "%s  autopoll complete error %d\n",
1425                        __func__, rc);
1426
1427                if (rc == -ENOENT) {
1428                        if (dev->poll_mod_count != 0)
1429                                return rc;
1430                        goto stop_poll;
1431                } else if (rc < 0) {
1432                        nfc_err(dev->dev,
1433                                "Error %d when running autopoll\n", rc);
1434                        goto stop_poll;
1435                }
1436        }
1437
1438        nbtg = resp->data[0];
1439        if ((nbtg > 2) || (nbtg <= 0))
1440                return -EAGAIN;
1441
1442        apr = (struct pn532_autopoll_resp *)&resp->data[1];
1443        while (nbtg--) {
1444                memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1445                switch (apr->type) {
1446                case PN532_AUTOPOLL_TYPE_ISOA:
1447                        dev_dbg(dev->dev, "ISOA\n");
1448                        rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata,
1449                                                       apr->ln - 1);
1450                        break;
1451                case PN532_AUTOPOLL_TYPE_FELICA212:
1452                case PN532_AUTOPOLL_TYPE_FELICA424:
1453                        dev_dbg(dev->dev, "FELICA\n");
1454                        rc = pn533_target_found_felica(&nfc_tgt, apr->tgdata,
1455                                                       apr->ln - 1);
1456                        break;
1457                case PN532_AUTOPOLL_TYPE_JEWEL:
1458                        dev_dbg(dev->dev, "JEWEL\n");
1459                        rc = pn533_target_found_jewel(&nfc_tgt, apr->tgdata,
1460                                                      apr->ln - 1);
1461                        break;
1462                case PN532_AUTOPOLL_TYPE_ISOB:
1463                        dev_dbg(dev->dev, "ISOB\n");
1464                        rc = pn533_target_found_type_b(&nfc_tgt, apr->tgdata,
1465                                                       apr->ln - 1);
1466                        break;
1467                case PN532_AUTOPOLL_TYPE_MIFARE:
1468                        dev_dbg(dev->dev, "Mifare\n");
1469                        rc = pn533_target_found_type_a(&nfc_tgt, apr->tgdata,
1470                                                       apr->ln - 1);
1471                        break;
1472                default:
1473                        nfc_err(dev->dev,
1474                                    "Unknown current poll modulation\n");
1475                        rc = -EPROTO;
1476                }
1477
1478                if (rc)
1479                        goto done;
1480
1481                if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1482                        nfc_err(dev->dev,
1483                                    "The Tg found doesn't have the desired protocol\n");
1484                        rc = -EAGAIN;
1485                        goto done;
1486                }
1487
1488                dev->tgt_available_prots = nfc_tgt.supported_protocols;
1489                apr = (struct pn532_autopoll_resp *)
1490                        (apr->tgdata + (apr->ln - 1));
1491        }
1492
1493        pn533_poll_reset_mod_list(dev);
1494        nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1495
1496done:
1497        dev_kfree_skb(resp);
1498        return rc;
1499
1500stop_poll:
1501        nfc_err(dev->dev, "autopoll operation has been stopped\n");
1502
1503        pn533_poll_reset_mod_list(dev);
1504        dev->poll_protocols = 0;
1505        return rc;
1506}
1507
1508static int pn533_poll_complete(struct pn533 *dev, void *arg,
1509                               struct sk_buff *resp)
1510{
1511        struct pn533_poll_modulations *cur_mod;
1512        int rc;
1513
1514        dev_dbg(dev->dev, "%s\n", __func__);
1515
1516        if (IS_ERR(resp)) {
1517                rc = PTR_ERR(resp);
1518
1519                nfc_err(dev->dev, "%s  Poll complete error %d\n",
1520                        __func__, rc);
1521
1522                if (rc == -ENOENT) {
1523                        if (dev->poll_mod_count != 0)
1524                                return rc;
1525                        goto stop_poll;
1526                } else if (rc < 0) {
1527                        nfc_err(dev->dev,
1528                                "Error %d when running poll\n", rc);
1529                        goto stop_poll;
1530                }
1531        }
1532
1533        cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1534
1535        if (cur_mod->len == 0) { /* Target mode */
1536                del_timer(&dev->listen_timer);
1537                rc = pn533_init_target_complete(dev, resp);
1538                goto done;
1539        }
1540
1541        /* Initiator mode */
1542        rc = pn533_start_poll_complete(dev, resp);
1543        if (!rc)
1544                goto done;
1545
1546        if (!dev->poll_mod_count) {
1547                dev_dbg(dev->dev, "Polling has been stopped\n");
1548                goto done;
1549        }
1550
1551        pn533_poll_next_mod(dev);
1552        /* Not target found, turn radio off */
1553        queue_work(dev->wq, &dev->rf_work);
1554
1555done:
1556        dev_kfree_skb(resp);
1557        return rc;
1558
1559stop_poll:
1560        nfc_err(dev->dev, "Polling operation has been stopped\n");
1561
1562        pn533_poll_reset_mod_list(dev);
1563        dev->poll_protocols = 0;
1564        return rc;
1565}
1566
1567static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1568                                        struct pn533_poll_modulations *mod)
1569{
1570        struct sk_buff *skb;
1571
1572        skb = pn533_alloc_skb(dev, mod->len);
1573        if (!skb)
1574                return NULL;
1575
1576        skb_put_data(skb, &mod->data, mod->len);
1577
1578        return skb;
1579}
1580
1581static int pn533_send_poll_frame(struct pn533 *dev)
1582{
1583        struct pn533_poll_modulations *mod;
1584        struct sk_buff *skb;
1585        int rc;
1586        u8 cmd_code;
1587
1588        mod = dev->poll_mod_active[dev->poll_mod_curr];
1589
1590        dev_dbg(dev->dev, "%s mod len %d\n",
1591                __func__, mod->len);
1592
1593        if ((dev->poll_protocols & NFC_PROTO_NFC_DEP_MASK) && dev->poll_dep)  {
1594                dev->poll_dep = 0;
1595                return pn533_poll_dep(dev->nfc_dev);
1596        }
1597
1598        if (mod->len == 0) {  /* Listen mode */
1599                cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1600                skb = pn533_alloc_poll_tg_frame(dev);
1601        } else {  /* Polling mode */
1602                cmd_code =  PN533_CMD_IN_LIST_PASSIVE_TARGET;
1603                skb = pn533_alloc_poll_in_frame(dev, mod);
1604        }
1605
1606        if (!skb) {
1607                nfc_err(dev->dev, "Failed to allocate skb\n");
1608                return -ENOMEM;
1609        }
1610
1611        rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1612                                  NULL);
1613        if (rc < 0) {
1614                dev_kfree_skb(skb);
1615                nfc_err(dev->dev, "Polling loop error %d\n", rc);
1616        }
1617
1618        return rc;
1619}
1620
1621static void pn533_wq_poll(struct work_struct *work)
1622{
1623        struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
1624        struct pn533_poll_modulations *cur_mod;
1625        int rc;
1626
1627        cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1628
1629        dev_dbg(dev->dev,
1630                "%s cancel_listen %d modulation len %d\n",
1631                __func__, dev->cancel_listen, cur_mod->len);
1632
1633        if (dev->cancel_listen == 1) {
1634                dev->cancel_listen = 0;
1635                dev->phy_ops->abort_cmd(dev, GFP_ATOMIC);
1636        }
1637
1638        rc = pn533_send_poll_frame(dev);
1639        if (rc)
1640                return;
1641
1642        if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1643                mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1644}
1645
1646static int pn533_start_poll(struct nfc_dev *nfc_dev,
1647                            u32 im_protocols, u32 tm_protocols)
1648{
1649        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1650        struct pn533_poll_modulations *cur_mod;
1651        struct sk_buff *skb;
1652        u8 rand_mod;
1653        int rc;
1654
1655        dev_dbg(dev->dev,
1656                "%s: im protocols 0x%x tm protocols 0x%x\n",
1657                __func__, im_protocols, tm_protocols);
1658
1659        if (dev->tgt_active_prot) {
1660                nfc_err(dev->dev,
1661                        "Cannot poll with a target already activated\n");
1662                return -EBUSY;
1663        }
1664
1665        if (dev->tgt_mode) {
1666                nfc_err(dev->dev,
1667                        "Cannot poll while already being activated\n");
1668                return -EBUSY;
1669        }
1670
1671        if (tm_protocols) {
1672                dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1673                if (dev->gb == NULL)
1674                        tm_protocols = 0;
1675        }
1676
1677        dev->poll_protocols = im_protocols;
1678        dev->listen_protocols = tm_protocols;
1679        if (dev->device_type == PN533_DEVICE_PN532_AUTOPOLL) {
1680                skb = pn533_alloc_skb(dev, 4 + 6);
1681                if (!skb)
1682                        return -ENOMEM;
1683
1684                *((u8 *)skb_put(skb, sizeof(u8))) =
1685                        PN532_AUTOPOLL_POLLNR_INFINITE;
1686                *((u8 *)skb_put(skb, sizeof(u8))) = PN532_AUTOPOLL_PERIOD;
1687
1688                if ((im_protocols & NFC_PROTO_MIFARE_MASK) &&
1689                                (im_protocols & NFC_PROTO_ISO14443_MASK) &&
1690                                (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1691                        *((u8 *)skb_put(skb, sizeof(u8))) =
1692                                PN532_AUTOPOLL_TYPE_GENERIC_106;
1693                else {
1694                        if (im_protocols & NFC_PROTO_MIFARE_MASK)
1695                                *((u8 *)skb_put(skb, sizeof(u8))) =
1696                                        PN532_AUTOPOLL_TYPE_MIFARE;
1697
1698                        if (im_protocols & NFC_PROTO_ISO14443_MASK)
1699                                *((u8 *)skb_put(skb, sizeof(u8))) =
1700                                        PN532_AUTOPOLL_TYPE_ISOA;
1701
1702                        if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1703                                *((u8 *)skb_put(skb, sizeof(u8))) =
1704                                        PN532_AUTOPOLL_TYPE_DEP_PASSIVE_106;
1705                                *((u8 *)skb_put(skb, sizeof(u8))) =
1706                                        PN532_AUTOPOLL_TYPE_DEP_PASSIVE_212;
1707                                *((u8 *)skb_put(skb, sizeof(u8))) =
1708                                        PN532_AUTOPOLL_TYPE_DEP_PASSIVE_424;
1709                        }
1710                }
1711
1712                if (im_protocols & NFC_PROTO_FELICA_MASK ||
1713                                im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1714                        *((u8 *)skb_put(skb, sizeof(u8))) =
1715                                PN532_AUTOPOLL_TYPE_FELICA212;
1716                        *((u8 *)skb_put(skb, sizeof(u8))) =
1717                                PN532_AUTOPOLL_TYPE_FELICA424;
1718                }
1719
1720                if (im_protocols & NFC_PROTO_JEWEL_MASK)
1721                        *((u8 *)skb_put(skb, sizeof(u8))) =
1722                                PN532_AUTOPOLL_TYPE_JEWEL;
1723
1724                if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1725                        *((u8 *)skb_put(skb, sizeof(u8))) =
1726                                PN532_AUTOPOLL_TYPE_ISOB;
1727
1728                if (tm_protocols)
1729                        *((u8 *)skb_put(skb, sizeof(u8))) =
1730                                PN532_AUTOPOLL_TYPE_DEP_ACTIVE_106;
1731
1732                rc = pn533_send_cmd_async(dev, PN533_CMD_IN_AUTOPOLL, skb,
1733                                pn533_autopoll_complete, NULL);
1734
1735                if (rc < 0)
1736                        dev_kfree_skb(skb);
1737                else
1738                        dev->poll_mod_count++;
1739
1740                return rc;
1741        }
1742
1743        pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1744
1745        /* Do not always start polling from the same modulation */
1746        get_random_bytes(&rand_mod, sizeof(rand_mod));
1747        rand_mod %= dev->poll_mod_count;
1748        dev->poll_mod_curr = rand_mod;
1749
1750        cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1751
1752        rc = pn533_send_poll_frame(dev);
1753
1754        /* Start listen timer */
1755        if (!rc && cur_mod->len == 0 && dev->poll_mod_count > 1)
1756                mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1757
1758        return rc;
1759}
1760
1761static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1762{
1763        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1764
1765        del_timer(&dev->listen_timer);
1766
1767        if (!dev->poll_mod_count) {
1768                dev_dbg(dev->dev,
1769                        "Polling operation was not running\n");
1770                return;
1771        }
1772
1773        dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
1774        flush_delayed_work(&dev->poll_work);
1775        pn533_poll_reset_mod_list(dev);
1776}
1777
1778static int pn533_activate_target_nfcdep(struct pn533 *dev)
1779{
1780        struct pn533_cmd_activate_response *rsp;
1781        u16 gt_len;
1782        int rc;
1783        struct sk_buff *skb;
1784        struct sk_buff *resp;
1785
1786        dev_dbg(dev->dev, "%s\n", __func__);
1787
1788        skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1789        if (!skb)
1790                return -ENOMEM;
1791
1792        skb_put_u8(skb, 1); /* TG */
1793        skb_put_u8(skb, 0); /* Next */
1794
1795        resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1796        if (IS_ERR(resp))
1797                return PTR_ERR(resp);
1798
1799        rsp = (struct pn533_cmd_activate_response *)resp->data;
1800        rc = rsp->status & PN533_CMD_RET_MASK;
1801        if (rc != PN533_CMD_RET_SUCCESS) {
1802                nfc_err(dev->dev,
1803                        "Target activation failed (error 0x%x)\n", rc);
1804                dev_kfree_skb(resp);
1805                return -EIO;
1806        }
1807
1808        /* ATR_RES general bytes are located at offset 16 */
1809        gt_len = resp->len - 16;
1810        rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1811
1812        dev_kfree_skb(resp);
1813        return rc;
1814}
1815
1816static int pn533_activate_target(struct nfc_dev *nfc_dev,
1817                                 struct nfc_target *target, u32 protocol)
1818{
1819        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1820        int rc;
1821
1822        dev_dbg(dev->dev, "%s: protocol=%u\n", __func__, protocol);
1823
1824        if (dev->poll_mod_count) {
1825                nfc_err(dev->dev,
1826                        "Cannot activate while polling\n");
1827                return -EBUSY;
1828        }
1829
1830        if (dev->tgt_active_prot) {
1831                nfc_err(dev->dev,
1832                        "There is already an active target\n");
1833                return -EBUSY;
1834        }
1835
1836        if (!dev->tgt_available_prots) {
1837                nfc_err(dev->dev,
1838                        "There is no available target to activate\n");
1839                return -EINVAL;
1840        }
1841
1842        if (!(dev->tgt_available_prots & (1 << protocol))) {
1843                nfc_err(dev->dev,
1844                        "Target doesn't support requested proto %u\n",
1845                        protocol);
1846                return -EINVAL;
1847        }
1848
1849        if (protocol == NFC_PROTO_NFC_DEP) {
1850                rc = pn533_activate_target_nfcdep(dev);
1851                if (rc) {
1852                        nfc_err(dev->dev,
1853                                "Activating target with DEP failed %d\n", rc);
1854                        return rc;
1855                }
1856        }
1857
1858        dev->tgt_active_prot = protocol;
1859        dev->tgt_available_prots = 0;
1860
1861        return 0;
1862}
1863
1864static int pn533_deactivate_target_complete(struct pn533 *dev, void *arg,
1865                             struct sk_buff *resp)
1866{
1867        int rc = 0;
1868
1869        dev_dbg(dev->dev, "%s\n", __func__);
1870
1871        if (IS_ERR(resp)) {
1872                rc = PTR_ERR(resp);
1873
1874                nfc_err(dev->dev, "Target release error %d\n", rc);
1875
1876                return rc;
1877        }
1878
1879        rc = resp->data[0] & PN533_CMD_RET_MASK;
1880        if (rc != PN533_CMD_RET_SUCCESS)
1881                nfc_err(dev->dev,
1882                        "Error 0x%x when releasing the target\n", rc);
1883
1884        dev_kfree_skb(resp);
1885        return rc;
1886}
1887
1888static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1889                                    struct nfc_target *target, u8 mode)
1890{
1891        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1892        struct sk_buff *skb;
1893        int rc;
1894
1895        dev_dbg(dev->dev, "%s\n", __func__);
1896
1897        if (!dev->tgt_active_prot) {
1898                nfc_err(dev->dev, "There is no active target\n");
1899                return;
1900        }
1901
1902        dev->tgt_active_prot = 0;
1903        skb_queue_purge(&dev->resp_q);
1904
1905        skb = pn533_alloc_skb(dev, sizeof(u8));
1906        if (!skb)
1907                return;
1908
1909        skb_put_u8(skb, 1); /* TG*/
1910
1911        rc = pn533_send_cmd_async(dev, PN533_CMD_IN_RELEASE, skb,
1912                                  pn533_deactivate_target_complete, NULL);
1913        if (rc < 0) {
1914                dev_kfree_skb(skb);
1915                nfc_err(dev->dev, "Target release error %d\n", rc);
1916        }
1917}
1918
1919
1920static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1921                                         struct sk_buff *resp)
1922{
1923        struct pn533_cmd_jump_dep_response *rsp;
1924        u8 target_gt_len;
1925        int rc;
1926        u8 active = *(u8 *)arg;
1927
1928        kfree(arg);
1929
1930        if (IS_ERR(resp))
1931                return PTR_ERR(resp);
1932
1933        if (dev->tgt_available_prots &&
1934            !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1935                nfc_err(dev->dev,
1936                        "The target does not support DEP\n");
1937                rc =  -EINVAL;
1938                goto error;
1939        }
1940
1941        rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1942
1943        rc = rsp->status & PN533_CMD_RET_MASK;
1944        if (rc != PN533_CMD_RET_SUCCESS) {
1945                nfc_err(dev->dev,
1946                        "Bringing DEP link up failed (error 0x%x)\n", rc);
1947                goto error;
1948        }
1949
1950        if (!dev->tgt_available_prots) {
1951                struct nfc_target nfc_target;
1952
1953                dev_dbg(dev->dev, "Creating new target\n");
1954
1955                nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1956                nfc_target.nfcid1_len = 10;
1957                memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1958                rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1959                if (rc)
1960                        goto error;
1961
1962                dev->tgt_available_prots = 0;
1963        }
1964
1965        dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1966
1967        /* ATR_RES general bytes are located at offset 17 */
1968        target_gt_len = resp->len - 17;
1969        rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1970                                          rsp->gt, target_gt_len);
1971        if (rc == 0)
1972                rc = nfc_dep_link_is_up(dev->nfc_dev,
1973                                        dev->nfc_dev->targets[0].idx,
1974                                        !active, NFC_RF_INITIATOR);
1975
1976error:
1977        dev_kfree_skb(resp);
1978        return rc;
1979}
1980
1981static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
1982static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1983                             u8 comm_mode, u8 *gb, size_t gb_len)
1984{
1985        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1986        struct sk_buff *skb;
1987        int rc, skb_len;
1988        u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
1989        u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1990
1991        dev_dbg(dev->dev, "%s\n", __func__);
1992
1993        if (dev->poll_mod_count) {
1994                nfc_err(dev->dev,
1995                        "Cannot bring the DEP link up while polling\n");
1996                return -EBUSY;
1997        }
1998
1999        if (dev->tgt_active_prot) {
2000                nfc_err(dev->dev,
2001                        "There is already an active target\n");
2002                return -EBUSY;
2003        }
2004
2005        skb_len = 3 + gb_len; /* ActPass + BR + Next */
2006        skb_len += PASSIVE_DATA_LEN;
2007
2008        /* NFCID3 */
2009        skb_len += NFC_NFCID3_MAXSIZE;
2010        if (target && !target->nfcid2_len) {
2011                nfcid3[0] = 0x1;
2012                nfcid3[1] = 0xfe;
2013                get_random_bytes(nfcid3 + 2, 6);
2014        }
2015
2016        skb = pn533_alloc_skb(dev, skb_len);
2017        if (!skb)
2018                return -ENOMEM;
2019
2020        skb_put_u8(skb, !comm_mode);  /* ActPass */
2021        skb_put_u8(skb, 0x02);  /* 424 kbps */
2022
2023        next = skb_put(skb, 1);  /* Next */
2024        *next = 0;
2025
2026        /* Copy passive data */
2027        skb_put_data(skb, passive_data, PASSIVE_DATA_LEN);
2028        *next |= 1;
2029
2030        /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2031        if (target && target->nfcid2_len)
2032                memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2033                       target->nfcid2_len);
2034        else
2035                skb_put_data(skb, nfcid3, NFC_NFCID3_MAXSIZE);
2036        *next |= 2;
2037
2038        if (gb != NULL && gb_len > 0) {
2039                skb_put_data(skb, gb, gb_len);
2040                *next |= 4; /* We have some Gi */
2041        } else {
2042                *next = 0;
2043        }
2044
2045        arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2046        if (!arg) {
2047                dev_kfree_skb(skb);
2048                return -ENOMEM;
2049        }
2050
2051        *arg = !comm_mode;
2052
2053        pn533_rf_field(dev->nfc_dev, 0);
2054
2055        rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2056                                  pn533_in_dep_link_up_complete, arg);
2057
2058        if (rc < 0) {
2059                dev_kfree_skb(skb);
2060                kfree(arg);
2061        }
2062
2063        return rc;
2064}
2065
2066static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2067{
2068        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2069
2070        dev_dbg(dev->dev, "%s\n", __func__);
2071
2072        pn533_poll_reset_mod_list(dev);
2073
2074        if (dev->tgt_mode || dev->tgt_active_prot)
2075                dev->phy_ops->abort_cmd(dev, GFP_KERNEL);
2076
2077        dev->tgt_active_prot = 0;
2078        dev->tgt_mode = 0;
2079
2080        skb_queue_purge(&dev->resp_q);
2081
2082        return 0;
2083}
2084
2085struct pn533_data_exchange_arg {
2086        data_exchange_cb_t cb;
2087        void *cb_context;
2088};
2089
2090static struct sk_buff *pn533_build_response(struct pn533 *dev)
2091{
2092        struct sk_buff *skb, *tmp, *t;
2093        unsigned int skb_len = 0, tmp_len = 0;
2094
2095        dev_dbg(dev->dev, "%s\n", __func__);
2096
2097        if (skb_queue_empty(&dev->resp_q))
2098                return NULL;
2099
2100        if (skb_queue_len(&dev->resp_q) == 1) {
2101                skb = skb_dequeue(&dev->resp_q);
2102                goto out;
2103        }
2104
2105        skb_queue_walk_safe(&dev->resp_q, tmp, t)
2106                skb_len += tmp->len;
2107
2108        dev_dbg(dev->dev, "%s total length %d\n",
2109                __func__, skb_len);
2110
2111        skb = alloc_skb(skb_len, GFP_KERNEL);
2112        if (skb == NULL)
2113                goto out;
2114
2115        skb_put(skb, skb_len);
2116
2117        skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2118                memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2119                tmp_len += tmp->len;
2120        }
2121
2122out:
2123        skb_queue_purge(&dev->resp_q);
2124
2125        return skb;
2126}
2127
2128static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2129                                        struct sk_buff *resp)
2130{
2131        struct pn533_data_exchange_arg *arg = _arg;
2132        struct sk_buff *skb;
2133        int rc = 0;
2134        u8 status, ret, mi;
2135
2136        dev_dbg(dev->dev, "%s\n", __func__);
2137
2138        if (IS_ERR(resp)) {
2139                rc = PTR_ERR(resp);
2140                goto _error;
2141        }
2142
2143        status = resp->data[0];
2144        ret = status & PN533_CMD_RET_MASK;
2145        mi = status & PN533_CMD_MI_MASK;
2146
2147        skb_pull(resp, sizeof(status));
2148
2149        if (ret != PN533_CMD_RET_SUCCESS) {
2150                nfc_err(dev->dev,
2151                        "Exchanging data failed (error 0x%x)\n", ret);
2152                rc = -EIO;
2153                goto error;
2154        }
2155
2156        skb_queue_tail(&dev->resp_q, resp);
2157
2158        if (mi) {
2159                dev->cmd_complete_mi_arg = arg;
2160                queue_work(dev->wq, &dev->mi_rx_work);
2161                return -EINPROGRESS;
2162        }
2163
2164        /* Prepare for the next round */
2165        if (skb_queue_len(&dev->fragment_skb) > 0) {
2166                dev->cmd_complete_dep_arg = arg;
2167                queue_work(dev->wq, &dev->mi_tx_work);
2168
2169                return -EINPROGRESS;
2170        }
2171
2172        skb = pn533_build_response(dev);
2173        if (!skb) {
2174                rc = -ENOMEM;
2175                goto error;
2176        }
2177
2178        arg->cb(arg->cb_context, skb, 0);
2179        kfree(arg);
2180        return 0;
2181
2182error:
2183        dev_kfree_skb(resp);
2184_error:
2185        skb_queue_purge(&dev->resp_q);
2186        arg->cb(arg->cb_context, NULL, rc);
2187        kfree(arg);
2188        return rc;
2189}
2190
2191/*
2192 * Receive an incoming pn533 frame. skb contains only header and payload.
2193 * If skb == NULL, it is a notification that the link below is dead.
2194 */
2195void pn533_recv_frame(struct pn533 *dev, struct sk_buff *skb, int status)
2196{
2197        if (!dev->cmd)
2198                goto sched_wq;
2199
2200        dev->cmd->status = status;
2201
2202        if (status != 0) {
2203                dev_dbg(dev->dev, "%s: Error received: %d\n", __func__, status);
2204                goto sched_wq;
2205        }
2206
2207        if (skb == NULL) {
2208                pr_err("NULL Frame -> link is dead\n");
2209                goto sched_wq;
2210        }
2211
2212        if (pn533_rx_frame_is_ack(skb->data)) {
2213                dev_dbg(dev->dev, "%s: Received ACK frame\n", __func__);
2214                dev_kfree_skb(skb);
2215                return;
2216        }
2217
2218        print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, skb->data,
2219                             dev->ops->rx_frame_size(skb->data), false);
2220
2221        if (!dev->ops->rx_is_frame_valid(skb->data, dev)) {
2222                nfc_err(dev->dev, "Received an invalid frame\n");
2223                dev->cmd->status = -EIO;
2224        } else if (!pn533_rx_frame_is_cmd_response(dev, skb->data)) {
2225                nfc_err(dev->dev, "It it not the response to the last command\n");
2226                dev->cmd->status = -EIO;
2227        }
2228
2229        dev->cmd->resp = skb;
2230
2231sched_wq:
2232        queue_work(dev->wq, &dev->cmd_complete_work);
2233}
2234EXPORT_SYMBOL(pn533_recv_frame);
2235
2236/* Split the Tx skb into small chunks */
2237static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2238{
2239        struct sk_buff *frag;
2240        int  frag_size;
2241
2242        do {
2243                /* Remaining size */
2244                if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2245                        frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2246                else
2247                        frag_size = skb->len;
2248
2249                /* Allocate and reserve */
2250                frag = pn533_alloc_skb(dev, frag_size);
2251                if (!frag) {
2252                        skb_queue_purge(&dev->fragment_skb);
2253                        break;
2254                }
2255
2256                if (!dev->tgt_mode) {
2257                        /* Reserve the TG/MI byte */
2258                        skb_reserve(frag, 1);
2259
2260                        /* MI + TG */
2261                        if (frag_size  == PN533_CMD_DATAFRAME_MAXLEN)
2262                                *(u8 *)skb_push(frag, sizeof(u8)) =
2263                                                (PN533_CMD_MI_MASK | 1);
2264                        else
2265                                *(u8 *)skb_push(frag, sizeof(u8)) =  1; /* TG */
2266                }
2267
2268                skb_put_data(frag, skb->data, frag_size);
2269
2270                /* Reduce the size of incoming buffer */
2271                skb_pull(skb, frag_size);
2272
2273                /* Add this to skb_queue */
2274                skb_queue_tail(&dev->fragment_skb, frag);
2275
2276        } while (skb->len > 0);
2277
2278        dev_kfree_skb(skb);
2279
2280        return skb_queue_len(&dev->fragment_skb);
2281}
2282
2283static int pn533_transceive(struct nfc_dev *nfc_dev,
2284                            struct nfc_target *target, struct sk_buff *skb,
2285                            data_exchange_cb_t cb, void *cb_context)
2286{
2287        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2288        struct pn533_data_exchange_arg *arg = NULL;
2289        int rc;
2290
2291        dev_dbg(dev->dev, "%s\n", __func__);
2292
2293        if (!dev->tgt_active_prot) {
2294                nfc_err(dev->dev,
2295                        "Can't exchange data if there is no active target\n");
2296                rc = -EINVAL;
2297                goto error;
2298        }
2299
2300        arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2301        if (!arg) {
2302                rc = -ENOMEM;
2303                goto error;
2304        }
2305
2306        arg->cb = cb;
2307        arg->cb_context = cb_context;
2308
2309        switch (dev->device_type) {
2310        case PN533_DEVICE_PASORI:
2311                if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2312                        rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2313                                                   skb,
2314                                                   pn533_data_exchange_complete,
2315                                                   arg);
2316
2317                        break;
2318                }
2319                fallthrough;
2320        default:
2321                /* jumbo frame ? */
2322                if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2323                        rc = pn533_fill_fragment_skbs(dev, skb);
2324                        if (rc <= 0)
2325                                goto error;
2326
2327                        skb = skb_dequeue(&dev->fragment_skb);
2328                        if (!skb) {
2329                                rc = -EIO;
2330                                goto error;
2331                        }
2332                } else {
2333                        *(u8 *)skb_push(skb, sizeof(u8)) =  1; /* TG */
2334                }
2335
2336                rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2337                                           skb, pn533_data_exchange_complete,
2338                                           arg);
2339
2340                break;
2341        }
2342
2343        if (rc < 0) /* rc from send_async */
2344                goto error;
2345
2346        return 0;
2347
2348error:
2349        kfree(arg);
2350        dev_kfree_skb(skb);
2351        return rc;
2352}
2353
2354static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2355                                  struct sk_buff *resp)
2356{
2357        u8 status;
2358
2359        dev_dbg(dev->dev, "%s\n", __func__);
2360
2361        if (IS_ERR(resp))
2362                return PTR_ERR(resp);
2363
2364        status = resp->data[0];
2365
2366        /* Prepare for the next round */
2367        if (skb_queue_len(&dev->fragment_skb) > 0) {
2368                queue_work(dev->wq, &dev->mi_tm_tx_work);
2369                return -EINPROGRESS;
2370        }
2371        dev_kfree_skb(resp);
2372
2373        if (status != 0) {
2374                nfc_tm_deactivated(dev->nfc_dev);
2375
2376                dev->tgt_mode = 0;
2377
2378                return 0;
2379        }
2380
2381        queue_work(dev->wq, &dev->tg_work);
2382
2383        return 0;
2384}
2385
2386static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2387{
2388        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2389        int rc;
2390
2391        dev_dbg(dev->dev, "%s\n", __func__);
2392
2393        /* let's split in multiple chunks if size's too big */
2394        if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2395                rc = pn533_fill_fragment_skbs(dev, skb);
2396                if (rc <= 0)
2397                        goto error;
2398
2399                /* get the first skb */
2400                skb = skb_dequeue(&dev->fragment_skb);
2401                if (!skb) {
2402                        rc = -EIO;
2403                        goto error;
2404                }
2405
2406                rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_META_DATA, skb,
2407                                                pn533_tm_send_complete, NULL);
2408        } else {
2409                /* Send th skb */
2410                rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2411                                                pn533_tm_send_complete, NULL);
2412        }
2413
2414error:
2415        if (rc < 0) {
2416                dev_kfree_skb(skb);
2417                skb_queue_purge(&dev->fragment_skb);
2418        }
2419
2420        return rc;
2421}
2422
2423static void pn533_wq_mi_recv(struct work_struct *work)
2424{
2425        struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2426        struct sk_buff *skb;
2427        int rc;
2428
2429        dev_dbg(dev->dev, "%s\n", __func__);
2430
2431        skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2432        if (!skb)
2433                goto error;
2434
2435        switch (dev->device_type) {
2436        case PN533_DEVICE_PASORI:
2437                if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2438                        rc = pn533_send_cmd_direct_async(dev,
2439                                                PN533_CMD_IN_COMM_THRU,
2440                                                skb,
2441                                                pn533_data_exchange_complete,
2442                                                 dev->cmd_complete_mi_arg);
2443
2444                        break;
2445                }
2446                fallthrough;
2447        default:
2448                skb_put_u8(skb, 1); /*TG*/
2449
2450                rc = pn533_send_cmd_direct_async(dev,
2451                                                 PN533_CMD_IN_DATA_EXCHANGE,
2452                                                 skb,
2453                                                 pn533_data_exchange_complete,
2454                                                 dev->cmd_complete_mi_arg);
2455
2456                break;
2457        }
2458
2459        if (rc == 0) /* success */
2460                return;
2461
2462        nfc_err(dev->dev,
2463                "Error %d when trying to perform data_exchange\n", rc);
2464
2465        dev_kfree_skb(skb);
2466        kfree(dev->cmd_complete_mi_arg);
2467
2468error:
2469        dev->phy_ops->send_ack(dev, GFP_KERNEL);
2470        queue_work(dev->wq, &dev->cmd_work);
2471}
2472
2473static void pn533_wq_mi_send(struct work_struct *work)
2474{
2475        struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2476        struct sk_buff *skb;
2477        int rc;
2478
2479        dev_dbg(dev->dev, "%s\n", __func__);
2480
2481        /* Grab the first skb in the queue */
2482        skb = skb_dequeue(&dev->fragment_skb);
2483
2484        if (skb == NULL) {      /* No more data */
2485                /* Reset the queue for future use */
2486                skb_queue_head_init(&dev->fragment_skb);
2487                goto error;
2488        }
2489
2490        switch (dev->device_type) {
2491        case PN533_DEVICE_PASORI:
2492                if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2493                        rc = -EIO;
2494                        break;
2495                }
2496
2497                rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2498                                                 skb,
2499                                                 pn533_data_exchange_complete,
2500                                                 dev->cmd_complete_dep_arg);
2501
2502                break;
2503
2504        default:
2505                /* Still some fragments? */
2506                rc = pn533_send_cmd_direct_async(dev,
2507                                                 PN533_CMD_IN_DATA_EXCHANGE,
2508                                                 skb,
2509                                                 pn533_data_exchange_complete,
2510                                                 dev->cmd_complete_dep_arg);
2511
2512                break;
2513        }
2514
2515        if (rc == 0) /* success */
2516                return;
2517
2518        nfc_err(dev->dev,
2519                "Error %d when trying to perform data_exchange\n", rc);
2520
2521        dev_kfree_skb(skb);
2522        kfree(dev->cmd_complete_dep_arg);
2523
2524error:
2525        dev->phy_ops->send_ack(dev, GFP_KERNEL);
2526        queue_work(dev->wq, &dev->cmd_work);
2527}
2528
2529static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2530                                                                u8 cfgdata_len)
2531{
2532        struct sk_buff *skb;
2533        struct sk_buff *resp;
2534        int skb_len;
2535
2536        dev_dbg(dev->dev, "%s\n", __func__);
2537
2538        skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2539
2540        skb = pn533_alloc_skb(dev, skb_len);
2541        if (!skb)
2542                return -ENOMEM;
2543
2544        skb_put_u8(skb, cfgitem);
2545        skb_put_data(skb, cfgdata, cfgdata_len);
2546
2547        resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2548        if (IS_ERR(resp))
2549                return PTR_ERR(resp);
2550
2551        dev_kfree_skb(resp);
2552        return 0;
2553}
2554
2555static int pn533_get_firmware_version(struct pn533 *dev,
2556                                      struct pn533_fw_version *fv)
2557{
2558        struct sk_buff *skb;
2559        struct sk_buff *resp;
2560
2561        skb = pn533_alloc_skb(dev, 0);
2562        if (!skb)
2563                return -ENOMEM;
2564
2565        resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2566        if (IS_ERR(resp))
2567                return PTR_ERR(resp);
2568
2569        fv->ic = resp->data[0];
2570        fv->ver = resp->data[1];
2571        fv->rev = resp->data[2];
2572        fv->support = resp->data[3];
2573
2574        dev_kfree_skb(resp);
2575        return 0;
2576}
2577
2578static int pn533_pasori_fw_reset(struct pn533 *dev)
2579{
2580        struct sk_buff *skb;
2581        struct sk_buff *resp;
2582
2583        dev_dbg(dev->dev, "%s\n", __func__);
2584
2585        skb = pn533_alloc_skb(dev, sizeof(u8));
2586        if (!skb)
2587                return -ENOMEM;
2588
2589        skb_put_u8(skb, 0x1);
2590
2591        resp = pn533_send_cmd_sync(dev, 0x18, skb);
2592        if (IS_ERR(resp))
2593                return PTR_ERR(resp);
2594
2595        dev_kfree_skb(resp);
2596
2597        return 0;
2598}
2599
2600static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2601{
2602        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2603        u8 rf_field = !!rf;
2604        int rc;
2605
2606        rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2607
2608        rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2609                                     (u8 *)&rf_field, 1);
2610        if (rc) {
2611                nfc_err(dev->dev, "Error on setting RF field\n");
2612                return rc;
2613        }
2614
2615        return 0;
2616}
2617
2618static int pn532_sam_configuration(struct nfc_dev *nfc_dev)
2619{
2620        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2621        struct sk_buff *skb;
2622        struct sk_buff *resp;
2623
2624        skb = pn533_alloc_skb(dev, 1);
2625        if (!skb)
2626                return -ENOMEM;
2627
2628        skb_put_u8(skb, 0x01);
2629
2630        resp = pn533_send_cmd_sync(dev, PN533_CMD_SAM_CONFIGURATION, skb);
2631        if (IS_ERR(resp))
2632                return PTR_ERR(resp);
2633
2634        dev_kfree_skb(resp);
2635        return 0;
2636}
2637
2638static int pn533_dev_up(struct nfc_dev *nfc_dev)
2639{
2640        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2641        int rc;
2642
2643        if (dev->phy_ops->dev_up) {
2644                rc = dev->phy_ops->dev_up(dev);
2645                if (rc)
2646                        return rc;
2647        }
2648
2649        if ((dev->device_type == PN533_DEVICE_PN532) ||
2650                (dev->device_type == PN533_DEVICE_PN532_AUTOPOLL)) {
2651                rc = pn532_sam_configuration(nfc_dev);
2652
2653                if (rc)
2654                        return rc;
2655        }
2656
2657        return pn533_rf_field(nfc_dev, 1);
2658}
2659
2660static int pn533_dev_down(struct nfc_dev *nfc_dev)
2661{
2662        struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2663        int ret;
2664
2665        ret = pn533_rf_field(nfc_dev, 0);
2666        if (dev->phy_ops->dev_down && !ret)
2667                ret = dev->phy_ops->dev_down(dev);
2668
2669        return ret;
2670}
2671
2672static struct nfc_ops pn533_nfc_ops = {
2673        .dev_up = pn533_dev_up,
2674        .dev_down = pn533_dev_down,
2675        .dep_link_up = pn533_dep_link_up,
2676        .dep_link_down = pn533_dep_link_down,
2677        .start_poll = pn533_start_poll,
2678        .stop_poll = pn533_stop_poll,
2679        .activate_target = pn533_activate_target,
2680        .deactivate_target = pn533_deactivate_target,
2681        .im_transceive = pn533_transceive,
2682        .tm_send = pn533_tm_send,
2683};
2684
2685static int pn533_setup(struct pn533 *dev)
2686{
2687        struct pn533_config_max_retries max_retries;
2688        struct pn533_config_timing timing;
2689        u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2690        int rc;
2691
2692        switch (dev->device_type) {
2693        case PN533_DEVICE_STD:
2694        case PN533_DEVICE_PASORI:
2695        case PN533_DEVICE_ACR122U:
2696        case PN533_DEVICE_PN532:
2697        case PN533_DEVICE_PN532_AUTOPOLL:
2698                max_retries.mx_rty_atr = 0x2;
2699                max_retries.mx_rty_psl = 0x1;
2700                max_retries.mx_rty_passive_act =
2701                        PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2702
2703                timing.rfu = PN533_CONFIG_TIMING_102;
2704                timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2705                timing.dep_timeout = PN533_CONFIG_TIMING_204;
2706
2707                break;
2708
2709        default:
2710                nfc_err(dev->dev, "Unknown device type %d\n",
2711                        dev->device_type);
2712                return -EINVAL;
2713        }
2714
2715        rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2716                                     (u8 *)&max_retries, sizeof(max_retries));
2717        if (rc) {
2718                nfc_err(dev->dev,
2719                        "Error on setting MAX_RETRIES config\n");
2720                return rc;
2721        }
2722
2723
2724        rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2725                                     (u8 *)&timing, sizeof(timing));
2726        if (rc) {
2727                nfc_err(dev->dev, "Error on setting RF timings\n");
2728                return rc;
2729        }
2730
2731        switch (dev->device_type) {
2732        case PN533_DEVICE_STD:
2733        case PN533_DEVICE_PN532:
2734        case PN533_DEVICE_PN532_AUTOPOLL:
2735                break;
2736
2737        case PN533_DEVICE_PASORI:
2738                pn533_pasori_fw_reset(dev);
2739
2740                rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2741                                             pasori_cfg, 3);
2742                if (rc) {
2743                        nfc_err(dev->dev,
2744                                "Error while settings PASORI config\n");
2745                        return rc;
2746                }
2747
2748                pn533_pasori_fw_reset(dev);
2749
2750                break;
2751        }
2752
2753        return 0;
2754}
2755
2756int pn533_finalize_setup(struct pn533 *dev)
2757{
2758
2759        struct pn533_fw_version fw_ver;
2760        int rc;
2761
2762        memset(&fw_ver, 0, sizeof(fw_ver));
2763
2764        rc = pn533_get_firmware_version(dev, &fw_ver);
2765        if (rc) {
2766                nfc_err(dev->dev, "Unable to get FW version\n");
2767                return rc;
2768        }
2769
2770        nfc_info(dev->dev, "NXP PN5%02X firmware ver %d.%d now attached\n",
2771                fw_ver.ic, fw_ver.ver, fw_ver.rev);
2772
2773        rc = pn533_setup(dev);
2774        if (rc)
2775                return rc;
2776
2777        return 0;
2778}
2779EXPORT_SYMBOL_GPL(pn533_finalize_setup);
2780
2781struct pn533 *pn53x_common_init(u32 device_type,
2782                                enum pn533_protocol_type protocol_type,
2783                                void *phy,
2784                                struct pn533_phy_ops *phy_ops,
2785                                struct pn533_frame_ops *fops,
2786                                struct device *dev)
2787{
2788        struct pn533 *priv;
2789
2790        priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2791        if (!priv)
2792                return ERR_PTR(-ENOMEM);
2793
2794        priv->phy = phy;
2795        priv->phy_ops = phy_ops;
2796        priv->dev = dev;
2797        if (fops != NULL)
2798                priv->ops = fops;
2799        else
2800                priv->ops = &pn533_std_frame_ops;
2801
2802        priv->protocol_type = protocol_type;
2803        priv->device_type = device_type;
2804
2805        mutex_init(&priv->cmd_lock);
2806
2807        INIT_WORK(&priv->cmd_work, pn533_wq_cmd);
2808        INIT_WORK(&priv->cmd_complete_work, pn533_wq_cmd_complete);
2809        INIT_WORK(&priv->mi_rx_work, pn533_wq_mi_recv);
2810        INIT_WORK(&priv->mi_tx_work, pn533_wq_mi_send);
2811        INIT_WORK(&priv->tg_work, pn533_wq_tg_get_data);
2812        INIT_WORK(&priv->mi_tm_rx_work, pn533_wq_tm_mi_recv);
2813        INIT_WORK(&priv->mi_tm_tx_work, pn533_wq_tm_mi_send);
2814        INIT_DELAYED_WORK(&priv->poll_work, pn533_wq_poll);
2815        INIT_WORK(&priv->rf_work, pn533_wq_rf);
2816        priv->wq = alloc_ordered_workqueue("pn533", 0);
2817        if (priv->wq == NULL)
2818                goto error;
2819
2820        timer_setup(&priv->listen_timer, pn533_listen_mode_timer, 0);
2821
2822        skb_queue_head_init(&priv->resp_q);
2823        skb_queue_head_init(&priv->fragment_skb);
2824
2825        INIT_LIST_HEAD(&priv->cmd_queue);
2826        return priv;
2827
2828error:
2829        kfree(priv);
2830        return ERR_PTR(-ENOMEM);
2831}
2832EXPORT_SYMBOL_GPL(pn53x_common_init);
2833
2834void pn53x_common_clean(struct pn533 *priv)
2835{
2836        struct pn533_cmd *cmd, *n;
2837
2838        flush_delayed_work(&priv->poll_work);
2839        destroy_workqueue(priv->wq);
2840
2841        skb_queue_purge(&priv->resp_q);
2842
2843        del_timer(&priv->listen_timer);
2844
2845        list_for_each_entry_safe(cmd, n, &priv->cmd_queue, queue) {
2846                list_del(&cmd->queue);
2847                kfree(cmd);
2848        }
2849
2850        kfree(priv);
2851}
2852EXPORT_SYMBOL_GPL(pn53x_common_clean);
2853
2854int pn532_i2c_nfc_alloc(struct pn533 *priv, u32 protocols,
2855                        struct device *parent)
2856{
2857        priv->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2858                                           priv->ops->tx_header_len +
2859                                           PN533_CMD_DATAEXCH_HEAD_LEN,
2860                                           priv->ops->tx_tail_len);
2861        if (!priv->nfc_dev)
2862                return -ENOMEM;
2863
2864        nfc_set_parent_dev(priv->nfc_dev, parent);
2865        nfc_set_drvdata(priv->nfc_dev, priv);
2866        return 0;
2867}
2868EXPORT_SYMBOL_GPL(pn532_i2c_nfc_alloc);
2869
2870int pn53x_register_nfc(struct pn533 *priv, u32 protocols,
2871                        struct device *parent)
2872{
2873        int rc;
2874
2875        rc = pn532_i2c_nfc_alloc(priv, protocols, parent);
2876        if (rc)
2877                return rc;
2878
2879        rc = nfc_register_device(priv->nfc_dev);
2880        if (rc)
2881                nfc_free_device(priv->nfc_dev);
2882
2883        return rc;
2884}
2885EXPORT_SYMBOL_GPL(pn53x_register_nfc);
2886
2887void pn53x_unregister_nfc(struct pn533 *priv)
2888{
2889        nfc_unregister_device(priv->nfc_dev);
2890        nfc_free_device(priv->nfc_dev);
2891}
2892EXPORT_SYMBOL_GPL(pn53x_unregister_nfc);
2893
2894MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
2895MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
2896MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
2897MODULE_DESCRIPTION("PN533 driver ver " VERSION);
2898MODULE_VERSION(VERSION);
2899MODULE_LICENSE("GPL");
2900