linux/drivers/nfc/port100.c
<<
>>
Prefs
   1/*
   2 * Sony NFC Port-100 Series driver
   3 * Copyright (c) 2013, Intel Corporation.
   4 *
   5 * Partly based/Inspired by Stephen Tiedemann's nfcpy
   6 *
   7 * This program is free software; you can redistribute it and/or modify it
   8 * under the terms and conditions of the GNU General Public License,
   9 * version 2, as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope it will be useful, but WITHOUT
  12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  14 * more details.
  15 *
  16 */
  17
  18#include <linux/module.h>
  19#include <linux/usb.h>
  20#include <net/nfc/digital.h>
  21
  22#define VERSION "0.1"
  23
  24#define SONY_VENDOR_ID    0x054c
  25#define RCS380_PRODUCT_ID 0x06c1
  26
  27#define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK    | \
  28                           NFC_PROTO_MIFARE_MASK   | \
  29                           NFC_PROTO_FELICA_MASK   | \
  30                           NFC_PROTO_NFC_DEP_MASK  | \
  31                           NFC_PROTO_ISO14443_MASK | \
  32                           NFC_PROTO_ISO14443_B_MASK)
  33
  34#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
  35                              NFC_DIGITAL_DRV_CAPS_TG_CRC)
  36
  37/* Standard port100 frame definitions */
  38#define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
  39                                  + 2) /* data[0] CC, data[1] SCC */
  40#define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
  41
  42#define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
  43
  44/*
  45 * Max extended frame payload len, excluding CC and SCC
  46 * which are already in PORT100_FRAME_HEADER_LEN.
  47 */
  48#define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
  49
  50#define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
  51                                    Postamble (1) */
  52static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
  53        0x00, 0x00, 0xff, 0x00, 0xff, 0x00
  54};
  55
  56#define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
  57#define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
  58
  59/* start of frame */
  60#define PORT100_FRAME_SOF       0x00FF
  61#define PORT100_FRAME_EXT       0xFFFF
  62#define PORT100_FRAME_ACK       0x00FF
  63
  64/* Port-100 command: in or out */
  65#define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
  66#define PORT100_FRAME_DIR_OUT 0xD6
  67#define PORT100_FRAME_DIR_IN  0xD7
  68
  69/* Port-100 sub-command */
  70#define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
  71
  72#define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
  73#define PORT100_CMD_GET_COMMAND_TYPE     0x28
  74#define PORT100_CMD_SET_COMMAND_TYPE     0x2A
  75
  76#define PORT100_CMD_IN_SET_RF       0x00
  77#define PORT100_CMD_IN_SET_PROTOCOL 0x02
  78#define PORT100_CMD_IN_COMM_RF      0x04
  79
  80#define PORT100_CMD_TG_SET_RF       0x40
  81#define PORT100_CMD_TG_SET_PROTOCOL 0x42
  82#define PORT100_CMD_TG_SET_RF_OFF   0x46
  83#define PORT100_CMD_TG_COMM_RF      0x48
  84
  85#define PORT100_CMD_SWITCH_RF       0x06
  86
  87#define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
  88
  89#define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
  90        ((mask) & (0x01 << (cmd_type)))
  91#define PORT100_CMD_TYPE_0      0
  92#define PORT100_CMD_TYPE_1      1
  93
  94#define PORT100_CMD_STATUS_OK      0x00
  95#define PORT100_CMD_STATUS_TIMEOUT 0x80
  96
  97#define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
  98#define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK      0x02
  99
 100struct port100;
 101
 102typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
 103                                              struct sk_buff *resp);
 104
 105/**
 106 * Setting sets structure for in_set_rf command
 107 *
 108 * @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
 109 *              This table contains multiple RF setting sets required for RF
 110 *              communication.
 111 *
 112 * @in_*_comm_type: Theses fields set the communication type to be used.
 113 */
 114struct port100_in_rf_setting {
 115        u8 in_send_set_number;
 116        u8 in_send_comm_type;
 117        u8 in_recv_set_number;
 118        u8 in_recv_comm_type;
 119} __packed;
 120
 121#define PORT100_COMM_TYPE_IN_212F 0x01
 122#define PORT100_COMM_TYPE_IN_424F 0x02
 123#define PORT100_COMM_TYPE_IN_106A 0x03
 124#define PORT100_COMM_TYPE_IN_106B 0x07
 125
 126static const struct port100_in_rf_setting in_rf_settings[] = {
 127        [NFC_DIGITAL_RF_TECH_212F] = {
 128                .in_send_set_number = 1,
 129                .in_send_comm_type  = PORT100_COMM_TYPE_IN_212F,
 130                .in_recv_set_number = 15,
 131                .in_recv_comm_type  = PORT100_COMM_TYPE_IN_212F,
 132        },
 133        [NFC_DIGITAL_RF_TECH_424F] = {
 134                .in_send_set_number = 1,
 135                .in_send_comm_type  = PORT100_COMM_TYPE_IN_424F,
 136                .in_recv_set_number = 15,
 137                .in_recv_comm_type  = PORT100_COMM_TYPE_IN_424F,
 138        },
 139        [NFC_DIGITAL_RF_TECH_106A] = {
 140                .in_send_set_number = 2,
 141                .in_send_comm_type  = PORT100_COMM_TYPE_IN_106A,
 142                .in_recv_set_number = 15,
 143                .in_recv_comm_type  = PORT100_COMM_TYPE_IN_106A,
 144        },
 145        [NFC_DIGITAL_RF_TECH_106B] = {
 146                .in_send_set_number = 3,
 147                .in_send_comm_type  = PORT100_COMM_TYPE_IN_106B,
 148                .in_recv_set_number = 15,
 149                .in_recv_comm_type  = PORT100_COMM_TYPE_IN_106B,
 150        },
 151        /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
 152        [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
 153};
 154
 155/**
 156 * Setting sets structure for tg_set_rf command
 157 *
 158 * @tg_set_number: Represents the entry index in the port-100 RF Base Table.
 159 *                 This table contains multiple RF setting sets required for RF
 160 *                 communication. this field is used for both send and receive
 161 *                 settings.
 162 *
 163 * @tg_comm_type: Sets the communication type to be used to send and receive
 164 *                data.
 165 */
 166struct port100_tg_rf_setting {
 167        u8 tg_set_number;
 168        u8 tg_comm_type;
 169} __packed;
 170
 171#define PORT100_COMM_TYPE_TG_106A 0x0B
 172#define PORT100_COMM_TYPE_TG_212F 0x0C
 173#define PORT100_COMM_TYPE_TG_424F 0x0D
 174
 175static const struct port100_tg_rf_setting tg_rf_settings[] = {
 176        [NFC_DIGITAL_RF_TECH_106A] = {
 177                .tg_set_number = 8,
 178                .tg_comm_type = PORT100_COMM_TYPE_TG_106A,
 179        },
 180        [NFC_DIGITAL_RF_TECH_212F] = {
 181                .tg_set_number = 8,
 182                .tg_comm_type = PORT100_COMM_TYPE_TG_212F,
 183        },
 184        [NFC_DIGITAL_RF_TECH_424F] = {
 185                .tg_set_number = 8,
 186                .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
 187        },
 188        /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
 189        [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
 190
 191};
 192
 193#define PORT100_IN_PROT_INITIAL_GUARD_TIME      0x00
 194#define PORT100_IN_PROT_ADD_CRC                 0x01
 195#define PORT100_IN_PROT_CHECK_CRC               0x02
 196#define PORT100_IN_PROT_MULTI_CARD              0x03
 197#define PORT100_IN_PROT_ADD_PARITY              0x04
 198#define PORT100_IN_PROT_CHECK_PARITY            0x05
 199#define PORT100_IN_PROT_BITWISE_AC_RECV_MODE    0x06
 200#define PORT100_IN_PROT_VALID_BIT_NUMBER        0x07
 201#define PORT100_IN_PROT_CRYPTO1                 0x08
 202#define PORT100_IN_PROT_ADD_SOF                 0x09
 203#define PORT100_IN_PROT_CHECK_SOF               0x0A
 204#define PORT100_IN_PROT_ADD_EOF                 0x0B
 205#define PORT100_IN_PROT_CHECK_EOF               0x0C
 206#define PORT100_IN_PROT_DEAF_TIME               0x0E
 207#define PORT100_IN_PROT_CRM                     0x0F
 208#define PORT100_IN_PROT_CRM_MIN_LEN             0x10
 209#define PORT100_IN_PROT_T1_TAG_FRAME            0x11
 210#define PORT100_IN_PROT_RFCA                    0x12
 211#define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
 212#define PORT100_IN_PROT_END                     0x14
 213
 214#define PORT100_IN_MAX_NUM_PROTOCOLS            19
 215
 216#define PORT100_TG_PROT_TU           0x00
 217#define PORT100_TG_PROT_RF_OFF       0x01
 218#define PORT100_TG_PROT_CRM          0x02
 219#define PORT100_TG_PROT_END          0x03
 220
 221#define PORT100_TG_MAX_NUM_PROTOCOLS 3
 222
 223struct port100_protocol {
 224        u8 number;
 225        u8 value;
 226} __packed;
 227
 228static struct port100_protocol
 229in_protocols[][PORT100_IN_MAX_NUM_PROTOCOLS + 1] = {
 230        [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
 231                { PORT100_IN_PROT_INITIAL_GUARD_TIME,      6 },
 232                { PORT100_IN_PROT_ADD_CRC,                 0 },
 233                { PORT100_IN_PROT_CHECK_CRC,               0 },
 234                { PORT100_IN_PROT_MULTI_CARD,              0 },
 235                { PORT100_IN_PROT_ADD_PARITY,              0 },
 236                { PORT100_IN_PROT_CHECK_PARITY,            1 },
 237                { PORT100_IN_PROT_BITWISE_AC_RECV_MODE,    0 },
 238                { PORT100_IN_PROT_VALID_BIT_NUMBER,        7 },
 239                { PORT100_IN_PROT_CRYPTO1,                 0 },
 240                { PORT100_IN_PROT_ADD_SOF,                 0 },
 241                { PORT100_IN_PROT_CHECK_SOF,               0 },
 242                { PORT100_IN_PROT_ADD_EOF,                 0 },
 243                { PORT100_IN_PROT_CHECK_EOF,               0 },
 244                { PORT100_IN_PROT_DEAF_TIME,               4 },
 245                { PORT100_IN_PROT_CRM,                     0 },
 246                { PORT100_IN_PROT_CRM_MIN_LEN,             0 },
 247                { PORT100_IN_PROT_T1_TAG_FRAME,            0 },
 248                { PORT100_IN_PROT_RFCA,                    0 },
 249                { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
 250                { PORT100_IN_PROT_END,                     0 },
 251        },
 252        [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
 253                { PORT100_IN_PROT_INITIAL_GUARD_TIME,      6 },
 254                { PORT100_IN_PROT_ADD_CRC,                 0 },
 255                { PORT100_IN_PROT_CHECK_CRC,               0 },
 256                { PORT100_IN_PROT_MULTI_CARD,              0 },
 257                { PORT100_IN_PROT_ADD_PARITY,              1 },
 258                { PORT100_IN_PROT_CHECK_PARITY,            1 },
 259                { PORT100_IN_PROT_BITWISE_AC_RECV_MODE,    0 },
 260                { PORT100_IN_PROT_VALID_BIT_NUMBER,        8 },
 261                { PORT100_IN_PROT_CRYPTO1,                 0 },
 262                { PORT100_IN_PROT_ADD_SOF,                 0 },
 263                { PORT100_IN_PROT_CHECK_SOF,               0 },
 264                { PORT100_IN_PROT_ADD_EOF,                 0 },
 265                { PORT100_IN_PROT_CHECK_EOF,               0 },
 266                { PORT100_IN_PROT_DEAF_TIME,               4 },
 267                { PORT100_IN_PROT_CRM,                     0 },
 268                { PORT100_IN_PROT_CRM_MIN_LEN,             0 },
 269                { PORT100_IN_PROT_T1_TAG_FRAME,            0 },
 270                { PORT100_IN_PROT_RFCA,                    0 },
 271                { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
 272                { PORT100_IN_PROT_END,                     0 },
 273        },
 274        [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
 275                { PORT100_IN_PROT_INITIAL_GUARD_TIME,      6 },
 276                { PORT100_IN_PROT_ADD_CRC,                 1 },
 277                { PORT100_IN_PROT_CHECK_CRC,               1 },
 278                { PORT100_IN_PROT_MULTI_CARD,              0 },
 279                { PORT100_IN_PROT_ADD_PARITY,              1 },
 280                { PORT100_IN_PROT_CHECK_PARITY,            1 },
 281                { PORT100_IN_PROT_BITWISE_AC_RECV_MODE,    0 },
 282                { PORT100_IN_PROT_VALID_BIT_NUMBER,        8 },
 283                { PORT100_IN_PROT_CRYPTO1,                 0 },
 284                { PORT100_IN_PROT_ADD_SOF,                 0 },
 285                { PORT100_IN_PROT_CHECK_SOF,               0 },
 286                { PORT100_IN_PROT_ADD_EOF,                 0 },
 287                { PORT100_IN_PROT_CHECK_EOF,               0 },
 288                { PORT100_IN_PROT_DEAF_TIME,               4 },
 289                { PORT100_IN_PROT_CRM,                     0 },
 290                { PORT100_IN_PROT_CRM_MIN_LEN,             0 },
 291                { PORT100_IN_PROT_T1_TAG_FRAME,            0 },
 292                { PORT100_IN_PROT_RFCA,                    0 },
 293                { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
 294                { PORT100_IN_PROT_END,                     0 },
 295        },
 296        [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
 297                /* nfc_digital_framing_nfca_short */
 298                { PORT100_IN_PROT_ADD_CRC,          2 },
 299                { PORT100_IN_PROT_CHECK_CRC,        2 },
 300                { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
 301                { PORT100_IN_PROT_T1_TAG_FRAME,     2 },
 302                { PORT100_IN_PROT_END,              0 },
 303        },
 304        [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
 305                /* nfc_digital_framing_nfca_standard */
 306                { PORT100_IN_PROT_ADD_CRC,   1 },
 307                { PORT100_IN_PROT_CHECK_CRC, 0 },
 308                { PORT100_IN_PROT_END,       0 },
 309        },
 310        [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
 311                /* nfc_digital_framing_nfca_standard_with_crc_a */
 312                { PORT100_IN_PROT_END,       0 },
 313        },
 314        [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
 315                /* nfc_digital_framing_nfca_standard */
 316                { PORT100_IN_PROT_END, 0 },
 317        },
 318        [NFC_DIGITAL_FRAMING_NFCF] = {
 319                { PORT100_IN_PROT_INITIAL_GUARD_TIME,     18 },
 320                { PORT100_IN_PROT_ADD_CRC,                 1 },
 321                { PORT100_IN_PROT_CHECK_CRC,               1 },
 322                { PORT100_IN_PROT_MULTI_CARD,              0 },
 323                { PORT100_IN_PROT_ADD_PARITY,              0 },
 324                { PORT100_IN_PROT_CHECK_PARITY,            0 },
 325                { PORT100_IN_PROT_BITWISE_AC_RECV_MODE,    0 },
 326                { PORT100_IN_PROT_VALID_BIT_NUMBER,        8 },
 327                { PORT100_IN_PROT_CRYPTO1,                 0 },
 328                { PORT100_IN_PROT_ADD_SOF,                 0 },
 329                { PORT100_IN_PROT_CHECK_SOF,               0 },
 330                { PORT100_IN_PROT_ADD_EOF,                 0 },
 331                { PORT100_IN_PROT_CHECK_EOF,               0 },
 332                { PORT100_IN_PROT_DEAF_TIME,               4 },
 333                { PORT100_IN_PROT_CRM,                     0 },
 334                { PORT100_IN_PROT_CRM_MIN_LEN,             0 },
 335                { PORT100_IN_PROT_T1_TAG_FRAME,            0 },
 336                { PORT100_IN_PROT_RFCA,                    0 },
 337                { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
 338                { PORT100_IN_PROT_END,                     0 },
 339        },
 340        [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
 341                /* nfc_digital_framing_nfcf */
 342                { PORT100_IN_PROT_END, 0 },
 343        },
 344        [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
 345                /* nfc_digital_framing_nfcf */
 346                { PORT100_IN_PROT_END, 0 },
 347        },
 348        [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
 349                { PORT100_IN_PROT_END, 0 },
 350        },
 351        [NFC_DIGITAL_FRAMING_NFCB] = {
 352                { PORT100_IN_PROT_INITIAL_GUARD_TIME,     20 },
 353                { PORT100_IN_PROT_ADD_CRC,                 1 },
 354                { PORT100_IN_PROT_CHECK_CRC,               1 },
 355                { PORT100_IN_PROT_MULTI_CARD,              0 },
 356                { PORT100_IN_PROT_ADD_PARITY,              0 },
 357                { PORT100_IN_PROT_CHECK_PARITY,            0 },
 358                { PORT100_IN_PROT_BITWISE_AC_RECV_MODE,    0 },
 359                { PORT100_IN_PROT_VALID_BIT_NUMBER,        8 },
 360                { PORT100_IN_PROT_CRYPTO1,                 0 },
 361                { PORT100_IN_PROT_ADD_SOF,                 1 },
 362                { PORT100_IN_PROT_CHECK_SOF,               1 },
 363                { PORT100_IN_PROT_ADD_EOF,                 1 },
 364                { PORT100_IN_PROT_CHECK_EOF,               1 },
 365                { PORT100_IN_PROT_DEAF_TIME,               4 },
 366                { PORT100_IN_PROT_CRM,                     0 },
 367                { PORT100_IN_PROT_CRM_MIN_LEN,             0 },
 368                { PORT100_IN_PROT_T1_TAG_FRAME,            0 },
 369                { PORT100_IN_PROT_RFCA,                    0 },
 370                { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
 371                { PORT100_IN_PROT_END,                     0 },
 372        },
 373        [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
 374                /* nfc_digital_framing_nfcb */
 375                { PORT100_IN_PROT_END,                     0 },
 376        },
 377        /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
 378        [NFC_DIGITAL_FRAMING_LAST] = {
 379                { PORT100_IN_PROT_END, 0 },
 380        },
 381};
 382
 383static struct port100_protocol
 384tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
 385        [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
 386                { PORT100_TG_PROT_END, 0 },
 387        },
 388        [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
 389                { PORT100_TG_PROT_END, 0 },
 390        },
 391        [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
 392                { PORT100_TG_PROT_END, 0 },
 393        },
 394        [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
 395                { PORT100_TG_PROT_END, 0 },
 396        },
 397        [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
 398                { PORT100_TG_PROT_END, 0 },
 399        },
 400        [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
 401                { PORT100_TG_PROT_TU,     1 },
 402                { PORT100_TG_PROT_RF_OFF, 0 },
 403                { PORT100_TG_PROT_CRM,    7 },
 404                { PORT100_TG_PROT_END,    0 },
 405        },
 406        [NFC_DIGITAL_FRAMING_NFCF] = {
 407                { PORT100_TG_PROT_END, 0 },
 408        },
 409        [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
 410                { PORT100_TG_PROT_END, 0 },
 411        },
 412        [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
 413                { PORT100_TG_PROT_TU,     1 },
 414                { PORT100_TG_PROT_RF_OFF, 0 },
 415                { PORT100_TG_PROT_CRM,    7 },
 416                { PORT100_TG_PROT_END,    0 },
 417        },
 418        [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
 419                { PORT100_TG_PROT_RF_OFF, 1 },
 420                { PORT100_TG_PROT_END,    0 },
 421        },
 422        /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
 423        [NFC_DIGITAL_FRAMING_LAST] = {
 424                { PORT100_TG_PROT_END,    0 },
 425        },
 426};
 427
 428struct port100 {
 429        struct nfc_digital_dev *nfc_digital_dev;
 430
 431        int skb_headroom;
 432        int skb_tailroom;
 433
 434        struct usb_device *udev;
 435        struct usb_interface *interface;
 436
 437        struct urb *out_urb;
 438        struct urb *in_urb;
 439
 440        struct work_struct cmd_complete_work;
 441
 442        u8 cmd_type;
 443
 444        /* The digital stack serializes commands to be sent. There is no need
 445         * for any queuing/locking mechanism at driver level.
 446         */
 447        struct port100_cmd *cmd;
 448};
 449
 450struct port100_cmd {
 451        u8 code;
 452        int status;
 453        struct sk_buff *req;
 454        struct sk_buff *resp;
 455        int resp_len;
 456        port100_send_async_complete_t  complete_cb;
 457        void *complete_cb_context;
 458};
 459
 460struct port100_frame {
 461        u8 preamble;
 462        __be16 start_frame;
 463        __be16 extended_frame;
 464        __le16 datalen;
 465        u8 datalen_checksum;
 466        u8 data[];
 467} __packed;
 468
 469struct port100_ack_frame {
 470        u8 preamble;
 471        __be16 start_frame;
 472        __be16 ack_frame;
 473        u8 postambule;
 474} __packed;
 475
 476struct port100_cb_arg {
 477        nfc_digital_cmd_complete_t complete_cb;
 478        void *complete_arg;
 479        u8 mdaa;
 480};
 481
 482struct port100_tg_comm_rf_cmd {
 483        __le16 guard_time;
 484        __le16 send_timeout;
 485        u8 mdaa;
 486        u8 nfca_param[6];
 487        u8 nfcf_param[18];
 488        u8 mf_halted;
 489        u8 arae_flag;
 490        __le16 recv_timeout;
 491        u8 data[];
 492} __packed;
 493
 494struct port100_tg_comm_rf_res {
 495        u8 comm_type;
 496        u8 ar_status;
 497        u8 target_activated;
 498        __le32 status;
 499        u8 data[];
 500} __packed;
 501
 502/* The rule: value + checksum = 0 */
 503static inline u8 port100_checksum(u16 value)
 504{
 505        return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
 506}
 507
 508/* The rule: sum(data elements) + checksum = 0 */
 509static u8 port100_data_checksum(u8 *data, int datalen)
 510{
 511        u8 sum = 0;
 512        int i;
 513
 514        for (i = 0; i < datalen; i++)
 515                sum += data[i];
 516
 517        return port100_checksum(sum);
 518}
 519
 520static void port100_tx_frame_init(void *_frame, u8 cmd_code)
 521{
 522        struct port100_frame *frame = _frame;
 523
 524        frame->preamble = 0;
 525        frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
 526        frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
 527        PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
 528        PORT100_FRAME_CMD(frame) = cmd_code;
 529        frame->datalen = cpu_to_le16(2);
 530}
 531
 532static void port100_tx_frame_finish(void *_frame)
 533{
 534        struct port100_frame *frame = _frame;
 535
 536        frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
 537
 538        PORT100_FRAME_CHECKSUM(frame) =
 539                port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
 540
 541        PORT100_FRAME_POSTAMBLE(frame) = 0;
 542}
 543
 544static void port100_tx_update_payload_len(void *_frame, int len)
 545{
 546        struct port100_frame *frame = _frame;
 547
 548        frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
 549}
 550
 551static bool port100_rx_frame_is_valid(void *_frame)
 552{
 553        u8 checksum;
 554        struct port100_frame *frame = _frame;
 555
 556        if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
 557            frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
 558                return false;
 559
 560        checksum = port100_checksum(le16_to_cpu(frame->datalen));
 561        if (checksum != frame->datalen_checksum)
 562                return false;
 563
 564        checksum = port100_data_checksum(frame->data,
 565                                         le16_to_cpu(frame->datalen));
 566        if (checksum != PORT100_FRAME_CHECKSUM(frame))
 567                return false;
 568
 569        return true;
 570}
 571
 572static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
 573{
 574        return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
 575                frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
 576}
 577
 578static inline int port100_rx_frame_size(void *frame)
 579{
 580        struct port100_frame *f = frame;
 581
 582        return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
 583               PORT100_FRAME_TAIL_LEN;
 584}
 585
 586static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
 587{
 588        struct port100_frame *f = frame;
 589
 590        return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
 591}
 592
 593static void port100_recv_response(struct urb *urb)
 594{
 595        struct port100 *dev = urb->context;
 596        struct port100_cmd *cmd = dev->cmd;
 597        u8 *in_frame;
 598
 599        cmd->status = urb->status;
 600
 601        switch (urb->status) {
 602        case 0:
 603                break; /* success */
 604        case -ECONNRESET:
 605        case -ENOENT:
 606                nfc_err(&dev->interface->dev,
 607                        "The urb has been canceled (status %d)", urb->status);
 608                goto sched_wq;
 609        case -ESHUTDOWN:
 610        default:
 611                nfc_err(&dev->interface->dev, "Urb failure (status %d)",
 612                        urb->status);
 613                goto sched_wq;
 614        }
 615
 616        in_frame = dev->in_urb->transfer_buffer;
 617
 618        if (!port100_rx_frame_is_valid(in_frame)) {
 619                nfc_err(&dev->interface->dev, "Received an invalid frame");
 620                cmd->status = -EIO;
 621                goto sched_wq;
 622        }
 623
 624        print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
 625                             port100_rx_frame_size(in_frame), false);
 626
 627        if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
 628                nfc_err(&dev->interface->dev,
 629                        "It's not the response to the last command");
 630                cmd->status = -EIO;
 631                goto sched_wq;
 632        }
 633
 634sched_wq:
 635        schedule_work(&dev->cmd_complete_work);
 636}
 637
 638static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
 639{
 640        dev->in_urb->complete = port100_recv_response;
 641
 642        return usb_submit_urb(dev->in_urb, flags);
 643}
 644
 645static void port100_recv_ack(struct urb *urb)
 646{
 647        struct port100 *dev = urb->context;
 648        struct port100_cmd *cmd = dev->cmd;
 649        struct port100_ack_frame *in_frame;
 650        int rc;
 651
 652        cmd->status = urb->status;
 653
 654        switch (urb->status) {
 655        case 0:
 656                break; /* success */
 657        case -ECONNRESET:
 658        case -ENOENT:
 659                nfc_err(&dev->interface->dev,
 660                        "The urb has been stopped (status %d)", urb->status);
 661                goto sched_wq;
 662        case -ESHUTDOWN:
 663        default:
 664                nfc_err(&dev->interface->dev, "Urb failure (status %d)",
 665                        urb->status);
 666                goto sched_wq;
 667        }
 668
 669        in_frame = dev->in_urb->transfer_buffer;
 670
 671        if (!port100_rx_frame_is_ack(in_frame)) {
 672                nfc_err(&dev->interface->dev, "Received an invalid ack");
 673                cmd->status = -EIO;
 674                goto sched_wq;
 675        }
 676
 677        rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
 678        if (rc) {
 679                nfc_err(&dev->interface->dev,
 680                        "usb_submit_urb failed with result %d", rc);
 681                cmd->status = rc;
 682                goto sched_wq;
 683        }
 684
 685        return;
 686
 687sched_wq:
 688        schedule_work(&dev->cmd_complete_work);
 689}
 690
 691static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
 692{
 693        dev->in_urb->complete = port100_recv_ack;
 694
 695        return usb_submit_urb(dev->in_urb, flags);
 696}
 697
 698static int port100_send_ack(struct port100 *dev)
 699{
 700        int rc;
 701
 702        dev->out_urb->transfer_buffer = ack_frame;
 703        dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
 704        rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
 705
 706        return rc;
 707}
 708
 709static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
 710                                    struct sk_buff *in, int in_len)
 711{
 712        int rc;
 713
 714        dev->out_urb->transfer_buffer = out->data;
 715        dev->out_urb->transfer_buffer_length = out->len;
 716
 717        dev->in_urb->transfer_buffer = in->data;
 718        dev->in_urb->transfer_buffer_length = in_len;
 719
 720        print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
 721                             out->data, out->len, false);
 722
 723        rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
 724        if (rc)
 725                return rc;
 726
 727        rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
 728        if (rc)
 729                goto error;
 730
 731        return 0;
 732
 733error:
 734        usb_unlink_urb(dev->out_urb);
 735        return rc;
 736}
 737
 738static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
 739                                    struct sk_buff *skb)
 740{
 741        /* payload is already there, just update datalen */
 742        int payload_len = skb->len;
 743
 744        skb_push(skb, PORT100_FRAME_HEADER_LEN);
 745        skb_put(skb, PORT100_FRAME_TAIL_LEN);
 746
 747        port100_tx_frame_init(skb->data, cmd_code);
 748        port100_tx_update_payload_len(skb->data, payload_len);
 749        port100_tx_frame_finish(skb->data);
 750}
 751
 752static void port100_send_async_complete(struct port100 *dev)
 753{
 754        struct port100_cmd *cmd = dev->cmd;
 755        int status = cmd->status;
 756
 757        struct sk_buff *req = cmd->req;
 758        struct sk_buff *resp = cmd->resp;
 759
 760        dev_kfree_skb(req);
 761
 762        dev->cmd = NULL;
 763
 764        if (status < 0) {
 765                cmd->complete_cb(dev, cmd->complete_cb_context,
 766                                 ERR_PTR(status));
 767                dev_kfree_skb(resp);
 768                goto done;
 769        }
 770
 771        skb_put(resp, port100_rx_frame_size(resp->data));
 772        skb_pull(resp, PORT100_FRAME_HEADER_LEN);
 773        skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
 774
 775        cmd->complete_cb(dev, cmd->complete_cb_context, resp);
 776
 777done:
 778        kfree(cmd);
 779}
 780
 781static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
 782                                struct sk_buff *req,
 783                                port100_send_async_complete_t complete_cb,
 784                                void *complete_cb_context)
 785{
 786        struct port100_cmd *cmd;
 787        struct sk_buff *resp;
 788        int rc;
 789        int  resp_len = PORT100_FRAME_HEADER_LEN +
 790                        PORT100_FRAME_MAX_PAYLOAD_LEN +
 791                        PORT100_FRAME_TAIL_LEN;
 792
 793        resp = alloc_skb(resp_len, GFP_KERNEL);
 794        if (!resp)
 795                return -ENOMEM;
 796
 797        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 798        if (!cmd) {
 799                dev_kfree_skb(resp);
 800                return -ENOMEM;
 801        }
 802
 803        cmd->code = cmd_code;
 804        cmd->req = req;
 805        cmd->resp = resp;
 806        cmd->resp_len = resp_len;
 807        cmd->complete_cb = complete_cb;
 808        cmd->complete_cb_context = complete_cb_context;
 809
 810        port100_build_cmd_frame(dev, cmd_code, req);
 811
 812        dev->cmd = cmd;
 813
 814        rc = port100_send_frame_async(dev, req, resp, resp_len);
 815        if (rc) {
 816                kfree(cmd);
 817                dev_kfree_skb(resp);
 818                dev->cmd = NULL;
 819        }
 820
 821        return rc;
 822}
 823
 824struct port100_sync_cmd_response {
 825        struct sk_buff *resp;
 826        struct completion done;
 827};
 828
 829static void port100_wq_cmd_complete(struct work_struct *work)
 830{
 831        struct port100 *dev = container_of(work, struct port100,
 832                                           cmd_complete_work);
 833
 834        port100_send_async_complete(dev);
 835}
 836
 837static void port100_send_sync_complete(struct port100 *dev, void *_arg,
 838                                      struct sk_buff *resp)
 839{
 840        struct port100_sync_cmd_response *arg = _arg;
 841
 842        arg->resp = resp;
 843        complete(&arg->done);
 844}
 845
 846static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
 847                                             struct sk_buff *req)
 848{
 849        int rc;
 850        struct port100_sync_cmd_response arg;
 851
 852        init_completion(&arg.done);
 853
 854        rc = port100_send_cmd_async(dev, cmd_code, req,
 855                                    port100_send_sync_complete, &arg);
 856        if (rc) {
 857                dev_kfree_skb(req);
 858                return ERR_PTR(rc);
 859        }
 860
 861        wait_for_completion(&arg.done);
 862
 863        return arg.resp;
 864}
 865
 866static void port100_send_complete(struct urb *urb)
 867{
 868        struct port100 *dev = urb->context;
 869
 870        switch (urb->status) {
 871        case 0:
 872                break; /* success */
 873        case -ECONNRESET:
 874        case -ENOENT:
 875                nfc_err(&dev->interface->dev,
 876                        "The urb has been stopped (status %d)", urb->status);
 877                break;
 878        case -ESHUTDOWN:
 879        default:
 880                nfc_err(&dev->interface->dev, "Urb failure (status %d)",
 881                        urb->status);
 882        }
 883}
 884
 885static void port100_abort_cmd(struct nfc_digital_dev *ddev)
 886{
 887        struct port100 *dev = nfc_digital_get_drvdata(ddev);
 888
 889        /* An ack will cancel the last issued command */
 890        port100_send_ack(dev);
 891
 892        /* cancel the urb request */
 893        usb_kill_urb(dev->in_urb);
 894}
 895
 896static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
 897{
 898        struct sk_buff *skb;
 899
 900        skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
 901                        GFP_KERNEL);
 902        if (skb)
 903                skb_reserve(skb, dev->skb_headroom);
 904
 905        return skb;
 906}
 907
 908static int port100_set_command_type(struct port100 *dev, u8 command_type)
 909{
 910        struct sk_buff *skb;
 911        struct sk_buff *resp;
 912        int rc;
 913
 914        skb = port100_alloc_skb(dev, 1);
 915        if (!skb)
 916                return -ENOMEM;
 917
 918        *skb_put(skb, sizeof(u8)) = command_type;
 919
 920        resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
 921        if (IS_ERR(resp))
 922                return PTR_ERR(resp);
 923
 924        rc = resp->data[0];
 925
 926        dev_kfree_skb(resp);
 927
 928        return rc;
 929}
 930
 931static u64 port100_get_command_type_mask(struct port100 *dev)
 932{
 933        struct sk_buff *skb;
 934        struct sk_buff *resp;
 935        u64 mask;
 936
 937        skb = port100_alloc_skb(dev, 0);
 938        if (!skb)
 939                return -ENOMEM;
 940
 941        resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
 942        if (IS_ERR(resp))
 943                return PTR_ERR(resp);
 944
 945        if (resp->len < 8)
 946                mask = 0;
 947        else
 948                mask = be64_to_cpu(*(__be64 *)resp->data);
 949
 950        dev_kfree_skb(resp);
 951
 952        return mask;
 953}
 954
 955static u16 port100_get_firmware_version(struct port100 *dev)
 956{
 957        struct sk_buff *skb;
 958        struct sk_buff *resp;
 959        u16 fw_ver;
 960
 961        skb = port100_alloc_skb(dev, 0);
 962        if (!skb)
 963                return 0;
 964
 965        resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
 966                                     skb);
 967        if (IS_ERR(resp))
 968                return 0;
 969
 970        fw_ver = le16_to_cpu(*(__le16 *)resp->data);
 971
 972        dev_kfree_skb(resp);
 973
 974        return fw_ver;
 975}
 976
 977static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
 978{
 979        struct port100 *dev = nfc_digital_get_drvdata(ddev);
 980        struct sk_buff *skb, *resp;
 981
 982        skb = port100_alloc_skb(dev, 1);
 983        if (!skb)
 984                return -ENOMEM;
 985
 986        *skb_put(skb, 1) = on ? 1 : 0;
 987
 988        resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
 989
 990        if (IS_ERR(resp))
 991                return PTR_ERR(resp);
 992
 993        dev_kfree_skb(resp);
 994
 995        return 0;
 996}
 997
 998static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
 999{
1000        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1001        struct sk_buff *skb;
1002        struct sk_buff *resp;
1003        int rc;
1004
1005        if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1006                return -EINVAL;
1007
1008        skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
1009        if (!skb)
1010                return -ENOMEM;
1011
1012        memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
1013               &in_rf_settings[rf],
1014               sizeof(struct port100_in_rf_setting));
1015
1016        resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1017
1018        if (IS_ERR(resp))
1019                return PTR_ERR(resp);
1020
1021        rc = resp->data[0];
1022
1023        dev_kfree_skb(resp);
1024
1025        return rc;
1026}
1027
1028static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1029{
1030        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1031        struct port100_protocol *protocols;
1032        struct sk_buff *skb;
1033        struct sk_buff *resp;
1034        int num_protocols;
1035        size_t size;
1036        int rc;
1037
1038        if (param >= NFC_DIGITAL_FRAMING_LAST)
1039                return -EINVAL;
1040
1041        protocols = in_protocols[param];
1042
1043        num_protocols = 0;
1044        while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1045                num_protocols++;
1046
1047        if (!num_protocols)
1048                return 0;
1049
1050        size = sizeof(struct port100_protocol) * num_protocols;
1051
1052        skb = port100_alloc_skb(dev, size);
1053        if (!skb)
1054                return -ENOMEM;
1055
1056        memcpy(skb_put(skb, size), protocols, size);
1057
1058        resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1059
1060        if (IS_ERR(resp))
1061                return PTR_ERR(resp);
1062
1063        rc = resp->data[0];
1064
1065        dev_kfree_skb(resp);
1066
1067        return rc;
1068}
1069
1070static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1071                                   int param)
1072{
1073        if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1074                return port100_in_set_rf(ddev, param);
1075
1076        if (type == NFC_DIGITAL_CONFIG_FRAMING)
1077                return port100_in_set_framing(ddev, param);
1078
1079        return -EINVAL;
1080}
1081
1082static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1083                                       struct sk_buff *resp)
1084{
1085        struct port100_cb_arg *cb_arg = arg;
1086        nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1087        u32 status;
1088        int rc;
1089
1090        if (IS_ERR(resp)) {
1091                rc =  PTR_ERR(resp);
1092                goto exit;
1093        }
1094
1095        if (resp->len < 4) {
1096                nfc_err(&dev->interface->dev,
1097                        "Invalid packet length received.\n");
1098                rc = -EIO;
1099                goto error;
1100        }
1101
1102        status = le32_to_cpu(*(__le32 *)resp->data);
1103
1104        skb_pull(resp, sizeof(u32));
1105
1106        if (status == PORT100_CMD_STATUS_TIMEOUT) {
1107                rc = -ETIMEDOUT;
1108                goto error;
1109        }
1110
1111        if (status != PORT100_CMD_STATUS_OK) {
1112                nfc_err(&dev->interface->dev,
1113                        "in_comm_rf failed with status 0x%08x\n", status);
1114                rc = -EIO;
1115                goto error;
1116        }
1117
1118        /* Remove collision bits byte */
1119        skb_pull(resp, 1);
1120
1121        goto exit;
1122
1123error:
1124        kfree_skb(resp);
1125        resp = ERR_PTR(rc);
1126
1127exit:
1128        cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1129
1130        kfree(cb_arg);
1131}
1132
1133static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1134                               struct sk_buff *skb, u16 _timeout,
1135                               nfc_digital_cmd_complete_t cb, void *arg)
1136{
1137        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1138        struct port100_cb_arg *cb_arg;
1139        __le16 timeout;
1140
1141        cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1142        if (!cb_arg)
1143                return -ENOMEM;
1144
1145        cb_arg->complete_cb = cb;
1146        cb_arg->complete_arg = arg;
1147
1148        timeout = cpu_to_le16(_timeout * 10);
1149
1150        memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1151
1152        return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1153                                      port100_in_comm_rf_complete, cb_arg);
1154}
1155
1156static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1157{
1158        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1159        struct sk_buff *skb;
1160        struct sk_buff *resp;
1161        int rc;
1162
1163        if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1164                return -EINVAL;
1165
1166        skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1167        if (!skb)
1168                return -ENOMEM;
1169
1170        memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1171               &tg_rf_settings[rf],
1172               sizeof(struct port100_tg_rf_setting));
1173
1174        resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1175
1176        if (IS_ERR(resp))
1177                return PTR_ERR(resp);
1178
1179        rc = resp->data[0];
1180
1181        dev_kfree_skb(resp);
1182
1183        return rc;
1184}
1185
1186static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1187{
1188        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1189        struct port100_protocol *protocols;
1190        struct sk_buff *skb;
1191        struct sk_buff *resp;
1192        int rc;
1193        int num_protocols;
1194        size_t size;
1195
1196        if (param >= NFC_DIGITAL_FRAMING_LAST)
1197                return -EINVAL;
1198
1199        protocols = tg_protocols[param];
1200
1201        num_protocols = 0;
1202        while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1203                num_protocols++;
1204
1205        if (!num_protocols)
1206                return 0;
1207
1208        size = sizeof(struct port100_protocol) * num_protocols;
1209
1210        skb = port100_alloc_skb(dev, size);
1211        if (!skb)
1212                return -ENOMEM;
1213
1214        memcpy(skb_put(skb, size), protocols, size);
1215
1216        resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1217
1218        if (IS_ERR(resp))
1219                return PTR_ERR(resp);
1220
1221        rc = resp->data[0];
1222
1223        dev_kfree_skb(resp);
1224
1225        return rc;
1226}
1227
1228static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1229                                   int param)
1230{
1231        if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1232                return port100_tg_set_rf(ddev, param);
1233
1234        if (type == NFC_DIGITAL_CONFIG_FRAMING)
1235                return port100_tg_set_framing(ddev, param);
1236
1237        return -EINVAL;
1238}
1239
1240static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1241{
1242        u8 mask;
1243
1244        switch (dev->cmd_type) {
1245        case PORT100_CMD_TYPE_0:
1246                mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1247                break;
1248        case PORT100_CMD_TYPE_1:
1249                mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1250                       PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1251                break;
1252        default:
1253                nfc_err(&dev->interface->dev, "Unknonwn command type.\n");
1254                return false;
1255        }
1256
1257        return ((tgt_activated & mask) == mask);
1258}
1259
1260static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1261                                        struct sk_buff *resp)
1262{
1263        u32 status;
1264        struct port100_cb_arg *cb_arg = arg;
1265        nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1266        struct port100_tg_comm_rf_res *hdr;
1267
1268        if (IS_ERR(resp))
1269                goto exit;
1270
1271        hdr = (struct port100_tg_comm_rf_res *)resp->data;
1272
1273        status = le32_to_cpu(hdr->status);
1274
1275        if (cb_arg->mdaa &&
1276            !port100_tg_target_activated(dev, hdr->target_activated)) {
1277                kfree_skb(resp);
1278                resp = ERR_PTR(-ETIMEDOUT);
1279
1280                goto exit;
1281        }
1282
1283        skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1284
1285        if (status != PORT100_CMD_STATUS_OK) {
1286                kfree_skb(resp);
1287
1288                if (status == PORT100_CMD_STATUS_TIMEOUT)
1289                        resp = ERR_PTR(-ETIMEDOUT);
1290                else
1291                        resp = ERR_PTR(-EIO);
1292        }
1293
1294exit:
1295        cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1296
1297        kfree(cb_arg);
1298}
1299
1300static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1301                               struct sk_buff *skb, u16 timeout,
1302                               nfc_digital_cmd_complete_t cb, void *arg)
1303{
1304        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1305        struct port100_tg_comm_rf_cmd *hdr;
1306        struct port100_cb_arg *cb_arg;
1307
1308        cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1309        if (!cb_arg)
1310                return -ENOMEM;
1311
1312        cb_arg->complete_cb = cb;
1313        cb_arg->complete_arg = arg;
1314
1315        skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1316
1317        hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1318
1319        memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1320        hdr->guard_time = cpu_to_le16(500);
1321        hdr->send_timeout = cpu_to_le16(0xFFFF);
1322        hdr->recv_timeout = cpu_to_le16(timeout);
1323
1324        return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1325                                      port100_tg_comm_rf_complete, cb_arg);
1326}
1327
1328static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1329                               struct digital_tg_mdaa_params *params,
1330                               u16 timeout,
1331                               nfc_digital_cmd_complete_t cb, void *arg)
1332{
1333        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1334        struct port100_tg_comm_rf_cmd *hdr;
1335        struct port100_cb_arg *cb_arg;
1336        struct sk_buff *skb;
1337        int rc;
1338
1339        rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1340                                     NFC_DIGITAL_RF_TECH_106A);
1341        if (rc)
1342                return rc;
1343
1344        rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1345                                     NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1346        if (rc)
1347                return rc;
1348
1349        cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1350        if (!cb_arg)
1351                return -ENOMEM;
1352
1353        cb_arg->complete_cb = cb;
1354        cb_arg->complete_arg = arg;
1355        cb_arg->mdaa = 1;
1356
1357        skb = port100_alloc_skb(dev, 0);
1358        if (!skb) {
1359                kfree(cb_arg);
1360                return -ENOMEM;
1361        }
1362
1363        skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1364        hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1365
1366        memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1367
1368        hdr->guard_time = 0;
1369        hdr->send_timeout = cpu_to_le16(0xFFFF);
1370        hdr->mdaa = 1;
1371        hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1372        hdr->nfca_param[1] = params->sens_res & 0xFF;
1373        memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1374        hdr->nfca_param[5] = params->sel_res;
1375        memcpy(hdr->nfcf_param, params->nfcid2, 8);
1376        hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1377        hdr->nfcf_param[17] = params->sc & 0xFF;
1378        hdr->recv_timeout = cpu_to_le16(timeout);
1379
1380        return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1381                                      port100_tg_comm_rf_complete, cb_arg);
1382}
1383
1384static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1385                          nfc_digital_cmd_complete_t cb, void *arg)
1386{
1387        struct port100 *dev = nfc_digital_get_drvdata(ddev);
1388        struct sk_buff *skb;
1389
1390        skb = port100_alloc_skb(dev, 0);
1391        if (!skb)
1392                return -ENOMEM;
1393
1394        return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1395}
1396
1397static struct nfc_digital_ops port100_digital_ops = {
1398        .in_configure_hw = port100_in_configure_hw,
1399        .in_send_cmd = port100_in_send_cmd,
1400
1401        .tg_listen_mdaa = port100_listen_mdaa,
1402        .tg_listen = port100_listen,
1403        .tg_configure_hw = port100_tg_configure_hw,
1404        .tg_send_cmd = port100_tg_send_cmd,
1405
1406        .switch_rf = port100_switch_rf,
1407        .abort_cmd = port100_abort_cmd,
1408};
1409
1410static const struct usb_device_id port100_table[] = {
1411        { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
1412        { }
1413};
1414MODULE_DEVICE_TABLE(usb, port100_table);
1415
1416static int port100_probe(struct usb_interface *interface,
1417                         const struct usb_device_id *id)
1418{
1419        struct port100 *dev;
1420        int rc;
1421        struct usb_host_interface *iface_desc;
1422        struct usb_endpoint_descriptor *endpoint;
1423        int in_endpoint;
1424        int out_endpoint;
1425        u16 fw_version;
1426        u64 cmd_type_mask;
1427        int i;
1428
1429        dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1430        if (!dev)
1431                return -ENOMEM;
1432
1433        dev->udev = usb_get_dev(interface_to_usbdev(interface));
1434        dev->interface = interface;
1435        usb_set_intfdata(interface, dev);
1436
1437        in_endpoint = out_endpoint = 0;
1438        iface_desc = interface->cur_altsetting;
1439        for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1440                endpoint = &iface_desc->endpoint[i].desc;
1441
1442                if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1443                        in_endpoint = endpoint->bEndpointAddress;
1444
1445                if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1446                        out_endpoint = endpoint->bEndpointAddress;
1447        }
1448
1449        if (!in_endpoint || !out_endpoint) {
1450                nfc_err(&interface->dev,
1451                        "Could not find bulk-in or bulk-out endpoint\n");
1452                rc = -ENODEV;
1453                goto error;
1454        }
1455
1456        dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1457        dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1458
1459        if (!dev->in_urb || !dev->out_urb) {
1460                nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1461                rc = -ENOMEM;
1462                goto error;
1463        }
1464
1465        usb_fill_bulk_urb(dev->in_urb, dev->udev,
1466                          usb_rcvbulkpipe(dev->udev, in_endpoint),
1467                          NULL, 0, NULL, dev);
1468        usb_fill_bulk_urb(dev->out_urb, dev->udev,
1469                          usb_sndbulkpipe(dev->udev, out_endpoint),
1470                          NULL, 0, port100_send_complete, dev);
1471
1472        dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1473                            PORT100_COMM_RF_HEAD_MAX_LEN;
1474        dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1475
1476        INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1477
1478        /* The first thing to do with the Port-100 is to set the command type
1479         * to be used. If supported we use command type 1. 0 otherwise.
1480         */
1481        cmd_type_mask = port100_get_command_type_mask(dev);
1482        if (!cmd_type_mask) {
1483                nfc_err(&interface->dev,
1484                        "Could not get supported command types.\n");
1485                rc = -ENODEV;
1486                goto error;
1487        }
1488
1489        if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1490                dev->cmd_type = PORT100_CMD_TYPE_1;
1491        else
1492                dev->cmd_type = PORT100_CMD_TYPE_0;
1493
1494        rc = port100_set_command_type(dev, dev->cmd_type);
1495        if (rc) {
1496                nfc_err(&interface->dev,
1497                        "The device does not support command type %u.\n",
1498                        dev->cmd_type);
1499                goto error;
1500        }
1501
1502        fw_version = port100_get_firmware_version(dev);
1503        if (!fw_version)
1504                nfc_err(&interface->dev,
1505                        "Could not get device firmware version.\n");
1506
1507        nfc_info(&interface->dev,
1508                 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1509                 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1510
1511        dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1512                                                           PORT100_PROTOCOLS,
1513                                                           PORT100_CAPABILITIES,
1514                                                           dev->skb_headroom,
1515                                                           dev->skb_tailroom);
1516        if (!dev->nfc_digital_dev) {
1517                nfc_err(&interface->dev,
1518                        "Could not allocate nfc_digital_dev.\n");
1519                rc = -ENOMEM;
1520                goto error;
1521        }
1522
1523        nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1524        nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1525
1526        rc = nfc_digital_register_device(dev->nfc_digital_dev);
1527        if (rc) {
1528                nfc_err(&interface->dev,
1529                        "Could not register digital device.\n");
1530                goto free_nfc_dev;
1531        }
1532
1533        return 0;
1534
1535free_nfc_dev:
1536        nfc_digital_free_device(dev->nfc_digital_dev);
1537
1538error:
1539        usb_free_urb(dev->in_urb);
1540        usb_free_urb(dev->out_urb);
1541        usb_put_dev(dev->udev);
1542
1543        return rc;
1544}
1545
1546static void port100_disconnect(struct usb_interface *interface)
1547{
1548        struct port100 *dev;
1549
1550        dev = usb_get_intfdata(interface);
1551        usb_set_intfdata(interface, NULL);
1552
1553        nfc_digital_unregister_device(dev->nfc_digital_dev);
1554        nfc_digital_free_device(dev->nfc_digital_dev);
1555
1556        usb_kill_urb(dev->in_urb);
1557        usb_kill_urb(dev->out_urb);
1558
1559        usb_free_urb(dev->in_urb);
1560        usb_free_urb(dev->out_urb);
1561        usb_put_dev(dev->udev);
1562
1563        kfree(dev->cmd);
1564
1565        nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected");
1566}
1567
1568static struct usb_driver port100_driver = {
1569        .name =         "port100",
1570        .probe =        port100_probe,
1571        .disconnect =   port100_disconnect,
1572        .id_table =     port100_table,
1573};
1574
1575module_usb_driver(port100_driver);
1576
1577MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1578MODULE_VERSION(VERSION);
1579MODULE_LICENSE("GPL");
1580
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.