146fe7771SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ed06aeefSChristophe Ricard /*
3ed06aeefSChristophe Ricard * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
4ed06aeefSChristophe Ricard *
5ed06aeefSChristophe Ricard * Copyright (C) 2014-2015 STMicroelectronics SAS. All rights reserved.
6ed06aeefSChristophe Ricard */
7ed06aeefSChristophe Ricard
8ed06aeefSChristophe Ricard #include <linux/sched.h>
9ed06aeefSChristophe Ricard #include <net/nfc/nci_core.h>
10ed06aeefSChristophe Ricard
11ed06aeefSChristophe Ricard #include "st-nci.h"
12ed06aeefSChristophe Ricard
13ed06aeefSChristophe Ricard #define NDLC_TIMER_T1 100
14ed06aeefSChristophe Ricard #define NDLC_TIMER_T1_WAIT 400
15ed06aeefSChristophe Ricard #define NDLC_TIMER_T2 1200
16ed06aeefSChristophe Ricard
17ed06aeefSChristophe Ricard #define PCB_TYPE_DATAFRAME 0x80
18ed06aeefSChristophe Ricard #define PCB_TYPE_SUPERVISOR 0xc0
19ed06aeefSChristophe Ricard #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
20ed06aeefSChristophe Ricard
21ed06aeefSChristophe Ricard #define PCB_SYNC_ACK 0x20
22ed06aeefSChristophe Ricard #define PCB_SYNC_NACK 0x10
23ed06aeefSChristophe Ricard #define PCB_SYNC_WAIT 0x30
24ed06aeefSChristophe Ricard #define PCB_SYNC_NOINFO 0x00
25ed06aeefSChristophe Ricard #define PCB_SYNC_MASK PCB_SYNC_WAIT
26ed06aeefSChristophe Ricard
27ed06aeefSChristophe Ricard #define PCB_DATAFRAME_RETRANSMIT_YES 0x00
28ed06aeefSChristophe Ricard #define PCB_DATAFRAME_RETRANSMIT_NO 0x04
29ed06aeefSChristophe Ricard #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
30ed06aeefSChristophe Ricard
31ed06aeefSChristophe Ricard #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
32ed06aeefSChristophe Ricard #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
33ed06aeefSChristophe Ricard #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
34ed06aeefSChristophe Ricard
35ed06aeefSChristophe Ricard #define PCB_FRAME_CRC_INFO_PRESENT 0x08
36ed06aeefSChristophe Ricard #define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
37ed06aeefSChristophe Ricard #define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
38ed06aeefSChristophe Ricard
39ed06aeefSChristophe Ricard #define NDLC_DUMP_SKB(info, skb) \
40ed06aeefSChristophe Ricard do { \
41ed06aeefSChristophe Ricard pr_debug("%s:\n", info); \
42ed06aeefSChristophe Ricard print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
43ed06aeefSChristophe Ricard 16, 1, skb->data, skb->len, 0); \
44ed06aeefSChristophe Ricard } while (0)
45ed06aeefSChristophe Ricard
ndlc_open(struct llt_ndlc * ndlc)46ed06aeefSChristophe Ricard int ndlc_open(struct llt_ndlc *ndlc)
47ed06aeefSChristophe Ricard {
48ed06aeefSChristophe Ricard /* toggle reset pin */
49ed06aeefSChristophe Ricard ndlc->ops->enable(ndlc->phy_id);
50ed06aeefSChristophe Ricard ndlc->powered = 1;
51ed06aeefSChristophe Ricard return 0;
52ed06aeefSChristophe Ricard }
53ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_open);
54ed06aeefSChristophe Ricard
ndlc_close(struct llt_ndlc * ndlc)55ed06aeefSChristophe Ricard void ndlc_close(struct llt_ndlc *ndlc)
56ed06aeefSChristophe Ricard {
57ed06aeefSChristophe Ricard struct nci_mode_set_cmd cmd;
58ed06aeefSChristophe Ricard
59ed06aeefSChristophe Ricard cmd.cmd_type = ST_NCI_SET_NFC_MODE;
60ed06aeefSChristophe Ricard cmd.mode = 0;
61ed06aeefSChristophe Ricard
62ed06aeefSChristophe Ricard /* toggle reset pin */
63ed06aeefSChristophe Ricard ndlc->ops->enable(ndlc->phy_id);
64ed06aeefSChristophe Ricard
65ed06aeefSChristophe Ricard nci_prop_cmd(ndlc->ndev, ST_NCI_CORE_PROP,
66ed06aeefSChristophe Ricard sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
67ed06aeefSChristophe Ricard
68ed06aeefSChristophe Ricard ndlc->powered = 0;
69ed06aeefSChristophe Ricard ndlc->ops->disable(ndlc->phy_id);
70ed06aeefSChristophe Ricard }
71ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_close);
72ed06aeefSChristophe Ricard
ndlc_send(struct llt_ndlc * ndlc,struct sk_buff * skb)73ed06aeefSChristophe Ricard int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
74ed06aeefSChristophe Ricard {
75ed06aeefSChristophe Ricard /* add ndlc header */
76ed06aeefSChristophe Ricard u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
77ed06aeefSChristophe Ricard PCB_FRAME_CRC_INFO_NOTPRESENT;
78ed06aeefSChristophe Ricard
79d58ff351SJohannes Berg *(u8 *)skb_push(skb, 1) = pcb;
80ed06aeefSChristophe Ricard skb_queue_tail(&ndlc->send_q, skb);
81ed06aeefSChristophe Ricard
82ed06aeefSChristophe Ricard schedule_work(&ndlc->sm_work);
83ed06aeefSChristophe Ricard
84ed06aeefSChristophe Ricard return 0;
85ed06aeefSChristophe Ricard }
86ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_send);
87ed06aeefSChristophe Ricard
llt_ndlc_send_queue(struct llt_ndlc * ndlc)88ed06aeefSChristophe Ricard static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
89ed06aeefSChristophe Ricard {
90ed06aeefSChristophe Ricard struct sk_buff *skb;
91ed06aeefSChristophe Ricard int r;
92ed06aeefSChristophe Ricard unsigned long time_sent;
93ed06aeefSChristophe Ricard
94ed06aeefSChristophe Ricard if (ndlc->send_q.qlen)
95ed06aeefSChristophe Ricard pr_debug("sendQlen=%d unackQlen=%d\n",
96ed06aeefSChristophe Ricard ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
97ed06aeefSChristophe Ricard
98ed06aeefSChristophe Ricard while (ndlc->send_q.qlen) {
99ed06aeefSChristophe Ricard skb = skb_dequeue(&ndlc->send_q);
100ed06aeefSChristophe Ricard NDLC_DUMP_SKB("ndlc frame written", skb);
101ed06aeefSChristophe Ricard r = ndlc->ops->write(ndlc->phy_id, skb);
102ed06aeefSChristophe Ricard if (r < 0) {
103ed06aeefSChristophe Ricard ndlc->hard_fault = r;
104ed06aeefSChristophe Ricard break;
105ed06aeefSChristophe Ricard }
106ed06aeefSChristophe Ricard time_sent = jiffies;
107ed06aeefSChristophe Ricard *(unsigned long *)skb->cb = time_sent;
108ed06aeefSChristophe Ricard
109ed06aeefSChristophe Ricard skb_queue_tail(&ndlc->ack_pending_q, skb);
110ed06aeefSChristophe Ricard
111ed06aeefSChristophe Ricard /* start timer t1 for ndlc aknowledge */
112ed06aeefSChristophe Ricard ndlc->t1_active = true;
113ed06aeefSChristophe Ricard mod_timer(&ndlc->t1_timer, time_sent +
114ed06aeefSChristophe Ricard msecs_to_jiffies(NDLC_TIMER_T1));
115ed06aeefSChristophe Ricard /* start timer t2 for chip availability */
116ed06aeefSChristophe Ricard ndlc->t2_active = true;
117ed06aeefSChristophe Ricard mod_timer(&ndlc->t2_timer, time_sent +
118ed06aeefSChristophe Ricard msecs_to_jiffies(NDLC_TIMER_T2));
119ed06aeefSChristophe Ricard }
120ed06aeefSChristophe Ricard }
121ed06aeefSChristophe Ricard
llt_ndlc_requeue_data_pending(struct llt_ndlc * ndlc)122ed06aeefSChristophe Ricard static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
123ed06aeefSChristophe Ricard {
124ed06aeefSChristophe Ricard struct sk_buff *skb;
125ed06aeefSChristophe Ricard u8 pcb;
126ed06aeefSChristophe Ricard
127ed06aeefSChristophe Ricard while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
128ed06aeefSChristophe Ricard pcb = skb->data[0];
129ed06aeefSChristophe Ricard switch (pcb & PCB_TYPE_MASK) {
130ed06aeefSChristophe Ricard case PCB_TYPE_SUPERVISOR:
131ed06aeefSChristophe Ricard skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
132ed06aeefSChristophe Ricard PCB_SUPERVISOR_RETRANSMIT_YES;
133ed06aeefSChristophe Ricard break;
134ed06aeefSChristophe Ricard case PCB_TYPE_DATAFRAME:
135ed06aeefSChristophe Ricard skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
136ed06aeefSChristophe Ricard PCB_DATAFRAME_RETRANSMIT_YES;
137ed06aeefSChristophe Ricard break;
138ed06aeefSChristophe Ricard default:
139ed06aeefSChristophe Ricard pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
140ed06aeefSChristophe Ricard kfree_skb(skb);
141ed06aeefSChristophe Ricard continue;
142ed06aeefSChristophe Ricard }
143ed06aeefSChristophe Ricard skb_queue_head(&ndlc->send_q, skb);
144ed06aeefSChristophe Ricard }
145ed06aeefSChristophe Ricard }
146ed06aeefSChristophe Ricard
llt_ndlc_rcv_queue(struct llt_ndlc * ndlc)147ed06aeefSChristophe Ricard static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
148ed06aeefSChristophe Ricard {
149ed06aeefSChristophe Ricard struct sk_buff *skb;
150ed06aeefSChristophe Ricard u8 pcb;
151ed06aeefSChristophe Ricard unsigned long time_sent;
152ed06aeefSChristophe Ricard
153ed06aeefSChristophe Ricard if (ndlc->rcv_q.qlen)
154ed06aeefSChristophe Ricard pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
155ed06aeefSChristophe Ricard
156ed06aeefSChristophe Ricard while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
157ed06aeefSChristophe Ricard pcb = skb->data[0];
158ed06aeefSChristophe Ricard skb_pull(skb, 1);
159ed06aeefSChristophe Ricard if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
160ed06aeefSChristophe Ricard switch (pcb & PCB_SYNC_MASK) {
161ed06aeefSChristophe Ricard case PCB_SYNC_ACK:
1621d816b6eSChristophe Ricard skb = skb_dequeue(&ndlc->ack_pending_q);
1631d816b6eSChristophe Ricard kfree_skb(skb);
164ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t1_timer);
165ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t2_timer);
166ed06aeefSChristophe Ricard ndlc->t2_active = false;
167ed06aeefSChristophe Ricard ndlc->t1_active = false;
168ed06aeefSChristophe Ricard break;
169ed06aeefSChristophe Ricard case PCB_SYNC_NACK:
170ed06aeefSChristophe Ricard llt_ndlc_requeue_data_pending(ndlc);
171ed06aeefSChristophe Ricard llt_ndlc_send_queue(ndlc);
172ed06aeefSChristophe Ricard /* start timer t1 for ndlc aknowledge */
173ed06aeefSChristophe Ricard time_sent = jiffies;
174ed06aeefSChristophe Ricard ndlc->t1_active = true;
175ed06aeefSChristophe Ricard mod_timer(&ndlc->t1_timer, time_sent +
176ed06aeefSChristophe Ricard msecs_to_jiffies(NDLC_TIMER_T1));
177ed06aeefSChristophe Ricard break;
178ed06aeefSChristophe Ricard case PCB_SYNC_WAIT:
179ed06aeefSChristophe Ricard time_sent = jiffies;
180ed06aeefSChristophe Ricard ndlc->t1_active = true;
181ed06aeefSChristophe Ricard mod_timer(&ndlc->t1_timer, time_sent +
182ed06aeefSChristophe Ricard msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
183ed06aeefSChristophe Ricard break;
184ed06aeefSChristophe Ricard default:
185ed06aeefSChristophe Ricard kfree_skb(skb);
186ed06aeefSChristophe Ricard break;
187ed06aeefSChristophe Ricard }
1888b706884SChristophe Ricard } else if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_DATAFRAME) {
189ed06aeefSChristophe Ricard nci_recv_frame(ndlc->ndev, skb);
1908b706884SChristophe Ricard } else {
1918b706884SChristophe Ricard kfree_skb(skb);
192ed06aeefSChristophe Ricard }
193ed06aeefSChristophe Ricard }
194ed06aeefSChristophe Ricard }
195ed06aeefSChristophe Ricard
llt_ndlc_sm_work(struct work_struct * work)196ed06aeefSChristophe Ricard static void llt_ndlc_sm_work(struct work_struct *work)
197ed06aeefSChristophe Ricard {
198ed06aeefSChristophe Ricard struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
199ed06aeefSChristophe Ricard
200ed06aeefSChristophe Ricard llt_ndlc_send_queue(ndlc);
201ed06aeefSChristophe Ricard llt_ndlc_rcv_queue(ndlc);
202ed06aeefSChristophe Ricard
203ed06aeefSChristophe Ricard if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
204ed06aeefSChristophe Ricard pr_debug
205ed06aeefSChristophe Ricard ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
206ed06aeefSChristophe Ricard ndlc->t1_active = false;
207ed06aeefSChristophe Ricard
208ed06aeefSChristophe Ricard llt_ndlc_requeue_data_pending(ndlc);
209ed06aeefSChristophe Ricard llt_ndlc_send_queue(ndlc);
210ed06aeefSChristophe Ricard }
211ed06aeefSChristophe Ricard
212ed06aeefSChristophe Ricard if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
213ed06aeefSChristophe Ricard pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
214ed06aeefSChristophe Ricard ndlc->t2_active = false;
215ed06aeefSChristophe Ricard ndlc->t1_active = false;
216ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t1_timer);
217ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t2_timer);
218ed06aeefSChristophe Ricard ndlc_close(ndlc);
219ed06aeefSChristophe Ricard ndlc->hard_fault = -EREMOTEIO;
220ed06aeefSChristophe Ricard }
221ed06aeefSChristophe Ricard }
222ed06aeefSChristophe Ricard
ndlc_recv(struct llt_ndlc * ndlc,struct sk_buff * skb)223ed06aeefSChristophe Ricard void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
224ed06aeefSChristophe Ricard {
225ed06aeefSChristophe Ricard if (skb == NULL) {
226ed06aeefSChristophe Ricard pr_err("NULL Frame -> link is dead\n");
227ed06aeefSChristophe Ricard ndlc->hard_fault = -EREMOTEIO;
228ed06aeefSChristophe Ricard ndlc_close(ndlc);
229ed06aeefSChristophe Ricard } else {
230ed06aeefSChristophe Ricard NDLC_DUMP_SKB("incoming frame", skb);
231ed06aeefSChristophe Ricard skb_queue_tail(&ndlc->rcv_q, skb);
232ed06aeefSChristophe Ricard }
233ed06aeefSChristophe Ricard
234ed06aeefSChristophe Ricard schedule_work(&ndlc->sm_work);
235ed06aeefSChristophe Ricard }
236ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_recv);
237ed06aeefSChristophe Ricard
ndlc_t1_timeout(struct timer_list * t)238e99e88a9SKees Cook static void ndlc_t1_timeout(struct timer_list *t)
239ed06aeefSChristophe Ricard {
240e99e88a9SKees Cook struct llt_ndlc *ndlc = from_timer(ndlc, t, t1_timer);
241ed06aeefSChristophe Ricard
242ed06aeefSChristophe Ricard schedule_work(&ndlc->sm_work);
243ed06aeefSChristophe Ricard }
244ed06aeefSChristophe Ricard
ndlc_t2_timeout(struct timer_list * t)245e99e88a9SKees Cook static void ndlc_t2_timeout(struct timer_list *t)
246ed06aeefSChristophe Ricard {
247e99e88a9SKees Cook struct llt_ndlc *ndlc = from_timer(ndlc, t, t2_timer);
248ed06aeefSChristophe Ricard
249ed06aeefSChristophe Ricard schedule_work(&ndlc->sm_work);
250ed06aeefSChristophe Ricard }
251ed06aeefSChristophe Ricard
ndlc_probe(void * phy_id,const struct nfc_phy_ops * phy_ops,struct device * dev,int phy_headroom,int phy_tailroom,struct llt_ndlc ** ndlc_id,struct st_nci_se_status * se_status)2527a5e98daSKrzysztof Kozlowski int ndlc_probe(void *phy_id, const struct nfc_phy_ops *phy_ops,
2537a5e98daSKrzysztof Kozlowski struct device *dev, int phy_headroom, int phy_tailroom,
2547a5e98daSKrzysztof Kozlowski struct llt_ndlc **ndlc_id, struct st_nci_se_status *se_status)
255ed06aeefSChristophe Ricard {
256ed06aeefSChristophe Ricard struct llt_ndlc *ndlc;
257ed06aeefSChristophe Ricard
258ed06aeefSChristophe Ricard ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
259ed06aeefSChristophe Ricard if (!ndlc)
260ed06aeefSChristophe Ricard return -ENOMEM;
261ed06aeefSChristophe Ricard
262ed06aeefSChristophe Ricard ndlc->ops = phy_ops;
263ed06aeefSChristophe Ricard ndlc->phy_id = phy_id;
264ed06aeefSChristophe Ricard ndlc->dev = dev;
265ed06aeefSChristophe Ricard ndlc->powered = 0;
266ed06aeefSChristophe Ricard
267ed06aeefSChristophe Ricard *ndlc_id = ndlc;
268ed06aeefSChristophe Ricard
269ed06aeefSChristophe Ricard /* initialize timers */
270e99e88a9SKees Cook timer_setup(&ndlc->t1_timer, ndlc_t1_timeout, 0);
271e99e88a9SKees Cook timer_setup(&ndlc->t2_timer, ndlc_t2_timeout, 0);
272ed06aeefSChristophe Ricard
273ed06aeefSChristophe Ricard skb_queue_head_init(&ndlc->rcv_q);
274ed06aeefSChristophe Ricard skb_queue_head_init(&ndlc->send_q);
275ed06aeefSChristophe Ricard skb_queue_head_init(&ndlc->ack_pending_q);
276ed06aeefSChristophe Ricard
277ed06aeefSChristophe Ricard INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
278ed06aeefSChristophe Ricard
2793648dc6dSChristophe Ricard return st_nci_probe(ndlc, phy_headroom, phy_tailroom, se_status);
280ed06aeefSChristophe Ricard }
281ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_probe);
282ed06aeefSChristophe Ricard
ndlc_remove(struct llt_ndlc * ndlc)283ed06aeefSChristophe Ricard void ndlc_remove(struct llt_ndlc *ndlc)
284ed06aeefSChristophe Ricard {
285ed06aeefSChristophe Ricard /* cancel timers */
286ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t1_timer);
287ed06aeefSChristophe Ricard del_timer_sync(&ndlc->t2_timer);
288ed06aeefSChristophe Ricard ndlc->t2_active = false;
289ed06aeefSChristophe Ricard ndlc->t1_active = false;
290*5000fe6cSZheng Wang /* cancel work */
291*5000fe6cSZheng Wang cancel_work_sync(&ndlc->sm_work);
292*5000fe6cSZheng Wang
293*5000fe6cSZheng Wang st_nci_remove(ndlc->ndev);
294ed06aeefSChristophe Ricard
295ed06aeefSChristophe Ricard skb_queue_purge(&ndlc->rcv_q);
296ed06aeefSChristophe Ricard skb_queue_purge(&ndlc->send_q);
297ed06aeefSChristophe Ricard }
298ed06aeefSChristophe Ricard EXPORT_SYMBOL(ndlc_remove);
299