blob: 56c6a4cb4c9619ff7a35ccfbd568b998474dc636 [file] [log] [blame]
Christophe Ricard35630df2014-05-25 22:35:38 +02001/*
2 * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
3 *
Christophe Ricarded06aeef2015-06-09 22:26:05 +02004 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
Christophe Ricard35630df2014-05-25 22:35:38 +02005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/sched.h>
20#include <net/nfc/nci_core.h>
21
22#include "ndlc.h"
Christophe Ricarded06aeef2015-06-09 22:26:05 +020023#include "st-nci.h"
Christophe Ricard35630df2014-05-25 22:35:38 +020024
25#define NDLC_TIMER_T1 100
26#define NDLC_TIMER_T1_WAIT 400
27#define NDLC_TIMER_T2 1200
28
29#define PCB_TYPE_DATAFRAME 0x80
30#define PCB_TYPE_SUPERVISOR 0xc0
31#define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
32
33#define PCB_SYNC_ACK 0x20
34#define PCB_SYNC_NACK 0x10
35#define PCB_SYNC_WAIT 0x30
36#define PCB_SYNC_NOINFO 0x00
37#define PCB_SYNC_MASK PCB_SYNC_WAIT
38
39#define PCB_DATAFRAME_RETRANSMIT_YES 0x00
40#define PCB_DATAFRAME_RETRANSMIT_NO 0x04
41#define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
42
43#define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
44#define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
45#define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
46
47#define PCB_FRAME_CRC_INFO_PRESENT 0x08
48#define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
49#define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
50
51#define NDLC_DUMP_SKB(info, skb) \
52do { \
53 pr_debug("%s:\n", info); \
54 print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
55 16, 1, skb->data, skb->len, 0); \
56} while (0)
57
58int ndlc_open(struct llt_ndlc *ndlc)
59{
60 /* toggle reset pin */
61 ndlc->ops->enable(ndlc->phy_id);
Christophe Ricard183fe2d2015-06-06 13:16:50 +020062 ndlc->powered = 1;
Christophe Ricard35630df2014-05-25 22:35:38 +020063 return 0;
64}
65EXPORT_SYMBOL(ndlc_open);
66
67void ndlc_close(struct llt_ndlc *ndlc)
68{
Christophe Ricard41fc2f52015-06-06 13:16:52 +020069 struct nci_mode_set_cmd cmd;
70
Christophe Ricarded06aeef2015-06-09 22:26:05 +020071 cmd.cmd_type = ST_NCI_SET_NFC_MODE;
Christophe Ricard41fc2f52015-06-06 13:16:52 +020072 cmd.mode = 0;
73
Christophe Ricard35630df2014-05-25 22:35:38 +020074 /* toggle reset pin */
Christophe Ricard41fc2f52015-06-06 13:16:52 +020075 ndlc->ops->enable(ndlc->phy_id);
76
Christophe Ricarded06aeef2015-06-09 22:26:05 +020077 nci_prop_cmd(ndlc->ndev, ST_NCI_CORE_PROP,
Christophe Ricard41fc2f52015-06-06 13:16:52 +020078 sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
79
Christophe Ricard183fe2d2015-06-06 13:16:50 +020080 ndlc->powered = 0;
Christophe Ricard41fc2f52015-06-06 13:16:52 +020081 ndlc->ops->disable(ndlc->phy_id);
Christophe Ricard35630df2014-05-25 22:35:38 +020082}
83EXPORT_SYMBOL(ndlc_close);
84
85int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
86{
87 /* add ndlc header */
88 u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
89 PCB_FRAME_CRC_INFO_NOTPRESENT;
90
91 *skb_push(skb, 1) = pcb;
92 skb_queue_tail(&ndlc->send_q, skb);
93
94 schedule_work(&ndlc->sm_work);
95
96 return 0;
97}
98EXPORT_SYMBOL(ndlc_send);
99
100static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
101{
102 struct sk_buff *skb;
103 int r;
104 unsigned long time_sent;
105
106 if (ndlc->send_q.qlen)
107 pr_debug("sendQlen=%d unackQlen=%d\n",
108 ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
109
110 while (ndlc->send_q.qlen) {
111 skb = skb_dequeue(&ndlc->send_q);
112 NDLC_DUMP_SKB("ndlc frame written", skb);
113 r = ndlc->ops->write(ndlc->phy_id, skb);
114 if (r < 0) {
115 ndlc->hard_fault = r;
116 break;
117 }
118 time_sent = jiffies;
119 *(unsigned long *)skb->cb = time_sent;
120
121 skb_queue_tail(&ndlc->ack_pending_q, skb);
122
123 /* start timer t1 for ndlc aknowledge */
124 ndlc->t1_active = true;
125 mod_timer(&ndlc->t1_timer, time_sent +
126 msecs_to_jiffies(NDLC_TIMER_T1));
Christophe Ricardfa0daa02014-09-13 10:28:48 +0200127 /* start timer t2 for chip availability */
128 ndlc->t2_active = true;
129 mod_timer(&ndlc->t2_timer, time_sent +
130 msecs_to_jiffies(NDLC_TIMER_T2));
Christophe Ricard35630df2014-05-25 22:35:38 +0200131 }
132}
133
134static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
135{
136 struct sk_buff *skb;
137 u8 pcb;
138
139 while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
140 pcb = skb->data[0];
141 switch (pcb & PCB_TYPE_MASK) {
142 case PCB_TYPE_SUPERVISOR:
143 skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
144 PCB_SUPERVISOR_RETRANSMIT_YES;
145 break;
146 case PCB_TYPE_DATAFRAME:
147 skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
148 PCB_DATAFRAME_RETRANSMIT_YES;
149 break;
150 default:
151 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
152 kfree_skb(skb);
Christophe Ricard20842462015-01-25 23:33:23 +0100153 continue;
Christophe Ricard35630df2014-05-25 22:35:38 +0200154 }
155 skb_queue_head(&ndlc->send_q, skb);
156 }
157}
158
159static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
160{
161 struct sk_buff *skb;
162 u8 pcb;
163 unsigned long time_sent;
164
165 if (ndlc->rcv_q.qlen)
166 pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
167
168 while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
169 pcb = skb->data[0];
170 skb_pull(skb, 1);
171 if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
172 switch (pcb & PCB_SYNC_MASK) {
173 case PCB_SYNC_ACK:
174 del_timer_sync(&ndlc->t1_timer);
175 del_timer_sync(&ndlc->t2_timer);
176 ndlc->t2_active = false;
177 ndlc->t1_active = false;
178 break;
179 case PCB_SYNC_NACK:
180 llt_ndlc_requeue_data_pending(ndlc);
181 llt_ndlc_send_queue(ndlc);
182 /* start timer t1 for ndlc aknowledge */
183 time_sent = jiffies;
184 ndlc->t1_active = true;
185 mod_timer(&ndlc->t1_timer, time_sent +
186 msecs_to_jiffies(NDLC_TIMER_T1));
187 break;
188 case PCB_SYNC_WAIT:
189 time_sent = jiffies;
190 ndlc->t1_active = true;
191 mod_timer(&ndlc->t1_timer, time_sent +
192 msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
193 break;
194 default:
195 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
196 kfree_skb(skb);
197 break;
198 }
199 } else {
200 nci_recv_frame(ndlc->ndev, skb);
201 }
202 }
203}
204
205static void llt_ndlc_sm_work(struct work_struct *work)
206{
207 struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
208
209 llt_ndlc_send_queue(ndlc);
210 llt_ndlc_rcv_queue(ndlc);
211
212 if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
213 pr_debug
214 ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
215 ndlc->t1_active = false;
216
217 llt_ndlc_requeue_data_pending(ndlc);
218 llt_ndlc_send_queue(ndlc);
219 }
220
221 if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
222 pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
223 ndlc->t2_active = false;
224 ndlc->t1_active = false;
225 del_timer_sync(&ndlc->t1_timer);
Christophe Ricardfa0daa02014-09-13 10:28:48 +0200226 del_timer_sync(&ndlc->t2_timer);
Christophe Ricard35630df2014-05-25 22:35:38 +0200227 ndlc_close(ndlc);
228 ndlc->hard_fault = -EREMOTEIO;
229 }
230}
231
232void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
233{
234 if (skb == NULL) {
235 pr_err("NULL Frame -> link is dead\n");
236 ndlc->hard_fault = -EREMOTEIO;
237 ndlc_close(ndlc);
238 } else {
239 NDLC_DUMP_SKB("incoming frame", skb);
240 skb_queue_tail(&ndlc->rcv_q, skb);
241 }
242
243 schedule_work(&ndlc->sm_work);
244}
245EXPORT_SYMBOL(ndlc_recv);
246
247static void ndlc_t1_timeout(unsigned long data)
248{
249 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
250
251 pr_debug("\n");
252
253 schedule_work(&ndlc->sm_work);
254}
255
256static void ndlc_t2_timeout(unsigned long data)
257{
258 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
259
260 pr_debug("\n");
261
262 schedule_work(&ndlc->sm_work);
263}
264
265int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
266 int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id)
267{
268 struct llt_ndlc *ndlc;
269
270 ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
Joe Perches3590ebc2015-04-07 00:17:00 -0700271 if (!ndlc)
Christophe Ricard35630df2014-05-25 22:35:38 +0200272 return -ENOMEM;
Joe Perches3590ebc2015-04-07 00:17:00 -0700273
Christophe Ricard35630df2014-05-25 22:35:38 +0200274 ndlc->ops = phy_ops;
275 ndlc->phy_id = phy_id;
276 ndlc->dev = dev;
Christophe Ricard183fe2d2015-06-06 13:16:50 +0200277 ndlc->powered = 0;
Christophe Ricard35630df2014-05-25 22:35:38 +0200278
279 *ndlc_id = ndlc;
280
Christophe Ricardace91832014-11-13 00:30:40 +0100281 /* initialize timers */
Christophe Ricard35630df2014-05-25 22:35:38 +0200282 init_timer(&ndlc->t1_timer);
283 ndlc->t1_timer.data = (unsigned long)ndlc;
284 ndlc->t1_timer.function = ndlc_t1_timeout;
285
286 init_timer(&ndlc->t2_timer);
287 ndlc->t2_timer.data = (unsigned long)ndlc;
288 ndlc->t2_timer.function = ndlc_t2_timeout;
289
290 skb_queue_head_init(&ndlc->rcv_q);
291 skb_queue_head_init(&ndlc->send_q);
292 skb_queue_head_init(&ndlc->ack_pending_q);
293
294 INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
295
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200296 return st_nci_probe(ndlc, phy_headroom, phy_tailroom);
Christophe Ricard35630df2014-05-25 22:35:38 +0200297}
298EXPORT_SYMBOL(ndlc_probe);
299
300void ndlc_remove(struct llt_ndlc *ndlc)
301{
Christophe Ricarded06aeef2015-06-09 22:26:05 +0200302 st_nci_remove(ndlc->ndev);
Christophe Ricarde8b72c22015-06-06 13:16:49 +0200303
Christophe Ricard35630df2014-05-25 22:35:38 +0200304 /* cancel timers */
305 del_timer_sync(&ndlc->t1_timer);
306 del_timer_sync(&ndlc->t2_timer);
307 ndlc->t2_active = false;
308 ndlc->t1_active = false;
309
310 skb_queue_purge(&ndlc->rcv_q);
311 skb_queue_purge(&ndlc->send_q);
Christophe Ricard35630df2014-05-25 22:35:38 +0200312}
313EXPORT_SYMBOL(ndlc_remove);