/*
* Sony NFC Port-100 Series driver
* Copyright (c) 2013, Intel Corporation.
*
* Partly based/Inspired by Stephen Tiedemann's nfcpy
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#include <linux/module.h>
#include <linux/usb.h>
#include <net/nfc/digital.h>
#define VERSION "0.1"
#define SONY_VENDOR_ID 0x054c
#define RCS380_PRODUCT_ID 0x06c1
#define PORT100_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
NFC_PROTO_MIFARE_MASK | \
NFC_PROTO_FELICA_MASK | \
NFC_PROTO_NFC_DEP_MASK | \
NFC_PROTO_ISO14443_MASK)
#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
NFC_DIGITAL_DRV_CAPS_TG_CRC)
/* Standard port100 frame definitions */
#define PORT100_FRAME_HEADER_LEN (sizeof(struct port100_frame) \
+ 2) /* data[0] CC, data[1] SCC */
#define PORT100_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
#define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
/*
* Max extended frame payload len, excluding CC and SCC
* which are already in PORT100_FRAME_HEADER_LEN.
*/
#define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
#define PORT100_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
Postamble (1) */
static u8 ack_frame[PORT100_FRAME_ACK_SIZE] = {
0x00, 0x00, 0xff, 0x00, 0xff, 0x00
};
#define PORT100_FRAME_CHECKSUM(f) (f->data[le16_to_cpu(f->datalen)])
#define PORT100_FRAME_POSTAMBLE(f) (f->data[le16_to_cpu(f->datalen) + 1])
/* start of frame */
#define PORT100_FRAME_SOF 0x00FF
#define PORT100_FRAME_EXT 0xFFFF
#define PORT100_FRAME_ACK 0x00FF
/* Port-100 command: in or out */
#define PORT100_FRAME_DIRECTION(f) (f->data[0]) /* CC */
#define PORT100_FRAME_DIR_OUT 0xD6
#define PORT100_FRAME_DIR_IN 0xD7
/* Port-100 sub-command */
#define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
#define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
#define PORT100_CMD_GET_COMMAND_TYPE 0x28
#define PORT100_CMD_SET_COMMAND_TYPE 0x2A
#define PORT100_CMD_IN_SET_RF 0x00
#define PORT100_CMD_IN_SET_PROTOCOL 0x02
#define PORT100_CMD_IN_COMM_RF 0x04
#define PORT100_CMD_TG_SET_RF 0x40
#define PORT100_CMD_TG_SET_PROTOCOL 0x42
#define PORT100_CMD_TG_SET_RF_OFF 0x46
#define PORT100_CMD_TG_COMM_RF 0x48
#define PORT100_CMD_SWITCH_RF 0x06
#define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
#define PORT100_CMD_TYPE_IS_SUPPORTED(mask, cmd_type) \
((mask) & (0x01 << (cmd_type)))
#define PORT100_CMD_TYPE_0 0
#define PORT100_CMD_TYPE_1 1
#define PORT100_CMD_STATUS_OK 0x00
#define PORT100_CMD_STATUS_TIMEOUT 0x80
#define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
#define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
struct port100;
typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
struct sk_buff *resp);
/**
* Setting sets structure for in_set_rf command
*
* @in_*_set_number: Represent the entry indexes in the port-100 RF Base Table.
* This table contains multiple RF setting sets required for RF
* communication.
*
* @in_*_comm_type: Theses fields set the communication type to be used.
*/
struct port100_in_rf_setting {
u8 in_send_set_number;
u8 in_send_comm_type;
u8 in_recv_set_number;
u8 in_recv_comm_type;
} __packed;
#define PORT100_COMM_TYPE_IN_212F 0x01
#define PORT100_COMM_TYPE_IN_424F 0x02
#define PORT100_COMM_TYPE_IN_106A 0x03
static const struct port100_in_rf_setting in_rf_settings[] = {
[NFC_DIGITAL_RF_TECH_212F] = {
.in_send_set_number = 1,
.in_send_comm_type = PORT100_COMM_TYPE_IN_212F,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_212F,
},
[NFC_DIGITAL_RF_TECH_424F] = {
.in_send_set_number = 1,
.in_send_comm_type = PORT100_COMM_TYPE_IN_424F,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_424F,
},
[NFC_DIGITAL_RF_TECH_106A] = {
.in_send_set_number = 2,
.in_send_comm_type = PORT100_COMM_TYPE_IN_106A,
.in_recv_set_number = 15,
.in_recv_comm_type = PORT100_COMM_TYPE_IN_106A,
},
/* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
[NFC_DIGITAL_RF_TECH_LAST] = { 0 },
};
/**
* Setting sets structure for tg_set_rf command
*
* @tg_set_number: Represents the entry index in the port-100 RF Base Table.
* This table contains multiple RF setting sets required for RF
* communication. this field is used for both send and receive
* settings.
*
* @tg_comm_type: Sets the communication type to be used to send and receive
* data.
*/
struct port100_tg_rf_setting {
u8 tg_set_number;
u8 tg_comm_type;
} __packed;
#define PORT100_COMM_TYPE_TG_106A 0x0B
#define PORT100_COMM_TYPE_TG_212F 0x0C
#define PORT100_COMM_TYPE_TG_424F 0x0D
static const struct port100_tg_rf_setting tg_rf_settings[] = {
[NFC_DIGITAL_RF_TECH_106A] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_106A,
},
[NFC_DIGITAL_RF_TECH_212F] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_212F,
},
[NFC_DIGITAL_RF_TECH_424F] = {
.tg_set_number = 8,
.tg_comm_type = PORT100_COMM_TYPE_TG_424F,
},
/* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
[NFC_DIGITAL_RF_TECH_LAST] = { 0 },
};
#define PORT100_IN_PROT_INITIAL_GUARD_TIME 0x00
#define PORT100_IN_PROT_ADD_CRC 0x01
#define PORT100_IN_PROT_CHECK_CRC 0x02
#define PORT100_IN_PROT_MULTI_CARD 0x03
#define PORT100_IN_PROT_ADD_PARITY 0x04
#define PORT100_IN_PROT_CHECK_PARITY 0x05
#define PORT100_IN_PROT_BITWISE_AC_RECV_MODE 0x06
#define PORT100_IN_PROT_VALID_BIT_NUMBER 0x07
#define PORT100_IN_PROT_CRYPTO1 0x08
#define PORT100_IN_PROT_ADD_SOF 0x09
#define PORT100_IN_PROT_CHECK_SOF 0x0A
#define PORT100_IN_PROT_ADD_EOF 0x0B
#define PORT100_IN_PROT_CHECK_EOF 0x0C
#define PORT100_IN_PROT_DEAF_TIME 0x0E
#define PORT100_IN_PROT_CRM 0x0F
#define PORT100_IN_PROT_CRM_MIN_LEN 0x10
#define PORT100_IN_PROT_T1_TAG_FRAME 0x11
#define PORT100_IN_PROT_RFCA 0x12
#define PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR 0x13
#define PORT100_IN_PROT_END 0x14
#define PORT100_IN_MAX_NUM_PROTOCOLS 19
#define PORT100_TG_PROT_TU 0x00
#define PORT100_TG_PROT_RF_OFF 0x01
#define PORT100_TG_PROT_CRM 0x02
#define PORT100_TG_PROT_END 0x03
#define PORT100_TG_MAX_NUM_PROTOCOLS 3
struct port100_protocol {
u8 number;
u8 value;
} __packed;
static struct port100_protocol
in_protocols[
|