Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / nfc / st-nci / ndlc.c
blob50880d747b02113bb1cb264c2c8f82ca9d72d98c
1 /*
2 * Low Level Transport (NDLC) Driver for STMicroelectronics NFC Chip
4 * Copyright (C) 2014-2015 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>
22 #include "st-nci.h"
24 #define NDLC_TIMER_T1 100
25 #define NDLC_TIMER_T1_WAIT 400
26 #define NDLC_TIMER_T2 1200
28 #define PCB_TYPE_DATAFRAME 0x80
29 #define PCB_TYPE_SUPERVISOR 0xc0
30 #define PCB_TYPE_MASK PCB_TYPE_SUPERVISOR
32 #define PCB_SYNC_ACK 0x20
33 #define PCB_SYNC_NACK 0x10
34 #define PCB_SYNC_WAIT 0x30
35 #define PCB_SYNC_NOINFO 0x00
36 #define PCB_SYNC_MASK PCB_SYNC_WAIT
38 #define PCB_DATAFRAME_RETRANSMIT_YES 0x00
39 #define PCB_DATAFRAME_RETRANSMIT_NO 0x04
40 #define PCB_DATAFRAME_RETRANSMIT_MASK PCB_DATAFRAME_RETRANSMIT_NO
42 #define PCB_SUPERVISOR_RETRANSMIT_YES 0x00
43 #define PCB_SUPERVISOR_RETRANSMIT_NO 0x02
44 #define PCB_SUPERVISOR_RETRANSMIT_MASK PCB_SUPERVISOR_RETRANSMIT_NO
46 #define PCB_FRAME_CRC_INFO_PRESENT 0x08
47 #define PCB_FRAME_CRC_INFO_NOTPRESENT 0x00
48 #define PCB_FRAME_CRC_INFO_MASK PCB_FRAME_CRC_INFO_PRESENT
50 #define NDLC_DUMP_SKB(info, skb) \
51 do { \
52 pr_debug("%s:\n", info); \
53 print_hex_dump(KERN_DEBUG, "ndlc: ", DUMP_PREFIX_OFFSET, \
54 16, 1, skb->data, skb->len, 0); \
55 } while (0)
57 int ndlc_open(struct llt_ndlc *ndlc)
59 /* toggle reset pin */
60 ndlc->ops->enable(ndlc->phy_id);
61 ndlc->powered = 1;
62 return 0;
64 EXPORT_SYMBOL(ndlc_open);
66 void ndlc_close(struct llt_ndlc *ndlc)
68 struct nci_mode_set_cmd cmd;
70 cmd.cmd_type = ST_NCI_SET_NFC_MODE;
71 cmd.mode = 0;
73 /* toggle reset pin */
74 ndlc->ops->enable(ndlc->phy_id);
76 nci_prop_cmd(ndlc->ndev, ST_NCI_CORE_PROP,
77 sizeof(struct nci_mode_set_cmd), (__u8 *)&cmd);
79 ndlc->powered = 0;
80 ndlc->ops->disable(ndlc->phy_id);
82 EXPORT_SYMBOL(ndlc_close);
84 int ndlc_send(struct llt_ndlc *ndlc, struct sk_buff *skb)
86 /* add ndlc header */
87 u8 pcb = PCB_TYPE_DATAFRAME | PCB_DATAFRAME_RETRANSMIT_NO |
88 PCB_FRAME_CRC_INFO_NOTPRESENT;
90 *skb_push(skb, 1) = pcb;
91 skb_queue_tail(&ndlc->send_q, skb);
93 schedule_work(&ndlc->sm_work);
95 return 0;
97 EXPORT_SYMBOL(ndlc_send);
99 static void llt_ndlc_send_queue(struct llt_ndlc *ndlc)
101 struct sk_buff *skb;
102 int r;
103 unsigned long time_sent;
105 if (ndlc->send_q.qlen)
106 pr_debug("sendQlen=%d unackQlen=%d\n",
107 ndlc->send_q.qlen, ndlc->ack_pending_q.qlen);
109 while (ndlc->send_q.qlen) {
110 skb = skb_dequeue(&ndlc->send_q);
111 NDLC_DUMP_SKB("ndlc frame written", skb);
112 r = ndlc->ops->write(ndlc->phy_id, skb);
113 if (r < 0) {
114 ndlc->hard_fault = r;
115 break;
117 time_sent = jiffies;
118 *(unsigned long *)skb->cb = time_sent;
120 skb_queue_tail(&ndlc->ack_pending_q, skb);
122 /* start timer t1 for ndlc aknowledge */
123 ndlc->t1_active = true;
124 mod_timer(&ndlc->t1_timer, time_sent +
125 msecs_to_jiffies(NDLC_TIMER_T1));
126 /* start timer t2 for chip availability */
127 ndlc->t2_active = true;
128 mod_timer(&ndlc->t2_timer, time_sent +
129 msecs_to_jiffies(NDLC_TIMER_T2));
133 static void llt_ndlc_requeue_data_pending(struct llt_ndlc *ndlc)
135 struct sk_buff *skb;
136 u8 pcb;
138 while ((skb = skb_dequeue_tail(&ndlc->ack_pending_q))) {
139 pcb = skb->data[0];
140 switch (pcb & PCB_TYPE_MASK) {
141 case PCB_TYPE_SUPERVISOR:
142 skb->data[0] = (pcb & ~PCB_SUPERVISOR_RETRANSMIT_MASK) |
143 PCB_SUPERVISOR_RETRANSMIT_YES;
144 break;
145 case PCB_TYPE_DATAFRAME:
146 skb->data[0] = (pcb & ~PCB_DATAFRAME_RETRANSMIT_MASK) |
147 PCB_DATAFRAME_RETRANSMIT_YES;
148 break;
149 default:
150 pr_err("UNKNOWN Packet Control Byte=%d\n", pcb);
151 kfree_skb(skb);
152 continue;
154 skb_queue_head(&ndlc->send_q, skb);
158 static void llt_ndlc_rcv_queue(struct llt_ndlc *ndlc)
160 struct sk_buff *skb;
161 u8 pcb;
162 unsigned long time_sent;
164 if (ndlc->rcv_q.qlen)
165 pr_debug("rcvQlen=%d\n", ndlc->rcv_q.qlen);
167 while ((skb = skb_dequeue(&ndlc->rcv_q)) != NULL) {
168 pcb = skb->data[0];
169 skb_pull(skb, 1);
170 if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_SUPERVISOR) {
171 switch (pcb & PCB_SYNC_MASK) {
172 case PCB_SYNC_ACK:
173 skb = skb_dequeue(&ndlc->ack_pending_q);
174 kfree_skb(skb);
175 del_timer_sync(&ndlc->t1_timer);
176 del_timer_sync(&ndlc->t2_timer);
177 ndlc->t2_active = false;
178 ndlc->t1_active = false;
179 break;
180 case PCB_SYNC_NACK:
181 llt_ndlc_requeue_data_pending(ndlc);
182 llt_ndlc_send_queue(ndlc);
183 /* start timer t1 for ndlc aknowledge */
184 time_sent = jiffies;
185 ndlc->t1_active = true;
186 mod_timer(&ndlc->t1_timer, time_sent +
187 msecs_to_jiffies(NDLC_TIMER_T1));
188 break;
189 case PCB_SYNC_WAIT:
190 time_sent = jiffies;
191 ndlc->t1_active = true;
192 mod_timer(&ndlc->t1_timer, time_sent +
193 msecs_to_jiffies(NDLC_TIMER_T1_WAIT));
194 break;
195 default:
196 kfree_skb(skb);
197 break;
199 } else if ((pcb & PCB_TYPE_MASK) == PCB_TYPE_DATAFRAME) {
200 nci_recv_frame(ndlc->ndev, skb);
201 } else {
202 kfree_skb(skb);
207 static void llt_ndlc_sm_work(struct work_struct *work)
209 struct llt_ndlc *ndlc = container_of(work, struct llt_ndlc, sm_work);
211 llt_ndlc_send_queue(ndlc);
212 llt_ndlc_rcv_queue(ndlc);
214 if (ndlc->t1_active && timer_pending(&ndlc->t1_timer) == 0) {
215 pr_debug
216 ("Handle T1(recv SUPERVISOR) elapsed (T1 now inactive)\n");
217 ndlc->t1_active = false;
219 llt_ndlc_requeue_data_pending(ndlc);
220 llt_ndlc_send_queue(ndlc);
223 if (ndlc->t2_active && timer_pending(&ndlc->t2_timer) == 0) {
224 pr_debug("Handle T2(recv DATA) elapsed (T2 now inactive)\n");
225 ndlc->t2_active = false;
226 ndlc->t1_active = false;
227 del_timer_sync(&ndlc->t1_timer);
228 del_timer_sync(&ndlc->t2_timer);
229 ndlc_close(ndlc);
230 ndlc->hard_fault = -EREMOTEIO;
234 void ndlc_recv(struct llt_ndlc *ndlc, struct sk_buff *skb)
236 if (skb == NULL) {
237 pr_err("NULL Frame -> link is dead\n");
238 ndlc->hard_fault = -EREMOTEIO;
239 ndlc_close(ndlc);
240 } else {
241 NDLC_DUMP_SKB("incoming frame", skb);
242 skb_queue_tail(&ndlc->rcv_q, skb);
245 schedule_work(&ndlc->sm_work);
247 EXPORT_SYMBOL(ndlc_recv);
249 static void ndlc_t1_timeout(unsigned long data)
251 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
253 pr_debug("\n");
255 schedule_work(&ndlc->sm_work);
258 static void ndlc_t2_timeout(unsigned long data)
260 struct llt_ndlc *ndlc = (struct llt_ndlc *)data;
262 pr_debug("\n");
264 schedule_work(&ndlc->sm_work);
267 int ndlc_probe(void *phy_id, struct nfc_phy_ops *phy_ops, struct device *dev,
268 int phy_headroom, int phy_tailroom, struct llt_ndlc **ndlc_id,
269 struct st_nci_se_status *se_status)
271 struct llt_ndlc *ndlc;
273 ndlc = devm_kzalloc(dev, sizeof(struct llt_ndlc), GFP_KERNEL);
274 if (!ndlc)
275 return -ENOMEM;
277 ndlc->ops = phy_ops;
278 ndlc->phy_id = phy_id;
279 ndlc->dev = dev;
280 ndlc->powered = 0;
282 *ndlc_id = ndlc;
284 /* initialize timers */
285 init_timer(&ndlc->t1_timer);
286 ndlc->t1_timer.data = (unsigned long)ndlc;
287 ndlc->t1_timer.function = ndlc_t1_timeout;
289 init_timer(&ndlc->t2_timer);
290 ndlc->t2_timer.data = (unsigned long)ndlc;
291 ndlc->t2_timer.function = ndlc_t2_timeout;
293 skb_queue_head_init(&ndlc->rcv_q);
294 skb_queue_head_init(&ndlc->send_q);
295 skb_queue_head_init(&ndlc->ack_pending_q);
297 INIT_WORK(&ndlc->sm_work, llt_ndlc_sm_work);
299 return st_nci_probe(ndlc, phy_headroom, phy_tailroom, se_status);
301 EXPORT_SYMBOL(ndlc_probe);
303 void ndlc_remove(struct llt_ndlc *ndlc)
305 st_nci_remove(ndlc->ndev);
307 /* cancel timers */
308 del_timer_sync(&ndlc->t1_timer);
309 del_timer_sync(&ndlc->t2_timer);
310 ndlc->t2_active = false;
311 ndlc->t1_active = false;
313 skb_queue_purge(&ndlc->rcv_q);
314 skb_queue_purge(&ndlc->send_q);
316 EXPORT_SYMBOL(ndlc_remove);