2 * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
4 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/sched.h>
20 #include <net/nfc/nci_core.h>
25 #define NDLC_TIMER_T1 100
26 #define NDLC_TIMER_T1_WAIT 400
27 #define NDLC_TIMER_T2 1200
29 #define PCB_TYPE_DATAFRAME 0x80
30 #define PCB_TYPE_SUPERVISOR 0xc0
31 #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
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
39 #define PCB_DATAFRAME_RETRANSMIT_YES 0x00
40 #define PCB_DATAFRAME_RETRANSMIT_NO 0x04
41 #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
43 #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
44 #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
45 #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
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
51 #define NDLC_DUMP_SKB(info, skb) \
53 pr_debug("%s:\n", info); \
54 print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
55 16, 1, skb->data, skb->len, 0); \
58 int ndlc_open(struct llt_ndlc *ndlc)
60 /* toggle reset pin */
61 ndlc->ops->enable(ndlc->phy_id);
64 EXPORT_SYMBOL(ndlc_open);
66 void ndlc_close(struct llt_ndlc *ndlc)
68 /* toggle reset pin */
69 ndlc->ops->disable(ndlc->phy_id);
71 EXPORT_SYMBOL(ndlc_close);
73 int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
76 u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
77 PCB_FRAME_CRC_INFO_NOTPRESENT;
79 *skb_push(skb, 1) = pcb;
80 skb_queue_tail(&ndlc->send_q, skb);
82 schedule_work(&ndlc->sm_work);
86 EXPORT_SYMBOL(ndlc_send);
88 static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
92 unsigned long time_sent;
94 if (ndlc->send_q.qlen)
95 pr_debug("sendQlen=%d unackQlen=%d\n",
96 ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
98 while (ndlc->send_q.qlen) {
99 skb = skb_dequeue(&ndlc->send_q);
100 NDLC_DUMP_SKB("ndlc frame written", skb);
101 r = ndlc->ops->write(ndlc->phy_id, skb);
103 ndlc->hard_fault = r;
107 *(unsigned long *)skb->cb = time_sent;
109 skb_queue_tail(&ndlc->ack_pending_q, skb);
111 /* start timer t1 for ndlc aknowledge */
112 ndlc->t1_active = true;
113 mod_timer(&ndlc->t1_timer, time_sent +
114 msecs_to_jiffies(NDLC_TIMER_T1));
118 static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
123 while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
125 switch (pcb & PCB_TYPE_MASK) {
126 case PCB_TYPE_SUPERVISOR:
127 skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
128 PCB_SUPERVISOR_RETRANSMIT_YES;
130 case PCB_TYPE_DATAFRAME:
131 skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
132 PCB_DATAFRAME_RETRANSMIT_YES;
135 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
139 skb_queue_head(&ndlc->send_q, skb);
143 static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
147 unsigned long time_sent;
149 if (ndlc->rcv_q.qlen)
150 pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
152 while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
155 if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
156 switch (pcb & PCB_SYNC_MASK) {
158 del_timer_sync(&ndlc->t1_timer);
159 del_timer_sync(&ndlc->t2_timer);
160 ndlc->t2_active = false;
161 ndlc->t1_active = false;
164 llt_ndlc_requeue_data_pending(ndlc);
165 llt_ndlc_send_queue(ndlc);
166 /* start timer t1 for ndlc aknowledge */
168 ndlc->t1_active = true;
169 mod_timer(&ndlc->t1_timer, time_sent +
170 msecs_to_jiffies(NDLC_TIMER_T1));
174 ndlc->t1_active = true;
175 mod_timer(&ndlc->t1_timer, time_sent +
176 msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
179 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
184 nci_recv_frame(ndlc->ndev, skb);
189 static void llt_ndlc_sm_work(struct work_struct *work)
191 struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
193 llt_ndlc_send_queue(ndlc);
194 llt_ndlc_rcv_queue(ndlc);
196 if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
198 ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
199 ndlc->t1_active = false;
201 llt_ndlc_requeue_data_pending(ndlc);
202 llt_ndlc_send_queue(ndlc);
205 if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
206 pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
207 ndlc->t2_active = false;
208 ndlc->t1_active = false;
209 del_timer_sync(&ndlc->t1_timer);
212 ndlc->hard_fault = -EREMOTEIO;
216 void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
219 pr_err("NULL Frame -> link is dead\n");
220 ndlc->hard_fault = -EREMOTEIO;
223 NDLC_DUMP_SKB("incoming frame", skb);
224 skb_queue_tail(&ndlc->rcv_q, skb);
227 schedule_work(&ndlc->sm_work);
229 EXPORT_SYMBOL(ndlc_recv);
231 static void ndlc_t1_timeout(unsigned long data)
233 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
237 schedule_work(&ndlc->sm_work);
240 static void ndlc_t2_timeout(unsigned long data)
242 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
246 schedule_work(&ndlc->sm_work);
249 int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
250 int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id)
252 struct llt_ndlc *ndlc;
254 ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
256 nfc_err(dev, "Cannot allocate memory for ndlc.\n");
260 ndlc->phy_id = phy_id;
266 init_timer(&ndlc->t1_timer);
267 ndlc->t1_timer.data = (unsigned long)ndlc;
268 ndlc->t1_timer.function = ndlc_t1_timeout;
270 init_timer(&ndlc->t2_timer);
271 ndlc->t2_timer.data = (unsigned long)ndlc;
272 ndlc->t2_timer.function = ndlc_t2_timeout;
274 skb_queue_head_init(&ndlc->rcv_q);
275 skb_queue_head_init(&ndlc->send_q);
276 skb_queue_head_init(&ndlc->ack_pending_q);
278 INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
280 return st21nfcb_nci_probe(ndlc, phy_headroom, phy_tailroom);
282 EXPORT_SYMBOL(ndlc_probe);
284 void ndlc_remove(struct llt_ndlc *ndlc)
287 del_timer_sync(&ndlc->t1_timer);
288 del_timer_sync(&ndlc->t2_timer);
289 ndlc->t2_active = false;
290 ndlc->t1_active = false;
292 skb_queue_purge(&ndlc->rcv_q);
293 skb_queue_purge(&ndlc->send_q);
295 st21nfcb_nci_remove(ndlc->ndev);
298 EXPORT_SYMBOL(ndlc_remove);