/*
* Copyright (C) 2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#define pr_fmt(fmt) "shdlc: %s: " fmt, __func__
#include <linux/sched.h>
#include <linux/export.h>
#include <linux/wait.h>
#include <linux/crc-ccitt.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <net/nfc/hci.h>
#include <net/nfc/shdlc.h>
#define SHDLC_LLC_HEAD_ROOM 2
#define SHDLC_LLC_TAIL_ROOM 2
#define SHDLC_MAX_WINDOW 4
#define SHDLC_SREJ_SUPPORT false
#define SHDLC_CONTROL_HEAD_MASK 0xe0
#define SHDLC_CONTROL_HEAD_I 0x80
#define SHDLC_CONTROL_HEAD_I2 0xa0
#define SHDLC_CONTROL_HEAD_S 0xc0
#define SHDLC_CONTROL_HEAD_U 0xe0
#define SHDLC_CONTROL_NS_MASK 0x38
#define SHDLC_CONTROL_NR_MASK 0x07
#define SHDLC_CONTROL_TYPE_MASK 0x18
#define SHDLC_CONTROL_M_MASK 0x1f
enum sframe_type {
S_FRAME_RR = 0x00,
S_FRAME_REJ = 0x01,
S_FRAME_RNR = 0x02,
S_FRAME_SREJ = 0x03
};
enum uframe_modifier {
U_FRAME_UA = 0x06,
U_FRAME_RSET = 0x19
};
#define SHDLC_CONNECT_VALUE_MS 5
#define SHDLC_T1_VALUE_MS(w) ((5 * w) / 4)
#define SHDLC_T2_VALUE_MS 300
#define SHDLC_DUMP_SKB(info, skb) \
do { \
pr_debug("%s:\n", info); \
print_hex_dump(KERN_DEBUG, "shdlc: ", DUMP_PREFIX_OFFSET, \
16, 1, skb->data, skb->len, 0); \
} while (0)
/* checks x < y <= z modulo 8 */
static bool nfc_shdlc_x_lt_y_lteq_z(int x, int y, int z)
{
if (x < z)
return ((x < y) && (y <= z)) ? true : false;
else
return ((y > x) || (y <= z)) ? true : false;
}
/* checks x <= y < z modulo 8 */
static bool nfc_shdlc_x_lteq_y_lt_z(int x, int y, int z)
{
if (x <= z)
return ((x <= y) && (y < z)) ? true : false;
else /* x > z -> z+8 > x */
return ((y >= x) || (y < z)) ? true : false;
}
static struct sk_buff *nfc_shdlc_alloc_skb(struct nfc_shdlc *shdlc,
int payload_len)
{
struct sk_buff *skb;
skb = alloc_skb(shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM +
shdlc->client_tailroom + SHDLC_LLC_TAIL_ROOM +
payload_len, GFP_KERNEL);
if (skb)
skb_reserve(skb, shdlc->client_headroom + SHDLC_LLC_HEAD_ROOM);
return skb;
}
static void nfc_shdlc_add_len_crc(struct sk_buff *skb)
{
u16 crc;
int len;
len = skb->len + 2;
*skb_push(skb, 1) = len;
crc = crc_ccitt(0xffff, skb->data, skb->len);
crc = ~crc;
*skb_put(skb, 1) = crc & 0xff;
*skb_put(skb, 1) = crc >> 8;
}
/* immediately sends an S frame. */
static int nfc_shdlc_send_s_frame(struct nfc_shdlc *shdlc,
enum sframe_type sframe_type, int nr)
{
int r;
struct sk_buff *skb;
pr_debug("sframe_type=%d nr=%d\n", sframe_type, nr);
skb = nfc_shdlc_alloc_skb(shdlc, 0);
if (skb == NULL)
return -ENOMEM;
*skb_push(skb, 1) = SHDLC_CONTROL_HEAD_S | (sframe_type << 3) | nr;
nfc_shdlc_add_len_crc(skb);
r = shdlc->ops->xmit(shdlc, skb);
kfree_skb(skb);
return r;
}
/* immediately sends an U frame. skb may contain optional payload */
static int nfc_shdlc_send_u_frame(struct nfc_shdlc *shdlc,
struct sk_buff *skb,
enum uframe_modifier uframe_modifier)
{
int r;
pr_debug("uframe_modifier=%d\n", uframe_modifier);
*skb_push(skb, 1) = SHDLC_CONTROL_HEAD_U | uframe_modifier;
nfc_shdlc_add_len_crc(skb);
r = shdlc->ops->xmit(shdlc, skb);
kfree_skb(skb);
return r;
}
/*
* Free ack_pending frames until y_nr - 1, and reset t2 according to
* the remaining oldest ack_pending frame sent time
*/
static void nfc_shdlc_reset_t2(struct nfc_shdlc *shdlc, int y_nr)
{
struct sk_buff *skb;
int dnr = shdlc->dnr; /* MUST initially be < y_nr */
pr_debug("release ack pending up to frame %d excluded\n", y_nr);
while (dnr != y_nr) {
pr_debug("release ack pending frame %d\n", dnr);
skb = skb_dequeue(&shdlc->ack_pending_q);
kfree_skb(skb);
dnr = (dnr + 1) % 8;
}
if (skb_queue_empty(&shdlc->ack_pending_q)) {
if (shdlc->