]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/nfc/st21nfcb/ndlc.c
Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus
[karo-tx-linux.git] / drivers / nfc / st21nfcb / ndlc.c
1 /*
2  * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
3  *
4  * Copyright (C) 2014  STMicroelectronics SAS. All rights reserved.
5  *
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"
23 #include "st21nfcb.h"
24
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)                                 \
52 do {                                                             \
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
58 int ndlc_open(struct llt_ndlc *ndlc)
59 {
60         /* toggle reset pin */
61         ndlc->ops->enable(ndlc->phy_id);
62         return 0;
63 }
64 EXPORT_SYMBOL(ndlc_open);
65
66 void ndlc_close(struct llt_ndlc *ndlc)
67 {
68         /* toggle reset pin */
69         ndlc->ops->disable(ndlc->phy_id);
70 }
71 EXPORT_SYMBOL(ndlc_close);
72
73 int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
74 {
75         /* add ndlc header */
76         u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
77                 PCB_FRAME_CRC_INFO_NOTPRESENT;
78
79         *skb_push(skb, 1) = pcb;
80         skb_queue_tail(&ndlc->send_q, skb);
81
82         schedule_work(&ndlc->sm_work);
83
84         return 0;
85 }
86 EXPORT_SYMBOL(ndlc_send);
87
88 static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
89 {
90         struct sk_buff *skb;
91         int r;
92         unsigned long time_sent;
93
94         if (ndlc->send_q.qlen)
95                 pr_debug("sendQlen=%d unackQlen=%d\n",
96                          ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
97
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);
102                 if (r < 0) {
103                         ndlc->hard_fault = r;
104                         break;
105                 }
106                 time_sent = jiffies;
107                 *(unsigned long *)skb->cb = time_sent;
108
109                 skb_queue_tail(&ndlc->ack_pending_q, skb);
110
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         }
116 }
117
118 static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
119 {
120         struct sk_buff *skb;
121         u8 pcb;
122
123         while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
124                 pcb = skb->data[0];
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;
129                         break;
130                 case PCB_TYPE_DATAFRAME:
131                         skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
132                                 PCB_DATAFRAME_RETRANSMIT_YES;
133                         break;
134                 default:
135                         pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
136                         kfree_skb(skb);
137                         break;
138                 }
139                 skb_queue_head(&ndlc->send_q, skb);
140         }
141 }
142
143 static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
144 {
145         struct sk_buff *skb;
146         u8 pcb;
147         unsigned long time_sent;
148
149         if (ndlc->rcv_q.qlen)
150                 pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
151
152         while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
153                 pcb = skb->data[0];
154                 skb_pull(skb, 1);
155                 if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
156                         switch (pcb & PCB_SYNC_MASK) {
157                         case PCB_SYNC_ACK:
158                                 del_timer_sync(&ndlc->t1_timer);
159                                 del_timer_sync(&ndlc->t2_timer);
160                                 ndlc->t2_active = false;
161                                 ndlc->t1_active = false;
162                                 break;
163                         case PCB_SYNC_NACK:
164                                 llt_ndlc_requeue_data_pending(ndlc);
165                                 llt_ndlc_send_queue(ndlc);
166                                 /* start timer t1 for ndlc aknowledge */
167                                 time_sent = jiffies;
168                                 ndlc->t1_active = true;
169                                 mod_timer(&ndlc->t1_timer, time_sent +
170                                         msecs_to_jiffies(NDLC_TIMER_T1));
171                                 break;
172                         case PCB_SYNC_WAIT:
173                                 time_sent = jiffies;
174                                 ndlc->t1_active = true;
175                                 mod_timer(&ndlc->t1_timer, time_sent +
176                                           msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
177                                 break;
178                         default:
179                                 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
180                                 kfree_skb(skb);
181                                 break;
182                         }
183                 } else {
184                         nci_recv_frame(ndlc->ndev, skb);
185                 }
186         }
187 }
188
189 static void llt_ndlc_sm_work(struct work_struct *work)
190 {
191         struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
192
193         llt_ndlc_send_queue(ndlc);
194         llt_ndlc_rcv_queue(ndlc);
195
196         if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
197                 pr_debug
198                     ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
199                 ndlc->t1_active = false;
200
201                 llt_ndlc_requeue_data_pending(ndlc);
202                 llt_ndlc_send_queue(ndlc);
203         }
204
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);
210
211                 ndlc_close(ndlc);
212                 ndlc->hard_fault = -EREMOTEIO;
213         }
214 }
215
216 void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
217 {
218         if (skb == NULL) {
219                 pr_err("NULL Frame -> link is dead\n");
220                 ndlc->hard_fault = -EREMOTEIO;
221                 ndlc_close(ndlc);
222         } else {
223                 NDLC_DUMP_SKB("incoming frame", skb);
224                 skb_queue_tail(&ndlc->rcv_q, skb);
225         }
226
227         schedule_work(&ndlc->sm_work);
228 }
229 EXPORT_SYMBOL(ndlc_recv);
230
231 static void ndlc_t1_timeout(unsigned long data)
232 {
233         struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
234
235         pr_debug("\n");
236
237         schedule_work(&ndlc->sm_work);
238 }
239
240 static void ndlc_t2_timeout(unsigned long data)
241 {
242         struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
243
244         pr_debug("\n");
245
246         schedule_work(&ndlc->sm_work);
247 }
248
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)
251 {
252         struct llt_ndlc *ndlc;
253
254         ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
255         if (!ndlc) {
256                 nfc_err(dev, "Cannot allocate memory for ndlc.\n");
257                 return -ENOMEM;
258         }
259         ndlc->ops = phy_ops;
260         ndlc->phy_id = phy_id;
261         ndlc->dev = dev;
262
263         *ndlc_id = ndlc;
264
265         /* start timers */
266         init_timer(&ndlc->t1_timer);
267         ndlc->t1_timer.data = (unsigned long)ndlc;
268         ndlc->t1_timer.function = ndlc_t1_timeout;
269
270         init_timer(&ndlc->t2_timer);
271         ndlc->t2_timer.data = (unsigned long)ndlc;
272         ndlc->t2_timer.function = ndlc_t2_timeout;
273
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);
277
278         INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
279
280         return st21nfcb_nci_probe(ndlc, phy_headroom, phy_tailroom);
281 }
282 EXPORT_SYMBOL(ndlc_probe);
283
284 void ndlc_remove(struct llt_ndlc *ndlc)
285 {
286         /* cancel timers */
287         del_timer_sync(&ndlc->t1_timer);
288         del_timer_sync(&ndlc->t2_timer);
289         ndlc->t2_active = false;
290         ndlc->t1_active = false;
291
292         skb_queue_purge(&ndlc->rcv_q);
293         skb_queue_purge(&ndlc->send_q);
294
295         st21nfcb_nci_remove(ndlc->ndev);
296         kfree(ndlc);
297 }
298 EXPORT_SYMBOL(ndlc_remove);