2 * shdlc Link Layer Control
4 * Copyright (C) 2012 Intel Corporation. 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, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
23 #include <linux/types.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
26 #include <linux/slab.h>
27 #include <linux/skbuff.h>
32 SHDLC_DISCONNECTED = 0,
34 SHDLC_NEGOCIATING = 2,
39 struct nfc_hci_dev *hdev;
40 xmit_to_drv_t xmit_to_drv;
41 rcv_to_hci_t rcv_to_hci;
43 struct mutex state_mutex;
44 enum shdlc_state state;
47 wait_queue_head_t *connect_wq;
50 struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
52 u8 w; /* window size */
55 struct timer_list t1_timer; /* send ack timeout */
58 struct timer_list t2_timer; /* guard/retransmit timeout */
61 int ns; /* next seq num for send */
62 int nr; /* next expected seq num for receive */
63 int dnr; /* oldest sent unacked seq num */
65 struct sk_buff_head rcv_q;
67 struct sk_buff_head send_q;
68 bool rnr; /* other side is not ready to receive */
70 struct sk_buff_head ack_pending_q;
72 struct work_struct sm_work;
77 llc_failure_t llc_failure;
80 #define SHDLC_LLC_HEAD_ROOM 2
82 #define SHDLC_MAX_WINDOW 4
83 #define SHDLC_SREJ_SUPPORT false
85 #define SHDLC_CONTROL_HEAD_MASK 0xe0
86 #define SHDLC_CONTROL_HEAD_I 0x80
87 #define SHDLC_CONTROL_HEAD_I2 0xa0
88 #define SHDLC_CONTROL_HEAD_S 0xc0
89 #define SHDLC_CONTROL_HEAD_U 0xe0
91 #define SHDLC_CONTROL_NS_MASK 0x38
92 #define SHDLC_CONTROL_NR_MASK 0x07
93 #define SHDLC_CONTROL_TYPE_MASK 0x18
95 #define SHDLC_CONTROL_M_MASK 0x1f
104 enum uframe_modifier {
109 #define SHDLC_CONNECT_VALUE_MS 5
110 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
111 #define SHDLC_T2_VALUE_MS 300
113 #define SHDLC_DUMP_SKB(info, skb) \
115 pr_debug("%s:\n", info); \
116 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
117 16, 1, skb->data, skb->len, 0); \
120 /* checks x < y <= z modulo 8 */
121 static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
124 return ((x < y) && (y <= z)) ? true : false;
126 return ((y > x) || (y <= z)) ? true : false;
129 /* checks x <= y < z modulo 8 */
130 static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
133 return ((x <= y) && (y < z)) ? true : false;
134 else /* x > z -> z+8 > x */
135 return ((y >= x) || (y < z)) ? true : false;
138 static struct sk_buff *llc_shdlc_alloc_skb(struct llc_shdlc *shdlc,
143 skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
144 shdlc->tx_tailroom + payload_len, GFP_KERNEL);
146 skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
151 /* immediately sends an S frame. */
152 static int llc_shdlc_send_s_frame(struct llc_shdlc *shdlc,
153 enum sframe_type sframe_type, int nr)
158 pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
160 skb = llc_shdlc_alloc_skb(shdlc, 0);
164 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
166 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
173 /* immediately sends an U frame. skb may contain optional payload */
174 static int llc_shdlc_send_u_frame(struct llc_shdlc *shdlc,
176 enum uframe_modifier uframe_modifier)
180 pr_debug("uframe_modifier=%d\n", uframe_modifier);
182 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
184 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
192 * Free ack_pending frames until y_nr - 1, and reset t2 according to
193 * the remaining oldest ack_pending frame sent time
195 static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
198 int dnr = shdlc->dnr; /* MUST initially be < y_nr */
200 pr_debug("release ack pending up to frame %d excluded\n", y_nr);
202 while (dnr != y_nr) {
203 pr_debug("release ack pending frame %d\n", dnr);
205 skb = skb_dequeue(&shdlc->ack_pending_q);
211 if (skb_queue_empty(&shdlc->ack_pending_q)) {
212 if (shdlc->t2_active) {
213 del_timer_sync(&shdlc->t2_timer);
214 shdlc->t2_active = false;
217 ("All sent frames acked. Stopped T2(retransmit)\n");
220 skb = skb_peek(&shdlc->ack_pending_q);
222 mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
223 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
224 shdlc->t2_active = true;
227 ("Start T2(retransmit) for remaining unacked sent frames\n");
232 * Receive validated frames from lower layer. skb contains HCI payload only.
233 * Handle according to algorithm at spec:10.8.2
235 static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
236 struct sk_buff *skb, int ns, int nr)
241 pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
243 if (shdlc->state != SHDLC_CONNECTED)
246 if (x_ns != shdlc->nr) {
247 llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
251 if (shdlc->t1_active == false) {
252 shdlc->t1_active = true;
253 mod_timer(&shdlc->t1_timer, jiffies +
254 msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
255 pr_debug("(re)Start T1(send ack)\n");
259 shdlc->rcv_to_hci(shdlc->hdev, skb);
263 shdlc->nr = (shdlc->nr + 1) % 8;
265 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
266 llc_shdlc_reset_t2(shdlc, y_nr);
275 static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
277 pr_debug("remote acked up to frame %d excluded\n", y_nr);
279 if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
280 llc_shdlc_reset_t2(shdlc, y_nr);
285 static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
289 pr_debug("ns reset to %d\n", shdlc->dnr);
291 while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
292 skb_pull(skb, 1); /* remove control field */
293 skb_queue_head(&shdlc->send_q, skb);
295 shdlc->ns = shdlc->dnr;
298 static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
302 pr_debug("remote asks retransmition from frame %d\n", y_nr);
304 if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
305 if (shdlc->t2_active) {
306 del_timer_sync(&shdlc->t2_timer);
307 shdlc->t2_active = false;
308 pr_debug("Stopped T2(retransmit)\n");
311 if (shdlc->dnr != y_nr) {
312 while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
313 skb = skb_dequeue(&shdlc->ack_pending_q);
318 llc_shdlc_requeue_ack_pending(shdlc);
322 /* See spec RR:10.8.3 REJ:10.8.4 */
323 static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
324 enum sframe_type s_frame_type, int nr)
328 if (shdlc->state != SHDLC_CONNECTED)
331 switch (s_frame_type) {
333 llc_shdlc_rcv_ack(shdlc, nr);
334 if (shdlc->rnr == true) { /* see SHDLC 10.7.7 */
336 if (shdlc->send_q.qlen == 0) {
337 skb = llc_shdlc_alloc_skb(shdlc, 0);
339 skb_queue_tail(&shdlc->send_q, skb);
344 llc_shdlc_rcv_rej(shdlc, nr);
347 llc_shdlc_rcv_ack(shdlc, nr);
355 static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
357 pr_debug("result=%d\n", r);
359 del_timer_sync(&shdlc->connect_timer);
366 shdlc->state = SHDLC_CONNECTED;
368 shdlc->state = SHDLC_DISCONNECTED;
371 shdlc->connect_result = r;
373 wake_up(shdlc->connect_wq);
376 static int llc_shdlc_connect_initiate(struct llc_shdlc *shdlc)
382 skb = llc_shdlc_alloc_skb(shdlc, 2);
386 *skb_put(skb, 1) = SHDLC_MAX_WINDOW;
387 *skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
389 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
392 static int llc_shdlc_connect_send_ua(struct llc_shdlc *shdlc)
398 skb = llc_shdlc_alloc_skb(shdlc, 0);
402 return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
405 static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
407 enum uframe_modifier u_frame_modifier)
409 u8 w = SHDLC_MAX_WINDOW;
410 bool srej_support = SHDLC_SREJ_SUPPORT;
413 pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
415 switch (u_frame_modifier) {
417 if (shdlc->state == SHDLC_NEGOCIATING) {
418 /* we sent RSET, but chip wants to negociate */
423 srej_support = skb->data[1] & 0x01 ? true :
426 if ((w <= SHDLC_MAX_WINDOW) &&
427 (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
429 shdlc->srej_support = srej_support;
430 r = llc_shdlc_connect_send_ua(shdlc);
431 llc_shdlc_connect_complete(shdlc, r);
433 } else if (shdlc->state == SHDLC_CONNECTED) {
435 * Chip wants to reset link. This is unexpected and
438 shdlc->hard_fault = -ECONNRESET;
442 if ((shdlc->state == SHDLC_CONNECTING &&
443 shdlc->connect_tries > 0) ||
444 (shdlc->state == SHDLC_NEGOCIATING))
445 llc_shdlc_connect_complete(shdlc, 0);
454 static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
460 enum sframe_type s_frame_type;
461 enum uframe_modifier u_frame_modifier;
463 if (shdlc->rcv_q.qlen)
464 pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
466 while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
467 control = skb->data[0];
469 switch (control & SHDLC_CONTROL_HEAD_MASK) {
470 case SHDLC_CONTROL_HEAD_I:
471 case SHDLC_CONTROL_HEAD_I2:
472 ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
473 nr = control & SHDLC_CONTROL_NR_MASK;
474 llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
476 case SHDLC_CONTROL_HEAD_S:
477 s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
478 nr = control & SHDLC_CONTROL_NR_MASK;
479 llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
482 case SHDLC_CONTROL_HEAD_U:
483 u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
484 llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
487 pr_err("UNKNOWN Control=%d\n", control);
494 static int llc_shdlc_w_used(int ns, int dnr)
499 unack_count = ns - dnr;
501 unack_count = 8 - dnr + ns;
506 /* Send frames according to algorithm at spec:10.8.1 */
507 static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
511 unsigned long time_sent;
513 if (shdlc->send_q.qlen)
515 ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
516 shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
517 shdlc->rnr == false ? "false" : "true",
518 shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
519 shdlc->ack_pending_q.qlen);
521 while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
522 (shdlc->rnr == false)) {
524 if (shdlc->t1_active) {
525 del_timer_sync(&shdlc->t1_timer);
526 shdlc->t1_active = false;
527 pr_debug("Stopped T1(send ack)\n");
530 skb = skb_dequeue(&shdlc->send_q);
532 *skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
535 pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
537 SHDLC_DUMP_SKB("shdlc frame written", skb);
539 r = shdlc->xmit_to_drv(shdlc->hdev, skb);
541 shdlc->hard_fault = r;
545 shdlc->ns = (shdlc->ns + 1) % 8;
548 *(unsigned long *)skb->cb = time_sent;
550 skb_queue_tail(&shdlc->ack_pending_q, skb);
552 if (shdlc->t2_active == false) {
553 shdlc->t2_active = true;
554 mod_timer(&shdlc->t2_timer, time_sent +
555 msecs_to_jiffies(SHDLC_T2_VALUE_MS));
556 pr_debug("Started T2 (retransmit)\n");
561 static void llc_shdlc_connect_timeout(unsigned long data)
563 struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
567 queue_work(system_nrt_wq, &shdlc->sm_work);
570 static void llc_shdlc_t1_timeout(unsigned long data)
572 struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
574 pr_debug("SoftIRQ: need to send ack\n");
576 queue_work(system_nrt_wq, &shdlc->sm_work);
579 static void llc_shdlc_t2_timeout(unsigned long data)
581 struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
583 pr_debug("SoftIRQ: need to retransmit\n");
585 queue_work(system_nrt_wq, &shdlc->sm_work);
588 static void llc_shdlc_sm_work(struct work_struct *work)
590 struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
595 mutex_lock(&shdlc->state_mutex);
597 switch (shdlc->state) {
598 case SHDLC_DISCONNECTED:
599 skb_queue_purge(&shdlc->rcv_q);
600 skb_queue_purge(&shdlc->send_q);
601 skb_queue_purge(&shdlc->ack_pending_q);
603 case SHDLC_CONNECTING:
604 if (shdlc->hard_fault) {
605 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
609 if (shdlc->connect_tries++ < 5)
610 r = llc_shdlc_connect_initiate(shdlc);
614 llc_shdlc_connect_complete(shdlc, r);
616 mod_timer(&shdlc->connect_timer, jiffies +
617 msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
619 shdlc->state = SHDLC_NEGOCIATING;
622 case SHDLC_NEGOCIATING:
623 if (timer_pending(&shdlc->connect_timer) == 0) {
624 shdlc->state = SHDLC_CONNECTING;
625 queue_work(system_nrt_wq, &shdlc->sm_work);
628 llc_shdlc_handle_rcv_queue(shdlc);
630 if (shdlc->hard_fault) {
631 llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
635 case SHDLC_CONNECTED:
636 llc_shdlc_handle_rcv_queue(shdlc);
637 llc_shdlc_handle_send_queue(shdlc);
639 if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
641 ("Handle T1(send ack) elapsed (T1 now inactive)\n");
643 shdlc->t1_active = false;
644 r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
647 shdlc->hard_fault = r;
650 if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
652 ("Handle T2(retransmit) elapsed (T2 inactive)\n");
654 shdlc->t2_active = false;
656 llc_shdlc_requeue_ack_pending(shdlc);
657 llc_shdlc_handle_send_queue(shdlc);
660 if (shdlc->hard_fault) {
661 shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
667 mutex_unlock(&shdlc->state_mutex);
671 * Called from syscall context to establish shdlc link. Sleeps until
672 * link is ready or failure.
674 static int llc_shdlc_connect(struct llc_shdlc *shdlc)
676 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
680 mutex_lock(&shdlc->state_mutex);
682 shdlc->state = SHDLC_CONNECTING;
683 shdlc->connect_wq = &connect_wq;
684 shdlc->connect_tries = 0;
685 shdlc->connect_result = 1;
687 mutex_unlock(&shdlc->state_mutex);
689 queue_work(system_nrt_wq, &shdlc->sm_work);
691 wait_event(connect_wq, shdlc->connect_result != 1);
693 return shdlc->connect_result;
696 static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
700 mutex_lock(&shdlc->state_mutex);
702 shdlc->state = SHDLC_DISCONNECTED;
704 mutex_unlock(&shdlc->state_mutex);
706 queue_work(system_nrt_wq, &shdlc->sm_work);
710 * Receive an incoming shdlc frame. Frame has already been crc-validated.
711 * skb contains only LLC header and payload.
712 * If skb == NULL, it is a notification that the link below is dead.
714 static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
717 pr_err("NULL Frame -> link is dead\n");
718 shdlc->hard_fault = -EREMOTEIO;
720 SHDLC_DUMP_SKB("incoming frame", skb);
721 skb_queue_tail(&shdlc->rcv_q, skb);
724 queue_work(system_nrt_wq, &shdlc->sm_work);
727 static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
728 rcv_to_hci_t rcv_to_hci, int tx_headroom,
729 int tx_tailroom, int *rx_headroom, int *rx_tailroom,
730 llc_failure_t llc_failure)
732 struct llc_shdlc *shdlc;
734 *rx_headroom = SHDLC_LLC_HEAD_ROOM;
737 shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
741 mutex_init(&shdlc->state_mutex);
742 shdlc->state = SHDLC_DISCONNECTED;
744 init_timer(&shdlc->connect_timer);
745 shdlc->connect_timer.data = (unsigned long)shdlc;
746 shdlc->connect_timer.function = llc_shdlc_connect_timeout;
748 init_timer(&shdlc->t1_timer);
749 shdlc->t1_timer.data = (unsigned long)shdlc;
750 shdlc->t1_timer.function = llc_shdlc_t1_timeout;
752 init_timer(&shdlc->t2_timer);
753 shdlc->t2_timer.data = (unsigned long)shdlc;
754 shdlc->t2_timer.function = llc_shdlc_t2_timeout;
756 shdlc->w = SHDLC_MAX_WINDOW;
757 shdlc->srej_support = SHDLC_SREJ_SUPPORT;
759 skb_queue_head_init(&shdlc->rcv_q);
760 skb_queue_head_init(&shdlc->send_q);
761 skb_queue_head_init(&shdlc->ack_pending_q);
763 INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
766 shdlc->xmit_to_drv = xmit_to_drv;
767 shdlc->rcv_to_hci = rcv_to_hci;
768 shdlc->tx_headroom = tx_headroom;
769 shdlc->tx_tailroom = tx_tailroom;
770 shdlc->llc_failure = llc_failure;
775 static void llc_shdlc_deinit(struct nfc_llc *llc)
777 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
779 skb_queue_purge(&shdlc->rcv_q);
780 skb_queue_purge(&shdlc->send_q);
781 skb_queue_purge(&shdlc->ack_pending_q);
786 static int llc_shdlc_start(struct nfc_llc *llc)
788 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
790 return llc_shdlc_connect(shdlc);
793 static int llc_shdlc_stop(struct nfc_llc *llc)
795 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
797 llc_shdlc_disconnect(shdlc);
802 static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
804 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
806 llc_shdlc_recv_frame(shdlc, skb);
809 static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
811 struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
813 skb_queue_tail(&shdlc->send_q, skb);
815 queue_work(system_nrt_wq, &shdlc->sm_work);
820 static struct nfc_llc_ops llc_shdlc_ops = {
821 .init = llc_shdlc_init,
822 .deinit = llc_shdlc_deinit,
823 .start = llc_shdlc_start,
824 .stop = llc_shdlc_stop,
825 .rcv_from_drv = llc_shdlc_rcv_from_drv,
826 .xmit_from_hci = llc_shdlc_xmit_from_hci,
829 int nfc_llc_shdlc_register()
831 return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);