xref: /openbmc/linux/net/nfc/hci/llc_shdlc.c (revision 916082b073ebb7f4e064cebce0768e34cacde508)
14a61cd66SEric Lapuyade /*
24a61cd66SEric Lapuyade  * shdlc Link Layer Control
34a61cd66SEric Lapuyade  *
44a61cd66SEric Lapuyade  * Copyright (C) 2012  Intel Corporation. All rights reserved.
54a61cd66SEric Lapuyade  *
64a61cd66SEric Lapuyade  * This program is free software; you can redistribute it and/or modify it
74a61cd66SEric Lapuyade  * under the terms and conditions of the GNU General Public License,
84a61cd66SEric Lapuyade  * version 2, as published by the Free Software Foundation.
94a61cd66SEric Lapuyade  *
104a61cd66SEric Lapuyade  * This program is distributed in the hope that it will be useful,
114a61cd66SEric Lapuyade  * but WITHOUT ANY WARRANTY; without even the implied warranty of
124a61cd66SEric Lapuyade  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134a61cd66SEric Lapuyade  * GNU General Public License for more details.
144a61cd66SEric Lapuyade  *
154a61cd66SEric Lapuyade  * You should have received a copy of the GNU General Public License
164a61cd66SEric Lapuyade  * along with this program; if not, write to the
174a61cd66SEric Lapuyade  * Free Software Foundation, Inc.,
184a61cd66SEric Lapuyade  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
194a61cd66SEric Lapuyade  */
204a61cd66SEric Lapuyade 
214a61cd66SEric Lapuyade #define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
224a61cd66SEric Lapuyade 
234a61cd66SEric Lapuyade #include <linux/types.h>
244a61cd66SEric Lapuyade #include <linux/sched.h>
254a61cd66SEric Lapuyade #include <linux/wait.h>
264a61cd66SEric Lapuyade #include <linux/slab.h>
274a61cd66SEric Lapuyade #include <linux/skbuff.h>
284a61cd66SEric Lapuyade 
294a61cd66SEric Lapuyade #include "llc.h"
304a61cd66SEric Lapuyade 
314a61cd66SEric Lapuyade enum shdlc_state {
324a61cd66SEric Lapuyade 	SHDLC_DISCONNECTED = 0,
334a61cd66SEric Lapuyade 	SHDLC_CONNECTING = 1,
344c0ba9acSWaldemar Rymarkiewicz 	SHDLC_NEGOTIATING = 2,
3512bfd1e8SWaldemar Rymarkiewicz 	SHDLC_HALF_CONNECTED = 3,
3612bfd1e8SWaldemar Rymarkiewicz 	SHDLC_CONNECTED = 4
374a61cd66SEric Lapuyade };
384a61cd66SEric Lapuyade 
394a61cd66SEric Lapuyade struct llc_shdlc {
404a61cd66SEric Lapuyade 	struct nfc_hci_dev *hdev;
414a61cd66SEric Lapuyade 	xmit_to_drv_t xmit_to_drv;
424a61cd66SEric Lapuyade 	rcv_to_hci_t rcv_to_hci;
434a61cd66SEric Lapuyade 
444a61cd66SEric Lapuyade 	struct mutex state_mutex;
454a61cd66SEric Lapuyade 	enum shdlc_state state;
464a61cd66SEric Lapuyade 	int hard_fault;
474a61cd66SEric Lapuyade 
484a61cd66SEric Lapuyade 	wait_queue_head_t *connect_wq;
494a61cd66SEric Lapuyade 	int connect_tries;
504a61cd66SEric Lapuyade 	int connect_result;
514a61cd66SEric Lapuyade 	struct timer_list connect_timer;/* aka T3 in spec 10.6.1 */
524a61cd66SEric Lapuyade 
534a61cd66SEric Lapuyade 	u8 w;				/* window size */
544a61cd66SEric Lapuyade 	bool srej_support;
554a61cd66SEric Lapuyade 
564a61cd66SEric Lapuyade 	struct timer_list t1_timer;	/* send ack timeout */
574a61cd66SEric Lapuyade 	bool t1_active;
584a61cd66SEric Lapuyade 
594a61cd66SEric Lapuyade 	struct timer_list t2_timer;	/* guard/retransmit timeout */
604a61cd66SEric Lapuyade 	bool t2_active;
614a61cd66SEric Lapuyade 
624a61cd66SEric Lapuyade 	int ns;				/* next seq num for send */
634a61cd66SEric Lapuyade 	int nr;				/* next expected seq num for receive */
644a61cd66SEric Lapuyade 	int dnr;			/* oldest sent unacked seq num */
654a61cd66SEric Lapuyade 
664a61cd66SEric Lapuyade 	struct sk_buff_head rcv_q;
674a61cd66SEric Lapuyade 
684a61cd66SEric Lapuyade 	struct sk_buff_head send_q;
694a61cd66SEric Lapuyade 	bool rnr;			/* other side is not ready to receive */
704a61cd66SEric Lapuyade 
714a61cd66SEric Lapuyade 	struct sk_buff_head ack_pending_q;
724a61cd66SEric Lapuyade 
734a61cd66SEric Lapuyade 	struct work_struct sm_work;
744a61cd66SEric Lapuyade 
754a61cd66SEric Lapuyade 	int tx_headroom;
764a61cd66SEric Lapuyade 	int tx_tailroom;
774a61cd66SEric Lapuyade 
784a61cd66SEric Lapuyade 	llc_failure_t llc_failure;
794a61cd66SEric Lapuyade };
804a61cd66SEric Lapuyade 
814a61cd66SEric Lapuyade #define SHDLC_LLC_HEAD_ROOM	2
824a61cd66SEric Lapuyade 
834a61cd66SEric Lapuyade #define SHDLC_MAX_WINDOW	4
844a61cd66SEric Lapuyade #define SHDLC_SREJ_SUPPORT	false
854a61cd66SEric Lapuyade 
864a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_MASK	0xe0
874a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_I	0x80
884a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_I2	0xa0
894a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_S	0xc0
904a61cd66SEric Lapuyade #define SHDLC_CONTROL_HEAD_U	0xe0
914a61cd66SEric Lapuyade 
924a61cd66SEric Lapuyade #define SHDLC_CONTROL_NS_MASK	0x38
934a61cd66SEric Lapuyade #define SHDLC_CONTROL_NR_MASK	0x07
944a61cd66SEric Lapuyade #define SHDLC_CONTROL_TYPE_MASK	0x18
954a61cd66SEric Lapuyade 
964a61cd66SEric Lapuyade #define SHDLC_CONTROL_M_MASK	0x1f
974a61cd66SEric Lapuyade 
984a61cd66SEric Lapuyade enum sframe_type {
994a61cd66SEric Lapuyade 	S_FRAME_RR = 0x00,
1004a61cd66SEric Lapuyade 	S_FRAME_REJ = 0x01,
1014a61cd66SEric Lapuyade 	S_FRAME_RNR = 0x02,
1024a61cd66SEric Lapuyade 	S_FRAME_SREJ = 0x03
1034a61cd66SEric Lapuyade };
1044a61cd66SEric Lapuyade 
1054a61cd66SEric Lapuyade enum uframe_modifier {
1064a61cd66SEric Lapuyade 	U_FRAME_UA = 0x06,
1074a61cd66SEric Lapuyade 	U_FRAME_RSET = 0x19
1084a61cd66SEric Lapuyade };
1094a61cd66SEric Lapuyade 
1104a61cd66SEric Lapuyade #define SHDLC_CONNECT_VALUE_MS	5
1114a61cd66SEric Lapuyade #define SHDLC_T1_VALUE_MS(w)	((5 * w) / 4)
1124a61cd66SEric Lapuyade #define SHDLC_T2_VALUE_MS	300
1134a61cd66SEric Lapuyade 
1144a61cd66SEric Lapuyade #define SHDLC_DUMP_SKB(info, skb)				  \
1154a61cd66SEric Lapuyade do {								  \
1164a61cd66SEric Lapuyade 	pr_debug("%s:\n", info);				  \
1174a61cd66SEric Lapuyade 	print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
1184a61cd66SEric Lapuyade 		       16, 1, skb->data, skb->len, 0);		  \
1194a61cd66SEric Lapuyade } while (0)
1204a61cd66SEric Lapuyade 
1214a61cd66SEric Lapuyade /* checks x < y <= z modulo 8 */
1224a61cd66SEric Lapuyade static bool llc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
1234a61cd66SEric Lapuyade {
1244a61cd66SEric Lapuyade 	if (x < z)
1254a61cd66SEric Lapuyade 		return ((x < y) && (y <= z)) ? true : false;
1264a61cd66SEric Lapuyade 	else
1274a61cd66SEric Lapuyade 		return ((y > x) || (y <= z)) ? true : false;
1284a61cd66SEric Lapuyade }
1294a61cd66SEric Lapuyade 
1304a61cd66SEric Lapuyade /* checks x <= y < z modulo 8 */
1314a61cd66SEric Lapuyade static bool llc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
1324a61cd66SEric Lapuyade {
1334a61cd66SEric Lapuyade 	if (x <= z)
1344a61cd66SEric Lapuyade 		return ((x <= y) && (y < z)) ? true : false;
1354a61cd66SEric Lapuyade 	else			/* x > z -> z+8 > x */
1364a61cd66SEric Lapuyade 		return ((y >= x) || (y < z)) ? true : false;
1374a61cd66SEric Lapuyade }
1384a61cd66SEric Lapuyade 
1394a61cd66SEric Lapuyade static struct sk_buff *llc_shdlc_alloc_skb(struct llc_shdlc *shdlc,
1404a61cd66SEric Lapuyade 					   int payload_len)
1414a61cd66SEric Lapuyade {
1424a61cd66SEric Lapuyade 	struct sk_buff *skb;
1434a61cd66SEric Lapuyade 
1444a61cd66SEric Lapuyade 	skb = alloc_skb(shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM +
1454a61cd66SEric Lapuyade 			shdlc->tx_tailroom + payload_len, GFP_KERNEL);
1464a61cd66SEric Lapuyade 	if (skb)
1474a61cd66SEric Lapuyade 		skb_reserve(skb, shdlc->tx_headroom + SHDLC_LLC_HEAD_ROOM);
1484a61cd66SEric Lapuyade 
1494a61cd66SEric Lapuyade 	return skb;
1504a61cd66SEric Lapuyade }
1514a61cd66SEric Lapuyade 
1524a61cd66SEric Lapuyade /* immediately sends an S frame. */
1534a61cd66SEric Lapuyade static int llc_shdlc_send_s_frame(struct llc_shdlc *shdlc,
1544a61cd66SEric Lapuyade 				  enum sframe_type sframe_type, int nr)
1554a61cd66SEric Lapuyade {
1564a61cd66SEric Lapuyade 	int r;
1574a61cd66SEric Lapuyade 	struct sk_buff *skb;
1584a61cd66SEric Lapuyade 
1594a61cd66SEric Lapuyade 	pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
1604a61cd66SEric Lapuyade 
1614a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 0);
1624a61cd66SEric Lapuyade 	if (skb == NULL)
1634a61cd66SEric Lapuyade 		return -ENOMEM;
1644a61cd66SEric Lapuyade 
1654a61cd66SEric Lapuyade 	*skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
1664a61cd66SEric Lapuyade 
1674a61cd66SEric Lapuyade 	r = shdlc->xmit_to_drv(shdlc->hdev, skb);
1684a61cd66SEric Lapuyade 
1694a61cd66SEric Lapuyade 	kfree_skb(skb);
1704a61cd66SEric Lapuyade 
1714a61cd66SEric Lapuyade 	return r;
1724a61cd66SEric Lapuyade }
1734a61cd66SEric Lapuyade 
1744a61cd66SEric Lapuyade /* immediately sends an U frame. skb may contain optional payload */
1754a61cd66SEric Lapuyade static int llc_shdlc_send_u_frame(struct llc_shdlc *shdlc,
1764a61cd66SEric Lapuyade 				  struct sk_buff *skb,
1774a61cd66SEric Lapuyade 				  enum uframe_modifier uframe_modifier)
1784a61cd66SEric Lapuyade {
1794a61cd66SEric Lapuyade 	int r;
1804a61cd66SEric Lapuyade 
1814a61cd66SEric Lapuyade 	pr_debug("uframe_modifier=%d\n", uframe_modifier);
1824a61cd66SEric Lapuyade 
1834a61cd66SEric Lapuyade 	*skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
1844a61cd66SEric Lapuyade 
1854a61cd66SEric Lapuyade 	r = shdlc->xmit_to_drv(shdlc->hdev, skb);
1864a61cd66SEric Lapuyade 
1874a61cd66SEric Lapuyade 	kfree_skb(skb);
1884a61cd66SEric Lapuyade 
1894a61cd66SEric Lapuyade 	return r;
1904a61cd66SEric Lapuyade }
1914a61cd66SEric Lapuyade 
1924a61cd66SEric Lapuyade /*
1934a61cd66SEric Lapuyade  * Free ack_pending frames until y_nr - 1, and reset t2 according to
1944a61cd66SEric Lapuyade  * the remaining oldest ack_pending frame sent time
1954a61cd66SEric Lapuyade  */
1964a61cd66SEric Lapuyade static void llc_shdlc_reset_t2(struct llc_shdlc *shdlc, int y_nr)
1974a61cd66SEric Lapuyade {
1984a61cd66SEric Lapuyade 	struct sk_buff *skb;
1994a61cd66SEric Lapuyade 	int dnr = shdlc->dnr;	/* MUST initially be < y_nr */
2004a61cd66SEric Lapuyade 
2014a61cd66SEric Lapuyade 	pr_debug("release ack pending up to frame %d excluded\n", y_nr);
2024a61cd66SEric Lapuyade 
2034a61cd66SEric Lapuyade 	while (dnr != y_nr) {
2044a61cd66SEric Lapuyade 		pr_debug("release ack pending frame %d\n", dnr);
2054a61cd66SEric Lapuyade 
2064a61cd66SEric Lapuyade 		skb = skb_dequeue(&shdlc->ack_pending_q);
2074a61cd66SEric Lapuyade 		kfree_skb(skb);
2084a61cd66SEric Lapuyade 
2094a61cd66SEric Lapuyade 		dnr = (dnr + 1) % 8;
2104a61cd66SEric Lapuyade 	}
2114a61cd66SEric Lapuyade 
2124a61cd66SEric Lapuyade 	if (skb_queue_empty(&shdlc->ack_pending_q)) {
2134a61cd66SEric Lapuyade 		if (shdlc->t2_active) {
2144a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t2_timer);
2154a61cd66SEric Lapuyade 			shdlc->t2_active = false;
2164a61cd66SEric Lapuyade 
2174a61cd66SEric Lapuyade 			pr_debug
2184a61cd66SEric Lapuyade 			    ("All sent frames acked. Stopped T2(retransmit)\n");
2194a61cd66SEric Lapuyade 		}
2204a61cd66SEric Lapuyade 	} else {
2214a61cd66SEric Lapuyade 		skb = skb_peek(&shdlc->ack_pending_q);
2224a61cd66SEric Lapuyade 
2234a61cd66SEric Lapuyade 		mod_timer(&shdlc->t2_timer, *(unsigned long *)skb->cb +
2244a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
2254a61cd66SEric Lapuyade 		shdlc->t2_active = true;
2264a61cd66SEric Lapuyade 
2274a61cd66SEric Lapuyade 		pr_debug
2284a61cd66SEric Lapuyade 		    ("Start T2(retransmit) for remaining unacked sent frames\n");
2294a61cd66SEric Lapuyade 	}
2304a61cd66SEric Lapuyade }
2314a61cd66SEric Lapuyade 
2324a61cd66SEric Lapuyade /*
2334a61cd66SEric Lapuyade  * Receive validated frames from lower layer. skb contains HCI payload only.
2344a61cd66SEric Lapuyade  * Handle according to algorithm at spec:10.8.2
2354a61cd66SEric Lapuyade  */
2364a61cd66SEric Lapuyade static void llc_shdlc_rcv_i_frame(struct llc_shdlc *shdlc,
2374a61cd66SEric Lapuyade 				  struct sk_buff *skb, int ns, int nr)
2384a61cd66SEric Lapuyade {
2394a61cd66SEric Lapuyade 	int x_ns = ns;
2404a61cd66SEric Lapuyade 	int y_nr = nr;
2414a61cd66SEric Lapuyade 
2424a61cd66SEric Lapuyade 	pr_debug("recvd I-frame %d, remote waiting frame %d\n", ns, nr);
2434a61cd66SEric Lapuyade 
2444a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
2454a61cd66SEric Lapuyade 		goto exit;
2464a61cd66SEric Lapuyade 
2474a61cd66SEric Lapuyade 	if (x_ns != shdlc->nr) {
2484a61cd66SEric Lapuyade 		llc_shdlc_send_s_frame(shdlc, S_FRAME_REJ, shdlc->nr);
2494a61cd66SEric Lapuyade 		goto exit;
2504a61cd66SEric Lapuyade 	}
2514a61cd66SEric Lapuyade 
2524a61cd66SEric Lapuyade 	if (shdlc->t1_active == false) {
2534a61cd66SEric Lapuyade 		shdlc->t1_active = true;
2544a61cd66SEric Lapuyade 		mod_timer(&shdlc->t1_timer, jiffies +
2554a61cd66SEric Lapuyade 			  msecs_to_jiffies(SHDLC_T1_VALUE_MS(shdlc->w)));
2564a61cd66SEric Lapuyade 		pr_debug("(re)Start T1(send ack)\n");
2574a61cd66SEric Lapuyade 	}
2584a61cd66SEric Lapuyade 
2594a61cd66SEric Lapuyade 	if (skb->len) {
2604a61cd66SEric Lapuyade 		shdlc->rcv_to_hci(shdlc->hdev, skb);
2614a61cd66SEric Lapuyade 		skb = NULL;
2624a61cd66SEric Lapuyade 	}
2634a61cd66SEric Lapuyade 
2644a61cd66SEric Lapuyade 	shdlc->nr = (shdlc->nr + 1) % 8;
2654a61cd66SEric Lapuyade 
2664a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2674a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2684a61cd66SEric Lapuyade 
2694a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2704a61cd66SEric Lapuyade 	}
2714a61cd66SEric Lapuyade 
2724a61cd66SEric Lapuyade exit:
2734a61cd66SEric Lapuyade 	kfree_skb(skb);
2744a61cd66SEric Lapuyade }
2754a61cd66SEric Lapuyade 
2764a61cd66SEric Lapuyade static void llc_shdlc_rcv_ack(struct llc_shdlc *shdlc, int y_nr)
2774a61cd66SEric Lapuyade {
2784a61cd66SEric Lapuyade 	pr_debug("remote acked up to frame %d excluded\n", y_nr);
2794a61cd66SEric Lapuyade 
2804a61cd66SEric Lapuyade 	if (llc_shdlc_x_lt_y_lteq_z(shdlc->dnr, y_nr, shdlc->ns)) {
2814a61cd66SEric Lapuyade 		llc_shdlc_reset_t2(shdlc, y_nr);
2824a61cd66SEric Lapuyade 		shdlc->dnr = y_nr;
2834a61cd66SEric Lapuyade 	}
2844a61cd66SEric Lapuyade }
2854a61cd66SEric Lapuyade 
2864a61cd66SEric Lapuyade static void llc_shdlc_requeue_ack_pending(struct llc_shdlc *shdlc)
2874a61cd66SEric Lapuyade {
2884a61cd66SEric Lapuyade 	struct sk_buff *skb;
2894a61cd66SEric Lapuyade 
2904a61cd66SEric Lapuyade 	pr_debug("ns reset to %d\n", shdlc->dnr);
2914a61cd66SEric Lapuyade 
2924a61cd66SEric Lapuyade 	while ((skb = skb_dequeue_tail(&shdlc->ack_pending_q))) {
2934a61cd66SEric Lapuyade 		skb_pull(skb, 1);	/* remove control field */
2944a61cd66SEric Lapuyade 		skb_queue_head(&shdlc->send_q, skb);
2954a61cd66SEric Lapuyade 	}
2964a61cd66SEric Lapuyade 	shdlc->ns = shdlc->dnr;
2974a61cd66SEric Lapuyade }
2984a61cd66SEric Lapuyade 
2994a61cd66SEric Lapuyade static void llc_shdlc_rcv_rej(struct llc_shdlc *shdlc, int y_nr)
3004a61cd66SEric Lapuyade {
3014a61cd66SEric Lapuyade 	struct sk_buff *skb;
3024a61cd66SEric Lapuyade 
3034a61cd66SEric Lapuyade 	pr_debug("remote asks retransmition from frame %d\n", y_nr);
3044a61cd66SEric Lapuyade 
3054a61cd66SEric Lapuyade 	if (llc_shdlc_x_lteq_y_lt_z(shdlc->dnr, y_nr, shdlc->ns)) {
3064a61cd66SEric Lapuyade 		if (shdlc->t2_active) {
3074a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t2_timer);
3084a61cd66SEric Lapuyade 			shdlc->t2_active = false;
3094a61cd66SEric Lapuyade 			pr_debug("Stopped T2(retransmit)\n");
3104a61cd66SEric Lapuyade 		}
3114a61cd66SEric Lapuyade 
3124a61cd66SEric Lapuyade 		if (shdlc->dnr != y_nr) {
3134a61cd66SEric Lapuyade 			while ((shdlc->dnr = ((shdlc->dnr + 1) % 8)) != y_nr) {
3144a61cd66SEric Lapuyade 				skb = skb_dequeue(&shdlc->ack_pending_q);
3154a61cd66SEric Lapuyade 				kfree_skb(skb);
3164a61cd66SEric Lapuyade 			}
3174a61cd66SEric Lapuyade 		}
3184a61cd66SEric Lapuyade 
3194a61cd66SEric Lapuyade 		llc_shdlc_requeue_ack_pending(shdlc);
3204a61cd66SEric Lapuyade 	}
3214a61cd66SEric Lapuyade }
3224a61cd66SEric Lapuyade 
3234a61cd66SEric Lapuyade /* See spec RR:10.8.3 REJ:10.8.4 */
3244a61cd66SEric Lapuyade static void llc_shdlc_rcv_s_frame(struct llc_shdlc *shdlc,
3254a61cd66SEric Lapuyade 				  enum sframe_type s_frame_type, int nr)
3264a61cd66SEric Lapuyade {
3274a61cd66SEric Lapuyade 	struct sk_buff *skb;
3284a61cd66SEric Lapuyade 
3294a61cd66SEric Lapuyade 	if (shdlc->state != SHDLC_CONNECTED)
3304a61cd66SEric Lapuyade 		return;
3314a61cd66SEric Lapuyade 
3324a61cd66SEric Lapuyade 	switch (s_frame_type) {
3334a61cd66SEric Lapuyade 	case S_FRAME_RR:
3344a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3354a61cd66SEric Lapuyade 		if (shdlc->rnr == true) {	/* see SHDLC 10.7.7 */
3364a61cd66SEric Lapuyade 			shdlc->rnr = false;
3374a61cd66SEric Lapuyade 			if (shdlc->send_q.qlen == 0) {
3384a61cd66SEric Lapuyade 				skb = llc_shdlc_alloc_skb(shdlc, 0);
3394a61cd66SEric Lapuyade 				if (skb)
3404a61cd66SEric Lapuyade 					skb_queue_tail(&shdlc->send_q, skb);
3414a61cd66SEric Lapuyade 			}
3424a61cd66SEric Lapuyade 		}
3434a61cd66SEric Lapuyade 		break;
3444a61cd66SEric Lapuyade 	case S_FRAME_REJ:
3454a61cd66SEric Lapuyade 		llc_shdlc_rcv_rej(shdlc, nr);
3464a61cd66SEric Lapuyade 		break;
3474a61cd66SEric Lapuyade 	case S_FRAME_RNR:
3484a61cd66SEric Lapuyade 		llc_shdlc_rcv_ack(shdlc, nr);
3494a61cd66SEric Lapuyade 		shdlc->rnr = true;
3504a61cd66SEric Lapuyade 		break;
3514a61cd66SEric Lapuyade 	default:
3524a61cd66SEric Lapuyade 		break;
3534a61cd66SEric Lapuyade 	}
3544a61cd66SEric Lapuyade }
3554a61cd66SEric Lapuyade 
3564a61cd66SEric Lapuyade static void llc_shdlc_connect_complete(struct llc_shdlc *shdlc, int r)
3574a61cd66SEric Lapuyade {
3584a61cd66SEric Lapuyade 	pr_debug("result=%d\n", r);
3594a61cd66SEric Lapuyade 
3604a61cd66SEric Lapuyade 	del_timer_sync(&shdlc->connect_timer);
3614a61cd66SEric Lapuyade 
3624a61cd66SEric Lapuyade 	if (r == 0) {
3634a61cd66SEric Lapuyade 		shdlc->ns = 0;
3644a61cd66SEric Lapuyade 		shdlc->nr = 0;
3654a61cd66SEric Lapuyade 		shdlc->dnr = 0;
3664a61cd66SEric Lapuyade 
36712bfd1e8SWaldemar Rymarkiewicz 		shdlc->state = SHDLC_HALF_CONNECTED;
3684a61cd66SEric Lapuyade 	} else {
3694a61cd66SEric Lapuyade 		shdlc->state = SHDLC_DISCONNECTED;
3704a61cd66SEric Lapuyade 	}
3714a61cd66SEric Lapuyade 
3724a61cd66SEric Lapuyade 	shdlc->connect_result = r;
3734a61cd66SEric Lapuyade 
3744a61cd66SEric Lapuyade 	wake_up(shdlc->connect_wq);
3754a61cd66SEric Lapuyade }
3764a61cd66SEric Lapuyade 
3774a61cd66SEric Lapuyade static int llc_shdlc_connect_initiate(struct llc_shdlc *shdlc)
3784a61cd66SEric Lapuyade {
3794a61cd66SEric Lapuyade 	struct sk_buff *skb;
3804a61cd66SEric Lapuyade 
3814a61cd66SEric Lapuyade 	pr_debug("\n");
3824a61cd66SEric Lapuyade 
3834a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 2);
3844a61cd66SEric Lapuyade 	if (skb == NULL)
3854a61cd66SEric Lapuyade 		return -ENOMEM;
3864a61cd66SEric Lapuyade 
3874a61cd66SEric Lapuyade 	*skb_put(skb, 1) = SHDLC_MAX_WINDOW;
3884a61cd66SEric Lapuyade 	*skb_put(skb, 1) = SHDLC_SREJ_SUPPORT ? 1 : 0;
3894a61cd66SEric Lapuyade 
3904a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_RSET);
3914a61cd66SEric Lapuyade }
3924a61cd66SEric Lapuyade 
3934a61cd66SEric Lapuyade static int llc_shdlc_connect_send_ua(struct llc_shdlc *shdlc)
3944a61cd66SEric Lapuyade {
3954a61cd66SEric Lapuyade 	struct sk_buff *skb;
3964a61cd66SEric Lapuyade 
3974a61cd66SEric Lapuyade 	pr_debug("\n");
3984a61cd66SEric Lapuyade 
3994a61cd66SEric Lapuyade 	skb = llc_shdlc_alloc_skb(shdlc, 0);
4004a61cd66SEric Lapuyade 	if (skb == NULL)
4014a61cd66SEric Lapuyade 		return -ENOMEM;
4024a61cd66SEric Lapuyade 
4034a61cd66SEric Lapuyade 	return llc_shdlc_send_u_frame(shdlc, skb, U_FRAME_UA);
4044a61cd66SEric Lapuyade }
4054a61cd66SEric Lapuyade 
4064a61cd66SEric Lapuyade static void llc_shdlc_rcv_u_frame(struct llc_shdlc *shdlc,
4074a61cd66SEric Lapuyade 				  struct sk_buff *skb,
4084a61cd66SEric Lapuyade 				  enum uframe_modifier u_frame_modifier)
4094a61cd66SEric Lapuyade {
4104a61cd66SEric Lapuyade 	u8 w = SHDLC_MAX_WINDOW;
4114a61cd66SEric Lapuyade 	bool srej_support = SHDLC_SREJ_SUPPORT;
4124a61cd66SEric Lapuyade 	int r;
4134a61cd66SEric Lapuyade 
4144a61cd66SEric Lapuyade 	pr_debug("u_frame_modifier=%d\n", u_frame_modifier);
4154a61cd66SEric Lapuyade 
4164a61cd66SEric Lapuyade 	switch (u_frame_modifier) {
4174a61cd66SEric Lapuyade 	case U_FRAME_RSET:
41812bfd1e8SWaldemar Rymarkiewicz 		switch (shdlc->state) {
4194c0ba9acSWaldemar Rymarkiewicz 		case SHDLC_NEGOTIATING:
42012bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTING:
42112bfd1e8SWaldemar Rymarkiewicz 			/*
42212bfd1e8SWaldemar Rymarkiewicz 			 * We sent RSET, but chip wants to negociate or we
42312bfd1e8SWaldemar Rymarkiewicz 			 * got RSET before we managed to send out our.
42412bfd1e8SWaldemar Rymarkiewicz 			 */
4254a61cd66SEric Lapuyade 			if (skb->len > 0)
4264a61cd66SEric Lapuyade 				w = skb->data[0];
4274a61cd66SEric Lapuyade 
4284a61cd66SEric Lapuyade 			if (skb->len > 1)
4294a61cd66SEric Lapuyade 				srej_support = skb->data[1] & 0x01 ? true :
4304a61cd66SEric Lapuyade 					       false;
4314a61cd66SEric Lapuyade 
4324a61cd66SEric Lapuyade 			if ((w <= SHDLC_MAX_WINDOW) &&
4334a61cd66SEric Lapuyade 			    (SHDLC_SREJ_SUPPORT || (srej_support == false))) {
4344a61cd66SEric Lapuyade 				shdlc->w = w;
4354a61cd66SEric Lapuyade 				shdlc->srej_support = srej_support;
4364a61cd66SEric Lapuyade 				r = llc_shdlc_connect_send_ua(shdlc);
4374a61cd66SEric Lapuyade 				llc_shdlc_connect_complete(shdlc, r);
4384a61cd66SEric Lapuyade 			}
43912bfd1e8SWaldemar Rymarkiewicz 			break;
44012bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_HALF_CONNECTED:
44112bfd1e8SWaldemar Rymarkiewicz 			/*
44212bfd1e8SWaldemar Rymarkiewicz 			 * Chip resent RSET due to its timeout - Ignote it
44312bfd1e8SWaldemar Rymarkiewicz 			 * as we already sent UA.
44412bfd1e8SWaldemar Rymarkiewicz 			 */
44512bfd1e8SWaldemar Rymarkiewicz 			break;
44612bfd1e8SWaldemar Rymarkiewicz 		case SHDLC_CONNECTED:
4474a61cd66SEric Lapuyade 			/*
4484a61cd66SEric Lapuyade 			 * Chip wants to reset link. This is unexpected and
4494a61cd66SEric Lapuyade 			 * unsupported.
4504a61cd66SEric Lapuyade 			 */
4514a61cd66SEric Lapuyade 			shdlc->hard_fault = -ECONNRESET;
45212bfd1e8SWaldemar Rymarkiewicz 			break;
45312bfd1e8SWaldemar Rymarkiewicz 		default:
45412bfd1e8SWaldemar Rymarkiewicz 			break;
4554a61cd66SEric Lapuyade 		}
4564a61cd66SEric Lapuyade 		break;
4574a61cd66SEric Lapuyade 	case U_FRAME_UA:
4584a61cd66SEric Lapuyade 		if ((shdlc->state == SHDLC_CONNECTING &&
4594a61cd66SEric Lapuyade 		     shdlc->connect_tries > 0) ||
4604c0ba9acSWaldemar Rymarkiewicz 		    (shdlc->state == SHDLC_NEGOTIATING)) {
4614a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, 0);
46212bfd1e8SWaldemar Rymarkiewicz 			shdlc->state = SHDLC_CONNECTED;
46312bfd1e8SWaldemar Rymarkiewicz 		}
4644a61cd66SEric Lapuyade 		break;
4654a61cd66SEric Lapuyade 	default:
4664a61cd66SEric Lapuyade 		break;
4674a61cd66SEric Lapuyade 	}
4684a61cd66SEric Lapuyade 
4694a61cd66SEric Lapuyade 	kfree_skb(skb);
4704a61cd66SEric Lapuyade }
4714a61cd66SEric Lapuyade 
4724a61cd66SEric Lapuyade static void llc_shdlc_handle_rcv_queue(struct llc_shdlc *shdlc)
4734a61cd66SEric Lapuyade {
4744a61cd66SEric Lapuyade 	struct sk_buff *skb;
4754a61cd66SEric Lapuyade 	u8 control;
4764a61cd66SEric Lapuyade 	int nr;
4774a61cd66SEric Lapuyade 	int ns;
4784a61cd66SEric Lapuyade 	enum sframe_type s_frame_type;
4794a61cd66SEric Lapuyade 	enum uframe_modifier u_frame_modifier;
4804a61cd66SEric Lapuyade 
4814a61cd66SEric Lapuyade 	if (shdlc->rcv_q.qlen)
4824a61cd66SEric Lapuyade 		pr_debug("rcvQlen=%d\n", shdlc->rcv_q.qlen);
4834a61cd66SEric Lapuyade 
4844a61cd66SEric Lapuyade 	while ((skb = skb_dequeue(&shdlc->rcv_q)) != NULL) {
4854a61cd66SEric Lapuyade 		control = skb->data[0];
4864a61cd66SEric Lapuyade 		skb_pull(skb, 1);
4874a61cd66SEric Lapuyade 		switch (control & SHDLC_CONTROL_HEAD_MASK) {
4884a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I:
4894a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_I2:
49012bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
49112bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
49212bfd1e8SWaldemar Rymarkiewicz 
4934a61cd66SEric Lapuyade 			ns = (control & SHDLC_CONTROL_NS_MASK) >> 3;
4944a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
4954a61cd66SEric Lapuyade 			llc_shdlc_rcv_i_frame(shdlc, skb, ns, nr);
4964a61cd66SEric Lapuyade 			break;
4974a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_S:
49812bfd1e8SWaldemar Rymarkiewicz 			if (shdlc->state == SHDLC_HALF_CONNECTED)
49912bfd1e8SWaldemar Rymarkiewicz 				shdlc->state = SHDLC_CONNECTED;
50012bfd1e8SWaldemar Rymarkiewicz 
5014a61cd66SEric Lapuyade 			s_frame_type = (control & SHDLC_CONTROL_TYPE_MASK) >> 3;
5024a61cd66SEric Lapuyade 			nr = control & SHDLC_CONTROL_NR_MASK;
5034a61cd66SEric Lapuyade 			llc_shdlc_rcv_s_frame(shdlc, s_frame_type, nr);
5044a61cd66SEric Lapuyade 			kfree_skb(skb);
5054a61cd66SEric Lapuyade 			break;
5064a61cd66SEric Lapuyade 		case SHDLC_CONTROL_HEAD_U:
5074a61cd66SEric Lapuyade 			u_frame_modifier = control & SHDLC_CONTROL_M_MASK;
5084a61cd66SEric Lapuyade 			llc_shdlc_rcv_u_frame(shdlc, skb, u_frame_modifier);
5094a61cd66SEric Lapuyade 			break;
5104a61cd66SEric Lapuyade 		default:
5114a61cd66SEric Lapuyade 			pr_err("UNKNOWN Control=%d\n", control);
5124a61cd66SEric Lapuyade 			kfree_skb(skb);
5134a61cd66SEric Lapuyade 			break;
5144a61cd66SEric Lapuyade 		}
5154a61cd66SEric Lapuyade 	}
5164a61cd66SEric Lapuyade }
5174a61cd66SEric Lapuyade 
5184a61cd66SEric Lapuyade static int llc_shdlc_w_used(int ns, int dnr)
5194a61cd66SEric Lapuyade {
5204a61cd66SEric Lapuyade 	int unack_count;
5214a61cd66SEric Lapuyade 
5224a61cd66SEric Lapuyade 	if (dnr <= ns)
5234a61cd66SEric Lapuyade 		unack_count = ns - dnr;
5244a61cd66SEric Lapuyade 	else
5254a61cd66SEric Lapuyade 		unack_count = 8 - dnr + ns;
5264a61cd66SEric Lapuyade 
5274a61cd66SEric Lapuyade 	return unack_count;
5284a61cd66SEric Lapuyade }
5294a61cd66SEric Lapuyade 
5304a61cd66SEric Lapuyade /* Send frames according to algorithm at spec:10.8.1 */
5314a61cd66SEric Lapuyade static void llc_shdlc_handle_send_queue(struct llc_shdlc *shdlc)
5324a61cd66SEric Lapuyade {
5334a61cd66SEric Lapuyade 	struct sk_buff *skb;
5344a61cd66SEric Lapuyade 	int r;
5354a61cd66SEric Lapuyade 	unsigned long time_sent;
5364a61cd66SEric Lapuyade 
5374a61cd66SEric Lapuyade 	if (shdlc->send_q.qlen)
5384a61cd66SEric Lapuyade 		pr_debug
5394a61cd66SEric Lapuyade 		    ("sendQlen=%d ns=%d dnr=%d rnr=%s w_room=%d unackQlen=%d\n",
5404a61cd66SEric Lapuyade 		     shdlc->send_q.qlen, shdlc->ns, shdlc->dnr,
5414a61cd66SEric Lapuyade 		     shdlc->rnr == false ? "false" : "true",
5424a61cd66SEric Lapuyade 		     shdlc->w - llc_shdlc_w_used(shdlc->ns, shdlc->dnr),
5434a61cd66SEric Lapuyade 		     shdlc->ack_pending_q.qlen);
5444a61cd66SEric Lapuyade 
5454a61cd66SEric Lapuyade 	while (shdlc->send_q.qlen && shdlc->ack_pending_q.qlen < shdlc->w &&
5464a61cd66SEric Lapuyade 	       (shdlc->rnr == false)) {
5474a61cd66SEric Lapuyade 
5484a61cd66SEric Lapuyade 		if (shdlc->t1_active) {
5494a61cd66SEric Lapuyade 			del_timer_sync(&shdlc->t1_timer);
5504a61cd66SEric Lapuyade 			shdlc->t1_active = false;
5514a61cd66SEric Lapuyade 			pr_debug("Stopped T1(send ack)\n");
5524a61cd66SEric Lapuyade 		}
5534a61cd66SEric Lapuyade 
5544a61cd66SEric Lapuyade 		skb = skb_dequeue(&shdlc->send_q);
5554a61cd66SEric Lapuyade 
5564a61cd66SEric Lapuyade 		*skb_push(skb, 1) = SHDLC_CONTROL_HEAD_I | (shdlc->ns << 3) |
5574a61cd66SEric Lapuyade 				    shdlc->nr;
5584a61cd66SEric Lapuyade 
5594a61cd66SEric Lapuyade 		pr_debug("Sending I-Frame %d, waiting to rcv %d\n", shdlc->ns,
5604a61cd66SEric Lapuyade 			 shdlc->nr);
561412fda53SEric Lapuyade 		SHDLC_DUMP_SKB("shdlc frame written", skb);
5624a61cd66SEric Lapuyade 
5634a61cd66SEric Lapuyade 		r = shdlc->xmit_to_drv(shdlc->hdev, skb);
5644a61cd66SEric Lapuyade 		if (r < 0) {
5654a61cd66SEric Lapuyade 			shdlc->hard_fault = r;
5664a61cd66SEric Lapuyade 			break;
5674a61cd66SEric Lapuyade 		}
5684a61cd66SEric Lapuyade 
5694a61cd66SEric Lapuyade 		shdlc->ns = (shdlc->ns + 1) % 8;
5704a61cd66SEric Lapuyade 
5714a61cd66SEric Lapuyade 		time_sent = jiffies;
5724a61cd66SEric Lapuyade 		*(unsigned long *)skb->cb = time_sent;
5734a61cd66SEric Lapuyade 
5744a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->ack_pending_q, skb);
5754a61cd66SEric Lapuyade 
5764a61cd66SEric Lapuyade 		if (shdlc->t2_active == false) {
5774a61cd66SEric Lapuyade 			shdlc->t2_active = true;
5784a61cd66SEric Lapuyade 			mod_timer(&shdlc->t2_timer, time_sent +
5794a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_T2_VALUE_MS));
5804a61cd66SEric Lapuyade 			pr_debug("Started T2 (retransmit)\n");
5814a61cd66SEric Lapuyade 		}
5824a61cd66SEric Lapuyade 	}
5834a61cd66SEric Lapuyade }
5844a61cd66SEric Lapuyade 
5854a61cd66SEric Lapuyade static void llc_shdlc_connect_timeout(unsigned long data)
5864a61cd66SEric Lapuyade {
5874a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
5884a61cd66SEric Lapuyade 
5894a61cd66SEric Lapuyade 	pr_debug("\n");
5904a61cd66SEric Lapuyade 
591*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
5924a61cd66SEric Lapuyade }
5934a61cd66SEric Lapuyade 
5944a61cd66SEric Lapuyade static void llc_shdlc_t1_timeout(unsigned long data)
5954a61cd66SEric Lapuyade {
5964a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
5974a61cd66SEric Lapuyade 
5984a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to send ack\n");
5994a61cd66SEric Lapuyade 
600*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
6014a61cd66SEric Lapuyade }
6024a61cd66SEric Lapuyade 
6034a61cd66SEric Lapuyade static void llc_shdlc_t2_timeout(unsigned long data)
6044a61cd66SEric Lapuyade {
6054a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = (struct llc_shdlc *)data;
6064a61cd66SEric Lapuyade 
6074a61cd66SEric Lapuyade 	pr_debug("SoftIRQ: need to retransmit\n");
6084a61cd66SEric Lapuyade 
609*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
6104a61cd66SEric Lapuyade }
6114a61cd66SEric Lapuyade 
6124a61cd66SEric Lapuyade static void llc_shdlc_sm_work(struct work_struct *work)
6134a61cd66SEric Lapuyade {
6144a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = container_of(work, struct llc_shdlc, sm_work);
6154a61cd66SEric Lapuyade 	int r;
6164a61cd66SEric Lapuyade 
6174a61cd66SEric Lapuyade 	pr_debug("\n");
6184a61cd66SEric Lapuyade 
6194a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
6204a61cd66SEric Lapuyade 
6214a61cd66SEric Lapuyade 	switch (shdlc->state) {
6224a61cd66SEric Lapuyade 	case SHDLC_DISCONNECTED:
6234a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->rcv_q);
6244a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->send_q);
6254a61cd66SEric Lapuyade 		skb_queue_purge(&shdlc->ack_pending_q);
6264a61cd66SEric Lapuyade 		break;
6274a61cd66SEric Lapuyade 	case SHDLC_CONNECTING:
6284a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6294a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6304a61cd66SEric Lapuyade 			break;
6314a61cd66SEric Lapuyade 		}
6324a61cd66SEric Lapuyade 
6334a61cd66SEric Lapuyade 		if (shdlc->connect_tries++ < 5)
6344a61cd66SEric Lapuyade 			r = llc_shdlc_connect_initiate(shdlc);
6354a61cd66SEric Lapuyade 		else
6364a61cd66SEric Lapuyade 			r = -ETIME;
6374a61cd66SEric Lapuyade 		if (r < 0)
6384a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, r);
6394a61cd66SEric Lapuyade 		else {
6404a61cd66SEric Lapuyade 			mod_timer(&shdlc->connect_timer, jiffies +
6414a61cd66SEric Lapuyade 				  msecs_to_jiffies(SHDLC_CONNECT_VALUE_MS));
6424a61cd66SEric Lapuyade 
6434c0ba9acSWaldemar Rymarkiewicz 			shdlc->state = SHDLC_NEGOTIATING;
6444a61cd66SEric Lapuyade 		}
6454a61cd66SEric Lapuyade 		break;
6464c0ba9acSWaldemar Rymarkiewicz 	case SHDLC_NEGOTIATING:
6474a61cd66SEric Lapuyade 		if (timer_pending(&shdlc->connect_timer) == 0) {
6484a61cd66SEric Lapuyade 			shdlc->state = SHDLC_CONNECTING;
649*916082b0SLinus Torvalds 			schedule_work(&shdlc->sm_work);
6504a61cd66SEric Lapuyade 		}
6514a61cd66SEric Lapuyade 
6524a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6534a61cd66SEric Lapuyade 
6544a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6554a61cd66SEric Lapuyade 			llc_shdlc_connect_complete(shdlc, shdlc->hard_fault);
6564a61cd66SEric Lapuyade 			break;
6574a61cd66SEric Lapuyade 		}
6584a61cd66SEric Lapuyade 		break;
65912bfd1e8SWaldemar Rymarkiewicz 	case SHDLC_HALF_CONNECTED:
6604a61cd66SEric Lapuyade 	case SHDLC_CONNECTED:
6614a61cd66SEric Lapuyade 		llc_shdlc_handle_rcv_queue(shdlc);
6624a61cd66SEric Lapuyade 		llc_shdlc_handle_send_queue(shdlc);
6634a61cd66SEric Lapuyade 
6644a61cd66SEric Lapuyade 		if (shdlc->t1_active && timer_pending(&shdlc->t1_timer) == 0) {
6654a61cd66SEric Lapuyade 			pr_debug
6664a61cd66SEric Lapuyade 			    ("Handle T1(send ack) elapsed (T1 now inactive)\n");
6674a61cd66SEric Lapuyade 
6684a61cd66SEric Lapuyade 			shdlc->t1_active = false;
6694a61cd66SEric Lapuyade 			r = llc_shdlc_send_s_frame(shdlc, S_FRAME_RR,
6704a61cd66SEric Lapuyade 						   shdlc->nr);
6714a61cd66SEric Lapuyade 			if (r < 0)
6724a61cd66SEric Lapuyade 				shdlc->hard_fault = r;
6734a61cd66SEric Lapuyade 		}
6744a61cd66SEric Lapuyade 
6754a61cd66SEric Lapuyade 		if (shdlc->t2_active && timer_pending(&shdlc->t2_timer) == 0) {
6764a61cd66SEric Lapuyade 			pr_debug
6774a61cd66SEric Lapuyade 			    ("Handle T2(retransmit) elapsed (T2 inactive)\n");
6784a61cd66SEric Lapuyade 
6794a61cd66SEric Lapuyade 			shdlc->t2_active = false;
6804a61cd66SEric Lapuyade 
6814a61cd66SEric Lapuyade 			llc_shdlc_requeue_ack_pending(shdlc);
6824a61cd66SEric Lapuyade 			llc_shdlc_handle_send_queue(shdlc);
6834a61cd66SEric Lapuyade 		}
6844a61cd66SEric Lapuyade 
6854a61cd66SEric Lapuyade 		if (shdlc->hard_fault) {
6864a61cd66SEric Lapuyade 			shdlc->llc_failure(shdlc->hdev, shdlc->hard_fault);
6874a61cd66SEric Lapuyade 		}
6884a61cd66SEric Lapuyade 		break;
6894a61cd66SEric Lapuyade 	default:
6904a61cd66SEric Lapuyade 		break;
6914a61cd66SEric Lapuyade 	}
6924a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
6934a61cd66SEric Lapuyade }
6944a61cd66SEric Lapuyade 
6954a61cd66SEric Lapuyade /*
6964a61cd66SEric Lapuyade  * Called from syscall context to establish shdlc link. Sleeps until
6974a61cd66SEric Lapuyade  * link is ready or failure.
6984a61cd66SEric Lapuyade  */
6994a61cd66SEric Lapuyade static int llc_shdlc_connect(struct llc_shdlc *shdlc)
7004a61cd66SEric Lapuyade {
7014a61cd66SEric Lapuyade 	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(connect_wq);
7024a61cd66SEric Lapuyade 
7034a61cd66SEric Lapuyade 	pr_debug("\n");
7044a61cd66SEric Lapuyade 
7054a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
7064a61cd66SEric Lapuyade 
7074a61cd66SEric Lapuyade 	shdlc->state = SHDLC_CONNECTING;
7084a61cd66SEric Lapuyade 	shdlc->connect_wq = &connect_wq;
7094a61cd66SEric Lapuyade 	shdlc->connect_tries = 0;
7104a61cd66SEric Lapuyade 	shdlc->connect_result = 1;
7114a61cd66SEric Lapuyade 
7124a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
7134a61cd66SEric Lapuyade 
714*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7154a61cd66SEric Lapuyade 
7164a61cd66SEric Lapuyade 	wait_event(connect_wq, shdlc->connect_result != 1);
7174a61cd66SEric Lapuyade 
7184a61cd66SEric Lapuyade 	return shdlc->connect_result;
7194a61cd66SEric Lapuyade }
7204a61cd66SEric Lapuyade 
7214a61cd66SEric Lapuyade static void llc_shdlc_disconnect(struct llc_shdlc *shdlc)
7224a61cd66SEric Lapuyade {
7234a61cd66SEric Lapuyade 	pr_debug("\n");
7244a61cd66SEric Lapuyade 
7254a61cd66SEric Lapuyade 	mutex_lock(&shdlc->state_mutex);
7264a61cd66SEric Lapuyade 
7274a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
7284a61cd66SEric Lapuyade 
7294a61cd66SEric Lapuyade 	mutex_unlock(&shdlc->state_mutex);
7304a61cd66SEric Lapuyade 
731*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7324a61cd66SEric Lapuyade }
7334a61cd66SEric Lapuyade 
7344a61cd66SEric Lapuyade /*
7354a61cd66SEric Lapuyade  * Receive an incoming shdlc frame. Frame has already been crc-validated.
7364a61cd66SEric Lapuyade  * skb contains only LLC header and payload.
7374a61cd66SEric Lapuyade  * If skb == NULL, it is a notification that the link below is dead.
7384a61cd66SEric Lapuyade  */
7394a61cd66SEric Lapuyade static void llc_shdlc_recv_frame(struct llc_shdlc *shdlc, struct sk_buff *skb)
7404a61cd66SEric Lapuyade {
7414a61cd66SEric Lapuyade 	if (skb == NULL) {
7424a61cd66SEric Lapuyade 		pr_err("NULL Frame -> link is dead\n");
7434a61cd66SEric Lapuyade 		shdlc->hard_fault = -EREMOTEIO;
7444a61cd66SEric Lapuyade 	} else {
7454a61cd66SEric Lapuyade 		SHDLC_DUMP_SKB("incoming frame", skb);
7464a61cd66SEric Lapuyade 		skb_queue_tail(&shdlc->rcv_q, skb);
7474a61cd66SEric Lapuyade 	}
7484a61cd66SEric Lapuyade 
749*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
7504a61cd66SEric Lapuyade }
7514a61cd66SEric Lapuyade 
7524a61cd66SEric Lapuyade static void *llc_shdlc_init(struct nfc_hci_dev *hdev, xmit_to_drv_t xmit_to_drv,
7534a61cd66SEric Lapuyade 			    rcv_to_hci_t rcv_to_hci, int tx_headroom,
7544a61cd66SEric Lapuyade 			    int tx_tailroom, int *rx_headroom, int *rx_tailroom,
7554a61cd66SEric Lapuyade 			    llc_failure_t llc_failure)
7564a61cd66SEric Lapuyade {
7574a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc;
7584a61cd66SEric Lapuyade 
7594a61cd66SEric Lapuyade 	*rx_headroom = SHDLC_LLC_HEAD_ROOM;
7604a61cd66SEric Lapuyade 	*rx_tailroom = 0;
7614a61cd66SEric Lapuyade 
7624a61cd66SEric Lapuyade 	shdlc = kzalloc(sizeof(struct llc_shdlc), GFP_KERNEL);
7634a61cd66SEric Lapuyade 	if (shdlc == NULL)
7644a61cd66SEric Lapuyade 		return NULL;
7654a61cd66SEric Lapuyade 
7664a61cd66SEric Lapuyade 	mutex_init(&shdlc->state_mutex);
7674a61cd66SEric Lapuyade 	shdlc->state = SHDLC_DISCONNECTED;
7684a61cd66SEric Lapuyade 
7694a61cd66SEric Lapuyade 	init_timer(&shdlc->connect_timer);
7704a61cd66SEric Lapuyade 	shdlc->connect_timer.data = (unsigned long)shdlc;
7714a61cd66SEric Lapuyade 	shdlc->connect_timer.function = llc_shdlc_connect_timeout;
7724a61cd66SEric Lapuyade 
7734a61cd66SEric Lapuyade 	init_timer(&shdlc->t1_timer);
7744a61cd66SEric Lapuyade 	shdlc->t1_timer.data = (unsigned long)shdlc;
7754a61cd66SEric Lapuyade 	shdlc->t1_timer.function = llc_shdlc_t1_timeout;
7764a61cd66SEric Lapuyade 
7774a61cd66SEric Lapuyade 	init_timer(&shdlc->t2_timer);
7784a61cd66SEric Lapuyade 	shdlc->t2_timer.data = (unsigned long)shdlc;
7794a61cd66SEric Lapuyade 	shdlc->t2_timer.function = llc_shdlc_t2_timeout;
7804a61cd66SEric Lapuyade 
7814a61cd66SEric Lapuyade 	shdlc->w = SHDLC_MAX_WINDOW;
7824a61cd66SEric Lapuyade 	shdlc->srej_support = SHDLC_SREJ_SUPPORT;
7834a61cd66SEric Lapuyade 
7844a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->rcv_q);
7854a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->send_q);
7864a61cd66SEric Lapuyade 	skb_queue_head_init(&shdlc->ack_pending_q);
7874a61cd66SEric Lapuyade 
7884a61cd66SEric Lapuyade 	INIT_WORK(&shdlc->sm_work, llc_shdlc_sm_work);
7894a61cd66SEric Lapuyade 
7904a61cd66SEric Lapuyade 	shdlc->hdev = hdev;
7914a61cd66SEric Lapuyade 	shdlc->xmit_to_drv = xmit_to_drv;
7924a61cd66SEric Lapuyade 	shdlc->rcv_to_hci = rcv_to_hci;
7934a61cd66SEric Lapuyade 	shdlc->tx_headroom = tx_headroom;
7944a61cd66SEric Lapuyade 	shdlc->tx_tailroom = tx_tailroom;
7954a61cd66SEric Lapuyade 	shdlc->llc_failure = llc_failure;
7964a61cd66SEric Lapuyade 
7974a61cd66SEric Lapuyade 	return shdlc;
7984a61cd66SEric Lapuyade }
7994a61cd66SEric Lapuyade 
8004a61cd66SEric Lapuyade static void llc_shdlc_deinit(struct nfc_llc *llc)
8014a61cd66SEric Lapuyade {
8024a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8034a61cd66SEric Lapuyade 
8044a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->rcv_q);
8054a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->send_q);
8064a61cd66SEric Lapuyade 	skb_queue_purge(&shdlc->ack_pending_q);
8074a61cd66SEric Lapuyade 
8084a61cd66SEric Lapuyade 	kfree(shdlc);
8094a61cd66SEric Lapuyade }
8104a61cd66SEric Lapuyade 
8114a61cd66SEric Lapuyade static int llc_shdlc_start(struct nfc_llc *llc)
8124a61cd66SEric Lapuyade {
8134a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8144a61cd66SEric Lapuyade 
8154a61cd66SEric Lapuyade 	return llc_shdlc_connect(shdlc);
8164a61cd66SEric Lapuyade }
8174a61cd66SEric Lapuyade 
8184a61cd66SEric Lapuyade static int llc_shdlc_stop(struct nfc_llc *llc)
8194a61cd66SEric Lapuyade {
8204a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8214a61cd66SEric Lapuyade 
8224a61cd66SEric Lapuyade 	llc_shdlc_disconnect(shdlc);
8234a61cd66SEric Lapuyade 
8244a61cd66SEric Lapuyade 	return 0;
8254a61cd66SEric Lapuyade }
8264a61cd66SEric Lapuyade 
8274a61cd66SEric Lapuyade static void llc_shdlc_rcv_from_drv(struct nfc_llc *llc, struct sk_buff *skb)
8284a61cd66SEric Lapuyade {
8294a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8304a61cd66SEric Lapuyade 
8314a61cd66SEric Lapuyade 	llc_shdlc_recv_frame(shdlc, skb);
8324a61cd66SEric Lapuyade }
8334a61cd66SEric Lapuyade 
8344a61cd66SEric Lapuyade static int llc_shdlc_xmit_from_hci(struct nfc_llc *llc, struct sk_buff *skb)
8354a61cd66SEric Lapuyade {
8364a61cd66SEric Lapuyade 	struct llc_shdlc *shdlc = nfc_llc_get_data(llc);
8374a61cd66SEric Lapuyade 
8384a61cd66SEric Lapuyade 	skb_queue_tail(&shdlc->send_q, skb);
8394a61cd66SEric Lapuyade 
840*916082b0SLinus Torvalds 	schedule_work(&shdlc->sm_work);
8414a61cd66SEric Lapuyade 
8424a61cd66SEric Lapuyade 	return 0;
8434a61cd66SEric Lapuyade }
8444a61cd66SEric Lapuyade 
8454a61cd66SEric Lapuyade static struct nfc_llc_ops llc_shdlc_ops = {
8464a61cd66SEric Lapuyade 	.init = llc_shdlc_init,
8474a61cd66SEric Lapuyade 	.deinit = llc_shdlc_deinit,
8484a61cd66SEric Lapuyade 	.start = llc_shdlc_start,
8494a61cd66SEric Lapuyade 	.stop = llc_shdlc_stop,
8504a61cd66SEric Lapuyade 	.rcv_from_drv = llc_shdlc_rcv_from_drv,
8514a61cd66SEric Lapuyade 	.xmit_from_hci = llc_shdlc_xmit_from_hci,
8524a61cd66SEric Lapuyade };
8534a61cd66SEric Lapuyade 
854a7d0281bSEric Lapuyade int nfc_llc_shdlc_register(void)
8554a61cd66SEric Lapuyade {
8564a61cd66SEric Lapuyade 	return nfc_llc_register(LLC_SHDLC_NAME, &llc_shdlc_ops);
8574a61cd66SEric Lapuyade }
858