Linux 4.16.11
[linux/fpc-iii.git] / drivers / nfc / port100.c
blobbb43cebda9dcd39ec3aa3a5a729758f987354882
1 /*
2 * Sony NFC Port-100 Series driver
3 * Copyright (c) 2013, Intel Corporation.
5 * Partly based/Inspired by Stephen Tiedemann's nfcpy
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.
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.
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <net/nfc/digital.h>
22 #define VERSION "0.1"
24 #define SONY_VENDOR_ID 0x054c
25 #define RCS380S_PRODUCT_ID 0x06c1
26 #define RCS380P_PRODUCT_ID 0x06c3
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)
35 #define PORT100_CAPABILITIES (NFC_DIGITAL_DRV_CAPS_IN_CRC | \
36 NFC_DIGITAL_DRV_CAPS_TG_CRC)
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*/
43 #define PORT100_COMM_RF_HEAD_MAX_LEN (sizeof(struct port100_tg_comm_rf_cmd))
46 * Max extended frame payload len, excluding CC and SCC
47 * which are already in PORT100_FRAME_HEADER_LEN.
49 #define PORT100_FRAME_MAX_PAYLOAD_LEN 1001
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
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])
60 /* start of frame */
61 #define PORT100_FRAME_SOF 0x00FF
62 #define PORT100_FRAME_EXT 0xFFFF
63 #define PORT100_FRAME_ACK 0x00FF
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
70 /* Port-100 sub-command */
71 #define PORT100_FRAME_CMD(f) (f->data[1]) /* SCC */
73 #define PORT100_CMD_GET_FIRMWARE_VERSION 0x20
74 #define PORT100_CMD_GET_COMMAND_TYPE 0x28
75 #define PORT100_CMD_SET_COMMAND_TYPE 0x2A
77 #define PORT100_CMD_IN_SET_RF 0x00
78 #define PORT100_CMD_IN_SET_PROTOCOL 0x02
79 #define PORT100_CMD_IN_COMM_RF 0x04
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
86 #define PORT100_CMD_SWITCH_RF 0x06
88 #define PORT100_CMD_RESPONSE(cmd) (cmd + 1)
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
95 #define PORT100_CMD_STATUS_OK 0x00
96 #define PORT100_CMD_STATUS_TIMEOUT 0x80
98 #define PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK 0x01
99 #define PORT100_MDAA_TGT_WAS_ACTIVATED_MASK 0x02
101 struct port100;
103 typedef void (*port100_send_async_complete_t)(struct port100 *dev, void *arg,
104 struct sk_buff *resp);
107 * Setting sets structure for in_set_rf command
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.
113 * @in_*_comm_type: Theses fields set the communication type to be used.
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;
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
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,
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,
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,
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,
152 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
153 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
157 * Setting sets structure for tg_set_rf command
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.
164 * @tg_comm_type: Sets the communication type to be used to send and receive
165 * data.
167 struct port100_tg_rf_setting {
168 u8 tg_set_number;
169 u8 tg_comm_type;
170 } __packed;
172 #define PORT100_COMM_TYPE_TG_106A 0x0B
173 #define PORT100_COMM_TYPE_TG_212F 0x0C
174 #define PORT100_COMM_TYPE_TG_424F 0x0D
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,
181 [NFC_DIGITAL_RF_TECH_212F] = {
182 .tg_set_number = 8,
183 .tg_comm_type = PORT100_COMM_TYPE_TG_212F,
185 [NFC_DIGITAL_RF_TECH_424F] = {
186 .tg_set_number = 8,
187 .tg_comm_type = PORT100_COMM_TYPE_TG_424F,
189 /* Ensures the array has NFC_DIGITAL_RF_TECH_LAST elements */
190 [NFC_DIGITAL_RF_TECH_LAST] = { 0 },
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
215 #define PORT100_IN_MAX_NUM_PROTOCOLS 19
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
222 #define PORT100_TG_MAX_NUM_PROTOCOLS 3
224 struct port100_protocol {
225 u8 number;
226 u8 value;
227 } __packed;
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 },
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 },
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 },
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 },
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 },
311 [NFC_DIGITAL_FRAMING_NFCA_T4T] = {
312 /* nfc_digital_framing_nfca_standard_with_crc_a */
313 { PORT100_IN_PROT_END, 0 },
315 [NFC_DIGITAL_FRAMING_NFCA_NFC_DEP] = {
316 /* nfc_digital_framing_nfca_standard */
317 { PORT100_IN_PROT_END, 0 },
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 },
341 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
342 /* nfc_digital_framing_nfcf */
343 { PORT100_IN_PROT_END, 0 },
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 },
368 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
369 { PORT100_IN_PROT_END, 0 },
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 },
393 [NFC_DIGITAL_FRAMING_NFCB_T4T] = {
394 /* nfc_digital_framing_nfcb */
395 { PORT100_IN_PROT_END, 0 },
397 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
398 [NFC_DIGITAL_FRAMING_LAST] = {
399 { PORT100_IN_PROT_END, 0 },
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 },
408 [NFC_DIGITAL_FRAMING_NFCA_STANDARD] = {
409 { PORT100_TG_PROT_END, 0 },
411 [NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A] = {
412 { PORT100_TG_PROT_END, 0 },
414 [NFC_DIGITAL_FRAMING_NFCA_T1T] = {
415 { PORT100_TG_PROT_END, 0 },
417 [NFC_DIGITAL_FRAMING_NFCA_T2T] = {
418 { PORT100_TG_PROT_END, 0 },
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 },
426 [NFC_DIGITAL_FRAMING_NFCF] = {
427 { PORT100_TG_PROT_END, 0 },
429 [NFC_DIGITAL_FRAMING_NFCF_T3T] = {
430 { PORT100_TG_PROT_END, 0 },
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 },
438 [NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED] = {
439 { PORT100_TG_PROT_RF_OFF, 1 },
440 { PORT100_TG_PROT_END, 0 },
442 /* Ensures the array has NFC_DIGITAL_FRAMING_LAST elements */
443 [NFC_DIGITAL_FRAMING_LAST] = {
444 { PORT100_TG_PROT_END, 0 },
448 struct port100 {
449 struct nfc_digital_dev *nfc_digital_dev;
451 int skb_headroom;
452 int skb_tailroom;
454 struct usb_device *udev;
455 struct usb_interface *interface;
457 struct urb *out_urb;
458 struct urb *in_urb;
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().
464 struct mutex out_urb_lock;
466 struct work_struct cmd_complete_work;
468 u8 cmd_type;
470 /* The digital stack serializes commands to be sent. There is no need
471 * for any queuing/locking mechanism at driver level.
473 struct port100_cmd *cmd;
475 bool cmd_cancel;
476 struct completion cmd_cancel_done;
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;
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;
498 struct port100_ack_frame {
499 u8 preamble;
500 __be16 start_frame;
501 __be16 ack_frame;
502 u8 postambule;
503 } __packed;
505 struct port100_cb_arg {
506 nfc_digital_cmd_complete_t complete_cb;
507 void *complete_arg;
508 u8 mdaa;
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;
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;
531 /* The rule: value + checksum = 0 */
532 static inline u8 port100_checksum(u16 value)
534 return ~(((u8 *)&value)[0] + ((u8 *)&value)[1]) + 1;
537 /* The rule: sum(data elements) + checksum = 0 */
538 static u8 port100_data_checksum(u8 *data, int datalen)
540 u8 sum = 0;
541 int i;
543 for (i = 0; i < datalen; i++)
544 sum += data[i];
546 return port100_checksum(sum);
549 static void port100_tx_frame_init(void *_frame, u8 cmd_code)
551 struct port100_frame *frame = _frame;
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);
561 static void port100_tx_frame_finish(void *_frame)
563 struct port100_frame *frame = _frame;
565 frame->datalen_checksum = port100_checksum(le16_to_cpu(frame->datalen));
567 PORT100_FRAME_CHECKSUM(frame) =
568 port100_data_checksum(frame->data, le16_to_cpu(frame->datalen));
570 PORT100_FRAME_POSTAMBLE(frame) = 0;
573 static void port100_tx_update_payload_len(void *_frame, int len)
575 struct port100_frame *frame = _frame;
577 frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
580 static bool port100_rx_frame_is_valid(void *_frame)
582 u8 checksum;
583 struct port100_frame *frame = _frame;
585 if (frame->start_frame != cpu_to_be16(PORT100_FRAME_SOF) ||
586 frame->extended_frame != cpu_to_be16(PORT100_FRAME_EXT))
587 return false;
589 checksum = port100_checksum(le16_to_cpu(frame->datalen));
590 if (checksum != frame->datalen_checksum)
591 return false;
593 checksum = port100_data_checksum(frame->data,
594 le16_to_cpu(frame->datalen));
595 if (checksum != PORT100_FRAME_CHECKSUM(frame))
596 return false;
598 return true;
601 static bool port100_rx_frame_is_ack(struct port100_ack_frame *frame)
603 return (frame->start_frame == cpu_to_be16(PORT100_FRAME_SOF) &&
604 frame->ack_frame == cpu_to_be16(PORT100_FRAME_ACK));
607 static inline int port100_rx_frame_size(void *frame)
609 struct port100_frame *f = frame;
611 return sizeof(struct port100_frame) + le16_to_cpu(f->datalen) +
612 PORT100_FRAME_TAIL_LEN;
615 static bool port100_rx_frame_is_cmd_response(struct port100 *dev, void *frame)
617 struct port100_frame *f = frame;
619 return (PORT100_FRAME_CMD(f) == PORT100_CMD_RESPONSE(dev->cmd->code));
622 static void port100_recv_response(struct urb *urb)
624 struct port100 *dev = urb->context;
625 struct port100_cmd *cmd = dev->cmd;
626 u8 *in_frame;
628 cmd->status = urb->status;
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;
645 in_frame = dev->in_urb->transfer_buffer;
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;
653 print_hex_dump_debug("PORT100 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
654 port100_rx_frame_size(in_frame), false);
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;
663 sched_wq:
664 schedule_work(&dev->cmd_complete_work);
667 static int port100_submit_urb_for_response(struct port100 *dev, gfp_t flags)
669 dev->in_urb->complete = port100_recv_response;
671 return usb_submit_urb(dev->in_urb, flags);
674 static void port100_recv_ack(struct urb *urb)
676 struct port100 *dev = urb->context;
677 struct port100_cmd *cmd = dev->cmd;
678 struct port100_ack_frame *in_frame;
679 int rc;
681 cmd->status = urb->status;
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;
698 in_frame = dev->in_urb->transfer_buffer;
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;
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;
714 return;
716 sched_wq:
717 schedule_work(&dev->cmd_complete_work);
720 static int port100_submit_urb_for_ack(struct port100 *dev, gfp_t flags)
722 dev->in_urb->complete = port100_recv_ack;
724 return usb_submit_urb(dev->in_urb, flags);
727 static int port100_send_ack(struct port100 *dev)
729 int rc = 0;
731 mutex_lock(&dev->out_urb_lock);
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.
740 if (!dev->cmd_cancel) {
741 reinit_completion(&dev->cmd_cancel_done);
743 usb_kill_urb(dev->out_urb);
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);
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().
754 dev->cmd_cancel = !rc;
757 mutex_unlock(&dev->out_urb_lock);
759 if (!rc)
760 wait_for_completion(&dev->cmd_cancel_done);
762 return rc;
765 static int port100_send_frame_async(struct port100 *dev, struct sk_buff *out,
766 struct sk_buff *in, int in_len)
768 int rc;
770 mutex_lock(&dev->out_urb_lock);
772 /* A command cancel frame as been sent through dev->out_urb. Don't try
773 * to submit a new one.
775 if (dev->cmd_cancel) {
776 rc = -EAGAIN;
777 goto exit;
780 dev->out_urb->transfer_buffer = out->data;
781 dev->out_urb->transfer_buffer_length = out->len;
783 dev->in_urb->transfer_buffer = in->data;
784 dev->in_urb->transfer_buffer_length = in_len;
786 print_hex_dump_debug("PORT100 TX: ", DUMP_PREFIX_NONE, 16, 1,
787 out->data, out->len, false);
789 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
790 if (rc)
791 goto exit;
793 rc = port100_submit_urb_for_ack(dev, GFP_KERNEL);
794 if (rc)
795 usb_unlink_urb(dev->out_urb);
797 exit:
798 mutex_unlock(&dev->out_urb_lock);
800 return rc;
803 static void port100_build_cmd_frame(struct port100 *dev, u8 cmd_code,
804 struct sk_buff *skb)
806 /* payload is already there, just update datalen */
807 int payload_len = skb->len;
809 skb_push(skb, PORT100_FRAME_HEADER_LEN);
810 skb_put(skb, PORT100_FRAME_TAIL_LEN);
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);
817 static void port100_send_async_complete(struct port100 *dev)
819 struct port100_cmd *cmd = dev->cmd;
820 int status = cmd->status;
822 struct sk_buff *req = cmd->req;
823 struct sk_buff *resp = cmd->resp;
825 dev_kfree_skb(req);
827 dev->cmd = NULL;
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;
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);
840 cmd->complete_cb(dev, cmd->complete_cb_context, resp);
842 done:
843 kfree(cmd);
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)
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;
858 if (dev->cmd) {
859 nfc_err(&dev->interface->dev,
860 "A command is still in process\n");
861 return -EBUSY;
864 resp = alloc_skb(resp_len, GFP_KERNEL);
865 if (!resp)
866 return -ENOMEM;
868 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
869 if (!cmd) {
870 dev_kfree_skb(resp);
871 return -ENOMEM;
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;
881 port100_build_cmd_frame(dev, cmd_code, req);
883 dev->cmd = cmd;
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;
892 return rc;
895 struct port100_sync_cmd_response {
896 struct sk_buff *resp;
897 struct completion done;
900 static void port100_wq_cmd_complete(struct work_struct *work)
902 struct port100 *dev = container_of(work, struct port100,
903 cmd_complete_work);
905 port100_send_async_complete(dev);
908 static void port100_send_sync_complete(struct port100 *dev, void *_arg,
909 struct sk_buff *resp)
911 struct port100_sync_cmd_response *arg = _arg;
913 arg->resp = resp;
914 complete(&arg->done);
917 static struct sk_buff *port100_send_cmd_sync(struct port100 *dev, u8 cmd_code,
918 struct sk_buff *req)
920 int rc;
921 struct port100_sync_cmd_response arg;
923 init_completion(&arg.done);
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);
932 wait_for_completion(&arg.done);
934 return arg.resp;
937 static void port100_send_complete(struct urb *urb)
939 struct port100 *dev = urb->context;
941 if (dev->cmd_cancel) {
942 complete_all(&dev->cmd_cancel_done);
943 dev->cmd_cancel = false;
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);
961 static void port100_abort_cmd(struct nfc_digital_dev *ddev)
963 struct port100 *dev = nfc_digital_get_drvdata(ddev);
965 /* An ack will cancel the last issued command */
966 port100_send_ack(dev);
968 /* cancel the urb request */
969 usb_kill_urb(dev->in_urb);
972 static struct sk_buff *port100_alloc_skb(struct port100 *dev, unsigned int size)
974 struct sk_buff *skb;
976 skb = alloc_skb(dev->skb_headroom + dev->skb_tailroom + size,
977 GFP_KERNEL);
978 if (skb)
979 skb_reserve(skb, dev->skb_headroom);
981 return skb;
984 static int port100_set_command_type(struct port100 *dev, u8 command_type)
986 struct sk_buff *skb;
987 struct sk_buff *resp;
988 int rc;
990 skb = port100_alloc_skb(dev, 1);
991 if (!skb)
992 return -ENOMEM;
994 skb_put_u8(skb, command_type);
996 resp = port100_send_cmd_sync(dev, PORT100_CMD_SET_COMMAND_TYPE, skb);
997 if (IS_ERR(resp))
998 return PTR_ERR(resp);
1000 rc = resp->data[0];
1002 dev_kfree_skb(resp);
1004 return rc;
1007 static u64 port100_get_command_type_mask(struct port100 *dev)
1009 struct sk_buff *skb;
1010 struct sk_buff *resp;
1011 u64 mask;
1013 skb = port100_alloc_skb(dev, 0);
1014 if (!skb)
1015 return -ENOMEM;
1017 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_COMMAND_TYPE, skb);
1018 if (IS_ERR(resp))
1019 return PTR_ERR(resp);
1021 if (resp->len < 8)
1022 mask = 0;
1023 else
1024 mask = be64_to_cpu(*(__be64 *)resp->data);
1026 dev_kfree_skb(resp);
1028 return mask;
1031 static u16 port100_get_firmware_version(struct port100 *dev)
1033 struct sk_buff *skb;
1034 struct sk_buff *resp;
1035 u16 fw_ver;
1037 skb = port100_alloc_skb(dev, 0);
1038 if (!skb)
1039 return 0;
1041 resp = port100_send_cmd_sync(dev, PORT100_CMD_GET_FIRMWARE_VERSION,
1042 skb);
1043 if (IS_ERR(resp))
1044 return 0;
1046 fw_ver = le16_to_cpu(*(__le16 *)resp->data);
1048 dev_kfree_skb(resp);
1050 return fw_ver;
1053 static int port100_switch_rf(struct nfc_digital_dev *ddev, bool on)
1055 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1056 struct sk_buff *skb, *resp;
1058 skb = port100_alloc_skb(dev, 1);
1059 if (!skb)
1060 return -ENOMEM;
1062 skb_put_u8(skb, on ? 1 : 0);
1064 /* Cancel the last command if the device is being switched off */
1065 if (!on)
1066 port100_abort_cmd(ddev);
1068 resp = port100_send_cmd_sync(dev, PORT100_CMD_SWITCH_RF, skb);
1070 if (IS_ERR(resp))
1071 return PTR_ERR(resp);
1073 dev_kfree_skb(resp);
1075 return 0;
1078 static int port100_in_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1080 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1081 struct sk_buff *skb;
1082 struct sk_buff *resp;
1083 int rc;
1085 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1086 return -EINVAL;
1088 skb = port100_alloc_skb(dev, sizeof(struct port100_in_rf_setting));
1089 if (!skb)
1090 return -ENOMEM;
1092 skb_put_data(skb, &in_rf_settings[rf],
1093 sizeof(struct port100_in_rf_setting));
1095 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_RF, skb);
1097 if (IS_ERR(resp))
1098 return PTR_ERR(resp);
1100 rc = resp->data[0];
1102 dev_kfree_skb(resp);
1104 return rc;
1107 static int port100_in_set_framing(struct nfc_digital_dev *ddev, int param)
1109 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1110 struct port100_protocol *protocols;
1111 struct sk_buff *skb;
1112 struct sk_buff *resp;
1113 int num_protocols;
1114 size_t size;
1115 int rc;
1117 if (param >= NFC_DIGITAL_FRAMING_LAST)
1118 return -EINVAL;
1120 protocols = in_protocols[param];
1122 num_protocols = 0;
1123 while (protocols[num_protocols].number != PORT100_IN_PROT_END)
1124 num_protocols++;
1126 if (!num_protocols)
1127 return 0;
1129 size = sizeof(struct port100_protocol) * num_protocols;
1131 skb = port100_alloc_skb(dev, size);
1132 if (!skb)
1133 return -ENOMEM;
1135 skb_put_data(skb, protocols, size);
1137 resp = port100_send_cmd_sync(dev, PORT100_CMD_IN_SET_PROTOCOL, skb);
1139 if (IS_ERR(resp))
1140 return PTR_ERR(resp);
1142 rc = resp->data[0];
1144 dev_kfree_skb(resp);
1146 return rc;
1149 static int port100_in_configure_hw(struct nfc_digital_dev *ddev, int type,
1150 int param)
1152 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1153 return port100_in_set_rf(ddev, param);
1155 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1156 return port100_in_set_framing(ddev, param);
1158 return -EINVAL;
1161 static void port100_in_comm_rf_complete(struct port100 *dev, void *arg,
1162 struct sk_buff *resp)
1164 struct port100_cb_arg *cb_arg = arg;
1165 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1166 u32 status;
1167 int rc;
1169 if (IS_ERR(resp)) {
1170 rc = PTR_ERR(resp);
1171 goto exit;
1174 if (resp->len < 4) {
1175 nfc_err(&dev->interface->dev,
1176 "Invalid packet length received\n");
1177 rc = -EIO;
1178 goto error;
1181 status = le32_to_cpu(*(__le32 *)resp->data);
1183 skb_pull(resp, sizeof(u32));
1185 if (status == PORT100_CMD_STATUS_TIMEOUT) {
1186 rc = -ETIMEDOUT;
1187 goto error;
1190 if (status != PORT100_CMD_STATUS_OK) {
1191 nfc_err(&dev->interface->dev,
1192 "in_comm_rf failed with status 0x%08x\n", status);
1193 rc = -EIO;
1194 goto error;
1197 /* Remove collision bits byte */
1198 skb_pull(resp, 1);
1200 goto exit;
1202 error:
1203 kfree_skb(resp);
1204 resp = ERR_PTR(rc);
1206 exit:
1207 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1209 kfree(cb_arg);
1212 static int port100_in_send_cmd(struct nfc_digital_dev *ddev,
1213 struct sk_buff *skb, u16 _timeout,
1214 nfc_digital_cmd_complete_t cb, void *arg)
1216 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1217 struct port100_cb_arg *cb_arg;
1218 __le16 timeout;
1220 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1221 if (!cb_arg)
1222 return -ENOMEM;
1224 cb_arg->complete_cb = cb;
1225 cb_arg->complete_arg = arg;
1227 timeout = cpu_to_le16(_timeout * 10);
1229 memcpy(skb_push(skb, sizeof(__le16)), &timeout, sizeof(__le16));
1231 return port100_send_cmd_async(dev, PORT100_CMD_IN_COMM_RF, skb,
1232 port100_in_comm_rf_complete, cb_arg);
1235 static int port100_tg_set_rf(struct nfc_digital_dev *ddev, u8 rf)
1237 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1238 struct sk_buff *skb;
1239 struct sk_buff *resp;
1240 int rc;
1242 if (rf >= NFC_DIGITAL_RF_TECH_LAST)
1243 return -EINVAL;
1245 skb = port100_alloc_skb(dev, sizeof(struct port100_tg_rf_setting));
1246 if (!skb)
1247 return -ENOMEM;
1249 skb_put_data(skb, &tg_rf_settings[rf],
1250 sizeof(struct port100_tg_rf_setting));
1252 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_RF, skb);
1254 if (IS_ERR(resp))
1255 return PTR_ERR(resp);
1257 rc = resp->data[0];
1259 dev_kfree_skb(resp);
1261 return rc;
1264 static int port100_tg_set_framing(struct nfc_digital_dev *ddev, int param)
1266 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1267 struct port100_protocol *protocols;
1268 struct sk_buff *skb;
1269 struct sk_buff *resp;
1270 int rc;
1271 int num_protocols;
1272 size_t size;
1274 if (param >= NFC_DIGITAL_FRAMING_LAST)
1275 return -EINVAL;
1277 protocols = tg_protocols[param];
1279 num_protocols = 0;
1280 while (protocols[num_protocols].number != PORT100_TG_PROT_END)
1281 num_protocols++;
1283 if (!num_protocols)
1284 return 0;
1286 size = sizeof(struct port100_protocol) * num_protocols;
1288 skb = port100_alloc_skb(dev, size);
1289 if (!skb)
1290 return -ENOMEM;
1292 skb_put_data(skb, protocols, size);
1294 resp = port100_send_cmd_sync(dev, PORT100_CMD_TG_SET_PROTOCOL, skb);
1296 if (IS_ERR(resp))
1297 return PTR_ERR(resp);
1299 rc = resp->data[0];
1301 dev_kfree_skb(resp);
1303 return rc;
1306 static int port100_tg_configure_hw(struct nfc_digital_dev *ddev, int type,
1307 int param)
1309 if (type == NFC_DIGITAL_CONFIG_RF_TECH)
1310 return port100_tg_set_rf(ddev, param);
1312 if (type == NFC_DIGITAL_CONFIG_FRAMING)
1313 return port100_tg_set_framing(ddev, param);
1315 return -EINVAL;
1318 static bool port100_tg_target_activated(struct port100 *dev, u8 tgt_activated)
1320 u8 mask;
1322 switch (dev->cmd_type) {
1323 case PORT100_CMD_TYPE_0:
1324 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK;
1325 break;
1326 case PORT100_CMD_TYPE_1:
1327 mask = PORT100_MDAA_TGT_HAS_BEEN_ACTIVATED_MASK |
1328 PORT100_MDAA_TGT_WAS_ACTIVATED_MASK;
1329 break;
1330 default:
1331 nfc_err(&dev->interface->dev, "Unknown command type\n");
1332 return false;
1335 return ((tgt_activated & mask) == mask);
1338 static void port100_tg_comm_rf_complete(struct port100 *dev, void *arg,
1339 struct sk_buff *resp)
1341 u32 status;
1342 struct port100_cb_arg *cb_arg = arg;
1343 nfc_digital_cmd_complete_t cb = cb_arg->complete_cb;
1344 struct port100_tg_comm_rf_res *hdr;
1346 if (IS_ERR(resp))
1347 goto exit;
1349 hdr = (struct port100_tg_comm_rf_res *)resp->data;
1351 status = le32_to_cpu(hdr->status);
1353 if (cb_arg->mdaa &&
1354 !port100_tg_target_activated(dev, hdr->target_activated)) {
1355 kfree_skb(resp);
1356 resp = ERR_PTR(-ETIMEDOUT);
1358 goto exit;
1361 skb_pull(resp, sizeof(struct port100_tg_comm_rf_res));
1363 if (status != PORT100_CMD_STATUS_OK) {
1364 kfree_skb(resp);
1366 if (status == PORT100_CMD_STATUS_TIMEOUT)
1367 resp = ERR_PTR(-ETIMEDOUT);
1368 else
1369 resp = ERR_PTR(-EIO);
1372 exit:
1373 cb(dev->nfc_digital_dev, cb_arg->complete_arg, resp);
1375 kfree(cb_arg);
1378 static int port100_tg_send_cmd(struct nfc_digital_dev *ddev,
1379 struct sk_buff *skb, u16 timeout,
1380 nfc_digital_cmd_complete_t cb, void *arg)
1382 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1383 struct port100_tg_comm_rf_cmd *hdr;
1384 struct port100_cb_arg *cb_arg;
1386 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1387 if (!cb_arg)
1388 return -ENOMEM;
1390 cb_arg->complete_cb = cb;
1391 cb_arg->complete_arg = arg;
1393 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1395 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1397 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1398 hdr->guard_time = cpu_to_le16(500);
1399 hdr->send_timeout = cpu_to_le16(0xFFFF);
1400 hdr->recv_timeout = cpu_to_le16(timeout);
1402 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1403 port100_tg_comm_rf_complete, cb_arg);
1406 static int port100_listen_mdaa(struct nfc_digital_dev *ddev,
1407 struct digital_tg_mdaa_params *params,
1408 u16 timeout,
1409 nfc_digital_cmd_complete_t cb, void *arg)
1411 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1412 struct port100_tg_comm_rf_cmd *hdr;
1413 struct port100_cb_arg *cb_arg;
1414 struct sk_buff *skb;
1415 int rc;
1417 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
1418 NFC_DIGITAL_RF_TECH_106A);
1419 if (rc)
1420 return rc;
1422 rc = port100_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
1423 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
1424 if (rc)
1425 return rc;
1427 cb_arg = kzalloc(sizeof(struct port100_cb_arg), GFP_KERNEL);
1428 if (!cb_arg)
1429 return -ENOMEM;
1431 cb_arg->complete_cb = cb;
1432 cb_arg->complete_arg = arg;
1433 cb_arg->mdaa = 1;
1435 skb = port100_alloc_skb(dev, 0);
1436 if (!skb) {
1437 kfree(cb_arg);
1438 return -ENOMEM;
1441 skb_push(skb, sizeof(struct port100_tg_comm_rf_cmd));
1442 hdr = (struct port100_tg_comm_rf_cmd *)skb->data;
1444 memset(hdr, 0, sizeof(struct port100_tg_comm_rf_cmd));
1446 hdr->guard_time = 0;
1447 hdr->send_timeout = cpu_to_le16(0xFFFF);
1448 hdr->mdaa = 1;
1449 hdr->nfca_param[0] = (params->sens_res >> 8) & 0xFF;
1450 hdr->nfca_param[1] = params->sens_res & 0xFF;
1451 memcpy(hdr->nfca_param + 2, params->nfcid1, 3);
1452 hdr->nfca_param[5] = params->sel_res;
1453 memcpy(hdr->nfcf_param, params->nfcid2, 8);
1454 hdr->nfcf_param[16] = (params->sc >> 8) & 0xFF;
1455 hdr->nfcf_param[17] = params->sc & 0xFF;
1456 hdr->recv_timeout = cpu_to_le16(timeout);
1458 return port100_send_cmd_async(dev, PORT100_CMD_TG_COMM_RF, skb,
1459 port100_tg_comm_rf_complete, cb_arg);
1462 static int port100_listen(struct nfc_digital_dev *ddev, u16 timeout,
1463 nfc_digital_cmd_complete_t cb, void *arg)
1465 struct port100 *dev = nfc_digital_get_drvdata(ddev);
1466 struct sk_buff *skb;
1468 skb = port100_alloc_skb(dev, 0);
1469 if (!skb)
1470 return -ENOMEM;
1472 return port100_tg_send_cmd(ddev, skb, timeout, cb, arg);
1475 static struct nfc_digital_ops port100_digital_ops = {
1476 .in_configure_hw = port100_in_configure_hw,
1477 .in_send_cmd = port100_in_send_cmd,
1479 .tg_listen_mdaa = port100_listen_mdaa,
1480 .tg_listen = port100_listen,
1481 .tg_configure_hw = port100_tg_configure_hw,
1482 .tg_send_cmd = port100_tg_send_cmd,
1484 .switch_rf = port100_switch_rf,
1485 .abort_cmd = port100_abort_cmd,
1488 static const struct usb_device_id port100_table[] = {
1489 { USB_DEVICE(SONY_VENDOR_ID, RCS380S_PRODUCT_ID), },
1490 { USB_DEVICE(SONY_VENDOR_ID, RCS380P_PRODUCT_ID), },
1493 MODULE_DEVICE_TABLE(usb, port100_table);
1495 static int port100_probe(struct usb_interface *interface,
1496 const struct usb_device_id *id)
1498 struct port100 *dev;
1499 int rc;
1500 struct usb_host_interface *iface_desc;
1501 struct usb_endpoint_descriptor *endpoint;
1502 int in_endpoint;
1503 int out_endpoint;
1504 u16 fw_version;
1505 u64 cmd_type_mask;
1506 int i;
1508 dev = devm_kzalloc(&interface->dev, sizeof(struct port100), GFP_KERNEL);
1509 if (!dev)
1510 return -ENOMEM;
1512 mutex_init(&dev->out_urb_lock);
1513 dev->udev = usb_get_dev(interface_to_usbdev(interface));
1514 dev->interface = interface;
1515 usb_set_intfdata(interface, dev);
1517 in_endpoint = out_endpoint = 0;
1518 iface_desc = interface->cur_altsetting;
1519 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
1520 endpoint = &iface_desc->endpoint[i].desc;
1522 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
1523 in_endpoint = endpoint->bEndpointAddress;
1525 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
1526 out_endpoint = endpoint->bEndpointAddress;
1529 if (!in_endpoint || !out_endpoint) {
1530 nfc_err(&interface->dev,
1531 "Could not find bulk-in or bulk-out endpoint\n");
1532 rc = -ENODEV;
1533 goto error;
1536 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
1537 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
1539 if (!dev->in_urb || !dev->out_urb) {
1540 nfc_err(&interface->dev, "Could not allocate USB URBs\n");
1541 rc = -ENOMEM;
1542 goto error;
1545 usb_fill_bulk_urb(dev->in_urb, dev->udev,
1546 usb_rcvbulkpipe(dev->udev, in_endpoint),
1547 NULL, 0, NULL, dev);
1548 usb_fill_bulk_urb(dev->out_urb, dev->udev,
1549 usb_sndbulkpipe(dev->udev, out_endpoint),
1550 NULL, 0, port100_send_complete, dev);
1551 dev->out_urb->transfer_flags = URB_ZERO_PACKET;
1553 dev->skb_headroom = PORT100_FRAME_HEADER_LEN +
1554 PORT100_COMM_RF_HEAD_MAX_LEN;
1555 dev->skb_tailroom = PORT100_FRAME_TAIL_LEN;
1557 init_completion(&dev->cmd_cancel_done);
1558 INIT_WORK(&dev->cmd_complete_work, port100_wq_cmd_complete);
1560 /* The first thing to do with the Port-100 is to set the command type
1561 * to be used. If supported we use command type 1. 0 otherwise.
1563 cmd_type_mask = port100_get_command_type_mask(dev);
1564 if (!cmd_type_mask) {
1565 nfc_err(&interface->dev,
1566 "Could not get supported command types\n");
1567 rc = -ENODEV;
1568 goto error;
1571 if (PORT100_CMD_TYPE_IS_SUPPORTED(cmd_type_mask, PORT100_CMD_TYPE_1))
1572 dev->cmd_type = PORT100_CMD_TYPE_1;
1573 else
1574 dev->cmd_type = PORT100_CMD_TYPE_0;
1576 rc = port100_set_command_type(dev, dev->cmd_type);
1577 if (rc) {
1578 nfc_err(&interface->dev,
1579 "The device does not support command type %u\n",
1580 dev->cmd_type);
1581 goto error;
1584 fw_version = port100_get_firmware_version(dev);
1585 if (!fw_version)
1586 nfc_err(&interface->dev,
1587 "Could not get device firmware version\n");
1589 nfc_info(&interface->dev,
1590 "Sony NFC Port-100 Series attached (firmware v%x.%02x)\n",
1591 (fw_version & 0xFF00) >> 8, fw_version & 0xFF);
1593 dev->nfc_digital_dev = nfc_digital_allocate_device(&port100_digital_ops,
1594 PORT100_PROTOCOLS,
1595 PORT100_CAPABILITIES,
1596 dev->skb_headroom,
1597 dev->skb_tailroom);
1598 if (!dev->nfc_digital_dev) {
1599 nfc_err(&interface->dev,
1600 "Could not allocate nfc_digital_dev\n");
1601 rc = -ENOMEM;
1602 goto error;
1605 nfc_digital_set_parent_dev(dev->nfc_digital_dev, &interface->dev);
1606 nfc_digital_set_drvdata(dev->nfc_digital_dev, dev);
1608 rc = nfc_digital_register_device(dev->nfc_digital_dev);
1609 if (rc) {
1610 nfc_err(&interface->dev,
1611 "Could not register digital device\n");
1612 goto free_nfc_dev;
1615 return 0;
1617 free_nfc_dev:
1618 nfc_digital_free_device(dev->nfc_digital_dev);
1620 error:
1621 usb_free_urb(dev->in_urb);
1622 usb_free_urb(dev->out_urb);
1623 usb_put_dev(dev->udev);
1625 return rc;
1628 static void port100_disconnect(struct usb_interface *interface)
1630 struct port100 *dev;
1632 dev = usb_get_intfdata(interface);
1633 usb_set_intfdata(interface, NULL);
1635 nfc_digital_unregister_device(dev->nfc_digital_dev);
1636 nfc_digital_free_device(dev->nfc_digital_dev);
1638 usb_kill_urb(dev->in_urb);
1639 usb_kill_urb(dev->out_urb);
1641 usb_free_urb(dev->in_urb);
1642 usb_free_urb(dev->out_urb);
1643 usb_put_dev(dev->udev);
1645 kfree(dev->cmd);
1647 nfc_info(&interface->dev, "Sony Port-100 NFC device disconnected\n");
1650 static struct usb_driver port100_driver = {
1651 .name = "port100",
1652 .probe = port100_probe,
1653 .disconnect = port100_disconnect,
1654 .id_table = port100_table,
1657 module_usb_driver(port100_driver);
1659 MODULE_DESCRIPTION("NFC Port-100 series usb driver ver " VERSION);
1660 MODULE_VERSION(VERSION);
1661 MODULE_LICENSE("GPL");