xref: /openbmc/linux/drivers/nfc/port100.c (revision 020c5260)
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(skb, sizeof(u8)) = 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(skb, 1) = 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 	memcpy(skb_put(skb, sizeof(struct port100_in_rf_setting)),
1093 	       &in_rf_settings[rf],
1094 	       sizeof(struct port100_in_rf_setting));
1095 
1096 	resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1097 
1098 	if (IS_ERR(resp))
1099 		return PTR_ERR(resp);
1100 
1101 	rc = resp->data[0];
1102 
1103 	dev_kfree_skb(resp);
1104 
1105 	return rc;
1106 }
1107 
1108 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1109 {
1110 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1111 	struct port100_protocol *protocols;
1112 	struct sk_buff *skb;
1113 	struct sk_buff *resp;
1114 	int num_protocols;
1115 	size_t size;
1116 	int rc;
1117 
1118 	if (param >= NFC_DIGITAL_FRAMING_LAST)
1119 		return -EINVAL;
1120 
1121 	protocols = in_protocols[param];
1122 
1123 	num_protocols = 0;
1124 	while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1125 		num_protocols++;
1126 
1127 	if (!num_protocols)
1128 		return 0;
1129 
1130 	size = sizeof(struct port100_protocol) * num_protocols;
1131 
1132 	skb = port100_alloc_skb(dev, size);
1133 	if (!skb)
1134 		return -ENOMEM;
1135 
1136 	memcpy(skb_put(skb, size), protocols, size);
1137 
1138 	resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1139 
1140 	if (IS_ERR(resp))
1141 		return PTR_ERR(resp);
1142 
1143 	rc = resp->data[0];
1144 
1145 	dev_kfree_skb(resp);
1146 
1147 	return rc;
1148 }
1149 
1150 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1151 				   int param)
1152 {
1153 	if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1154 		return port100_in_set_rf(ddev, param);
1155 
1156 	if (type == NFC_DIGITAL_CONFIG_FRAMING)
1157 		return port100_in_set_framing(ddev, param);
1158 
1159 	return -EINVAL;
1160 }
1161 
1162 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1163 				       struct sk_buff *resp)
1164 {
1165 	struct port100_cb_arg *cb_arg = arg;
1166 	nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1167 	u32 status;
1168 	int rc;
1169 
1170 	if (IS_ERR(resp)) {
1171 		rc =  PTR_ERR(resp);
1172 		goto exit;
1173 	}
1174 
1175 	if (resp->len < 4) {
1176 		nfc_err(&dev->interface->dev,
1177 			"Invalid packet length received\n");
1178 		rc = -EIO;
1179 		goto error;
1180 	}
1181 
1182 	status = le32_to_cpu(*(__le32 *)resp->data);
1183 
1184 	skb_pull(resp, sizeof(u32));
1185 
1186 	if (status == PORT100_CMD_STATUS_TIMEOUT) {
1187 		rc = -ETIMEDOUT;
1188 		goto error;
1189 	}
1190 
1191 	if (status != PORT100_CMD_STATUS_OK) {
1192 		nfc_err(&dev->interface->dev,
1193 			"in_comm_rf failed with status 0x%08x\n", status);
1194 		rc = -EIO;
1195 		goto error;
1196 	}
1197 
1198 	/* Remove collision bits byte */
1199 	skb_pull(resp, 1);
1200 
1201 	goto exit;
1202 
1203 error:
1204 	kfree_skb(resp);
1205 	resp = ERR_PTR(rc);
1206 
1207 exit:
1208 	cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1209 
1210 	kfree(cb_arg);
1211 }
1212 
1213 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1214 			       struct sk_buff *skb, u16 _timeout,
1215 			       nfc_digital_cmd_complete_t cb, void *arg)
1216 {
1217 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1218 	struct port100_cb_arg *cb_arg;
1219 	__le16 timeout;
1220 
1221 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1222 	if (!cb_arg)
1223 		return -ENOMEM;
1224 
1225 	cb_arg->complete_cb = cb;
1226 	cb_arg->complete_arg = arg;
1227 
1228 	timeout = cpu_to_le16(_timeout * 10);
1229 
1230 	memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1231 
1232 	return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1233 				      port100_in_comm_rf_complete, cb_arg);
1234 }
1235 
1236 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1237 {
1238 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1239 	struct sk_buff *skb;
1240 	struct sk_buff *resp;
1241 	int rc;
1242 
1243 	if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1244 		return -EINVAL;
1245 
1246 	skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1247 	if (!skb)
1248 		return -ENOMEM;
1249 
1250 	memcpy(skb_put(skb, sizeof(struct port100_tg_rf_setting)),
1251 	       &tg_rf_settings[rf],
1252 	       sizeof(struct port100_tg_rf_setting));
1253 
1254 	resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1255 
1256 	if (IS_ERR(resp))
1257 		return PTR_ERR(resp);
1258 
1259 	rc = resp->data[0];
1260 
1261 	dev_kfree_skb(resp);
1262 
1263 	return rc;
1264 }
1265 
1266 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1267 {
1268 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1269 	struct port100_protocol *protocols;
1270 	struct sk_buff *skb;
1271 	struct sk_buff *resp;
1272 	int rc;
1273 	int num_protocols;
1274 	size_t size;
1275 
1276 	if (param >= NFC_DIGITAL_FRAMING_LAST)
1277 		return -EINVAL;
1278 
1279 	protocols = tg_protocols[param];
1280 
1281 	num_protocols = 0;
1282 	while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1283 		num_protocols++;
1284 
1285 	if (!num_protocols)
1286 		return 0;
1287 
1288 	size = sizeof(struct port100_protocol) * num_protocols;
1289 
1290 	skb = port100_alloc_skb(dev, size);
1291 	if (!skb)
1292 		return -ENOMEM;
1293 
1294 	memcpy(skb_put(skb, size), protocols, size);
1295 
1296 	resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1297 
1298 	if (IS_ERR(resp))
1299 		return PTR_ERR(resp);
1300 
1301 	rc = resp->data[0];
1302 
1303 	dev_kfree_skb(resp);
1304 
1305 	return rc;
1306 }
1307 
1308 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1309 				   int param)
1310 {
1311 	if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1312 		return port100_tg_set_rf(ddev, param);
1313 
1314 	if (type == NFC_DIGITAL_CONFIG_FRAMING)
1315 		return port100_tg_set_framing(ddev, param);
1316 
1317 	return -EINVAL;
1318 }
1319 
1320 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1321 {
1322 	u8 mask;
1323 
1324 	switch (dev->cmd_type) {
1325 	case PORT100_CMD_TYPE_0:
1326 		mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1327 		break;
1328 	case PORT100_CMD_TYPE_1:
1329 		mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1330 		       PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1331 		break;
1332 	default:
1333 		nfc_err(&dev->interface->dev, "Unknown command type\n");
1334 		return false;
1335 	}
1336 
1337 	return ((tgt_activated & mask) == mask);
1338 }
1339 
1340 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1341 					struct sk_buff *resp)
1342 {
1343 	u32 status;
1344 	struct port100_cb_arg *cb_arg = arg;
1345 	nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1346 	struct port100_tg_comm_rf_res *hdr;
1347 
1348 	if (IS_ERR(resp))
1349 		goto exit;
1350 
1351 	hdr = (struct port100_tg_comm_rf_res *)resp->data;
1352 
1353 	status = le32_to_cpu(hdr->status);
1354 
1355 	if (cb_arg->mdaa &&
1356 	    !port100_tg_target_activated(dev, hdr->target_activated)) {
1357 		kfree_skb(resp);
1358 		resp = ERR_PTR(-ETIMEDOUT);
1359 
1360 		goto exit;
1361 	}
1362 
1363 	skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1364 
1365 	if (status != PORT100_CMD_STATUS_OK) {
1366 		kfree_skb(resp);
1367 
1368 		if (status == PORT100_CMD_STATUS_TIMEOUT)
1369 			resp = ERR_PTR(-ETIMEDOUT);
1370 		else
1371 			resp = ERR_PTR(-EIO);
1372 	}
1373 
1374 exit:
1375 	cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1376 
1377 	kfree(cb_arg);
1378 }
1379 
1380 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1381 			       struct sk_buff *skb, u16 timeout,
1382 			       nfc_digital_cmd_complete_t cb, void *arg)
1383 {
1384 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1385 	struct port100_tg_comm_rf_cmd *hdr;
1386 	struct port100_cb_arg *cb_arg;
1387 
1388 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1389 	if (!cb_arg)
1390 		return -ENOMEM;
1391 
1392 	cb_arg->complete_cb = cb;
1393 	cb_arg->complete_arg = arg;
1394 
1395 	skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1396 
1397 	hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1398 
1399 	memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1400 	hdr->guard_time = cpu_to_le16(500);
1401 	hdr->send_timeout = cpu_to_le16(0xFFFF);
1402 	hdr->recv_timeout = cpu_to_le16(timeout);
1403 
1404 	return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1405 				      port100_tg_comm_rf_complete, cb_arg);
1406 }
1407 
1408 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1409 			       struct digital_tg_mdaa_params *params,
1410 			       u16 timeout,
1411 			       nfc_digital_cmd_complete_t cb, void *arg)
1412 {
1413 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1414 	struct port100_tg_comm_rf_cmd *hdr;
1415 	struct port100_cb_arg *cb_arg;
1416 	struct sk_buff *skb;
1417 	int rc;
1418 
1419 	rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1420 				     NFC_DIGITAL_RF_TECH_106A);
1421 	if (rc)
1422 		return rc;
1423 
1424 	rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1425 				     NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1426 	if (rc)
1427 		return rc;
1428 
1429 	cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1430 	if (!cb_arg)
1431 		return -ENOMEM;
1432 
1433 	cb_arg->complete_cb = cb;
1434 	cb_arg->complete_arg = arg;
1435 	cb_arg->mdaa = 1;
1436 
1437 	skb = port100_alloc_skb(dev, 0);
1438 	if (!skb) {
1439 		kfree(cb_arg);
1440 		return -ENOMEM;
1441 	}
1442 
1443 	skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1444 	hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1445 
1446 	memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1447 
1448 	hdr->guard_time = 0;
1449 	hdr->send_timeout = cpu_to_le16(0xFFFF);
1450 	hdr->mdaa = 1;
1451 	hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1452 	hdr->nfca_param[1] = params->sens_res & 0xFF;
1453 	memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1454 	hdr->nfca_param[5] = params->sel_res;
1455 	memcpy(hdr->nfcf_param, params->nfcid2, 8);
1456 	hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1457 	hdr->nfcf_param[17] = params->sc & 0xFF;
1458 	hdr->recv_timeout = cpu_to_le16(timeout);
1459 
1460 	return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1461 				      port100_tg_comm_rf_complete, cb_arg);
1462 }
1463 
1464 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1465 			  nfc_digital_cmd_complete_t cb, void *arg)
1466 {
1467 	struct port100 *dev = nfc_digital_get_drvdata(ddev);
1468 	struct sk_buff *skb;
1469 
1470 	skb = port100_alloc_skb(dev, 0);
1471 	if (!skb)
1472 		return -ENOMEM;
1473 
1474 	return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1475 }
1476 
1477 static struct nfc_digital_ops port100_digital_ops = {
1478 	.in_configure_hw = port100_in_configure_hw,
1479 	.in_send_cmd = port100_in_send_cmd,
1480 
1481 	.tg_listen_mdaa = port100_listen_mdaa,
1482 	.tg_listen = port100_listen,
1483 	.tg_configure_hw = port100_tg_configure_hw,
1484 	.tg_send_cmd = port100_tg_send_cmd,
1485 
1486 	.switch_rf = port100_switch_rf,
1487 	.abort_cmd = port100_abort_cmd,
1488 };
1489 
1490 static const struct usb_device_id port100_table[] = {
1491 	{ USB_DEVICE(SONY_VENDOR_ID, RCS380S_PRODUCT_ID), },
1492 	{ USB_DEVICE(SONY_VENDOR_ID, RCS380P_PRODUCT_ID), },
1493 	{ }
1494 };
1495 MODULE_DEVICE_TABLE(usb, port100_table);
1496 
1497 static int port100_probe(struct usb_interface *interface,
1498 			 const struct usb_device_id *id)
1499 {
1500 	struct port100 *dev;
1501 	int rc;
1502 	struct usb_host_interface *iface_desc;
1503 	struct usb_endpoint_descriptor *endpoint;
1504 	int in_endpoint;
1505 	int out_endpoint;
1506 	u16 fw_version;
1507 	u64 cmd_type_mask;
1508 	int i;
1509 
1510 	dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1511 	if (!dev)
1512 		return -ENOMEM;
1513 
1514 	mutex_init(&dev->out_urb_lock);
1515 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
1516 	dev->interface = interface;
1517 	usb_set_intfdata(interface, dev);
1518 
1519 	in_endpoint = out_endpoint = 0;
1520 	iface_desc = interface->cur_altsetting;
1521 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1522 		endpoint = &iface_desc->endpoint[i].desc;
1523 
1524 		if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1525 			in_endpoint = endpoint->bEndpointAddress;
1526 
1527 		if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1528 			out_endpoint = endpoint->bEndpointAddress;
1529 	}
1530 
1531 	if (!in_endpoint || !out_endpoint) {
1532 		nfc_err(&interface->dev,
1533 			"Could not find bulk-in or bulk-out endpoint\n");
1534 		rc = -ENODEV;
1535 		goto error;
1536 	}
1537 
1538 	dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1539 	dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1540 
1541 	if (!dev->in_urb || !dev->out_urb) {
1542 		nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1543 		rc = -ENOMEM;
1544 		goto error;
1545 	}
1546 
1547 	usb_fill_bulk_urb(dev->in_urb, dev->udev,
1548 			  usb_rcvbulkpipe(dev->udev, in_endpoint),
1549 			  NULL, 0, NULL, dev);
1550 	usb_fill_bulk_urb(dev->out_urb, dev->udev,
1551 			  usb_sndbulkpipe(dev->udev, out_endpoint),
1552 			  NULL, 0, port100_send_complete, dev);
1553 	dev->out_urb->transfer_flags = URB_ZERO_PACKET;
1554 
1555 	dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1556 			    PORT100_COMM_RF_HEAD_MAX_LEN;
1557 	dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1558 
1559 	init_completion(&dev->cmd_cancel_done);
1560 	INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1561 
1562 	/* The first thing to do with the Port-100 is to set the command type
1563 	 * to be used. If supported we use command type 1. 0 otherwise.
1564 	 */
1565 	cmd_type_mask = port100_get_command_type_mask(dev);
1566 	if (!cmd_type_mask) {
1567 		nfc_err(&interface->dev,
1568 			"Could not get supported command types\n");
1569 		rc = -ENODEV;
1570 		goto error;
1571 	}
1572 
1573 	if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1574 		dev->cmd_type = PORT100_CMD_TYPE_1;
1575 	else
1576 		dev->cmd_type = PORT100_CMD_TYPE_0;
1577 
1578 	rc = port100_set_command_type(dev, dev->cmd_type);
1579 	if (rc) {
1580 		nfc_err(&interface->dev,
1581 			"The device does not support command type %u\n",
1582 			dev->cmd_type);
1583 		goto error;
1584 	}
1585 
1586 	fw_version = port100_get_firmware_version(dev);
1587 	if (!fw_version)
1588 		nfc_err(&interface->dev,
1589 			"Could not get device firmware version\n");
1590 
1591 	nfc_info(&interface->dev,
1592 		 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1593 		 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1594 
1595 	dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1596 							   PORT100_PROTOCOLS,
1597 							   PORT100_CAPABILITIES,
1598 							   dev->skb_headroom,
1599 							   dev->skb_tailroom);
1600 	if (!dev->nfc_digital_dev) {
1601 		nfc_err(&interface->dev,
1602 			"Could not allocate nfc_digital_dev\n");
1603 		rc = -ENOMEM;
1604 		goto error;
1605 	}
1606 
1607 	nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1608 	nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1609 
1610 	rc = nfc_digital_register_device(dev->nfc_digital_dev);
1611 	if (rc) {
1612 		nfc_err(&interface->dev,
1613 			"Could not register digital device\n");
1614 		goto free_nfc_dev;
1615 	}
1616 
1617 	return 0;
1618 
1619 free_nfc_dev:
1620 	nfc_digital_free_device(dev->nfc_digital_dev);
1621 
1622 error:
1623 	usb_free_urb(dev->in_urb);
1624 	usb_free_urb(dev->out_urb);
1625 	usb_put_dev(dev->udev);
1626 
1627 	return rc;
1628 }
1629 
1630 static void port100_disconnect(struct usb_interface *interface)
1631 {
1632 	struct port100 *dev;
1633 
1634 	dev = usb_get_intfdata(interface);
1635 	usb_set_intfdata(interface, NULL);
1636 
1637 	nfc_digital_unregister_device(dev->nfc_digital_dev);
1638 	nfc_digital_free_device(dev->nfc_digital_dev);
1639 
1640 	usb_kill_urb(dev->in_urb);
1641 	usb_kill_urb(dev->out_urb);
1642 
1643 	usb_free_urb(dev->in_urb);
1644 	usb_free_urb(dev->out_urb);
1645 	usb_put_dev(dev->udev);
1646 
1647 	kfree(dev->cmd);
1648 
1649 	nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
1650 }
1651 
1652 static struct usb_driver port100_driver = {
1653 	.name =		"port100",
1654 	.probe =	port100_probe,
1655 	.disconnect =	port100_disconnect,
1656 	.id_table =	port100_table,
1657 };
1658 
1659 module_usb_driver(port100_driver);
1660 
1661 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1662 MODULE_VERSION(VERSION);
1663 MODULE_LICENSE("GPL");
1664