1 // SPDX-License-Identifier: GPL-2.0-only
3 * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
5 * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
8 #include <linux/sched.h>
9 #include <net/nfc/nci_core.h>
13 #define NDLC_TIMER_T1 100
14 #define NDLC_TIMER_T1_WAIT 400
15 #define NDLC_TIMER_T2 1200
17 #define PCB_TYPE_DATAFRAME 0x80
18 #define PCB_TYPE_SUPERVISOR 0xc0
19 #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
21 #define PCB_SYNC_ACK 0x20
22 #define PCB_SYNC_NACK 0x10
23 #define PCB_SYNC_WAIT 0x30
24 #define PCB_SYNC_NOINFO 0x00
25 #define PCB_SYNC_MASK PCB_SYNC_WAIT
27 #define PCB_DATAFRAME_RETRANSMIT_YES 0x00
28 #define PCB_DATAFRAME_RETRANSMIT_NO 0x04
29 #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
31 #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
32 #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
33 #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
35 #define PCB_FRAME_CRC_INFO_PRESENT 0x08
36 #define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
37 #define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
39 #define NDLC_DUMP_SKB(info, skb) \
41 pr_debug("%s:\n", info); \
42 print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
43 16, 1, skb->data, skb->len, 0); \
46 int ndlc_open(struct llt_ndlc
*ndlc
)
48 /* toggle reset pin */
49 ndlc
->ops
->enable(ndlc
->phy_id
);
53 EXPORT_SYMBOL(ndlc_open
);
55 void ndlc_close(struct llt_ndlc
*ndlc
)
57 struct nci_mode_set_cmd cmd
;
59 cmd
.cmd_type
= ST_NCI_SET_NFC_MODE
;
62 /* toggle reset pin */
63 ndlc
->ops
->enable(ndlc
->phy_id
);
65 nci_prop_cmd(ndlc
->ndev
, ST_NCI_CORE_PROP
,
66 sizeof(struct nci_mode_set_cmd
), (__u8
*)&cmd
);
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 *(u8
*)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
));
115 /* start timer t2 for chip availability */
116 ndlc
->t2_active
= true;
117 mod_timer(&ndlc
->t2_timer
, time_sent
+
118 msecs_to_jiffies(NDLC_TIMER_T2
));
122 static void llt_ndlc_requeue_data_pending(struct llt_ndlc
*ndlc
)
127 while ((skb
= skb_dequeue_tail(&ndlc
->ack_pending_q
))) {
129 switch (pcb
& PCB_TYPE_MASK
) {
130 case PCB_TYPE_SUPERVISOR
:
131 skb
->data
[0] = (pcb
& ~PCB_SUPERVISOR_RETRANSMIT_MASK
) |
132 PCB_SUPERVISOR_RETRANSMIT_YES
;
134 case PCB_TYPE_DATAFRAME
:
135 skb
->data
[0] = (pcb
& ~PCB_DATAFRAME_RETRANSMIT_MASK
) |
136 PCB_DATAFRAME_RETRANSMIT_YES
;
139 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb
);
143 skb_queue_head(&ndlc
->send_q
, skb
);
147 static void llt_ndlc_rcv_queue(struct llt_ndlc
*ndlc
)
151 unsigned long time_sent
;
153 if (ndlc
->rcv_q
.qlen
)
154 pr_debug("rcvQlen=%d\n", ndlc
->rcv_q
.qlen
);
156 while ((skb
= skb_dequeue(&ndlc
->rcv_q
)) != NULL
) {
159 if ((pcb
& PCB_TYPE_MASK
) == PCB_TYPE_SUPERVISOR
) {
160 switch (pcb
& PCB_SYNC_MASK
) {
162 skb
= skb_dequeue(&ndlc
->ack_pending_q
);
164 del_timer_sync(&ndlc
->t1_timer
);
165 del_timer_sync(&ndlc
->t2_timer
);
166 ndlc
->t2_active
= false;
167 ndlc
->t1_active
= false;
170 llt_ndlc_requeue_data_pending(ndlc
);
171 llt_ndlc_send_queue(ndlc
);
172 /* start timer t1 for ndlc aknowledge */
174 ndlc
->t1_active
= true;
175 mod_timer(&ndlc
->t1_timer
, time_sent
+
176 msecs_to_jiffies(NDLC_TIMER_T1
));
180 ndlc
->t1_active
= true;
181 mod_timer(&ndlc
->t1_timer
, time_sent
+
182 msecs_to_jiffies(NDLC_TIMER_T1_WAIT
));
188 } else if ((pcb
& PCB_TYPE_MASK
) == PCB_TYPE_DATAFRAME
) {
189 nci_recv_frame(ndlc
->ndev
, skb
);
196 static void llt_ndlc_sm_work(struct work_struct
*work
)
198 struct llt_ndlc
*ndlc
= container_of(work
, struct llt_ndlc
, sm_work
);
200 llt_ndlc_send_queue(ndlc
);
201 llt_ndlc_rcv_queue(ndlc
);
203 if (ndlc
->t1_active
&& timer_pending(&ndlc
->t1_timer
) == 0) {
205 ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
206 ndlc
->t1_active
= false;
208 llt_ndlc_requeue_data_pending(ndlc
);
209 llt_ndlc_send_queue(ndlc
);
212 if (ndlc
->t2_active
&& timer_pending(&ndlc
->t2_timer
) == 0) {
213 pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
214 ndlc
->t2_active
= false;
215 ndlc
->t1_active
= false;
216 del_timer_sync(&ndlc
->t1_timer
);
217 del_timer_sync(&ndlc
->t2_timer
);
219 ndlc
->hard_fault
= -EREMOTEIO
;
223 void ndlc_recv(struct llt_ndlc
*ndlc
, struct sk_buff
*skb
)
226 pr_err("NULL Frame -> link is dead\n");
227 ndlc
->hard_fault
= -EREMOTEIO
;
230 NDLC_DUMP_SKB("incoming frame", skb
);
231 skb_queue_tail(&ndlc
->rcv_q
, skb
);
234 schedule_work(&ndlc
->sm_work
);
236 EXPORT_SYMBOL(ndlc_recv
);
238 static void ndlc_t1_timeout(struct timer_list
*t
)
240 struct llt_ndlc
*ndlc
= from_timer(ndlc
, t
, t1_timer
);
244 schedule_work(&ndlc
->sm_work
);
247 static void ndlc_t2_timeout(struct timer_list
*t
)
249 struct llt_ndlc
*ndlc
= from_timer(ndlc
, t
, t2_timer
);
253 schedule_work(&ndlc
->sm_work
);
256 int ndlc_probe(void *phy_id
, struct nfc_phy_ops
*phy_ops
, struct device
*dev
,
257 int phy_headroom
, int phy_tailroom
, struct llt_ndlc
**ndlc_id
,
258 struct st_nci_se_status
*se_status
)
260 struct llt_ndlc
*ndlc
;
262 ndlc
= devm_kzalloc(dev
, sizeof(struct llt_ndlc
), GFP_KERNEL
);
267 ndlc
->phy_id
= phy_id
;
273 /* initialize timers */
274 timer_setup(&ndlc
->t1_timer
, ndlc_t1_timeout
, 0);
275 timer_setup(&ndlc
->t2_timer
, ndlc_t2_timeout
, 0);
277 skb_queue_head_init(&ndlc
->rcv_q
);
278 skb_queue_head_init(&ndlc
->send_q
);
279 skb_queue_head_init(&ndlc
->ack_pending_q
);
281 INIT_WORK(&ndlc
->sm_work
, llt_ndlc_sm_work
);
283 return st_nci_probe(ndlc
, phy_headroom
, phy_tailroom
, se_status
);
285 EXPORT_SYMBOL(ndlc_probe
);
287 void ndlc_remove(struct llt_ndlc
*ndlc
)
289 st_nci_remove(ndlc
->ndev
);
292 del_timer_sync(&ndlc
->t1_timer
);
293 del_timer_sync(&ndlc
->t2_timer
);
294 ndlc
->t2_active
= false;
295 ndlc
->t1_active
= false;
297 skb_queue_purge(&ndlc
->rcv_q
);
298 skb_queue_purge(&ndlc
->send_q
);
300 EXPORT_SYMBOL(ndlc_remove
);