aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc/pn544_hci.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/nfc/pn544_hci.c')
-rw-r--r--drivers/nfc/pn544_hci.c177
1 files changed, 120 insertions, 57 deletions
diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
index aa71807189ba..c9c8570273ab 100644
--- a/drivers/nfc/pn544_hci.c
+++ b/drivers/nfc/pn544_hci.c
@@ -29,7 +29,7 @@
29 29
30#include <linux/nfc.h> 30#include <linux/nfc.h>
31#include <net/nfc/hci.h> 31#include <net/nfc/hci.h>
32#include <net/nfc/shdlc.h> 32#include <net/nfc/llc.h>
33 33
34#include <linux/nfc/pn544.h> 34#include <linux/nfc/pn544.h>
35 35
@@ -128,10 +128,12 @@ static struct nfc_hci_gate pn544_gates[] = {
128 128
129/* Largest headroom needed for outgoing custom commands */ 129/* Largest headroom needed for outgoing custom commands */
130#define PN544_CMDS_HEADROOM 2 130#define PN544_CMDS_HEADROOM 2
131#define PN544_FRAME_HEADROOM 1
132#define PN544_FRAME_TAILROOM 2
131 133
132struct pn544_hci_info { 134struct pn544_hci_info {
133 struct i2c_client *i2c_dev; 135 struct i2c_client *i2c_dev;
134 struct nfc_shdlc *shdlc; 136 struct nfc_hci_dev *hdev;
135 137
136 enum pn544_state state; 138 enum pn544_state state;
137 139
@@ -146,6 +148,9 @@ struct pn544_hci_info {
146 * < 0 if hardware error occured (e.g. i2c err) 148 * < 0 if hardware error occured (e.g. i2c err)
147 * and prevents normal operation. 149 * and prevents normal operation.
148 */ 150 */
151 int async_cb_type;
152 data_exchange_cb_t async_cb;
153 void *async_cb_context;
149}; 154};
150 155
151static void pn544_hci_platform_init(struct pn544_hci_info *info) 156static void pn544_hci_platform_init(struct pn544_hci_info *info)
@@ -230,8 +235,12 @@ static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
230 r = i2c_master_send(client, buf, len); 235 r = i2c_master_send(client, buf, len);
231 } 236 }
232 237
233 if (r >= 0 && r != len) 238 if (r >= 0) {
234 r = -EREMOTEIO; 239 if (r != len)
240 return -EREMOTEIO;
241 else
242 return 0;
243 }
235 244
236 return r; 245 return r;
237} 246}
@@ -341,13 +350,16 @@ flush:
341static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id) 350static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
342{ 351{
343 struct pn544_hci_info *info = dev_id; 352 struct pn544_hci_info *info = dev_id;
344 struct i2c_client *client = info->i2c_dev; 353 struct i2c_client *client;
345 struct sk_buff *skb = NULL; 354 struct sk_buff *skb = NULL;
346 int r; 355 int r;
347 356
348 BUG_ON(!info); 357 if (!info || irq != info->i2c_dev->irq) {
349 BUG_ON(irq != info->i2c_dev->irq); 358 WARN_ON_ONCE(1);
359 return IRQ_NONE;
360 }
350 361
362 client = info->i2c_dev;
351 dev_dbg(&client->dev, "IRQ\n"); 363 dev_dbg(&client->dev, "IRQ\n");
352 364
353 if (info->hard_fault != 0) 365 if (info->hard_fault != 0)
@@ -357,21 +369,21 @@ static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
357 if (r == -EREMOTEIO) { 369 if (r == -EREMOTEIO) {
358 info->hard_fault = r; 370 info->hard_fault = r;
359 371
360 nfc_shdlc_recv_frame(info->shdlc, NULL); 372 nfc_hci_recv_frame(info->hdev, NULL);
361 373
362 return IRQ_HANDLED; 374 return IRQ_HANDLED;
363 } else if ((r == -ENOMEM) || (r == -EBADMSG)) { 375 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
364 return IRQ_HANDLED; 376 return IRQ_HANDLED;
365 } 377 }
366 378
367 nfc_shdlc_recv_frame(info->shdlc, skb); 379 nfc_hci_recv_frame(info->hdev, skb);
368 380
369 return IRQ_HANDLED; 381 return IRQ_HANDLED;
370} 382}
371 383
372static int pn544_hci_open(struct nfc_shdlc *shdlc) 384static int pn544_hci_open(struct nfc_hci_dev *hdev)
373{ 385{
374 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 386 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
375 int r = 0; 387 int r = 0;
376 388
377 mutex_lock(&info->info_lock); 389 mutex_lock(&info->info_lock);
@@ -391,9 +403,9 @@ out:
391 return r; 403 return r;
392} 404}
393 405
394static void pn544_hci_close(struct nfc_shdlc *shdlc) 406static void pn544_hci_close(struct nfc_hci_dev *hdev)
395{ 407{
396 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 408 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
397 409
398 mutex_lock(&info->info_lock); 410 mutex_lock(&info->info_lock);
399 411
@@ -408,9 +420,8 @@ out:
408 mutex_unlock(&info->info_lock); 420 mutex_unlock(&info->info_lock);
409} 421}
410 422
411static int pn544_hci_ready(struct nfc_shdlc *shdlc) 423static int pn544_hci_ready(struct nfc_hci_dev *hdev)
412{ 424{
413 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
414 struct sk_buff *skb; 425 struct sk_buff *skb;
415 static struct hw_config { 426 static struct hw_config {
416 u8 adr[2]; 427 u8 adr[2];
@@ -576,21 +587,45 @@ static int pn544_hci_ready(struct nfc_shdlc *shdlc)
576 return 0; 587 return 0;
577} 588}
578 589
579static int pn544_hci_xmit(struct nfc_shdlc *shdlc, struct sk_buff *skb) 590static void pn544_hci_add_len_crc(struct sk_buff *skb)
580{ 591{
581 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 592 u16 crc;
593 int len;
594
595 len = skb->len + 2;
596 *skb_push(skb, 1) = len;
597
598 crc = crc_ccitt(0xffff, skb->data, skb->len);
599 crc = ~crc;
600 *skb_put(skb, 1) = crc & 0xff;
601 *skb_put(skb, 1) = crc >> 8;
602}
603
604static void pn544_hci_remove_len_crc(struct sk_buff *skb)
605{
606 skb_pull(skb, PN544_FRAME_HEADROOM);
607 skb_trim(skb, PN544_FRAME_TAILROOM);
608}
609
610static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
611{
612 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
582 struct i2c_client *client = info->i2c_dev; 613 struct i2c_client *client = info->i2c_dev;
614 int r;
583 615
584 if (info->hard_fault != 0) 616 if (info->hard_fault != 0)
585 return info->hard_fault; 617 return info->hard_fault;
586 618
587 return pn544_hci_i2c_write(client, skb->data, skb->len); 619 pn544_hci_add_len_crc(skb);
620 r = pn544_hci_i2c_write(client, skb->data, skb->len);
621 pn544_hci_remove_len_crc(skb);
622
623 return r;
588} 624}
589 625
590static int pn544_hci_start_poll(struct nfc_shdlc *shdlc, 626static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
591 u32 im_protocols, u32 tm_protocols) 627 u32 im_protocols, u32 tm_protocols)
592{ 628{
593 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
594 u8 phases = 0; 629 u8 phases = 0;
595 int r; 630 int r;
596 u8 duration[2]; 631 u8 duration[2];
@@ -641,7 +676,7 @@ static int pn544_hci_start_poll(struct nfc_shdlc *shdlc,
641 return r; 676 return r;
642} 677}
643 678
644static int pn544_hci_target_from_gate(struct nfc_shdlc *shdlc, u8 gate, 679static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
645 struct nfc_target *target) 680 struct nfc_target *target)
646{ 681{
647 switch (gate) { 682 switch (gate) {
@@ -659,11 +694,10 @@ static int pn544_hci_target_from_gate(struct nfc_shdlc *shdlc, u8 gate,
659 return 0; 694 return 0;
660} 695}
661 696
662static int pn544_hci_complete_target_discovered(struct nfc_shdlc *shdlc, 697static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
663 u8 gate, 698 u8 gate,
664 struct nfc_target *target) 699 struct nfc_target *target)
665{ 700{
666 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
667 struct sk_buff *uid_skb; 701 struct sk_buff *uid_skb;
668 int r = 0; 702 int r = 0;
669 703
@@ -704,6 +738,26 @@ static int pn544_hci_complete_target_discovered(struct nfc_shdlc *shdlc,
704 return r; 738 return r;
705} 739}
706 740
741#define PN544_CB_TYPE_READER_F 1
742
743static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
744 int err)
745{
746 struct pn544_hci_info *info = context;
747
748 switch (info->async_cb_type) {
749 case PN544_CB_TYPE_READER_F:
750 if (err == 0)
751 skb_pull(skb, 1);
752 info->async_cb(info->async_cb_context, skb, err);
753 break;
754 default:
755 if (err == 0)
756 kfree_skb(skb);
757 break;
758 }
759}
760
707#define MIFARE_CMD_AUTH_KEY_A 0x60 761#define MIFARE_CMD_AUTH_KEY_A 0x60
708#define MIFARE_CMD_AUTH_KEY_B 0x61 762#define MIFARE_CMD_AUTH_KEY_B 0x61
709#define MIFARE_CMD_HEADER 2 763#define MIFARE_CMD_HEADER 2
@@ -715,13 +769,12 @@ static int pn544_hci_complete_target_discovered(struct nfc_shdlc *shdlc,
715 * <= 0: driver handled the data exchange 769 * <= 0: driver handled the data exchange
716 * 1: driver doesn't especially handle, please do standard processing 770 * 1: driver doesn't especially handle, please do standard processing
717 */ 771 */
718static int pn544_hci_data_exchange(struct nfc_shdlc *shdlc, 772static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev,
719 struct nfc_target *target, 773 struct nfc_target *target,
720 struct sk_buff *skb, 774 struct sk_buff *skb, data_exchange_cb_t cb,
721 struct sk_buff **res_skb) 775 void *cb_context)
722{ 776{
723 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc); 777 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
724 int r;
725 778
726 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__, 779 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
727 target->hci_reader_gate); 780 target->hci_reader_gate);
@@ -746,41 +799,43 @@ static int pn544_hci_data_exchange(struct nfc_shdlc *shdlc,
746 memcpy(data, uid, MIFARE_UID_LEN); 799 memcpy(data, uid, MIFARE_UID_LEN);
747 } 800 }
748 801
749 return nfc_hci_send_cmd(hdev, target->hci_reader_gate, 802 return nfc_hci_send_cmd_async(hdev,
750 PN544_MIFARE_CMD, 803 target->hci_reader_gate,
751 skb->data, skb->len, res_skb); 804 PN544_MIFARE_CMD,
805 skb->data, skb->len,
806 cb, cb_context);
752 } else 807 } else
753 return 1; 808 return 1;
754 case PN544_RF_READER_F_GATE: 809 case PN544_RF_READER_F_GATE:
755 *skb_push(skb, 1) = 0; 810 *skb_push(skb, 1) = 0;
756 *skb_push(skb, 1) = 0; 811 *skb_push(skb, 1) = 0;
757 812
758 r = nfc_hci_send_cmd(hdev, target->hci_reader_gate, 813 info->async_cb_type = PN544_CB_TYPE_READER_F;
759 PN544_FELICA_RAW, 814 info->async_cb = cb;
760 skb->data, skb->len, res_skb); 815 info->async_cb_context = cb_context;
761 if (r == 0) 816
762 skb_pull(*res_skb, 1); 817 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
763 return r; 818 PN544_FELICA_RAW, skb->data,
819 skb->len,
820 pn544_hci_data_exchange_cb, info);
764 case PN544_RF_READER_JEWEL_GATE: 821 case PN544_RF_READER_JEWEL_GATE:
765 return nfc_hci_send_cmd(hdev, target->hci_reader_gate, 822 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
766 PN544_JEWEL_RAW_CMD, 823 PN544_JEWEL_RAW_CMD, skb->data,
767 skb->data, skb->len, res_skb); 824 skb->len, cb, cb_context);
768 default: 825 default:
769 return 1; 826 return 1;
770 } 827 }
771} 828}
772 829
773static int pn544_hci_check_presence(struct nfc_shdlc *shdlc, 830static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
774 struct nfc_target *target) 831 struct nfc_target *target)
775{ 832{
776 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
777
778 return nfc_hci_send_cmd(hdev, target->hci_reader_gate, 833 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
779 PN544_RF_READER_CMD_PRESENCE_CHECK, 834 PN544_RF_READER_CMD_PRESENCE_CHECK,
780 NULL, 0, NULL); 835 NULL, 0, NULL);
781} 836}
782 837
783static struct nfc_shdlc_ops pn544_shdlc_ops = { 838static struct nfc_hci_ops pn544_hci_ops = {
784 .open = pn544_hci_open, 839 .open = pn544_hci_open,
785 .close = pn544_hci_close, 840 .close = pn544_hci_close,
786 .hci_ready = pn544_hci_ready, 841 .hci_ready = pn544_hci_ready,
@@ -848,8 +903,8 @@ static int __devinit pn544_hci_probe(struct i2c_client *client,
848 pn544_hci_platform_init(info); 903 pn544_hci_platform_init(info);
849 904
850 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn, 905 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
851 IRQF_TRIGGER_RISING, PN544_HCI_DRIVER_NAME, 906 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
852 info); 907 PN544_HCI_DRIVER_NAME, info);
853 if (r < 0) { 908 if (r < 0) {
854 dev_err(&client->dev, "Unable to register IRQ handler\n"); 909 dev_err(&client->dev, "Unable to register IRQ handler\n");
855 goto err_rti; 910 goto err_rti;
@@ -872,22 +927,30 @@ static int __devinit pn544_hci_probe(struct i2c_client *client,
872 NFC_PROTO_ISO14443_B_MASK | 927 NFC_PROTO_ISO14443_B_MASK |
873 NFC_PROTO_NFC_DEP_MASK; 928 NFC_PROTO_NFC_DEP_MASK;
874 929
875 info->shdlc = nfc_shdlc_allocate(&pn544_shdlc_ops, 930 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
876 &init_data, protocols, 931 protocols, LLC_SHDLC_NAME,
877 PN544_CMDS_HEADROOM, 0, 932 PN544_FRAME_HEADROOM +
878 PN544_HCI_LLC_MAX_PAYLOAD, 933 PN544_CMDS_HEADROOM,
879 dev_name(&client->dev)); 934 PN544_FRAME_TAILROOM,
880 if (!info->shdlc) { 935 PN544_HCI_LLC_MAX_PAYLOAD);
881 dev_err(&client->dev, "Cannot allocate nfc shdlc.\n"); 936 if (!info->hdev) {
937 dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
882 r = -ENOMEM; 938 r = -ENOMEM;
883 goto err_allocshdlc; 939 goto err_alloc_hdev;
884 } 940 }
885 941
886 nfc_shdlc_set_clientdata(info->shdlc, info); 942 nfc_hci_set_clientdata(info->hdev, info);
943
944 r = nfc_hci_register_device(info->hdev);
945 if (r)
946 goto err_regdev;
887 947
888 return 0; 948 return 0;
889 949
890err_allocshdlc: 950err_regdev:
951 nfc_hci_free_device(info->hdev);
952
953err_alloc_hdev:
891 free_irq(client->irq, info); 954 free_irq(client->irq, info);
892 955
893err_rti: 956err_rti:
@@ -908,7 +971,7 @@ static __devexit int pn544_hci_remove(struct i2c_client *client)
908 971
909 dev_dbg(&client->dev, "%s\n", __func__); 972 dev_dbg(&client->dev, "%s\n", __func__);
910 973
911 nfc_shdlc_free(info->shdlc); 974 nfc_hci_free_device(info->hdev);
912 975
913 if (info->state != PN544_ST_COLD) { 976 if (info->state != PN544_ST_COLD) {
914 if (pdata->disable) 977 if (pdata->disable)