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