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