blob: 2d4bbe3fad57d38432a1df4c76bd56d07bd632cf [file] [log] [blame]
Thierry Escande562d4d52013-10-04 12:12:00 +02001/*
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 | \
Thierry Escande2a26f9a2014-01-27 00:31:33 +010030 NFC_PROTO_NFC_DEP_MASK | \
Thierry Escande09592cc2014-04-12 00:03:17 +020031 NFC_PROTO_ISO14443_MASK | \
32 NFC_PROTO_ISO14443_B_MASK)
Thierry Escande562d4d52013-10-04 12:12:00 +020033
34#define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
35 NFC_DIGITAL_DRV_CAPS_TG_CRC)
36
Thierry Escande0347a6a2013-10-04 12:12:01 +020037/* 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
Thierry Escande9f7b57f2013-10-04 12:12:02 +020076#define PORT100_CMD_IN_SET_RF 0x00
77#define PORT100_CMD_IN_SET_PROTOCOL 0x02
78#define PORT100_CMD_IN_COMM_RF 0x04
79
Thierry Escande7227c022013-10-04 12:12:03 +020080#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
Thierry Escande9f7b57f2013-10-04 12:12:02 +020085#define PORT100_CMD_SWITCH_RF 0x06
86
Thierry Escande0347a6a2013-10-04 12:12:01 +020087#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
Thierry Escande9f7b57f2013-10-04 12:12:02 +020094#define PORT100_CMD_STATUS_OK 0x00
95#define PORT100_CMD_STATUS_TIMEOUT 0x80
96
Thierry Escande7227c022013-10-04 12:12:03 +020097#define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
98#define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
99
Thierry Escande0347a6a2013-10-04 12:12:01 +0200100struct port100;
101
102typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
103 struct sk_buff *resp);
104
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200105/**
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
Thierry Escande09592cc2014-04-12 00:03:17 +0200124#define PORT100_COMM_TYPE_IN_106B 0x07
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200125
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 },
Thierry Escande09592cc2014-04-12 00:03:17 +0200145 [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 },
Thierry Escanded3815ea2014-01-27 00:31:14 +0100151 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
152 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200153};
154
Thierry Escande7227c022013-10-04 12:12:03 +0200155/**
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 },
Thierry Escanded3815ea2014-01-27 00:31:14 +0100188 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
189 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
190
Thierry Escande7227c022013-10-04 12:12:03 +0200191};
192
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200193#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
Thierry Escande7227c022013-10-04 12:12:03 +0200216#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
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200223struct 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 },
Thierry Escande2a26f9a2014-01-27 00:31:33 +0100310 [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
311 /* nfc_digital_framing_nfca_standard_with_crc_a */
312 { PORT100_IN_PROT_END, 0 },
313 },
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200314 [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 */
Thierry Escandea81ba502016-06-07 16:21:51 +0200346 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 18 },
347 { PORT100_IN_PROT_ADD_CRC, 1 },
348 { PORT100_IN_PROT_CHECK_CRC, 1 },
349 { PORT100_IN_PROT_MULTI_CARD, 0 },
350 { PORT100_IN_PROT_ADD_PARITY, 0 },
351 { PORT100_IN_PROT_CHECK_PARITY, 0 },
352 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
353 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
354 { PORT100_IN_PROT_CRYPTO1, 0 },
355 { PORT100_IN_PROT_ADD_SOF, 0 },
356 { PORT100_IN_PROT_CHECK_SOF, 0 },
357 { PORT100_IN_PROT_ADD_EOF, 0 },
358 { PORT100_IN_PROT_CHECK_EOF, 0 },
359 { PORT100_IN_PROT_DEAF_TIME, 4 },
360 { PORT100_IN_PROT_CRM, 0 },
361 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
362 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
363 { PORT100_IN_PROT_RFCA, 0 },
364 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
365 { PORT100_IN_PROT_END, 0 },
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200366 },
367 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
368 { PORT100_IN_PROT_END, 0 },
369 },
Thierry Escande09592cc2014-04-12 00:03:17 +0200370 [NFC_DIGITAL_FRAMING_NFCB] = {
371 { PORT100_IN_PROT_INITIAL_GUARD_TIME, 20 },
372 { PORT100_IN_PROT_ADD_CRC, 1 },
373 { PORT100_IN_PROT_CHECK_CRC, 1 },
374 { PORT100_IN_PROT_MULTI_CARD, 0 },
375 { PORT100_IN_PROT_ADD_PARITY, 0 },
376 { PORT100_IN_PROT_CHECK_PARITY, 0 },
377 { PORT100_IN_PROT_BITWISE_AC_RECV_MODE, 0 },
378 { PORT100_IN_PROT_VALID_BIT_NUMBER, 8 },
379 { PORT100_IN_PROT_CRYPTO1, 0 },
380 { PORT100_IN_PROT_ADD_SOF, 1 },
381 { PORT100_IN_PROT_CHECK_SOF, 1 },
382 { PORT100_IN_PROT_ADD_EOF, 1 },
383 { PORT100_IN_PROT_CHECK_EOF, 1 },
384 { PORT100_IN_PROT_DEAF_TIME, 4 },
385 { PORT100_IN_PROT_CRM, 0 },
386 { PORT100_IN_PROT_CRM_MIN_LEN, 0 },
387 { PORT100_IN_PROT_T1_TAG_FRAME, 0 },
388 { PORT100_IN_PROT_RFCA, 0 },
389 { PORT100_IN_PROT_GUARD_TIME_AT_INITIATOR, 6 },
390 { PORT100_IN_PROT_END, 0 },
391 },
392 [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
393 /* nfc_digital_framing_nfcb */
394 { PORT100_IN_PROT_END, 0 },
395 },
Thierry Escanded3815ea2014-01-27 00:31:14 +0100396 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
397 [NFC_DIGITAL_FRAMING_LAST] = {
398 { PORT100_IN_PROT_END, 0 },
399 },
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200400};
401
Thierry Escande7227c022013-10-04 12:12:03 +0200402static struct port100_protocol
403tg_protocols[][PORT100_TG_MAX_NUM_PROTOCOLS + 1] = {
404 [NFC_DIGITAL_FRAMING_NFCA_SHORT] = {
405 { PORT100_TG_PROT_END, 0 },
406 },
407 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
408 { PORT100_TG_PROT_END, 0 },
409 },
410 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
411 { PORT100_TG_PROT_END, 0 },
412 },
413 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
414 { PORT100_TG_PROT_END, 0 },
415 },
416 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
417 { PORT100_TG_PROT_END, 0 },
418 },
419 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
420 { PORT100_TG_PROT_TU, 1 },
421 { PORT100_TG_PROT_RF_OFF, 0 },
422 { PORT100_TG_PROT_CRM, 7 },
423 { PORT100_TG_PROT_END, 0 },
424 },
425 [NFC_DIGITAL_FRAMING_NFCF] = {
426 { PORT100_TG_PROT_END, 0 },
427 },
428 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
429 { PORT100_TG_PROT_END, 0 },
430 },
431 [NFC_DIGITAL_FRAMING_NFCF_NFC_DEP] = {
432 { PORT100_TG_PROT_TU, 1 },
433 { PORT100_TG_PROT_RF_OFF, 0 },
434 { PORT100_TG_PROT_CRM, 7 },
435 { PORT100_TG_PROT_END, 0 },
436 },
437 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
438 { PORT100_TG_PROT_RF_OFF, 1 },
439 { PORT100_TG_PROT_END, 0 },
440 },
Thierry Escanded3815ea2014-01-27 00:31:14 +0100441 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
442 [NFC_DIGITAL_FRAMING_LAST] = {
443 { PORT100_TG_PROT_END, 0 },
444 },
Thierry Escande7227c022013-10-04 12:12:03 +0200445};
446
Thierry Escande562d4d52013-10-04 12:12:00 +0200447struct port100 {
448 struct nfc_digital_dev *nfc_digital_dev;
449
450 int skb_headroom;
451 int skb_tailroom;
452
453 struct usb_device *udev;
454 struct usb_interface *interface;
Thierry Escande0347a6a2013-10-04 12:12:01 +0200455
456 struct urb *out_urb;
457 struct urb *in_urb;
458
459 struct work_struct cmd_complete_work;
460
461 u8 cmd_type;
462
463 /* The digital stack serializes commands to be sent. There is no need
464 * for any queuing/locking mechanism at driver level.
465 */
466 struct port100_cmd *cmd;
Thierry Escande562d4d52013-10-04 12:12:00 +0200467};
468
Thierry Escande0347a6a2013-10-04 12:12:01 +0200469struct port100_cmd {
470 u8 code;
471 int status;
472 struct sk_buff *req;
473 struct sk_buff *resp;
474 int resp_len;
475 port100_send_async_complete_t complete_cb;
476 void *complete_cb_context;
477};
478
479struct port100_frame {
480 u8 preamble;
481 __be16 start_frame;
482 __be16 extended_frame;
483 __le16 datalen;
484 u8 datalen_checksum;
485 u8 data[];
486} __packed;
487
488struct port100_ack_frame {
489 u8 preamble;
490 __be16 start_frame;
491 __be16 ack_frame;
492 u8 postambule;
493} __packed;
494
495struct port100_cb_arg {
496 nfc_digital_cmd_complete_t complete_cb;
497 void *complete_arg;
498 u8 mdaa;
499};
500
501struct port100_tg_comm_rf_cmd {
502 __le16 guard_time;
503 __le16 send_timeout;
504 u8 mdaa;
505 u8 nfca_param[6];
506 u8 nfcf_param[18];
507 u8 mf_halted;
508 u8 arae_flag;
509 __le16 recv_timeout;
510 u8 data[];
511} __packed;
512
Thierry Escande7227c022013-10-04 12:12:03 +0200513struct port100_tg_comm_rf_res {
514 u8 comm_type;
515 u8 ar_status;
516 u8 target_activated;
517 __le32 status;
518 u8 data[];
519} __packed;
520
Thierry Escande0347a6a2013-10-04 12:12:01 +0200521/* The rule: value + checksum = 0 */
522static inline u8 port100_checksum(u16 value)
523{
524 return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
525}
526
527/* The rule: sum(data elements) + checksum = 0 */
528static u8 port100_data_checksum(u8 *data, int datalen)
529{
530 u8 sum = 0;
531 int i;
532
533 for (i = 0; i < datalen; i++)
534 sum += data[i];
535
536 return port100_checksum(sum);
537}
538
539static void port100_tx_frame_init(void *_frame, u8 cmd_code)
540{
541 struct port100_frame *frame = _frame;
542
543 frame->preamble = 0;
544 frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
545 frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
546 PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
547 PORT100_FRAME_CMD(frame) = cmd_code;
548 frame->datalen = cpu_to_le16(2);
549}
550
551static void port100_tx_frame_finish(void *_frame)
552{
553 struct port100_frame *frame = _frame;
554
555 frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
556
557 PORT100_FRAME_CHECKSUM(frame) =
558 port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
559
560 PORT100_FRAME_POSTAMBLE(frame) = 0;
561}
562
563static void port100_tx_update_payload_len(void *_frame, int len)
564{
565 struct port100_frame *frame = _frame;
566
567 frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
568}
569
570static bool port100_rx_frame_is_valid(void *_frame)
571{
572 u8 checksum;
573 struct port100_frame *frame = _frame;
574
575 if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
576 frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
577 return false;
578
579 checksum = port100_checksum(le16_to_cpu(frame->datalen));
580 if (checksum != frame->datalen_checksum)
581 return false;
582
583 checksum = port100_data_checksum(frame->data,
584 le16_to_cpu(frame->datalen));
585 if (checksum != PORT100_FRAME_CHECKSUM(frame))
586 return false;
587
588 return true;
589}
590
591static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
592{
593 return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
594 frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
595}
596
597static inline int port100_rx_frame_size(void *frame)
598{
599 struct port100_frame *f = frame;
600
601 return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
602 PORT100_FRAME_TAIL_LEN;
603}
604
605static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
606{
607 struct port100_frame *f = frame;
608
609 return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
610}
611
612static void port100_recv_response(struct urb *urb)
613{
614 struct port100 *dev = urb->context;
615 struct port100_cmd *cmd = dev->cmd;
616 u8 *in_frame;
617
618 cmd->status = urb->status;
619
620 switch (urb->status) {
621 case 0:
622 break; /* success */
623 case -ECONNRESET:
624 case -ENOENT:
625 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -0700626 "The urb has been canceled (status %d)\n", urb->status);
Thierry Escande0347a6a2013-10-04 12:12:01 +0200627 goto sched_wq;
628 case -ESHUTDOWN:
629 default:
Joe Perches3590ebc2015-04-07 00:17:00 -0700630 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
Thierry Escande0347a6a2013-10-04 12:12:01 +0200631 urb->status);
632 goto sched_wq;
633 }
634
635 in_frame = dev->in_urb->transfer_buffer;
636
637 if (!port100_rx_frame_is_valid(in_frame)) {
Joe Perches3590ebc2015-04-07 00:17:00 -0700638 nfc_err(&dev->interface->dev, "Received an invalid frame\n");
Thierry Escande0347a6a2013-10-04 12:12:01 +0200639 cmd->status = -EIO;
640 goto sched_wq;
641 }
642
643 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
644 port100_rx_frame_size(in_frame), false);
645
646 if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
647 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -0700648 "It's not the response to the last command\n");
Thierry Escande0347a6a2013-10-04 12:12:01 +0200649 cmd->status = -EIO;
650 goto sched_wq;
651 }
652
653sched_wq:
654 schedule_work(&dev->cmd_complete_work);
655}
656
657static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
658{
659 dev->in_urb->complete = port100_recv_response;
660
661 return usb_submit_urb(dev->in_urb, flags);
662}
663
664static void port100_recv_ack(struct urb *urb)
665{
666 struct port100 *dev = urb->context;
667 struct port100_cmd *cmd = dev->cmd;
668 struct port100_ack_frame *in_frame;
669 int rc;
670
671 cmd->status = urb->status;
672
673 switch (urb->status) {
674 case 0:
675 break; /* success */
676 case -ECONNRESET:
677 case -ENOENT:
678 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -0700679 "The urb has been stopped (status %d)\n", urb->status);
Thierry Escande0347a6a2013-10-04 12:12:01 +0200680 goto sched_wq;
681 case -ESHUTDOWN:
682 default:
Joe Perches3590ebc2015-04-07 00:17:00 -0700683 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
Thierry Escande0347a6a2013-10-04 12:12:01 +0200684 urb->status);
685 goto sched_wq;
686 }
687
688 in_frame = dev->in_urb->transfer_buffer;
689
690 if (!port100_rx_frame_is_ack(in_frame)) {
Joe Perches3590ebc2015-04-07 00:17:00 -0700691 nfc_err(&dev->interface->dev, "Received an invalid ack\n");
Thierry Escande0347a6a2013-10-04 12:12:01 +0200692 cmd->status = -EIO;
693 goto sched_wq;
694 }
695
696 rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
697 if (rc) {
698 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -0700699 "usb_submit_urb failed with result %d\n", rc);
Thierry Escande0347a6a2013-10-04 12:12:01 +0200700 cmd->status = rc;
701 goto sched_wq;
702 }
703
704 return;
705
706sched_wq:
707 schedule_work(&dev->cmd_complete_work);
708}
709
710static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
711{
712 dev->in_urb->complete = port100_recv_ack;
713
714 return usb_submit_urb(dev->in_urb, flags);
715}
716
717static int port100_send_ack(struct port100 *dev)
718{
719 int rc;
720
721 dev->out_urb->transfer_buffer = ack_frame;
722 dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
723 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
724
725 return rc;
726}
727
728static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
729 struct sk_buff *in, int in_len)
730{
731 int rc;
732
733 dev->out_urb->transfer_buffer = out->data;
734 dev->out_urb->transfer_buffer_length = out->len;
735
736 dev->in_urb->transfer_buffer = in->data;
737 dev->in_urb->transfer_buffer_length = in_len;
738
739 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
740 out->data, out->len, false);
741
742 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
743 if (rc)
744 return rc;
745
746 rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
747 if (rc)
748 goto error;
749
750 return 0;
751
752error:
753 usb_unlink_urb(dev->out_urb);
754 return rc;
755}
756
757static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
758 struct sk_buff *skb)
759{
760 /* payload is already there, just update datalen */
761 int payload_len = skb->len;
762
763 skb_push(skb, PORT100_FRAME_HEADER_LEN);
764 skb_put(skb, PORT100_FRAME_TAIL_LEN);
765
766 port100_tx_frame_init(skb->data, cmd_code);
767 port100_tx_update_payload_len(skb->data, payload_len);
768 port100_tx_frame_finish(skb->data);
769}
770
771static void port100_send_async_complete(struct port100 *dev)
772{
773 struct port100_cmd *cmd = dev->cmd;
774 int status = cmd->status;
775
776 struct sk_buff *req = cmd->req;
777 struct sk_buff *resp = cmd->resp;
778
779 dev_kfree_skb(req);
780
781 dev->cmd = NULL;
782
783 if (status < 0) {
784 cmd->complete_cb(dev, cmd->complete_cb_context,
785 ERR_PTR(status));
786 dev_kfree_skb(resp);
787 goto done;
788 }
789
790 skb_put(resp, port100_rx_frame_size(resp->data));
791 skb_pull(resp, PORT100_FRAME_HEADER_LEN);
792 skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
793
794 cmd->complete_cb(dev, cmd->complete_cb_context, resp);
795
796done:
797 kfree(cmd);
798}
799
800static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
801 struct sk_buff *req,
802 port100_send_async_complete_t complete_cb,
803 void *complete_cb_context)
804{
805 struct port100_cmd *cmd;
806 struct sk_buff *resp;
807 int rc;
808 int resp_len = PORT100_FRAME_HEADER_LEN +
809 PORT100_FRAME_MAX_PAYLOAD_LEN +
810 PORT100_FRAME_TAIL_LEN;
811
812 resp = alloc_skb(resp_len, GFP_KERNEL);
813 if (!resp)
814 return -ENOMEM;
815
816 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
817 if (!cmd) {
818 dev_kfree_skb(resp);
819 return -ENOMEM;
820 }
821
822 cmd->code = cmd_code;
823 cmd->req = req;
824 cmd->resp = resp;
825 cmd->resp_len = resp_len;
826 cmd->complete_cb = complete_cb;
827 cmd->complete_cb_context = complete_cb_context;
828
829 port100_build_cmd_frame(dev, cmd_code, req);
830
831 dev->cmd = cmd;
832
833 rc = port100_send_frame_async(dev, req, resp, resp_len);
834 if (rc) {
835 kfree(cmd);
836 dev_kfree_skb(resp);
837 dev->cmd = NULL;
838 }
839
840 return rc;
841}
842
843struct port100_sync_cmd_response {
844 struct sk_buff *resp;
845 struct completion done;
846};
847
848static void port100_wq_cmd_complete(struct work_struct *work)
849{
850 struct port100 *dev = container_of(work, struct port100,
851 cmd_complete_work);
852
853 port100_send_async_complete(dev);
854}
855
856static void port100_send_sync_complete(struct port100 *dev, void *_arg,
857 struct sk_buff *resp)
858{
859 struct port100_sync_cmd_response *arg = _arg;
860
861 arg->resp = resp;
862 complete(&arg->done);
863}
864
865static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
866 struct sk_buff *req)
867{
868 int rc;
869 struct port100_sync_cmd_response arg;
870
871 init_completion(&arg.done);
872
873 rc = port100_send_cmd_async(dev, cmd_code, req,
874 port100_send_sync_complete, &arg);
875 if (rc) {
876 dev_kfree_skb(req);
877 return ERR_PTR(rc);
878 }
879
880 wait_for_completion(&arg.done);
881
882 return arg.resp;
883}
884
885static void port100_send_complete(struct urb *urb)
886{
887 struct port100 *dev = urb->context;
888
889 switch (urb->status) {
890 case 0:
891 break; /* success */
892 case -ECONNRESET:
893 case -ENOENT:
894 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -0700895 "The urb has been stopped (status %d)\n", urb->status);
Thierry Escande0347a6a2013-10-04 12:12:01 +0200896 break;
897 case -ESHUTDOWN:
898 default:
Joe Perches3590ebc2015-04-07 00:17:00 -0700899 nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
Thierry Escande0347a6a2013-10-04 12:12:01 +0200900 urb->status);
901 }
902}
903
Thierry Escande562d4d52013-10-04 12:12:00 +0200904static void port100_abort_cmd(struct nfc_digital_dev *ddev)
905{
Thierry Escande0347a6a2013-10-04 12:12:01 +0200906 struct port100 *dev = nfc_digital_get_drvdata(ddev);
907
908 /* An ack will cancel the last issued command */
909 port100_send_ack(dev);
910
911 /* cancel the urb request */
912 usb_kill_urb(dev->in_urb);
913}
914
915static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
916{
917 struct sk_buff *skb;
918
919 skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
920 GFP_KERNEL);
921 if (skb)
922 skb_reserve(skb, dev->skb_headroom);
923
924 return skb;
925}
926
927static int port100_set_command_type(struct port100 *dev, u8 command_type)
928{
929 struct sk_buff *skb;
930 struct sk_buff *resp;
931 int rc;
932
933 skb = port100_alloc_skb(dev, 1);
934 if (!skb)
935 return -ENOMEM;
936
937 *skb_put(skb, sizeof(u8)) = command_type;
938
939 resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
940 if (IS_ERR(resp))
941 return PTR_ERR(resp);
942
943 rc = resp->data[0];
944
945 dev_kfree_skb(resp);
946
947 return rc;
948}
949
950static u64 port100_get_command_type_mask(struct port100 *dev)
951{
952 struct sk_buff *skb;
953 struct sk_buff *resp;
954 u64 mask;
955
956 skb = port100_alloc_skb(dev, 0);
957 if (!skb)
958 return -ENOMEM;
959
960 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
961 if (IS_ERR(resp))
962 return PTR_ERR(resp);
963
964 if (resp->len < 8)
965 mask = 0;
966 else
967 mask = be64_to_cpu(*(__be64 *)resp->data);
968
969 dev_kfree_skb(resp);
970
971 return mask;
972}
973
974static u16 port100_get_firmware_version(struct port100 *dev)
975{
976 struct sk_buff *skb;
977 struct sk_buff *resp;
978 u16 fw_ver;
979
980 skb = port100_alloc_skb(dev, 0);
981 if (!skb)
982 return 0;
983
984 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
985 skb);
986 if (IS_ERR(resp))
987 return 0;
988
989 fw_ver = le16_to_cpu(*(__le16 *)resp->data);
990
991 dev_kfree_skb(resp);
992
993 return fw_ver;
Thierry Escande562d4d52013-10-04 12:12:00 +0200994}
995
996static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
997{
Thierry Escande9f7b57f2013-10-04 12:12:02 +0200998 struct port100 *dev = nfc_digital_get_drvdata(ddev);
999 struct sk_buff *skb, *resp;
1000
1001 skb = port100_alloc_skb(dev, 1);
1002 if (!skb)
1003 return -ENOMEM;
1004
1005 *skb_put(skb, 1) = on ? 1 : 0;
1006
1007 resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
1008
1009 if (IS_ERR(resp))
1010 return PTR_ERR(resp);
1011
1012 dev_kfree_skb(resp);
1013
1014 return 0;
1015}
1016
1017static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1018{
1019 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1020 struct sk_buff *skb;
1021 struct sk_buff *resp;
1022 int rc;
1023
1024 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1025 return -EINVAL;
1026
1027 skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
1028 if (!skb)
1029 return -ENOMEM;
1030
1031 memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
1032 &in_rf_settings[rf],
1033 sizeof(struct port100_in_rf_setting));
1034
1035 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1036
1037 if (IS_ERR(resp))
1038 return PTR_ERR(resp);
1039
1040 rc = resp->data[0];
1041
1042 dev_kfree_skb(resp);
1043
1044 return rc;
1045}
1046
1047static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1048{
1049 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1050 struct port100_protocol *protocols;
1051 struct sk_buff *skb;
1052 struct sk_buff *resp;
1053 int num_protocols;
1054 size_t size;
1055 int rc;
1056
1057 if (param >= NFC_DIGITAL_FRAMING_LAST)
1058 return -EINVAL;
1059
1060 protocols = in_protocols[param];
1061
1062 num_protocols = 0;
1063 while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1064 num_protocols++;
1065
1066 if (!num_protocols)
1067 return 0;
1068
1069 size = sizeof(struct port100_protocol) * num_protocols;
1070
1071 skb = port100_alloc_skb(dev, size);
1072 if (!skb)
1073 return -ENOMEM;
1074
1075 memcpy(skb_put(skb, size), protocols, size);
1076
1077 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1078
1079 if (IS_ERR(resp))
1080 return PTR_ERR(resp);
1081
1082 rc = resp->data[0];
1083
1084 dev_kfree_skb(resp);
1085
1086 return rc;
Thierry Escande562d4d52013-10-04 12:12:00 +02001087}
1088
1089static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1090 int param)
1091{
Thierry Escande9f7b57f2013-10-04 12:12:02 +02001092 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1093 return port100_in_set_rf(ddev, param);
1094
1095 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1096 return port100_in_set_framing(ddev, param);
1097
1098 return -EINVAL;
1099}
1100
1101static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1102 struct sk_buff *resp)
1103{
1104 struct port100_cb_arg *cb_arg = arg;
1105 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1106 u32 status;
1107 int rc;
1108
1109 if (IS_ERR(resp)) {
1110 rc = PTR_ERR(resp);
1111 goto exit;
1112 }
1113
1114 if (resp->len < 4) {
1115 nfc_err(&dev->interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001116 "Invalid packet length received\n");
Thierry Escande9f7b57f2013-10-04 12:12:02 +02001117 rc = -EIO;
1118 goto error;
1119 }
1120
1121 status = le32_to_cpu(*(__le32 *)resp->data);
1122
1123 skb_pull(resp, sizeof(u32));
1124
1125 if (status == PORT100_CMD_STATUS_TIMEOUT) {
1126 rc = -ETIMEDOUT;
1127 goto error;
1128 }
1129
1130 if (status != PORT100_CMD_STATUS_OK) {
1131 nfc_err(&dev->interface->dev,
1132 "in_comm_rf failed with status 0x%08x\n", status);
1133 rc = -EIO;
1134 goto error;
1135 }
1136
1137 /* Remove collision bits byte */
1138 skb_pull(resp, 1);
1139
1140 goto exit;
1141
1142error:
1143 kfree_skb(resp);
1144 resp = ERR_PTR(rc);
1145
1146exit:
1147 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1148
1149 kfree(cb_arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001150}
1151
1152static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1153 struct sk_buff *skb, u16 _timeout,
1154 nfc_digital_cmd_complete_t cb, void *arg)
1155{
Thierry Escande9f7b57f2013-10-04 12:12:02 +02001156 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1157 struct port100_cb_arg *cb_arg;
1158 __le16 timeout;
1159
1160 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1161 if (!cb_arg)
1162 return -ENOMEM;
1163
1164 cb_arg->complete_cb = cb;
1165 cb_arg->complete_arg = arg;
1166
1167 timeout = cpu_to_le16(_timeout * 10);
1168
1169 memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1170
1171 return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1172 port100_in_comm_rf_complete, cb_arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001173}
1174
Thierry Escande7227c022013-10-04 12:12:03 +02001175static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1176{
1177 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1178 struct sk_buff *skb;
1179 struct sk_buff *resp;
1180 int rc;
1181
1182 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1183 return -EINVAL;
1184
1185 skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1186 if (!skb)
1187 return -ENOMEM;
1188
1189 memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1190 &tg_rf_settings[rf],
1191 sizeof(struct port100_tg_rf_setting));
1192
1193 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1194
1195 if (IS_ERR(resp))
1196 return PTR_ERR(resp);
1197
1198 rc = resp->data[0];
1199
1200 dev_kfree_skb(resp);
1201
1202 return rc;
1203}
1204
1205static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1206{
1207 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1208 struct port100_protocol *protocols;
1209 struct sk_buff *skb;
1210 struct sk_buff *resp;
1211 int rc;
1212 int num_protocols;
1213 size_t size;
1214
1215 if (param >= NFC_DIGITAL_FRAMING_LAST)
1216 return -EINVAL;
1217
1218 protocols = tg_protocols[param];
1219
1220 num_protocols = 0;
1221 while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1222 num_protocols++;
1223
1224 if (!num_protocols)
1225 return 0;
1226
1227 size = sizeof(struct port100_protocol) * num_protocols;
1228
1229 skb = port100_alloc_skb(dev, size);
1230 if (!skb)
1231 return -ENOMEM;
1232
1233 memcpy(skb_put(skb, size), protocols, size);
1234
1235 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1236
1237 if (IS_ERR(resp))
1238 return PTR_ERR(resp);
1239
1240 rc = resp->data[0];
1241
1242 dev_kfree_skb(resp);
1243
1244 return rc;
1245}
1246
Thierry Escande562d4d52013-10-04 12:12:00 +02001247static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1248 int param)
1249{
Thierry Escande7227c022013-10-04 12:12:03 +02001250 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1251 return port100_tg_set_rf(ddev, param);
1252
1253 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1254 return port100_tg_set_framing(ddev, param);
1255
1256 return -EINVAL;
1257}
1258
1259static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1260{
1261 u8 mask;
1262
1263 switch (dev->cmd_type) {
1264 case PORT100_CMD_TYPE_0:
1265 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1266 break;
1267 case PORT100_CMD_TYPE_1:
1268 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1269 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1270 break;
1271 default:
Joe Perches3590ebc2015-04-07 00:17:00 -07001272 nfc_err(&dev->interface->dev, "Unknown command type\n");
Thierry Escande7227c022013-10-04 12:12:03 +02001273 return false;
1274 }
1275
1276 return ((tgt_activated & mask) == mask);
1277}
1278
1279static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1280 struct sk_buff *resp)
1281{
1282 u32 status;
1283 struct port100_cb_arg *cb_arg = arg;
1284 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1285 struct port100_tg_comm_rf_res *hdr;
1286
1287 if (IS_ERR(resp))
1288 goto exit;
1289
1290 hdr = (struct port100_tg_comm_rf_res *)resp->data;
1291
1292 status = le32_to_cpu(hdr->status);
1293
1294 if (cb_arg->mdaa &&
1295 !port100_tg_target_activated(dev, hdr->target_activated)) {
1296 kfree_skb(resp);
1297 resp = ERR_PTR(-ETIMEDOUT);
1298
1299 goto exit;
1300 }
1301
1302 skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1303
1304 if (status != PORT100_CMD_STATUS_OK) {
1305 kfree_skb(resp);
1306
1307 if (status == PORT100_CMD_STATUS_TIMEOUT)
1308 resp = ERR_PTR(-ETIMEDOUT);
1309 else
1310 resp = ERR_PTR(-EIO);
1311 }
1312
1313exit:
1314 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1315
1316 kfree(cb_arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001317}
1318
1319static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1320 struct sk_buff *skb, u16 timeout,
1321 nfc_digital_cmd_complete_t cb, void *arg)
1322{
Thierry Escande7227c022013-10-04 12:12:03 +02001323 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1324 struct port100_tg_comm_rf_cmd *hdr;
1325 struct port100_cb_arg *cb_arg;
1326
1327 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1328 if (!cb_arg)
1329 return -ENOMEM;
1330
1331 cb_arg->complete_cb = cb;
1332 cb_arg->complete_arg = arg;
1333
1334 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1335
1336 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1337
1338 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1339 hdr->guard_time = cpu_to_le16(500);
1340 hdr->send_timeout = cpu_to_le16(0xFFFF);
1341 hdr->recv_timeout = cpu_to_le16(timeout);
1342
1343 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1344 port100_tg_comm_rf_complete, cb_arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001345}
1346
1347static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1348 struct digital_tg_mdaa_params *params,
1349 u16 timeout,
1350 nfc_digital_cmd_complete_t cb, void *arg)
1351{
Thierry Escande7227c022013-10-04 12:12:03 +02001352 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1353 struct port100_tg_comm_rf_cmd *hdr;
1354 struct port100_cb_arg *cb_arg;
1355 struct sk_buff *skb;
1356 int rc;
1357
1358 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1359 NFC_DIGITAL_RF_TECH_106A);
1360 if (rc)
1361 return rc;
1362
1363 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1364 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1365 if (rc)
1366 return rc;
1367
1368 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1369 if (!cb_arg)
1370 return -ENOMEM;
1371
1372 cb_arg->complete_cb = cb;
1373 cb_arg->complete_arg = arg;
1374 cb_arg->mdaa = 1;
1375
1376 skb = port100_alloc_skb(dev, 0);
1377 if (!skb) {
1378 kfree(cb_arg);
1379 return -ENOMEM;
1380 }
1381
1382 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1383 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1384
1385 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1386
1387 hdr->guard_time = 0;
1388 hdr->send_timeout = cpu_to_le16(0xFFFF);
1389 hdr->mdaa = 1;
1390 hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1391 hdr->nfca_param[1] = params->sens_res & 0xFF;
1392 memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1393 hdr->nfca_param[5] = params->sel_res;
1394 memcpy(hdr->nfcf_param, params->nfcid2, 8);
1395 hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1396 hdr->nfcf_param[17] = params->sc & 0xFF;
1397 hdr->recv_timeout = cpu_to_le16(timeout);
1398
1399 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1400 port100_tg_comm_rf_complete, cb_arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001401}
1402
1403static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1404 nfc_digital_cmd_complete_t cb, void *arg)
1405{
Thierry Escande7227c022013-10-04 12:12:03 +02001406 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1407 struct sk_buff *skb;
1408
1409 skb = port100_alloc_skb(dev, 0);
1410 if (!skb)
1411 return -ENOMEM;
1412
1413 return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
Thierry Escande562d4d52013-10-04 12:12:00 +02001414}
1415
1416static struct nfc_digital_ops port100_digital_ops = {
1417 .in_configure_hw = port100_in_configure_hw,
1418 .in_send_cmd = port100_in_send_cmd,
1419
1420 .tg_listen_mdaa = port100_listen_mdaa,
1421 .tg_listen = port100_listen,
1422 .tg_configure_hw = port100_tg_configure_hw,
1423 .tg_send_cmd = port100_tg_send_cmd,
1424
1425 .switch_rf = port100_switch_rf,
1426 .abort_cmd = port100_abort_cmd,
1427};
1428
1429static const struct usb_device_id port100_table[] = {
Axel Lin4aa7ed02014-02-14 17:00:12 +08001430 { USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
Thierry Escande562d4d52013-10-04 12:12:00 +02001431 { }
1432};
1433MODULE_DEVICE_TABLE(usb, port100_table);
1434
1435static int port100_probe(struct usb_interface *interface,
1436 const struct usb_device_id *id)
1437{
1438 struct port100 *dev;
1439 int rc;
Thierry Escande0347a6a2013-10-04 12:12:01 +02001440 struct usb_host_interface *iface_desc;
1441 struct usb_endpoint_descriptor *endpoint;
1442 int in_endpoint;
1443 int out_endpoint;
1444 u16 fw_version;
1445 u64 cmd_type_mask;
1446 int i;
Thierry Escande562d4d52013-10-04 12:12:00 +02001447
1448 dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1449 if (!dev)
1450 return -ENOMEM;
1451
1452 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1453 dev->interface = interface;
1454 usb_set_intfdata(interface, dev);
1455
Thierry Escande0347a6a2013-10-04 12:12:01 +02001456 in_endpoint = out_endpoint = 0;
1457 iface_desc = interface->cur_altsetting;
1458 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1459 endpoint = &iface_desc->endpoint[i].desc;
1460
1461 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1462 in_endpoint = endpoint->bEndpointAddress;
1463
1464 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1465 out_endpoint = endpoint->bEndpointAddress;
1466 }
1467
1468 if (!in_endpoint || !out_endpoint) {
1469 nfc_err(&interface->dev,
1470 "Could not find bulk-in or bulk-out endpoint\n");
1471 rc = -ENODEV;
1472 goto error;
1473 }
1474
1475 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1476 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1477
1478 if (!dev->in_urb || !dev->out_urb) {
1479 nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1480 rc = -ENOMEM;
1481 goto error;
1482 }
1483
1484 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1485 usb_rcvbulkpipe(dev->udev, in_endpoint),
1486 NULL, 0, NULL, dev);
1487 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1488 usb_sndbulkpipe(dev->udev, out_endpoint),
1489 NULL, 0, port100_send_complete, dev);
1490
1491 dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1492 PORT100_COMM_RF_HEAD_MAX_LEN;
1493 dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1494
1495 INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1496
1497 /* The first thing to do with the Port-100 is to set the command type
1498 * to be used. If supported we use command type 1. 0 otherwise.
1499 */
1500 cmd_type_mask = port100_get_command_type_mask(dev);
1501 if (!cmd_type_mask) {
1502 nfc_err(&interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001503 "Could not get supported command types\n");
Thierry Escande0347a6a2013-10-04 12:12:01 +02001504 rc = -ENODEV;
1505 goto error;
1506 }
1507
1508 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1509 dev->cmd_type = PORT100_CMD_TYPE_1;
1510 else
1511 dev->cmd_type = PORT100_CMD_TYPE_0;
1512
1513 rc = port100_set_command_type(dev, dev->cmd_type);
1514 if (rc) {
1515 nfc_err(&interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001516 "The device does not support command type %u\n",
Thierry Escande0347a6a2013-10-04 12:12:01 +02001517 dev->cmd_type);
1518 goto error;
1519 }
1520
1521 fw_version = port100_get_firmware_version(dev);
1522 if (!fw_version)
1523 nfc_err(&interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001524 "Could not get device firmware version\n");
Thierry Escande0347a6a2013-10-04 12:12:01 +02001525
1526 nfc_info(&interface->dev,
1527 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1528 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
Thierry Escande562d4d52013-10-04 12:12:00 +02001529
1530 dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1531 PORT100_PROTOCOLS,
1532 PORT100_CAPABILITIES,
1533 dev->skb_headroom,
1534 dev->skb_tailroom);
1535 if (!dev->nfc_digital_dev) {
1536 nfc_err(&interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001537 "Could not allocate nfc_digital_dev\n");
Thierry Escande562d4d52013-10-04 12:12:00 +02001538 rc = -ENOMEM;
1539 goto error;
1540 }
1541
1542 nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1543 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1544
1545 rc = nfc_digital_register_device(dev->nfc_digital_dev);
1546 if (rc) {
1547 nfc_err(&interface->dev,
Joe Perches3590ebc2015-04-07 00:17:00 -07001548 "Could not register digital device\n");
Thierry Escande562d4d52013-10-04 12:12:00 +02001549 goto free_nfc_dev;
1550 }
1551
1552 return 0;
1553
1554free_nfc_dev:
1555 nfc_digital_free_device(dev->nfc_digital_dev);
1556
1557error:
Thierry Escande0347a6a2013-10-04 12:12:01 +02001558 usb_free_urb(dev->in_urb);
1559 usb_free_urb(dev->out_urb);
1560 usb_put_dev(dev->udev);
1561
Thierry Escande562d4d52013-10-04 12:12:00 +02001562 return rc;
1563}
1564
1565static void port100_disconnect(struct usb_interface *interface)
1566{
1567 struct port100 *dev;
1568
1569 dev = usb_get_intfdata(interface);
1570 usb_set_intfdata(interface, NULL);
1571
1572 nfc_digital_unregister_device(dev->nfc_digital_dev);
1573 nfc_digital_free_device(dev->nfc_digital_dev);
1574
Thierry Escande0347a6a2013-10-04 12:12:01 +02001575 usb_kill_urb(dev->in_urb);
1576 usb_kill_urb(dev->out_urb);
1577
1578 usb_free_urb(dev->in_urb);
1579 usb_free_urb(dev->out_urb);
Alexey Khoroshilovc36aeba2014-01-05 00:08:05 +04001580 usb_put_dev(dev->udev);
Thierry Escande0347a6a2013-10-04 12:12:01 +02001581
1582 kfree(dev->cmd);
1583
Joe Perches3590ebc2015-04-07 00:17:00 -07001584 nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
Thierry Escande562d4d52013-10-04 12:12:00 +02001585}
1586
1587static struct usb_driver port100_driver = {
1588 .name = "port100",
1589 .probe = port100_probe,
1590 .disconnect = port100_disconnect,
1591 .id_table = port100_table,
1592};
1593
1594module_usb_driver(port100_driver);
1595
1596MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1597MODULE_VERSION(VERSION);
1598MODULE_LICENSE("GPL");