linux/net/nfc/digital_dep.c
<<
>>
Prefs
   1/*
   2 * NFC Digital Protocol stack
   3 * Copyright (c) 2013, Intel Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 *
  14 */
  15
  16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
  17
  18#include "digital.h"
  19
  20#define DIGITAL_NFC_DEP_N_RETRY_NACK    2
  21#define DIGITAL_NFC_DEP_N_RETRY_ATN     2
  22
  23#define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
  24#define DIGITAL_NFC_DEP_FRAME_DIR_IN  0xD5
  25
  26#define DIGITAL_NFC_DEP_NFCA_SOD_SB   0xF0
  27
  28#define DIGITAL_CMD_ATR_REQ 0x00
  29#define DIGITAL_CMD_ATR_RES 0x01
  30#define DIGITAL_CMD_PSL_REQ 0x04
  31#define DIGITAL_CMD_PSL_RES 0x05
  32#define DIGITAL_CMD_DEP_REQ 0x06
  33#define DIGITAL_CMD_DEP_RES 0x07
  34
  35#define DIGITAL_ATR_REQ_MIN_SIZE 16
  36#define DIGITAL_ATR_REQ_MAX_SIZE 64
  37
  38#define DIGITAL_ATR_RES_TO_WT(s)        ((s) & 0xF)
  39
  40#define DIGITAL_DID_MAX 14
  41
  42#define DIGITAL_PAYLOAD_SIZE_MAX        254
  43#define DIGITAL_PAYLOAD_BITS_TO_PP(s)   (((s) & 0x3) << 4)
  44#define DIGITAL_PAYLOAD_PP_TO_BITS(s)   (((s) >> 4) & 0x3)
  45#define DIGITAL_PAYLOAD_BITS_TO_FSL(s)  ((s) & 0x3)
  46#define DIGITAL_PAYLOAD_FSL_TO_BITS(s)  ((s) & 0x3)
  47
  48#define DIGITAL_GB_BIT  0x02
  49
  50#define DIGITAL_NFC_DEP_REQ_RES_HEADROOM        2 /* SoD: [SB (NFC-A)] + LEN */
  51#define DIGITAL_NFC_DEP_REQ_RES_TAILROOM        2 /* EoD: 2-byte CRC */
  52
  53#define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
  54
  55#define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
  56#define DIGITAL_NFC_DEP_PFB_MI_BIT      0x10
  57#define DIGITAL_NFC_DEP_PFB_NACK_BIT    0x10
  58#define DIGITAL_NFC_DEP_PFB_DID_BIT     0x04
  59
  60#define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
  61                                ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
  62#define DIGITAL_NFC_DEP_MI_BIT_SET(pfb)  ((pfb) & DIGITAL_NFC_DEP_PFB_MI_BIT)
  63#define DIGITAL_NFC_DEP_NACK_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_NACK_BIT)
  64#define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
  65#define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & DIGITAL_NFC_DEP_PFB_DID_BIT)
  66#define DIGITAL_NFC_DEP_PFB_PNI(pfb)     ((pfb) & 0x03)
  67
  68#define DIGITAL_NFC_DEP_RTOX_VALUE(data) ((data) & 0x3F)
  69#define DIGITAL_NFC_DEP_RTOX_MAX         59
  70
  71#define DIGITAL_NFC_DEP_PFB_I_PDU          0x00
  72#define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU   0x40
  73#define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
  74
  75struct digital_atr_req {
  76        u8 dir;
  77        u8 cmd;
  78        u8 nfcid3[10];
  79        u8 did;
  80        u8 bs;
  81        u8 br;
  82        u8 pp;
  83        u8 gb[0];
  84} __packed;
  85
  86struct digital_atr_res {
  87        u8 dir;
  88        u8 cmd;
  89        u8 nfcid3[10];
  90        u8 did;
  91        u8 bs;
  92        u8 br;
  93        u8 to;
  94        u8 pp;
  95        u8 gb[0];
  96} __packed;
  97
  98struct digital_psl_req {
  99        u8 dir;
 100        u8 cmd;
 101        u8 did;
 102        u8 brs;
 103        u8 fsl;
 104} __packed;
 105
 106struct digital_psl_res {
 107        u8 dir;
 108        u8 cmd;
 109        u8 did;
 110} __packed;
 111
 112struct digital_dep_req_res {
 113        u8 dir;
 114        u8 cmd;
 115        u8 pfb;
 116} __packed;
 117
 118static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
 119                                    struct sk_buff *resp);
 120static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
 121                                    struct sk_buff *resp);
 122
 123static const u8 digital_payload_bits_map[4] = {
 124        [0] = 64,
 125        [1] = 128,
 126        [2] = 192,
 127        [3] = 254
 128};
 129
 130/* Response Waiting Time for ATR_RES PDU in ms
 131 *
 132 * RWT(ATR_RES) = RWT(nfcdep,activation) + dRWT(nfcdep) + dT(nfcdep,initiator)
 133 *
 134 * with:
 135 *  RWT(nfcdep,activation) = 4096 * 2^12 / f(c) s
 136 *  dRWT(nfcdep) = 16 / f(c) s
 137 *  dT(nfcdep,initiator) = 100 ms
 138 *  f(c) = 13560000 Hz
 139 */
 140#define DIGITAL_ATR_RES_RWT 1337
 141
 142/* Response Waiting Time for other DEP PDUs in ms
 143 *
 144 * max_rwt = rwt + dRWT(nfcdep) + dT(nfcdep,initiator)
 145 *
 146 * with:
 147 *  rwt = (256 * 16 / f(c)) * 2^wt s
 148 *  dRWT(nfcdep) = 16 / f(c) s
 149 *  dT(nfcdep,initiator) = 100 ms
 150 *  f(c) = 13560000 Hz
 151 *  0 <= wt <= 14 (given by the target by the TO field of ATR_RES response)
 152 */
 153#define DIGITAL_NFC_DEP_IN_MAX_WT 14
 154#define DIGITAL_NFC_DEP_TG_MAX_WT 8
 155static const u16 digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT + 1] = {
 156        100,  101,  101,  102,  105,
 157        110,  119,  139,  177,  255,
 158        409,  719, 1337, 2575, 5049,
 159};
 160
 161static u8 digital_payload_bits_to_size(u8 payload_bits)
 162{
 163        if (payload_bits >= ARRAY_SIZE(digital_payload_bits_map))
 164                return 0;
 165
 166        return digital_payload_bits_map[payload_bits];
 167}
 168
 169static u8 digital_payload_size_to_bits(u8 payload_size)
 170{
 171        int i;
 172
 173        for (i = 0; i < ARRAY_SIZE(digital_payload_bits_map); i++)
 174                if (digital_payload_bits_map[i] == payload_size)
 175                        return i;
 176
 177        return 0xff;
 178}
 179
 180static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
 181                                     struct sk_buff *skb)
 182{
 183        skb_push(skb, sizeof(u8));
 184
 185        skb->data[0] = skb->len;
 186
 187        if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
 188                *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
 189}
 190
 191static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
 192                                    struct sk_buff *skb)
 193{
 194        u8 size;
 195
 196        if (skb->len < 2)
 197                return -EIO;
 198
 199        if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
 200                skb_pull(skb, sizeof(u8));
 201
 202        size = skb->data[0];
 203        if (size != skb->len)
 204                return -EIO;
 205
 206        skb_pull(skb, sizeof(u8));
 207
 208        return 0;
 209}
 210
 211static struct sk_buff *
 212digital_send_dep_data_prep(struct nfc_digital_dev *ddev, struct sk_buff *skb,
 213                           struct digital_dep_req_res *dep_req_res,
 214                           struct digital_data_exch *data_exch)
 215{
 216        struct sk_buff *new_skb;
 217
 218        if (skb->len > ddev->remote_payload_max) {
 219                dep_req_res->pfb |= DIGITAL_NFC_DEP_PFB_MI_BIT;
 220
 221                new_skb = digital_skb_alloc(ddev, ddev->remote_payload_max);
 222                if (!new_skb) {
 223                        kfree_skb(ddev->chaining_skb);
 224                        ddev->chaining_skb = NULL;
 225
 226                        return ERR_PTR(-ENOMEM);
 227                }
 228
 229                memcpy(skb_put(new_skb, ddev->remote_payload_max), skb->data,
 230                       ddev->remote_payload_max);
 231                skb_pull(skb, ddev->remote_payload_max);
 232
 233                ddev->chaining_skb = skb;
 234                ddev->data_exch = data_exch;
 235        } else {
 236                ddev->chaining_skb = NULL;
 237                new_skb = skb;
 238        }
 239
 240        return new_skb;
 241}
 242
 243static struct sk_buff *
 244digital_recv_dep_data_gather(struct nfc_digital_dev *ddev, u8 pfb,
 245                             struct sk_buff *resp,
 246                             int (*send_ack)(struct nfc_digital_dev *ddev,
 247                                             struct digital_data_exch
 248                                                             *data_exch),
 249                             struct digital_data_exch *data_exch)
 250{
 251        struct sk_buff *new_skb;
 252        int rc;
 253
 254        if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb) && (!ddev->chaining_skb)) {
 255                ddev->chaining_skb =
 256                        nfc_alloc_recv_skb(8 * ddev->local_payload_max,
 257                                           GFP_KERNEL);
 258                if (!ddev->chaining_skb) {
 259                        rc = -ENOMEM;
 260                        goto error;
 261                }
 262        }
 263
 264        if (ddev->chaining_skb) {
 265                if (resp->len > skb_tailroom(ddev->chaining_skb)) {
 266                        new_skb = skb_copy_expand(ddev->chaining_skb,
 267                                                  skb_headroom(
 268                                                          ddev->chaining_skb),
 269                                                  8 * ddev->local_payload_max,
 270                                                  GFP_KERNEL);
 271                        if (!new_skb) {
 272                                rc = -ENOMEM;
 273                                goto error;
 274                        }
 275
 276                        kfree_skb(ddev->chaining_skb);
 277                        ddev->chaining_skb = new_skb;
 278                }
 279
 280                memcpy(skb_put(ddev->chaining_skb, resp->len), resp->data,
 281                       resp->len);
 282
 283                kfree_skb(resp);
 284                resp = NULL;
 285
 286                if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
 287                        rc = send_ack(ddev, data_exch);
 288                        if (rc)
 289                                goto error;
 290
 291                        return NULL;
 292                }
 293
 294                resp = ddev->chaining_skb;
 295                ddev->chaining_skb = NULL;
 296        }
 297
 298        return resp;
 299
 300error:
 301        kfree_skb(resp);
 302
 303        kfree_skb(ddev->chaining_skb);
 304        ddev->chaining_skb = NULL;
 305
 306        return ERR_PTR(rc);
 307}
 308
 309static void digital_in_recv_psl_res(struct nfc_digital_dev *ddev, void *arg,
 310                                    struct sk_buff *resp)
 311{
 312        struct nfc_target *target = arg;
 313        struct digital_psl_res *psl_res;
 314        int rc;
 315
 316        if (IS_ERR(resp)) {
 317                rc = PTR_ERR(resp);
 318                resp = NULL;
 319                goto exit;
 320        }
 321
 322        rc = ddev->skb_check_crc(resp);
 323        if (rc) {
 324                PROTOCOL_ERR("14.4.1.6");
 325                goto exit;
 326        }
 327
 328        rc = digital_skb_pull_dep_sod(ddev, resp);
 329        if (rc) {
 330                PROTOCOL_ERR("14.4.1.2");
 331                goto exit;
 332        }
 333
 334        psl_res = (struct digital_psl_res *)resp->data;
 335
 336        if ((resp->len != sizeof(*psl_res)) ||
 337            (psl_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN) ||
 338            (psl_res->cmd != DIGITAL_CMD_PSL_RES)) {
 339                rc = -EIO;
 340                goto exit;
 341        }
 342
 343        rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
 344                                     NFC_DIGITAL_RF_TECH_424F);
 345        if (rc)
 346                goto exit;
 347
 348        rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
 349                                     NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
 350        if (rc)
 351                goto exit;
 352
 353        if (!DIGITAL_DRV_CAPS_IN_CRC(ddev) &&
 354            (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)) {
 355                ddev->skb_add_crc = digital_skb_add_crc_f;
 356                ddev->skb_check_crc = digital_skb_check_crc_f;
 357        }
 358
 359        ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_424F;
 360
 361        nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
 362                           NFC_RF_INITIATOR);
 363
 364        ddev->curr_nfc_dep_pni = 0;
 365
 366exit:
 367        dev_kfree_skb(resp);
 368
 369        if (rc)
 370                ddev->curr_protocol = 0;
 371}
 372
 373static int digital_in_send_psl_req(struct nfc_digital_dev *ddev,
 374                                   struct nfc_target *target)
 375{
 376        struct sk_buff *skb;
 377        struct digital_psl_req *psl_req;
 378        int rc;
 379        u8 payload_size, payload_bits;
 380
 381        skb = digital_skb_alloc(ddev, sizeof(*psl_req));
 382        if (!skb)
 383                return -ENOMEM;
 384
 385        skb_put(skb, sizeof(*psl_req));
 386
 387        psl_req = (struct digital_psl_req *)skb->data;
 388
 389        psl_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 390        psl_req->cmd = DIGITAL_CMD_PSL_REQ;
 391        psl_req->did = 0;
 392        psl_req->brs = (0x2 << 3) | 0x2; /* 424F both directions */
 393
 394        payload_size = min(ddev->local_payload_max, ddev->remote_payload_max);
 395        payload_bits = digital_payload_size_to_bits(payload_size);
 396        psl_req->fsl = DIGITAL_PAYLOAD_BITS_TO_FSL(payload_bits);
 397
 398        ddev->local_payload_max = payload_size;
 399        ddev->remote_payload_max = payload_size;
 400
 401        digital_skb_push_dep_sod(ddev, skb);
 402
 403        ddev->skb_add_crc(skb);
 404
 405        rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
 406                                 digital_in_recv_psl_res, target);
 407        if (rc)
 408                kfree_skb(skb);
 409
 410        return rc;
 411}
 412
 413static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
 414                                 struct sk_buff *resp)
 415{
 416        struct nfc_target *target = arg;
 417        struct digital_atr_res *atr_res;
 418        u8 gb_len, payload_bits;
 419        u8 wt;
 420        int rc;
 421
 422        if (IS_ERR(resp)) {
 423                rc = PTR_ERR(resp);
 424                resp = NULL;
 425                goto exit;
 426        }
 427
 428        rc = ddev->skb_check_crc(resp);
 429        if (rc) {
 430                PROTOCOL_ERR("14.4.1.6");
 431                goto exit;
 432        }
 433
 434        rc = digital_skb_pull_dep_sod(ddev, resp);
 435        if (rc) {
 436                PROTOCOL_ERR("14.4.1.2");
 437                goto exit;
 438        }
 439
 440        if (resp->len < sizeof(struct digital_atr_res)) {
 441                rc = -EIO;
 442                goto exit;
 443        }
 444
 445        gb_len = resp->len - sizeof(struct digital_atr_res);
 446
 447        atr_res = (struct digital_atr_res *)resp->data;
 448
 449        wt = DIGITAL_ATR_RES_TO_WT(atr_res->to);
 450        if (wt > DIGITAL_NFC_DEP_IN_MAX_WT)
 451                wt = DIGITAL_NFC_DEP_IN_MAX_WT;
 452        ddev->dep_rwt = digital_rwt_map[wt];
 453
 454        payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_res->pp);
 455        ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
 456
 457        if (!ddev->remote_payload_max) {
 458                rc = -EINVAL;
 459                goto exit;
 460        }
 461
 462        rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
 463        if (rc)
 464                goto exit;
 465
 466        if ((ddev->protocols & NFC_PROTO_FELICA_MASK) &&
 467            (ddev->curr_rf_tech != NFC_DIGITAL_RF_TECH_424F)) {
 468                rc = digital_in_send_psl_req(ddev, target);
 469                if (!rc)
 470                        goto exit;
 471        }
 472
 473        rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
 474                                NFC_RF_INITIATOR);
 475
 476        ddev->curr_nfc_dep_pni = 0;
 477
 478exit:
 479        dev_kfree_skb(resp);
 480
 481        if (rc)
 482                ddev->curr_protocol = 0;
 483}
 484
 485int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
 486                            struct nfc_target *target, __u8 comm_mode, __u8 *gb,
 487                            size_t gb_len)
 488{
 489        struct sk_buff *skb;
 490        struct digital_atr_req *atr_req;
 491        uint size;
 492        int rc;
 493        u8 payload_bits;
 494
 495        size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
 496
 497        if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
 498                PROTOCOL_ERR("14.6.1.1");
 499                return -EINVAL;
 500        }
 501
 502        skb = digital_skb_alloc(ddev, size);
 503        if (!skb)
 504                return -ENOMEM;
 505
 506        skb_put(skb, sizeof(struct digital_atr_req));
 507
 508        atr_req = (struct digital_atr_req *)skb->data;
 509        memset(atr_req, 0, sizeof(struct digital_atr_req));
 510
 511        atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 512        atr_req->cmd = DIGITAL_CMD_ATR_REQ;
 513        if (target->nfcid2_len)
 514                memcpy(atr_req->nfcid3, target->nfcid2, NFC_NFCID2_MAXSIZE);
 515        else
 516                get_random_bytes(atr_req->nfcid3, NFC_NFCID3_MAXSIZE);
 517
 518        atr_req->did = 0;
 519        atr_req->bs = 0;
 520        atr_req->br = 0;
 521
 522        ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
 523        payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
 524        atr_req->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
 525
 526        if (gb_len) {
 527                atr_req->pp |= DIGITAL_GB_BIT;
 528                memcpy(skb_put(skb, gb_len), gb, gb_len);
 529        }
 530
 531        digital_skb_push_dep_sod(ddev, skb);
 532
 533        ddev->skb_add_crc(skb);
 534
 535        rc = digital_in_send_cmd(ddev, skb, DIGITAL_ATR_RES_RWT,
 536                                 digital_in_recv_atr_res, target);
 537        if (rc)
 538                kfree_skb(skb);
 539
 540        return rc;
 541}
 542
 543static int digital_in_send_ack(struct nfc_digital_dev *ddev,
 544                               struct digital_data_exch *data_exch)
 545{
 546        struct digital_dep_req_res *dep_req;
 547        struct sk_buff *skb;
 548        int rc;
 549
 550        skb = digital_skb_alloc(ddev, 1);
 551        if (!skb)
 552                return -ENOMEM;
 553
 554        skb_push(skb, sizeof(struct digital_dep_req_res));
 555
 556        dep_req = (struct digital_dep_req_res *)skb->data;
 557
 558        dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 559        dep_req->cmd = DIGITAL_CMD_DEP_REQ;
 560        dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
 561                       ddev->curr_nfc_dep_pni;
 562
 563        digital_skb_push_dep_sod(ddev, skb);
 564
 565        ddev->skb_add_crc(skb);
 566
 567        ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
 568
 569        rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
 570                                 digital_in_recv_dep_res, data_exch);
 571        if (rc) {
 572                kfree_skb(skb);
 573                kfree_skb(ddev->saved_skb);
 574                ddev->saved_skb = NULL;
 575        }
 576
 577        return rc;
 578}
 579
 580static int digital_in_send_nack(struct nfc_digital_dev *ddev,
 581                                struct digital_data_exch *data_exch)
 582{
 583        struct digital_dep_req_res *dep_req;
 584        struct sk_buff *skb;
 585        int rc;
 586
 587        skb = digital_skb_alloc(ddev, 1);
 588        if (!skb)
 589                return -ENOMEM;
 590
 591        skb_push(skb, sizeof(struct digital_dep_req_res));
 592
 593        dep_req = (struct digital_dep_req_res *)skb->data;
 594
 595        dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 596        dep_req->cmd = DIGITAL_CMD_DEP_REQ;
 597        dep_req->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
 598                       DIGITAL_NFC_DEP_PFB_NACK_BIT | ddev->curr_nfc_dep_pni;
 599
 600        digital_skb_push_dep_sod(ddev, skb);
 601
 602        ddev->skb_add_crc(skb);
 603
 604        rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
 605                                 digital_in_recv_dep_res, data_exch);
 606        if (rc)
 607                kfree_skb(skb);
 608
 609        return rc;
 610}
 611
 612static int digital_in_send_atn(struct nfc_digital_dev *ddev,
 613                               struct digital_data_exch *data_exch)
 614{
 615        struct digital_dep_req_res *dep_req;
 616        struct sk_buff *skb;
 617        int rc;
 618
 619        skb = digital_skb_alloc(ddev, 1);
 620        if (!skb)
 621                return -ENOMEM;
 622
 623        skb_push(skb, sizeof(struct digital_dep_req_res));
 624
 625        dep_req = (struct digital_dep_req_res *)skb->data;
 626
 627        dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 628        dep_req->cmd = DIGITAL_CMD_DEP_REQ;
 629        dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
 630
 631        digital_skb_push_dep_sod(ddev, skb);
 632
 633        ddev->skb_add_crc(skb);
 634
 635        rc = digital_in_send_cmd(ddev, skb, ddev->dep_rwt,
 636                                 digital_in_recv_dep_res, data_exch);
 637        if (rc)
 638                kfree_skb(skb);
 639
 640        return rc;
 641}
 642
 643static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
 644                                struct digital_data_exch *data_exch, u8 rtox)
 645{
 646        struct digital_dep_req_res *dep_req;
 647        struct sk_buff *skb;
 648        int rc;
 649        u16 rwt_int;
 650
 651        rwt_int = ddev->dep_rwt * rtox;
 652        if (rwt_int > digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT])
 653                rwt_int = digital_rwt_map[DIGITAL_NFC_DEP_IN_MAX_WT];
 654
 655        skb = digital_skb_alloc(ddev, 1);
 656        if (!skb)
 657                return -ENOMEM;
 658
 659        *skb_put(skb, 1) = rtox;
 660
 661        skb_push(skb, sizeof(struct digital_dep_req_res));
 662
 663        dep_req = (struct digital_dep_req_res *)skb->data;
 664
 665        dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 666        dep_req->cmd = DIGITAL_CMD_DEP_REQ;
 667        dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
 668                       DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
 669
 670        digital_skb_push_dep_sod(ddev, skb);
 671
 672        ddev->skb_add_crc(skb);
 673
 674        rc = digital_in_send_cmd(ddev, skb, rwt_int,
 675                                 digital_in_recv_dep_res, data_exch);
 676        if (rc)
 677                kfree_skb(skb);
 678
 679        return rc;
 680}
 681
 682static int digital_in_send_saved_skb(struct nfc_digital_dev *ddev,
 683                                     struct digital_data_exch *data_exch)
 684{
 685        int rc;
 686
 687        if (!ddev->saved_skb)
 688                return -EINVAL;
 689
 690        skb_get(ddev->saved_skb);
 691
 692        rc = digital_in_send_cmd(ddev, ddev->saved_skb, ddev->dep_rwt,
 693                                 digital_in_recv_dep_res, data_exch);
 694        if (rc)
 695                kfree_skb(ddev->saved_skb);
 696
 697        return rc;
 698}
 699
 700static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
 701                                    struct sk_buff *resp)
 702{
 703        struct digital_data_exch *data_exch = arg;
 704        struct digital_dep_req_res *dep_res;
 705        u8 pfb;
 706        uint size;
 707        int rc;
 708        u8 rtox;
 709
 710        if (IS_ERR(resp)) {
 711                rc = PTR_ERR(resp);
 712                resp = NULL;
 713
 714                if ((rc == -EIO || (rc == -ETIMEDOUT && ddev->nack_count)) &&
 715                    (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
 716                        ddev->atn_count = 0;
 717
 718                        rc = digital_in_send_nack(ddev, data_exch);
 719                        if (rc)
 720                                goto error;
 721
 722                        return;
 723                } else if ((rc == -ETIMEDOUT) &&
 724                           (ddev->atn_count++ < DIGITAL_NFC_DEP_N_RETRY_ATN)) {
 725                        ddev->nack_count = 0;
 726
 727                        rc = digital_in_send_atn(ddev, data_exch);
 728                        if (rc)
 729                                goto error;
 730
 731                        return;
 732                }
 733
 734                goto exit;
 735        }
 736
 737        rc = digital_skb_pull_dep_sod(ddev, resp);
 738        if (rc) {
 739                PROTOCOL_ERR("14.4.1.2");
 740                goto exit;
 741        }
 742
 743        rc = ddev->skb_check_crc(resp);
 744        if (rc) {
 745                if ((resp->len >= 4) &&
 746                    (ddev->nack_count++ < DIGITAL_NFC_DEP_N_RETRY_NACK)) {
 747                        ddev->atn_count = 0;
 748
 749                        rc = digital_in_send_nack(ddev, data_exch);
 750                        if (rc)
 751                                goto error;
 752
 753                        kfree_skb(resp);
 754
 755                        return;
 756                }
 757
 758                PROTOCOL_ERR("14.4.1.6");
 759                goto error;
 760        }
 761
 762        ddev->atn_count = 0;
 763        ddev->nack_count = 0;
 764
 765        if (resp->len > ddev->local_payload_max) {
 766                rc = -EMSGSIZE;
 767                goto exit;
 768        }
 769
 770        size = sizeof(struct digital_dep_req_res);
 771        dep_res = (struct digital_dep_req_res *)resp->data;
 772
 773        if (resp->len < size || dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
 774            dep_res->cmd != DIGITAL_CMD_DEP_RES) {
 775                rc = -EIO;
 776                goto error;
 777        }
 778
 779        pfb = dep_res->pfb;
 780
 781        if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
 782                PROTOCOL_ERR("14.8.2.1");
 783                rc = -EIO;
 784                goto error;
 785        }
 786
 787        if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
 788                rc = -EIO;
 789                goto exit;
 790        }
 791
 792        if (size > resp->len) {
 793                rc = -EIO;
 794                goto error;
 795        }
 796
 797        skb_pull(resp, size);
 798
 799        switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
 800        case DIGITAL_NFC_DEP_PFB_I_PDU:
 801                if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
 802                        PROTOCOL_ERR("14.12.3.3");
 803                        rc = -EIO;
 804                        goto error;
 805                }
 806
 807                ddev->curr_nfc_dep_pni =
 808                        DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
 809
 810                kfree_skb(ddev->saved_skb);
 811                ddev->saved_skb = NULL;
 812
 813                resp = digital_recv_dep_data_gather(ddev, pfb, resp,
 814                                                    digital_in_send_ack,
 815                                                    data_exch);
 816                if (IS_ERR(resp)) {
 817                        rc = PTR_ERR(resp);
 818                        resp = NULL;
 819                        goto error;
 820                }
 821
 822                /* If resp is NULL then we're still chaining so return and
 823                 * wait for the next part of the PDU.  Else, the PDU is
 824                 * complete so pass it up.
 825                 */
 826                if (!resp)
 827                        return;
 828
 829                rc = 0;
 830                break;
 831
 832        case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
 833                if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) {
 834                        PROTOCOL_ERR("14.12.4.5");
 835                        rc = -EIO;
 836                        goto exit;
 837                }
 838
 839                if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
 840                        PROTOCOL_ERR("14.12.3.3");
 841                        rc = -EIO;
 842                        goto exit;
 843                }
 844
 845                ddev->curr_nfc_dep_pni =
 846                        DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
 847
 848                if (!ddev->chaining_skb) {
 849                        PROTOCOL_ERR("14.12.4.3");
 850                        rc = -EIO;
 851                        goto exit;
 852                }
 853
 854                /* The initiator has received a valid ACK. Free the last sent
 855                 * PDU and keep on sending chained skb.
 856                 */
 857                kfree_skb(ddev->saved_skb);
 858                ddev->saved_skb = NULL;
 859
 860                rc = digital_in_send_dep_req(ddev, NULL,
 861                                             ddev->chaining_skb,
 862                                             ddev->data_exch);
 863                if (rc)
 864                        goto error;
 865
 866                goto free_resp;
 867
 868        case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
 869                if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) { /* ATN */
 870                        rc = digital_in_send_saved_skb(ddev, data_exch);
 871                        if (rc)
 872                                goto error;
 873
 874                        goto free_resp;
 875                }
 876
 877                if (ddev->atn_count || ddev->nack_count) {
 878                        PROTOCOL_ERR("14.12.4.4");
 879                        rc = -EIO;
 880                        goto error;
 881                }
 882
 883                rtox = DIGITAL_NFC_DEP_RTOX_VALUE(resp->data[0]);
 884                if (!rtox || rtox > DIGITAL_NFC_DEP_RTOX_MAX) {
 885                        PROTOCOL_ERR("14.8.4.1");
 886                        rc = -EIO;
 887                        goto error;
 888                }
 889
 890                rc = digital_in_send_rtox(ddev, data_exch, rtox);
 891                if (rc)
 892                        goto error;
 893
 894                goto free_resp;
 895        }
 896
 897exit:
 898        data_exch->cb(data_exch->cb_context, resp, rc);
 899
 900error:
 901        kfree(data_exch);
 902
 903        kfree_skb(ddev->chaining_skb);
 904        ddev->chaining_skb = NULL;
 905
 906        kfree_skb(ddev->saved_skb);
 907        ddev->saved_skb = NULL;
 908
 909        if (rc)
 910                kfree_skb(resp);
 911
 912        return;
 913
 914free_resp:
 915        dev_kfree_skb(resp);
 916}
 917
 918int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
 919                            struct nfc_target *target, struct sk_buff *skb,
 920                            struct digital_data_exch *data_exch)
 921{
 922        struct digital_dep_req_res *dep_req;
 923        struct sk_buff *chaining_skb, *tmp_skb;
 924        int rc;
 925
 926        skb_push(skb, sizeof(struct digital_dep_req_res));
 927
 928        dep_req = (struct digital_dep_req_res *)skb->data;
 929
 930        dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
 931        dep_req->cmd = DIGITAL_CMD_DEP_REQ;
 932        dep_req->pfb = ddev->curr_nfc_dep_pni;
 933
 934        ddev->atn_count = 0;
 935        ddev->nack_count = 0;
 936
 937        chaining_skb = ddev->chaining_skb;
 938
 939        tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_req, data_exch);
 940        if (IS_ERR(tmp_skb))
 941                return PTR_ERR(tmp_skb);
 942
 943        digital_skb_push_dep_sod(ddev, tmp_skb);
 944
 945        ddev->skb_add_crc(tmp_skb);
 946
 947        ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
 948
 949        rc = digital_in_send_cmd(ddev, tmp_skb, ddev->dep_rwt,
 950                                 digital_in_recv_dep_res, data_exch);
 951        if (rc) {
 952                if (tmp_skb != skb)
 953                        kfree_skb(tmp_skb);
 954
 955                kfree_skb(chaining_skb);
 956                ddev->chaining_skb = NULL;
 957
 958                kfree_skb(ddev->saved_skb);
 959                ddev->saved_skb = NULL;
 960        }
 961
 962        return rc;
 963}
 964
 965static void digital_tg_set_rf_tech(struct nfc_digital_dev *ddev, u8 rf_tech)
 966{
 967        ddev->curr_rf_tech = rf_tech;
 968
 969        ddev->skb_add_crc = digital_skb_add_crc_none;
 970        ddev->skb_check_crc = digital_skb_check_crc_none;
 971
 972        if (DIGITAL_DRV_CAPS_TG_CRC(ddev))
 973                return;
 974
 975        switch (ddev->curr_rf_tech) {
 976        case NFC_DIGITAL_RF_TECH_106A:
 977                ddev->skb_add_crc = digital_skb_add_crc_a;
 978                ddev->skb_check_crc = digital_skb_check_crc_a;
 979                break;
 980
 981        case NFC_DIGITAL_RF_TECH_212F:
 982        case NFC_DIGITAL_RF_TECH_424F:
 983                ddev->skb_add_crc = digital_skb_add_crc_f;
 984                ddev->skb_check_crc = digital_skb_check_crc_f;
 985                break;
 986
 987        default:
 988                break;
 989        }
 990}
 991
 992static int digital_tg_send_ack(struct nfc_digital_dev *ddev,
 993                               struct digital_data_exch *data_exch)
 994{
 995        struct digital_dep_req_res *dep_res;
 996        struct sk_buff *skb;
 997        int rc;
 998
 999        skb = digital_skb_alloc(ddev, 1);
1000        if (!skb)
1001                return -ENOMEM;
1002
1003        skb_push(skb, sizeof(struct digital_dep_req_res));
1004
1005        dep_res = (struct digital_dep_req_res *)skb->data;
1006
1007        dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1008        dep_res->cmd = DIGITAL_CMD_DEP_RES;
1009        dep_res->pfb = DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU |
1010                       ddev->curr_nfc_dep_pni;
1011
1012        if (ddev->did) {
1013                dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1014
1015                memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1016                       sizeof(ddev->did));
1017        }
1018
1019        ddev->curr_nfc_dep_pni =
1020                DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1021
1022        digital_skb_push_dep_sod(ddev, skb);
1023
1024        ddev->skb_add_crc(skb);
1025
1026        ddev->saved_skb = pskb_copy(skb, GFP_KERNEL);
1027
1028        rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1029                                 data_exch);
1030        if (rc) {
1031                kfree_skb(skb);
1032                kfree_skb(ddev->saved_skb);
1033                ddev->saved_skb = NULL;
1034        }
1035
1036        return rc;
1037}
1038
1039static int digital_tg_send_atn(struct nfc_digital_dev *ddev)
1040{
1041        struct digital_dep_req_res *dep_res;
1042        struct sk_buff *skb;
1043        int rc;
1044
1045        skb = digital_skb_alloc(ddev, 1);
1046        if (!skb)
1047                return -ENOMEM;
1048
1049        skb_push(skb, sizeof(struct digital_dep_req_res));
1050
1051        dep_res = (struct digital_dep_req_res *)skb->data;
1052
1053        dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1054        dep_res->cmd = DIGITAL_CMD_DEP_RES;
1055        dep_res->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU;
1056
1057        if (ddev->did) {
1058                dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1059
1060                memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1061                       sizeof(ddev->did));
1062        }
1063
1064        digital_skb_push_dep_sod(ddev, skb);
1065
1066        ddev->skb_add_crc(skb);
1067
1068        rc = digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
1069                                 NULL);
1070        if (rc)
1071                kfree_skb(skb);
1072
1073        return rc;
1074}
1075
1076static int digital_tg_send_saved_skb(struct nfc_digital_dev *ddev)
1077{
1078        int rc;
1079
1080        if (!ddev->saved_skb)
1081                return -EINVAL;
1082
1083        skb_get(ddev->saved_skb);
1084
1085        rc = digital_tg_send_cmd(ddev, ddev->saved_skb, 1500,
1086                                 digital_tg_recv_dep_req, NULL);
1087        if (rc)
1088                kfree_skb(ddev->saved_skb);
1089
1090        return rc;
1091}
1092
1093static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
1094                                    struct sk_buff *resp)
1095{
1096        int rc;
1097        struct digital_dep_req_res *dep_req;
1098        u8 pfb;
1099        size_t size;
1100
1101        if (IS_ERR(resp)) {
1102                rc = PTR_ERR(resp);
1103                resp = NULL;
1104                goto exit;
1105        }
1106
1107        rc = ddev->skb_check_crc(resp);
1108        if (rc) {
1109                PROTOCOL_ERR("14.4.1.6");
1110                goto exit;
1111        }
1112
1113        rc = digital_skb_pull_dep_sod(ddev, resp);
1114        if (rc) {
1115                PROTOCOL_ERR("14.4.1.2");
1116                goto exit;
1117        }
1118
1119        if (resp->len > ddev->local_payload_max) {
1120                rc = -EMSGSIZE;
1121                goto exit;
1122        }
1123
1124        size = sizeof(struct digital_dep_req_res);
1125        dep_req = (struct digital_dep_req_res *)resp->data;
1126
1127        if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1128            dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
1129                rc = -EIO;
1130                goto exit;
1131        }
1132
1133        pfb = dep_req->pfb;
1134
1135        if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb)) {
1136                if (ddev->did && (ddev->did == resp->data[3])) {
1137                        size++;
1138                } else {
1139                        rc = -EIO;
1140                        goto exit;
1141                }
1142        } else if (ddev->did) {
1143                rc = -EIO;
1144                goto exit;
1145        }
1146
1147        if (DIGITAL_NFC_DEP_NAD_BIT_SET(pfb)) {
1148                rc = -EIO;
1149                goto exit;
1150        }
1151
1152        if (size > resp->len) {
1153                rc = -EIO;
1154                goto exit;
1155        }
1156
1157        skb_pull(resp, size);
1158
1159        switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
1160        case DIGITAL_NFC_DEP_PFB_I_PDU:
1161                pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
1162
1163                if (ddev->atn_count) {
1164                        /* The target has received (and replied to) at least one
1165                         * ATN DEP_REQ.
1166                         */
1167                        ddev->atn_count = 0;
1168
1169                        /* pni of resp PDU equal to the target current pni - 1
1170                         * means resp is the previous DEP_REQ PDU received from
1171                         * the initiator so the target replies with saved_skb
1172                         * which is the previous DEP_RES saved in
1173                         * digital_tg_send_dep_res().
1174                         */
1175                        if (DIGITAL_NFC_DEP_PFB_PNI(pfb) ==
1176                          DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni - 1)) {
1177                                rc = digital_tg_send_saved_skb(ddev);
1178                                if (rc)
1179                                        goto exit;
1180
1181                                goto free_resp;
1182                        }
1183
1184                        /* atn_count > 0 and PDU pni != curr_nfc_dep_pni - 1
1185                         * means the target probably did not received the last
1186                         * DEP_REQ PDU sent by the initiator. The target
1187                         * fallbacks to normal processing then.
1188                         */
1189                }
1190
1191                if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
1192                        PROTOCOL_ERR("14.12.3.4");
1193                        rc = -EIO;
1194                        goto exit;
1195                }
1196
1197                kfree_skb(ddev->saved_skb);
1198                ddev->saved_skb = NULL;
1199
1200                resp = digital_recv_dep_data_gather(ddev, pfb, resp,
1201                                                    digital_tg_send_ack, NULL);
1202                if (IS_ERR(resp)) {
1203                        rc = PTR_ERR(resp);
1204                        resp = NULL;
1205                        goto exit;
1206                }
1207
1208                /* If resp is NULL then we're still chaining so return and
1209                 * wait for the next part of the PDU.  Else, the PDU is
1210                 * complete so pass it up.
1211                 */
1212                if (!resp)
1213                        return;
1214
1215                rc = 0;
1216                break;
1217        case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
1218                if (DIGITAL_NFC_DEP_NACK_BIT_SET(pfb)) { /* NACK */
1219                        if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) !=
1220                                                ddev->curr_nfc_dep_pni) {
1221                                rc = -EIO;
1222                                goto exit;
1223                        }
1224
1225                        ddev->atn_count = 0;
1226
1227                        rc = digital_tg_send_saved_skb(ddev);
1228                        if (rc)
1229                                goto exit;
1230
1231                        goto free_resp;
1232                }
1233
1234                /* ACK */
1235                if (ddev->atn_count) {
1236                        /* The target has previously recevied one or more ATN
1237                         * PDUs.
1238                         */
1239                        ddev->atn_count = 0;
1240
1241                        /* If the ACK PNI is equal to the target PNI - 1 means
1242                         * that the initiator did not receive the previous PDU
1243                         * sent by the target so re-send it.
1244                         */
1245                        if (DIGITAL_NFC_DEP_PFB_PNI(pfb + 1) ==
1246                                                ddev->curr_nfc_dep_pni) {
1247                                rc = digital_tg_send_saved_skb(ddev);
1248                                if (rc)
1249                                        goto exit;
1250
1251                                goto free_resp;
1252                        }
1253
1254                        /* Otherwise, the target did not receive the previous
1255                         * ACK PDU from the initiator. Fallback to normal
1256                         * processing of chained PDU then.
1257                         */
1258                }
1259
1260                /* Keep on sending chained PDU */
1261                if (!ddev->chaining_skb ||
1262                    DIGITAL_NFC_DEP_PFB_PNI(pfb) !=
1263                                        ddev->curr_nfc_dep_pni) {
1264                        rc = -EIO;
1265                        goto exit;
1266                }
1267
1268                kfree_skb(ddev->saved_skb);
1269                ddev->saved_skb = NULL;
1270
1271                rc = digital_tg_send_dep_res(ddev, ddev->chaining_skb);
1272                if (rc)
1273                        goto exit;
1274
1275                goto free_resp;
1276        case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
1277                if (DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
1278                        rc = -EINVAL;
1279                        goto exit;
1280                }
1281
1282                rc = digital_tg_send_atn(ddev);
1283                if (rc)
1284                        goto exit;
1285
1286                ddev->atn_count++;
1287
1288                goto free_resp;
1289        }
1290
1291        rc = nfc_tm_data_received(ddev->nfc_dev, resp);
1292
1293exit:
1294        kfree_skb(ddev->chaining_skb);
1295        ddev->chaining_skb = NULL;
1296
1297        ddev->atn_count = 0;
1298
1299        kfree_skb(ddev->saved_skb);
1300        ddev->saved_skb = NULL;
1301
1302        if (rc)
1303                kfree_skb(resp);
1304
1305        return;
1306
1307free_resp:
1308        dev_kfree_skb(resp);
1309}
1310
1311int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
1312{
1313        struct digital_dep_req_res *dep_res;
1314        struct sk_buff *chaining_skb, *tmp_skb;
1315        int rc;
1316
1317        skb_push(skb, sizeof(struct digital_dep_req_res));
1318
1319        dep_res = (struct digital_dep_req_res *)skb->data;
1320
1321        dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1322        dep_res->cmd = DIGITAL_CMD_DEP_RES;
1323        dep_res->pfb = ddev->curr_nfc_dep_pni;
1324
1325        if (ddev->did) {
1326                dep_res->pfb |= DIGITAL_NFC_DEP_PFB_DID_BIT;
1327
1328                memcpy(skb_put(skb, sizeof(ddev->did)), &ddev->did,
1329                       sizeof(ddev->did));
1330        }
1331
1332        ddev->curr_nfc_dep_pni =
1333                DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
1334
1335        chaining_skb = ddev->chaining_skb;
1336
1337        tmp_skb = digital_send_dep_data_prep(ddev, skb, dep_res, NULL);
1338        if (IS_ERR(tmp_skb))
1339                return PTR_ERR(tmp_skb);
1340
1341        digital_skb_push_dep_sod(ddev, tmp_skb);
1342
1343        ddev->skb_add_crc(tmp_skb);
1344
1345        ddev->saved_skb = pskb_copy(tmp_skb, GFP_KERNEL);
1346
1347        rc = digital_tg_send_cmd(ddev, tmp_skb, 1500, digital_tg_recv_dep_req,
1348                                 NULL);
1349        if (rc) {
1350                if (tmp_skb != skb)
1351                        kfree_skb(tmp_skb);
1352
1353                kfree_skb(chaining_skb);
1354                ddev->chaining_skb = NULL;
1355
1356                kfree_skb(ddev->saved_skb);
1357                ddev->saved_skb = NULL;
1358        }
1359
1360        return rc;
1361}
1362
1363static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
1364                                             void *arg, struct sk_buff *resp)
1365{
1366        u8 rf_tech = (unsigned long)arg;
1367
1368        if (IS_ERR(resp))
1369                return;
1370
1371        digital_tg_set_rf_tech(ddev, rf_tech);
1372
1373        digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
1374
1375        digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
1376
1377        dev_kfree_skb(resp);
1378}
1379
1380static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
1381                                   u8 rf_tech)
1382{
1383        struct digital_psl_res *psl_res;
1384        struct sk_buff *skb;
1385        int rc;
1386
1387        skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
1388        if (!skb)
1389                return -ENOMEM;
1390
1391        skb_put(skb, sizeof(struct digital_psl_res));
1392
1393        psl_res = (struct digital_psl_res *)skb->data;
1394
1395        psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1396        psl_res->cmd = DIGITAL_CMD_PSL_RES;
1397        psl_res->did = did;
1398
1399        digital_skb_push_dep_sod(ddev, skb);
1400
1401        ddev->skb_add_crc(skb);
1402
1403        ddev->curr_nfc_dep_pni = 0;
1404
1405        rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
1406                                 (void *)(unsigned long)rf_tech);
1407        if (rc)
1408                kfree_skb(skb);
1409
1410        return rc;
1411}
1412
1413static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
1414                                    struct sk_buff *resp)
1415{
1416        int rc;
1417        struct digital_psl_req *psl_req;
1418        u8 rf_tech;
1419        u8 dsi, payload_size, payload_bits;
1420
1421        if (IS_ERR(resp)) {
1422                rc = PTR_ERR(resp);
1423                resp = NULL;
1424                goto exit;
1425        }
1426
1427        rc = ddev->skb_check_crc(resp);
1428        if (rc) {
1429                PROTOCOL_ERR("14.4.1.6");
1430                goto exit;
1431        }
1432
1433        rc = digital_skb_pull_dep_sod(ddev, resp);
1434        if (rc) {
1435                PROTOCOL_ERR("14.4.1.2");
1436                goto exit;
1437        }
1438
1439        psl_req = (struct digital_psl_req *)resp->data;
1440
1441        if (resp->len != sizeof(struct digital_psl_req) ||
1442            psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1443            psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
1444                rc = -EIO;
1445                goto exit;
1446        }
1447
1448        dsi = (psl_req->brs >> 3) & 0x07;
1449        switch (dsi) {
1450        case 0:
1451                rf_tech = NFC_DIGITAL_RF_TECH_106A;
1452                break;
1453        case 1:
1454                rf_tech = NFC_DIGITAL_RF_TECH_212F;
1455                break;
1456        case 2:
1457                rf_tech = NFC_DIGITAL_RF_TECH_424F;
1458                break;
1459        default:
1460                pr_err("Unsupported dsi value %d\n", dsi);
1461                goto exit;
1462        }
1463
1464        payload_bits = DIGITAL_PAYLOAD_FSL_TO_BITS(psl_req->fsl);
1465        payload_size = digital_payload_bits_to_size(payload_bits);
1466
1467        if (!payload_size || (payload_size > min(ddev->local_payload_max,
1468                                                 ddev->remote_payload_max))) {
1469                rc = -EINVAL;
1470                goto exit;
1471        }
1472
1473        ddev->local_payload_max = payload_size;
1474        ddev->remote_payload_max = payload_size;
1475
1476        rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
1477
1478exit:
1479        kfree_skb(resp);
1480}
1481
1482static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
1483                                             void *arg, struct sk_buff *resp)
1484{
1485        int offset;
1486
1487        if (IS_ERR(resp)) {
1488                digital_poll_next_tech(ddev);
1489                return;
1490        }
1491
1492        offset = 2;
1493        if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
1494                offset++;
1495
1496        ddev->atn_count = 0;
1497
1498        if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
1499                digital_tg_recv_psl_req(ddev, arg, resp);
1500        else
1501                digital_tg_recv_dep_req(ddev, arg, resp);
1502}
1503
1504static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
1505                                   struct digital_atr_req *atr_req)
1506{
1507        struct digital_atr_res *atr_res;
1508        struct sk_buff *skb;
1509        u8 *gb, payload_bits;
1510        size_t gb_len;
1511        int rc;
1512
1513        gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
1514        if (!gb)
1515                gb_len = 0;
1516
1517        skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
1518        if (!skb)
1519                return -ENOMEM;
1520
1521        skb_put(skb, sizeof(struct digital_atr_res));
1522        atr_res = (struct digital_atr_res *)skb->data;
1523
1524        memset(atr_res, 0, sizeof(struct digital_atr_res));
1525
1526        atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
1527        atr_res->cmd = DIGITAL_CMD_ATR_RES;
1528        memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
1529        atr_res->to = DIGITAL_NFC_DEP_TG_MAX_WT;
1530
1531        ddev->local_payload_max = DIGITAL_PAYLOAD_SIZE_MAX;
1532        payload_bits = digital_payload_size_to_bits(ddev->local_payload_max);
1533        atr_res->pp = DIGITAL_PAYLOAD_BITS_TO_PP(payload_bits);
1534
1535        if (gb_len) {
1536                skb_put(skb, gb_len);
1537
1538                atr_res->pp |= DIGITAL_GB_BIT;
1539                memcpy(atr_res->gb, gb, gb_len);
1540        }
1541
1542        digital_skb_push_dep_sod(ddev, skb);
1543
1544        ddev->skb_add_crc(skb);
1545
1546        ddev->curr_nfc_dep_pni = 0;
1547
1548        rc = digital_tg_send_cmd(ddev, skb, 999,
1549                                 digital_tg_send_atr_res_complete, NULL);
1550        if (rc)
1551                kfree_skb(skb);
1552
1553        return rc;
1554}
1555
1556void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
1557                             struct sk_buff *resp)
1558{
1559        int rc;
1560        struct digital_atr_req *atr_req;
1561        size_t gb_len, min_size;
1562        u8 poll_tech_count, payload_bits;
1563
1564        if (IS_ERR(resp)) {
1565                rc = PTR_ERR(resp);
1566                resp = NULL;
1567                goto exit;
1568        }
1569
1570        if (!resp->len) {
1571                rc = -EIO;
1572                goto exit;
1573        }
1574
1575        if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
1576                min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
1577                digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_106A);
1578        } else {
1579                min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
1580                digital_tg_set_rf_tech(ddev, NFC_DIGITAL_RF_TECH_212F);
1581        }
1582
1583        if (resp->len < min_size) {
1584                rc = -EIO;
1585                goto exit;
1586        }
1587
1588        ddev->curr_protocol = NFC_PROTO_NFC_DEP_MASK;
1589
1590        rc = ddev->skb_check_crc(resp);
1591        if (rc) {
1592                PROTOCOL_ERR("14.4.1.6");
1593                goto exit;
1594        }
1595
1596        rc = digital_skb_pull_dep_sod(ddev, resp);
1597        if (rc) {
1598                PROTOCOL_ERR("14.4.1.2");
1599                goto exit;
1600        }
1601
1602        atr_req = (struct digital_atr_req *)resp->data;
1603
1604        if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
1605            atr_req->cmd != DIGITAL_CMD_ATR_REQ ||
1606            atr_req->did > DIGITAL_DID_MAX) {
1607                rc = -EINVAL;
1608                goto exit;
1609        }
1610
1611        payload_bits = DIGITAL_PAYLOAD_PP_TO_BITS(atr_req->pp);
1612        ddev->remote_payload_max = digital_payload_bits_to_size(payload_bits);
1613
1614        if (!ddev->remote_payload_max) {
1615                rc = -EINVAL;
1616                goto exit;
1617        }
1618
1619        ddev->did = atr_req->did;
1620
1621        rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1622                                     NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
1623        if (rc)
1624                goto exit;
1625
1626        rc = digital_tg_send_atr_res(ddev, atr_req);
1627        if (rc)
1628                goto exit;
1629
1630        gb_len = resp->len - sizeof(struct digital_atr_req);
1631
1632        poll_tech_count = ddev->poll_tech_count;
1633        ddev->poll_tech_count = 0;
1634
1635        rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1636                              NFC_COMM_PASSIVE, atr_req->gb, gb_len);
1637        if (rc) {
1638                ddev->poll_tech_count = poll_tech_count;
1639                goto exit;
1640        }
1641
1642        rc = 0;
1643exit:
1644        if (rc)
1645                digital_poll_next_tech(ddev);
1646
1647        dev_kfree_skb(resp);
1648}
1649