-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathst25r391x_st25tb.c
182 lines (162 loc) · 5.03 KB
/
st25r391x_st25tb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
/*
* ST25R3916/7 NFC Reader Driver
*
* Copyright (C) 2020-2022 Paul Guyot <pguyot@kallisys.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA
*/
#include <linux/types.h>
#include <linux/i2c.h>
#include <linux/string.h>
#include "nfc.h"
#include "st25r391x_commands.h"
#include "st25r391x_common.h"
#include "st25r391x_dev.h"
#include "st25r391x_i2c.h"
#include "st25r391x_interrupts.h"
#include "st25r391x_nfcb.h"
#include "st25r391x_registers.h"
#include "st25r391x_st25tb.h"
#include "st25r391x.h"
// ST25TB commands
#define ST25TB_COMMAND_INITIATE_H 0x06
#define ST25TB_COMMAND_INITIATE_L 0x00
#define ST25TB_COMMAND_PCALL16_H 0x06
#define ST25TB_COMMAND_PCALL16_L 0x04
#define ST25TB_COMMAND_READ_BLOCK_H 0x08
#define ST25TB_COMMAND_WRITE_BLOCK_H 0x09
#define ST25TB_COMMAND_GET_UID 0x0B
#define ST25TB_COMMAND_RESET_TO_INVENTORY 0x0C
#define ST25TB_COMMAND_SELECT_H 0x0E
#define ST25TB_COMMAND_COMPLETION 0x0F
static s32
st25r391x_st25tb_select_and_get_uid(struct st25r391x_i2c_data *priv,
struct nfc_tag_info_st25tb *tag_info,
u8 chip_id)
{
s32 result;
u8 buffer[10];
struct i2c_client *i2c = priv->i2c;
struct st25r391x_interrupts *ints = &priv->ints;
do {
buffer[0] = ST25TB_COMMAND_SELECT_H;
buffer[1] = chip_id;
result = st25r391x_transceive_frame(
i2c, ints, buffer, 2, buffer, sizeof(buffer), 0,
5000); // TODO: check rx timeout
if (result < 0)
break;
if (result != 3 || buffer[0] != chip_id) {
dev_err(priv->device,
"st25r391x_st25tb_select_and_get_uid: unexpected answer to select, result = %d, buffer[0] = %d, chip_id = %d",
result, buffer[0], chip_id);
result = -1;
break;
}
buffer[0] = ST25TB_COMMAND_GET_UID;
result = st25r391x_transceive_frame(
i2c, ints, buffer, 1, buffer, sizeof(buffer), 0,
5000); // TODO: check rx timeout
if (result < 0)
break;
if (result != 10) {
dev_err(priv->device,
"st25r391x_st25tb_select_and_get_uid: unexpected answer to get_uid, result = %d",
result);
result = -1;
break;
}
memcpy((void *)tag_info->uid, (const void *)buffer,
sizeof(tag_info->uid));
} while (0);
return result;
}
static s32 st25r391x_st25tb_initiate(struct st25r391x_i2c_data *priv,
struct nfc_tag_info_st25tb *tag_info,
u8 *cid)
{
s32 result;
u8 buffer[3];
struct i2c_client *i2c = priv->i2c;
struct st25r391x_interrupts *ints = &priv->ints;
do {
result = st25r391x_set_iso14443b_mode(i2c);
if (result < 0) {
dev_err(priv->device,
"st25r391x_st25tb_initiate: Failed to set iso14443b mode: %d",
result);
break;
}
// Enable Tx & Rx
result = st25r391x_enable_tx_and_rx(i2c);
if (result < 0) {
dev_err(priv->device,
"st25r391x_st25tb_initiate: : Failed to enable tx and rx: %d",
result);
break;
}
buffer[0] = ST25TB_COMMAND_INITIATE_H;
buffer[1] = ST25TB_COMMAND_INITIATE_L;
result = st25r391x_transceive_frame(
i2c, ints, buffer, 2, buffer, sizeof(buffer), 0,
5000); // TODO: check rx timeout
if (result < 0)
break;
if (buffer[0] == 255) {
dev_err(priv->device,
"st25r391x_st25tb_initiate: got a collision, unimplemented");
result = -1;
break;
}
result = st25r391x_st25tb_select_and_get_uid(priv, tag_info,
buffer[0]);
if (result >= 0)
*cid = buffer[0];
} while (0);
return result;
}
void st25r391x_st25tb_discover(struct st25r391x_i2c_data *priv)
{
// Passive poll discover ST25TB
struct nfc_detected_tag_message_payload tag_payload;
s32 result;
u8 cid;
memset(&tag_payload, 0, sizeof(tag_payload));
result = st25r391x_st25tb_initiate(priv, &tag_payload.tag_info.st25tb,
&cid);
if (result >= 0 &&
priv->mode_params.discover.protocols & NFC_TAG_PROTOCOL_ST25TB) {
tag_payload.tag_type = NFC_TAG_TYPE_ST25TB;
st25r391x_process_selected_tag(priv, &tag_payload, cid);
}
}
void st25r391x_st25tb_select(struct st25r391x_i2c_data *priv)
{
// Passive poll select ST25TB
struct nfc_detected_tag_message_payload tag_payload;
s32 result;
u8 cid;
memset(&tag_payload, 0, sizeof(tag_payload));
result = st25r391x_st25tb_initiate(priv, &tag_payload.tag_info.st25tb,
&cid);
if (result >= 0 &&
memcmp(priv->mode_params.select.tag_id.uid,
tag_payload.tag_info.st25tb.uid,
sizeof(tag_payload.tag_info.st25tb.uid)) == 0) {
tag_payload.tag_type = NFC_TAG_TYPE_ST25TB;
st25r391x_process_selected_tag(priv, &tag_payload, cid);
}
}