aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Lapuyade <eric.lapuyade@linux.intel.com>2012-09-18 13:45:48 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2012-09-24 18:17:26 -0400
commit412fda538f4b1317ecd0fbe6e5bc9124792bea88 (patch)
treeafd09c49880919110dd1dc25027b75a63f8dd169
parent4a61cd6687fc6348d08724676d34e38160d6cf9b (diff)
NFC: Changed HCI and PN544 HCI driver to use the new HCI LLC Core
The previous shdlc HCI driver and its header are removed from the tree. PN544 now registers directly with HCI and passes the name of the llc it requires (shdlc). HCI instantiation now allocates the required llc instance. The llc is started when the HCI device is brought up. Signed-off-by: Eric Lapuyade <eric.lapuyade@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
-rw-r--r--drivers/nfc/Kconfig2
-rw-r--r--drivers/nfc/pn544_hci.c73
-rw-r--r--include/net/nfc/hci.h3
-rw-r--r--include/net/nfc/shdlc.h106
-rw-r--r--net/nfc/hci/Makefile2
-rw-r--r--net/nfc/hci/core.c210
-rw-r--r--net/nfc/hci/llc_shdlc.c2
-rw-r--r--net/nfc/hci/shdlc.c918
8 files changed, 164 insertions, 1152 deletions
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index 3b20b73ee649..89c57d1a7cad 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -19,7 +19,7 @@ config PN544_NFC
19 19
20config PN544_HCI_NFC 20config PN544_HCI_NFC
21 tristate "HCI PN544 NFC driver" 21 tristate "HCI PN544 NFC driver"
22 depends on I2C && NFC_SHDLC 22 depends on I2C && NFC_HCI && NFC_SHDLC
23 select CRC_CCITT 23 select CRC_CCITT
24 default n 24 default n
25 ---help--- 25 ---help---
diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544_hci.c
index a176d1f7bbf3..e681da2e3413 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
@@ -133,7 +133,7 @@ static struct nfc_hci_gate pn544_gates[] = {
133 133
134struct pn544_hci_info { 134struct pn544_hci_info {
135 struct i2c_client *i2c_dev; 135 struct i2c_client *i2c_dev;
136 struct nfc_shdlc *shdlc; 136 struct nfc_hci_dev *hdev;
137 137
138 enum pn544_state state; 138 enum pn544_state state;
139 139
@@ -362,21 +362,21 @@ static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
362 if (r == -EREMOTEIO) { 362 if (r == -EREMOTEIO) {
363 info->hard_fault = r; 363 info->hard_fault = r;
364 364
365 nfc_shdlc_recv_frame(info->shdlc, NULL); 365 nfc_hci_recv_frame(info->hdev, NULL);
366 366
367 return IRQ_HANDLED; 367 return IRQ_HANDLED;
368 } else if ((r == -ENOMEM) || (r == -EBADMSG)) { 368 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
369 return IRQ_HANDLED; 369 return IRQ_HANDLED;
370 } 370 }
371 371
372 nfc_shdlc_recv_frame(info->shdlc, skb); 372 nfc_hci_recv_frame(info->hdev, skb);
373 373
374 return IRQ_HANDLED; 374 return IRQ_HANDLED;
375} 375}
376 376
377static int pn544_hci_open(struct nfc_shdlc *shdlc) 377static int pn544_hci_open(struct nfc_hci_dev *hdev)
378{ 378{
379 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 379 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
380 int r = 0; 380 int r = 0;
381 381
382 mutex_lock(&info->info_lock); 382 mutex_lock(&info->info_lock);
@@ -396,9 +396,9 @@ out:
396 return r; 396 return r;
397} 397}
398 398
399static void pn544_hci_close(struct nfc_shdlc *shdlc) 399static void pn544_hci_close(struct nfc_hci_dev *hdev)
400{ 400{
401 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 401 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
402 402
403 mutex_lock(&info->info_lock); 403 mutex_lock(&info->info_lock);
404 404
@@ -413,9 +413,8 @@ out:
413 mutex_unlock(&info->info_lock); 413 mutex_unlock(&info->info_lock);
414} 414}
415 415
416static int pn544_hci_ready(struct nfc_shdlc *shdlc) 416static int pn544_hci_ready(struct nfc_hci_dev *hdev)
417{ 417{
418 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
419 struct sk_buff *skb; 418 struct sk_buff *skb;
420 static struct hw_config { 419 static struct hw_config {
421 u8 adr[2]; 420 u8 adr[2];
@@ -601,9 +600,9 @@ static void pn544_hci_remove_len_crc(struct sk_buff *skb)
601 skb_trim(skb, PN544_FRAME_TAILROOM); 600 skb_trim(skb, PN544_FRAME_TAILROOM);
602} 601}
603 602
604static int pn544_hci_xmit(struct nfc_shdlc *shdlc, struct sk_buff *skb) 603static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
605{ 604{
606 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 605 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
607 struct i2c_client *client = info->i2c_dev; 606 struct i2c_client *client = info->i2c_dev;
608 int r; 607 int r;
609 608
@@ -617,10 +616,9 @@ static int pn544_hci_xmit(struct nfc_shdlc *shdlc, struct sk_buff *skb)
617 return r; 616 return r;
618} 617}
619 618
620static int pn544_hci_start_poll(struct nfc_shdlc *shdlc, 619static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
621 u32 im_protocols, u32 tm_protocols) 620 u32 im_protocols, u32 tm_protocols)
622{ 621{
623 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
624 u8 phases = 0; 622 u8 phases = 0;
625 int r; 623 int r;
626 u8 duration[2]; 624 u8 duration[2];
@@ -671,7 +669,7 @@ static int pn544_hci_start_poll(struct nfc_shdlc *shdlc,
671 return r; 669 return r;
672} 670}
673 671
674static int pn544_hci_target_from_gate(struct nfc_shdlc *shdlc, u8 gate, 672static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
675 struct nfc_target *target) 673 struct nfc_target *target)
676{ 674{
677 switch (gate) { 675 switch (gate) {
@@ -689,11 +687,10 @@ static int pn544_hci_target_from_gate(struct nfc_shdlc *shdlc, u8 gate,
689 return 0; 687 return 0;
690} 688}
691 689
692static int pn544_hci_complete_target_discovered(struct nfc_shdlc *shdlc, 690static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
693 u8 gate, 691 u8 gate,
694 struct nfc_target *target) 692 struct nfc_target *target)
695{ 693{
696 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
697 struct sk_buff *uid_skb; 694 struct sk_buff *uid_skb;
698 int r = 0; 695 int r = 0;
699 696
@@ -765,13 +762,12 @@ static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
765 * <= 0: driver handled the data exchange 762 * <= 0: driver handled the data exchange
766 * 1: driver doesn't especially handle, please do standard processing 763 * 1: driver doesn't especially handle, please do standard processing
767 */ 764 */
768static int pn544_hci_data_exchange(struct nfc_shdlc *shdlc, 765static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev,
769 struct nfc_target *target, 766 struct nfc_target *target,
770 struct sk_buff *skb, data_exchange_cb_t cb, 767 struct sk_buff *skb, data_exchange_cb_t cb,
771 void *cb_context) 768 void *cb_context)
772{ 769{
773 struct pn544_hci_info *info = nfc_shdlc_get_clientdata(shdlc); 770 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
774 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
775 771
776 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__, 772 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
777 target->hci_reader_gate); 773 target->hci_reader_gate);
@@ -824,17 +820,15 @@ static int pn544_hci_data_exchange(struct nfc_shdlc *shdlc,
824 } 820 }
825} 821}
826 822
827static int pn544_hci_check_presence(struct nfc_shdlc *shdlc, 823static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
828 struct nfc_target *target) 824 struct nfc_target *target)
829{ 825{
830 struct nfc_hci_dev *hdev = nfc_shdlc_get_hci_dev(shdlc);
831
832 return nfc_hci_send_cmd(hdev, target->hci_reader_gate, 826 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
833 PN544_RF_READER_CMD_PRESENCE_CHECK, 827 PN544_RF_READER_CMD_PRESENCE_CHECK,
834 NULL, 0, NULL); 828 NULL, 0, NULL);
835} 829}
836 830
837static struct nfc_shdlc_ops pn544_shdlc_ops = { 831static struct nfc_hci_ops pn544_hci_ops = {
838 .open = pn544_hci_open, 832 .open = pn544_hci_open,
839 .close = pn544_hci_close, 833 .close = pn544_hci_close,
840 .hci_ready = pn544_hci_ready, 834 .hci_ready = pn544_hci_ready,
@@ -926,23 +920,30 @@ static int __devinit pn544_hci_probe(struct i2c_client *client,
926 NFC_PROTO_ISO14443_B_MASK | 920 NFC_PROTO_ISO14443_B_MASK |
927 NFC_PROTO_NFC_DEP_MASK; 921 NFC_PROTO_NFC_DEP_MASK;
928 922
929 info->shdlc = nfc_shdlc_allocate(&pn544_shdlc_ops, 923 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
930 &init_data, protocols, 924 protocols, LLC_SHDLC_NAME,
931 PN544_FRAME_HEADROOM + PN544_CMDS_HEADROOM, 925 PN544_FRAME_HEADROOM +
932 PN544_FRAME_TAILROOM, 926 PN544_CMDS_HEADROOM,
933 PN544_HCI_LLC_MAX_PAYLOAD, 927 PN544_FRAME_TAILROOM,
934 dev_name(&client->dev)); 928 PN544_HCI_LLC_MAX_PAYLOAD);
935 if (!info->shdlc) { 929 if (!info->hdev) {
936 dev_err(&client->dev, "Cannot allocate nfc shdlc.\n"); 930 dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
937 r = -ENOMEM; 931 r = -ENOMEM;
938 goto err_allocshdlc; 932 goto err_alloc_hdev;
939 } 933 }
940 934
941 nfc_shdlc_set_clientdata(info->shdlc, info); 935 nfc_hci_set_clientdata(info->hdev, info);
936
937 r = nfc_hci_register_device(info->hdev);
938 if (r)
939 goto err_regdev;
942 940
943 return 0; 941 return 0;
944 942
945err_allocshdlc: 943err_regdev:
944 nfc_hci_free_device(info->hdev);
945
946err_alloc_hdev:
946 free_irq(client->irq, info); 947 free_irq(client->irq, info);
947 948
948err_rti: 949err_rti:
@@ -963,7 +964,7 @@ static __devexit int pn544_hci_remove(struct i2c_client *client)
963 964
964 dev_dbg(&client->dev, "%s\n", __func__); 965 dev_dbg(&client->dev, "%s\n", __func__);
965 966
966 nfc_shdlc_free(info->shdlc); 967 nfc_hci_free_device(info->hdev);
967 968
968 if (info->state != PN544_ST_COLD) { 969 if (info->state != PN544_ST_COLD) {
969 if (pdata->disable) 970 if (pdata->disable)
diff --git a/include/net/nfc/hci.h b/include/net/nfc/hci.h
index 6cee6e21fc46..9b5ed2d94d60 100644
--- a/include/net/nfc/hci.h
+++ b/include/net/nfc/hci.h
@@ -87,6 +87,8 @@ struct nfc_hci_dev {
87 87
88 struct nfc_hci_ops *ops; 88 struct nfc_hci_ops *ops;
89 89
90 struct nfc_llc *llc;
91
90 struct nfc_hci_init_data init_data; 92 struct nfc_hci_init_data init_data;
91 93
92 void *clientdata; 94 void *clientdata;
@@ -113,6 +115,7 @@ struct nfc_hci_dev {
113struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, 115struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
114 struct nfc_hci_init_data *init_data, 116 struct nfc_hci_init_data *init_data,
115 u32 protocols, 117 u32 protocols,
118 const char *llc_name,
116 int tx_headroom, 119 int tx_headroom,
117 int tx_tailroom, 120 int tx_tailroom,
118 int max_link_payload); 121 int max_link_payload);
diff --git a/include/net/nfc/shdlc.h b/include/net/nfc/shdlc.h
deleted file mode 100644
index fe5e19829352..000000000000
--- a/include/net/nfc/shdlc.h
+++ /dev/null
@@ -1,106 +0,0 @@
1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#ifndef __NFC_SHDLC_H
21#define __NFC_SHDLC_H
22
23struct nfc_shdlc;
24
25struct nfc_shdlc_ops {
26 int (*open) (struct nfc_shdlc *shdlc);
27 void (*close) (struct nfc_shdlc *shdlc);
28 int (*hci_ready) (struct nfc_shdlc *shdlc);
29 int (*xmit) (struct nfc_shdlc *shdlc, struct sk_buff *skb);
30 int (*start_poll) (struct nfc_shdlc *shdlc,
31 u32 im_protocols, u32 tm_protocols);
32 int (*target_from_gate) (struct nfc_shdlc *shdlc, u8 gate,
33 struct nfc_target *target);
34 int (*complete_target_discovered) (struct nfc_shdlc *shdlc, u8 gate,
35 struct nfc_target *target);
36 int (*data_exchange) (struct nfc_shdlc *shdlc,
37 struct nfc_target *target, struct sk_buff *skb,
38 data_exchange_cb_t cb, void *cb_context);
39 int (*check_presence)(struct nfc_shdlc *shdlc,
40 struct nfc_target *target);
41};
42
43enum shdlc_state {
44 SHDLC_DISCONNECTED = 0,
45 SHDLC_CONNECTING = 1,
46 SHDLC_NEGOCIATING = 2,
47 SHDLC_CONNECTED = 3
48};
49
50struct nfc_shdlc {
51 struct mutex state_mutex;
52 enum shdlc_state state;
53 int hard_fault;
54
55 struct nfc_hci_dev *hdev;
56
57 wait_queue_head_t *connect_wq;
58 int connect_tries;
59 int connect_result;
60 struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
61
62 u8 w; /* window size */
63 bool srej_support;
64
65 struct timer_list t1_timer; /* send ack timeout */
66 bool t1_active;
67
68 struct timer_list t2_timer; /* guard/retransmit timeout */
69 bool t2_active;
70
71 int ns; /* next seq num for send */
72 int nr; /* next expected seq num for receive */
73 int dnr; /* oldest sent unacked seq num */
74
75 struct sk_buff_head rcv_q;
76
77 struct sk_buff_head send_q;
78 bool rnr; /* other side is not ready to receive */
79
80 struct sk_buff_head ack_pending_q;
81
82 struct work_struct sm_work;
83
84 struct nfc_shdlc_ops *ops;
85
86 int client_headroom;
87 int client_tailroom;
88
89 void *clientdata;
90};
91
92void nfc_shdlc_recv_frame(struct nfc_shdlc *shdlc, struct sk_buff *skb);
93
94struct nfc_shdlc *nfc_shdlc_allocate(struct nfc_shdlc_ops *ops,
95 struct nfc_hci_init_data *init_data,
96 u32 protocols,
97 int tx_headroom, int tx_tailroom,
98 int max_link_payload, const char *devname);
99
100void nfc_shdlc_free(struct nfc_shdlc *shdlc);
101
102void nfc_shdlc_set_clientdata(struct nfc_shdlc *shdlc, void *clientdata);
103void *nfc_shdlc_get_clientdata(struct nfc_shdlc *shdlc);
104struct nfc_hci_dev *nfc_shdlc_get_hci_dev(struct nfc_shdlc *shdlc);
105
106#endif /* __NFC_SHDLC_H */
diff --git a/net/nfc/hci/Makefile b/net/nfc/hci/Makefile
index c4d65479629b..c5dbb6891b24 100644
--- a/net/nfc/hci/Makefile
+++ b/net/nfc/hci/Makefile
@@ -5,4 +5,4 @@
5obj-$(CONFIG_NFC_HCI) += hci.o 5obj-$(CONFIG_NFC_HCI) += hci.o
6 6
7hci-y := core.o hcp.o command.o llc.o llc_nop.o 7hci-y := core.o hcp.o command.o llc.o llc_nop.o
8hci-$(CONFIG_NFC_SHDLC) += shdlc.o llc_shdlc.o 8hci-$(CONFIG_NFC_SHDLC) += llc_shdlc.o
diff --git a/net/nfc/hci/core.c b/net/nfc/hci/core.c
index 069e2d6056e5..c1129c22d835 100644
--- a/net/nfc/hci/core.c
+++ b/net/nfc/hci/core.c
@@ -78,7 +78,7 @@ next_msg:
78 78
79 pr_debug("msg_tx_queue has a cmd to send\n"); 79 pr_debug("msg_tx_queue has a cmd to send\n");
80 while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) { 80 while ((skb = skb_dequeue(&msg->msg_frags)) != NULL) {
81 r = hdev->ops->xmit(hdev, skb); 81 r = nfc_llc_xmit_from_hci(hdev->llc, skb);
82 if (r < 0) { 82 if (r < 0) {
83 kfree_skb(skb); 83 kfree_skb(skb);
84 skb_queue_purge(&msg->msg_frags); 84 skb_queue_purge(&msg->msg_frags);
@@ -469,29 +469,38 @@ static int hci_dev_up(struct nfc_dev *nfc_dev)
469 return r; 469 return r;
470 } 470 }
471 471
472 r = nfc_llc_start(hdev->llc);
473 if (r < 0)
474 goto exit_close;
475
472 r = hci_dev_session_init(hdev); 476 r = hci_dev_session_init(hdev);
473 if (r < 0) 477 if (r < 0)
474 goto exit; 478 goto exit_llc;
475 479
476 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE, 480 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
477 NFC_HCI_EVT_END_OPERATION, NULL, 0); 481 NFC_HCI_EVT_END_OPERATION, NULL, 0);
478 if (r < 0) 482 if (r < 0)
479 goto exit; 483 goto exit_llc;
480 484
481 if (hdev->ops->hci_ready) { 485 if (hdev->ops->hci_ready) {
482 r = hdev->ops->hci_ready(hdev); 486 r = hdev->ops->hci_ready(hdev);
483 if (r < 0) 487 if (r < 0)
484 goto exit; 488 goto exit_llc;
485 } 489 }
486 490
487 r = hci_dev_version(hdev); 491 r = hci_dev_version(hdev);
488 if (r < 0) 492 if (r < 0)
489 goto exit; 493 goto exit_llc;
494
495 return 0;
496
497exit_llc:
498 nfc_llc_stop(hdev->llc);
499
500exit_close:
501 if (hdev->ops->close)
502 hdev->ops->close(hdev);
490 503
491exit:
492 if (r < 0)
493 if (hdev->ops->close)
494 hdev->ops->close(hdev);
495 return r; 504 return r;
496} 505}
497 506
@@ -499,6 +508,8 @@ static int hci_dev_down(struct nfc_dev *nfc_dev)
499{ 508{
500 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev); 509 struct nfc_hci_dev *hdev = nfc_get_drvdata(nfc_dev);
501 510
511 nfc_llc_stop(hdev->llc);
512
502 if (hdev->ops->close) 513 if (hdev->ops->close)
503 hdev->ops->close(hdev); 514 hdev->ops->close(hdev);
504 515
@@ -620,6 +631,93 @@ static int hci_check_presence(struct nfc_dev *nfc_dev,
620 return 0; 631 return 0;
621} 632}
622 633
634static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
635{
636 mutex_lock(&hdev->msg_tx_mutex);
637
638 if (hdev->cmd_pending_msg == NULL) {
639 nfc_driver_failure(hdev->ndev, err);
640 goto exit;
641 }
642
643 __nfc_hci_cmd_completion(hdev, err, NULL);
644
645exit:
646 mutex_unlock(&hdev->msg_tx_mutex);
647}
648
649static void nfc_hci_llc_failure(struct nfc_hci_dev *hdev, int err)
650{
651 nfc_hci_failure(hdev, err);
652}
653
654static void nfc_hci_recv_from_llc(struct nfc_hci_dev *hdev, struct sk_buff *skb)
655{
656 struct hcp_packet *packet;
657 u8 type;
658 u8 instruction;
659 struct sk_buff *hcp_skb;
660 u8 pipe;
661 struct sk_buff *frag_skb;
662 int msg_len;
663
664 packet = (struct hcp_packet *)skb->data;
665 if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
666 skb_queue_tail(&hdev->rx_hcp_frags, skb);
667 return;
668 }
669
670 /* it's the last fragment. Does it need re-aggregation? */
671 if (skb_queue_len(&hdev->rx_hcp_frags)) {
672 pipe = packet->header & NFC_HCI_FRAGMENT;
673 skb_queue_tail(&hdev->rx_hcp_frags, skb);
674
675 msg_len = 0;
676 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
677 msg_len += (frag_skb->len -
678 NFC_HCI_HCP_PACKET_HEADER_LEN);
679 }
680
681 hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
682 msg_len, GFP_KERNEL);
683 if (hcp_skb == NULL) {
684 nfc_hci_failure(hdev, -ENOMEM);
685 return;
686 }
687
688 *skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
689
690 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
691 msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
692 memcpy(skb_put(hcp_skb, msg_len),
693 frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
694 msg_len);
695 }
696
697 skb_queue_purge(&hdev->rx_hcp_frags);
698 } else {
699 packet->header &= NFC_HCI_FRAGMENT;
700 hcp_skb = skb;
701 }
702
703 /* if this is a response, dispatch immediately to
704 * unblock waiting cmd context. Otherwise, enqueue to dispatch
705 * in separate context where handler can also execute command.
706 */
707 packet = (struct hcp_packet *)hcp_skb->data;
708 type = HCP_MSG_GET_TYPE(packet->message.header);
709 if (type == NFC_HCI_HCP_RESPONSE) {
710 pipe = packet->header;
711 instruction = HCP_MSG_GET_CMD(packet->message.header);
712 skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
713 NFC_HCI_HCP_MESSAGE_HEADER_LEN);
714 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
715 } else {
716 skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
717 queue_work(system_nrt_wq, &hdev->msg_rx_work);
718 }
719}
720
623static struct nfc_ops hci_nfc_ops = { 721static struct nfc_ops hci_nfc_ops = {
624 .dev_up = hci_dev_up, 722 .dev_up = hci_dev_up,
625 .dev_down = hci_dev_down, 723 .dev_down = hci_dev_down,
@@ -634,6 +732,7 @@ static struct nfc_ops hci_nfc_ops = {
634struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops, 732struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
635 struct nfc_hci_init_data *init_data, 733 struct nfc_hci_init_data *init_data,
636 u32 protocols, 734 u32 protocols,
735 const char *llc_name,
637 int tx_headroom, 736 int tx_headroom,
638 int tx_tailroom, 737 int tx_tailroom,
639 int max_link_payload) 738 int max_link_payload)
@@ -650,10 +749,19 @@ struct nfc_hci_dev *nfc_hci_allocate_device(struct nfc_hci_ops *ops,
650 if (hdev == NULL) 749 if (hdev == NULL)
651 return NULL; 750 return NULL;
652 751
752 hdev->llc = nfc_llc_allocate(llc_name, hdev, ops->xmit,
753 nfc_hci_recv_from_llc, tx_headroom,
754 tx_tailroom, nfc_hci_llc_failure);
755 if (hdev->llc == NULL) {
756 kfree(hdev);
757 return NULL;
758 }
759
653 hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols, 760 hdev->ndev = nfc_allocate_device(&hci_nfc_ops, protocols,
654 tx_headroom + HCI_CMDS_HEADROOM, 761 tx_headroom + HCI_CMDS_HEADROOM,
655 tx_tailroom); 762 tx_tailroom);
656 if (!hdev->ndev) { 763 if (!hdev->ndev) {
764 nfc_llc_free(hdev->llc);
657 kfree(hdev); 765 kfree(hdev);
658 return NULL; 766 return NULL;
659 } 767 }
@@ -673,6 +781,7 @@ EXPORT_SYMBOL(nfc_hci_allocate_device);
673void nfc_hci_free_device(struct nfc_hci_dev *hdev) 781void nfc_hci_free_device(struct nfc_hci_dev *hdev)
674{ 782{
675 nfc_free_device(hdev->ndev); 783 nfc_free_device(hdev->ndev);
784 nfc_llc_free(hdev->llc);
676 kfree(hdev); 785 kfree(hdev);
677} 786}
678EXPORT_SYMBOL(nfc_hci_free_device); 787EXPORT_SYMBOL(nfc_hci_free_device);
@@ -733,92 +842,15 @@ void *nfc_hci_get_clientdata(struct nfc_hci_dev *hdev)
733} 842}
734EXPORT_SYMBOL(nfc_hci_get_clientdata); 843EXPORT_SYMBOL(nfc_hci_get_clientdata);
735 844
736static void nfc_hci_failure(struct nfc_hci_dev *hdev, int err)
737{
738 mutex_lock(&hdev->msg_tx_mutex);
739
740 if (hdev->cmd_pending_msg == NULL) {
741 nfc_driver_failure(hdev->ndev, err);
742 goto exit;
743 }
744
745 __nfc_hci_cmd_completion(hdev, err, NULL);
746
747exit:
748 mutex_unlock(&hdev->msg_tx_mutex);
749}
750
751void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err) 845void nfc_hci_driver_failure(struct nfc_hci_dev *hdev, int err)
752{ 846{
753 nfc_hci_failure(hdev, err); 847 nfc_hci_failure(hdev, err);
754} 848}
755EXPORT_SYMBOL(nfc_hci_driver_failure); 849EXPORT_SYMBOL(nfc_hci_driver_failure);
756 850
757void nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb) 851void inline nfc_hci_recv_frame(struct nfc_hci_dev *hdev, struct sk_buff *skb)
758{ 852{
759 struct hcp_packet *packet; 853 nfc_llc_rcv_from_drv(hdev->llc, skb);
760 u8 type;
761 u8 instruction;
762 struct sk_buff *hcp_skb;
763 u8 pipe;
764 struct sk_buff *frag_skb;
765 int msg_len;
766
767 packet = (struct hcp_packet *)skb->data;
768 if ((packet->header & ~NFC_HCI_FRAGMENT) == 0) {
769 skb_queue_tail(&hdev->rx_hcp_frags, skb);
770 return;
771 }
772
773 /* it's the last fragment. Does it need re-aggregation? */
774 if (skb_queue_len(&hdev->rx_hcp_frags)) {
775 pipe = packet->header & NFC_HCI_FRAGMENT;
776 skb_queue_tail(&hdev->rx_hcp_frags, skb);
777
778 msg_len = 0;
779 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
780 msg_len += (frag_skb->len -
781 NFC_HCI_HCP_PACKET_HEADER_LEN);
782 }
783
784 hcp_skb = nfc_alloc_recv_skb(NFC_HCI_HCP_PACKET_HEADER_LEN +
785 msg_len, GFP_KERNEL);
786 if (hcp_skb == NULL) {
787 nfc_hci_failure(hdev, -ENOMEM);
788 return;
789 }
790
791 *skb_put(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN) = pipe;
792
793 skb_queue_walk(&hdev->rx_hcp_frags, frag_skb) {
794 msg_len = frag_skb->len - NFC_HCI_HCP_PACKET_HEADER_LEN;
795 memcpy(skb_put(hcp_skb, msg_len),
796 frag_skb->data + NFC_HCI_HCP_PACKET_HEADER_LEN,
797 msg_len);
798 }
799
800 skb_queue_purge(&hdev->rx_hcp_frags);
801 } else {
802 packet->header &= NFC_HCI_FRAGMENT;
803 hcp_skb = skb;
804 }
805
806 /* if this is a response, dispatch immediately to
807 * unblock waiting cmd context. Otherwise, enqueue to dispatch
808 * in separate context where handler can also execute command.
809 */
810 packet = (struct hcp_packet *)hcp_skb->data;
811 type = HCP_MSG_GET_TYPE(packet->message.header);
812 if (type == NFC_HCI_HCP_RESPONSE) {
813 pipe = packet->header;
814 instruction = HCP_MSG_GET_CMD(packet->message.header);
815 skb_pull(hcp_skb, NFC_HCI_HCP_PACKET_HEADER_LEN +
816 NFC_HCI_HCP_MESSAGE_HEADER_LEN);
817 nfc_hci_hcp_message_rx(hdev, pipe, type, instruction, hcp_skb);
818 } else {
819 skb_queue_tail(&hdev->msg_rx_queue, hcp_skb);
820 queue_work(system_nrt_wq, &hdev->msg_rx_work);
821 }
822} 854}
823EXPORT_SYMBOL(nfc_hci_recv_frame); 855EXPORT_SYMBOL(nfc_hci_recv_frame);
824 856
@@ -832,7 +864,7 @@ static void __exit nfc_hci_exit(void)
832 nfc_llc_exit(); 864 nfc_llc_exit();
833} 865}
834 866
835module_init(nfc_hci_init); 867subsys_initcall(nfc_hci_init);
836module_exit(nfc_hci_exit); 868module_exit(nfc_hci_exit);
837 869
838MODULE_LICENSE("GPL"); 870MODULE_LICENSE("GPL");
diff --git a/net/nfc/hci/llc_shdlc.c b/net/nfc/hci/llc_shdlc.c
index bb191100ee96..fad6cd18d613 100644
--- a/net/nfc/hci/llc_shdlc.c
+++ b/net/nfc/hci/llc_shdlc.c
@@ -535,7 +535,7 @@ static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
535 535
536 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns, 536 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
537 shdlc->nr); 537 shdlc->nr);
538 /* SHDLC_DUMP_SKB("shdlc frame written", skb); */ 538 SHDLC_DUMP_SKB("shdlc frame written", skb);
539 539
540 r = shdlc->xmit_to_drv(shdlc->hdev, skb); 540 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
541 if (r < 0) { 541 if (r < 0) {
diff --git a/net/nfc/hci/shdlc.c b/net/nfc/hci/shdlc.c
deleted file mode 100644
index c63af7d3e859..000000000000
--- a/net/nfc/hci/shdlc.c
+++ /dev/null
@@ -1,918 +0,0 @@
1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
21
22#include <linux/sched.h>
23#include <linux/export.h>
24#include <linux/wait.h>
25#include <linux/slab.h>
26#include <linux/skbuff.h>
27
28#include <net/nfc/hci.h>
29#include <net/nfc/shdlc.h>
30
31#define SHDLC_LLC_HEAD_ROOM 2
32
33#define SHDLC_MAX_WINDOW 4
34#define SHDLC_SREJ_SUPPORT false
35
36#define SHDLC_CONTROL_HEAD_MASK 0xe0
37#define SHDLC_CONTROL_HEAD_I 0x80
38#define SHDLC_CONTROL_HEAD_I2 0xa0
39#define SHDLC_CONTROL_HEAD_S 0xc0
40#define SHDLC_CONTROL_HEAD_U 0xe0
41
42#define SHDLC_CONTROL_NS_MASK 0x38
43#define SHDLC_CONTROL_NR_MASK 0x07
44#define SHDLC_CONTROL_TYPE_MASK 0x18
45
46#define SHDLC_CONTROL_M_MASK 0x1f
47
48enum sframe_type {
49 S_FRAME_RR = 0x00,
50 S_FRAME_REJ = 0x01,
51 S_FRAME_RNR = 0x02,
52 S_FRAME_SREJ = 0x03
53};
54
55enum uframe_modifier {
56 U_FRAME_UA = 0x06,
57 U_FRAME_RSET = 0x19
58};
59
60#define SHDLC_CONNECT_VALUE_MS 5
61#define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
62#define SHDLC_T2_VALUE_MS 300
63
64#define SHDLC_DUMP_SKB(info, skb) \
65do { \
66 pr_debug("%s:\n", info); \
67 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
68 16, 1, skb->data, skb->len, 0); \
69} while (0)
70
71/* checks x < y <= z modulo 8 */
72static bool nfc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
73{
74 if (x < z)
75 return ((x < y) && (y <= z)) ? true : false;
76 else
77 return ((y > x) || (y <= z)) ? true : false;
78}
79
80/* checks x <= y < z modulo 8 */
81static bool nfc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
82{
83 if (x <= z)
84 return ((x <= y) && (y < z)) ? true : false;
85 else /* x > z -> z+8 > x */
86 return ((y >= x) || (y < z)) ? true : false;
87}
88
89static struct sk_buff *nfc_shdlc_alloc_skb(struct nfc_shdlc *shdlc,
90 int payload_len)
91{
92 struct sk_buff *skb;
93
94 skb = alloc_skb(shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM +
95 shdlc->client_tailroom + payload_len, GFP_KERNEL);
96 if (skb)
97 skb_reserve(skb, shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM);
98
99 return skb;
100}
101
102/* immediately sends an S frame. */
103static int nfc_shdlc_send_s_frame(struct nfc_shdlc *shdlc,
104 enum sframe_type sframe_type, int nr)
105{
106 int r;
107 struct sk_buff *skb;
108
109 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
110
111 skb = nfc_shdlc_alloc_skb(shdlc, 0);
112 if (skb == NULL)
113 return -ENOMEM;
114
115 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
116
117 r = shdlc->ops->xmit(shdlc, skb);
118
119 kfree_skb(skb);
120
121 return r;
122}
123
124/* immediately sends an U frame. skb may contain optional payload */
125static int nfc_shdlc_send_u_frame(struct nfc_shdlc *shdlc,
126 struct sk_buff *skb,
127 enum uframe_modifier uframe_modifier)
128{
129 int r;
130
131 pr_debug("uframe_modifier=%d\n", uframe_modifier);
132
133 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
134
135 r = shdlc->ops->xmit(shdlc, skb);
136
137 kfree_skb(skb);
138
139 return r;
140}
141
142/*
143 * Free ack_pending frames until y_nr - 1, and reset t2 according to
144 * the remaining oldest ack_pending frame sent time
145 */
146static void nfc_shdlc_reset_t2(struct nfc_shdlc *shdlc, int y_nr)
147{
148 struct sk_buff *skb;
149 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
150
151 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
152
153 while (dnr != y_nr) {
154 pr_debug("release ack pending frame %d\n", dnr);
155
156 skb = skb_dequeue(&shdlc->ack_pending_q);
157 kfree_skb(skb);
158
159 dnr = (dnr + 1) % 8;
160 }
161
162 if (skb_queue_empty(&shdlc->ack_pending_q)) {
163 if (shdlc->t2_active) {
164 del_timer_sync(&shdlc->t2_timer);
165 shdlc->t2_active = false;
166
167 pr_debug
168 ("All sent frames acked. Stopped T2(retransmit)\n");
169 }
170 } else {
171 skb = skb_peek(&shdlc->ack_pending_q);
172
173 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
174 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
175 shdlc->t2_active = true;
176
177 pr_debug
178 ("Start T2(retransmit) for remaining unacked sent frames\n");
179 }
180}
181
182/*
183 * Receive validated frames from lower layer. skb contains HCI payload only.
184 * Handle according to algorithm at spec:10.8.2
185 */
186static void nfc_shdlc_rcv_i_frame(struct nfc_shdlc *shdlc,
187 struct sk_buff *skb, int ns, int nr)
188{
189 int x_ns = ns;
190 int y_nr = nr;
191
192 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
193
194 if (shdlc->state != SHDLC_CONNECTED)
195 goto exit;
196
197 if (x_ns != shdlc->nr) {
198 nfc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
199 goto exit;
200 }
201
202 if (shdlc->t1_active == false) {
203 shdlc->t1_active = true;
204 mod_timer(&shdlc->t1_timer,
205 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
206 pr_debug("(re)Start T1(send ack)\n");
207 }
208
209 if (skb->len) {
210 nfc_hci_recv_frame(shdlc->hdev, skb);
211 skb = NULL;
212 }
213
214 shdlc->nr = (shdlc->nr + 1) % 8;
215
216 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
217 nfc_shdlc_reset_t2(shdlc, y_nr);
218
219 shdlc->dnr = y_nr;
220 }
221
222exit:
223 kfree_skb(skb);
224}
225
226static void nfc_shdlc_rcv_ack(struct nfc_shdlc *shdlc, int y_nr)
227{
228 pr_debug("remote acked up to frame %d excluded\n", y_nr);
229
230 if (nfc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
231 nfc_shdlc_reset_t2(shdlc, y_nr);
232 shdlc->dnr = y_nr;
233 }
234}
235
236static void nfc_shdlc_requeue_ack_pending(struct nfc_shdlc *shdlc)
237{
238 struct sk_buff *skb;
239
240 pr_debug("ns reset to %d\n", shdlc->dnr);
241
242 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
243 skb_pull(skb, 1); /* remove control field */
244 skb_queue_head(&shdlc->send_q, skb);
245 }
246 shdlc->ns = shdlc->dnr;
247}
248
249static void nfc_shdlc_rcv_rej(struct nfc_shdlc *shdlc, int y_nr)
250{
251 struct sk_buff *skb;
252
253 pr_debug("remote asks retransmition from frame %d\n", y_nr);
254
255 if (nfc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
256 if (shdlc->t2_active) {
257 del_timer_sync(&shdlc->t2_timer);
258 shdlc->t2_active = false;
259 pr_debug("Stopped T2(retransmit)\n");
260 }
261
262 if (shdlc->dnr != y_nr) {
263 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
264 skb = skb_dequeue(&shdlc->ack_pending_q);
265 kfree_skb(skb);
266 }
267 }
268
269 nfc_shdlc_requeue_ack_pending(shdlc);
270 }
271}
272
273/* See spec RR:10.8.3 REJ:10.8.4 */
274static void nfc_shdlc_rcv_s_frame(struct nfc_shdlc *shdlc,
275 enum sframe_type s_frame_type, int nr)
276{
277 struct sk_buff *skb;
278
279 if (shdlc->state != SHDLC_CONNECTED)
280 return;
281
282 switch (s_frame_type) {
283 case S_FRAME_RR:
284 nfc_shdlc_rcv_ack(shdlc, nr);
285 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
286 shdlc->rnr = false;
287 if (shdlc->send_q.qlen == 0) {
288 skb = nfc_shdlc_alloc_skb(shdlc, 0);
289 if (skb)
290 skb_queue_tail(&shdlc->send_q, skb);
291 }
292 }
293 break;
294 case S_FRAME_REJ:
295 nfc_shdlc_rcv_rej(shdlc, nr);
296 break;
297 case S_FRAME_RNR:
298 nfc_shdlc_rcv_ack(shdlc, nr);
299 shdlc->rnr = true;
300 break;
301 default:
302 break;
303 }
304}
305
306static void nfc_shdlc_connect_complete(struct nfc_shdlc *shdlc, int r)
307{
308 pr_debug("result=%d\n", r);
309
310 del_timer_sync(&shdlc->connect_timer);
311
312 if (r == 0) {
313 shdlc->ns = 0;
314 shdlc->nr = 0;
315 shdlc->dnr = 0;
316
317 shdlc->state = SHDLC_CONNECTED;
318 } else {
319 shdlc->state = SHDLC_DISCONNECTED;
320 }
321
322 shdlc->connect_result = r;
323
324 wake_up(shdlc->connect_wq);
325}
326
327static int nfc_shdlc_connect_initiate(struct nfc_shdlc *shdlc)
328{
329 struct sk_buff *skb;
330
331 pr_debug("\n");
332
333 skb = nfc_shdlc_alloc_skb(shdlc, 2);
334 if (skb == NULL)
335 return -ENOMEM;
336
337 *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
338 *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
339
340 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
341}
342
343static int nfc_shdlc_connect_send_ua(struct nfc_shdlc *shdlc)
344{
345 struct sk_buff *skb;
346
347 pr_debug("\n");
348
349 skb = nfc_shdlc_alloc_skb(shdlc, 0);
350 if (skb == NULL)
351 return -ENOMEM;
352
353 return nfc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
354}
355
356static void nfc_shdlc_rcv_u_frame(struct nfc_shdlc *shdlc,
357 struct sk_buff *skb,
358 enum uframe_modifier u_frame_modifier)
359{
360 u8 w = SHDLC_MAX_WINDOW;
361 bool srej_support = SHDLC_SREJ_SUPPORT;
362 int r;
363
364 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
365
366 switch (u_frame_modifier) {
367 case U_FRAME_RSET:
368 if (shdlc->state == SHDLC_NEGOCIATING) {
369 /* we sent RSET, but chip wants to negociate */
370 if (skb->len > 0)
371 w = skb->data[0];
372
373 if (skb->len > 1)
374 srej_support = skb->data[1] & 0x01 ? true :
375 false;
376
377 if ((w <= SHDLC_MAX_WINDOW) &&
378 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
379 shdlc->w = w;
380 shdlc->srej_support = srej_support;
381 r = nfc_shdlc_connect_send_ua(shdlc);
382 nfc_shdlc_connect_complete(shdlc, r);
383 }
384 } else if (shdlc->state == SHDLC_CONNECTED) {
385 /*
386 * Chip wants to reset link. This is unexpected and
387 * unsupported.
388 */
389 shdlc->hard_fault = -ECONNRESET;
390 }
391 break;
392 case U_FRAME_UA:
393 if ((shdlc->state == SHDLC_CONNECTING &&
394 shdlc->connect_tries > 0) ||
395 (shdlc->state == SHDLC_NEGOCIATING))
396 nfc_shdlc_connect_complete(shdlc, 0);
397 break;
398 default:
399 break;
400 }
401
402 kfree_skb(skb);
403}
404
405static void nfc_shdlc_handle_rcv_queue(struct nfc_shdlc *shdlc)
406{
407 struct sk_buff *skb;
408 u8 control;
409 int nr;
410 int ns;
411 enum sframe_type s_frame_type;
412 enum uframe_modifier u_frame_modifier;
413
414 if (shdlc->rcv_q.qlen)
415 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
416
417 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
418 control = skb->data[0];
419 skb_pull(skb, 1);
420 switch (control & SHDLC_CONTROL_HEAD_MASK) {
421 case SHDLC_CONTROL_HEAD_I:
422 case SHDLC_CONTROL_HEAD_I2:
423 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
424 nr = control & SHDLC_CONTROL_NR_MASK;
425 nfc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
426 break;
427 case SHDLC_CONTROL_HEAD_S:
428 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
429 nr = control & SHDLC_CONTROL_NR_MASK;
430 nfc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
431 kfree_skb(skb);
432 break;
433 case SHDLC_CONTROL_HEAD_U:
434 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
435 nfc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
436 break;
437 default:
438 pr_err("UNKNOWN Control=%d\n", control);
439 kfree_skb(skb);
440 break;
441 }
442 }
443}
444
445static int nfc_shdlc_w_used(int ns, int dnr)
446{
447 int unack_count;
448
449 if (dnr <= ns)
450 unack_count = ns - dnr;
451 else
452 unack_count = 8 - dnr + ns;
453
454 return unack_count;
455}
456
457/* Send frames according to algorithm at spec:10.8.1 */
458static void nfc_shdlc_handle_send_queue(struct nfc_shdlc *shdlc)
459{
460 struct sk_buff *skb;
461 int r;
462 unsigned long time_sent;
463
464 if (shdlc->send_q.qlen)
465 pr_debug
466 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
467 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
468 shdlc->rnr == false ? "false" : "true",
469 shdlc->w - nfc_shdlc_w_used(shdlc->ns, shdlc->dnr),
470 shdlc->ack_pending_q.qlen);
471
472 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
473 (shdlc->rnr == false)) {
474
475 if (shdlc->t1_active) {
476 del_timer_sync(&shdlc->t1_timer);
477 shdlc->t1_active = false;
478 pr_debug("Stopped T1(send ack)\n");
479 }
480
481 skb = skb_dequeue(&shdlc->send_q);
482
483 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
484 shdlc->nr;
485
486 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
487 shdlc->nr);
488 /* SHDLC_DUMP_SKB("shdlc frame written", skb); */
489
490 r = shdlc->ops->xmit(shdlc, skb);
491 if (r < 0) {
492 shdlc->hard_fault = r;
493 break;
494 }
495
496 shdlc->ns = (shdlc->ns + 1) % 8;
497
498 time_sent = jiffies;
499 *(unsigned long *)skb->cb = time_sent;
500
501 skb_queue_tail(&shdlc->ack_pending_q, skb);
502
503 if (shdlc->t2_active == false) {
504 shdlc->t2_active = true;
505 mod_timer(&shdlc->t2_timer, time_sent +
506 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
507 pr_debug("Started T2 (retransmit)\n");
508 }
509 }
510}
511
512static void nfc_shdlc_connect_timeout(unsigned long data)
513{
514 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
515
516 pr_debug("\n");
517
518 queue_work(system_nrt_wq, &shdlc->sm_work);
519}
520
521static void nfc_shdlc_t1_timeout(unsigned long data)
522{
523 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
524
525 pr_debug("SoftIRQ: need to send ack\n");
526
527 queue_work(system_nrt_wq, &shdlc->sm_work);
528}
529
530static void nfc_shdlc_t2_timeout(unsigned long data)
531{
532 struct nfc_shdlc *shdlc = (struct nfc_shdlc *)data;
533
534 pr_debug("SoftIRQ: need to retransmit\n");
535
536 queue_work(system_nrt_wq, &shdlc->sm_work);
537}
538
539static void nfc_shdlc_sm_work(struct work_struct *work)
540{
541 struct nfc_shdlc *shdlc = container_of(work, struct nfc_shdlc, sm_work);
542 int r;
543
544 pr_debug("\n");
545
546 mutex_lock(&shdlc->state_mutex);
547
548 switch (shdlc->state) {
549 case SHDLC_DISCONNECTED:
550 skb_queue_purge(&shdlc->rcv_q);
551 skb_queue_purge(&shdlc->send_q);
552 skb_queue_purge(&shdlc->ack_pending_q);
553 break;
554 case SHDLC_CONNECTING:
555 if (shdlc->hard_fault) {
556 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
557 break;
558 }
559
560 if (shdlc->connect_tries++ < 5)
561 r = nfc_shdlc_connect_initiate(shdlc);
562 else
563 r = -ETIME;
564 if (r < 0)
565 nfc_shdlc_connect_complete(shdlc, r);
566 else {
567 mod_timer(&shdlc->connect_timer, jiffies +
568 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
569
570 shdlc->state = SHDLC_NEGOCIATING;
571 }
572 break;
573 case SHDLC_NEGOCIATING:
574 if (timer_pending(&shdlc->connect_timer) == 0) {
575 shdlc->state = SHDLC_CONNECTING;
576 queue_work(system_nrt_wq, &shdlc->sm_work);
577 }
578
579 nfc_shdlc_handle_rcv_queue(shdlc);
580
581 if (shdlc->hard_fault) {
582 nfc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
583 break;
584 }
585 break;
586 case SHDLC_CONNECTED:
587 nfc_shdlc_handle_rcv_queue(shdlc);
588 nfc_shdlc_handle_send_queue(shdlc);
589
590 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
591 pr_debug
592 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
593
594 shdlc->t1_active = false;
595 r = nfc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
596 shdlc->nr);
597 if (r < 0)
598 shdlc->hard_fault = r;
599 }
600
601 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
602 pr_debug
603 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
604
605 shdlc->t2_active = false;
606
607 nfc_shdlc_requeue_ack_pending(shdlc);
608 nfc_shdlc_handle_send_queue(shdlc);
609 }
610
611 if (shdlc->hard_fault) {
612 nfc_hci_driver_failure(shdlc->hdev, shdlc->hard_fault);
613 }
614 break;
615 default:
616 break;
617 }
618 mutex_unlock(&shdlc->state_mutex);
619}
620
621/*
622 * Called from syscall context to establish shdlc link. Sleeps until
623 * link is ready or failure.
624 */
625static int nfc_shdlc_connect(struct nfc_shdlc *shdlc)
626{
627 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
628
629 pr_debug("\n");
630
631 mutex_lock(&shdlc->state_mutex);
632
633 shdlc->state = SHDLC_CONNECTING;
634 shdlc->connect_wq = &connect_wq;
635 shdlc->connect_tries = 0;
636 shdlc->connect_result = 1;
637
638 mutex_unlock(&shdlc->state_mutex);
639
640 queue_work(system_nrt_wq, &shdlc->sm_work);
641
642 wait_event(connect_wq, shdlc->connect_result != 1);
643
644 return shdlc->connect_result;
645}
646
647static void nfc_shdlc_disconnect(struct nfc_shdlc *shdlc)
648{
649 pr_debug("\n");
650
651 mutex_lock(&shdlc->state_mutex);
652
653 shdlc->state = SHDLC_DISCONNECTED;
654
655 mutex_unlock(&shdlc->state_mutex);
656
657 queue_work(system_nrt_wq, &shdlc->sm_work);
658}
659
660/*
661 * Receive an incoming shdlc frame. Frame has already been crc-validated.
662 * skb contains only LLC header and payload.
663 * If skb == NULL, it is a notification that the link below is dead.
664 */
665void nfc_shdlc_recv_frame(struct nfc_shdlc *shdlc, struct sk_buff *skb)
666{
667 if (skb == NULL) {
668 pr_err("NULL Frame -> link is dead\n");
669 shdlc->hard_fault = -EREMOTEIO;
670 } else {
671 SHDLC_DUMP_SKB("incoming frame", skb);
672 skb_queue_tail(&shdlc->rcv_q, skb);
673 }
674
675 queue_work(system_nrt_wq, &shdlc->sm_work);
676}
677EXPORT_SYMBOL(nfc_shdlc_recv_frame);
678
679static int nfc_shdlc_open(struct nfc_hci_dev *hdev)
680{
681 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
682 int r;
683
684 pr_debug("\n");
685
686 if (shdlc->ops->open) {
687 r = shdlc->ops->open(shdlc);
688 if (r < 0)
689 return r;
690 }
691
692 r = nfc_shdlc_connect(shdlc);
693 if (r < 0 && shdlc->ops->close)
694 shdlc->ops->close(shdlc);
695
696 return r;
697}
698
699static void nfc_shdlc_close(struct nfc_hci_dev *hdev)
700{
701 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
702
703 pr_debug("\n");
704
705 nfc_shdlc_disconnect(shdlc);
706
707 if (shdlc->ops->close)
708 shdlc->ops->close(shdlc);
709}
710
711static int nfc_shdlc_hci_ready(struct nfc_hci_dev *hdev)
712{
713 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
714 int r = 0;
715
716 pr_debug("\n");
717
718 if (shdlc->ops->hci_ready)
719 r = shdlc->ops->hci_ready(shdlc);
720
721 return r;
722}
723
724static int nfc_shdlc_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
725{
726 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
727
728 SHDLC_DUMP_SKB("queuing HCP packet to shdlc", skb);
729
730 skb_queue_tail(&shdlc->send_q, skb);
731
732 queue_work(system_nrt_wq, &shdlc->sm_work);
733
734 return 0;
735}
736
737static int nfc_shdlc_start_poll(struct nfc_hci_dev *hdev,
738 u32 im_protocols, u32 tm_protocols)
739{
740 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
741
742 pr_debug("\n");
743
744 if (shdlc->ops->start_poll)
745 return shdlc->ops->start_poll(shdlc,
746 im_protocols, tm_protocols);
747
748 return 0;
749}
750
751static int nfc_shdlc_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
752 struct nfc_target *target)
753{
754 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
755
756 if (shdlc->ops->target_from_gate)
757 return shdlc->ops->target_from_gate(shdlc, gate, target);
758
759 return -EPERM;
760}
761
762static int nfc_shdlc_complete_target_discovered(struct nfc_hci_dev *hdev,
763 u8 gate,
764 struct nfc_target *target)
765{
766 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
767
768 pr_debug("\n");
769
770 if (shdlc->ops->complete_target_discovered)
771 return shdlc->ops->complete_target_discovered(shdlc, gate,
772 target);
773
774 return 0;
775}
776
777static int nfc_shdlc_data_exchange(struct nfc_hci_dev *hdev,
778 struct nfc_target *target,
779 struct sk_buff *skb,
780 data_exchange_cb_t cb, void *cb_context)
781{
782 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
783
784 if (shdlc->ops->data_exchange)
785 return shdlc->ops->data_exchange(shdlc, target, skb, cb,
786 cb_context);
787
788 return -EPERM;
789}
790
791static int nfc_shdlc_check_presence(struct nfc_hci_dev *hdev,
792 struct nfc_target *target)
793{
794 struct nfc_shdlc *shdlc = nfc_hci_get_clientdata(hdev);
795
796 if (shdlc->ops->check_presence)
797 return shdlc->ops->check_presence(shdlc, target);
798
799 return 0;
800}
801
802static struct nfc_hci_ops shdlc_ops = {
803 .open = nfc_shdlc_open,
804 .close = nfc_shdlc_close,
805 .hci_ready = nfc_shdlc_hci_ready,
806 .xmit = nfc_shdlc_xmit,
807 .start_poll = nfc_shdlc_start_poll,
808 .target_from_gate = nfc_shdlc_target_from_gate,
809 .complete_target_discovered = nfc_shdlc_complete_target_discovered,
810 .data_exchange = nfc_shdlc_data_exchange,
811 .check_presence = nfc_shdlc_check_presence,
812};
813
814struct nfc_shdlc *nfc_shdlc_allocate(struct nfc_shdlc_ops *ops,
815 struct nfc_hci_init_data *init_data,
816 u32 protocols,
817 int tx_headroom, int tx_tailroom,
818 int max_link_payload, const char *devname)
819{
820 struct nfc_shdlc *shdlc;
821 int r;
822
823 if (ops->xmit == NULL)
824 return NULL;
825
826 shdlc = kzalloc(sizeof(struct nfc_shdlc), GFP_KERNEL);
827 if (shdlc == NULL)
828 return NULL;
829
830 mutex_init(&shdlc->state_mutex);
831 shdlc->ops = ops;
832 shdlc->state = SHDLC_DISCONNECTED;
833
834 init_timer(&shdlc->connect_timer);
835 shdlc->connect_timer.data = (unsigned long)shdlc;
836 shdlc->connect_timer.function = nfc_shdlc_connect_timeout;
837
838 init_timer(&shdlc->t1_timer);
839 shdlc->t1_timer.data = (unsigned long)shdlc;
840 shdlc->t1_timer.function = nfc_shdlc_t1_timeout;
841
842 init_timer(&shdlc->t2_timer);
843 shdlc->t2_timer.data = (unsigned long)shdlc;
844 shdlc->t2_timer.function = nfc_shdlc_t2_timeout;
845
846 shdlc->w = SHDLC_MAX_WINDOW;
847 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
848
849 skb_queue_head_init(&shdlc->rcv_q);
850 skb_queue_head_init(&shdlc->send_q);
851 skb_queue_head_init(&shdlc->ack_pending_q);
852
853 INIT_WORK(&shdlc->sm_work, nfc_shdlc_sm_work);
854
855 shdlc->client_headroom = tx_headroom;
856 shdlc->client_tailroom = tx_tailroom;
857
858 shdlc->hdev = nfc_hci_allocate_device(&shdlc_ops, init_data, protocols,
859 tx_headroom + SHDLC_LLC_HEAD_ROOM,
860 tx_tailroom,
861 max_link_payload);
862 if (shdlc->hdev == NULL)
863 goto err_allocdev;
864
865 nfc_hci_set_clientdata(shdlc->hdev, shdlc);
866
867 r = nfc_hci_register_device(shdlc->hdev);
868 if (r < 0)
869 goto err_regdev;
870
871 return shdlc;
872
873err_regdev:
874 nfc_hci_free_device(shdlc->hdev);
875
876err_allocdev:
877 kfree(shdlc);
878
879 return NULL;
880}
881EXPORT_SYMBOL(nfc_shdlc_allocate);
882
883void nfc_shdlc_free(struct nfc_shdlc *shdlc)
884{
885 pr_debug("\n");
886
887 nfc_hci_unregister_device(shdlc->hdev);
888 nfc_hci_free_device(shdlc->hdev);
889
890 cancel_work_sync(&shdlc->sm_work);
891
892 skb_queue_purge(&shdlc->rcv_q);
893 skb_queue_purge(&shdlc->send_q);
894 skb_queue_purge(&shdlc->ack_pending_q);
895
896 kfree(shdlc);
897}
898EXPORT_SYMBOL(nfc_shdlc_free);
899
900void nfc_shdlc_set_clientdata(struct nfc_shdlc *shdlc, void *clientdata)
901{
902 pr_debug("\n");
903
904 shdlc->clientdata = clientdata;
905}
906EXPORT_SYMBOL(nfc_shdlc_set_clientdata);
907
908void *nfc_shdlc_get_clientdata(struct nfc_shdlc *shdlc)
909{
910 return shdlc->clientdata;
911}
912EXPORT_SYMBOL(nfc_shdlc_get_clientdata);
913
914struct nfc_hci_dev *nfc_shdlc_get_hci_dev(struct nfc_shdlc *shdlc)
915{
916 return shdlc->hdev;
917}
918EXPORT_SYMBOL(nfc_shdlc_get_hci_dev);