mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / nfc / pn533.c
blob30e50987b8dfd35bb0434b7ff634009295aad711
1 /*
2 * Copyright (C) 2011 Instituto Nokia de Tecnologia
3 * Copyright (C) 2012-2013 Tieto Poland
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/usb.h>
26 #include <linux/nfc.h>
27 #include <linux/netdevice.h>
28 #include <net/nfc/nfc.h>
30 #define VERSION "0.2"
32 #define PN533_VENDOR_ID 0x4CC
33 #define PN533_PRODUCT_ID 0x2533
35 #define SCM_VENDOR_ID 0x4E6
36 #define SCL3711_PRODUCT_ID 0x5591
38 #define SONY_VENDOR_ID 0x054c
39 #define PASORI_PRODUCT_ID 0x02e1
41 #define ACS_VENDOR_ID 0x072f
42 #define ACR122U_PRODUCT_ID 0x2200
44 #define PN533_DEVICE_STD 0x1
45 #define PN533_DEVICE_PASORI 0x2
46 #define PN533_DEVICE_ACR122U 0x3
48 #define PN533_ALL_PROTOCOLS (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK |\
49 NFC_PROTO_FELICA_MASK | NFC_PROTO_ISO14443_MASK |\
50 NFC_PROTO_NFC_DEP_MASK |\
51 NFC_PROTO_ISO14443_B_MASK)
53 #define PN533_NO_TYPE_B_PROTOCOLS (NFC_PROTO_JEWEL_MASK | \
54 NFC_PROTO_MIFARE_MASK | \
55 NFC_PROTO_FELICA_MASK | \
56 NFC_PROTO_ISO14443_MASK | \
57 NFC_PROTO_NFC_DEP_MASK)
59 static const struct usb_device_id pn533_table[] = {
60 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
61 .idVendor = PN533_VENDOR_ID,
62 .idProduct = PN533_PRODUCT_ID,
63 .driver_info = PN533_DEVICE_STD,
65 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
66 .idVendor = SCM_VENDOR_ID,
67 .idProduct = SCL3711_PRODUCT_ID,
68 .driver_info = PN533_DEVICE_STD,
70 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
71 .idVendor = SONY_VENDOR_ID,
72 .idProduct = PASORI_PRODUCT_ID,
73 .driver_info = PN533_DEVICE_PASORI,
75 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
76 .idVendor = ACS_VENDOR_ID,
77 .idProduct = ACR122U_PRODUCT_ID,
78 .driver_info = PN533_DEVICE_ACR122U,
80 { }
82 MODULE_DEVICE_TABLE(usb, pn533_table);
84 /* How much time we spend listening for initiators */
85 #define PN533_LISTEN_TIME 2
86 /* Delay between each poll frame (ms) */
87 #define PN533_POLL_INTERVAL 10
89 /* Standard pn533 frame definitions (standard and extended)*/
90 #define PN533_STD_FRAME_HEADER_LEN (sizeof(struct pn533_std_frame) \
91 + 2) /* data[0] TFI, data[1] CC */
92 #define PN533_STD_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
94 #define PN533_EXT_FRAME_HEADER_LEN (sizeof(struct pn533_ext_frame) \
95 + 2) /* data[0] TFI, data[1] CC */
97 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
98 #define PN533_CMD_DATAFRAME_MAXLEN 240 /* max data length (send) */
101 * Max extended frame payload len, excluding TFI and CC
102 * which are already in PN533_FRAME_HEADER_LEN.
104 #define PN533_STD_FRAME_MAX_PAYLOAD_LEN 263
106 #define PN533_STD_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
107 Postamble (1) */
108 #define PN533_STD_FRAME_CHECKSUM(f) (f->data[f->datalen])
109 #define PN533_STD_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
110 /* Half start code (3), LEN (4) should be 0xffff for extended frame */
111 #define PN533_STD_IS_EXTENDED(hdr) ((hdr)->datalen == 0xFF \
112 && (hdr)->datalen_checksum == 0xFF)
113 #define PN533_EXT_FRAME_CHECKSUM(f) (f->data[be16_to_cpu(f->datalen)])
115 /* start of frame */
116 #define PN533_STD_FRAME_SOF 0x00FF
118 /* standard frame identifier: in/out/error */
119 #define PN533_STD_FRAME_IDENTIFIER(f) (f->data[0]) /* TFI */
120 #define PN533_STD_FRAME_DIR_OUT 0xD4
121 #define PN533_STD_FRAME_DIR_IN 0xD5
123 /* ACS ACR122 pn533 frame definitions */
124 #define PN533_ACR122_TX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_tx_frame) \
125 + 2)
126 #define PN533_ACR122_TX_FRAME_TAIL_LEN 0
127 #define PN533_ACR122_RX_FRAME_HEADER_LEN (sizeof(struct pn533_acr122_rx_frame) \
128 + 2)
129 #define PN533_ACR122_RX_FRAME_TAIL_LEN 2
130 #define PN533_ACR122_FRAME_MAX_PAYLOAD_LEN PN533_STD_FRAME_MAX_PAYLOAD_LEN
132 /* CCID messages types */
133 #define PN533_ACR122_PC_TO_RDR_ICCPOWERON 0x62
134 #define PN533_ACR122_PC_TO_RDR_ESCAPE 0x6B
136 #define PN533_ACR122_RDR_TO_PC_ESCAPE 0x83
138 /* PN533 Commands */
139 #define PN533_FRAME_CMD(f) (f->data[1])
141 #define PN533_CMD_GET_FIRMWARE_VERSION 0x02
142 #define PN533_CMD_RF_CONFIGURATION 0x32
143 #define PN533_CMD_IN_DATA_EXCHANGE 0x40
144 #define PN533_CMD_IN_COMM_THRU 0x42
145 #define PN533_CMD_IN_LIST_PASSIVE_TARGET 0x4A
146 #define PN533_CMD_IN_ATR 0x50
147 #define PN533_CMD_IN_RELEASE 0x52
148 #define PN533_CMD_IN_JUMP_FOR_DEP 0x56
150 #define PN533_CMD_TG_INIT_AS_TARGET 0x8c
151 #define PN533_CMD_TG_GET_DATA 0x86
152 #define PN533_CMD_TG_SET_DATA 0x8e
153 #define PN533_CMD_UNDEF 0xff
155 #define PN533_CMD_RESPONSE(cmd) (cmd + 1)
157 /* PN533 Return codes */
158 #define PN533_CMD_RET_MASK 0x3F
159 #define PN533_CMD_MI_MASK 0x40
160 #define PN533_CMD_RET_SUCCESS 0x00
162 struct pn533;
164 typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
165 struct sk_buff *resp);
167 /* structs for pn533 commands */
169 /* PN533_CMD_GET_FIRMWARE_VERSION */
170 struct pn533_fw_version {
171 u8 ic;
172 u8 ver;
173 u8 rev;
174 u8 support;
177 /* PN533_CMD_RF_CONFIGURATION */
178 #define PN533_CFGITEM_RF_FIELD 0x01
179 #define PN533_CFGITEM_TIMING 0x02
180 #define PN533_CFGITEM_MAX_RETRIES 0x05
181 #define PN533_CFGITEM_PASORI 0x82
183 #define PN533_CFGITEM_RF_FIELD_AUTO_RFCA 0x2
184 #define PN533_CFGITEM_RF_FIELD_ON 0x1
185 #define PN533_CFGITEM_RF_FIELD_OFF 0x0
187 #define PN533_CONFIG_TIMING_102 0xb
188 #define PN533_CONFIG_TIMING_204 0xc
189 #define PN533_CONFIG_TIMING_409 0xd
190 #define PN533_CONFIG_TIMING_819 0xe
192 #define PN533_CONFIG_MAX_RETRIES_NO_RETRY 0x00
193 #define PN533_CONFIG_MAX_RETRIES_ENDLESS 0xFF
195 struct pn533_config_max_retries {
196 u8 mx_rty_atr;
197 u8 mx_rty_psl;
198 u8 mx_rty_passive_act;
199 } __packed;
201 struct pn533_config_timing {
202 u8 rfu;
203 u8 atr_res_timeout;
204 u8 dep_timeout;
205 } __packed;
207 /* PN533_CMD_IN_LIST_PASSIVE_TARGET */
209 /* felica commands opcode */
210 #define PN533_FELICA_OPC_SENSF_REQ 0
211 #define PN533_FELICA_OPC_SENSF_RES 1
212 /* felica SENSF_REQ parameters */
213 #define PN533_FELICA_SENSF_SC_ALL 0xFFFF
214 #define PN533_FELICA_SENSF_RC_NO_SYSTEM_CODE 0
215 #define PN533_FELICA_SENSF_RC_SYSTEM_CODE 1
216 #define PN533_FELICA_SENSF_RC_ADVANCED_PROTOCOL 2
218 /* type B initiator_data values */
219 #define PN533_TYPE_B_AFI_ALL_FAMILIES 0
220 #define PN533_TYPE_B_POLL_METHOD_TIMESLOT 0
221 #define PN533_TYPE_B_POLL_METHOD_PROBABILISTIC 1
223 union pn533_cmd_poll_initdata {
224 struct {
225 u8 afi;
226 u8 polling_method;
227 } __packed type_b;
228 struct {
229 u8 opcode;
230 __be16 sc;
231 u8 rc;
232 u8 tsn;
233 } __packed felica;
236 /* Poll modulations */
237 enum {
238 PN533_POLL_MOD_106KBPS_A,
239 PN533_POLL_MOD_212KBPS_FELICA,
240 PN533_POLL_MOD_424KBPS_FELICA,
241 PN533_POLL_MOD_106KBPS_JEWEL,
242 PN533_POLL_MOD_847KBPS_B,
243 PN533_LISTEN_MOD,
245 __PN533_POLL_MOD_AFTER_LAST,
247 #define PN533_POLL_MOD_MAX (__PN533_POLL_MOD_AFTER_LAST - 1)
249 struct pn533_poll_modulations {
250 struct {
251 u8 maxtg;
252 u8 brty;
253 union pn533_cmd_poll_initdata initiator_data;
254 } __packed data;
255 u8 len;
258 static const struct pn533_poll_modulations poll_mod[] = {
259 [PN533_POLL_MOD_106KBPS_A] = {
260 .data = {
261 .maxtg = 1,
262 .brty = 0,
264 .len = 2,
266 [PN533_POLL_MOD_212KBPS_FELICA] = {
267 .data = {
268 .maxtg = 1,
269 .brty = 1,
270 .initiator_data.felica = {
271 .opcode = PN533_FELICA_OPC_SENSF_REQ,
272 .sc = PN533_FELICA_SENSF_SC_ALL,
273 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
274 .tsn = 0x03,
277 .len = 7,
279 [PN533_POLL_MOD_424KBPS_FELICA] = {
280 .data = {
281 .maxtg = 1,
282 .brty = 2,
283 .initiator_data.felica = {
284 .opcode = PN533_FELICA_OPC_SENSF_REQ,
285 .sc = PN533_FELICA_SENSF_SC_ALL,
286 .rc = PN533_FELICA_SENSF_RC_SYSTEM_CODE,
287 .tsn = 0x03,
290 .len = 7,
292 [PN533_POLL_MOD_106KBPS_JEWEL] = {
293 .data = {
294 .maxtg = 1,
295 .brty = 4,
297 .len = 2,
299 [PN533_POLL_MOD_847KBPS_B] = {
300 .data = {
301 .maxtg = 1,
302 .brty = 8,
303 .initiator_data.type_b = {
304 .afi = PN533_TYPE_B_AFI_ALL_FAMILIES,
305 .polling_method =
306 PN533_TYPE_B_POLL_METHOD_TIMESLOT,
309 .len = 3,
311 [PN533_LISTEN_MOD] = {
312 .len = 0,
316 /* PN533_CMD_IN_ATR */
318 struct pn533_cmd_activate_response {
319 u8 status;
320 u8 nfcid3t[10];
321 u8 didt;
322 u8 bst;
323 u8 brt;
324 u8 to;
325 u8 ppt;
326 /* optional */
327 u8 gt[];
328 } __packed;
330 struct pn533_cmd_jump_dep_response {
331 u8 status;
332 u8 tg;
333 u8 nfcid3t[10];
334 u8 didt;
335 u8 bst;
336 u8 brt;
337 u8 to;
338 u8 ppt;
339 /* optional */
340 u8 gt[];
341 } __packed;
344 /* PN533_TG_INIT_AS_TARGET */
345 #define PN533_INIT_TARGET_PASSIVE 0x1
346 #define PN533_INIT_TARGET_DEP 0x2
348 #define PN533_INIT_TARGET_RESP_FRAME_MASK 0x3
349 #define PN533_INIT_TARGET_RESP_ACTIVE 0x1
350 #define PN533_INIT_TARGET_RESP_DEP 0x4
352 enum pn533_protocol_type {
353 PN533_PROTO_REQ_ACK_RESP = 0,
354 PN533_PROTO_REQ_RESP
357 struct pn533 {
358 struct usb_device *udev;
359 struct usb_interface *interface;
360 struct nfc_dev *nfc_dev;
361 u32 device_type;
362 enum pn533_protocol_type protocol_type;
364 struct urb *out_urb;
365 struct urb *in_urb;
367 struct sk_buff_head resp_q;
368 struct sk_buff_head fragment_skb;
370 struct workqueue_struct *wq;
371 struct work_struct cmd_work;
372 struct work_struct cmd_complete_work;
373 struct delayed_work poll_work;
374 struct work_struct mi_rx_work;
375 struct work_struct mi_tx_work;
376 struct work_struct tg_work;
377 struct work_struct rf_work;
379 struct list_head cmd_queue;
380 struct pn533_cmd *cmd;
381 u8 cmd_pending;
382 struct mutex cmd_lock; /* protects cmd queue */
384 void *cmd_complete_mi_arg;
385 void *cmd_complete_dep_arg;
387 struct pn533_poll_modulations *poll_mod_active[PN533_POLL_MOD_MAX + 1];
388 u8 poll_mod_count;
389 u8 poll_mod_curr;
390 u32 poll_protocols;
391 u32 listen_protocols;
392 struct timer_list listen_timer;
393 int cancel_listen;
395 u8 *gb;
396 size_t gb_len;
398 u8 tgt_available_prots;
399 u8 tgt_active_prot;
400 u8 tgt_mode;
402 struct pn533_frame_ops *ops;
405 struct pn533_cmd {
406 struct list_head queue;
407 u8 code;
408 int status;
409 struct sk_buff *req;
410 struct sk_buff *resp;
411 int resp_len;
412 pn533_send_async_complete_t complete_cb;
413 void *complete_cb_context;
416 struct pn533_std_frame {
417 u8 preamble;
418 __be16 start_frame;
419 u8 datalen;
420 u8 datalen_checksum;
421 u8 data[];
422 } __packed;
424 struct pn533_ext_frame { /* Extended Information frame */
425 u8 preamble;
426 __be16 start_frame;
427 __be16 eif_flag; /* fixed to 0xFFFF */
428 __be16 datalen;
429 u8 datalen_checksum;
430 u8 data[];
431 } __packed;
433 struct pn533_frame_ops {
434 void (*tx_frame_init)(void *frame, u8 cmd_code);
435 void (*tx_frame_finish)(void *frame);
436 void (*tx_update_payload_len)(void *frame, int len);
437 int tx_header_len;
438 int tx_tail_len;
440 bool (*rx_is_frame_valid)(void *frame, struct pn533 *dev);
441 int (*rx_frame_size)(void *frame);
442 int rx_header_len;
443 int rx_tail_len;
445 int max_payload_len;
446 u8 (*get_cmd_code)(void *frame);
449 struct pn533_acr122_ccid_hdr {
450 u8 type;
451 u32 datalen;
452 u8 slot;
453 u8 seq;
454 u8 params[3]; /* 3 msg specific bytes or status, error and 1 specific
455 byte for reposnse msg */
456 u8 data[]; /* payload */
457 } __packed;
459 struct pn533_acr122_apdu_hdr {
460 u8 class;
461 u8 ins;
462 u8 p1;
463 u8 p2;
464 } __packed;
466 struct pn533_acr122_tx_frame {
467 struct pn533_acr122_ccid_hdr ccid;
468 struct pn533_acr122_apdu_hdr apdu;
469 u8 datalen;
470 u8 data[]; /* pn533 frame: TFI ... */
471 } __packed;
473 struct pn533_acr122_rx_frame {
474 struct pn533_acr122_ccid_hdr ccid;
475 u8 data[]; /* pn533 frame : TFI ... */
476 } __packed;
478 static void pn533_acr122_tx_frame_init(void *_frame, u8 cmd_code)
480 struct pn533_acr122_tx_frame *frame = _frame;
482 frame->ccid.type = PN533_ACR122_PC_TO_RDR_ESCAPE;
483 frame->ccid.datalen = sizeof(frame->apdu) + 1; /* sizeof(apdu_hdr) +
484 sizeof(datalen) */
485 frame->ccid.slot = 0;
486 frame->ccid.seq = 0;
487 frame->ccid.params[0] = 0;
488 frame->ccid.params[1] = 0;
489 frame->ccid.params[2] = 0;
491 frame->data[0] = PN533_STD_FRAME_DIR_OUT;
492 frame->data[1] = cmd_code;
493 frame->datalen = 2; /* data[0] + data[1] */
495 frame->apdu.class = 0xFF;
496 frame->apdu.ins = 0;
497 frame->apdu.p1 = 0;
498 frame->apdu.p2 = 0;
501 static void pn533_acr122_tx_frame_finish(void *_frame)
503 struct pn533_acr122_tx_frame *frame = _frame;
505 frame->ccid.datalen += frame->datalen;
508 static void pn533_acr122_tx_update_payload_len(void *_frame, int len)
510 struct pn533_acr122_tx_frame *frame = _frame;
512 frame->datalen += len;
515 static bool pn533_acr122_is_rx_frame_valid(void *_frame, struct pn533 *dev)
517 struct pn533_acr122_rx_frame *frame = _frame;
519 if (frame->ccid.type != 0x83)
520 return false;
522 if (frame->data[frame->ccid.datalen - 2] == 0x63)
523 return false;
525 return true;
528 static int pn533_acr122_rx_frame_size(void *frame)
530 struct pn533_acr122_rx_frame *f = frame;
532 /* f->ccid.datalen already includes tail length */
533 return sizeof(struct pn533_acr122_rx_frame) + f->ccid.datalen;
536 static u8 pn533_acr122_get_cmd_code(void *frame)
538 struct pn533_acr122_rx_frame *f = frame;
540 return PN533_FRAME_CMD(f);
543 static struct pn533_frame_ops pn533_acr122_frame_ops = {
544 .tx_frame_init = pn533_acr122_tx_frame_init,
545 .tx_frame_finish = pn533_acr122_tx_frame_finish,
546 .tx_update_payload_len = pn533_acr122_tx_update_payload_len,
547 .tx_header_len = PN533_ACR122_TX_FRAME_HEADER_LEN,
548 .tx_tail_len = PN533_ACR122_TX_FRAME_TAIL_LEN,
550 .rx_is_frame_valid = pn533_acr122_is_rx_frame_valid,
551 .rx_header_len = PN533_ACR122_RX_FRAME_HEADER_LEN,
552 .rx_tail_len = PN533_ACR122_RX_FRAME_TAIL_LEN,
553 .rx_frame_size = pn533_acr122_rx_frame_size,
555 .max_payload_len = PN533_ACR122_FRAME_MAX_PAYLOAD_LEN,
556 .get_cmd_code = pn533_acr122_get_cmd_code,
559 /* The rule: value(high byte) + value(low byte) + checksum = 0 */
560 static inline u8 pn533_ext_checksum(u16 value)
562 return ~(u8)(((value & 0xFF00) >> 8) + (u8)(value & 0xFF)) + 1;
565 /* The rule: value + checksum = 0 */
566 static inline u8 pn533_std_checksum(u8 value)
568 return ~value + 1;
571 /* The rule: sum(data elements) + checksum = 0 */
572 static u8 pn533_std_data_checksum(u8 *data, int datalen)
574 u8 sum = 0;
575 int i;
577 for (i = 0; i < datalen; i++)
578 sum += data[i];
580 return pn533_std_checksum(sum);
583 static void pn533_std_tx_frame_init(void *_frame, u8 cmd_code)
585 struct pn533_std_frame *frame = _frame;
587 frame->preamble = 0;
588 frame->start_frame = cpu_to_be16(PN533_STD_FRAME_SOF);
589 PN533_STD_FRAME_IDENTIFIER(frame) = PN533_STD_FRAME_DIR_OUT;
590 PN533_FRAME_CMD(frame) = cmd_code;
591 frame->datalen = 2;
594 static void pn533_std_tx_frame_finish(void *_frame)
596 struct pn533_std_frame *frame = _frame;
598 frame->datalen_checksum = pn533_std_checksum(frame->datalen);
600 PN533_STD_FRAME_CHECKSUM(frame) =
601 pn533_std_data_checksum(frame->data, frame->datalen);
603 PN533_STD_FRAME_POSTAMBLE(frame) = 0;
606 static void pn533_std_tx_update_payload_len(void *_frame, int len)
608 struct pn533_std_frame *frame = _frame;
610 frame->datalen += len;
613 static bool pn533_std_rx_frame_is_valid(void *_frame, struct pn533 *dev)
615 u8 checksum;
616 struct pn533_std_frame *stdf = _frame;
618 if (stdf->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
619 return false;
621 if (likely(!PN533_STD_IS_EXTENDED(stdf))) {
622 /* Standard frame code */
623 dev->ops->rx_header_len = PN533_STD_FRAME_HEADER_LEN;
625 checksum = pn533_std_checksum(stdf->datalen);
626 if (checksum != stdf->datalen_checksum)
627 return false;
629 checksum = pn533_std_data_checksum(stdf->data, stdf->datalen);
630 if (checksum != PN533_STD_FRAME_CHECKSUM(stdf))
631 return false;
632 } else {
633 /* Extended */
634 struct pn533_ext_frame *eif = _frame;
636 dev->ops->rx_header_len = PN533_EXT_FRAME_HEADER_LEN;
638 checksum = pn533_ext_checksum(be16_to_cpu(eif->datalen));
639 if (checksum != eif->datalen_checksum)
640 return false;
642 /* check data checksum */
643 checksum = pn533_std_data_checksum(eif->data,
644 be16_to_cpu(eif->datalen));
645 if (checksum != PN533_EXT_FRAME_CHECKSUM(eif))
646 return false;
649 return true;
652 static bool pn533_std_rx_frame_is_ack(struct pn533_std_frame *frame)
654 if (frame->start_frame != cpu_to_be16(PN533_STD_FRAME_SOF))
655 return false;
657 if (frame->datalen != 0 || frame->datalen_checksum != 0xFF)
658 return false;
660 return true;
663 static inline int pn533_std_rx_frame_size(void *frame)
665 struct pn533_std_frame *f = frame;
667 /* check for Extended Information frame */
668 if (PN533_STD_IS_EXTENDED(f)) {
669 struct pn533_ext_frame *eif = frame;
671 return sizeof(struct pn533_ext_frame)
672 + be16_to_cpu(eif->datalen) + PN533_STD_FRAME_TAIL_LEN;
675 return sizeof(struct pn533_std_frame) + f->datalen +
676 PN533_STD_FRAME_TAIL_LEN;
679 static u8 pn533_std_get_cmd_code(void *frame)
681 struct pn533_std_frame *f = frame;
682 struct pn533_ext_frame *eif = frame;
684 if (PN533_STD_IS_EXTENDED(f))
685 return PN533_FRAME_CMD(eif);
686 else
687 return PN533_FRAME_CMD(f);
690 static struct pn533_frame_ops pn533_std_frame_ops = {
691 .tx_frame_init = pn533_std_tx_frame_init,
692 .tx_frame_finish = pn533_std_tx_frame_finish,
693 .tx_update_payload_len = pn533_std_tx_update_payload_len,
694 .tx_header_len = PN533_STD_FRAME_HEADER_LEN,
695 .tx_tail_len = PN533_STD_FRAME_TAIL_LEN,
697 .rx_is_frame_valid = pn533_std_rx_frame_is_valid,
698 .rx_frame_size = pn533_std_rx_frame_size,
699 .rx_header_len = PN533_STD_FRAME_HEADER_LEN,
700 .rx_tail_len = PN533_STD_FRAME_TAIL_LEN,
702 .max_payload_len = PN533_STD_FRAME_MAX_PAYLOAD_LEN,
703 .get_cmd_code = pn533_std_get_cmd_code,
706 static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
708 return (dev->ops->get_cmd_code(frame) ==
709 PN533_CMD_RESPONSE(dev->cmd->code));
712 static void pn533_recv_response(struct urb *urb)
714 struct pn533 *dev = urb->context;
715 struct pn533_cmd *cmd = dev->cmd;
716 u8 *in_frame;
718 cmd->status = urb->status;
720 switch (urb->status) {
721 case 0:
722 break; /* success */
723 case -ECONNRESET:
724 case -ENOENT:
725 nfc_dev_dbg(&dev->interface->dev,
726 "The urb has been canceled (status %d)",
727 urb->status);
728 goto sched_wq;
729 case -ESHUTDOWN:
730 default:
731 nfc_dev_err(&dev->interface->dev,
732 "Urb failure (status %d)", urb->status);
733 goto sched_wq;
736 in_frame = dev->in_urb->transfer_buffer;
738 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
739 print_hex_dump_debug("PN533 RX: ", DUMP_PREFIX_NONE, 16, 1, in_frame,
740 dev->ops->rx_frame_size(in_frame), false);
742 if (!dev->ops->rx_is_frame_valid(in_frame, dev)) {
743 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
744 cmd->status = -EIO;
745 goto sched_wq;
748 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
749 nfc_dev_err(&dev->interface->dev,
750 "It it not the response to the last command");
751 cmd->status = -EIO;
752 goto sched_wq;
755 sched_wq:
756 queue_work(dev->wq, &dev->cmd_complete_work);
759 static int pn533_submit_urb_for_response(struct pn533 *dev, gfp_t flags)
761 dev->in_urb->complete = pn533_recv_response;
763 return usb_submit_urb(dev->in_urb, flags);
766 static void pn533_recv_ack(struct urb *urb)
768 struct pn533 *dev = urb->context;
769 struct pn533_cmd *cmd = dev->cmd;
770 struct pn533_std_frame *in_frame;
771 int rc;
773 cmd->status = urb->status;
775 switch (urb->status) {
776 case 0:
777 break; /* success */
778 case -ECONNRESET:
779 case -ENOENT:
780 nfc_dev_dbg(&dev->interface->dev,
781 "The urb has been stopped (status %d)",
782 urb->status);
783 goto sched_wq;
784 case -ESHUTDOWN:
785 default:
786 nfc_dev_err(&dev->interface->dev,
787 "Urb failure (status %d)", urb->status);
788 goto sched_wq;
791 in_frame = dev->in_urb->transfer_buffer;
793 if (!pn533_std_rx_frame_is_ack(in_frame)) {
794 nfc_dev_err(&dev->interface->dev, "Received an invalid ack");
795 cmd->status = -EIO;
796 goto sched_wq;
799 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
800 if (rc) {
801 nfc_dev_err(&dev->interface->dev,
802 "usb_submit_urb failed with result %d", rc);
803 cmd->status = rc;
804 goto sched_wq;
807 return;
809 sched_wq:
810 queue_work(dev->wq, &dev->cmd_complete_work);
813 static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
815 dev->in_urb->complete = pn533_recv_ack;
817 return usb_submit_urb(dev->in_urb, flags);
820 static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
822 u8 ack[PN533_STD_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
823 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
824 int rc;
826 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
828 dev->out_urb->transfer_buffer = ack;
829 dev->out_urb->transfer_buffer_length = sizeof(ack);
830 rc = usb_submit_urb(dev->out_urb, flags);
832 return rc;
835 static int __pn533_send_frame_async(struct pn533 *dev,
836 struct sk_buff *out,
837 struct sk_buff *in,
838 int in_len)
840 int rc;
842 dev->out_urb->transfer_buffer = out->data;
843 dev->out_urb->transfer_buffer_length = out->len;
845 dev->in_urb->transfer_buffer = in->data;
846 dev->in_urb->transfer_buffer_length = in_len;
848 print_hex_dump_debug("PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
849 out->data, out->len, false);
851 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
852 if (rc)
853 return rc;
855 if (dev->protocol_type == PN533_PROTO_REQ_RESP) {
856 /* request for response for sent packet directly */
857 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
858 if (rc)
859 goto error;
860 } else if (dev->protocol_type == PN533_PROTO_REQ_ACK_RESP) {
861 /* request for ACK if that's the case */
862 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
863 if (rc)
864 goto error;
867 return 0;
869 error:
870 usb_unlink_urb(dev->out_urb);
871 return rc;
874 static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
875 struct sk_buff *skb)
877 /* payload is already there, just update datalen */
878 int payload_len = skb->len;
879 struct pn533_frame_ops *ops = dev->ops;
882 skb_push(skb, ops->tx_header_len);
883 skb_put(skb, ops->tx_tail_len);
885 ops->tx_frame_init(skb->data, cmd_code);
886 ops->tx_update_payload_len(skb->data, payload_len);
887 ops->tx_frame_finish(skb->data);
890 static int pn533_send_async_complete(struct pn533 *dev)
892 struct pn533_cmd *cmd = dev->cmd;
893 int status = cmd->status;
895 struct sk_buff *req = cmd->req;
896 struct sk_buff *resp = cmd->resp;
898 int rc;
900 dev_kfree_skb(req);
902 if (status < 0) {
903 rc = cmd->complete_cb(dev, cmd->complete_cb_context,
904 ERR_PTR(status));
905 dev_kfree_skb(resp);
906 goto done;
909 skb_put(resp, dev->ops->rx_frame_size(resp->data));
910 skb_pull(resp, dev->ops->rx_header_len);
911 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
913 rc = cmd->complete_cb(dev, cmd->complete_cb_context, resp);
915 done:
916 kfree(cmd);
917 dev->cmd = NULL;
918 return rc;
921 static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
922 struct sk_buff *req, struct sk_buff *resp,
923 int resp_len,
924 pn533_send_async_complete_t complete_cb,
925 void *complete_cb_context)
927 struct pn533_cmd *cmd;
928 int rc = 0;
930 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
932 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
933 if (!cmd)
934 return -ENOMEM;
936 cmd->code = cmd_code;
937 cmd->req = req;
938 cmd->resp = resp;
939 cmd->resp_len = resp_len;
940 cmd->complete_cb = complete_cb;
941 cmd->complete_cb_context = complete_cb_context;
943 pn533_build_cmd_frame(dev, cmd_code, req);
945 mutex_lock(&dev->cmd_lock);
947 if (!dev->cmd_pending) {
948 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
949 if (rc)
950 goto error;
952 dev->cmd_pending = 1;
953 dev->cmd = cmd;
954 goto unlock;
957 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
958 cmd_code);
960 INIT_LIST_HEAD(&cmd->queue);
961 list_add_tail(&cmd->queue, &dev->cmd_queue);
963 goto unlock;
965 error:
966 kfree(cmd);
967 unlock:
968 mutex_unlock(&dev->cmd_lock);
969 return rc;
972 static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
973 struct sk_buff *req,
974 pn533_send_async_complete_t complete_cb,
975 void *complete_cb_context)
977 struct sk_buff *resp;
978 int rc;
979 int resp_len = dev->ops->rx_header_len +
980 dev->ops->max_payload_len +
981 dev->ops->rx_tail_len;
983 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
984 if (!resp)
985 return -ENOMEM;
987 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
988 complete_cb_context);
989 if (rc)
990 dev_kfree_skb(resp);
992 return rc;
995 static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
996 struct sk_buff *req,
997 pn533_send_async_complete_t complete_cb,
998 void *complete_cb_context)
1000 struct sk_buff *resp;
1001 int rc;
1002 int resp_len = dev->ops->rx_header_len +
1003 dev->ops->max_payload_len +
1004 dev->ops->rx_tail_len;
1006 resp = alloc_skb(resp_len, GFP_KERNEL);
1007 if (!resp)
1008 return -ENOMEM;
1010 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
1011 complete_cb_context);
1012 if (rc)
1013 dev_kfree_skb(resp);
1015 return rc;
1019 * pn533_send_cmd_direct_async
1021 * The function sends a piority cmd directly to the chip omiting the cmd
1022 * queue. It's intended to be used by chaining mechanism of received responses
1023 * where the host has to request every single chunk of data before scheduling
1024 * next cmd from the queue.
1026 static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
1027 struct sk_buff *req,
1028 pn533_send_async_complete_t complete_cb,
1029 void *complete_cb_context)
1031 struct sk_buff *resp;
1032 struct pn533_cmd *cmd;
1033 int rc;
1034 int resp_len = dev->ops->rx_header_len +
1035 dev->ops->max_payload_len +
1036 dev->ops->rx_tail_len;
1038 resp = alloc_skb(resp_len, GFP_KERNEL);
1039 if (!resp)
1040 return -ENOMEM;
1042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1043 if (!cmd) {
1044 dev_kfree_skb(resp);
1045 return -ENOMEM;
1048 cmd->code = cmd_code;
1049 cmd->req = req;
1050 cmd->resp = resp;
1051 cmd->resp_len = resp_len;
1052 cmd->complete_cb = complete_cb;
1053 cmd->complete_cb_context = complete_cb_context;
1055 pn533_build_cmd_frame(dev, cmd_code, req);
1057 rc = __pn533_send_frame_async(dev, req, resp, resp_len);
1058 if (rc < 0) {
1059 dev_kfree_skb(resp);
1060 kfree(cmd);
1061 } else {
1062 dev->cmd = cmd;
1065 return rc;
1068 static void pn533_wq_cmd_complete(struct work_struct *work)
1070 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
1071 int rc;
1073 rc = pn533_send_async_complete(dev);
1074 if (rc != -EINPROGRESS)
1075 queue_work(dev->wq, &dev->cmd_work);
1078 static void pn533_wq_cmd(struct work_struct *work)
1080 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
1081 struct pn533_cmd *cmd;
1082 int rc;
1084 mutex_lock(&dev->cmd_lock);
1086 if (list_empty(&dev->cmd_queue)) {
1087 dev->cmd_pending = 0;
1088 mutex_unlock(&dev->cmd_lock);
1089 return;
1092 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
1094 list_del(&cmd->queue);
1096 mutex_unlock(&dev->cmd_lock);
1098 rc = __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len);
1099 if (rc < 0) {
1100 dev_kfree_skb(cmd->req);
1101 dev_kfree_skb(cmd->resp);
1102 kfree(cmd);
1103 return;
1106 dev->cmd = cmd;
1109 struct pn533_sync_cmd_response {
1110 struct sk_buff *resp;
1111 struct completion done;
1114 static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
1115 struct sk_buff *resp)
1117 struct pn533_sync_cmd_response *arg = _arg;
1119 arg->resp = resp;
1120 complete(&arg->done);
1122 return 0;
1125 /* pn533_send_cmd_sync
1127 * Please note the req parameter is freed inside the function to
1128 * limit a number of return value interpretations by the caller.
1130 * 1. negative in case of error during TX path -> req should be freed
1132 * 2. negative in case of error during RX path -> req should not be freed
1133 * as it's been already freed at the begining of RX path by
1134 * async_complete_cb.
1136 * 3. valid pointer in case of succesfult RX path
1138 * A caller has to check a return value with IS_ERR macro. If the test pass,
1139 * the returned pointer is valid.
1141 * */
1142 static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
1143 struct sk_buff *req)
1145 int rc;
1146 struct pn533_sync_cmd_response arg;
1148 init_completion(&arg.done);
1150 rc = pn533_send_cmd_async(dev, cmd_code, req,
1151 pn533_send_sync_complete, &arg);
1152 if (rc) {
1153 dev_kfree_skb(req);
1154 return ERR_PTR(rc);
1157 wait_for_completion(&arg.done);
1159 return arg.resp;
1162 static void pn533_send_complete(struct urb *urb)
1164 struct pn533 *dev = urb->context;
1166 switch (urb->status) {
1167 case 0:
1168 break; /* success */
1169 case -ECONNRESET:
1170 case -ENOENT:
1171 nfc_dev_dbg(&dev->interface->dev,
1172 "The urb has been stopped (status %d)",
1173 urb->status);
1174 break;
1175 case -ESHUTDOWN:
1176 default:
1177 nfc_dev_err(&dev->interface->dev,
1178 "Urb failure (status %d)", urb->status);
1182 static void pn533_abort_cmd(struct pn533 *dev, gfp_t flags)
1184 /* ACR122U does not support any command which aborts last
1185 * issued command i.e. as ACK for standard PN533. Additionally,
1186 * it behaves stange, sending broken or incorrect responses,
1187 * when we cancel urb before the chip will send response.
1189 if (dev->device_type == PN533_DEVICE_ACR122U)
1190 return;
1192 /* An ack will cancel the last issued command */
1193 pn533_send_ack(dev, flags);
1195 /* cancel the urb request */
1196 usb_kill_urb(dev->in_urb);
1199 static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
1201 struct sk_buff *skb;
1203 skb = alloc_skb(dev->ops->tx_header_len +
1204 size +
1205 dev->ops->tx_tail_len, GFP_KERNEL);
1207 if (skb)
1208 skb_reserve(skb, dev->ops->tx_header_len);
1210 return skb;
1213 struct pn533_target_type_a {
1214 __be16 sens_res;
1215 u8 sel_res;
1216 u8 nfcid_len;
1217 u8 nfcid_data[];
1218 } __packed;
1221 #define PN533_TYPE_A_SENS_RES_NFCID1(x) ((u8)((be16_to_cpu(x) & 0x00C0) >> 6))
1222 #define PN533_TYPE_A_SENS_RES_SSD(x) ((u8)((be16_to_cpu(x) & 0x001F) >> 0))
1223 #define PN533_TYPE_A_SENS_RES_PLATCONF(x) ((u8)((be16_to_cpu(x) & 0x0F00) >> 8))
1225 #define PN533_TYPE_A_SENS_RES_SSD_JEWEL 0x00
1226 #define PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL 0x0C
1228 #define PN533_TYPE_A_SEL_PROT(x) (((x) & 0x60) >> 5)
1229 #define PN533_TYPE_A_SEL_CASCADE(x) (((x) & 0x04) >> 2)
1231 #define PN533_TYPE_A_SEL_PROT_MIFARE 0
1232 #define PN533_TYPE_A_SEL_PROT_ISO14443 1
1233 #define PN533_TYPE_A_SEL_PROT_DEP 2
1234 #define PN533_TYPE_A_SEL_PROT_ISO14443_DEP 3
1236 static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
1237 int target_data_len)
1239 u8 ssd;
1240 u8 platconf;
1242 if (target_data_len < sizeof(struct pn533_target_type_a))
1243 return false;
1245 /* The lenght check of nfcid[] and ats[] are not being performed because
1246 the values are not being used */
1248 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1249 ssd = PN533_TYPE_A_SENS_RES_SSD(type_a->sens_res);
1250 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
1252 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1253 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1254 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1255 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1256 return false;
1258 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
1259 if (PN533_TYPE_A_SEL_CASCADE(type_a->sel_res) != 0)
1260 return false;
1262 return true;
1265 static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
1266 int tgt_data_len)
1268 struct pn533_target_type_a *tgt_type_a;
1270 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
1272 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
1273 return -EPROTO;
1275 switch (PN533_TYPE_A_SEL_PROT(tgt_type_a->sel_res)) {
1276 case PN533_TYPE_A_SEL_PROT_MIFARE:
1277 nfc_tgt->supported_protocols = NFC_PROTO_MIFARE_MASK;
1278 break;
1279 case PN533_TYPE_A_SEL_PROT_ISO14443:
1280 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK;
1281 break;
1282 case PN533_TYPE_A_SEL_PROT_DEP:
1283 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1284 break;
1285 case PN533_TYPE_A_SEL_PROT_ISO14443_DEP:
1286 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_MASK |
1287 NFC_PROTO_NFC_DEP_MASK;
1288 break;
1291 nfc_tgt->sens_res = be16_to_cpu(tgt_type_a->sens_res);
1292 nfc_tgt->sel_res = tgt_type_a->sel_res;
1293 nfc_tgt->nfcid1_len = tgt_type_a->nfcid_len;
1294 memcpy(nfc_tgt->nfcid1, tgt_type_a->nfcid_data, nfc_tgt->nfcid1_len);
1296 return 0;
1299 struct pn533_target_felica {
1300 u8 pol_res;
1301 u8 opcode;
1302 u8 nfcid2[NFC_NFCID2_MAXSIZE];
1303 u8 pad[8];
1304 /* optional */
1305 u8 syst_code[];
1306 } __packed;
1308 #define PN533_FELICA_SENSF_NFCID2_DEP_B1 0x01
1309 #define PN533_FELICA_SENSF_NFCID2_DEP_B2 0xFE
1311 static bool pn533_target_felica_is_valid(struct pn533_target_felica *felica,
1312 int target_data_len)
1314 if (target_data_len < sizeof(struct pn533_target_felica))
1315 return false;
1317 if (felica->opcode != PN533_FELICA_OPC_SENSF_RES)
1318 return false;
1320 return true;
1323 static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
1324 int tgt_data_len)
1326 struct pn533_target_felica *tgt_felica;
1328 tgt_felica = (struct pn533_target_felica *)tgt_data;
1330 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
1331 return -EPROTO;
1333 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
1334 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
1335 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1336 else
1337 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
1339 memcpy(nfc_tgt->sensf_res, &tgt_felica->opcode, 9);
1340 nfc_tgt->sensf_res_len = 9;
1342 memcpy(nfc_tgt->nfcid2, tgt_felica->nfcid2, NFC_NFCID2_MAXSIZE);
1343 nfc_tgt->nfcid2_len = NFC_NFCID2_MAXSIZE;
1345 return 0;
1348 struct pn533_target_jewel {
1349 __be16 sens_res;
1350 u8 jewelid[4];
1351 } __packed;
1353 static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
1354 int target_data_len)
1356 u8 ssd;
1357 u8 platconf;
1359 if (target_data_len < sizeof(struct pn533_target_jewel))
1360 return false;
1362 /* Requirement 4.6.3.3 from NFC Forum Digital Spec */
1363 ssd = PN533_TYPE_A_SENS_RES_SSD(jewel->sens_res);
1364 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
1366 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1367 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
1368 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
1369 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
1370 return false;
1372 return true;
1375 static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
1376 int tgt_data_len)
1378 struct pn533_target_jewel *tgt_jewel;
1380 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
1382 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
1383 return -EPROTO;
1385 nfc_tgt->supported_protocols = NFC_PROTO_JEWEL_MASK;
1386 nfc_tgt->sens_res = be16_to_cpu(tgt_jewel->sens_res);
1387 nfc_tgt->nfcid1_len = 4;
1388 memcpy(nfc_tgt->nfcid1, tgt_jewel->jewelid, nfc_tgt->nfcid1_len);
1390 return 0;
1393 struct pn533_type_b_prot_info {
1394 u8 bitrate;
1395 u8 fsci_type;
1396 u8 fwi_adc_fo;
1397 } __packed;
1399 #define PN533_TYPE_B_PROT_FCSI(x) (((x) & 0xF0) >> 4)
1400 #define PN533_TYPE_B_PROT_TYPE(x) (((x) & 0x0F) >> 0)
1401 #define PN533_TYPE_B_PROT_TYPE_RFU_MASK 0x8
1403 struct pn533_type_b_sens_res {
1404 u8 opcode;
1405 u8 nfcid[4];
1406 u8 appdata[4];
1407 struct pn533_type_b_prot_info prot_info;
1408 } __packed;
1410 #define PN533_TYPE_B_OPC_SENSB_RES 0x50
1412 struct pn533_target_type_b {
1413 struct pn533_type_b_sens_res sensb_res;
1414 u8 attrib_res_len;
1415 u8 attrib_res[];
1416 } __packed;
1418 static bool pn533_target_type_b_is_valid(struct pn533_target_type_b *type_b,
1419 int target_data_len)
1421 if (target_data_len < sizeof(struct pn533_target_type_b))
1422 return false;
1424 if (type_b->sensb_res.opcode != PN533_TYPE_B_OPC_SENSB_RES)
1425 return false;
1427 if (PN533_TYPE_B_PROT_TYPE(type_b->sensb_res.prot_info.fsci_type) &
1428 PN533_TYPE_B_PROT_TYPE_RFU_MASK)
1429 return false;
1431 return true;
1434 static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1435 int tgt_data_len)
1437 struct pn533_target_type_b *tgt_type_b;
1439 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1441 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1442 return -EPROTO;
1444 nfc_tgt->supported_protocols = NFC_PROTO_ISO14443_B_MASK;
1446 return 0;
1449 static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1450 int tgdata_len)
1452 struct nfc_target nfc_tgt;
1453 int rc;
1455 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1456 dev->poll_mod_curr);
1458 if (tg != 1)
1459 return -EPROTO;
1461 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1463 switch (dev->poll_mod_curr) {
1464 case PN533_POLL_MOD_106KBPS_A:
1465 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1466 break;
1467 case PN533_POLL_MOD_212KBPS_FELICA:
1468 case PN533_POLL_MOD_424KBPS_FELICA:
1469 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1470 break;
1471 case PN533_POLL_MOD_106KBPS_JEWEL:
1472 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1473 break;
1474 case PN533_POLL_MOD_847KBPS_B:
1475 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1476 break;
1477 default:
1478 nfc_dev_err(&dev->interface->dev,
1479 "Unknown current poll modulation");
1480 return -EPROTO;
1483 if (rc)
1484 return rc;
1486 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1487 nfc_dev_dbg(&dev->interface->dev,
1488 "The Tg found doesn't have the desired protocol");
1489 return -EAGAIN;
1492 nfc_dev_dbg(&dev->interface->dev,
1493 "Target found - supported protocols: 0x%x",
1494 nfc_tgt.supported_protocols);
1496 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1498 nfc_targets_found(dev->nfc_dev, &nfc_tgt, 1);
1500 return 0;
1503 static inline void pn533_poll_next_mod(struct pn533 *dev)
1505 dev->poll_mod_curr = (dev->poll_mod_curr + 1) % dev->poll_mod_count;
1508 static void pn533_poll_reset_mod_list(struct pn533 *dev)
1510 dev->poll_mod_count = 0;
1513 static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1515 dev->poll_mod_active[dev->poll_mod_count] =
1516 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1517 dev->poll_mod_count++;
1520 static void pn533_poll_create_mod_list(struct pn533 *dev,
1521 u32 im_protocols, u32 tm_protocols)
1523 pn533_poll_reset_mod_list(dev);
1525 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1526 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1527 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1528 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1530 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1531 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1532 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1533 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1536 if (im_protocols & NFC_PROTO_JEWEL_MASK)
1537 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_JEWEL);
1539 if (im_protocols & NFC_PROTO_ISO14443_B_MASK)
1540 pn533_poll_add_mod(dev, PN533_POLL_MOD_847KBPS_B);
1542 if (tm_protocols)
1543 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1546 static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1548 u8 nbtg, tg, *tgdata;
1549 int rc, tgdata_len;
1551 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1553 nbtg = resp->data[0];
1554 tg = resp->data[1];
1555 tgdata = &resp->data[2];
1556 tgdata_len = resp->len - 2; /* nbtg + tg */
1558 if (nbtg) {
1559 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1561 /* We must stop the poll after a valid target found */
1562 if (rc == 0) {
1563 pn533_poll_reset_mod_list(dev);
1564 return 0;
1568 return -EAGAIN;
1571 static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1573 struct sk_buff *skb;
1574 u8 *felica, *nfcid3, *gb;
1576 u8 *gbytes = dev->gb;
1577 size_t gbytes_len = dev->gb_len;
1579 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1580 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1581 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1582 0xff, 0xff}; /* System code */
1584 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1585 0x0, 0x0, 0x0,
1586 0x40}; /* SEL_RES for DEP */
1588 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1589 felica (18), nfcid3 (10), gb_len (1) */
1590 gbytes_len +
1591 1; /* len Tk*/
1593 skb = pn533_alloc_skb(dev, skb_len);
1594 if (!skb)
1595 return NULL;
1597 /* DEP support only */
1598 *skb_put(skb, 1) = PN533_INIT_TARGET_DEP;
1600 /* MIFARE params */
1601 memcpy(skb_put(skb, 6), mifare_params, 6);
1603 /* Felica params */
1604 felica = skb_put(skb, 18);
1605 memcpy(felica, felica_params, 18);
1606 get_random_bytes(felica + 2, 6);
1608 /* NFCID3 */
1609 nfcid3 = skb_put(skb, 10);
1610 memset(nfcid3, 0, 10);
1611 memcpy(nfcid3, felica, 8);
1613 /* General bytes */
1614 *skb_put(skb, 1) = gbytes_len;
1616 gb = skb_put(skb, gbytes_len);
1617 memcpy(gb, gbytes, gbytes_len);
1619 /* Len Tk */
1620 *skb_put(skb, 1) = 0;
1622 return skb;
1625 #define PN533_CMD_DATAEXCH_HEAD_LEN 1
1626 #define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1627 static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1628 struct sk_buff *resp)
1630 u8 status;
1632 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1634 if (IS_ERR(resp))
1635 return PTR_ERR(resp);
1637 status = resp->data[0];
1638 skb_pull(resp, sizeof(status));
1640 if (status != 0) {
1641 nfc_tm_deactivated(dev->nfc_dev);
1642 dev->tgt_mode = 0;
1643 dev_kfree_skb(resp);
1644 return 0;
1647 return nfc_tm_data_received(dev->nfc_dev, resp);
1650 static void pn533_wq_tg_get_data(struct work_struct *work)
1652 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1654 struct sk_buff *skb;
1655 int rc;
1657 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1659 skb = pn533_alloc_skb(dev, 0);
1660 if (!skb)
1661 return;
1663 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1664 pn533_tm_get_data_complete, NULL);
1666 if (rc < 0)
1667 dev_kfree_skb(skb);
1669 return;
1672 #define ATR_REQ_GB_OFFSET 17
1673 static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1675 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1676 size_t gb_len;
1677 int rc;
1679 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1681 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1682 return -EINVAL;
1684 mode = resp->data[0];
1685 cmd = &resp->data[1];
1687 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1688 mode, resp->len);
1690 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1691 PN533_INIT_TARGET_RESP_ACTIVE)
1692 comm_mode = NFC_COMM_ACTIVE;
1694 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1695 return -EOPNOTSUPP;
1697 gb = cmd + ATR_REQ_GB_OFFSET;
1698 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1700 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1701 comm_mode, gb, gb_len);
1702 if (rc < 0) {
1703 nfc_dev_err(&dev->interface->dev,
1704 "Error when signaling target activation");
1705 return rc;
1708 dev->tgt_mode = 1;
1709 queue_work(dev->wq, &dev->tg_work);
1711 return 0;
1714 static void pn533_listen_mode_timer(unsigned long data)
1716 struct pn533 *dev = (struct pn533 *)data;
1718 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1720 dev->cancel_listen = 1;
1722 pn533_poll_next_mod(dev);
1724 queue_delayed_work(dev->wq, &dev->poll_work,
1725 msecs_to_jiffies(PN533_POLL_INTERVAL));
1728 static int pn533_rf_complete(struct pn533 *dev, void *arg,
1729 struct sk_buff *resp)
1731 int rc = 0;
1733 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1735 if (IS_ERR(resp)) {
1736 rc = PTR_ERR(resp);
1738 nfc_dev_err(&dev->interface->dev, "%s RF setting error %d",
1739 __func__, rc);
1741 return rc;
1744 queue_delayed_work(dev->wq, &dev->poll_work,
1745 msecs_to_jiffies(PN533_POLL_INTERVAL));
1747 dev_kfree_skb(resp);
1748 return rc;
1751 static void pn533_wq_rf(struct work_struct *work)
1753 struct pn533 *dev = container_of(work, struct pn533, rf_work);
1754 struct sk_buff *skb;
1755 int rc;
1757 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1759 skb = pn533_alloc_skb(dev, 2);
1760 if (!skb)
1761 return;
1763 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD;
1764 *skb_put(skb, 1) = PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
1766 rc = pn533_send_cmd_async(dev, PN533_CMD_RF_CONFIGURATION, skb,
1767 pn533_rf_complete, NULL);
1768 if (rc < 0) {
1769 dev_kfree_skb(skb);
1770 nfc_dev_err(&dev->interface->dev, "RF setting error %d", rc);
1773 return;
1776 static int pn533_poll_complete(struct pn533 *dev, void *arg,
1777 struct sk_buff *resp)
1779 struct pn533_poll_modulations *cur_mod;
1780 int rc;
1782 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1784 if (IS_ERR(resp)) {
1785 rc = PTR_ERR(resp);
1787 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1788 __func__, rc);
1790 if (rc == -ENOENT) {
1791 if (dev->poll_mod_count != 0)
1792 return rc;
1793 else
1794 goto stop_poll;
1795 } else if (rc < 0) {
1796 nfc_dev_err(&dev->interface->dev,
1797 "Error %d when running poll", rc);
1798 goto stop_poll;
1802 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1804 if (cur_mod->len == 0) { /* Target mode */
1805 del_timer(&dev->listen_timer);
1806 rc = pn533_init_target_complete(dev, resp);
1807 goto done;
1810 /* Initiator mode */
1811 rc = pn533_start_poll_complete(dev, resp);
1812 if (!rc)
1813 goto done;
1815 if (!dev->poll_mod_count) {
1816 nfc_dev_dbg(&dev->interface->dev, "Polling has been stopped.");
1817 goto done;
1820 pn533_poll_next_mod(dev);
1821 /* Not target found, turn radio off */
1822 queue_work(dev->wq, &dev->rf_work);
1824 done:
1825 dev_kfree_skb(resp);
1826 return rc;
1828 stop_poll:
1829 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1831 pn533_poll_reset_mod_list(dev);
1832 dev->poll_protocols = 0;
1833 return rc;
1836 static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1837 struct pn533_poll_modulations *mod)
1839 struct sk_buff *skb;
1841 skb = pn533_alloc_skb(dev, mod->len);
1842 if (!skb)
1843 return NULL;
1845 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1847 return skb;
1850 static int pn533_send_poll_frame(struct pn533 *dev)
1852 struct pn533_poll_modulations *mod;
1853 struct sk_buff *skb;
1854 int rc;
1855 u8 cmd_code;
1857 mod = dev->poll_mod_active[dev->poll_mod_curr];
1859 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1860 __func__, mod->len);
1862 if (mod->len == 0) { /* Listen mode */
1863 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1864 skb = pn533_alloc_poll_tg_frame(dev);
1865 } else { /* Polling mode */
1866 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1867 skb = pn533_alloc_poll_in_frame(dev, mod);
1870 if (!skb) {
1871 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1872 return -ENOMEM;
1875 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1876 NULL);
1877 if (rc < 0) {
1878 dev_kfree_skb(skb);
1879 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1882 return rc;
1885 static void pn533_wq_poll(struct work_struct *work)
1887 struct pn533 *dev = container_of(work, struct pn533, poll_work.work);
1888 struct pn533_poll_modulations *cur_mod;
1889 int rc;
1891 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1893 nfc_dev_dbg(&dev->interface->dev,
1894 "%s cancel_listen %d modulation len %d",
1895 __func__, dev->cancel_listen, cur_mod->len);
1897 if (dev->cancel_listen == 1) {
1898 dev->cancel_listen = 0;
1899 pn533_abort_cmd(dev, GFP_ATOMIC);
1902 rc = pn533_send_poll_frame(dev);
1903 if (rc)
1904 return;
1906 if (cur_mod->len == 0 && dev->poll_mod_count > 1)
1907 mod_timer(&dev->listen_timer, jiffies + PN533_LISTEN_TIME * HZ);
1909 return;
1912 static int pn533_start_poll(struct nfc_dev *nfc_dev,
1913 u32 im_protocols, u32 tm_protocols)
1915 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1916 u8 rand_mod;
1918 nfc_dev_dbg(&dev->interface->dev,
1919 "%s: im protocols 0x%x tm protocols 0x%x",
1920 __func__, im_protocols, tm_protocols);
1922 if (dev->tgt_active_prot) {
1923 nfc_dev_err(&dev->interface->dev,
1924 "Cannot poll with a target already activated");
1925 return -EBUSY;
1928 if (dev->tgt_mode) {
1929 nfc_dev_err(&dev->interface->dev,
1930 "Cannot poll while already being activated");
1931 return -EBUSY;
1934 if (tm_protocols) {
1935 dev->gb = nfc_get_local_general_bytes(nfc_dev, &dev->gb_len);
1936 if (dev->gb == NULL)
1937 tm_protocols = 0;
1940 pn533_poll_create_mod_list(dev, im_protocols, tm_protocols);
1941 dev->poll_protocols = im_protocols;
1942 dev->listen_protocols = tm_protocols;
1944 /* Do not always start polling from the same modulation */
1945 get_random_bytes(&rand_mod, sizeof(rand_mod));
1946 rand_mod %= dev->poll_mod_count;
1947 dev->poll_mod_curr = rand_mod;
1949 return pn533_send_poll_frame(dev);
1952 static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1954 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1956 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1958 del_timer(&dev->listen_timer);
1960 if (!dev->poll_mod_count) {
1961 nfc_dev_dbg(&dev->interface->dev,
1962 "Polling operation was not running");
1963 return;
1966 pn533_abort_cmd(dev, GFP_KERNEL);
1967 flush_delayed_work(&dev->poll_work);
1968 pn533_poll_reset_mod_list(dev);
1971 static int pn533_activate_target_nfcdep(struct pn533 *dev)
1973 struct pn533_cmd_activate_response *rsp;
1974 u16 gt_len;
1975 int rc;
1977 struct sk_buff *skb;
1978 struct sk_buff *resp;
1980 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1982 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1983 if (!skb)
1984 return -ENOMEM;
1986 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1987 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1989 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1990 if (IS_ERR(resp))
1991 return PTR_ERR(resp);
1993 rsp = (struct pn533_cmd_activate_response *)resp->data;
1994 rc = rsp->status & PN533_CMD_RET_MASK;
1995 if (rc != PN533_CMD_RET_SUCCESS) {
1996 nfc_dev_err(&dev->interface->dev,
1997 "Target activation failed (error 0x%x)", rc);
1998 dev_kfree_skb(resp);
1999 return -EIO;
2002 /* ATR_RES general bytes are located at offset 16 */
2003 gt_len = resp->len - 16;
2004 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
2006 dev_kfree_skb(resp);
2007 return rc;
2010 static int pn533_activate_target(struct nfc_dev *nfc_dev,
2011 struct nfc_target *target, u32 protocol)
2013 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2014 int rc;
2016 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
2017 protocol);
2019 if (dev->poll_mod_count) {
2020 nfc_dev_err(&dev->interface->dev,
2021 "Cannot activate while polling");
2022 return -EBUSY;
2025 if (dev->tgt_active_prot) {
2026 nfc_dev_err(&dev->interface->dev,
2027 "There is already an active target");
2028 return -EBUSY;
2031 if (!dev->tgt_available_prots) {
2032 nfc_dev_err(&dev->interface->dev,
2033 "There is no available target to activate");
2034 return -EINVAL;
2037 if (!(dev->tgt_available_prots & (1 << protocol))) {
2038 nfc_dev_err(&dev->interface->dev,
2039 "Target doesn't support requested proto %u",
2040 protocol);
2041 return -EINVAL;
2044 if (protocol == NFC_PROTO_NFC_DEP) {
2045 rc = pn533_activate_target_nfcdep(dev);
2046 if (rc) {
2047 nfc_dev_err(&dev->interface->dev,
2048 "Activating target with DEP failed %d", rc);
2049 return rc;
2053 dev->tgt_active_prot = protocol;
2054 dev->tgt_available_prots = 0;
2056 return 0;
2059 static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
2060 struct nfc_target *target)
2062 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2064 struct sk_buff *skb;
2065 struct sk_buff *resp;
2067 int rc;
2069 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2071 if (!dev->tgt_active_prot) {
2072 nfc_dev_err(&dev->interface->dev, "There is no active target");
2073 return;
2076 dev->tgt_active_prot = 0;
2077 skb_queue_purge(&dev->resp_q);
2079 skb = pn533_alloc_skb(dev, sizeof(u8));
2080 if (!skb)
2081 return;
2083 *skb_put(skb, 1) = 1; /* TG*/
2085 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
2086 if (IS_ERR(resp))
2087 return;
2089 rc = resp->data[0] & PN533_CMD_RET_MASK;
2090 if (rc != PN533_CMD_RET_SUCCESS)
2091 nfc_dev_err(&dev->interface->dev,
2092 "Error 0x%x when releasing the target", rc);
2094 dev_kfree_skb(resp);
2095 return;
2099 static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
2100 struct sk_buff *resp)
2102 struct pn533_cmd_jump_dep_response *rsp;
2103 u8 target_gt_len;
2104 int rc;
2105 u8 active = *(u8 *)arg;
2107 kfree(arg);
2109 if (IS_ERR(resp))
2110 return PTR_ERR(resp);
2112 if (dev->tgt_available_prots &&
2113 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
2114 nfc_dev_err(&dev->interface->dev,
2115 "The target does not support DEP");
2116 rc = -EINVAL;
2117 goto error;
2120 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
2122 rc = rsp->status & PN533_CMD_RET_MASK;
2123 if (rc != PN533_CMD_RET_SUCCESS) {
2124 nfc_dev_err(&dev->interface->dev,
2125 "Bringing DEP link up failed (error 0x%x)", rc);
2126 goto error;
2129 if (!dev->tgt_available_prots) {
2130 struct nfc_target nfc_target;
2132 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
2134 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
2135 nfc_target.nfcid1_len = 10;
2136 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
2137 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
2138 if (rc)
2139 goto error;
2141 dev->tgt_available_prots = 0;
2144 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
2146 /* ATR_RES general bytes are located at offset 17 */
2147 target_gt_len = resp->len - 17;
2148 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
2149 rsp->gt, target_gt_len);
2150 if (rc == 0)
2151 rc = nfc_dep_link_is_up(dev->nfc_dev,
2152 dev->nfc_dev->targets[0].idx,
2153 !active, NFC_RF_INITIATOR);
2155 error:
2156 dev_kfree_skb(resp);
2157 return rc;
2160 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf);
2161 #define PASSIVE_DATA_LEN 5
2162 static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
2163 u8 comm_mode, u8 *gb, size_t gb_len)
2165 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2166 struct sk_buff *skb;
2167 int rc, skb_len;
2168 u8 *next, *arg, nfcid3[NFC_NFCID3_MAXSIZE];
2170 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
2172 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2174 if (dev->poll_mod_count) {
2175 nfc_dev_err(&dev->interface->dev,
2176 "Cannot bring the DEP link up while polling");
2177 return -EBUSY;
2180 if (dev->tgt_active_prot) {
2181 nfc_dev_err(&dev->interface->dev,
2182 "There is already an active target");
2183 return -EBUSY;
2186 skb_len = 3 + gb_len; /* ActPass + BR + Next */
2187 skb_len += PASSIVE_DATA_LEN;
2189 /* NFCID3 */
2190 skb_len += NFC_NFCID3_MAXSIZE;
2191 if (target && !target->nfcid2_len) {
2192 nfcid3[0] = 0x1;
2193 nfcid3[1] = 0xfe;
2194 get_random_bytes(nfcid3 + 2, 6);
2197 skb = pn533_alloc_skb(dev, skb_len);
2198 if (!skb)
2199 return -ENOMEM;
2201 *skb_put(skb, 1) = !comm_mode; /* ActPass */
2202 *skb_put(skb, 1) = 0x02; /* 424 kbps */
2204 next = skb_put(skb, 1); /* Next */
2205 *next = 0;
2207 /* Copy passive data */
2208 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data, PASSIVE_DATA_LEN);
2209 *next |= 1;
2211 /* Copy NFCID3 (which is NFCID2 from SENSF_RES) */
2212 if (target && target->nfcid2_len)
2213 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), target->nfcid2,
2214 target->nfcid2_len);
2215 else
2216 memcpy(skb_put(skb, NFC_NFCID3_MAXSIZE), nfcid3,
2217 NFC_NFCID3_MAXSIZE);
2218 *next |= 2;
2220 if (gb != NULL && gb_len > 0) {
2221 memcpy(skb_put(skb, gb_len), gb, gb_len);
2222 *next |= 4; /* We have some Gi */
2223 } else {
2224 *next = 0;
2227 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2228 if (!arg) {
2229 dev_kfree_skb(skb);
2230 return -ENOMEM;
2233 *arg = !comm_mode;
2235 pn533_rf_field(dev->nfc_dev, 0);
2237 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
2238 pn533_in_dep_link_up_complete, arg);
2240 if (rc < 0) {
2241 dev_kfree_skb(skb);
2242 kfree(arg);
2245 return rc;
2248 static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
2250 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2252 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2254 pn533_poll_reset_mod_list(dev);
2256 if (dev->tgt_mode || dev->tgt_active_prot)
2257 pn533_abort_cmd(dev, GFP_KERNEL);
2259 dev->tgt_active_prot = 0;
2260 dev->tgt_mode = 0;
2262 skb_queue_purge(&dev->resp_q);
2264 return 0;
2267 struct pn533_data_exchange_arg {
2268 data_exchange_cb_t cb;
2269 void *cb_context;
2272 static struct sk_buff *pn533_build_response(struct pn533 *dev)
2274 struct sk_buff *skb, *tmp, *t;
2275 unsigned int skb_len = 0, tmp_len = 0;
2277 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2279 if (skb_queue_empty(&dev->resp_q))
2280 return NULL;
2282 if (skb_queue_len(&dev->resp_q) == 1) {
2283 skb = skb_dequeue(&dev->resp_q);
2284 goto out;
2287 skb_queue_walk_safe(&dev->resp_q, tmp, t)
2288 skb_len += tmp->len;
2290 nfc_dev_dbg(&dev->interface->dev, "%s total length %d\n",
2291 __func__, skb_len);
2293 skb = alloc_skb(skb_len, GFP_KERNEL);
2294 if (skb == NULL)
2295 goto out;
2297 skb_put(skb, skb_len);
2299 skb_queue_walk_safe(&dev->resp_q, tmp, t) {
2300 memcpy(skb->data + tmp_len, tmp->data, tmp->len);
2301 tmp_len += tmp->len;
2304 out:
2305 skb_queue_purge(&dev->resp_q);
2307 return skb;
2310 static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2311 struct sk_buff *resp)
2313 struct pn533_data_exchange_arg *arg = _arg;
2314 struct sk_buff *skb;
2315 int rc = 0;
2316 u8 status, ret, mi;
2318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2320 if (IS_ERR(resp)) {
2321 rc = PTR_ERR(resp);
2322 goto _error;
2325 status = resp->data[0];
2326 ret = status & PN533_CMD_RET_MASK;
2327 mi = status & PN533_CMD_MI_MASK;
2329 skb_pull(resp, sizeof(status));
2331 if (ret != PN533_CMD_RET_SUCCESS) {
2332 nfc_dev_err(&dev->interface->dev,
2333 "Exchanging data failed (error 0x%x)", ret);
2334 rc = -EIO;
2335 goto error;
2338 skb_queue_tail(&dev->resp_q, resp);
2340 if (mi) {
2341 dev->cmd_complete_mi_arg = arg;
2342 queue_work(dev->wq, &dev->mi_rx_work);
2343 return -EINPROGRESS;
2346 /* Prepare for the next round */
2347 if (skb_queue_len(&dev->fragment_skb) > 0) {
2348 dev->cmd_complete_dep_arg = arg;
2349 queue_work(dev->wq, &dev->mi_tx_work);
2351 return -EINPROGRESS;
2354 skb = pn533_build_response(dev);
2355 if (!skb) {
2356 rc = -ENOMEM;
2357 goto error;
2360 arg->cb(arg->cb_context, skb, 0);
2361 kfree(arg);
2362 return 0;
2364 error:
2365 dev_kfree_skb(resp);
2366 _error:
2367 skb_queue_purge(&dev->resp_q);
2368 arg->cb(arg->cb_context, NULL, rc);
2369 kfree(arg);
2370 return rc;
2373 /* Split the Tx skb into small chunks */
2374 static int pn533_fill_fragment_skbs(struct pn533 *dev, struct sk_buff *skb)
2376 struct sk_buff *frag;
2377 int frag_size;
2379 do {
2380 /* Remaining size */
2381 if (skb->len > PN533_CMD_DATAFRAME_MAXLEN)
2382 frag_size = PN533_CMD_DATAFRAME_MAXLEN;
2383 else
2384 frag_size = skb->len;
2386 /* Allocate and reserve */
2387 frag = pn533_alloc_skb(dev, frag_size);
2388 if (!frag) {
2389 skb_queue_purge(&dev->fragment_skb);
2390 break;
2393 /* Reserve the TG/MI byte */
2394 skb_reserve(frag, 1);
2396 /* MI + TG */
2397 if (frag_size == PN533_CMD_DATAFRAME_MAXLEN)
2398 *skb_push(frag, sizeof(u8)) = (PN533_CMD_MI_MASK | 1);
2399 else
2400 *skb_push(frag, sizeof(u8)) = 1; /* TG */
2402 memcpy(skb_put(frag, frag_size), skb->data, frag_size);
2404 /* Reduce the size of incoming buffer */
2405 skb_pull(skb, frag_size);
2407 /* Add this to skb_queue */
2408 skb_queue_tail(&dev->fragment_skb, frag);
2410 } while (skb->len > 0);
2412 dev_kfree_skb(skb);
2414 return skb_queue_len(&dev->fragment_skb);
2417 static int pn533_transceive(struct nfc_dev *nfc_dev,
2418 struct nfc_target *target, struct sk_buff *skb,
2419 data_exchange_cb_t cb, void *cb_context)
2421 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2422 struct pn533_data_exchange_arg *arg = NULL;
2423 int rc;
2425 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2427 if (!dev->tgt_active_prot) {
2428 nfc_dev_err(&dev->interface->dev,
2429 "Can't exchange data if there is no active target");
2430 rc = -EINVAL;
2431 goto error;
2434 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2435 if (!arg) {
2436 rc = -ENOMEM;
2437 goto error;
2440 arg->cb = cb;
2441 arg->cb_context = cb_context;
2443 switch (dev->device_type) {
2444 case PN533_DEVICE_PASORI:
2445 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2446 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2447 skb,
2448 pn533_data_exchange_complete,
2449 arg);
2451 break;
2453 default:
2454 /* jumbo frame ? */
2455 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2456 rc = pn533_fill_fragment_skbs(dev, skb);
2457 if (rc <= 0)
2458 goto error;
2460 skb = skb_dequeue(&dev->fragment_skb);
2461 if (!skb) {
2462 rc = -EIO;
2463 goto error;
2465 } else {
2466 *skb_push(skb, sizeof(u8)) = 1; /* TG */
2469 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2470 skb, pn533_data_exchange_complete,
2471 arg);
2473 break;
2476 if (rc < 0) /* rc from send_async */
2477 goto error;
2479 return 0;
2481 error:
2482 kfree(arg);
2483 dev_kfree_skb(skb);
2484 return rc;
2487 static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2488 struct sk_buff *resp)
2490 u8 status;
2492 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2494 if (IS_ERR(resp))
2495 return PTR_ERR(resp);
2497 status = resp->data[0];
2499 dev_kfree_skb(resp);
2501 if (status != 0) {
2502 nfc_tm_deactivated(dev->nfc_dev);
2504 dev->tgt_mode = 0;
2506 return 0;
2509 queue_work(dev->wq, &dev->tg_work);
2511 return 0;
2514 static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2516 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2517 int rc;
2519 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2521 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2522 nfc_dev_err(&dev->interface->dev,
2523 "Data length greater than the max allowed: %d",
2524 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2525 return -ENOSYS;
2528 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2529 pn533_tm_send_complete, NULL);
2530 if (rc < 0)
2531 dev_kfree_skb(skb);
2533 return rc;
2536 static void pn533_wq_mi_recv(struct work_struct *work)
2538 struct pn533 *dev = container_of(work, struct pn533, mi_rx_work);
2540 struct sk_buff *skb;
2541 int rc;
2543 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2545 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2546 if (!skb)
2547 goto error;
2549 switch (dev->device_type) {
2550 case PN533_DEVICE_PASORI:
2551 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2552 rc = pn533_send_cmd_direct_async(dev,
2553 PN533_CMD_IN_COMM_THRU,
2554 skb,
2555 pn533_data_exchange_complete,
2556 dev->cmd_complete_mi_arg);
2558 break;
2560 default:
2561 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2563 rc = pn533_send_cmd_direct_async(dev,
2564 PN533_CMD_IN_DATA_EXCHANGE,
2565 skb,
2566 pn533_data_exchange_complete,
2567 dev->cmd_complete_mi_arg);
2569 break;
2572 if (rc == 0) /* success */
2573 return;
2575 nfc_dev_err(&dev->interface->dev,
2576 "Error %d when trying to perform data_exchange", rc);
2578 dev_kfree_skb(skb);
2579 kfree(dev->cmd_complete_mi_arg);
2581 error:
2582 pn533_send_ack(dev, GFP_KERNEL);
2583 queue_work(dev->wq, &dev->cmd_work);
2586 static void pn533_wq_mi_send(struct work_struct *work)
2588 struct pn533 *dev = container_of(work, struct pn533, mi_tx_work);
2589 struct sk_buff *skb;
2590 int rc;
2592 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2594 /* Grab the first skb in the queue */
2595 skb = skb_dequeue(&dev->fragment_skb);
2597 if (skb == NULL) { /* No more data */
2598 /* Reset the queue for future use */
2599 skb_queue_head_init(&dev->fragment_skb);
2600 goto error;
2603 switch (dev->device_type) {
2604 case PN533_DEVICE_PASORI:
2605 if (dev->tgt_active_prot != NFC_PROTO_FELICA) {
2606 rc = -EIO;
2607 break;
2610 rc = pn533_send_cmd_direct_async(dev, PN533_CMD_IN_COMM_THRU,
2611 skb,
2612 pn533_data_exchange_complete,
2613 dev->cmd_complete_dep_arg);
2615 break;
2617 default:
2618 /* Still some fragments? */
2619 rc = pn533_send_cmd_direct_async(dev,PN533_CMD_IN_DATA_EXCHANGE,
2620 skb,
2621 pn533_data_exchange_complete,
2622 dev->cmd_complete_dep_arg);
2624 break;
2627 if (rc == 0) /* success */
2628 return;
2630 nfc_dev_err(&dev->interface->dev,
2631 "Error %d when trying to perform data_exchange", rc);
2633 dev_kfree_skb(skb);
2634 kfree(dev->cmd_complete_dep_arg);
2636 error:
2637 pn533_send_ack(dev, GFP_KERNEL);
2638 queue_work(dev->wq, &dev->cmd_work);
2641 static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2642 u8 cfgdata_len)
2644 struct sk_buff *skb;
2645 struct sk_buff *resp;
2647 int skb_len;
2649 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2651 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2653 skb = pn533_alloc_skb(dev, skb_len);
2654 if (!skb)
2655 return -ENOMEM;
2657 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2658 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2660 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2661 if (IS_ERR(resp))
2662 return PTR_ERR(resp);
2664 dev_kfree_skb(resp);
2665 return 0;
2668 static int pn533_get_firmware_version(struct pn533 *dev,
2669 struct pn533_fw_version *fv)
2671 struct sk_buff *skb;
2672 struct sk_buff *resp;
2674 skb = pn533_alloc_skb(dev, 0);
2675 if (!skb)
2676 return -ENOMEM;
2678 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2679 if (IS_ERR(resp))
2680 return PTR_ERR(resp);
2682 fv->ic = resp->data[0];
2683 fv->ver = resp->data[1];
2684 fv->rev = resp->data[2];
2685 fv->support = resp->data[3];
2687 dev_kfree_skb(resp);
2688 return 0;
2691 static int pn533_pasori_fw_reset(struct pn533 *dev)
2693 struct sk_buff *skb;
2694 struct sk_buff *resp;
2696 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2698 skb = pn533_alloc_skb(dev, sizeof(u8));
2699 if (!skb)
2700 return -ENOMEM;
2702 *skb_put(skb, sizeof(u8)) = 0x1;
2704 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2705 if (IS_ERR(resp))
2706 return PTR_ERR(resp);
2708 dev_kfree_skb(resp);
2710 return 0;
2713 struct pn533_acr122_poweron_rdr_arg {
2714 int rc;
2715 struct completion done;
2718 static void pn533_acr122_poweron_rdr_resp(struct urb *urb)
2720 struct pn533_acr122_poweron_rdr_arg *arg = urb->context;
2722 nfc_dev_dbg(&urb->dev->dev, "%s", __func__);
2724 print_hex_dump_debug("ACR122 RX: ", DUMP_PREFIX_NONE, 16, 1,
2725 urb->transfer_buffer, urb->transfer_buffer_length,
2726 false);
2728 arg->rc = urb->status;
2729 complete(&arg->done);
2732 static int pn533_acr122_poweron_rdr(struct pn533 *dev)
2734 /* Power on th reader (CCID cmd) */
2735 u8 cmd[10] = {PN533_ACR122_PC_TO_RDR_ICCPOWERON,
2736 0, 0, 0, 0, 0, 0, 3, 0, 0};
2737 u8 buf[255];
2738 int rc;
2739 void *cntx;
2740 struct pn533_acr122_poweron_rdr_arg arg;
2742 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2744 init_completion(&arg.done);
2745 cntx = dev->in_urb->context; /* backup context */
2747 dev->in_urb->transfer_buffer = buf;
2748 dev->in_urb->transfer_buffer_length = 255;
2749 dev->in_urb->complete = pn533_acr122_poweron_rdr_resp;
2750 dev->in_urb->context = &arg;
2752 dev->out_urb->transfer_buffer = cmd;
2753 dev->out_urb->transfer_buffer_length = sizeof(cmd);
2755 print_hex_dump_debug("ACR122 TX: ", DUMP_PREFIX_NONE, 16, 1,
2756 cmd, sizeof(cmd), false);
2758 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
2759 if (rc) {
2760 nfc_dev_err(&dev->interface->dev,
2761 "Reader power on cmd error %d", rc);
2762 return rc;
2765 rc = usb_submit_urb(dev->in_urb, GFP_KERNEL);
2766 if (rc) {
2767 nfc_dev_err(&dev->interface->dev,
2768 "Can't submit for reader power on cmd response %d",
2769 rc);
2770 return rc;
2773 wait_for_completion(&arg.done);
2774 dev->in_urb->context = cntx; /* restore context */
2776 return arg.rc;
2779 static int pn533_rf_field(struct nfc_dev *nfc_dev, u8 rf)
2781 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2782 u8 rf_field = !!rf;
2783 int rc;
2785 rf_field |= PN533_CFGITEM_RF_FIELD_AUTO_RFCA;
2787 rc = pn533_set_configuration(dev, PN533_CFGITEM_RF_FIELD,
2788 (u8 *)&rf_field, 1);
2789 if (rc) {
2790 nfc_dev_err(&dev->interface->dev,
2791 "Error on setting RF field");
2792 return rc;
2795 return rc;
2798 int pn533_dev_up(struct nfc_dev *nfc_dev)
2800 return pn533_rf_field(nfc_dev, 1);
2803 int pn533_dev_down(struct nfc_dev *nfc_dev)
2805 return pn533_rf_field(nfc_dev, 0);
2808 static struct nfc_ops pn533_nfc_ops = {
2809 .dev_up = pn533_dev_up,
2810 .dev_down = pn533_dev_down,
2811 .dep_link_up = pn533_dep_link_up,
2812 .dep_link_down = pn533_dep_link_down,
2813 .start_poll = pn533_start_poll,
2814 .stop_poll = pn533_stop_poll,
2815 .activate_target = pn533_activate_target,
2816 .deactivate_target = pn533_deactivate_target,
2817 .im_transceive = pn533_transceive,
2818 .tm_send = pn533_tm_send,
2821 static int pn533_setup(struct pn533 *dev)
2823 struct pn533_config_max_retries max_retries;
2824 struct pn533_config_timing timing;
2825 u8 pasori_cfg[3] = {0x08, 0x01, 0x08};
2826 int rc;
2828 switch (dev->device_type) {
2829 case PN533_DEVICE_STD:
2830 case PN533_DEVICE_PASORI:
2831 case PN533_DEVICE_ACR122U:
2832 max_retries.mx_rty_atr = 0x2;
2833 max_retries.mx_rty_psl = 0x1;
2834 max_retries.mx_rty_passive_act =
2835 PN533_CONFIG_MAX_RETRIES_NO_RETRY;
2837 timing.rfu = PN533_CONFIG_TIMING_102;
2838 timing.atr_res_timeout = PN533_CONFIG_TIMING_102;
2839 timing.dep_timeout = PN533_CONFIG_TIMING_204;
2841 break;
2843 default:
2844 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2845 dev->device_type);
2846 return -EINVAL;
2849 rc = pn533_set_configuration(dev, PN533_CFGITEM_MAX_RETRIES,
2850 (u8 *)&max_retries, sizeof(max_retries));
2851 if (rc) {
2852 nfc_dev_err(&dev->interface->dev,
2853 "Error on setting MAX_RETRIES config");
2854 return rc;
2858 rc = pn533_set_configuration(dev, PN533_CFGITEM_TIMING,
2859 (u8 *)&timing, sizeof(timing));
2860 if (rc) {
2861 nfc_dev_err(&dev->interface->dev,
2862 "Error on setting RF timings");
2863 return rc;
2866 switch (dev->device_type) {
2867 case PN533_DEVICE_STD:
2868 break;
2870 case PN533_DEVICE_PASORI:
2871 pn533_pasori_fw_reset(dev);
2873 rc = pn533_set_configuration(dev, PN533_CFGITEM_PASORI,
2874 pasori_cfg, 3);
2875 if (rc) {
2876 nfc_dev_err(&dev->interface->dev,
2877 "Error while settings PASORI config");
2878 return rc;
2881 pn533_pasori_fw_reset(dev);
2883 break;
2886 return 0;
2889 static int pn533_probe(struct usb_interface *interface,
2890 const struct usb_device_id *id)
2892 struct pn533_fw_version fw_ver;
2893 struct pn533 *dev;
2894 struct usb_host_interface *iface_desc;
2895 struct usb_endpoint_descriptor *endpoint;
2896 int in_endpoint = 0;
2897 int out_endpoint = 0;
2898 int rc = -ENOMEM;
2899 int i;
2900 u32 protocols;
2902 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2903 if (!dev)
2904 return -ENOMEM;
2906 dev->udev = usb_get_dev(interface_to_usbdev(interface));
2907 dev->interface = interface;
2908 mutex_init(&dev->cmd_lock);
2910 iface_desc = interface->cur_altsetting;
2911 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2912 endpoint = &iface_desc->endpoint[i].desc;
2914 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2915 in_endpoint = endpoint->bEndpointAddress;
2917 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2918 out_endpoint = endpoint->bEndpointAddress;
2921 if (!in_endpoint || !out_endpoint) {
2922 nfc_dev_err(&interface->dev,
2923 "Could not find bulk-in or bulk-out endpoint");
2924 rc = -ENODEV;
2925 goto error;
2928 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2929 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2931 if (!dev->in_urb || !dev->out_urb)
2932 goto error;
2934 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2935 usb_rcvbulkpipe(dev->udev, in_endpoint),
2936 NULL, 0, NULL, dev);
2937 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2938 usb_sndbulkpipe(dev->udev, out_endpoint),
2939 NULL, 0, pn533_send_complete, dev);
2941 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2942 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
2943 INIT_WORK(&dev->mi_rx_work, pn533_wq_mi_recv);
2944 INIT_WORK(&dev->mi_tx_work, pn533_wq_mi_send);
2945 INIT_WORK(&dev->tg_work, pn533_wq_tg_get_data);
2946 INIT_DELAYED_WORK(&dev->poll_work, pn533_wq_poll);
2947 INIT_WORK(&dev->rf_work, pn533_wq_rf);
2948 dev->wq = alloc_ordered_workqueue("pn533", 0);
2949 if (dev->wq == NULL)
2950 goto error;
2952 init_timer(&dev->listen_timer);
2953 dev->listen_timer.data = (unsigned long) dev;
2954 dev->listen_timer.function = pn533_listen_mode_timer;
2956 skb_queue_head_init(&dev->resp_q);
2957 skb_queue_head_init(&dev->fragment_skb);
2959 INIT_LIST_HEAD(&dev->cmd_queue);
2961 usb_set_intfdata(interface, dev);
2963 dev->ops = &pn533_std_frame_ops;
2965 dev->protocol_type = PN533_PROTO_REQ_ACK_RESP;
2966 dev->device_type = id->driver_info;
2967 switch (dev->device_type) {
2968 case PN533_DEVICE_STD:
2969 protocols = PN533_ALL_PROTOCOLS;
2970 break;
2972 case PN533_DEVICE_PASORI:
2973 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2974 break;
2976 case PN533_DEVICE_ACR122U:
2977 protocols = PN533_NO_TYPE_B_PROTOCOLS;
2978 dev->ops = &pn533_acr122_frame_ops;
2979 dev->protocol_type = PN533_PROTO_REQ_RESP,
2981 rc = pn533_acr122_poweron_rdr(dev);
2982 if (rc < 0) {
2983 nfc_dev_err(&dev->interface->dev,
2984 "Couldn't poweron the reader (error %d)",
2985 rc);
2986 goto destroy_wq;
2988 break;
2990 default:
2991 nfc_dev_err(&dev->interface->dev, "Unknown device type %d\n",
2992 dev->device_type);
2993 rc = -EINVAL;
2994 goto destroy_wq;
2997 memset(&fw_ver, 0, sizeof(fw_ver));
2998 rc = pn533_get_firmware_version(dev, &fw_ver);
2999 if (rc < 0)
3000 goto destroy_wq;
3002 nfc_dev_info(&dev->interface->dev,
3003 "NXP PN5%02X firmware ver %d.%d now attached",
3004 fw_ver.ic, fw_ver.ver, fw_ver.rev);
3007 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
3008 dev->ops->tx_header_len +
3009 PN533_CMD_DATAEXCH_HEAD_LEN,
3010 dev->ops->tx_tail_len);
3011 if (!dev->nfc_dev) {
3012 rc = -ENOMEM;
3013 goto destroy_wq;
3016 nfc_set_parent_dev(dev->nfc_dev, &interface->dev);
3017 nfc_set_drvdata(dev->nfc_dev, dev);
3019 rc = nfc_register_device(dev->nfc_dev);
3020 if (rc)
3021 goto free_nfc_dev;
3023 rc = pn533_setup(dev);
3024 if (rc)
3025 goto unregister_nfc_dev;
3027 return 0;
3029 unregister_nfc_dev:
3030 nfc_unregister_device(dev->nfc_dev);
3032 free_nfc_dev:
3033 nfc_free_device(dev->nfc_dev);
3035 destroy_wq:
3036 destroy_workqueue(dev->wq);
3037 error:
3038 usb_free_urb(dev->in_urb);
3039 usb_free_urb(dev->out_urb);
3040 usb_put_dev(dev->udev);
3041 kfree(dev);
3042 return rc;
3045 static void pn533_disconnect(struct usb_interface *interface)
3047 struct pn533 *dev;
3048 struct pn533_cmd *cmd, *n;
3050 dev = usb_get_intfdata(interface);
3051 usb_set_intfdata(interface, NULL);
3053 nfc_unregister_device(dev->nfc_dev);
3054 nfc_free_device(dev->nfc_dev);
3056 usb_kill_urb(dev->in_urb);
3057 usb_kill_urb(dev->out_urb);
3059 flush_delayed_work(&dev->poll_work);
3060 destroy_workqueue(dev->wq);
3062 skb_queue_purge(&dev->resp_q);
3064 del_timer(&dev->listen_timer);
3066 list_for_each_entry_safe(cmd, n, &dev->cmd_queue, queue) {
3067 list_del(&cmd->queue);
3068 kfree(cmd);
3071 usb_free_urb(dev->in_urb);
3072 usb_free_urb(dev->out_urb);
3073 kfree(dev);
3075 nfc_dev_info(&interface->dev, "NXP PN533 NFC device disconnected");
3078 static struct usb_driver pn533_driver = {
3079 .name = "pn533",
3080 .probe = pn533_probe,
3081 .disconnect = pn533_disconnect,
3082 .id_table = pn533_table,
3085 module_usb_driver(pn533_driver);
3087 MODULE_AUTHOR("Lauro Ramos Venancio <lauro.venancio@openbossa.org>");
3088 MODULE_AUTHOR("Aloisio Almeida Jr <aloisio.almeida@openbossa.org>");
3089 MODULE_AUTHOR("Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>");
3090 MODULE_DESCRIPTION("PN533 usb driver ver " VERSION);
3091 MODULE_VERSION(VERSION);
3092 MODULE_LICENSE("GPL");