aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
authorJohn W. Linville <linville@tuxdriver.com>2013-01-28 14:43:00 -0500
committerJohn W. Linville <linville@tuxdriver.com>2013-01-28 14:43:00 -0500
commit4205e6ef4ee747aa81930537b6035086ba5f1e28 (patch)
treeb2ebe2b4621f5f531f283cb9bf0005cd3c04ca7b /drivers/nfc
parentcef401de7be8c4e155c6746bfccf721a4fa5fab9 (diff)
parent9ebea3829fac7505e0cd2642fbd13cfa9c038831 (diff)
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-next into for-davem
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/Kconfig15
-rw-r--r--drivers/nfc/Makefile2
-rw-r--r--drivers/nfc/nfcwilink.c10
-rw-r--r--drivers/nfc/pn533.c1593
-rw-r--r--drivers/nfc/pn544/Kconfig23
-rw-r--r--drivers/nfc/pn544/Makefile5
-rw-r--r--drivers/nfc/pn544/i2c.c44
-rw-r--r--drivers/nfc/pn544/pn544.c65
8 files changed, 917 insertions, 840 deletions
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index ec857676c39f..80c728b28828 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -5,19 +5,6 @@
5menu "Near Field Communication (NFC) devices" 5menu "Near Field Communication (NFC) devices"
6 depends on NFC 6 depends on NFC
7 7
8config PN544_HCI_NFC
9 tristate "HCI PN544 NFC driver"
10 depends on I2C && NFC_HCI && NFC_SHDLC
11 select CRC_CCITT
12 default n
13 ---help---
14 NXP PN544 i2c driver.
15 This is a driver based on the SHDLC and HCI NFC kernel layers and
16 will thus not work with NXP libnfc library.
17
18 To compile this driver as a module, choose m here. The module will
19 be called pn544_hci.
20
21config NFC_PN533 8config NFC_PN533
22 tristate "NXP PN533 USB driver" 9 tristate "NXP PN533 USB driver"
23 depends on USB 10 depends on USB
@@ -39,4 +26,6 @@ config NFC_WILINK
39 Say Y here to compile support for Texas Instrument's NFC WiLink driver 26 Say Y here to compile support for Texas Instrument's NFC WiLink driver
40 into the kernel or say M to compile it as module. 27 into the kernel or say M to compile it as module.
41 28
29source "drivers/nfc/pn544/Kconfig"
30
42endmenu 31endmenu
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index 36c359043f54..574bbc04d97a 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -2,7 +2,7 @@
2# Makefile for nfc devices 2# Makefile for nfc devices
3# 3#
4 4
5obj-$(CONFIG_PN544_HCI_NFC) += pn544/ 5obj-$(CONFIG_NFC_PN544) += pn544/
6obj-$(CONFIG_NFC_PN533) += pn533.o 6obj-$(CONFIG_NFC_PN533) += pn533.o
7obj-$(CONFIG_NFC_WILINK) += nfcwilink.o 7obj-$(CONFIG_NFC_WILINK) += nfcwilink.o
8 8
diff --git a/drivers/nfc/nfcwilink.c b/drivers/nfc/nfcwilink.c
index 50b1ee41afc6..3b731acbc408 100644
--- a/drivers/nfc/nfcwilink.c
+++ b/drivers/nfc/nfcwilink.c
@@ -526,7 +526,7 @@ static int nfcwilink_probe(struct platform_device *pdev)
526 526
527 nfc_dev_dbg(&pdev->dev, "probe entry"); 527 nfc_dev_dbg(&pdev->dev, "probe entry");
528 528
529 drv = kzalloc(sizeof(struct nfcwilink), GFP_KERNEL); 529 drv = devm_kzalloc(&pdev->dev, sizeof(struct nfcwilink), GFP_KERNEL);
530 if (!drv) { 530 if (!drv) {
531 rc = -ENOMEM; 531 rc = -ENOMEM;
532 goto exit; 532 goto exit;
@@ -542,12 +542,13 @@ static int nfcwilink_probe(struct platform_device *pdev)
542 542
543 drv->ndev = nci_allocate_device(&nfcwilink_ops, 543 drv->ndev = nci_allocate_device(&nfcwilink_ops,
544 protocols, 544 protocols,
545 NFC_SE_NONE,
545 NFCWILINK_HDR_LEN, 546 NFCWILINK_HDR_LEN,
546 0); 547 0);
547 if (!drv->ndev) { 548 if (!drv->ndev) {
548 nfc_dev_err(&pdev->dev, "nci_allocate_device failed"); 549 nfc_dev_err(&pdev->dev, "nci_allocate_device failed");
549 rc = -ENOMEM; 550 rc = -ENOMEM;
550 goto free_exit; 551 goto exit;
551 } 552 }
552 553
553 nci_set_parent_dev(drv->ndev, &pdev->dev); 554 nci_set_parent_dev(drv->ndev, &pdev->dev);
@@ -566,9 +567,6 @@ static int nfcwilink_probe(struct platform_device *pdev)
566free_dev_exit: 567free_dev_exit:
567 nci_free_device(drv->ndev); 568 nci_free_device(drv->ndev);
568 569
569free_exit:
570 kfree(drv);
571
572exit: 570exit:
573 return rc; 571 return rc;
574} 572}
@@ -588,8 +586,6 @@ static int nfcwilink_remove(struct platform_device *pdev)
588 nci_unregister_device(ndev); 586 nci_unregister_device(ndev);
589 nci_free_device(ndev); 587 nci_free_device(ndev);
590 588
591 kfree(drv);
592
593 dev_set_drvdata(&pdev->dev, NULL); 589 dev_set_drvdata(&pdev->dev, NULL);
594 590
595 return 0; 591 return 0;
diff --git a/drivers/nfc/pn533.c b/drivers/nfc/pn533.c
index ada681b01a17..f696318cfb51 100644
--- a/drivers/nfc/pn533.c
+++ b/drivers/nfc/pn533.c
@@ -41,11 +41,6 @@
41#define SONY_VENDOR_ID 0x054c 41#define SONY_VENDOR_ID 0x054c
42#define PASORI_PRODUCT_ID 0x02e1 42#define PASORI_PRODUCT_ID 0x02e1
43 43
44#define PN533_QUIRKS_TYPE_A BIT(0)
45#define PN533_QUIRKS_TYPE_F BIT(1)
46#define PN533_QUIRKS_DEP BIT(2)
47#define PN533_QUIRKS_RAW_EXCHANGE BIT(3)
48
49#define PN533_DEVICE_STD 0x1 44#define PN533_DEVICE_STD 0x1
50#define PN533_DEVICE_PASORI 0x2 45#define PN533_DEVICE_PASORI 0x2
51 46
@@ -84,14 +79,18 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
84#define PN533_LISTEN_TIME 2 79#define PN533_LISTEN_TIME 2
85 80
86/* frame definitions */ 81/* frame definitions */
87#define PN533_NORMAL_FRAME_MAX_LEN 262 /* 6 (PREAMBLE, SOF, LEN, LCS, TFI) 82#define PN533_FRAME_HEADER_LEN (sizeof(struct pn533_frame) \
88 254 (DATA) 83 + 2) /* data[0] TFI, data[1] CC */
89 2 (DCS, postamble) */ 84#define PN533_FRAME_TAIL_LEN 2 /* data[len] DCS, data[len + 1] postamble*/
90 85
91#define PN533_FRAME_TAIL_SIZE 2 86/*
92#define PN533_FRAME_SIZE(f) (sizeof(struct pn533_frame) + f->datalen + \ 87 * Max extended frame payload len, excluding TFI and CC
93 PN533_FRAME_TAIL_SIZE) 88 * which are already in PN533_FRAME_HEADER_LEN.
94#define PN533_FRAME_ACK_SIZE (sizeof(struct pn533_frame) + 1) 89 */
90#define PN533_FRAME_MAX_PAYLOAD_LEN 263
91
92#define PN533_FRAME_ACK_SIZE 6 /* Preamble (1), SoPC (2), ACK Code (2),
93 Postamble (1) */
95#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen]) 94#define PN533_FRAME_CHECKSUM(f) (f->data[f->datalen])
96#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1]) 95#define PN533_FRAME_POSTAMBLE(f) (f->data[f->datalen + 1])
97 96
@@ -105,8 +104,6 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
105 104
106/* PN533 Commands */ 105/* PN533 Commands */
107#define PN533_FRAME_CMD(f) (f->data[1]) 106#define PN533_FRAME_CMD(f) (f->data[1])
108#define PN533_FRAME_CMD_PARAMS_PTR(f) (&f->data[2])
109#define PN533_FRAME_CMD_PARAMS_LEN(f) (f->datalen - 2)
110 107
111#define PN533_CMD_GET_FIRMWARE_VERSION 0x02 108#define PN533_CMD_GET_FIRMWARE_VERSION 0x02
112#define PN533_CMD_RF_CONFIGURATION 0x32 109#define PN533_CMD_RF_CONFIGURATION 0x32
@@ -120,6 +117,7 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
120#define PN533_CMD_TG_INIT_AS_TARGET 0x8c 117#define PN533_CMD_TG_INIT_AS_TARGET 0x8c
121#define PN533_CMD_TG_GET_DATA 0x86 118#define PN533_CMD_TG_GET_DATA 0x86
122#define PN533_CMD_TG_SET_DATA 0x8e 119#define PN533_CMD_TG_SET_DATA 0x8e
120#define PN533_CMD_UNDEF 0xff
123 121
124#define PN533_CMD_RESPONSE(cmd) (cmd + 1) 122#define PN533_CMD_RESPONSE(cmd) (cmd + 1)
125 123
@@ -128,13 +126,12 @@ MODULE_DEVICE_TABLE(usb, pn533_table);
128#define PN533_CMD_MI_MASK 0x40 126#define PN533_CMD_MI_MASK 0x40
129#define PN533_CMD_RET_SUCCESS 0x00 127#define PN533_CMD_RET_SUCCESS 0x00
130 128
131/* PN533 status codes */
132#define PN533_STATUS_TARGET_RELEASED 0x29
133
134struct pn533; 129struct pn533;
135 130
136typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, 131typedef int (*pn533_cmd_complete_t) (struct pn533 *dev, void *arg, int status);
137 u8 *params, int params_len); 132
133typedef int (*pn533_send_async_complete_t) (struct pn533 *dev, void *arg,
134 struct sk_buff *resp);
138 135
139/* structs for pn533 commands */ 136/* structs for pn533 commands */
140 137
@@ -282,11 +279,6 @@ const struct pn533_poll_modulations poll_mod[] = {
282 279
283/* PN533_CMD_IN_ATR */ 280/* PN533_CMD_IN_ATR */
284 281
285struct pn533_cmd_activate_param {
286 u8 tg;
287 u8 next;
288} __packed;
289
290struct pn533_cmd_activate_response { 282struct pn533_cmd_activate_response {
291 u8 status; 283 u8 status;
292 u8 nfcid3t[10]; 284 u8 nfcid3t[10];
@@ -299,14 +291,6 @@ struct pn533_cmd_activate_response {
299 u8 gt[]; 291 u8 gt[];
300} __packed; 292} __packed;
301 293
302/* PN533_CMD_IN_JUMP_FOR_DEP */
303struct pn533_cmd_jump_dep {
304 u8 active;
305 u8 baud;
306 u8 next;
307 u8 data[];
308} __packed;
309
310struct pn533_cmd_jump_dep_response { 294struct pn533_cmd_jump_dep_response {
311 u8 status; 295 u8 status;
312 u8 tg; 296 u8 tg;
@@ -329,32 +313,13 @@ struct pn533_cmd_jump_dep_response {
329#define PN533_INIT_TARGET_RESP_ACTIVE 0x1 313#define PN533_INIT_TARGET_RESP_ACTIVE 0x1
330#define PN533_INIT_TARGET_RESP_DEP 0x4 314#define PN533_INIT_TARGET_RESP_DEP 0x4
331 315
332struct pn533_cmd_init_target {
333 u8 mode;
334 u8 mifare[6];
335 u8 felica[18];
336 u8 nfcid3[10];
337 u8 gb_len;
338 u8 gb[];
339} __packed;
340
341struct pn533_cmd_init_target_response {
342 u8 mode;
343 u8 cmd[];
344} __packed;
345
346struct pn533 { 316struct pn533 {
347 struct usb_device *udev; 317 struct usb_device *udev;
348 struct usb_interface *interface; 318 struct usb_interface *interface;
349 struct nfc_dev *nfc_dev; 319 struct nfc_dev *nfc_dev;
350 320
351 struct urb *out_urb; 321 struct urb *out_urb;
352 int out_maxlen;
353 struct pn533_frame *out_frame;
354
355 struct urb *in_urb; 322 struct urb *in_urb;
356 int in_maxlen;
357 struct pn533_frame *in_frame;
358 323
359 struct sk_buff_head resp_q; 324 struct sk_buff_head resp_q;
360 325
@@ -365,12 +330,12 @@ struct pn533 {
365 struct work_struct mi_work; 330 struct work_struct mi_work;
366 struct work_struct tg_work; 331 struct work_struct tg_work;
367 struct timer_list listen_timer; 332 struct timer_list listen_timer;
368 struct pn533_frame *wq_in_frame;
369 int wq_in_error; 333 int wq_in_error;
370 int cancel_listen; 334 int cancel_listen;
371 335
372 pn533_cmd_complete_t cmd_complete; 336 pn533_cmd_complete_t cmd_complete;
373 void *cmd_complete_arg; 337 void *cmd_complete_arg;
338 void *cmd_complete_mi_arg;
374 struct mutex cmd_lock; 339 struct mutex cmd_lock;
375 u8 cmd; 340 u8 cmd;
376 341
@@ -391,16 +356,17 @@ struct pn533 {
391 356
392 struct list_head cmd_queue; 357 struct list_head cmd_queue;
393 u8 cmd_pending; 358 u8 cmd_pending;
359
360 struct pn533_frame_ops *ops;
394}; 361};
395 362
396struct pn533_cmd { 363struct pn533_cmd {
397 struct list_head queue; 364 struct list_head queue;
398 struct pn533_frame *out_frame; 365 u8 cmd_code;
399 struct pn533_frame *in_frame; 366 struct sk_buff *req;
400 int in_frame_len; 367 struct sk_buff *resp;
401 pn533_cmd_complete_t cmd_complete; 368 int resp_len;
402 void *arg; 369 void *arg;
403 gfp_t flags;
404}; 370};
405 371
406struct pn533_frame { 372struct pn533_frame {
@@ -411,6 +377,22 @@ struct pn533_frame {
411 u8 data[]; 377 u8 data[];
412} __packed; 378} __packed;
413 379
380struct pn533_frame_ops {
381 void (*tx_frame_init)(void *frame, u8 cmd_code);
382 void (*tx_frame_finish)(void *frame);
383 void (*tx_update_payload_len)(void *frame, int len);
384 int tx_header_len;
385 int tx_tail_len;
386
387 bool (*rx_is_frame_valid)(void *frame);
388 int (*rx_frame_size)(void *frame);
389 int rx_header_len;
390 int rx_tail_len;
391
392 int max_payload_len;
393 u8 (*get_cmd_code)(void *frame);
394};
395
414/* The rule: value + checksum = 0 */ 396/* The rule: value + checksum = 0 */
415static inline u8 pn533_checksum(u8 value) 397static inline u8 pn533_checksum(u8 value)
416{ 398{
@@ -429,37 +411,21 @@ static u8 pn533_data_checksum(u8 *data, int datalen)
429 return pn533_checksum(sum); 411 return pn533_checksum(sum);
430} 412}
431 413
432/** 414static void pn533_tx_frame_init(void *_frame, u8 cmd_code)
433 * pn533_tx_frame_ack - create a ack frame
434 * @frame: The frame to be set as ack
435 *
436 * Ack is different type of standard frame. As a standard frame, it has
437 * preamble and start_frame. However the checksum of this frame must fail,
438 * i.e. datalen + datalen_checksum must NOT be zero. When the checksum test
439 * fails and datalen = 0 and datalen_checksum = 0xFF, the frame is a ack.
440 * After datalen_checksum field, the postamble is placed.
441 */
442static void pn533_tx_frame_ack(struct pn533_frame *frame)
443{ 415{
444 frame->preamble = 0; 416 struct pn533_frame *frame = _frame;
445 frame->start_frame = cpu_to_be16(PN533_SOF);
446 frame->datalen = 0;
447 frame->datalen_checksum = 0xFF;
448 /* data[0] is used as postamble */
449 frame->data[0] = 0;
450}
451 417
452static void pn533_tx_frame_init(struct pn533_frame *frame, u8 cmd)
453{
454 frame->preamble = 0; 418 frame->preamble = 0;
455 frame->start_frame = cpu_to_be16(PN533_SOF); 419 frame->start_frame = cpu_to_be16(PN533_SOF);
456 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT; 420 PN533_FRAME_IDENTIFIER(frame) = PN533_DIR_OUT;
457 PN533_FRAME_CMD(frame) = cmd; 421 PN533_FRAME_CMD(frame) = cmd_code;
458 frame->datalen = 2; 422 frame->datalen = 2;
459} 423}
460 424
461static void pn533_tx_frame_finish(struct pn533_frame *frame) 425static void pn533_tx_frame_finish(void *_frame)
462{ 426{
427 struct pn533_frame *frame = _frame;
428
463 frame->datalen_checksum = pn533_checksum(frame->datalen); 429 frame->datalen_checksum = pn533_checksum(frame->datalen);
464 430
465 PN533_FRAME_CHECKSUM(frame) = 431 PN533_FRAME_CHECKSUM(frame) =
@@ -468,9 +434,17 @@ static void pn533_tx_frame_finish(struct pn533_frame *frame)
468 PN533_FRAME_POSTAMBLE(frame) = 0; 434 PN533_FRAME_POSTAMBLE(frame) = 0;
469} 435}
470 436
471static bool pn533_rx_frame_is_valid(struct pn533_frame *frame) 437static void pn533_tx_update_payload_len(void *_frame, int len)
438{
439 struct pn533_frame *frame = _frame;
440
441 frame->datalen += len;
442}
443
444static bool pn533_rx_frame_is_valid(void *_frame)
472{ 445{
473 u8 checksum; 446 u8 checksum;
447 struct pn533_frame *frame = _frame;
474 448
475 if (frame->start_frame != cpu_to_be16(PN533_SOF)) 449 if (frame->start_frame != cpu_to_be16(PN533_SOF))
476 return false; 450 return false;
@@ -497,28 +471,48 @@ static bool pn533_rx_frame_is_ack(struct pn533_frame *frame)
497 return true; 471 return true;
498} 472}
499 473
500static bool pn533_rx_frame_is_cmd_response(struct pn533_frame *frame, u8 cmd) 474static inline int pn533_rx_frame_size(void *frame)
475{
476 struct pn533_frame *f = frame;
477
478 return sizeof(struct pn533_frame) + f->datalen + PN533_FRAME_TAIL_LEN;
479}
480
481static u8 pn533_get_cmd_code(void *frame)
482{
483 struct pn533_frame *f = frame;
484
485 return PN533_FRAME_CMD(f);
486}
487
488struct pn533_frame_ops pn533_std_frame_ops = {
489 .tx_frame_init = pn533_tx_frame_init,
490 .tx_frame_finish = pn533_tx_frame_finish,
491 .tx_update_payload_len = pn533_tx_update_payload_len,
492 .tx_header_len = PN533_FRAME_HEADER_LEN,
493 .tx_tail_len = PN533_FRAME_TAIL_LEN,
494
495 .rx_is_frame_valid = pn533_rx_frame_is_valid,
496 .rx_frame_size = pn533_rx_frame_size,
497 .rx_header_len = PN533_FRAME_HEADER_LEN,
498 .rx_tail_len = PN533_FRAME_TAIL_LEN,
499
500 .max_payload_len = PN533_FRAME_MAX_PAYLOAD_LEN,
501 .get_cmd_code = pn533_get_cmd_code,
502};
503
504static bool pn533_rx_frame_is_cmd_response(struct pn533 *dev, void *frame)
501{ 505{
502 return (PN533_FRAME_CMD(frame) == PN533_CMD_RESPONSE(cmd)); 506 return (dev->ops->get_cmd_code(frame) == PN533_CMD_RESPONSE(dev->cmd));
503} 507}
504 508
505 509
506static void pn533_wq_cmd_complete(struct work_struct *work) 510static void pn533_wq_cmd_complete(struct work_struct *work)
507{ 511{
508 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work); 512 struct pn533 *dev = container_of(work, struct pn533, cmd_complete_work);
509 struct pn533_frame *in_frame;
510 int rc; 513 int rc;
511 514
512 in_frame = dev->wq_in_frame; 515 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, dev->wq_in_error);
513
514 if (dev->wq_in_error)
515 rc = dev->cmd_complete(dev, dev->cmd_complete_arg, NULL,
516 dev->wq_in_error);
517 else
518 rc = dev->cmd_complete(dev, dev->cmd_complete_arg,
519 PN533_FRAME_CMD_PARAMS_PTR(in_frame),
520 PN533_FRAME_CMD_PARAMS_LEN(in_frame));
521
522 if (rc != -EINPROGRESS) 516 if (rc != -EINPROGRESS)
523 queue_work(dev->wq, &dev->cmd_work); 517 queue_work(dev->wq, &dev->cmd_work);
524} 518}
@@ -526,46 +520,47 @@ static void pn533_wq_cmd_complete(struct work_struct *work)
526static void pn533_recv_response(struct urb *urb) 520static void pn533_recv_response(struct urb *urb)
527{ 521{
528 struct pn533 *dev = urb->context; 522 struct pn533 *dev = urb->context;
529 struct pn533_frame *in_frame; 523 u8 *in_frame;
530
531 dev->wq_in_frame = NULL;
532 524
533 switch (urb->status) { 525 switch (urb->status) {
534 case 0: 526 case 0:
535 /* success */ 527 break; /* success */
536 break;
537 case -ECONNRESET: 528 case -ECONNRESET:
538 case -ENOENT: 529 case -ENOENT:
539 case -ESHUTDOWN: 530 nfc_dev_dbg(&dev->interface->dev,
540 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" 531 "The urb has been canceled (status %d)",
541 " status: %d", urb->status); 532 urb->status);
542 dev->wq_in_error = urb->status; 533 dev->wq_in_error = urb->status;
543 goto sched_wq; 534 goto sched_wq;
535 break;
536 case -ESHUTDOWN:
544 default: 537 default:
545 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:" 538 nfc_dev_err(&dev->interface->dev,
546 " %d", urb->status); 539 "Urb failure (status %d)", urb->status);
547 dev->wq_in_error = urb->status; 540 dev->wq_in_error = urb->status;
548 goto sched_wq; 541 goto sched_wq;
549 } 542 }
550 543
551 in_frame = dev->in_urb->transfer_buffer; 544 in_frame = dev->in_urb->transfer_buffer;
552 545
553 if (!pn533_rx_frame_is_valid(in_frame)) { 546 nfc_dev_dbg(&dev->interface->dev, "Received a frame.");
547 print_hex_dump(KERN_DEBUG, "PN533 RX: ", DUMP_PREFIX_NONE, 16, 1,
548 in_frame, dev->ops->rx_frame_size(in_frame), false);
549
550 if (!dev->ops->rx_is_frame_valid(in_frame)) {
554 nfc_dev_err(&dev->interface->dev, "Received an invalid frame"); 551 nfc_dev_err(&dev->interface->dev, "Received an invalid frame");
555 dev->wq_in_error = -EIO; 552 dev->wq_in_error = -EIO;
556 goto sched_wq; 553 goto sched_wq;
557 } 554 }
558 555
559 if (!pn533_rx_frame_is_cmd_response(in_frame, dev->cmd)) { 556 if (!pn533_rx_frame_is_cmd_response(dev, in_frame)) {
560 nfc_dev_err(&dev->interface->dev, "The received frame is not " 557 nfc_dev_err(&dev->interface->dev,
561 "response to the last command"); 558 "It it not the response to the last command");
562 dev->wq_in_error = -EIO; 559 dev->wq_in_error = -EIO;
563 goto sched_wq; 560 goto sched_wq;
564 } 561 }
565 562
566 nfc_dev_dbg(&dev->interface->dev, "Received a valid frame");
567 dev->wq_in_error = 0; 563 dev->wq_in_error = 0;
568 dev->wq_in_frame = in_frame;
569 564
570sched_wq: 565sched_wq:
571 queue_work(dev->wq, &dev->cmd_complete_work); 566 queue_work(dev->wq, &dev->cmd_complete_work);
@@ -586,18 +581,19 @@ static void pn533_recv_ack(struct urb *urb)
586 581
587 switch (urb->status) { 582 switch (urb->status) {
588 case 0: 583 case 0:
589 /* success */ 584 break; /* success */
590 break;
591 case -ECONNRESET: 585 case -ECONNRESET:
592 case -ENOENT: 586 case -ENOENT:
593 case -ESHUTDOWN: 587 nfc_dev_dbg(&dev->interface->dev,
594 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" 588 "The urb has been stopped (status %d)",
595 " status: %d", urb->status); 589 urb->status);
596 dev->wq_in_error = urb->status; 590 dev->wq_in_error = urb->status;
597 goto sched_wq; 591 goto sched_wq;
592 break;
593 case -ESHUTDOWN:
598 default: 594 default:
599 nfc_dev_err(&dev->interface->dev, "Nonzero urb status received:" 595 nfc_dev_err(&dev->interface->dev,
600 " %d", urb->status); 596 "Urb failure (status %d)", urb->status);
601 dev->wq_in_error = urb->status; 597 dev->wq_in_error = urb->status;
602 goto sched_wq; 598 goto sched_wq;
603 } 599 }
@@ -610,12 +606,10 @@ static void pn533_recv_ack(struct urb *urb)
610 goto sched_wq; 606 goto sched_wq;
611 } 607 }
612 608
613 nfc_dev_dbg(&dev->interface->dev, "Received a valid ack");
614
615 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC); 609 rc = pn533_submit_urb_for_response(dev, GFP_ATOMIC);
616 if (rc) { 610 if (rc) {
617 nfc_dev_err(&dev->interface->dev, "usb_submit_urb failed with" 611 nfc_dev_err(&dev->interface->dev,
618 " result %d", rc); 612 "usb_submit_urb failed with result %d", rc);
619 dev->wq_in_error = rc; 613 dev->wq_in_error = rc;
620 goto sched_wq; 614 goto sched_wq;
621 } 615 }
@@ -623,7 +617,6 @@ static void pn533_recv_ack(struct urb *urb)
623 return; 617 return;
624 618
625sched_wq: 619sched_wq:
626 dev->wq_in_frame = NULL;
627 queue_work(dev->wq, &dev->cmd_complete_work); 620 queue_work(dev->wq, &dev->cmd_complete_work);
628} 621}
629 622
@@ -636,47 +629,46 @@ static int pn533_submit_urb_for_ack(struct pn533 *dev, gfp_t flags)
636 629
637static int pn533_send_ack(struct pn533 *dev, gfp_t flags) 630static int pn533_send_ack(struct pn533 *dev, gfp_t flags)
638{ 631{
632 u8 ack[PN533_FRAME_ACK_SIZE] = {0x00, 0x00, 0xff, 0x00, 0xff, 0x00};
633 /* spec 7.1.1.3: Preamble, SoPC (2), ACK Code (2), Postamble */
639 int rc; 634 int rc;
640 635
641 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 636 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
642 637
643 pn533_tx_frame_ack(dev->out_frame); 638 dev->out_urb->transfer_buffer = ack;
644 639 dev->out_urb->transfer_buffer_length = sizeof(ack);
645 dev->out_urb->transfer_buffer = dev->out_frame;
646 dev->out_urb->transfer_buffer_length = PN533_FRAME_ACK_SIZE;
647 rc = usb_submit_urb(dev->out_urb, flags); 640 rc = usb_submit_urb(dev->out_urb, flags);
648 641
649 return rc; 642 return rc;
650} 643}
651 644
652static int __pn533_send_cmd_frame_async(struct pn533 *dev, 645static int __pn533_send_frame_async(struct pn533 *dev,
653 struct pn533_frame *out_frame, 646 struct sk_buff *out,
654 struct pn533_frame *in_frame, 647 struct sk_buff *in,
655 int in_frame_len, 648 int in_len,
656 pn533_cmd_complete_t cmd_complete, 649 pn533_cmd_complete_t cmd_complete,
657 void *arg, gfp_t flags) 650 void *arg)
658{ 651{
659 int rc; 652 int rc;
660 653
661 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", 654 dev->cmd = dev->ops->get_cmd_code(out->data);
662 PN533_FRAME_CMD(out_frame));
663
664 dev->cmd = PN533_FRAME_CMD(out_frame);
665 dev->cmd_complete = cmd_complete; 655 dev->cmd_complete = cmd_complete;
666 dev->cmd_complete_arg = arg; 656 dev->cmd_complete_arg = arg;
667 657
668 dev->out_urb->transfer_buffer = out_frame; 658 dev->out_urb->transfer_buffer = out->data;
669 dev->out_urb->transfer_buffer_length = 659 dev->out_urb->transfer_buffer_length = out->len;
670 PN533_FRAME_SIZE(out_frame);
671 660
672 dev->in_urb->transfer_buffer = in_frame; 661 dev->in_urb->transfer_buffer = in->data;
673 dev->in_urb->transfer_buffer_length = in_frame_len; 662 dev->in_urb->transfer_buffer_length = in_len;
674 663
675 rc = usb_submit_urb(dev->out_urb, flags); 664 print_hex_dump(KERN_DEBUG, "PN533 TX: ", DUMP_PREFIX_NONE, 16, 1,
665 out->data, out->len, false);
666
667 rc = usb_submit_urb(dev->out_urb, GFP_KERNEL);
676 if (rc) 668 if (rc)
677 return rc; 669 return rc;
678 670
679 rc = pn533_submit_urb_for_ack(dev, flags); 671 rc = pn533_submit_urb_for_ack(dev, GFP_KERNEL);
680 if (rc) 672 if (rc)
681 goto error; 673 goto error;
682 674
@@ -687,146 +679,325 @@ error:
687 return rc; 679 return rc;
688} 680}
689 681
690static void pn533_wq_cmd(struct work_struct *work) 682static void pn533_build_cmd_frame(struct pn533 *dev, u8 cmd_code,
683 struct sk_buff *skb)
691{ 684{
692 struct pn533 *dev = container_of(work, struct pn533, cmd_work); 685 /* payload is already there, just update datalen */
693 struct pn533_cmd *cmd; 686 int payload_len = skb->len;
687 struct pn533_frame_ops *ops = dev->ops;
694 688
695 mutex_lock(&dev->cmd_lock);
696 689
697 if (list_empty(&dev->cmd_queue)) { 690 skb_push(skb, ops->tx_header_len);
698 dev->cmd_pending = 0; 691 skb_put(skb, ops->tx_tail_len);
699 mutex_unlock(&dev->cmd_lock);
700 return;
701 }
702 692
703 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue); 693 ops->tx_frame_init(skb->data, cmd_code);
694 ops->tx_update_payload_len(skb->data, payload_len);
695 ops->tx_frame_finish(skb->data);
696}
704 697
705 list_del(&cmd->queue); 698struct pn533_send_async_complete_arg {
699 pn533_send_async_complete_t complete_cb;
700 void *complete_cb_context;
701 struct sk_buff *resp;
702 struct sk_buff *req;
703};
706 704
707 mutex_unlock(&dev->cmd_lock); 705static int pn533_send_async_complete(struct pn533 *dev, void *_arg, int status)
706{
707 struct pn533_send_async_complete_arg *arg = _arg;
708 708
709 __pn533_send_cmd_frame_async(dev, cmd->out_frame, cmd->in_frame, 709 struct sk_buff *req = arg->req;
710 cmd->in_frame_len, cmd->cmd_complete, 710 struct sk_buff *resp = arg->resp;
711 cmd->arg, cmd->flags);
712 711
713 kfree(cmd); 712 int rc;
713
714 dev_kfree_skb(req);
715
716 if (status < 0) {
717 arg->complete_cb(dev, arg->complete_cb_context,
718 ERR_PTR(status));
719 dev_kfree_skb(resp);
720 kfree(arg);
721 return status;
722 }
723
724 skb_put(resp, dev->ops->rx_frame_size(resp->data));
725 skb_pull(resp, dev->ops->rx_header_len);
726 skb_trim(resp, resp->len - dev->ops->rx_tail_len);
727
728 rc = arg->complete_cb(dev, arg->complete_cb_context, resp);
729
730 kfree(arg);
731 return rc;
714} 732}
715 733
716static int pn533_send_cmd_frame_async(struct pn533 *dev, 734static int __pn533_send_async(struct pn533 *dev, u8 cmd_code,
717 struct pn533_frame *out_frame, 735 struct sk_buff *req, struct sk_buff *resp,
718 struct pn533_frame *in_frame, 736 int resp_len,
719 int in_frame_len, 737 pn533_send_async_complete_t complete_cb,
720 pn533_cmd_complete_t cmd_complete, 738 void *complete_cb_context)
721 void *arg, gfp_t flags)
722{ 739{
723 struct pn533_cmd *cmd; 740 struct pn533_cmd *cmd;
741 struct pn533_send_async_complete_arg *arg;
724 int rc = 0; 742 int rc = 0;
725 743
726 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 744 nfc_dev_dbg(&dev->interface->dev, "Sending command 0x%x", cmd_code);
745
746 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
747 if (!arg)
748 return -ENOMEM;
749
750 arg->complete_cb = complete_cb;
751 arg->complete_cb_context = complete_cb_context;
752 arg->resp = resp;
753 arg->req = req;
754
755 pn533_build_cmd_frame(dev, cmd_code, req);
727 756
728 mutex_lock(&dev->cmd_lock); 757 mutex_lock(&dev->cmd_lock);
729 758
730 if (!dev->cmd_pending) { 759 if (!dev->cmd_pending) {
731 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame, 760 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
732 in_frame_len, cmd_complete, 761 pn533_send_async_complete, arg);
733 arg, flags); 762 if (rc)
734 if (!rc) 763 goto error;
735 dev->cmd_pending = 1;
736 764
765 dev->cmd_pending = 1;
737 goto unlock; 766 goto unlock;
738 } 767 }
739 768
740 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command", __func__); 769 nfc_dev_dbg(&dev->interface->dev, "%s Queueing command 0x%x", __func__,
770 cmd_code);
741 771
742 cmd = kzalloc(sizeof(struct pn533_cmd), flags); 772 cmd = kzalloc(sizeof(struct pn533_cmd), GFP_KERNEL);
743 if (!cmd) { 773 if (!cmd) {
744 rc = -ENOMEM; 774 rc = -ENOMEM;
745 goto unlock; 775 goto error;
746 } 776 }
747 777
748 INIT_LIST_HEAD(&cmd->queue); 778 INIT_LIST_HEAD(&cmd->queue);
749 cmd->out_frame = out_frame; 779 cmd->cmd_code = cmd_code;
750 cmd->in_frame = in_frame; 780 cmd->req = req;
751 cmd->in_frame_len = in_frame_len; 781 cmd->resp = resp;
752 cmd->cmd_complete = cmd_complete; 782 cmd->resp_len = resp_len;
753 cmd->arg = arg; 783 cmd->arg = arg;
754 cmd->flags = flags;
755 784
756 list_add_tail(&cmd->queue, &dev->cmd_queue); 785 list_add_tail(&cmd->queue, &dev->cmd_queue);
757 786
787 goto unlock;
788
789error:
790 kfree(arg);
758unlock: 791unlock:
759 mutex_unlock(&dev->cmd_lock); 792 mutex_unlock(&dev->cmd_lock);
793 return rc;
794}
795
796static int pn533_send_data_async(struct pn533 *dev, u8 cmd_code,
797 struct sk_buff *req,
798 pn533_send_async_complete_t complete_cb,
799 void *complete_cb_context)
800{
801 struct sk_buff *resp;
802 int rc;
803 int resp_len = dev->ops->rx_header_len +
804 dev->ops->max_payload_len +
805 dev->ops->rx_tail_len;
806
807 resp = nfc_alloc_recv_skb(resp_len, GFP_KERNEL);
808 if (!resp)
809 return -ENOMEM;
810
811 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
812 complete_cb_context);
813 if (rc)
814 dev_kfree_skb(resp);
760 815
761 return rc; 816 return rc;
762} 817}
763 818
764struct pn533_sync_cmd_response { 819static int pn533_send_cmd_async(struct pn533 *dev, u8 cmd_code,
820 struct sk_buff *req,
821 pn533_send_async_complete_t complete_cb,
822 void *complete_cb_context)
823{
824 struct sk_buff *resp;
765 int rc; 825 int rc;
766 struct completion done; 826 int resp_len = dev->ops->rx_header_len +
767}; 827 dev->ops->max_payload_len +
828 dev->ops->rx_tail_len;
829
830 resp = alloc_skb(resp_len, GFP_KERNEL);
831 if (!resp)
832 return -ENOMEM;
833
834 rc = __pn533_send_async(dev, cmd_code, req, resp, resp_len, complete_cb,
835 complete_cb_context);
836 if (rc)
837 dev_kfree_skb(resp);
768 838
769static int pn533_sync_cmd_complete(struct pn533 *dev, void *_arg, 839 return rc;
770 u8 *params, int params_len) 840}
841
842/*
843 * pn533_send_cmd_direct_async
844 *
845 * The function sends a piority cmd directly to the chip omiting the cmd
846 * queue. It's intended to be used by chaining mechanism of received responses
847 * where the host has to request every single chunk of data before scheduling
848 * next cmd from the queue.
849 */
850static int pn533_send_cmd_direct_async(struct pn533 *dev, u8 cmd_code,
851 struct sk_buff *req,
852 pn533_send_async_complete_t complete_cb,
853 void *complete_cb_context)
771{ 854{
772 struct pn533_sync_cmd_response *arg = _arg; 855 struct pn533_send_async_complete_arg *arg;
856 struct sk_buff *resp;
857 int rc;
858 int resp_len = dev->ops->rx_header_len +
859 dev->ops->max_payload_len +
860 dev->ops->rx_tail_len;
773 861
774 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 862 resp = alloc_skb(resp_len, GFP_KERNEL);
863 if (!resp)
864 return -ENOMEM;
865
866 arg = kzalloc(sizeof(*arg), GFP_KERNEL);
867 if (!arg) {
868 dev_kfree_skb(resp);
869 return -ENOMEM;
870 }
775 871
776 arg->rc = 0; 872 arg->complete_cb = complete_cb;
873 arg->complete_cb_context = complete_cb_context;
874 arg->resp = resp;
875 arg->req = req;
777 876
778 if (params_len < 0) /* error */ 877 pn533_build_cmd_frame(dev, cmd_code, req);
779 arg->rc = params_len;
780 878
879 rc = __pn533_send_frame_async(dev, req, resp, resp_len,
880 pn533_send_async_complete, arg);
881 if (rc < 0) {
882 dev_kfree_skb(resp);
883 kfree(arg);
884 }
885
886 return rc;
887}
888
889static void pn533_wq_cmd(struct work_struct *work)
890{
891 struct pn533 *dev = container_of(work, struct pn533, cmd_work);
892 struct pn533_cmd *cmd;
893
894 mutex_lock(&dev->cmd_lock);
895
896 if (list_empty(&dev->cmd_queue)) {
897 dev->cmd_pending = 0;
898 mutex_unlock(&dev->cmd_lock);
899 return;
900 }
901
902 cmd = list_first_entry(&dev->cmd_queue, struct pn533_cmd, queue);
903
904 list_del(&cmd->queue);
905
906 mutex_unlock(&dev->cmd_lock);
907
908 __pn533_send_frame_async(dev, cmd->req, cmd->resp, cmd->resp_len,
909 pn533_send_async_complete, cmd->arg);
910
911 kfree(cmd);
912}
913
914struct pn533_sync_cmd_response {
915 struct sk_buff *resp;
916 struct completion done;
917};
918
919static int pn533_send_sync_complete(struct pn533 *dev, void *_arg,
920 struct sk_buff *resp)
921{
922 struct pn533_sync_cmd_response *arg = _arg;
923
924 arg->resp = resp;
781 complete(&arg->done); 925 complete(&arg->done);
782 926
783 return 0; 927 return 0;
784} 928}
785 929
786static int pn533_send_cmd_frame_sync(struct pn533 *dev, 930/* pn533_send_cmd_sync
787 struct pn533_frame *out_frame, 931 *
788 struct pn533_frame *in_frame, 932 * Please note the req parameter is freed inside the function to
789 int in_frame_len) 933 * limit a number of return value interpretations by the caller.
934 *
935 * 1. negative in case of error during TX path -> req should be freed
936 *
937 * 2. negative in case of error during RX path -> req should not be freed
938 * as it's been already freed at the begining of RX path by
939 * async_complete_cb.
940 *
941 * 3. valid pointer in case of succesfult RX path
942 *
943 * A caller has to check a return value with IS_ERR macro. If the test pass,
944 * the returned pointer is valid.
945 *
946 * */
947static struct sk_buff *pn533_send_cmd_sync(struct pn533 *dev, u8 cmd_code,
948 struct sk_buff *req)
790{ 949{
791 int rc; 950 int rc;
792 struct pn533_sync_cmd_response arg; 951 struct pn533_sync_cmd_response arg;
793 952
794 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
795
796 init_completion(&arg.done); 953 init_completion(&arg.done);
797 954
798 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, in_frame_len, 955 rc = pn533_send_cmd_async(dev, cmd_code, req,
799 pn533_sync_cmd_complete, &arg, GFP_KERNEL); 956 pn533_send_sync_complete, &arg);
800 if (rc) 957 if (rc) {
801 return rc; 958 dev_kfree_skb(req);
959 return ERR_PTR(rc);
960 }
802 961
803 wait_for_completion(&arg.done); 962 wait_for_completion(&arg.done);
804 963
805 return arg.rc; 964 return arg.resp;
806} 965}
807 966
808static void pn533_send_complete(struct urb *urb) 967static void pn533_send_complete(struct urb *urb)
809{ 968{
810 struct pn533 *dev = urb->context; 969 struct pn533 *dev = urb->context;
811 970
812 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
813
814 switch (urb->status) { 971 switch (urb->status) {
815 case 0: 972 case 0:
816 /* success */ 973 break; /* success */
817 break;
818 case -ECONNRESET: 974 case -ECONNRESET:
819 case -ENOENT: 975 case -ENOENT:
820 case -ESHUTDOWN: 976 nfc_dev_dbg(&dev->interface->dev,
821 nfc_dev_dbg(&dev->interface->dev, "Urb shutting down with" 977 "The urb has been stopped (status %d)",
822 " status: %d", urb->status); 978 urb->status);
823 break; 979 break;
980 case -ESHUTDOWN:
824 default: 981 default:
825 nfc_dev_dbg(&dev->interface->dev, "Nonzero urb status received:" 982 nfc_dev_err(&dev->interface->dev,
826 " %d", urb->status); 983 "Urb failure (status %d)", urb->status);
827 } 984 }
828} 985}
829 986
987static struct sk_buff *pn533_alloc_skb(struct pn533 *dev, unsigned int size)
988{
989 struct sk_buff *skb;
990
991 skb = alloc_skb(dev->ops->tx_header_len +
992 size +
993 dev->ops->tx_tail_len, GFP_KERNEL);
994
995 if (skb)
996 skb_reserve(skb, dev->ops->tx_header_len);
997
998 return skb;
999}
1000
830struct pn533_target_type_a { 1001struct pn533_target_type_a {
831 __be16 sens_res; 1002 __be16 sens_res;
832 u8 sel_res; 1003 u8 sel_res;
@@ -867,9 +1038,9 @@ static bool pn533_target_type_a_is_valid(struct pn533_target_type_a *type_a,
867 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res); 1038 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(type_a->sens_res);
868 1039
869 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && 1040 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
870 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || 1041 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
871 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && 1042 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
872 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) 1043 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
873 return false; 1044 return false;
874 1045
875 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */ 1046 /* Requirements 4.8.2.1, 4.8.2.3, 4.8.2.5 and 4.8.2.7 from NFC Forum */
@@ -884,7 +1055,7 @@ static int pn533_target_found_type_a(struct nfc_target *nfc_tgt, u8 *tgt_data,
884{ 1055{
885 struct pn533_target_type_a *tgt_type_a; 1056 struct pn533_target_type_a *tgt_type_a;
886 1057
887 tgt_type_a = (struct pn533_target_type_a *) tgt_data; 1058 tgt_type_a = (struct pn533_target_type_a *)tgt_data;
888 1059
889 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len)) 1060 if (!pn533_target_type_a_is_valid(tgt_type_a, tgt_data_len))
890 return -EPROTO; 1061 return -EPROTO;
@@ -942,14 +1113,13 @@ static int pn533_target_found_felica(struct nfc_target *nfc_tgt, u8 *tgt_data,
942{ 1113{
943 struct pn533_target_felica *tgt_felica; 1114 struct pn533_target_felica *tgt_felica;
944 1115
945 tgt_felica = (struct pn533_target_felica *) tgt_data; 1116 tgt_felica = (struct pn533_target_felica *)tgt_data;
946 1117
947 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len)) 1118 if (!pn533_target_felica_is_valid(tgt_felica, tgt_data_len))
948 return -EPROTO; 1119 return -EPROTO;
949 1120
950 if (tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1 && 1121 if ((tgt_felica->nfcid2[0] == PN533_FELICA_SENSF_NFCID2_DEP_B1) &&
951 tgt_felica->nfcid2[1] == 1122 (tgt_felica->nfcid2[1] == PN533_FELICA_SENSF_NFCID2_DEP_B2))
952 PN533_FELICA_SENSF_NFCID2_DEP_B2)
953 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK; 1123 nfc_tgt->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
954 else 1124 else
955 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK; 1125 nfc_tgt->supported_protocols = NFC_PROTO_FELICA_MASK;
@@ -979,9 +1149,9 @@ static bool pn533_target_jewel_is_valid(struct pn533_target_jewel *jewel,
979 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res); 1149 platconf = PN533_TYPE_A_SENS_RES_PLATCONF(jewel->sens_res);
980 1150
981 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL && 1151 if ((ssd == PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
982 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) || 1152 platconf != PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL) ||
983 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL && 1153 (ssd != PN533_TYPE_A_SENS_RES_SSD_JEWEL &&
984 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL)) 1154 platconf == PN533_TYPE_A_SENS_RES_PLATCONF_JEWEL))
985 return false; 1155 return false;
986 1156
987 return true; 1157 return true;
@@ -992,7 +1162,7 @@ static int pn533_target_found_jewel(struct nfc_target *nfc_tgt, u8 *tgt_data,
992{ 1162{
993 struct pn533_target_jewel *tgt_jewel; 1163 struct pn533_target_jewel *tgt_jewel;
994 1164
995 tgt_jewel = (struct pn533_target_jewel *) tgt_data; 1165 tgt_jewel = (struct pn533_target_jewel *)tgt_data;
996 1166
997 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len)) 1167 if (!pn533_target_jewel_is_valid(tgt_jewel, tgt_data_len))
998 return -EPROTO; 1168 return -EPROTO;
@@ -1051,7 +1221,7 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1051{ 1221{
1052 struct pn533_target_type_b *tgt_type_b; 1222 struct pn533_target_type_b *tgt_type_b;
1053 1223
1054 tgt_type_b = (struct pn533_target_type_b *) tgt_data; 1224 tgt_type_b = (struct pn533_target_type_b *)tgt_data;
1055 1225
1056 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len)) 1226 if (!pn533_target_type_b_is_valid(tgt_type_b, tgt_data_len))
1057 return -EPROTO; 1227 return -EPROTO;
@@ -1061,50 +1231,37 @@ static int pn533_target_found_type_b(struct nfc_target *nfc_tgt, u8 *tgt_data,
1061 return 0; 1231 return 0;
1062} 1232}
1063 1233
1064struct pn533_poll_response { 1234static int pn533_target_found(struct pn533 *dev, u8 tg, u8 *tgdata,
1065 u8 nbtg; 1235 int tgdata_len)
1066 u8 tg;
1067 u8 target_data[];
1068} __packed;
1069
1070static int pn533_target_found(struct pn533 *dev,
1071 struct pn533_poll_response *resp, int resp_len)
1072{ 1236{
1073 int target_data_len;
1074 struct nfc_target nfc_tgt; 1237 struct nfc_target nfc_tgt;
1075 int rc; 1238 int rc;
1076 1239
1077 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__, 1240 nfc_dev_dbg(&dev->interface->dev, "%s - modulation=%d", __func__,
1078 dev->poll_mod_curr); 1241 dev->poll_mod_curr);
1079 1242
1080 if (resp->tg != 1) 1243 if (tg != 1)
1081 return -EPROTO; 1244 return -EPROTO;
1082 1245
1083 memset(&nfc_tgt, 0, sizeof(struct nfc_target)); 1246 memset(&nfc_tgt, 0, sizeof(struct nfc_target));
1084 1247
1085 target_data_len = resp_len - sizeof(struct pn533_poll_response);
1086
1087 switch (dev->poll_mod_curr) { 1248 switch (dev->poll_mod_curr) {
1088 case PN533_POLL_MOD_106KBPS_A: 1249 case PN533_POLL_MOD_106KBPS_A:
1089 rc = pn533_target_found_type_a(&nfc_tgt, resp->target_data, 1250 rc = pn533_target_found_type_a(&nfc_tgt, tgdata, tgdata_len);
1090 target_data_len);
1091 break; 1251 break;
1092 case PN533_POLL_MOD_212KBPS_FELICA: 1252 case PN533_POLL_MOD_212KBPS_FELICA:
1093 case PN533_POLL_MOD_424KBPS_FELICA: 1253 case PN533_POLL_MOD_424KBPS_FELICA:
1094 rc = pn533_target_found_felica(&nfc_tgt, resp->target_data, 1254 rc = pn533_target_found_felica(&nfc_tgt, tgdata, tgdata_len);
1095 target_data_len);
1096 break; 1255 break;
1097 case PN533_POLL_MOD_106KBPS_JEWEL: 1256 case PN533_POLL_MOD_106KBPS_JEWEL:
1098 rc = pn533_target_found_jewel(&nfc_tgt, resp->target_data, 1257 rc = pn533_target_found_jewel(&nfc_tgt, tgdata, tgdata_len);
1099 target_data_len);
1100 break; 1258 break;
1101 case PN533_POLL_MOD_847KBPS_B: 1259 case PN533_POLL_MOD_847KBPS_B:
1102 rc = pn533_target_found_type_b(&nfc_tgt, resp->target_data, 1260 rc = pn533_target_found_type_b(&nfc_tgt, tgdata, tgdata_len);
1103 target_data_len);
1104 break; 1261 break;
1105 default: 1262 default:
1106 nfc_dev_err(&dev->interface->dev, "Unknown current poll" 1263 nfc_dev_err(&dev->interface->dev,
1107 " modulation"); 1264 "Unknown current poll modulation");
1108 return -EPROTO; 1265 return -EPROTO;
1109 } 1266 }
1110 1267
@@ -1112,13 +1269,14 @@ static int pn533_target_found(struct pn533 *dev,
1112 return rc; 1269 return rc;
1113 1270
1114 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) { 1271 if (!(nfc_tgt.supported_protocols & dev->poll_protocols)) {
1115 nfc_dev_dbg(&dev->interface->dev, "The target found does not" 1272 nfc_dev_dbg(&dev->interface->dev,
1116 " have the desired protocol"); 1273 "The Tg found doesn't have the desired protocol");
1117 return -EAGAIN; 1274 return -EAGAIN;
1118 } 1275 }
1119 1276
1120 nfc_dev_dbg(&dev->interface->dev, "Target found - supported protocols: " 1277 nfc_dev_dbg(&dev->interface->dev,
1121 "0x%x", nfc_tgt.supported_protocols); 1278 "Target found - supported protocols: 0x%x",
1279 nfc_tgt.supported_protocols);
1122 1280
1123 dev->tgt_available_prots = nfc_tgt.supported_protocols; 1281 dev->tgt_available_prots = nfc_tgt.supported_protocols;
1124 1282
@@ -1140,7 +1298,7 @@ static void pn533_poll_reset_mod_list(struct pn533 *dev)
1140static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index) 1298static void pn533_poll_add_mod(struct pn533 *dev, u8 mod_index)
1141{ 1299{
1142 dev->poll_mod_active[dev->poll_mod_count] = 1300 dev->poll_mod_active[dev->poll_mod_count] =
1143 (struct pn533_poll_modulations *) &poll_mod[mod_index]; 1301 (struct pn533_poll_modulations *)&poll_mod[mod_index];
1144 dev->poll_mod_count++; 1302 dev->poll_mod_count++;
1145} 1303}
1146 1304
@@ -1149,13 +1307,13 @@ static void pn533_poll_create_mod_list(struct pn533 *dev,
1149{ 1307{
1150 pn533_poll_reset_mod_list(dev); 1308 pn533_poll_reset_mod_list(dev);
1151 1309
1152 if (im_protocols & NFC_PROTO_MIFARE_MASK 1310 if ((im_protocols & NFC_PROTO_MIFARE_MASK) ||
1153 || im_protocols & NFC_PROTO_ISO14443_MASK 1311 (im_protocols & NFC_PROTO_ISO14443_MASK) ||
1154 || im_protocols & NFC_PROTO_NFC_DEP_MASK) 1312 (im_protocols & NFC_PROTO_NFC_DEP_MASK))
1155 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A); 1313 pn533_poll_add_mod(dev, PN533_POLL_MOD_106KBPS_A);
1156 1314
1157 if (im_protocols & NFC_PROTO_FELICA_MASK 1315 if (im_protocols & NFC_PROTO_FELICA_MASK ||
1158 || im_protocols & NFC_PROTO_NFC_DEP_MASK) { 1316 im_protocols & NFC_PROTO_NFC_DEP_MASK) {
1159 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA); 1317 pn533_poll_add_mod(dev, PN533_POLL_MOD_212KBPS_FELICA);
1160 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA); 1318 pn533_poll_add_mod(dev, PN533_POLL_MOD_424KBPS_FELICA);
1161 } 1319 }
@@ -1170,16 +1328,20 @@ static void pn533_poll_create_mod_list(struct pn533 *dev,
1170 pn533_poll_add_mod(dev, PN533_LISTEN_MOD); 1328 pn533_poll_add_mod(dev, PN533_LISTEN_MOD);
1171} 1329}
1172 1330
1173static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_len) 1331static int pn533_start_poll_complete(struct pn533 *dev, struct sk_buff *resp)
1174{ 1332{
1175 struct pn533_poll_response *resp; 1333 u8 nbtg, tg, *tgdata;
1176 int rc; 1334 int rc, tgdata_len;
1177 1335
1178 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1336 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1179 1337
1180 resp = (struct pn533_poll_response *) params; 1338 nbtg = resp->data[0];
1181 if (resp->nbtg) { 1339 tg = resp->data[1];
1182 rc = pn533_target_found(dev, resp, params_len); 1340 tgdata = &resp->data[2];
1341 tgdata_len = resp->len - 2; /* nbtg + tg */
1342
1343 if (nbtg) {
1344 rc = pn533_target_found(dev, tg, tgdata, tgdata_len);
1183 1345
1184 /* We must stop the poll after a valid target found */ 1346 /* We must stop the poll after a valid target found */
1185 if (rc == 0) { 1347 if (rc == 0) {
@@ -1191,158 +1353,134 @@ static int pn533_start_poll_complete(struct pn533 *dev, u8 *params, int params_l
1191 return -EAGAIN; 1353 return -EAGAIN;
1192} 1354}
1193 1355
1194static int pn533_init_target_frame(struct pn533_frame *frame, 1356static struct sk_buff *pn533_alloc_poll_tg_frame(struct pn533 *dev)
1195 u8 *gb, size_t gb_len)
1196{ 1357{
1197 struct pn533_cmd_init_target *cmd; 1358 struct sk_buff *skb;
1198 size_t cmd_len; 1359 u8 *felica, *nfcid3, *gb;
1360
1361 u8 *gbytes = dev->gb;
1362 size_t gbytes_len = dev->gb_len;
1363
1199 u8 felica_params[18] = {0x1, 0xfe, /* DEP */ 1364 u8 felica_params[18] = {0x1, 0xfe, /* DEP */
1200 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */ 1365 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, /* random */
1201 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 1366 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
1202 0xff, 0xff}; /* System code */ 1367 0xff, 0xff}; /* System code */
1368
1203 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */ 1369 u8 mifare_params[6] = {0x1, 0x1, /* SENS_RES */
1204 0x0, 0x0, 0x0, 1370 0x0, 0x0, 0x0,
1205 0x40}; /* SEL_RES for DEP */ 1371 0x40}; /* SEL_RES for DEP */
1206 1372
1207 cmd_len = sizeof(struct pn533_cmd_init_target) + gb_len + 1; 1373 unsigned int skb_len = 36 + /* mode (1), mifare (6),
1208 cmd = kzalloc(cmd_len, GFP_KERNEL); 1374 felica (18), nfcid3 (10), gb_len (1) */
1209 if (cmd == NULL) 1375 gbytes_len +
1210 return -ENOMEM; 1376 1; /* len Tk*/
1211 1377
1212 pn533_tx_frame_init(frame, PN533_CMD_TG_INIT_AS_TARGET); 1378 skb = pn533_alloc_skb(dev, skb_len);
1379 if (!skb)
1380 return NULL;
1213 1381
1214 /* DEP support only */ 1382 /* DEP support only */
1215 cmd->mode |= PN533_INIT_TARGET_DEP; 1383 *skb_put(skb, 1) |= PN533_INIT_TARGET_DEP;
1384
1385 /* MIFARE params */
1386 memcpy(skb_put(skb, 6), mifare_params, 6);
1216 1387
1217 /* Felica params */ 1388 /* Felica params */
1218 memcpy(cmd->felica, felica_params, 18); 1389 felica = skb_put(skb, 18);
1219 get_random_bytes(cmd->felica + 2, 6); 1390 memcpy(felica, felica_params, 18);
1391 get_random_bytes(felica + 2, 6);
1220 1392
1221 /* NFCID3 */ 1393 /* NFCID3 */
1222 memset(cmd->nfcid3, 0, 10); 1394 nfcid3 = skb_put(skb, 10);
1223 memcpy(cmd->nfcid3, cmd->felica, 8); 1395 memset(nfcid3, 0, 10);
1224 1396 memcpy(nfcid3, felica, 8);
1225 /* MIFARE params */
1226 memcpy(cmd->mifare, mifare_params, 6);
1227 1397
1228 /* General bytes */ 1398 /* General bytes */
1229 cmd->gb_len = gb_len; 1399 *skb_put(skb, 1) = gbytes_len;
1230 memcpy(cmd->gb, gb, gb_len);
1231
1232 /* Len Tk */
1233 cmd->gb[gb_len] = 0;
1234
1235 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), cmd, cmd_len);
1236 1400
1237 frame->datalen += cmd_len; 1401 gb = skb_put(skb, gbytes_len);
1402 memcpy(gb, gbytes, gbytes_len);
1238 1403
1239 pn533_tx_frame_finish(frame); 1404 /* Len Tk */
1240 1405 *skb_put(skb, 1) = 0;
1241 kfree(cmd);
1242 1406
1243 return 0; 1407 return skb;
1244} 1408}
1245 1409
1246#define PN533_CMD_DATAEXCH_HEAD_LEN (sizeof(struct pn533_frame) + 3) 1410#define PN533_CMD_DATAEXCH_HEAD_LEN 1
1247#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262 1411#define PN533_CMD_DATAEXCH_DATA_MAXLEN 262
1248static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg, 1412static int pn533_tm_get_data_complete(struct pn533 *dev, void *arg,
1249 u8 *params, int params_len) 1413 struct sk_buff *resp)
1250{ 1414{
1251 struct sk_buff *skb_resp = arg; 1415 u8 status;
1252 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data;
1253 1416
1254 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1417 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1255 1418
1256 if (params_len < 0) { 1419 if (IS_ERR(resp))
1257 nfc_dev_err(&dev->interface->dev, 1420 return PTR_ERR(resp);
1258 "Error %d when starting as a target",
1259 params_len);
1260 1421
1261 return params_len; 1422 status = resp->data[0];
1262 } 1423 skb_pull(resp, sizeof(status));
1263 1424
1264 if (params_len > 0 && params[0] != 0) { 1425 if (status != 0) {
1265 nfc_tm_deactivated(dev->nfc_dev); 1426 nfc_tm_deactivated(dev->nfc_dev);
1266
1267 dev->tgt_mode = 0; 1427 dev->tgt_mode = 0;
1268 1428 dev_kfree_skb(resp);
1269 kfree_skb(skb_resp);
1270 return 0; 1429 return 0;
1271 } 1430 }
1272 1431
1273 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame)); 1432 return nfc_tm_data_received(dev->nfc_dev, resp);
1274 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1275 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1276
1277 return nfc_tm_data_received(dev->nfc_dev, skb_resp);
1278} 1433}
1279 1434
1280static void pn533_wq_tg_get_data(struct work_struct *work) 1435static void pn533_wq_tg_get_data(struct work_struct *work)
1281{ 1436{
1282 struct pn533 *dev = container_of(work, struct pn533, tg_work); 1437 struct pn533 *dev = container_of(work, struct pn533, tg_work);
1283 struct pn533_frame *in_frame;
1284 struct sk_buff *skb_resp;
1285 size_t skb_resp_len;
1286 1438
1287 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1439 struct sk_buff *skb;
1440 int rc;
1288 1441
1289 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN + 1442 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1290 PN533_CMD_DATAEXCH_DATA_MAXLEN +
1291 PN533_FRAME_TAIL_SIZE;
1292 1443
1293 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL); 1444 skb = pn533_alloc_skb(dev, 0);
1294 if (!skb_resp) 1445 if (!skb)
1295 return; 1446 return;
1296 1447
1297 in_frame = (struct pn533_frame *)skb_resp->data; 1448 rc = pn533_send_data_async(dev, PN533_CMD_TG_GET_DATA, skb,
1449 pn533_tm_get_data_complete, NULL);
1298 1450
1299 pn533_tx_frame_init(dev->out_frame, PN533_CMD_TG_GET_DATA); 1451 if (rc < 0)
1300 pn533_tx_frame_finish(dev->out_frame); 1452 dev_kfree_skb(skb);
1301
1302 pn533_send_cmd_frame_async(dev, dev->out_frame, in_frame,
1303 skb_resp_len,
1304 pn533_tm_get_data_complete,
1305 skb_resp, GFP_KERNEL);
1306 1453
1307 return; 1454 return;
1308} 1455}
1309 1456
1310#define ATR_REQ_GB_OFFSET 17 1457#define ATR_REQ_GB_OFFSET 17
1311static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_len) 1458static int pn533_init_target_complete(struct pn533 *dev, struct sk_buff *resp)
1312{ 1459{
1313 struct pn533_cmd_init_target_response *resp; 1460 u8 mode, *cmd, comm_mode = NFC_COMM_PASSIVE, *gb;
1314 u8 frame, comm_mode = NFC_COMM_PASSIVE, *gb;
1315 size_t gb_len; 1461 size_t gb_len;
1316 int rc; 1462 int rc;
1317 1463
1318 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1464 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1319 1465
1320 if (params_len < 0) { 1466 if (resp->len < ATR_REQ_GB_OFFSET + 1)
1321 nfc_dev_err(&dev->interface->dev,
1322 "Error %d when starting as a target",
1323 params_len);
1324
1325 return params_len;
1326 }
1327
1328 if (params_len < ATR_REQ_GB_OFFSET + 1)
1329 return -EINVAL; 1467 return -EINVAL;
1330 1468
1331 resp = (struct pn533_cmd_init_target_response *) params; 1469 mode = resp->data[0];
1470 cmd = &resp->data[1];
1332 1471
1333 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x param len %d\n", 1472 nfc_dev_dbg(&dev->interface->dev, "Target mode 0x%x len %d\n",
1334 resp->mode, params_len); 1473 mode, resp->len);
1335 1474
1336 frame = resp->mode & PN533_INIT_TARGET_RESP_FRAME_MASK; 1475 if ((mode & PN533_INIT_TARGET_RESP_FRAME_MASK) ==
1337 if (frame == PN533_INIT_TARGET_RESP_ACTIVE) 1476 PN533_INIT_TARGET_RESP_ACTIVE)
1338 comm_mode = NFC_COMM_ACTIVE; 1477 comm_mode = NFC_COMM_ACTIVE;
1339 1478
1340 /* Again, only DEP */ 1479 if ((mode & PN533_INIT_TARGET_RESP_DEP) == 0) /* Only DEP supported */
1341 if ((resp->mode & PN533_INIT_TARGET_RESP_DEP) == 0)
1342 return -EOPNOTSUPP; 1480 return -EOPNOTSUPP;
1343 1481
1344 gb = resp->cmd + ATR_REQ_GB_OFFSET; 1482 gb = cmd + ATR_REQ_GB_OFFSET;
1345 gb_len = params_len - (ATR_REQ_GB_OFFSET + 1); 1483 gb_len = resp->len - (ATR_REQ_GB_OFFSET + 1);
1346 1484
1347 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK, 1485 rc = nfc_tm_activated(dev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
1348 comm_mode, gb, gb_len); 1486 comm_mode, gb, gb_len);
@@ -1353,7 +1491,6 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_
1353 } 1491 }
1354 1492
1355 dev->tgt_mode = 1; 1493 dev->tgt_mode = 1;
1356
1357 queue_work(dev->wq, &dev->tg_work); 1494 queue_work(dev->wq, &dev->tg_work);
1358 1495
1359 return 0; 1496 return 0;
@@ -1361,7 +1498,7 @@ static int pn533_init_target_complete(struct pn533 *dev, u8 *params, int params_
1361 1498
1362static void pn533_listen_mode_timer(unsigned long data) 1499static void pn533_listen_mode_timer(unsigned long data)
1363{ 1500{
1364 struct pn533 *dev = (struct pn533 *) data; 1501 struct pn533 *dev = (struct pn533 *)data;
1365 1502
1366 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout"); 1503 nfc_dev_dbg(&dev->interface->dev, "Listen mode timeout");
1367 1504
@@ -1376,88 +1513,104 @@ static void pn533_listen_mode_timer(unsigned long data)
1376} 1513}
1377 1514
1378static int pn533_poll_complete(struct pn533 *dev, void *arg, 1515static int pn533_poll_complete(struct pn533 *dev, void *arg,
1379 u8 *params, int params_len) 1516 struct sk_buff *resp)
1380{ 1517{
1381 struct pn533_poll_modulations *cur_mod; 1518 struct pn533_poll_modulations *cur_mod;
1382 int rc; 1519 int rc;
1383 1520
1384 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1521 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1385 1522
1386 if (params_len == -ENOENT) { 1523 if (IS_ERR(resp)) {
1387 if (dev->poll_mod_count != 0) 1524 rc = PTR_ERR(resp);
1388 return 0;
1389
1390 nfc_dev_err(&dev->interface->dev,
1391 "Polling operation has been stopped");
1392
1393 goto stop_poll;
1394 }
1395 1525
1396 if (params_len < 0) { 1526 nfc_dev_err(&dev->interface->dev, "%s Poll complete error %d",
1397 nfc_dev_err(&dev->interface->dev, 1527 __func__, rc);
1398 "Error %d when running poll", params_len);
1399 1528
1400 goto stop_poll; 1529 if (rc == -ENOENT) {
1530 if (dev->poll_mod_count != 0)
1531 return rc;
1532 else
1533 goto stop_poll;
1534 } else if (rc < 0) {
1535 nfc_dev_err(&dev->interface->dev,
1536 "Error %d when running poll", rc);
1537 goto stop_poll;
1538 }
1401 } 1539 }
1402 1540
1403 cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; 1541 cur_mod = dev->poll_mod_active[dev->poll_mod_curr];
1404 1542
1405 if (cur_mod->len == 0) { 1543 if (cur_mod->len == 0) { /* Target mode */
1406 del_timer(&dev->listen_timer); 1544 del_timer(&dev->listen_timer);
1407 1545 rc = pn533_init_target_complete(dev, resp);
1408 return pn533_init_target_complete(dev, params, params_len); 1546 goto done;
1409 } else {
1410 rc = pn533_start_poll_complete(dev, params, params_len);
1411 if (!rc)
1412 return rc;
1413 } 1547 }
1414 1548
1415 pn533_poll_next_mod(dev); 1549 /* Initiator mode */
1550 rc = pn533_start_poll_complete(dev, resp);
1551 if (!rc)
1552 goto done;
1416 1553
1554 pn533_poll_next_mod(dev);
1417 queue_work(dev->wq, &dev->poll_work); 1555 queue_work(dev->wq, &dev->poll_work);
1418 1556
1419 return 0; 1557done:
1558 dev_kfree_skb(resp);
1559 return rc;
1420 1560
1421stop_poll: 1561stop_poll:
1562 nfc_dev_err(&dev->interface->dev, "Polling operation has been stopped");
1563
1422 pn533_poll_reset_mod_list(dev); 1564 pn533_poll_reset_mod_list(dev);
1423 dev->poll_protocols = 0; 1565 dev->poll_protocols = 0;
1424 return 0; 1566 return rc;
1425} 1567}
1426 1568
1427static void pn533_build_poll_frame(struct pn533 *dev, 1569static struct sk_buff *pn533_alloc_poll_in_frame(struct pn533 *dev,
1428 struct pn533_frame *frame, 1570 struct pn533_poll_modulations *mod)
1429 struct pn533_poll_modulations *mod)
1430{ 1571{
1431 nfc_dev_dbg(&dev->interface->dev, "mod len %d\n", mod->len); 1572 struct sk_buff *skb;
1432 1573
1433 if (mod->len == 0) { 1574 skb = pn533_alloc_skb(dev, mod->len);
1434 /* Listen mode */ 1575 if (!skb)
1435 pn533_init_target_frame(frame, dev->gb, dev->gb_len); 1576 return NULL;
1436 } else {
1437 /* Polling mode */
1438 pn533_tx_frame_init(frame, PN533_CMD_IN_LIST_PASSIVE_TARGET);
1439 1577
1440 memcpy(PN533_FRAME_CMD_PARAMS_PTR(frame), &mod->data, mod->len); 1578 memcpy(skb_put(skb, mod->len), &mod->data, mod->len);
1441 frame->datalen += mod->len;
1442 1579
1443 pn533_tx_frame_finish(frame); 1580 return skb;
1444 }
1445} 1581}
1446 1582
1447static int pn533_send_poll_frame(struct pn533 *dev) 1583static int pn533_send_poll_frame(struct pn533 *dev)
1448{ 1584{
1449 struct pn533_poll_modulations *cur_mod; 1585 struct pn533_poll_modulations *mod;
1586 struct sk_buff *skb;
1450 int rc; 1587 int rc;
1588 u8 cmd_code;
1451 1589
1452 cur_mod = dev->poll_mod_active[dev->poll_mod_curr]; 1590 mod = dev->poll_mod_active[dev->poll_mod_curr];
1453 1591
1454 pn533_build_poll_frame(dev, dev->out_frame, cur_mod); 1592 nfc_dev_dbg(&dev->interface->dev, "%s mod len %d\n",
1593 __func__, mod->len);
1455 1594
1456 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame, 1595 if (mod->len == 0) { /* Listen mode */
1457 dev->in_maxlen, pn533_poll_complete, 1596 cmd_code = PN533_CMD_TG_INIT_AS_TARGET;
1458 NULL, GFP_KERNEL); 1597 skb = pn533_alloc_poll_tg_frame(dev);
1459 if (rc) 1598 } else { /* Polling mode */
1599 cmd_code = PN533_CMD_IN_LIST_PASSIVE_TARGET;
1600 skb = pn533_alloc_poll_in_frame(dev, mod);
1601 }
1602
1603 if (!skb) {
1604 nfc_dev_err(&dev->interface->dev, "Failed to allocate skb.");
1605 return -ENOMEM;
1606 }
1607
1608 rc = pn533_send_cmd_async(dev, cmd_code, skb, pn533_poll_complete,
1609 NULL);
1610 if (rc < 0) {
1611 dev_kfree_skb(skb);
1460 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc); 1612 nfc_dev_err(&dev->interface->dev, "Polling loop error %d", rc);
1613 }
1461 1614
1462 return rc; 1615 return rc;
1463} 1616}
@@ -1533,8 +1686,8 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1533 del_timer(&dev->listen_timer); 1686 del_timer(&dev->listen_timer);
1534 1687
1535 if (!dev->poll_mod_count) { 1688 if (!dev->poll_mod_count) {
1536 nfc_dev_dbg(&dev->interface->dev, "Polling operation was not" 1689 nfc_dev_dbg(&dev->interface->dev,
1537 " running"); 1690 "Polling operation was not running");
1538 return; 1691 return;
1539 } 1692 }
1540 1693
@@ -1549,38 +1702,38 @@ static void pn533_stop_poll(struct nfc_dev *nfc_dev)
1549 1702
1550static int pn533_activate_target_nfcdep(struct pn533 *dev) 1703static int pn533_activate_target_nfcdep(struct pn533 *dev)
1551{ 1704{
1552 struct pn533_cmd_activate_param param; 1705 struct pn533_cmd_activate_response *rsp;
1553 struct pn533_cmd_activate_response *resp;
1554 u16 gt_len; 1706 u16 gt_len;
1555 int rc; 1707 int rc;
1556 1708
1557 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1709 struct sk_buff *skb;
1710 struct sk_buff *resp;
1558 1711
1559 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_ATR); 1712 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1560 1713
1561 param.tg = 1; 1714 skb = pn533_alloc_skb(dev, sizeof(u8) * 2); /*TG + Next*/
1562 param.next = 0; 1715 if (!skb)
1563 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &param, 1716 return -ENOMEM;
1564 sizeof(struct pn533_cmd_activate_param));
1565 dev->out_frame->datalen += sizeof(struct pn533_cmd_activate_param);
1566 1717
1567 pn533_tx_frame_finish(dev->out_frame); 1718 *skb_put(skb, sizeof(u8)) = 1; /* TG */
1719 *skb_put(skb, sizeof(u8)) = 0; /* Next */
1568 1720
1569 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, 1721 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_ATR, skb);
1570 dev->in_maxlen); 1722 if (IS_ERR(resp))
1571 if (rc) 1723 return PTR_ERR(resp);
1572 return rc;
1573 1724
1574 resp = (struct pn533_cmd_activate_response *) 1725 rsp = (struct pn533_cmd_activate_response *)resp->data;
1575 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame); 1726 rc = rsp->status & PN533_CMD_RET_MASK;
1576 rc = resp->status & PN533_CMD_RET_MASK; 1727 if (rc != PN533_CMD_RET_SUCCESS) {
1577 if (rc != PN533_CMD_RET_SUCCESS) 1728 dev_kfree_skb(resp);
1578 return -EIO; 1729 return -EIO;
1730 }
1579 1731
1580 /* ATR_RES general bytes are located at offset 16 */ 1732 /* ATR_RES general bytes are located at offset 16 */
1581 gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 16; 1733 gt_len = resp->len - 16;
1582 rc = nfc_set_remote_general_bytes(dev->nfc_dev, resp->gt, gt_len); 1734 rc = nfc_set_remote_general_bytes(dev->nfc_dev, rsp->gt, gt_len);
1583 1735
1736 dev_kfree_skb(resp);
1584 return rc; 1737 return rc;
1585} 1738}
1586 1739
@@ -1591,38 +1744,38 @@ static int pn533_activate_target(struct nfc_dev *nfc_dev,
1591 int rc; 1744 int rc;
1592 1745
1593 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__, 1746 nfc_dev_dbg(&dev->interface->dev, "%s - protocol=%u", __func__,
1594 protocol); 1747 protocol);
1595 1748
1596 if (dev->poll_mod_count) { 1749 if (dev->poll_mod_count) {
1597 nfc_dev_err(&dev->interface->dev, "Cannot activate while" 1750 nfc_dev_err(&dev->interface->dev,
1598 " polling"); 1751 "Cannot activate while polling");
1599 return -EBUSY; 1752 return -EBUSY;
1600 } 1753 }
1601 1754
1602 if (dev->tgt_active_prot) { 1755 if (dev->tgt_active_prot) {
1603 nfc_dev_err(&dev->interface->dev, "There is already an active" 1756 nfc_dev_err(&dev->interface->dev,
1604 " target"); 1757 "There is already an active target");
1605 return -EBUSY; 1758 return -EBUSY;
1606 } 1759 }
1607 1760
1608 if (!dev->tgt_available_prots) { 1761 if (!dev->tgt_available_prots) {
1609 nfc_dev_err(&dev->interface->dev, "There is no available target" 1762 nfc_dev_err(&dev->interface->dev,
1610 " to activate"); 1763 "There is no available target to activate");
1611 return -EINVAL; 1764 return -EINVAL;
1612 } 1765 }
1613 1766
1614 if (!(dev->tgt_available_prots & (1 << protocol))) { 1767 if (!(dev->tgt_available_prots & (1 << protocol))) {
1615 nfc_dev_err(&dev->interface->dev, "The target does not support" 1768 nfc_dev_err(&dev->interface->dev,
1616 " the requested protocol %u", protocol); 1769 "Target doesn't support requested proto %u",
1770 protocol);
1617 return -EINVAL; 1771 return -EINVAL;
1618 } 1772 }
1619 1773
1620 if (protocol == NFC_PROTO_NFC_DEP) { 1774 if (protocol == NFC_PROTO_NFC_DEP) {
1621 rc = pn533_activate_target_nfcdep(dev); 1775 rc = pn533_activate_target_nfcdep(dev);
1622 if (rc) { 1776 if (rc) {
1623 nfc_dev_err(&dev->interface->dev, "Error %d when" 1777 nfc_dev_err(&dev->interface->dev,
1624 " activating target with" 1778 "Activating target with DEP failed %d", rc);
1625 " NFC_DEP protocol", rc);
1626 return rc; 1779 return rc;
1627 } 1780 }
1628 } 1781 }
@@ -1637,8 +1790,10 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1637 struct nfc_target *target) 1790 struct nfc_target *target)
1638{ 1791{
1639 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 1792 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1640 u8 tg; 1793
1641 u8 status; 1794 struct sk_buff *skb;
1795 struct sk_buff *resp;
1796
1642 int rc; 1797 int rc;
1643 1798
1644 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1799 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
@@ -1649,83 +1804,69 @@ static void pn533_deactivate_target(struct nfc_dev *nfc_dev,
1649 } 1804 }
1650 1805
1651 dev->tgt_active_prot = 0; 1806 dev->tgt_active_prot = 0;
1652
1653 skb_queue_purge(&dev->resp_q); 1807 skb_queue_purge(&dev->resp_q);
1654 1808
1655 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_RELEASE); 1809 skb = pn533_alloc_skb(dev, sizeof(u8));
1656 1810 if (!skb)
1657 tg = 1; 1811 return;
1658 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), &tg, sizeof(u8));
1659 dev->out_frame->datalen += sizeof(u8);
1660 1812
1661 pn533_tx_frame_finish(dev->out_frame); 1813 *skb_put(skb, 1) = 1; /* TG*/
1662 1814
1663 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, 1815 resp = pn533_send_cmd_sync(dev, PN533_CMD_IN_RELEASE, skb);
1664 dev->in_maxlen); 1816 if (IS_ERR(resp))
1665 if (rc) {
1666 nfc_dev_err(&dev->interface->dev, "Error when sending release"
1667 " command to the controller");
1668 return; 1817 return;
1669 }
1670 1818
1671 status = PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame)[0]; 1819 rc = resp->data[0] & PN533_CMD_RET_MASK;
1672 rc = status & PN533_CMD_RET_MASK;
1673 if (rc != PN533_CMD_RET_SUCCESS) 1820 if (rc != PN533_CMD_RET_SUCCESS)
1674 nfc_dev_err(&dev->interface->dev, "Error 0x%x when releasing" 1821 nfc_dev_err(&dev->interface->dev,
1675 " the target", rc); 1822 "Error 0x%x when releasing the target", rc);
1676 1823
1824 dev_kfree_skb(resp);
1677 return; 1825 return;
1678} 1826}
1679 1827
1680 1828
1681static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg, 1829static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1682 u8 *params, int params_len) 1830 struct sk_buff *resp)
1683{ 1831{
1684 struct pn533_cmd_jump_dep_response *resp; 1832 struct pn533_cmd_jump_dep_response *rsp;
1685 struct nfc_target nfc_target;
1686 u8 target_gt_len; 1833 u8 target_gt_len;
1687 int rc; 1834 int rc;
1688 struct pn533_cmd_jump_dep *cmd = (struct pn533_cmd_jump_dep *)arg; 1835 u8 active = *(u8 *)arg;
1689 u8 active = cmd->active;
1690 1836
1691 kfree(arg); 1837 kfree(arg);
1692 1838
1693 if (params_len == -ENOENT) { 1839 if (IS_ERR(resp))
1694 nfc_dev_dbg(&dev->interface->dev, ""); 1840 return PTR_ERR(resp);
1695 return 0;
1696 }
1697
1698 if (params_len < 0) {
1699 nfc_dev_err(&dev->interface->dev,
1700 "Error %d when bringing DEP link up",
1701 params_len);
1702 return 0;
1703 }
1704 1841
1705 if (dev->tgt_available_prots && 1842 if (dev->tgt_available_prots &&
1706 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) { 1843 !(dev->tgt_available_prots & (1 << NFC_PROTO_NFC_DEP))) {
1707 nfc_dev_err(&dev->interface->dev, 1844 nfc_dev_err(&dev->interface->dev,
1708 "The target does not support DEP"); 1845 "The target does not support DEP");
1709 return -EINVAL; 1846 rc = -EINVAL;
1847 goto error;
1710 } 1848 }
1711 1849
1712 resp = (struct pn533_cmd_jump_dep_response *) params; 1850 rsp = (struct pn533_cmd_jump_dep_response *)resp->data;
1713 rc = resp->status & PN533_CMD_RET_MASK; 1851
1852 rc = rsp->status & PN533_CMD_RET_MASK;
1714 if (rc != PN533_CMD_RET_SUCCESS) { 1853 if (rc != PN533_CMD_RET_SUCCESS) {
1715 nfc_dev_err(&dev->interface->dev, 1854 nfc_dev_err(&dev->interface->dev,
1716 "Bringing DEP link up failed %d", rc); 1855 "Bringing DEP link up failed %d", rc);
1717 return 0; 1856 goto error;
1718 } 1857 }
1719 1858
1720 if (!dev->tgt_available_prots) { 1859 if (!dev->tgt_available_prots) {
1860 struct nfc_target nfc_target;
1861
1721 nfc_dev_dbg(&dev->interface->dev, "Creating new target"); 1862 nfc_dev_dbg(&dev->interface->dev, "Creating new target");
1722 1863
1723 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK; 1864 nfc_target.supported_protocols = NFC_PROTO_NFC_DEP_MASK;
1724 nfc_target.nfcid1_len = 10; 1865 nfc_target.nfcid1_len = 10;
1725 memcpy(nfc_target.nfcid1, resp->nfcid3t, nfc_target.nfcid1_len); 1866 memcpy(nfc_target.nfcid1, rsp->nfcid3t, nfc_target.nfcid1_len);
1726 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1); 1867 rc = nfc_targets_found(dev->nfc_dev, &nfc_target, 1);
1727 if (rc) 1868 if (rc)
1728 return 0; 1869 goto error;
1729 1870
1730 dev->tgt_available_prots = 0; 1871 dev->tgt_available_prots = 0;
1731 } 1872 }
@@ -1733,15 +1874,17 @@ static int pn533_in_dep_link_up_complete(struct pn533 *dev, void *arg,
1733 dev->tgt_active_prot = NFC_PROTO_NFC_DEP; 1874 dev->tgt_active_prot = NFC_PROTO_NFC_DEP;
1734 1875
1735 /* ATR_RES general bytes are located at offset 17 */ 1876 /* ATR_RES general bytes are located at offset 17 */
1736 target_gt_len = PN533_FRAME_CMD_PARAMS_LEN(dev->in_frame) - 17; 1877 target_gt_len = resp->len - 17;
1737 rc = nfc_set_remote_general_bytes(dev->nfc_dev, 1878 rc = nfc_set_remote_general_bytes(dev->nfc_dev,
1738 resp->gt, target_gt_len); 1879 rsp->gt, target_gt_len);
1739 if (rc == 0) 1880 if (rc == 0)
1740 rc = nfc_dep_link_is_up(dev->nfc_dev, 1881 rc = nfc_dep_link_is_up(dev->nfc_dev,
1741 dev->nfc_dev->targets[0].idx, 1882 dev->nfc_dev->targets[0].idx,
1742 !active, NFC_RF_INITIATOR); 1883 !active, NFC_RF_INITIATOR);
1743 1884
1744 return 0; 1885error:
1886 dev_kfree_skb(resp);
1887 return rc;
1745} 1888}
1746 1889
1747static int pn533_mod_to_baud(struct pn533 *dev) 1890static int pn533_mod_to_baud(struct pn533 *dev)
@@ -1760,25 +1903,26 @@ static int pn533_mod_to_baud(struct pn533 *dev)
1760 1903
1761#define PASSIVE_DATA_LEN 5 1904#define PASSIVE_DATA_LEN 5
1762static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target, 1905static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1763 u8 comm_mode, u8* gb, size_t gb_len) 1906 u8 comm_mode, u8 *gb, size_t gb_len)
1764{ 1907{
1765 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 1908 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1766 struct pn533_cmd_jump_dep *cmd; 1909 struct sk_buff *skb;
1767 u8 cmd_len, *data_ptr; 1910 int rc, baud, skb_len;
1911 u8 *next, *arg;
1912
1768 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3}; 1913 u8 passive_data[PASSIVE_DATA_LEN] = {0x00, 0xff, 0xff, 0x00, 0x3};
1769 int rc, baud;
1770 1914
1771 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 1915 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1772 1916
1773 if (dev->poll_mod_count) { 1917 if (dev->poll_mod_count) {
1774 nfc_dev_err(&dev->interface->dev, 1918 nfc_dev_err(&dev->interface->dev,
1775 "Cannot bring the DEP link up while polling"); 1919 "Cannot bring the DEP link up while polling");
1776 return -EBUSY; 1920 return -EBUSY;
1777 } 1921 }
1778 1922
1779 if (dev->tgt_active_prot) { 1923 if (dev->tgt_active_prot) {
1780 nfc_dev_err(&dev->interface->dev, 1924 nfc_dev_err(&dev->interface->dev,
1781 "There is already an active target"); 1925 "There is already an active target");
1782 return -EBUSY; 1926 return -EBUSY;
1783 } 1927 }
1784 1928
@@ -1789,43 +1933,48 @@ static int pn533_dep_link_up(struct nfc_dev *nfc_dev, struct nfc_target *target,
1789 return baud; 1933 return baud;
1790 } 1934 }
1791 1935
1792 cmd_len = sizeof(struct pn533_cmd_jump_dep) + gb_len; 1936 skb_len = 3 + gb_len; /* ActPass + BR + Next */
1793 if (comm_mode == NFC_COMM_PASSIVE) 1937 if (comm_mode == NFC_COMM_PASSIVE)
1794 cmd_len += PASSIVE_DATA_LEN; 1938 skb_len += PASSIVE_DATA_LEN;
1795 1939
1796 cmd = kzalloc(cmd_len, GFP_KERNEL); 1940 skb = pn533_alloc_skb(dev, skb_len);
1797 if (cmd == NULL) 1941 if (!skb)
1798 return -ENOMEM; 1942 return -ENOMEM;
1799 1943
1800 pn533_tx_frame_init(dev->out_frame, PN533_CMD_IN_JUMP_FOR_DEP); 1944 *skb_put(skb, 1) = !comm_mode; /* ActPass */
1945 *skb_put(skb, 1) = baud; /* Baud rate */
1801 1946
1802 cmd->active = !comm_mode; 1947 next = skb_put(skb, 1); /* Next */
1803 cmd->next = 0; 1948 *next = 0;
1804 cmd->baud = baud; 1949
1805 data_ptr = cmd->data; 1950 if (comm_mode == NFC_COMM_PASSIVE && baud > 0) {
1806 if (comm_mode == NFC_COMM_PASSIVE && cmd->baud > 0) { 1951 memcpy(skb_put(skb, PASSIVE_DATA_LEN), passive_data,
1807 memcpy(data_ptr, passive_data, PASSIVE_DATA_LEN); 1952 PASSIVE_DATA_LEN);
1808 cmd->next |= 1; 1953 *next |= 1;
1809 data_ptr += PASSIVE_DATA_LEN;
1810 } 1954 }
1811 1955
1812 if (gb != NULL && gb_len > 0) { 1956 if (gb != NULL && gb_len > 0) {
1813 cmd->next |= 4; /* We have some Gi */ 1957 memcpy(skb_put(skb, gb_len), gb, gb_len);
1814 memcpy(data_ptr, gb, gb_len); 1958 *next |= 4; /* We have some Gi */
1815 } else { 1959 } else {
1816 cmd->next = 0; 1960 *next = 0;
1817 } 1961 }
1818 1962
1819 memcpy(PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame), cmd, cmd_len); 1963 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
1820 dev->out_frame->datalen += cmd_len; 1964 if (!arg) {
1965 dev_kfree_skb(skb);
1966 return -ENOMEM;
1967 }
1821 1968
1822 pn533_tx_frame_finish(dev->out_frame); 1969 *arg = !comm_mode;
1823 1970
1824 rc = pn533_send_cmd_frame_async(dev, dev->out_frame, dev->in_frame, 1971 rc = pn533_send_cmd_async(dev, PN533_CMD_IN_JUMP_FOR_DEP, skb,
1825 dev->in_maxlen, pn533_in_dep_link_up_complete, 1972 pn533_in_dep_link_up_complete, arg);
1826 cmd, GFP_KERNEL); 1973
1827 if (rc < 0) 1974 if (rc < 0) {
1828 kfree(cmd); 1975 dev_kfree_skb(skb);
1976 kfree(arg);
1977 }
1829 1978
1830 return rc; 1979 return rc;
1831} 1980}
@@ -1834,6 +1983,8 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1834{ 1983{
1835 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 1984 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
1836 1985
1986 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1987
1837 pn533_poll_reset_mod_list(dev); 1988 pn533_poll_reset_mod_list(dev);
1838 1989
1839 if (dev->tgt_mode || dev->tgt_active_prot) { 1990 if (dev->tgt_mode || dev->tgt_active_prot) {
@@ -1849,68 +2000,7 @@ static int pn533_dep_link_down(struct nfc_dev *nfc_dev)
1849 return 0; 2000 return 0;
1850} 2001}
1851 2002
1852static int pn533_build_tx_frame(struct pn533 *dev, struct sk_buff *skb,
1853 bool target)
1854{
1855 int payload_len = skb->len;
1856 struct pn533_frame *out_frame;
1857 u8 tg;
1858
1859 nfc_dev_dbg(&dev->interface->dev, "%s - Sending %d bytes", __func__,
1860 payload_len);
1861
1862 if (payload_len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
1863 /* TODO: Implement support to multi-part data exchange */
1864 nfc_dev_err(&dev->interface->dev, "Data length greater than the"
1865 " max allowed: %d",
1866 PN533_CMD_DATAEXCH_DATA_MAXLEN);
1867 return -ENOSYS;
1868 }
1869
1870 if (target == true) {
1871 switch (dev->device_type) {
1872 case PN533_DEVICE_PASORI:
1873 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
1874 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1875 out_frame = (struct pn533_frame *) skb->data;
1876 pn533_tx_frame_init(out_frame,
1877 PN533_CMD_IN_COMM_THRU);
1878
1879 break;
1880 }
1881
1882 default:
1883 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN);
1884 out_frame = (struct pn533_frame *) skb->data;
1885 pn533_tx_frame_init(out_frame,
1886 PN533_CMD_IN_DATA_EXCHANGE);
1887 tg = 1;
1888 memcpy(PN533_FRAME_CMD_PARAMS_PTR(out_frame),
1889 &tg, sizeof(u8));
1890 out_frame->datalen += sizeof(u8);
1891
1892 break;
1893 }
1894
1895 } else {
1896 skb_push(skb, PN533_CMD_DATAEXCH_HEAD_LEN - 1);
1897 out_frame = (struct pn533_frame *) skb->data;
1898 pn533_tx_frame_init(out_frame, PN533_CMD_TG_SET_DATA);
1899 }
1900
1901
1902 /* The data is already in the out_frame, just update the datalen */
1903 out_frame->datalen += payload_len;
1904
1905 pn533_tx_frame_finish(out_frame);
1906 skb_put(skb, PN533_FRAME_TAIL_SIZE);
1907
1908 return 0;
1909}
1910
1911struct pn533_data_exchange_arg { 2003struct pn533_data_exchange_arg {
1912 struct sk_buff *skb_resp;
1913 struct sk_buff *skb_out;
1914 data_exchange_cb_t cb; 2004 data_exchange_cb_t cb;
1915 void *cb_context; 2005 void *cb_context;
1916}; 2006};
@@ -1920,7 +2010,7 @@ static struct sk_buff *pn533_build_response(struct pn533 *dev)
1920 struct sk_buff *skb, *tmp, *t; 2010 struct sk_buff *skb, *tmp, *t;
1921 unsigned int skb_len = 0, tmp_len = 0; 2011 unsigned int skb_len = 0, tmp_len = 0;
1922 2012
1923 nfc_dev_dbg(&dev->interface->dev, "%s\n", __func__); 2013 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1924 2014
1925 if (skb_queue_empty(&dev->resp_q)) 2015 if (skb_queue_empty(&dev->resp_q))
1926 return NULL; 2016 return NULL;
@@ -1954,46 +2044,44 @@ out:
1954} 2044}
1955 2045
1956static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg, 2046static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
1957 u8 *params, int params_len) 2047 struct sk_buff *resp)
1958{ 2048{
1959 struct pn533_data_exchange_arg *arg = _arg; 2049 struct pn533_data_exchange_arg *arg = _arg;
1960 struct sk_buff *skb = NULL, *skb_resp = arg->skb_resp; 2050 struct sk_buff *skb;
1961 struct pn533_frame *in_frame = (struct pn533_frame *) skb_resp->data; 2051 int rc = 0;
1962 int err = 0; 2052 u8 status, ret, mi;
1963 u8 status;
1964 u8 cmd_ret;
1965 2053
1966 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2054 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
1967 2055
1968 dev_kfree_skb(arg->skb_out); 2056 if (IS_ERR(resp)) {
1969 2057 rc = PTR_ERR(resp);
1970 if (params_len < 0) { /* error */ 2058 goto _error;
1971 err = params_len;
1972 goto error;
1973 } 2059 }
1974 2060
1975 status = params[0]; 2061 status = resp->data[0];
2062 ret = status & PN533_CMD_RET_MASK;
2063 mi = status & PN533_CMD_MI_MASK;
2064
2065 skb_pull(resp, sizeof(status));
1976 2066
1977 cmd_ret = status & PN533_CMD_RET_MASK; 2067 if (ret != PN533_CMD_RET_SUCCESS) {
1978 if (cmd_ret != PN533_CMD_RET_SUCCESS) { 2068 nfc_dev_err(&dev->interface->dev,
1979 nfc_dev_err(&dev->interface->dev, "PN533 reported error %d when" 2069 "PN533 reported error %d when exchanging data",
1980 " exchanging data", cmd_ret); 2070 ret);
1981 err = -EIO; 2071 rc = -EIO;
1982 goto error; 2072 goto error;
1983 } 2073 }
1984 2074
1985 skb_put(skb_resp, PN533_FRAME_SIZE(in_frame)); 2075 skb_queue_tail(&dev->resp_q, resp);
1986 skb_pull(skb_resp, PN533_CMD_DATAEXCH_HEAD_LEN);
1987 skb_trim(skb_resp, skb_resp->len - PN533_FRAME_TAIL_SIZE);
1988 skb_queue_tail(&dev->resp_q, skb_resp);
1989 2076
1990 if (status & PN533_CMD_MI_MASK) { 2077 if (mi) {
2078 dev->cmd_complete_mi_arg = arg;
1991 queue_work(dev->wq, &dev->mi_work); 2079 queue_work(dev->wq, &dev->mi_work);
1992 return -EINPROGRESS; 2080 return -EINPROGRESS;
1993 } 2081 }
1994 2082
1995 skb = pn533_build_response(dev); 2083 skb = pn533_build_response(dev);
1996 if (skb == NULL) 2084 if (!skb)
1997 goto error; 2085 goto error;
1998 2086
1999 arg->cb(arg->cb_context, skb, 0); 2087 arg->cb(arg->cb_context, skb, 0);
@@ -2001,11 +2089,12 @@ static int pn533_data_exchange_complete(struct pn533 *dev, void *_arg,
2001 return 0; 2089 return 0;
2002 2090
2003error: 2091error:
2092 dev_kfree_skb(resp);
2093_error:
2004 skb_queue_purge(&dev->resp_q); 2094 skb_queue_purge(&dev->resp_q);
2005 dev_kfree_skb(skb_resp); 2095 arg->cb(arg->cb_context, NULL, rc);
2006 arg->cb(arg->cb_context, NULL, err);
2007 kfree(arg); 2096 kfree(arg);
2008 return 0; 2097 return rc;
2009} 2098}
2010 2099
2011static int pn533_transceive(struct nfc_dev *nfc_dev, 2100static int pn533_transceive(struct nfc_dev *nfc_dev,
@@ -2013,87 +2102,82 @@ static int pn533_transceive(struct nfc_dev *nfc_dev,
2013 data_exchange_cb_t cb, void *cb_context) 2102 data_exchange_cb_t cb, void *cb_context)
2014{ 2103{
2015 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 2104 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2016 struct pn533_frame *out_frame, *in_frame; 2105 struct pn533_data_exchange_arg *arg = NULL;
2017 struct pn533_data_exchange_arg *arg;
2018 struct sk_buff *skb_resp;
2019 int skb_resp_len;
2020 int rc; 2106 int rc;
2021 2107
2022 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2108 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2023 2109
2024 if (!dev->tgt_active_prot) { 2110 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2025 nfc_dev_err(&dev->interface->dev, "Cannot exchange data if" 2111 /* TODO: Implement support to multi-part data exchange */
2026 " there is no active target"); 2112 nfc_dev_err(&dev->interface->dev,
2027 rc = -EINVAL; 2113 "Data length greater than the max allowed: %d",
2114 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2115 rc = -ENOSYS;
2028 goto error; 2116 goto error;
2029 } 2117 }
2030 2118
2031 rc = pn533_build_tx_frame(dev, skb, true); 2119 if (!dev->tgt_active_prot) {
2032 if (rc) 2120 nfc_dev_err(&dev->interface->dev,
2033 goto error; 2121 "Can't exchange data if there is no active target");
2034 2122 rc = -EINVAL;
2035 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN +
2036 PN533_CMD_DATAEXCH_DATA_MAXLEN +
2037 PN533_FRAME_TAIL_SIZE;
2038
2039 skb_resp = nfc_alloc_recv_skb(skb_resp_len, GFP_KERNEL);
2040 if (!skb_resp) {
2041 rc = -ENOMEM;
2042 goto error; 2123 goto error;
2043 } 2124 }
2044 2125
2045 in_frame = (struct pn533_frame *) skb_resp->data; 2126 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
2046 out_frame = (struct pn533_frame *) skb->data;
2047
2048 arg = kmalloc(sizeof(struct pn533_data_exchange_arg), GFP_KERNEL);
2049 if (!arg) { 2127 if (!arg) {
2050 rc = -ENOMEM; 2128 rc = -ENOMEM;
2051 goto free_skb_resp; 2129 goto error;
2052 } 2130 }
2053 2131
2054 arg->skb_resp = skb_resp;
2055 arg->skb_out = skb;
2056 arg->cb = cb; 2132 arg->cb = cb;
2057 arg->cb_context = cb_context; 2133 arg->cb_context = cb_context;
2058 2134
2059 rc = pn533_send_cmd_frame_async(dev, out_frame, in_frame, skb_resp_len, 2135 switch (dev->device_type) {
2060 pn533_data_exchange_complete, arg, 2136 case PN533_DEVICE_PASORI:
2061 GFP_KERNEL); 2137 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2062 if (rc) { 2138 rc = pn533_send_data_async(dev, PN533_CMD_IN_COMM_THRU,
2063 nfc_dev_err(&dev->interface->dev, "Error %d when trying to" 2139 skb,
2064 " perform data_exchange", rc); 2140 pn533_data_exchange_complete,
2065 goto free_arg; 2141 arg);
2142
2143 break;
2144 }
2145 default:
2146 *skb_push(skb, sizeof(u8)) = 1; /*TG*/
2147
2148 rc = pn533_send_data_async(dev, PN533_CMD_IN_DATA_EXCHANGE,
2149 skb, pn533_data_exchange_complete,
2150 arg);
2151
2152 break;
2066 } 2153 }
2067 2154
2155 if (rc < 0) /* rc from send_async */
2156 goto error;
2157
2068 return 0; 2158 return 0;
2069 2159
2070free_arg:
2071 kfree(arg);
2072free_skb_resp:
2073 kfree_skb(skb_resp);
2074error: 2160error:
2075 kfree_skb(skb); 2161 kfree(arg);
2162 dev_kfree_skb(skb);
2076 return rc; 2163 return rc;
2077} 2164}
2078 2165
2079static int pn533_tm_send_complete(struct pn533 *dev, void *arg, 2166static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2080 u8 *params, int params_len) 2167 struct sk_buff *resp)
2081{ 2168{
2082 struct sk_buff *skb_out = arg; 2169 u8 status;
2083 2170
2084 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2171 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2085 2172
2086 dev_kfree_skb(skb_out); 2173 if (IS_ERR(resp))
2174 return PTR_ERR(resp);
2087 2175
2088 if (params_len < 0) { 2176 status = resp->data[0];
2089 nfc_dev_err(&dev->interface->dev,
2090 "Error %d when sending data",
2091 params_len);
2092 2177
2093 return params_len; 2178 dev_kfree_skb(resp);
2094 }
2095 2179
2096 if (params_len > 0 && params[0] != 0) { 2180 if (status != 0) {
2097 nfc_tm_deactivated(dev->nfc_dev); 2181 nfc_tm_deactivated(dev->nfc_dev);
2098 2182
2099 dev->tgt_mode = 0; 2183 dev->tgt_mode = 0;
@@ -2109,30 +2193,21 @@ static int pn533_tm_send_complete(struct pn533 *dev, void *arg,
2109static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb) 2193static int pn533_tm_send(struct nfc_dev *nfc_dev, struct sk_buff *skb)
2110{ 2194{
2111 struct pn533 *dev = nfc_get_drvdata(nfc_dev); 2195 struct pn533 *dev = nfc_get_drvdata(nfc_dev);
2112 struct pn533_frame *out_frame;
2113 int rc; 2196 int rc;
2114 2197
2115 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2198 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2116 2199
2117 rc = pn533_build_tx_frame(dev, skb, false); 2200 if (skb->len > PN533_CMD_DATAEXCH_DATA_MAXLEN) {
2118 if (rc)
2119 goto error;
2120
2121 out_frame = (struct pn533_frame *) skb->data;
2122
2123 rc = pn533_send_cmd_frame_async(dev, out_frame, dev->in_frame,
2124 dev->in_maxlen, pn533_tm_send_complete,
2125 skb, GFP_KERNEL);
2126 if (rc) {
2127 nfc_dev_err(&dev->interface->dev, 2201 nfc_dev_err(&dev->interface->dev,
2128 "Error %d when trying to send data", rc); 2202 "Data length greater than the max allowed: %d",
2129 goto error; 2203 PN533_CMD_DATAEXCH_DATA_MAXLEN);
2204 return -ENOSYS;
2130 } 2205 }
2131 2206
2132 return 0; 2207 rc = pn533_send_data_async(dev, PN533_CMD_TG_SET_DATA, skb,
2133 2208 pn533_tm_send_complete, NULL);
2134error: 2209 if (rc < 0)
2135 kfree_skb(skb); 2210 dev_kfree_skb(skb);
2136 2211
2137 return rc; 2212 return rc;
2138} 2213}
@@ -2140,107 +2215,123 @@ error:
2140static void pn533_wq_mi_recv(struct work_struct *work) 2215static void pn533_wq_mi_recv(struct work_struct *work)
2141{ 2216{
2142 struct pn533 *dev = container_of(work, struct pn533, mi_work); 2217 struct pn533 *dev = container_of(work, struct pn533, mi_work);
2143 struct sk_buff *skb_cmd; 2218
2144 struct pn533_data_exchange_arg *arg = dev->cmd_complete_arg; 2219 struct sk_buff *skb;
2145 struct pn533_frame *out_frame, *in_frame;
2146 struct sk_buff *skb_resp;
2147 int skb_resp_len;
2148 int rc; 2220 int rc;
2149 2221
2150 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2222 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2151 2223
2152 /* This is a zero payload size skb */ 2224 skb = pn533_alloc_skb(dev, PN533_CMD_DATAEXCH_HEAD_LEN);
2153 skb_cmd = alloc_skb(PN533_CMD_DATAEXCH_HEAD_LEN + PN533_FRAME_TAIL_SIZE, 2225 if (!skb)
2154 GFP_KERNEL); 2226 goto error;
2155 if (skb_cmd == NULL)
2156 goto error_cmd;
2157
2158 skb_reserve(skb_cmd, PN533_CMD_DATAEXCH_HEAD_LEN);
2159 2227
2160 rc = pn533_build_tx_frame(dev, skb_cmd, true); 2228 switch (dev->device_type) {
2161 if (rc) 2229 case PN533_DEVICE_PASORI:
2162 goto error_frame; 2230 if (dev->tgt_active_prot == NFC_PROTO_FELICA) {
2231 rc = pn533_send_cmd_direct_async(dev,
2232 PN533_CMD_IN_COMM_THRU,
2233 skb,
2234 pn533_data_exchange_complete,
2235 dev->cmd_complete_mi_arg);
2163 2236
2164 skb_resp_len = PN533_CMD_DATAEXCH_HEAD_LEN + 2237 break;
2165 PN533_CMD_DATAEXCH_DATA_MAXLEN + 2238 }
2166 PN533_FRAME_TAIL_SIZE; 2239 default:
2167 skb_resp = alloc_skb(skb_resp_len, GFP_KERNEL); 2240 *skb_put(skb, sizeof(u8)) = 1; /*TG*/
2168 if (!skb_resp) {
2169 rc = -ENOMEM;
2170 goto error_frame;
2171 }
2172 2241
2173 in_frame = (struct pn533_frame *) skb_resp->data; 2242 rc = pn533_send_cmd_direct_async(dev,
2174 out_frame = (struct pn533_frame *) skb_cmd->data; 2243 PN533_CMD_IN_DATA_EXCHANGE,
2244 skb,
2245 pn533_data_exchange_complete,
2246 dev->cmd_complete_mi_arg);
2175 2247
2176 arg->skb_resp = skb_resp; 2248 break;
2177 arg->skb_out = skb_cmd; 2249 }
2178 2250
2179 rc = __pn533_send_cmd_frame_async(dev, out_frame, in_frame, 2251 if (rc == 0) /* success */
2180 skb_resp_len,
2181 pn533_data_exchange_complete,
2182 dev->cmd_complete_arg, GFP_KERNEL);
2183 if (!rc)
2184 return; 2252 return;
2185 2253
2186 nfc_dev_err(&dev->interface->dev, "Error %d when trying to" 2254 nfc_dev_err(&dev->interface->dev,
2187 " perform data_exchange", rc); 2255 "Error %d when trying to perform data_exchange", rc);
2188
2189 kfree_skb(skb_resp);
2190 2256
2191error_frame: 2257 dev_kfree_skb(skb);
2192 kfree_skb(skb_cmd); 2258 kfree(dev->cmd_complete_arg);
2193 2259
2194error_cmd: 2260error:
2195 pn533_send_ack(dev, GFP_KERNEL); 2261 pn533_send_ack(dev, GFP_KERNEL);
2196
2197 kfree(arg);
2198
2199 queue_work(dev->wq, &dev->cmd_work); 2262 queue_work(dev->wq, &dev->cmd_work);
2200} 2263}
2201 2264
2202static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata, 2265static int pn533_set_configuration(struct pn533 *dev, u8 cfgitem, u8 *cfgdata,
2203 u8 cfgdata_len) 2266 u8 cfgdata_len)
2204{ 2267{
2205 int rc; 2268 struct sk_buff *skb;
2206 u8 *params; 2269 struct sk_buff *resp;
2270
2271 int skb_len;
2207 2272
2208 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2273 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2209 2274
2210 pn533_tx_frame_init(dev->out_frame, PN533_CMD_RF_CONFIGURATION); 2275 skb_len = sizeof(cfgitem) + cfgdata_len; /* cfgitem + cfgdata */
2211 2276
2212 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame); 2277 skb = pn533_alloc_skb(dev, skb_len);
2213 params[0] = cfgitem; 2278 if (!skb)
2214 memcpy(&params[1], cfgdata, cfgdata_len); 2279 return -ENOMEM;
2215 dev->out_frame->datalen += (1 + cfgdata_len);
2216 2280
2217 pn533_tx_frame_finish(dev->out_frame); 2281 *skb_put(skb, sizeof(cfgitem)) = cfgitem;
2282 memcpy(skb_put(skb, cfgdata_len), cfgdata, cfgdata_len);
2218 2283
2219 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, 2284 resp = pn533_send_cmd_sync(dev, PN533_CMD_RF_CONFIGURATION, skb);
2220 dev->in_maxlen); 2285 if (IS_ERR(resp))
2286 return PTR_ERR(resp);
2221 2287
2222 return rc; 2288 dev_kfree_skb(resp);
2289 return 0;
2290}
2291
2292static int pn533_get_firmware_version(struct pn533 *dev,
2293 struct pn533_fw_version *fv)
2294{
2295 struct sk_buff *skb;
2296 struct sk_buff *resp;
2297
2298 skb = pn533_alloc_skb(dev, 0);
2299 if (!skb)
2300 return -ENOMEM;
2301
2302 resp = pn533_send_cmd_sync(dev, PN533_CMD_GET_FIRMWARE_VERSION, skb);
2303 if (IS_ERR(resp))
2304 return PTR_ERR(resp);
2305
2306 fv->ic = resp->data[0];
2307 fv->ver = resp->data[1];
2308 fv->rev = resp->data[2];
2309 fv->support = resp->data[3];
2310
2311 dev_kfree_skb(resp);
2312 return 0;
2223} 2313}
2224 2314
2225static int pn533_fw_reset(struct pn533 *dev) 2315static int pn533_fw_reset(struct pn533 *dev)
2226{ 2316{
2227 int rc; 2317 struct sk_buff *skb;
2228 u8 *params; 2318 struct sk_buff *resp;
2229 2319
2230 nfc_dev_dbg(&dev->interface->dev, "%s", __func__); 2320 nfc_dev_dbg(&dev->interface->dev, "%s", __func__);
2231 2321
2232 pn533_tx_frame_init(dev->out_frame, 0x18); 2322 skb = pn533_alloc_skb(dev, sizeof(u8));
2323 if (!skb)
2324 return -ENOMEM;
2233 2325
2234 params = PN533_FRAME_CMD_PARAMS_PTR(dev->out_frame); 2326 *skb_put(skb, sizeof(u8)) = 0x1;
2235 params[0] = 0x1;
2236 dev->out_frame->datalen += 1;
2237 2327
2238 pn533_tx_frame_finish(dev->out_frame); 2328 resp = pn533_send_cmd_sync(dev, 0x18, skb);
2329 if (IS_ERR(resp))
2330 return PTR_ERR(resp);
2239 2331
2240 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame, 2332 dev_kfree_skb(resp);
2241 dev->in_maxlen);
2242 2333
2243 return rc; 2334 return 0;
2244} 2335}
2245 2336
2246static struct nfc_ops pn533_nfc_ops = { 2337static struct nfc_ops pn533_nfc_ops = {
@@ -2337,7 +2428,7 @@ static int pn533_setup(struct pn533 *dev)
2337static int pn533_probe(struct usb_interface *interface, 2428static int pn533_probe(struct usb_interface *interface,
2338 const struct usb_device_id *id) 2429 const struct usb_device_id *id)
2339{ 2430{
2340 struct pn533_fw_version *fw_ver; 2431 struct pn533_fw_version fw_ver;
2341 struct pn533 *dev; 2432 struct pn533 *dev;
2342 struct usb_host_interface *iface_desc; 2433 struct usb_host_interface *iface_desc;
2343 struct usb_endpoint_descriptor *endpoint; 2434 struct usb_endpoint_descriptor *endpoint;
@@ -2359,41 +2450,32 @@ static int pn533_probe(struct usb_interface *interface,
2359 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 2450 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
2360 endpoint = &iface_desc->endpoint[i].desc; 2451 endpoint = &iface_desc->endpoint[i].desc;
2361 2452
2362 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint)) { 2453 if (!in_endpoint && usb_endpoint_is_bulk_in(endpoint))
2363 dev->in_maxlen = le16_to_cpu(endpoint->wMaxPacketSize);
2364 in_endpoint = endpoint->bEndpointAddress; 2454 in_endpoint = endpoint->bEndpointAddress;
2365 }
2366 2455
2367 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint)) { 2456 if (!out_endpoint && usb_endpoint_is_bulk_out(endpoint))
2368 dev->out_maxlen =
2369 le16_to_cpu(endpoint->wMaxPacketSize);
2370 out_endpoint = endpoint->bEndpointAddress; 2457 out_endpoint = endpoint->bEndpointAddress;
2371 }
2372 } 2458 }
2373 2459
2374 if (!in_endpoint || !out_endpoint) { 2460 if (!in_endpoint || !out_endpoint) {
2375 nfc_dev_err(&interface->dev, "Could not find bulk-in or" 2461 nfc_dev_err(&interface->dev,
2376 " bulk-out endpoint"); 2462 "Could not find bulk-in or bulk-out endpoint");
2377 rc = -ENODEV; 2463 rc = -ENODEV;
2378 goto error; 2464 goto error;
2379 } 2465 }
2380 2466
2381 dev->in_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2382 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL); 2467 dev->in_urb = usb_alloc_urb(0, GFP_KERNEL);
2383 dev->out_frame = kmalloc(PN533_NORMAL_FRAME_MAX_LEN, GFP_KERNEL);
2384 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL); 2468 dev->out_urb = usb_alloc_urb(0, GFP_KERNEL);
2385 2469
2386 if (!dev->in_frame || !dev->out_frame || 2470 if (!dev->in_urb || !dev->out_urb)
2387 !dev->in_urb || !dev->out_urb)
2388 goto error; 2471 goto error;
2389 2472
2390 usb_fill_bulk_urb(dev->in_urb, dev->udev, 2473 usb_fill_bulk_urb(dev->in_urb, dev->udev,
2391 usb_rcvbulkpipe(dev->udev, in_endpoint), 2474 usb_rcvbulkpipe(dev->udev, in_endpoint),
2392 NULL, 0, NULL, dev); 2475 NULL, 0, NULL, dev);
2393 usb_fill_bulk_urb(dev->out_urb, dev->udev, 2476 usb_fill_bulk_urb(dev->out_urb, dev->udev,
2394 usb_sndbulkpipe(dev->udev, out_endpoint), 2477 usb_sndbulkpipe(dev->udev, out_endpoint),
2395 NULL, 0, 2478 NULL, 0, pn533_send_complete, dev);
2396 pn533_send_complete, dev);
2397 2479
2398 INIT_WORK(&dev->cmd_work, pn533_wq_cmd); 2480 INIT_WORK(&dev->cmd_work, pn533_wq_cmd);
2399 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete); 2481 INIT_WORK(&dev->cmd_complete_work, pn533_wq_cmd_complete);
@@ -2414,18 +2496,7 @@ static int pn533_probe(struct usb_interface *interface,
2414 2496
2415 usb_set_intfdata(interface, dev); 2497 usb_set_intfdata(interface, dev);
2416 2498
2417 pn533_tx_frame_init(dev->out_frame, PN533_CMD_GET_FIRMWARE_VERSION); 2499 dev->ops = &pn533_std_frame_ops;
2418 pn533_tx_frame_finish(dev->out_frame);
2419
2420 rc = pn533_send_cmd_frame_sync(dev, dev->out_frame, dev->in_frame,
2421 dev->in_maxlen);
2422 if (rc)
2423 goto destroy_wq;
2424
2425 fw_ver = (struct pn533_fw_version *)
2426 PN533_FRAME_CMD_PARAMS_PTR(dev->in_frame);
2427 nfc_dev_info(&dev->interface->dev, "NXP PN533 firmware ver %d.%d now"
2428 " attached", fw_ver->ver, fw_ver->rev);
2429 2500
2430 dev->device_type = id->driver_info; 2501 dev->device_type = id->driver_info;
2431 switch (dev->device_type) { 2502 switch (dev->device_type) {
@@ -2444,9 +2515,21 @@ static int pn533_probe(struct usb_interface *interface,
2444 goto destroy_wq; 2515 goto destroy_wq;
2445 } 2516 }
2446 2517
2518 memset(&fw_ver, 0, sizeof(fw_ver));
2519 rc = pn533_get_firmware_version(dev, &fw_ver);
2520 if (rc < 0)
2521 goto destroy_wq;
2522
2523 nfc_dev_info(&dev->interface->dev,
2524 "NXP PN533 firmware ver %d.%d now attached",
2525 fw_ver.ver, fw_ver.rev);
2526
2527
2447 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols, 2528 dev->nfc_dev = nfc_allocate_device(&pn533_nfc_ops, protocols,
2529 NFC_SE_NONE,
2530 dev->ops->tx_header_len +
2448 PN533_CMD_DATAEXCH_HEAD_LEN, 2531 PN533_CMD_DATAEXCH_HEAD_LEN,
2449 PN533_FRAME_TAIL_SIZE); 2532 dev->ops->tx_tail_len);
2450 if (!dev->nfc_dev) 2533 if (!dev->nfc_dev)
2451 goto destroy_wq; 2534 goto destroy_wq;
2452 2535
@@ -2472,9 +2555,7 @@ free_nfc_dev:
2472destroy_wq: 2555destroy_wq:
2473 destroy_workqueue(dev->wq); 2556 destroy_workqueue(dev->wq);
2474error: 2557error:
2475 kfree(dev->in_frame);
2476 usb_free_urb(dev->in_urb); 2558 usb_free_urb(dev->in_urb);
2477 kfree(dev->out_frame);
2478 usb_free_urb(dev->out_urb); 2559 usb_free_urb(dev->out_urb);
2479 kfree(dev); 2560 kfree(dev);
2480 return rc; 2561 return rc;
@@ -2505,9 +2586,7 @@ static void pn533_disconnect(struct usb_interface *interface)
2505 kfree(cmd); 2586 kfree(cmd);
2506 } 2587 }
2507 2588
2508 kfree(dev->in_frame);
2509 usb_free_urb(dev->in_urb); 2589 usb_free_urb(dev->in_urb);
2510 kfree(dev->out_frame);
2511 usb_free_urb(dev->out_urb); 2590 usb_free_urb(dev->out_urb);
2512 kfree(dev); 2591 kfree(dev);
2513 2592
diff --git a/drivers/nfc/pn544/Kconfig b/drivers/nfc/pn544/Kconfig
new file mode 100644
index 000000000000..c277790ac71c
--- /dev/null
+++ b/drivers/nfc/pn544/Kconfig
@@ -0,0 +1,23 @@
1config NFC_PN544
2 tristate "NXP PN544 NFC driver"
3 depends on NFC_HCI
4 select CRC_CCITT
5 default n
6 ---help---
7 NXP PN544 core driver.
8 This is a driver based on the HCI NFC kernel layers and
9 will thus not work with NXP libnfc library.
10
11 To compile this driver as a module, choose m here. The module will
12 be called pn544.
13 Say N if unsure.
14
15config NFC_PN544_I2C
16 tristate "NFC PN544 i2c support"
17 depends on NFC_PN544 && I2C && NFC_SHDLC
18 ---help---
19 This module adds support for the NXP pn544 i2c interface.
20 Select this if your platform is using the i2c bus.
21
22 If you choose to build a module, it'll be called pn544_i2c.
23 Say N if unsure. \ No newline at end of file
diff --git a/drivers/nfc/pn544/Makefile b/drivers/nfc/pn544/Makefile
index 725733881eb3..ac076793687d 100644
--- a/drivers/nfc/pn544/Makefile
+++ b/drivers/nfc/pn544/Makefile
@@ -2,6 +2,7 @@
2# Makefile for PN544 HCI based NFC driver 2# Makefile for PN544 HCI based NFC driver
3# 3#
4 4
5obj-$(CONFIG_PN544_HCI_NFC) += pn544_i2c.o 5pn544_i2c-objs = i2c.o
6 6
7pn544_i2c-y := pn544.o i2c.o 7obj-$(CONFIG_NFC_PN544) += pn544.o
8obj-$(CONFIG_NFC_PN544_I2C) += pn544_i2c.o
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c
index 2a9c8d93d2e8..8cf64c19f022 100644
--- a/drivers/nfc/pn544/i2c.c
+++ b/drivers/nfc/pn544/i2c.c
@@ -376,12 +376,12 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
376 return -ENODEV; 376 return -ENODEV;
377 } 377 }
378 378
379 phy = kzalloc(sizeof(struct pn544_i2c_phy), GFP_KERNEL); 379 phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
380 GFP_KERNEL);
380 if (!phy) { 381 if (!phy) {
381 dev_err(&client->dev, 382 dev_err(&client->dev,
382 "Cannot allocate memory for pn544 i2c phy.\n"); 383 "Cannot allocate memory for pn544 i2c phy.\n");
383 r = -ENOMEM; 384 return -ENOMEM;
384 goto err_phy_alloc;
385 } 385 }
386 386
387 phy->i2c_dev = client; 387 phy->i2c_dev = client;
@@ -390,20 +390,18 @@ static int pn544_hci_i2c_probe(struct i2c_client *client,
390 pdata = client->dev.platform_data; 390 pdata = client->dev.platform_data;
391 if (pdata == NULL) { 391 if (pdata == NULL) {
392 dev_err(&client->dev, "No platform data\n"); 392 dev_err(&client->dev, "No platform data\n");
393 r = -EINVAL; 393 return -EINVAL;
394 goto err_pdata;
395 } 394 }
396 395
397 if (pdata->request_resources == NULL) { 396 if (pdata->request_resources == NULL) {
398 dev_err(&client->dev, "request_resources() missing\n"); 397 dev_err(&client->dev, "request_resources() missing\n");
399 r = -EINVAL; 398 return -EINVAL;
400 goto err_pdata;
401 } 399 }
402 400
403 r = pdata->request_resources(client); 401 r = pdata->request_resources(client);
404 if (r) { 402 if (r) {
405 dev_err(&client->dev, "Cannot get platform resources\n"); 403 dev_err(&client->dev, "Cannot get platform resources\n");
406 goto err_pdata; 404 return r;
407 } 405 }
408 406
409 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE); 407 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
@@ -435,10 +433,6 @@ err_rti:
435 if (pdata->free_resources != NULL) 433 if (pdata->free_resources != NULL)
436 pdata->free_resources(); 434 pdata->free_resources();
437 435
438err_pdata:
439 kfree(phy);
440
441err_phy_alloc:
442 return r; 436 return r;
443} 437}
444 438
@@ -458,8 +452,6 @@ static int pn544_hci_i2c_remove(struct i2c_client *client)
458 if (pdata->free_resources) 452 if (pdata->free_resources)
459 pdata->free_resources(); 453 pdata->free_resources();
460 454
461 kfree(phy);
462
463 return 0; 455 return 0;
464} 456}
465 457
@@ -472,29 +464,7 @@ static struct i2c_driver pn544_hci_i2c_driver = {
472 .remove = pn544_hci_i2c_remove, 464 .remove = pn544_hci_i2c_remove,
473}; 465};
474 466
475static int __init pn544_hci_i2c_init(void) 467module_i2c_driver(pn544_hci_i2c_driver);
476{
477 int r;
478
479 pr_debug(DRIVER_DESC ": %s\n", __func__);
480
481 r = i2c_add_driver(&pn544_hci_i2c_driver);
482 if (r) {
483 pr_err(PN544_HCI_I2C_DRIVER_NAME
484 ": driver registration failed\n");
485 return r;
486 }
487
488 return 0;
489}
490
491static void __exit pn544_hci_i2c_exit(void)
492{
493 i2c_del_driver(&pn544_hci_i2c_driver);
494}
495
496module_init(pn544_hci_i2c_init);
497module_exit(pn544_hci_i2c_exit);
498 468
499MODULE_LICENSE("GPL"); 469MODULE_LICENSE("GPL");
500MODULE_DESCRIPTION(DRIVER_DESC); 470MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/pn544/pn544.c b/drivers/nfc/pn544/pn544.c
index cc666de3b8e5..9c5f16e7baef 100644
--- a/drivers/nfc/pn544/pn544.c
+++ b/drivers/nfc/pn544/pn544.c
@@ -20,6 +20,7 @@
20 20
21#include <linux/delay.h> 21#include <linux/delay.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/module.h>
23 24
24#include <linux/nfc.h> 25#include <linux/nfc.h>
25#include <net/nfc/hci.h> 26#include <net/nfc/hci.h>
@@ -675,11 +676,17 @@ static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
675 676
676static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb) 677static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
677{ 678{
679 int r;
680
678 /* Set default false for multiple information chaining */ 681 /* Set default false for multiple information chaining */
679 *skb_push(skb, 1) = 0; 682 *skb_push(skb, 1) = 0;
680 683
681 return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE, 684 r = nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
682 PN544_HCI_EVT_SND_DATA, skb->data, skb->len); 685 PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
686
687 kfree_skb(skb);
688
689 return r;
683} 690}
684 691
685static int pn544_hci_check_presence(struct nfc_hci_dev *hdev, 692static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
@@ -714,35 +721,40 @@ static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
714 return 0; 721 return 0;
715} 722}
716 723
717static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, 724/*
718 u8 event, struct sk_buff *skb) 725 * Returns:
726 * <= 0: driver handled the event, skb consumed
727 * 1: driver does not handle the event, please do standard processing
728 */
729static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
730 struct sk_buff *skb)
719{ 731{
720 struct sk_buff *rgb_skb = NULL; 732 struct sk_buff *rgb_skb = NULL;
721 int r = 0; 733 int r;
722 734
723 pr_debug("hci event %d", event); 735 pr_debug("hci event %d", event);
724 switch (event) { 736 switch (event) {
725 case PN544_HCI_EVT_ACTIVATED: 737 case PN544_HCI_EVT_ACTIVATED:
726 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) 738 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) {
727 nfc_hci_target_discovered(hdev, gate); 739 r = nfc_hci_target_discovered(hdev, gate);
728 else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) { 740 } else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
729 r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ, 741 r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
730 &rgb_skb); 742 &rgb_skb);
731
732 if (r < 0) 743 if (r < 0)
733 goto exit; 744 goto exit;
734 745
735 nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK, 746 r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
736 NFC_COMM_PASSIVE, rgb_skb->data, 747 NFC_COMM_PASSIVE, rgb_skb->data,
737 rgb_skb->len); 748 rgb_skb->len);
738 749
739 kfree_skb(rgb_skb); 750 kfree_skb(rgb_skb);
751 } else {
752 r = -EINVAL;
740 } 753 }
741
742 break; 754 break;
743 case PN544_HCI_EVT_DEACTIVATED: 755 case PN544_HCI_EVT_DEACTIVATED:
744 nfc_hci_send_event(hdev, gate, 756 r = nfc_hci_send_event(hdev, gate, NFC_HCI_EVT_END_OPERATION,
745 NFC_HCI_EVT_END_OPERATION, NULL, 0); 757 NULL, 0);
746 break; 758 break;
747 case PN544_HCI_EVT_RCV_DATA: 759 case PN544_HCI_EVT_RCV_DATA:
748 if (skb->len < 2) { 760 if (skb->len < 2) {
@@ -757,15 +769,15 @@ static void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
757 } 769 }
758 770
759 skb_pull(skb, 2); 771 skb_pull(skb, 2);
760 nfc_tm_data_received(hdev->ndev, skb); 772 return nfc_tm_data_received(hdev->ndev, skb);
761
762 return;
763 default: 773 default:
764 break; 774 return 1;
765 } 775 }
766 776
767exit: 777exit:
768 kfree_skb(skb); 778 kfree_skb(skb);
779
780 return r;
769} 781}
770 782
771static struct nfc_hci_ops pn544_hci_ops = { 783static struct nfc_hci_ops pn544_hci_ops = {
@@ -789,7 +801,7 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
789 struct nfc_hci_dev **hdev) 801 struct nfc_hci_dev **hdev)
790{ 802{
791 struct pn544_hci_info *info; 803 struct pn544_hci_info *info;
792 u32 protocols; 804 u32 protocols, se;
793 struct nfc_hci_init_data init_data; 805 struct nfc_hci_init_data init_data;
794 int r; 806 int r;
795 807
@@ -822,8 +834,10 @@ int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
822 NFC_PROTO_ISO14443_B_MASK | 834 NFC_PROTO_ISO14443_B_MASK |
823 NFC_PROTO_NFC_DEP_MASK; 835 NFC_PROTO_NFC_DEP_MASK;
824 836
825 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 837 se = NFC_SE_UICC | NFC_SE_EMBEDDED;
826 protocols, llc_name, 838
839 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 0,
840 protocols, se, llc_name,
827 phy_headroom + PN544_CMDS_HEADROOM, 841 phy_headroom + PN544_CMDS_HEADROOM,
828 phy_tailroom, phy_payload); 842 phy_tailroom, phy_payload);
829 if (!info->hdev) { 843 if (!info->hdev) {
@@ -851,6 +865,7 @@ err_alloc_hdev:
851err_info_alloc: 865err_info_alloc:
852 return r; 866 return r;
853} 867}
868EXPORT_SYMBOL(pn544_hci_probe);
854 869
855void pn544_hci_remove(struct nfc_hci_dev *hdev) 870void pn544_hci_remove(struct nfc_hci_dev *hdev)
856{ 871{
@@ -860,3 +875,7 @@ void pn544_hci_remove(struct nfc_hci_dev *hdev)
860 nfc_hci_free_device(hdev); 875 nfc_hci_free_device(hdev);
861 kfree(info); 876 kfree(info);
862} 877}
878EXPORT_SYMBOL(pn544_hci_remove);
879
880MODULE_LICENSE("GPL");
881MODULE_DESCRIPTION(DRIVER_DESC);