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