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>
81 #define SHDLC_LLC_HEAD_ROOM 2
83 #define SHDLC_MAX_WINDOW 4
84 #define SHDLC_SREJ_SUPPORT false
86 #define SHDLC_CONTROL_HEAD_MASK 0xe0
87 #define SHDLC_CONTROL_HEAD_I 0x80
88 #define SHDLC_CONTROL_HEAD_I2 0xa0
89 #define SHDLC_CONTROL_HEAD_S 0xc0
90 #define SHDLC_CONTROL_HEAD_U 0xe0
92 #define SHDLC_CONTROL_NS_MASK 0x38
93 #define SHDLC_CONTROL_NR_MASK 0x07
94 #define SHDLC_CONTROL_TYPE_MASK 0x18
96 #define SHDLC_CONTROL_M_MASK 0x1f
110 #define SHDLC_CONNECT_VALUE_MS 5
111 #define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
112 #define SHDLC_T2_VALUE_MS 300
114 #define SHDLC_DUMP_SKB(info, skb) \
116 pr_debug("%s:\n", info); \
117 print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
118 16, 1, skb->data, skb->len, 0); \
122 static bool llc_shdlc_x_lt_y_lteq_z(
int x,
int y,
int z)
125 return ((x < y) && (y <= z)) ?
true :
false;
127 return ((y > x) || (y <= z)) ?
true :
false;
131 static bool llc_shdlc_x_lteq_y_lt_z(
int x,
int y,
int z)
134 return ((x <= y) && (y < z)) ?
true :
false;
136 return ((y >= x) || (y < z)) ?
true :
false;
153 static int llc_shdlc_send_s_frame(
struct llc_shdlc *shdlc,
159 pr_debug(
"sframe_type=%d nr=%d\n", sframe_type, nr);
161 skb = llc_shdlc_alloc_skb(shdlc, 0);
175 static int llc_shdlc_send_u_frame(
struct llc_shdlc *shdlc,
181 pr_debug(
"uframe_modifier=%d\n", uframe_modifier);
196 static void llc_shdlc_reset_t2(
struct llc_shdlc *shdlc,
int y_nr)
199 int dnr = shdlc->
dnr;
201 pr_debug(
"release ack pending up to frame %d excluded\n", y_nr);
203 while (dnr != y_nr) {
204 pr_debug(
"release ack pending frame %d\n", dnr);
218 (
"All sent frames acked. Stopped T2(retransmit)\n");
228 (
"Start T2(retransmit) for remaining unacked sent frames\n");
236 static void llc_shdlc_rcv_i_frame(
struct llc_shdlc *shdlc,
242 pr_debug(
"recvd I-frame %d, remote waiting frame %d\n", ns, nr);
247 if (x_ns != shdlc->
nr) {
256 pr_debug(
"(re)Start T1(send ack)\n");
264 shdlc->
nr = (shdlc->
nr + 1) % 8;
266 if (llc_shdlc_x_lt_y_lteq_z(shdlc->
dnr, y_nr, shdlc->
ns)) {
267 llc_shdlc_reset_t2(shdlc, y_nr);
276 static void llc_shdlc_rcv_ack(
struct llc_shdlc *shdlc,
int y_nr)
278 pr_debug(
"remote acked up to frame %d excluded\n", y_nr);
280 if (llc_shdlc_x_lt_y_lteq_z(shdlc->
dnr, y_nr, shdlc->
ns)) {
281 llc_shdlc_reset_t2(shdlc, y_nr);
286 static void llc_shdlc_requeue_ack_pending(
struct llc_shdlc *shdlc)
296 shdlc->
ns = shdlc->
dnr;
299 static void llc_shdlc_rcv_rej(
struct llc_shdlc *shdlc,
int y_nr)
303 pr_debug(
"remote asks retransmition from frame %d\n", y_nr);
305 if (llc_shdlc_x_lteq_y_lt_z(shdlc->
dnr, y_nr, shdlc->
ns)) {
309 pr_debug(
"Stopped T2(retransmit)\n");
312 if (shdlc->
dnr != y_nr) {
313 while ((shdlc->
dnr = ((shdlc->
dnr + 1) % 8)) != y_nr) {
319 llc_shdlc_requeue_ack_pending(shdlc);
324 static void llc_shdlc_rcv_s_frame(
struct llc_shdlc *shdlc,
325 enum sframe_type s_frame_type,
int nr)
332 switch (s_frame_type) {
334 llc_shdlc_rcv_ack(shdlc, nr);
335 if (shdlc->
rnr ==
true) {
337 if (shdlc->
send_q.qlen == 0) {
338 skb = llc_shdlc_alloc_skb(shdlc, 0);
345 llc_shdlc_rcv_rej(shdlc, nr);
348 llc_shdlc_rcv_ack(shdlc, nr);
356 static void llc_shdlc_connect_complete(
struct llc_shdlc *shdlc,
int r)
377 static int llc_shdlc_connect_initiate(
struct llc_shdlc *shdlc)
383 skb = llc_shdlc_alloc_skb(shdlc, 2);
390 return llc_shdlc_send_u_frame(shdlc, skb,
U_FRAME_RSET);
393 static int llc_shdlc_connect_send_ua(
struct llc_shdlc *shdlc)
399 skb = llc_shdlc_alloc_skb(shdlc, 0);
403 return llc_shdlc_send_u_frame(shdlc, skb,
U_FRAME_UA);
406 static void llc_shdlc_rcv_u_frame(
struct llc_shdlc *shdlc,
408 enum uframe_modifier u_frame_modifier)
414 pr_debug(
"u_frame_modifier=%d\n", u_frame_modifier);
416 switch (u_frame_modifier) {
418 switch (shdlc->
state) {
429 srej_support = skb->
data[1] & 0x01 ?
true :
436 r = llc_shdlc_connect_send_ua(shdlc);
437 llc_shdlc_connect_complete(shdlc, r);
461 llc_shdlc_connect_complete(shdlc, 0);
472 static void llc_shdlc_handle_rcv_queue(
struct llc_shdlc *shdlc)
478 enum sframe_type s_frame_type;
479 enum uframe_modifier u_frame_modifier;
481 if (shdlc->
rcv_q.qlen)
485 control = skb->
data[0];
495 llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
503 llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
508 llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
511 pr_err(
"UNKNOWN Control=%d\n", control);
518 static int llc_shdlc_w_used(
int ns,
int dnr)
523 unack_count = ns - dnr;
525 unack_count = 8 - dnr +
ns;
531 static void llc_shdlc_handle_send_queue(
struct llc_shdlc *shdlc)
535 unsigned long time_sent;
539 (
"sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
541 shdlc->
rnr ==
false ?
"false" :
"true",
542 shdlc->
w - llc_shdlc_w_used(shdlc->
ns, shdlc->
dnr),
546 (shdlc->
rnr ==
false)) {
559 pr_debug(
"Sending I-Frame %d, waiting to rcv %d\n", shdlc->
ns,
569 shdlc->
ns = (shdlc->
ns + 1) % 8;
572 *(
unsigned long *)skb->cb = time_sent;
580 pr_debug(
"Started T2 (retransmit)\n");
585 static void llc_shdlc_connect_timeout(
unsigned long data)
594 static void llc_shdlc_t1_timeout(
unsigned long data)
598 pr_debug(
"SoftIRQ: need to send ack\n");
603 static void llc_shdlc_t2_timeout(
unsigned long data)
607 pr_debug(
"SoftIRQ: need to retransmit\n");
621 switch (shdlc->
state) {
629 llc_shdlc_connect_complete(shdlc, shdlc->
hard_fault);
634 r = llc_shdlc_connect_initiate(shdlc);
638 llc_shdlc_connect_complete(shdlc, r);
652 llc_shdlc_handle_rcv_queue(shdlc);
655 llc_shdlc_connect_complete(shdlc, shdlc->
hard_fault);
661 llc_shdlc_handle_rcv_queue(shdlc);
662 llc_shdlc_handle_send_queue(shdlc);
666 (
"Handle T1(send ack) elapsed (T1 now inactive)\n");
677 (
"Handle T2(retransmit) elapsed (T2 inactive)\n");
681 llc_shdlc_requeue_ack_pending(shdlc);
682 llc_shdlc_handle_send_queue(shdlc);
699 static int llc_shdlc_connect(
struct llc_shdlc *shdlc)
721 static void llc_shdlc_disconnect(
struct llc_shdlc *shdlc)
739 static void llc_shdlc_recv_frame(
struct llc_shdlc *shdlc,
struct sk_buff *skb)
742 pr_err(
"NULL Frame -> link is dead\n");
754 int tx_tailroom,
int *rx_headroom,
int *rx_tailroom,
775 shdlc->
t1_timer.function = llc_shdlc_t1_timeout;
779 shdlc->
t2_timer.function = llc_shdlc_t2_timeout;
784 skb_queue_head_init(&shdlc->
rcv_q);
785 skb_queue_head_init(&shdlc->
send_q);
800 static void llc_shdlc_deinit(
struct nfc_llc *llc)
811 static int llc_shdlc_start(
struct nfc_llc *llc)
815 return llc_shdlc_connect(shdlc);
818 static int llc_shdlc_stop(
struct nfc_llc *llc)
822 llc_shdlc_disconnect(shdlc);
827 static void llc_shdlc_rcv_from_drv(
struct nfc_llc *llc,
struct sk_buff *skb)
831 llc_shdlc_recv_frame(shdlc, skb);
834 static int llc_shdlc_xmit_from_hci(
struct nfc_llc *llc,
struct sk_buff *skb)
846 .init = llc_shdlc_init,
847 .deinit = llc_shdlc_deinit,
848 .start = llc_shdlc_start,
849 .stop = llc_shdlc_stop,
850 .rcv_from_drv = llc_shdlc_rcv_from_drv,
851 .xmit_from_hci = llc_shdlc_xmit_from_hci,