xref: /linux/drivers/nfc/port100.c (revision b6ebbac51bedf9e98e837688bc838f400196da5e)
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) */
52 static 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 
100 struct port100;
101 
102 typedef 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  */
114 struct 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 
126 static 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  */
166 struct 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 
175 static 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 
223 struct port100_protocol {
224 	u8 number;
225 	u8 value;
226 } __packed;
227 
228 static struct port100_protocol
229 in_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_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 },
366 	},
367 	[NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
368 		{ PORT100_IN_PROT_END, 0 },
369 	},
370 	[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 	},
396 	/* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
397 	[NFC_DIGITAL_FRAMING_LAST] = {
398 		{ PORT100_IN_PROT_END, 0 },
399 	},
400 };
401 
402 static struct port100_protocol
403 tg_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 	},
441 	/* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
442 	[NFC_DIGITAL_FRAMING_LAST] = {
443 		{ PORT100_TG_PROT_END,    0 },
444 	},
445 };
446 
447 struct 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;
455 
456 	struct urb *out_urb;
457 	struct urb *in_urb;
458 
459 	/* This mutex protects the out_urb and avoids to submit a new command
460 	 * through port100_send_frame_async() while the previous one is being
461 	 * canceled through port100_abort_cmd().
462 	 */
463 	struct mutex out_urb_lock;
464 
465 	struct work_struct cmd_complete_work;
466 
467 	u8 cmd_type;
468 
469 	/* The digital stack serializes commands to be sent. There is no need
470 	 * for any queuing/locking mechanism at driver level.
471 	 */
472 	struct port100_cmd *cmd;
473 
474 	bool cmd_cancel;
475 	struct completion cmd_cancel_done;
476 };
477 
478 struct port100_cmd {
479 	u8 code;
480 	int status;
481 	struct sk_buff *req;
482 	struct sk_buff *resp;
483 	int resp_len;
484 	port100_send_async_complete_t  complete_cb;
485 	void *complete_cb_context;
486 };
487 
488 struct port100_frame {
489 	u8 preamble;
490 	__be16 start_frame;
491 	__be16 extended_frame;
492 	__le16 datalen;
493 	u8 datalen_checksum;
494 	u8 data[];
495 } __packed;
496 
497 struct port100_ack_frame {
498 	u8 preamble;
499 	__be16 start_frame;
500 	__be16 ack_frame;
501 	u8 postambule;
502 } __packed;
503 
504 struct port100_cb_arg {
505 	nfc_digital_cmd_complete_t complete_cb;
506 	void *complete_arg;
507 	u8 mdaa;
508 };
509 
510 struct port100_tg_comm_rf_cmd {
511 	__le16 guard_time;
512 	__le16 send_timeout;
513 	u8 mdaa;
514 	u8 nfca_param[6];
515 	u8 nfcf_param[18];
516 	u8 mf_halted;
517 	u8 arae_flag;
518 	__le16 recv_timeout;
519 	u8 data[];
520 } __packed;
521 
522 struct port100_tg_comm_rf_res {
523 	u8 comm_type;
524 	u8 ar_status;
525 	u8 target_activated;
526 	__le32 status;
527 	u8 data[];
528 } __packed;
529 
530 /* The rule: value + checksum = 0 */
531 static inline u8 port100_checksum(u16 value)
532 {
533 	return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
534 }
535 
536 /* The rule: sum(data elements) + checksum = 0 */
537 static u8 port100_data_checksum(u8 *data, int datalen)
538 {
539 	u8 sum = 0;
540 	int i;
541 
542 	for (i = 0; i < datalen; i++)
543 		sum += data[i];
544 
545 	return port100_checksum(sum);
546 }
547 
548 static void port100_tx_frame_init(void *_frame, u8 cmd_code)
549 {
550 	struct port100_frame *frame = _frame;
551 
552 	frame->preamble = 0;
553 	frame->start_frame = cpu_to_be16(PORT100_FRAME_SOF);
554 	frame->extended_frame = cpu_to_be16(PORT100_FRAME_EXT);
555 	PORT100_FRAME_DIRECTION(frame) = PORT100_FRAME_DIR_OUT;
556 	PORT100_FRAME_CMD(frame) = cmd_code;
557 	frame->datalen = cpu_to_le16(2);
558 }
559 
560 static void port100_tx_frame_finish(void *_frame)
561 {
562 	struct port100_frame *frame = _frame;
563 
564 	frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
565 
566 	PORT100_FRAME_CHECKSUM(frame) =
567 		port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
568 
569 	PORT100_FRAME_POSTAMBLE(frame) = 0;
570 }
571 
572 static void port100_tx_update_payload_len(void *_frame, int len)
573 {
574 	struct port100_frame *frame = _frame;
575 
576 	frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
577 }
578 
579 static bool port100_rx_frame_is_valid(void *_frame)
580 {
581 	u8 checksum;
582 	struct port100_frame *frame = _frame;
583 
584 	if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
585 	    frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
586 		return false;
587 
588 	checksum = port100_checksum(le16_to_cpu(frame->datalen));
589 	if (checksum != frame->datalen_checksum)
590 		return false;
591 
592 	checksum = port100_data_checksum(frame->data,
593 					 le16_to_cpu(frame->datalen));
594 	if (checksum != PORT100_FRAME_CHECKSUM(frame))
595 		return false;
596 
597 	return true;
598 }
599 
600 static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
601 {
602 	return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
603 		frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
604 }
605 
606 static inline int port100_rx_frame_size(void *frame)
607 {
608 	struct port100_frame *f = frame;
609 
610 	return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
611 	       PORT100_FRAME_TAIL_LEN;
612 }
613 
614 static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
615 {
616 	struct port100_frame *f = frame;
617 
618 	return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
619 }
620 
621 static void port100_recv_response(struct urb *urb)
622 {
623 	struct port100 *dev = urb->context;
624 	struct port100_cmd *cmd = dev->cmd;
625 	u8 *in_frame;
626 
627 	cmd->status = urb->status;
628 
629 	switch (urb->status) {
630 	case 0:
631 		break; /* success */
632 	case -ECONNRESET:
633 	case -ENOENT:
634 		nfc_err(&dev->interface->dev,
635 			"The urb has been canceled (status %d)\n", urb->status);
636 		goto sched_wq;
637 	case -ESHUTDOWN:
638 	default:
639 		nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
640 			urb->status);
641 		goto sched_wq;
642 	}
643 
644 	in_frame = dev->in_urb->transfer_buffer;
645 
646 	if (!port100_rx_frame_is_valid(in_frame)) {
647 		nfc_err(&dev->interface->dev, "Received an invalid frame\n");
648 		cmd->status = -EIO;
649 		goto sched_wq;
650 	}
651 
652 	print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
653 			     port100_rx_frame_size(in_frame), false);
654 
655 	if (!port100_rx_frame_is_cmd_response(dev, in_frame)) {
656 		nfc_err(&dev->interface->dev,
657 			"It's not the response to the last command\n");
658 		cmd->status = -EIO;
659 		goto sched_wq;
660 	}
661 
662 sched_wq:
663 	schedule_work(&dev->cmd_complete_work);
664 }
665 
666 static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
667 {
668 	dev->in_urb->complete = port100_recv_response;
669 
670 	return usb_submit_urb(dev->in_urb, flags);
671 }
672 
673 static void port100_recv_ack(struct urb *urb)
674 {
675 	struct port100 *dev = urb->context;
676 	struct port100_cmd *cmd = dev->cmd;
677 	struct port100_ack_frame *in_frame;
678 	int rc;
679 
680 	cmd->status = urb->status;
681 
682 	switch (urb->status) {
683 	case 0:
684 		break; /* success */
685 	case -ECONNRESET:
686 	case -ENOENT:
687 		nfc_err(&dev->interface->dev,
688 			"The urb has been stopped (status %d)\n", urb->status);
689 		goto sched_wq;
690 	case -ESHUTDOWN:
691 	default:
692 		nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
693 			urb->status);
694 		goto sched_wq;
695 	}
696 
697 	in_frame = dev->in_urb->transfer_buffer;
698 
699 	if (!port100_rx_frame_is_ack(in_frame)) {
700 		nfc_err(&dev->interface->dev, "Received an invalid ack\n");
701 		cmd->status = -EIO;
702 		goto sched_wq;
703 	}
704 
705 	rc = port100_submit_urb_for_response(dev, GFP_ATOMIC);
706 	if (rc) {
707 		nfc_err(&dev->interface->dev,
708 			"usb_submit_urb failed with result %d\n", rc);
709 		cmd->status = rc;
710 		goto sched_wq;
711 	}
712 
713 	return;
714 
715 sched_wq:
716 	schedule_work(&dev->cmd_complete_work);
717 }
718 
719 static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
720 {
721 	dev->in_urb->complete = port100_recv_ack;
722 
723 	return usb_submit_urb(dev->in_urb, flags);
724 }
725 
726 static int port100_send_ack(struct port100 *dev)
727 {
728 	int rc;
729 
730 	mutex_lock(&dev->out_urb_lock);
731 
732 	init_completion(&dev->cmd_cancel_done);
733 
734 	usb_kill_urb(dev->out_urb);
735 
736 	dev->out_urb->transfer_buffer = ack_frame;
737 	dev->out_urb->transfer_buffer_length = sizeof(ack_frame);
738 	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
739 
740 	/* Set the cmd_cancel flag only if the URB has been successfully
741 	 * submitted. It will be reset by the out URB completion callback
742 	 * port100_send_complete().
743 	 */
744 	dev->cmd_cancel = !rc;
745 
746 	mutex_unlock(&dev->out_urb_lock);
747 
748 	if (!rc)
749 		wait_for_completion(&dev->cmd_cancel_done);
750 
751 	return rc;
752 }
753 
754 static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
755 				    struct sk_buff *in, int in_len)
756 {
757 	int rc;
758 
759 	mutex_lock(&dev->out_urb_lock);
760 
761 	/* A command cancel frame as been sent through dev->out_urb. Don't try
762 	 * to submit a new one.
763 	 */
764 	if (dev->cmd_cancel) {
765 		rc = -EAGAIN;
766 		goto exit;
767 	}
768 
769 	dev->out_urb->transfer_buffer = out->data;
770 	dev->out_urb->transfer_buffer_length = out->len;
771 
772 	dev->in_urb->transfer_buffer = in->data;
773 	dev->in_urb->transfer_buffer_length = in_len;
774 
775 	print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
776 			     out->data, out->len, false);
777 
778 	rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
779 	if (rc)
780 		goto exit;
781 
782 	rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
783 	if (rc)
784 		usb_unlink_urb(dev->out_urb);
785 
786 exit:
787 	mutex_unlock(&dev->out_urb_lock);
788 
789 	return rc;
790 }
791 
792 static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
793 				    struct sk_buff *skb)
794 {
795 	/* payload is already there, just update datalen */
796 	int payload_len = skb->len;
797 
798 	skb_push(skb, PORT100_FRAME_HEADER_LEN);
799 	skb_put(skb, PORT100_FRAME_TAIL_LEN);
800 
801 	port100_tx_frame_init(skb->data, cmd_code);
802 	port100_tx_update_payload_len(skb->data, payload_len);
803 	port100_tx_frame_finish(skb->data);
804 }
805 
806 static void port100_send_async_complete(struct port100 *dev)
807 {
808 	struct port100_cmd *cmd = dev->cmd;
809 	int status = cmd->status;
810 
811 	struct sk_buff *req = cmd->req;
812 	struct sk_buff *resp = cmd->resp;
813 
814 	dev_kfree_skb(req);
815 
816 	dev->cmd = NULL;
817 
818 	if (status < 0) {
819 		cmd->complete_cb(dev, cmd->complete_cb_context,
820 				 ERR_PTR(status));
821 		dev_kfree_skb(resp);
822 		goto done;
823 	}
824 
825 	skb_put(resp, port100_rx_frame_size(resp->data));
826 	skb_pull(resp, PORT100_FRAME_HEADER_LEN);
827 	skb_trim(resp, resp->len - PORT100_FRAME_TAIL_LEN);
828 
829 	cmd->complete_cb(dev, cmd->complete_cb_context, resp);
830 
831 done:
832 	kfree(cmd);
833 }
834 
835 static int port100_send_cmd_async(struct port100 *dev, u8 cmd_code,
836 				struct sk_buff *req,
837 				port100_send_async_complete_t complete_cb,
838 				void *complete_cb_context)
839 {
840 	struct port100_cmd *cmd;
841 	struct sk_buff *resp;
842 	int rc;
843 	int  resp_len = PORT100_FRAME_HEADER_LEN +
844 			PORT100_FRAME_MAX_PAYLOAD_LEN +
845 			PORT100_FRAME_TAIL_LEN;
846 
847 	if (dev->cmd) {
848 		nfc_err(&dev->interface->dev,
849 			"A command is still in process\n");
850 		return -EBUSY;
851 	}
852 
853 	resp = alloc_skb(resp_len, GFP_KERNEL);
854 	if (!resp)
855 		return -ENOMEM;
856 
857 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
858 	if (!cmd) {
859 		dev_kfree_skb(resp);
860 		return -ENOMEM;
861 	}
862 
863 	cmd->code = cmd_code;
864 	cmd->req = req;
865 	cmd->resp = resp;
866 	cmd->resp_len = resp_len;
867 	cmd->complete_cb = complete_cb;
868 	cmd->complete_cb_context = complete_cb_context;
869 
870 	port100_build_cmd_frame(dev, cmd_code, req);
871 
872 	dev->cmd = cmd;
873 
874 	rc = port100_send_frame_async(dev, req, resp, resp_len);
875 	if (rc) {
876 		kfree(cmd);
877 		dev_kfree_skb(resp);
878 		dev->cmd = NULL;
879 	}
880 
881 	return rc;
882 }
883 
884 struct port100_sync_cmd_response {
885 	struct sk_buff *resp;
886 	struct completion done;
887 };
888 
889 static void port100_wq_cmd_complete(struct work_struct *work)
890 {
891 	struct port100 *dev = container_of(work, struct port100,
892 					   cmd_complete_work);
893 
894 	port100_send_async_complete(dev);
895 }
896 
897 static void port100_send_sync_complete(struct port100 *dev, void *_arg,
898 				      struct sk_buff *resp)
899 {
900 	struct port100_sync_cmd_response *arg = _arg;
901 
902 	arg->resp = resp;
903 	complete(&arg->done);
904 }
905 
906 static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
907 					     struct sk_buff *req)
908 {
909 	int rc;
910 	struct port100_sync_cmd_response arg;
911 
912 	init_completion(&arg.done);
913 
914 	rc = port100_send_cmd_async(dev, cmd_code, req,
915 				    port100_send_sync_complete, &arg);
916 	if (rc) {
917 		dev_kfree_skb(req);
918 		return ERR_PTR(rc);
919 	}
920 
921 	wait_for_completion(&arg.done);
922 
923 	return arg.resp;
924 }
925 
926 static void port100_send_complete(struct urb *urb)
927 {
928 	struct port100 *dev = urb->context;
929 
930 	if (dev->cmd_cancel) {
931 		dev->cmd_cancel = false;
932 		complete(&dev->cmd_cancel_done);
933 	}
934 
935 	switch (urb->status) {
936 	case 0:
937 		break; /* success */
938 	case -ECONNRESET:
939 	case -ENOENT:
940 		nfc_err(&dev->interface->dev,
941 			"The urb has been stopped (status %d)\n", urb->status);
942 		break;
943 	case -ESHUTDOWN:
944 	default:
945 		nfc_err(&dev->interface->dev, "Urb failure (status %d)\n",
946 			urb->status);
947 	}
948 }
949 
950 static void port100_abort_cmd(struct nfc_digital_dev *ddev)
951 {
952 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
953 
954 	/* An ack will cancel the last issued command */
955 	port100_send_ack(dev);
956 
957 	/* cancel the urb request */
958 	usb_kill_urb(dev->in_urb);
959 }
960 
961 static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
962 {
963 	struct sk_buff *skb;
964 
965 	skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
966 			GFP_KERNEL);
967 	if (skb)
968 		skb_reserve(skb, dev->skb_headroom);
969 
970 	return skb;
971 }
972 
973 static int port100_set_command_type(struct port100 *dev, u8 command_type)
974 {
975 	struct sk_buff *skb;
976 	struct sk_buff *resp;
977 	int rc;
978 
979 	skb = port100_alloc_skb(dev, 1);
980 	if (!skb)
981 		return -ENOMEM;
982 
983 	*skb_put(skb, sizeof(u8)) = command_type;
984 
985 	resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
986 	if (IS_ERR(resp))
987 		return PTR_ERR(resp);
988 
989 	rc = resp->data[0];
990 
991 	dev_kfree_skb(resp);
992 
993 	return rc;
994 }
995 
996 static u64 port100_get_command_type_mask(struct port100 *dev)
997 {
998 	struct sk_buff *skb;
999 	struct sk_buff *resp;
1000 	u64 mask;
1001 
1002 	skb = port100_alloc_skb(dev, 0);
1003 	if (!skb)
1004 		return -ENOMEM;
1005 
1006 	resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
1007 	if (IS_ERR(resp))
1008 		return PTR_ERR(resp);
1009 
1010 	if (resp->len < 8)
1011 		mask = 0;
1012 	else
1013 		mask = be64_to_cpu(*(__be64 *)resp->data);
1014 
1015 	dev_kfree_skb(resp);
1016 
1017 	return mask;
1018 }
1019 
1020 static u16 port100_get_firmware_version(struct port100 *dev)
1021 {
1022 	struct sk_buff *skb;
1023 	struct sk_buff *resp;
1024 	u16 fw_ver;
1025 
1026 	skb = port100_alloc_skb(dev, 0);
1027 	if (!skb)
1028 		return 0;
1029 
1030 	resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
1031 				     skb);
1032 	if (IS_ERR(resp))
1033 		return 0;
1034 
1035 	fw_ver = le16_to_cpu(*(__le16 *)resp->data);
1036 
1037 	dev_kfree_skb(resp);
1038 
1039 	return fw_ver;
1040 }
1041 
1042 static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
1043 {
1044 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1045 	struct sk_buff *skb, *resp;
1046 
1047 	skb = port100_alloc_skb(dev, 1);
1048 	if (!skb)
1049 		return -ENOMEM;
1050 
1051 	*skb_put(skb, 1) = on ? 1 : 0;
1052 
1053 	/* Cancel the last command if the device is being switched off */
1054 	if (!on)
1055 		port100_abort_cmd(ddev);
1056 
1057 	resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
1058 
1059 	if (IS_ERR(resp))
1060 		return PTR_ERR(resp);
1061 
1062 	dev_kfree_skb(resp);
1063 
1064 	return 0;
1065 }
1066 
1067 static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1068 {
1069 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1070 	struct sk_buff *skb;
1071 	struct sk_buff *resp;
1072 	int rc;
1073 
1074 	if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1075 		return -EINVAL;
1076 
1077 	skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
1078 	if (!skb)
1079 		return -ENOMEM;
1080 
1081 	memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
1082 	       &in_rf_settings[rf],
1083 	       sizeof(struct port100_in_rf_setting));
1084 
1085 	resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1086 
1087 	if (IS_ERR(resp))
1088 		return PTR_ERR(resp);
1089 
1090 	rc = resp->data[0];
1091 
1092 	dev_kfree_skb(resp);
1093 
1094 	return rc;
1095 }
1096 
1097 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1098 {
1099 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1100 	struct port100_protocol *protocols;
1101 	struct sk_buff *skb;
1102 	struct sk_buff *resp;
1103 	int num_protocols;
1104 	size_t size;
1105 	int rc;
1106 
1107 	if (param >= NFC_DIGITAL_FRAMING_LAST)
1108 		return -EINVAL;
1109 
1110 	protocols = in_protocols[param];
1111 
1112 	num_protocols = 0;
1113 	while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1114 		num_protocols++;
1115 
1116 	if (!num_protocols)
1117 		return 0;
1118 
1119 	size = sizeof(struct port100_protocol) * num_protocols;
1120 
1121 	skb = port100_alloc_skb(dev, size);
1122 	if (!skb)
1123 		return -ENOMEM;
1124 
1125 	memcpy(skb_put(skb, size), protocols, size);
1126 
1127 	resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1128 
1129 	if (IS_ERR(resp))
1130 		return PTR_ERR(resp);
1131 
1132 	rc = resp->data[0];
1133 
1134 	dev_kfree_skb(resp);
1135 
1136 	return rc;
1137 }
1138 
1139 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1140 				   int param)
1141 {
1142 	if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1143 		return port100_in_set_rf(ddev, param);
1144 
1145 	if (type == NFC_DIGITAL_CONFIG_FRAMING)
1146 		return port100_in_set_framing(ddev, param);
1147 
1148 	return -EINVAL;
1149 }
1150 
1151 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1152 				       struct sk_buff *resp)
1153 {
1154 	struct port100_cb_arg *cb_arg = arg;
1155 	nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1156 	u32 status;
1157 	int rc;
1158 
1159 	if (IS_ERR(resp)) {
1160 		rc =  PTR_ERR(resp);
1161 		goto exit;
1162 	}
1163 
1164 	if (resp->len < 4) {
1165 		nfc_err(&dev->interface->dev,
1166 			"Invalid packet length received\n");
1167 		rc = -EIO;
1168 		goto error;
1169 	}
1170 
1171 	status = le32_to_cpu(*(__le32 *)resp->data);
1172 
1173 	skb_pull(resp, sizeof(u32));
1174 
1175 	if (status == PORT100_CMD_STATUS_TIMEOUT) {
1176 		rc = -ETIMEDOUT;
1177 		goto error;
1178 	}
1179 
1180 	if (status != PORT100_CMD_STATUS_OK) {
1181 		nfc_err(&dev->interface->dev,
1182 			"in_comm_rf failed with status 0x%08x\n", status);
1183 		rc = -EIO;
1184 		goto error;
1185 	}
1186 
1187 	/* Remove collision bits byte */
1188 	skb_pull(resp, 1);
1189 
1190 	goto exit;
1191 
1192 error:
1193 	kfree_skb(resp);
1194 	resp = ERR_PTR(rc);
1195 
1196 exit:
1197 	cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1198 
1199 	kfree(cb_arg);
1200 }
1201 
1202 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1203 			       struct sk_buff *skb, u16 _timeout,
1204 			       nfc_digital_cmd_complete_t cb, void *arg)
1205 {
1206 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1207 	struct port100_cb_arg *cb_arg;
1208 	__le16 timeout;
1209 
1210 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1211 	if (!cb_arg)
1212 		return -ENOMEM;
1213 
1214 	cb_arg->complete_cb = cb;
1215 	cb_arg->complete_arg = arg;
1216 
1217 	timeout = cpu_to_le16(_timeout * 10);
1218 
1219 	memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1220 
1221 	return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1222 				      port100_in_comm_rf_complete, cb_arg);
1223 }
1224 
1225 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1226 {
1227 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1228 	struct sk_buff *skb;
1229 	struct sk_buff *resp;
1230 	int rc;
1231 
1232 	if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1233 		return -EINVAL;
1234 
1235 	skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1236 	if (!skb)
1237 		return -ENOMEM;
1238 
1239 	memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1240 	       &tg_rf_settings[rf],
1241 	       sizeof(struct port100_tg_rf_setting));
1242 
1243 	resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1244 
1245 	if (IS_ERR(resp))
1246 		return PTR_ERR(resp);
1247 
1248 	rc = resp->data[0];
1249 
1250 	dev_kfree_skb(resp);
1251 
1252 	return rc;
1253 }
1254 
1255 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1256 {
1257 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1258 	struct port100_protocol *protocols;
1259 	struct sk_buff *skb;
1260 	struct sk_buff *resp;
1261 	int rc;
1262 	int num_protocols;
1263 	size_t size;
1264 
1265 	if (param >= NFC_DIGITAL_FRAMING_LAST)
1266 		return -EINVAL;
1267 
1268 	protocols = tg_protocols[param];
1269 
1270 	num_protocols = 0;
1271 	while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1272 		num_protocols++;
1273 
1274 	if (!num_protocols)
1275 		return 0;
1276 
1277 	size = sizeof(struct port100_protocol) * num_protocols;
1278 
1279 	skb = port100_alloc_skb(dev, size);
1280 	if (!skb)
1281 		return -ENOMEM;
1282 
1283 	memcpy(skb_put(skb, size), protocols, size);
1284 
1285 	resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1286 
1287 	if (IS_ERR(resp))
1288 		return PTR_ERR(resp);
1289 
1290 	rc = resp->data[0];
1291 
1292 	dev_kfree_skb(resp);
1293 
1294 	return rc;
1295 }
1296 
1297 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1298 				   int param)
1299 {
1300 	if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1301 		return port100_tg_set_rf(ddev, param);
1302 
1303 	if (type == NFC_DIGITAL_CONFIG_FRAMING)
1304 		return port100_tg_set_framing(ddev, param);
1305 
1306 	return -EINVAL;
1307 }
1308 
1309 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1310 {
1311 	u8 mask;
1312 
1313 	switch (dev->cmd_type) {
1314 	case PORT100_CMD_TYPE_0:
1315 		mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1316 		break;
1317 	case PORT100_CMD_TYPE_1:
1318 		mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1319 		       PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1320 		break;
1321 	default:
1322 		nfc_err(&dev->interface->dev, "Unknown command type\n");
1323 		return false;
1324 	}
1325 
1326 	return ((tgt_activated & mask) == mask);
1327 }
1328 
1329 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1330 					struct sk_buff *resp)
1331 {
1332 	u32 status;
1333 	struct port100_cb_arg *cb_arg = arg;
1334 	nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1335 	struct port100_tg_comm_rf_res *hdr;
1336 
1337 	if (IS_ERR(resp))
1338 		goto exit;
1339 
1340 	hdr = (struct port100_tg_comm_rf_res *)resp->data;
1341 
1342 	status = le32_to_cpu(hdr->status);
1343 
1344 	if (cb_arg->mdaa &&
1345 	    !port100_tg_target_activated(dev, hdr->target_activated)) {
1346 		kfree_skb(resp);
1347 		resp = ERR_PTR(-ETIMEDOUT);
1348 
1349 		goto exit;
1350 	}
1351 
1352 	skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1353 
1354 	if (status != PORT100_CMD_STATUS_OK) {
1355 		kfree_skb(resp);
1356 
1357 		if (status == PORT100_CMD_STATUS_TIMEOUT)
1358 			resp = ERR_PTR(-ETIMEDOUT);
1359 		else
1360 			resp = ERR_PTR(-EIO);
1361 	}
1362 
1363 exit:
1364 	cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1365 
1366 	kfree(cb_arg);
1367 }
1368 
1369 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1370 			       struct sk_buff *skb, u16 timeout,
1371 			       nfc_digital_cmd_complete_t cb, void *arg)
1372 {
1373 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1374 	struct port100_tg_comm_rf_cmd *hdr;
1375 	struct port100_cb_arg *cb_arg;
1376 
1377 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1378 	if (!cb_arg)
1379 		return -ENOMEM;
1380 
1381 	cb_arg->complete_cb = cb;
1382 	cb_arg->complete_arg = arg;
1383 
1384 	skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1385 
1386 	hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1387 
1388 	memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1389 	hdr->guard_time = cpu_to_le16(500);
1390 	hdr->send_timeout = cpu_to_le16(0xFFFF);
1391 	hdr->recv_timeout = cpu_to_le16(timeout);
1392 
1393 	return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1394 				      port100_tg_comm_rf_complete, cb_arg);
1395 }
1396 
1397 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1398 			       struct digital_tg_mdaa_params *params,
1399 			       u16 timeout,
1400 			       nfc_digital_cmd_complete_t cb, void *arg)
1401 {
1402 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1403 	struct port100_tg_comm_rf_cmd *hdr;
1404 	struct port100_cb_arg *cb_arg;
1405 	struct sk_buff *skb;
1406 	int rc;
1407 
1408 	rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1409 				     NFC_DIGITAL_RF_TECH_106A);
1410 	if (rc)
1411 		return rc;
1412 
1413 	rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1414 				     NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1415 	if (rc)
1416 		return rc;
1417 
1418 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1419 	if (!cb_arg)
1420 		return -ENOMEM;
1421 
1422 	cb_arg->complete_cb = cb;
1423 	cb_arg->complete_arg = arg;
1424 	cb_arg->mdaa = 1;
1425 
1426 	skb = port100_alloc_skb(dev, 0);
1427 	if (!skb) {
1428 		kfree(cb_arg);
1429 		return -ENOMEM;
1430 	}
1431 
1432 	skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1433 	hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1434 
1435 	memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1436 
1437 	hdr->guard_time = 0;
1438 	hdr->send_timeout = cpu_to_le16(0xFFFF);
1439 	hdr->mdaa = 1;
1440 	hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1441 	hdr->nfca_param[1] = params->sens_res & 0xFF;
1442 	memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1443 	hdr->nfca_param[5] = params->sel_res;
1444 	memcpy(hdr->nfcf_param, params->nfcid2, 8);
1445 	hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1446 	hdr->nfcf_param[17] = params->sc & 0xFF;
1447 	hdr->recv_timeout = cpu_to_le16(timeout);
1448 
1449 	return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1450 				      port100_tg_comm_rf_complete, cb_arg);
1451 }
1452 
1453 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1454 			  nfc_digital_cmd_complete_t cb, void *arg)
1455 {
1456 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1457 	struct sk_buff *skb;
1458 
1459 	skb = port100_alloc_skb(dev, 0);
1460 	if (!skb)
1461 		return -ENOMEM;
1462 
1463 	return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1464 }
1465 
1466 static struct nfc_digital_ops port100_digital_ops = {
1467 	.in_configure_hw = port100_in_configure_hw,
1468 	.in_send_cmd = port100_in_send_cmd,
1469 
1470 	.tg_listen_mdaa = port100_listen_mdaa,
1471 	.tg_listen = port100_listen,
1472 	.tg_configure_hw = port100_tg_configure_hw,
1473 	.tg_send_cmd = port100_tg_send_cmd,
1474 
1475 	.switch_rf = port100_switch_rf,
1476 	.abort_cmd = port100_abort_cmd,
1477 };
1478 
1479 static const struct usb_device_id port100_table[] = {
1480 	{ USB_DEVICE(SONY_VENDOR_ID, RCS380_PRODUCT_ID), },
1481 	{ }
1482 };
1483 MODULE_DEVICE_TABLE(usb, port100_table);
1484 
1485 static int port100_probe(struct usb_interface *interface,
1486 			 const struct usb_device_id *id)
1487 {
1488 	struct port100 *dev;
1489 	int rc;
1490 	struct usb_host_interface *iface_desc;
1491 	struct usb_endpoint_descriptor *endpoint;
1492 	int in_endpoint;
1493 	int out_endpoint;
1494 	u16 fw_version;
1495 	u64 cmd_type_mask;
1496 	int i;
1497 
1498 	dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1499 	if (!dev)
1500 		return -ENOMEM;
1501 
1502 	mutex_init(&dev->out_urb_lock);
1503 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
1504 	dev->interface = interface;
1505 	usb_set_intfdata(interface, dev);
1506 
1507 	in_endpoint = out_endpoint = 0;
1508 	iface_desc = interface->cur_altsetting;
1509 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1510 		endpoint = &iface_desc->endpoint[i].desc;
1511 
1512 		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1513 			in_endpoint = endpoint->bEndpointAddress;
1514 
1515 		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1516 			out_endpoint = endpoint->bEndpointAddress;
1517 	}
1518 
1519 	if (!in_endpoint || !out_endpoint) {
1520 		nfc_err(&interface->dev,
1521 			"Could not find bulk-in or bulk-out endpoint\n");
1522 		rc = -ENODEV;
1523 		goto error;
1524 	}
1525 
1526 	dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1527 	dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1528 
1529 	if (!dev->in_urb || !dev->out_urb) {
1530 		nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1531 		rc = -ENOMEM;
1532 		goto error;
1533 	}
1534 
1535 	usb_fill_bulk_urb(dev->in_urb, dev->udev,
1536 			  usb_rcvbulkpipe(dev->udev, in_endpoint),
1537 			  NULL, 0, NULL, dev);
1538 	usb_fill_bulk_urb(dev->out_urb, dev->udev,
1539 			  usb_sndbulkpipe(dev->udev, out_endpoint),
1540 			  NULL, 0, port100_send_complete, dev);
1541 
1542 	dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1543 			    PORT100_COMM_RF_HEAD_MAX_LEN;
1544 	dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1545 
1546 	INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1547 
1548 	/* The first thing to do with the Port-100 is to set the command type
1549 	 * to be used. If supported we use command type 1. 0 otherwise.
1550 	 */
1551 	cmd_type_mask = port100_get_command_type_mask(dev);
1552 	if (!cmd_type_mask) {
1553 		nfc_err(&interface->dev,
1554 			"Could not get supported command types\n");
1555 		rc = -ENODEV;
1556 		goto error;
1557 	}
1558 
1559 	if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1560 		dev->cmd_type = PORT100_CMD_TYPE_1;
1561 	else
1562 		dev->cmd_type = PORT100_CMD_TYPE_0;
1563 
1564 	rc = port100_set_command_type(dev, dev->cmd_type);
1565 	if (rc) {
1566 		nfc_err(&interface->dev,
1567 			"The device does not support command type %u\n",
1568 			dev->cmd_type);
1569 		goto error;
1570 	}
1571 
1572 	fw_version = port100_get_firmware_version(dev);
1573 	if (!fw_version)
1574 		nfc_err(&interface->dev,
1575 			"Could not get device firmware version\n");
1576 
1577 	nfc_info(&interface->dev,
1578 		 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1579 		 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1580 
1581 	dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1582 							   PORT100_PROTOCOLS,
1583 							   PORT100_CAPABILITIES,
1584 							   dev->skb_headroom,
1585 							   dev->skb_tailroom);
1586 	if (!dev->nfc_digital_dev) {
1587 		nfc_err(&interface->dev,
1588 			"Could not allocate nfc_digital_dev\n");
1589 		rc = -ENOMEM;
1590 		goto error;
1591 	}
1592 
1593 	nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1594 	nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1595 
1596 	rc = nfc_digital_register_device(dev->nfc_digital_dev);
1597 	if (rc) {
1598 		nfc_err(&interface->dev,
1599 			"Could not register digital device\n");
1600 		goto free_nfc_dev;
1601 	}
1602 
1603 	return 0;
1604 
1605 free_nfc_dev:
1606 	nfc_digital_free_device(dev->nfc_digital_dev);
1607 
1608 error:
1609 	usb_free_urb(dev->in_urb);
1610 	usb_free_urb(dev->out_urb);
1611 	usb_put_dev(dev->udev);
1612 
1613 	return rc;
1614 }
1615 
1616 static void port100_disconnect(struct usb_interface *interface)
1617 {
1618 	struct port100 *dev;
1619 
1620 	dev = usb_get_intfdata(interface);
1621 	usb_set_intfdata(interface, NULL);
1622 
1623 	nfc_digital_unregister_device(dev->nfc_digital_dev);
1624 	nfc_digital_free_device(dev->nfc_digital_dev);
1625 
1626 	usb_kill_urb(dev->in_urb);
1627 	usb_kill_urb(dev->out_urb);
1628 
1629 	usb_free_urb(dev->in_urb);
1630 	usb_free_urb(dev->out_urb);
1631 	usb_put_dev(dev->udev);
1632 
1633 	kfree(dev->cmd);
1634 
1635 	nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
1636 }
1637 
1638 static struct usb_driver port100_driver = {
1639 	.name =		"port100",
1640 	.probe =	port100_probe,
1641 	.disconnect =	port100_disconnect,
1642 	.id_table =	port100_table,
1643 };
1644 
1645 module_usb_driver(port100_driver);
1646 
1647 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1648 MODULE_VERSION(VERSION);
1649 MODULE_LICENSE("GPL");
1650