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