linux/drivers/nfc/microread/microread.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * HCI based Driver for Inside Secure microread NFC Chip
   4 *
   5 * Copyright (C) 2013  Intel Corporation. All rights reserved.
   6 */
   7
   8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   9
  10#include <linux/module.h>
  11#include <linux/delay.h>
  12#include <linux/slab.h>
  13#include <linux/crc-ccitt.h>
  14
  15#include <linux/nfc.h>
  16#include <net/nfc/nfc.h>
  17#include <net/nfc/hci.h>
  18#include <net/nfc/llc.h>
  19
  20#include "microread.h"
  21
  22/* Proprietary gates, events, commands and registers */
  23/* Admin */
  24#define MICROREAD_GATE_ID_ADM NFC_HCI_ADMIN_GATE
  25#define MICROREAD_GATE_ID_MGT 0x01
  26#define MICROREAD_GATE_ID_OS 0x02
  27#define MICROREAD_GATE_ID_TESTRF 0x03
  28#define MICROREAD_GATE_ID_LOOPBACK NFC_HCI_LOOPBACK_GATE
  29#define MICROREAD_GATE_ID_IDT NFC_HCI_ID_MGMT_GATE
  30#define MICROREAD_GATE_ID_LMS NFC_HCI_LINK_MGMT_GATE
  31
  32/* Reader */
  33#define MICROREAD_GATE_ID_MREAD_GEN 0x10
  34#define MICROREAD_GATE_ID_MREAD_ISO_B NFC_HCI_RF_READER_B_GATE
  35#define MICROREAD_GATE_ID_MREAD_NFC_T1 0x12
  36#define MICROREAD_GATE_ID_MREAD_ISO_A NFC_HCI_RF_READER_A_GATE
  37#define MICROREAD_GATE_ID_MREAD_NFC_T3 0x14
  38#define MICROREAD_GATE_ID_MREAD_ISO_15_3 0x15
  39#define MICROREAD_GATE_ID_MREAD_ISO_15_2 0x16
  40#define MICROREAD_GATE_ID_MREAD_ISO_B_3 0x17
  41#define MICROREAD_GATE_ID_MREAD_BPRIME 0x18
  42#define MICROREAD_GATE_ID_MREAD_ISO_A_3 0x19
  43
  44/* Card */
  45#define MICROREAD_GATE_ID_MCARD_GEN 0x20
  46#define MICROREAD_GATE_ID_MCARD_ISO_B 0x21
  47#define MICROREAD_GATE_ID_MCARD_BPRIME 0x22
  48#define MICROREAD_GATE_ID_MCARD_ISO_A 0x23
  49#define MICROREAD_GATE_ID_MCARD_NFC_T3 0x24
  50#define MICROREAD_GATE_ID_MCARD_ISO_15_3 0x25
  51#define MICROREAD_GATE_ID_MCARD_ISO_15_2 0x26
  52#define MICROREAD_GATE_ID_MCARD_ISO_B_2 0x27
  53#define MICROREAD_GATE_ID_MCARD_ISO_CUSTOM 0x28
  54#define MICROREAD_GATE_ID_SECURE_ELEMENT 0x2F
  55
  56/* P2P */
  57#define MICROREAD_GATE_ID_P2P_GEN 0x30
  58#define MICROREAD_GATE_ID_P2P_TARGET 0x31
  59#define MICROREAD_PAR_P2P_TARGET_MODE 0x01
  60#define MICROREAD_PAR_P2P_TARGET_GT 0x04
  61#define MICROREAD_GATE_ID_P2P_INITIATOR 0x32
  62#define MICROREAD_PAR_P2P_INITIATOR_GI 0x01
  63#define MICROREAD_PAR_P2P_INITIATOR_GT 0x03
  64
  65/* Those pipes are created/opened by default in the chip */
  66#define MICROREAD_PIPE_ID_LMS 0x00
  67#define MICROREAD_PIPE_ID_ADMIN 0x01
  68#define MICROREAD_PIPE_ID_MGT 0x02
  69#define MICROREAD_PIPE_ID_OS 0x03
  70#define MICROREAD_PIPE_ID_HDS_LOOPBACK 0x04
  71#define MICROREAD_PIPE_ID_HDS_IDT 0x05
  72#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B 0x08
  73#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_BPRIME 0x09
  74#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_A 0x0A
  75#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_3 0x0B
  76#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_15_2 0x0C
  77#define MICROREAD_PIPE_ID_HDS_MCARD_NFC_T3 0x0D
  78#define MICROREAD_PIPE_ID_HDS_MCARD_ISO_B_2 0x0E
  79#define MICROREAD_PIPE_ID_HDS_MCARD_CUSTOM 0x0F
  80#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B 0x10
  81#define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1 0x11
  82#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A 0x12
  83#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_3 0x13
  84#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_15_2 0x14
  85#define MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3 0x15
  86#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_B_3 0x16
  87#define MICROREAD_PIPE_ID_HDS_MREAD_BPRIME 0x17
  88#define MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3 0x18
  89#define MICROREAD_PIPE_ID_HDS_MREAD_GEN 0x1B
  90#define MICROREAD_PIPE_ID_HDS_STACKED_ELEMENT 0x1C
  91#define MICROREAD_PIPE_ID_HDS_INSTANCES 0x1D
  92#define MICROREAD_PIPE_ID_HDS_TESTRF 0x1E
  93#define MICROREAD_PIPE_ID_HDS_P2P_TARGET 0x1F
  94#define MICROREAD_PIPE_ID_HDS_P2P_INITIATOR 0x20
  95
  96/* Events */
  97#define MICROREAD_EVT_MREAD_DISCOVERY_OCCURED NFC_HCI_EVT_TARGET_DISCOVERED
  98#define MICROREAD_EVT_MREAD_CARD_FOUND 0x3D
  99#define MICROREAD_EMCF_A_ATQA 0
 100#define MICROREAD_EMCF_A_SAK 2
 101#define MICROREAD_EMCF_A_LEN 3
 102#define MICROREAD_EMCF_A_UID 4
 103#define MICROREAD_EMCF_A3_ATQA 0
 104#define MICROREAD_EMCF_A3_SAK 2
 105#define MICROREAD_EMCF_A3_LEN 3
 106#define MICROREAD_EMCF_A3_UID 4
 107#define MICROREAD_EMCF_B_UID 0
 108#define MICROREAD_EMCF_T1_ATQA 0
 109#define MICROREAD_EMCF_T1_UID 4
 110#define MICROREAD_EMCF_T3_UID 0
 111#define MICROREAD_EVT_MREAD_DISCOVERY_START NFC_HCI_EVT_READER_REQUESTED
 112#define MICROREAD_EVT_MREAD_DISCOVERY_START_SOME 0x3E
 113#define MICROREAD_EVT_MREAD_DISCOVERY_STOP NFC_HCI_EVT_END_OPERATION
 114#define MICROREAD_EVT_MREAD_SIM_REQUESTS 0x3F
 115#define MICROREAD_EVT_MCARD_EXCHANGE NFC_HCI_EVT_TARGET_DISCOVERED
 116#define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF 0x20
 117#define MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF 0x21
 118#define MICROREAD_EVT_MCARD_FIELD_ON 0x11
 119#define MICROREAD_EVT_P2P_TARGET_ACTIVATED 0x13
 120#define MICROREAD_EVT_P2P_TARGET_DEACTIVATED 0x12
 121#define MICROREAD_EVT_MCARD_FIELD_OFF 0x14
 122
 123/* Commands */
 124#define MICROREAD_CMD_MREAD_EXCHANGE 0x10
 125#define MICROREAD_CMD_MREAD_SUBSCRIBE 0x3F
 126
 127/* Hosts IDs */
 128#define MICROREAD_ELT_ID_HDS NFC_HCI_TERMINAL_HOST_ID
 129#define MICROREAD_ELT_ID_SIM NFC_HCI_UICC_HOST_ID
 130#define MICROREAD_ELT_ID_SE1 0x03
 131#define MICROREAD_ELT_ID_SE2 0x04
 132#define MICROREAD_ELT_ID_SE3 0x05
 133
 134static struct nfc_hci_gate microread_gates[] = {
 135        {MICROREAD_GATE_ID_ADM, MICROREAD_PIPE_ID_ADMIN},
 136        {MICROREAD_GATE_ID_LOOPBACK, MICROREAD_PIPE_ID_HDS_LOOPBACK},
 137        {MICROREAD_GATE_ID_IDT, MICROREAD_PIPE_ID_HDS_IDT},
 138        {MICROREAD_GATE_ID_LMS, MICROREAD_PIPE_ID_LMS},
 139        {MICROREAD_GATE_ID_MREAD_ISO_B, MICROREAD_PIPE_ID_HDS_MREAD_ISO_B},
 140        {MICROREAD_GATE_ID_MREAD_ISO_A, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A},
 141        {MICROREAD_GATE_ID_MREAD_ISO_A_3, MICROREAD_PIPE_ID_HDS_MREAD_ISO_A_3},
 142        {MICROREAD_GATE_ID_MGT, MICROREAD_PIPE_ID_MGT},
 143        {MICROREAD_GATE_ID_OS, MICROREAD_PIPE_ID_OS},
 144        {MICROREAD_GATE_ID_MREAD_NFC_T1, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T1},
 145        {MICROREAD_GATE_ID_MREAD_NFC_T3, MICROREAD_PIPE_ID_HDS_MREAD_NFC_T3},
 146        {MICROREAD_GATE_ID_P2P_TARGET, MICROREAD_PIPE_ID_HDS_P2P_TARGET},
 147        {MICROREAD_GATE_ID_P2P_INITIATOR, MICROREAD_PIPE_ID_HDS_P2P_INITIATOR}
 148};
 149
 150/* Largest headroom needed for outgoing custom commands */
 151#define MICROREAD_CMDS_HEADROOM 2
 152#define MICROREAD_CMD_TAILROOM  2
 153
 154struct microread_info {
 155        struct nfc_phy_ops *phy_ops;
 156        void *phy_id;
 157
 158        struct nfc_hci_dev *hdev;
 159
 160        int async_cb_type;
 161        data_exchange_cb_t async_cb;
 162        void *async_cb_context;
 163};
 164
 165static int microread_open(struct nfc_hci_dev *hdev)
 166{
 167        struct microread_info *info = nfc_hci_get_clientdata(hdev);
 168
 169        return info->phy_ops->enable(info->phy_id);
 170}
 171
 172static void microread_close(struct nfc_hci_dev *hdev)
 173{
 174        struct microread_info *info = nfc_hci_get_clientdata(hdev);
 175
 176        info->phy_ops->disable(info->phy_id);
 177}
 178
 179static int microread_hci_ready(struct nfc_hci_dev *hdev)
 180{
 181        int r;
 182        u8 param[4];
 183
 184        param[0] = 0x03;
 185        r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
 186                             MICROREAD_CMD_MREAD_SUBSCRIBE, param, 1, NULL);
 187        if (r)
 188                return r;
 189
 190        r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_A_3,
 191                             MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
 192        if (r)
 193                return r;
 194
 195        param[0] = 0x00;
 196        param[1] = 0x03;
 197        param[2] = 0x00;
 198        r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_ISO_B,
 199                             MICROREAD_CMD_MREAD_SUBSCRIBE, param, 3, NULL);
 200        if (r)
 201                return r;
 202
 203        r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T1,
 204                             MICROREAD_CMD_MREAD_SUBSCRIBE, NULL, 0, NULL);
 205        if (r)
 206                return r;
 207
 208        param[0] = 0xFF;
 209        param[1] = 0xFF;
 210        param[2] = 0x00;
 211        param[3] = 0x00;
 212        r = nfc_hci_send_cmd(hdev, MICROREAD_GATE_ID_MREAD_NFC_T3,
 213                             MICROREAD_CMD_MREAD_SUBSCRIBE, param, 4, NULL);
 214
 215        return r;
 216}
 217
 218static int microread_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
 219{
 220        struct microread_info *info = nfc_hci_get_clientdata(hdev);
 221
 222        return info->phy_ops->write(info->phy_id, skb);
 223}
 224
 225static int microread_start_poll(struct nfc_hci_dev *hdev,
 226                                u32 im_protocols, u32 tm_protocols)
 227{
 228        int r;
 229
 230        u8 param[2];
 231        u8 mode;
 232
 233        param[0] = 0x00;
 234        param[1] = 0x00;
 235
 236        if (im_protocols & NFC_PROTO_ISO14443_MASK)
 237                param[0] |= (1 << 2);
 238
 239        if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
 240                param[0] |= 1;
 241
 242        if (im_protocols & NFC_PROTO_MIFARE_MASK)
 243                param[1] |= 1;
 244
 245        if (im_protocols & NFC_PROTO_JEWEL_MASK)
 246                param[0] |= (1 << 1);
 247
 248        if (im_protocols & NFC_PROTO_FELICA_MASK)
 249                param[0] |= (1 << 5);
 250
 251        if (im_protocols & NFC_PROTO_NFC_DEP_MASK)
 252                param[1] |= (1 << 1);
 253
 254        if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
 255                hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
 256                                                       &hdev->gb_len);
 257                if (hdev->gb == NULL || hdev->gb_len == 0) {
 258                        im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
 259                        tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
 260                }
 261        }
 262
 263        r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
 264                               MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
 265        if (r)
 266                return r;
 267
 268        mode = 0xff;
 269        r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
 270                              MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
 271        if (r)
 272                return r;
 273
 274        if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
 275                r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
 276                                      MICROREAD_PAR_P2P_INITIATOR_GI,
 277                                      hdev->gb, hdev->gb_len);
 278                if (r)
 279                        return r;
 280        }
 281
 282        if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
 283                r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
 284                                      MICROREAD_PAR_P2P_TARGET_GT,
 285                                      hdev->gb, hdev->gb_len);
 286                if (r)
 287                        return r;
 288
 289                mode = 0x02;
 290                r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
 291                                      MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
 292                if (r)
 293                        return r;
 294        }
 295
 296        return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_MREAD_ISO_A,
 297                                  MICROREAD_EVT_MREAD_DISCOVERY_START_SOME,
 298                                  param, 2);
 299}
 300
 301static int microread_dep_link_up(struct nfc_hci_dev *hdev,
 302                                struct nfc_target *target, u8 comm_mode,
 303                                u8 *gb, size_t gb_len)
 304{
 305        struct sk_buff *rgb_skb = NULL;
 306        int r;
 307
 308        r = nfc_hci_get_param(hdev, target->hci_reader_gate,
 309                              MICROREAD_PAR_P2P_INITIATOR_GT, &rgb_skb);
 310        if (r < 0)
 311                return r;
 312
 313        if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
 314                r = -EPROTO;
 315                goto exit;
 316        }
 317
 318        r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
 319                                         rgb_skb->len);
 320        if (r == 0)
 321                r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
 322                                       NFC_RF_INITIATOR);
 323exit:
 324        kfree_skb(rgb_skb);
 325
 326        return r;
 327}
 328
 329static int microread_dep_link_down(struct nfc_hci_dev *hdev)
 330{
 331        return nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_INITIATOR,
 332                                  MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL, 0);
 333}
 334
 335static int microread_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
 336                                      struct nfc_target *target)
 337{
 338        switch (gate) {
 339        case MICROREAD_GATE_ID_P2P_INITIATOR:
 340                target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
 341                break;
 342        default:
 343                return -EPROTO;
 344        }
 345
 346        return 0;
 347}
 348
 349static int microread_complete_target_discovered(struct nfc_hci_dev *hdev,
 350                                                u8 gate,
 351                                                struct nfc_target *target)
 352{
 353        return 0;
 354}
 355
 356#define MICROREAD_CB_TYPE_READER_ALL 1
 357
 358static void microread_im_transceive_cb(void *context, struct sk_buff *skb,
 359                                       int err)
 360{
 361        struct microread_info *info = context;
 362
 363        switch (info->async_cb_type) {
 364        case MICROREAD_CB_TYPE_READER_ALL:
 365                if (err == 0) {
 366                        if (skb->len == 0) {
 367                                kfree_skb(skb);
 368                                info->async_cb(info->async_cb_context, NULL,
 369                                               -EPROTO);
 370                                return;
 371                        }
 372
 373                        if (skb->data[skb->len - 1] != 0) {
 374                                err = nfc_hci_result_to_errno(
 375                                                       skb->data[skb->len - 1]);
 376                                kfree_skb(skb);
 377                                info->async_cb(info->async_cb_context, NULL,
 378                                               err);
 379                                return;
 380                        }
 381
 382                        skb_trim(skb, skb->len - 1);    /* RF Error ind. */
 383                }
 384                info->async_cb(info->async_cb_context, skb, err);
 385                break;
 386        default:
 387                if (err == 0)
 388                        kfree_skb(skb);
 389                break;
 390        }
 391}
 392
 393/*
 394 * Returns:
 395 * <= 0: driver handled the data exchange
 396 *    1: driver doesn't especially handle, please do standard processing
 397 */
 398static int microread_im_transceive(struct nfc_hci_dev *hdev,
 399                                   struct nfc_target *target,
 400                                   struct sk_buff *skb, data_exchange_cb_t cb,
 401                                   void *cb_context)
 402{
 403        struct microread_info *info = nfc_hci_get_clientdata(hdev);
 404        u8 control_bits;
 405        u16 crc;
 406
 407        pr_info("data exchange to gate 0x%x\n", target->hci_reader_gate);
 408
 409        if (target->hci_reader_gate == MICROREAD_GATE_ID_P2P_INITIATOR) {
 410                *(u8 *)skb_push(skb, 1) = 0;
 411
 412                return nfc_hci_send_event(hdev, target->hci_reader_gate,
 413                                     MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_TO_RF,
 414                                     skb->data, skb->len);
 415        }
 416
 417        switch (target->hci_reader_gate) {
 418        case MICROREAD_GATE_ID_MREAD_ISO_A:
 419                control_bits = 0xCB;
 420                break;
 421        case MICROREAD_GATE_ID_MREAD_ISO_A_3:
 422                control_bits = 0xCB;
 423                break;
 424        case MICROREAD_GATE_ID_MREAD_ISO_B:
 425                control_bits = 0xCB;
 426                break;
 427        case MICROREAD_GATE_ID_MREAD_NFC_T1:
 428                control_bits = 0x1B;
 429
 430                crc = crc_ccitt(0xffff, skb->data, skb->len);
 431                crc = ~crc;
 432                skb_put_u8(skb, crc & 0xff);
 433                skb_put_u8(skb, crc >> 8);
 434                break;
 435        case MICROREAD_GATE_ID_MREAD_NFC_T3:
 436                control_bits = 0xDB;
 437                break;
 438        default:
 439                pr_info("Abort im_transceive to invalid gate 0x%x\n",
 440                        target->hci_reader_gate);
 441                return 1;
 442        }
 443
 444        *(u8 *)skb_push(skb, 1) = control_bits;
 445
 446        info->async_cb_type = MICROREAD_CB_TYPE_READER_ALL;
 447        info->async_cb = cb;
 448        info->async_cb_context = cb_context;
 449
 450        return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
 451                                      MICROREAD_CMD_MREAD_EXCHANGE,
 452                                      skb->data, skb->len,
 453                                      microread_im_transceive_cb, info);
 454}
 455
 456static int microread_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
 457{
 458        int r;
 459
 460        r = nfc_hci_send_event(hdev, MICROREAD_GATE_ID_P2P_TARGET,
 461                               MICROREAD_EVT_MCARD_EXCHANGE,
 462                               skb->data, skb->len);
 463
 464        kfree_skb(skb);
 465
 466        return r;
 467}
 468
 469static void microread_target_discovered(struct nfc_hci_dev *hdev, u8 gate,
 470                                        struct sk_buff *skb)
 471{
 472        struct nfc_target *targets;
 473        int r = 0;
 474
 475        pr_info("target discovered to gate 0x%x\n", gate);
 476
 477        targets = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
 478        if (targets == NULL) {
 479                r = -ENOMEM;
 480                goto exit;
 481        }
 482
 483        targets->hci_reader_gate = gate;
 484
 485        switch (gate) {
 486        case MICROREAD_GATE_ID_MREAD_ISO_A:
 487                targets->supported_protocols =
 488                      nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A_SAK]);
 489                targets->sens_res =
 490                         be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A_ATQA]);
 491                targets->sel_res = skb->data[MICROREAD_EMCF_A_SAK];
 492                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A_LEN];
 493                if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
 494                        r = -EINVAL;
 495                        goto exit_free;
 496                }
 497                memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A_UID],
 498                       targets->nfcid1_len);
 499                break;
 500        case MICROREAD_GATE_ID_MREAD_ISO_A_3:
 501                targets->supported_protocols =
 502                      nfc_hci_sak_to_protocol(skb->data[MICROREAD_EMCF_A3_SAK]);
 503                targets->sens_res =
 504                         be16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_A3_ATQA]);
 505                targets->sel_res = skb->data[MICROREAD_EMCF_A3_SAK];
 506                targets->nfcid1_len = skb->data[MICROREAD_EMCF_A3_LEN];
 507                if (targets->nfcid1_len > sizeof(targets->nfcid1)) {
 508                        r = -EINVAL;
 509                        goto exit_free;
 510                }
 511                memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_A3_UID],
 512                       targets->nfcid1_len);
 513                break;
 514        case MICROREAD_GATE_ID_MREAD_ISO_B:
 515                targets->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
 516                memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_B_UID], 4);
 517                targets->nfcid1_len = 4;
 518                break;
 519        case MICROREAD_GATE_ID_MREAD_NFC_T1:
 520                targets->supported_protocols = NFC_PROTO_JEWEL_MASK;
 521                targets->sens_res =
 522                        le16_to_cpu(*(u16 *)&skb->data[MICROREAD_EMCF_T1_ATQA]);
 523                memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T1_UID], 4);
 524                targets->nfcid1_len = 4;
 525                break;
 526        case MICROREAD_GATE_ID_MREAD_NFC_T3:
 527                targets->supported_protocols = NFC_PROTO_FELICA_MASK;
 528                memcpy(targets->nfcid1, &skb->data[MICROREAD_EMCF_T3_UID], 8);
 529                targets->nfcid1_len = 8;
 530                break;
 531        default:
 532                pr_info("discard target discovered to gate 0x%x\n", gate);
 533                goto exit_free;
 534        }
 535
 536        r = nfc_targets_found(hdev->ndev, targets, 1);
 537
 538exit_free:
 539        kfree(targets);
 540
 541exit:
 542        kfree_skb(skb);
 543
 544        if (r)
 545                pr_err("Failed to handle discovered target err=%d\n", r);
 546}
 547
 548static int microread_event_received(struct nfc_hci_dev *hdev, u8 pipe,
 549                                     u8 event, struct sk_buff *skb)
 550{
 551        int r;
 552        u8 gate = hdev->pipes[pipe].gate;
 553        u8 mode;
 554
 555        pr_info("Microread received event 0x%x to gate 0x%x\n", event, gate);
 556
 557        switch (event) {
 558        case MICROREAD_EVT_MREAD_CARD_FOUND:
 559                microread_target_discovered(hdev, gate, skb);
 560                return 0;
 561
 562        case MICROREAD_EVT_P2P_INITIATOR_EXCHANGE_FROM_RF:
 563                if (skb->len < 1) {
 564                        kfree_skb(skb);
 565                        return -EPROTO;
 566                }
 567
 568                if (skb->data[skb->len - 1]) {
 569                        kfree_skb(skb);
 570                        return -EIO;
 571                }
 572
 573                skb_trim(skb, skb->len - 1);
 574
 575                r = nfc_tm_data_received(hdev->ndev, skb);
 576                break;
 577
 578        case MICROREAD_EVT_MCARD_FIELD_ON:
 579        case MICROREAD_EVT_MCARD_FIELD_OFF:
 580                kfree_skb(skb);
 581                return 0;
 582
 583        case MICROREAD_EVT_P2P_TARGET_ACTIVATED:
 584                r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
 585                                     NFC_COMM_PASSIVE, skb->data,
 586                                     skb->len);
 587
 588                kfree_skb(skb);
 589                break;
 590
 591        case MICROREAD_EVT_MCARD_EXCHANGE:
 592                if (skb->len < 1) {
 593                        kfree_skb(skb);
 594                        return -EPROTO;
 595                }
 596
 597                if (skb->data[skb->len-1]) {
 598                        kfree_skb(skb);
 599                        return -EIO;
 600                }
 601
 602                skb_trim(skb, skb->len - 1);
 603
 604                r = nfc_tm_data_received(hdev->ndev, skb);
 605                break;
 606
 607        case MICROREAD_EVT_P2P_TARGET_DEACTIVATED:
 608                kfree_skb(skb);
 609
 610                mode = 0xff;
 611                r = nfc_hci_set_param(hdev, MICROREAD_GATE_ID_P2P_TARGET,
 612                                      MICROREAD_PAR_P2P_TARGET_MODE, &mode, 1);
 613                if (r)
 614                        break;
 615
 616                r = nfc_hci_send_event(hdev, gate,
 617                                       MICROREAD_EVT_MREAD_DISCOVERY_STOP, NULL,
 618                                       0);
 619                break;
 620
 621        default:
 622                return 1;
 623        }
 624
 625        return r;
 626}
 627
 628static struct nfc_hci_ops microread_hci_ops = {
 629        .open = microread_open,
 630        .close = microread_close,
 631        .hci_ready = microread_hci_ready,
 632        .xmit = microread_xmit,
 633        .start_poll = microread_start_poll,
 634        .dep_link_up = microread_dep_link_up,
 635        .dep_link_down = microread_dep_link_down,
 636        .target_from_gate = microread_target_from_gate,
 637        .complete_target_discovered = microread_complete_target_discovered,
 638        .im_transceive = microread_im_transceive,
 639        .tm_send = microread_tm_send,
 640        .check_presence = NULL,
 641        .event_received = microread_event_received,
 642};
 643
 644int microread_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
 645                    int phy_headroom, int phy_tailroom, int phy_payload,
 646                    struct nfc_hci_dev **hdev)
 647{
 648        struct microread_info *info;
 649        unsigned long quirks = 0;
 650        u32 protocols;
 651        struct nfc_hci_init_data init_data;
 652        int r;
 653
 654        info = kzalloc(sizeof(struct microread_info), GFP_KERNEL);
 655        if (!info) {
 656                r = -ENOMEM;
 657                goto err_info_alloc;
 658        }
 659
 660        info->phy_ops = phy_ops;
 661        info->phy_id = phy_id;
 662
 663        init_data.gate_count = ARRAY_SIZE(microread_gates);
 664        memcpy(init_data.gates, microread_gates, sizeof(microread_gates));
 665
 666        strcpy(init_data.session_id, "MICROREA");
 667
 668        set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
 669
 670        protocols = NFC_PROTO_JEWEL_MASK |
 671                    NFC_PROTO_MIFARE_MASK |
 672                    NFC_PROTO_FELICA_MASK |
 673                    NFC_PROTO_ISO14443_MASK |
 674                    NFC_PROTO_ISO14443_B_MASK |
 675                    NFC_PROTO_NFC_DEP_MASK;
 676
 677        info->hdev = nfc_hci_allocate_device(&microread_hci_ops, &init_data,
 678                                             quirks, protocols, llc_name,
 679                                             phy_headroom +
 680                                             MICROREAD_CMDS_HEADROOM,
 681                                             phy_tailroom +
 682                                             MICROREAD_CMD_TAILROOM,
 683                                             phy_payload);
 684        if (!info->hdev) {
 685                pr_err("Cannot allocate nfc hdev\n");
 686                r = -ENOMEM;
 687                goto err_alloc_hdev;
 688        }
 689
 690        nfc_hci_set_clientdata(info->hdev, info);
 691
 692        r = nfc_hci_register_device(info->hdev);
 693        if (r)
 694                goto err_regdev;
 695
 696        *hdev = info->hdev;
 697
 698        return 0;
 699
 700err_regdev:
 701        nfc_hci_free_device(info->hdev);
 702
 703err_alloc_hdev:
 704        kfree(info);
 705
 706err_info_alloc:
 707        return r;
 708}
 709EXPORT_SYMBOL(microread_probe);
 710
 711void microread_remove(struct nfc_hci_dev *hdev)
 712{
 713        struct microread_info *info = nfc_hci_get_clientdata(hdev);
 714
 715        nfc_hci_unregister_device(hdev);
 716        nfc_hci_free_device(hdev);
 717        kfree(info);
 718}
 719EXPORT_SYMBOL(microread_remove);
 720
 721MODULE_LICENSE("GPL");
 722MODULE_DESCRIPTION(DRIVER_DESC);
 723