From patchwork Wed Dec 19 15:56:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ben Whitten X-Patchwork-Id: 10737549 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 55E9813B5 for ; Wed, 19 Dec 2018 15:57:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41B0B2B434 for ; Wed, 19 Dec 2018 15:57:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 308832B4BE; Wed, 19 Dec 2018 15:57:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 91F2D2B434 for ; Wed, 19 Dec 2018 15:57:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=7l41RTXaPJuoWu6fyCRzK1AHmEzQj8KDnicSTT65/YU=; b=G1MA4DRyjf4Xk0zT2RvU12X4Ll DX7eYmLMUx3Ui1nqDbSBW2rHi/8ha2MYbBa9wFaBup28capPxC4Z2qrAmWeozLhLxgeS64ga3YWav 4hQfxmlV8ianlZWq0awlDiiqty/eSFg9LVuXM3TohK2LzOYWS4JsJTxhRba5XLNqovFhhcg3GG4x+ 0aFSRsR5XMcEewaRQh+1+JSr3FymBto4cRUi0050TS94Wl8+E7CO0dJd9AFR3Ir4F8xV6+4x1VWhn WVcp1dufinrRUl3o9jKim7ZfSBTIiF9m5GpaQuK0EtanO7jdXKYlvK47tayr2WfSxk1G0a+C22U4a UyDkqyhQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gZeEB-0006Lp-CZ; Wed, 19 Dec 2018 15:57:43 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gZeDC-0005OK-NG; Wed, 19 Dec 2018 15:56:49 +0000 Received: by mail-wm1-x341.google.com with SMTP id r24so9856524wmh.0; Wed, 19 Dec 2018 07:56:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=Dx/xK0RPW2Lm4RTnTSdayPB1mXeHlkjPqDKxt/FlTYM=; b=uT3RP4hZ/DEf3FHnmnLfA0r3NF1GIV1bsBOVPUlc9/prQ9MSPHrlpI4O+/KnlZZaDx IP4kkm0Rd+OPfRycRd5UKqh+mTKCBaz0riQLbsCWStbcwqMjytVZaJf4RBCbHuagNovc chuD2iyqKld37WgcAzc2RzpowHlRjJ1n8RbpBWv828/Bv0fyKw+nCKbrOgmkLV5T66HU nMawDGBcUalrU1AT6rbLpfBU9j9JH5Hsv8wpN1TC3rVV91ruqroIQ1YAXUPdNPG4Bh/8 SGID7Rg3z8pMAJ4JxVEDJ/2vXy2rccCXn1NhHDQH1fdHWfKVTLBVBqhuQqacWvf+nB7V z72w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=Dx/xK0RPW2Lm4RTnTSdayPB1mXeHlkjPqDKxt/FlTYM=; b=KOc3l5Z8AdDF1TOrIHCbAdxCE2nyUMeaqVyIdb9F2AhnGQG3XN0HvZBYZkRFY41e+C l44WiYrEVbejEUNlC0TUgrcZJ2nBn0WQGylJGntMLvxDsdRC65+fmtCbo5wDRNJPICUb 5d11xGvgez39eKHalw1RaE2GeAfBdtRCVxcOfMdgujDar+sgE6mU90sjk4GRpSLV6RIJ u8KUbTkStoqg5ijCdOanZXd3DYXkKKNGVsrq0w8B0Rx8IOYLAHaJcAKaF4UkMF8jDKm5 hJc0U+6B8KhMzUbuNZaizHi7MmcJTq7KovSRYWgW+F1eoliCi/jO6OtIhzpvZKK0SG94 uXjw== X-Gm-Message-State: AA+aEWZKEj58vVWFGeLMrvaau6VseNnfTrOXKPjZIF3UnsGi+8+gB9Mp bG3+IxhsohzQnhdCoYIF8nY= X-Google-Smtp-Source: AFSGD/WzrHrGMbMsACXlzav1xUQl3C83IiETnFwMbF8GBgDUaeahhM8ywgLn4QaMgw+3VzWzGQBK6Q== X-Received: by 2002:a1c:b1d5:: with SMTP id a204mr8133515wmf.32.1545234990621; Wed, 19 Dec 2018 07:56:30 -0800 (PST) Received: from Sarah.corp.lairdtech.com ([109.174.151.67]) by smtp.gmail.com with ESMTPSA id c9sm6836099wmh.27.2018.12.19.07.56.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Dec 2018 07:56:30 -0800 (PST) From: Ben Whitten X-Google-Original-From: Ben Whitten To: starnight@g.ncu.edu.tw, jiri@resnulli.us, afaerber@suse.de Subject: [PATCH RFC lora-next 4/4] net: lora: sx1301: introduce a lora frame for packet metadata Date: Wed, 19 Dec 2018 15:56:16 +0000 Message-Id: <20181219155616.9547-5-ben.whitten@lairdtech.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181219155616.9547-1-ben.whitten@lairdtech.com> References: <20181219155616.9547-1-ben.whitten@lairdtech.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181219_075643_231364_6BCCE12E X-CRM114-Status: GOOD ( 17.13 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ben Whitten , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-lpwan@lists.infradead.org, "David S. Miller" , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Information such as spreading factor, coding rate and power are on a per transmission basis so it makes sence to include this in a header much like CAN frames do. Signed-off-by: Ben Whitten --- drivers/net/lora/dev.c | 2 - drivers/net/lora/sx1301.c | 79 +++++++++++++++++++++++++++++++++------ include/uapi/linux/lora.h | 46 +++++++++++++++++++++++ 3 files changed, 114 insertions(+), 13 deletions(-) diff --git a/drivers/net/lora/dev.c b/drivers/net/lora/dev.c index 0d4823de8c06..62dbe21668b0 100644 --- a/drivers/net/lora/dev.c +++ b/drivers/net/lora/dev.c @@ -11,8 +11,6 @@ #include #include -#define LORA_MTU 256 /* XXX */ - struct sk_buff *alloc_lora_skb(struct net_device *dev, u8 **data) { struct sk_buff *skb; diff --git a/drivers/net/lora/sx1301.c b/drivers/net/lora/sx1301.c index 9bcbb967f307..fed6d6201bf3 100644 --- a/drivers/net/lora/sx1301.c +++ b/drivers/net/lora/sx1301.c @@ -584,7 +584,7 @@ static int sx1301_load_tx_gain_lut(struct sx1301_priv *priv) return ret; }; -static int sx1301_tx(struct sx1301_priv *priv, void *data, int len) +static int sx1301_tx(struct sx1301_priv *priv, struct lora_frame *frame) { int ret, i; u8 buff[256 + 16]; @@ -617,11 +617,67 @@ static int sx1301_tx(struct sx1301_priv *priv, void *data, int len) hdr->tx_power = 15; /* HACK power entry 15 */ hdr->u.lora.crc16_en = 1; /* Enable CRC16 */ - hdr->u.lora.cr = 1; /* CR 4/5 */ - hdr->u.lora.sf = 7; /* SF7 */ - hdr->u.lora.payload_len = len; /* Set the data len to the skb len */ + + switch (frame->cr) { + case LORA_CR_4_5: + hdr->u.lora.cr = 1; /* CR 4/5 */ + break; + case LORA_CR_4_6: + hdr->u.lora.cr = 2; /* CR 4/6 */ + break; + case LORA_CR_4_7: + hdr->u.lora.cr = 3; /* CR 4/7 */ + break; + case LORA_CR_4_8: + hdr->u.lora.cr = 4; /* CR 4/8 */ + break; + default: + return -ENXIO; + } + + switch (frame->sf) { + case LORA_SF_6: + hdr->u.lora.sf = 6; /* SF6 */ + break; + case LORA_SF_7: + hdr->u.lora.sf = 7; /* SF7 */ + break; + case LORA_SF_8: + hdr->u.lora.sf = 8; /* SF8 */ + break; + case LORA_SF_9: + hdr->u.lora.sf = 9; /* SF9 */ + break; + case LORA_SF_10: + hdr->u.lora.sf = 10; /* SF10 */ + break; + case LORA_SF_11: + hdr->u.lora.sf = 11; /* SF11 */ + break; + case LORA_SF_12: + hdr->u.lora.sf = 12; /* SF12 */ + break; + default: + return -ENXIO; + } + + hdr->u.lora.payload_len = frame->len; /* Set the data length */ hdr->u.lora.implicit_header = 0; /* No implicit header */ - hdr->u.lora.mod_bw = 0; /* Set 125KHz BW */ + + switch (frame->bw) { + case LORA_BW_125KHZ: + hdr->u.lora.mod_bw = 0; /* 125KHz BW */ + break; + case LORA_BW_250KHZ: + hdr->u.lora.mod_bw = 1; /* 250KHz BW */ + break; + case LORA_BW_500KHZ: + hdr->u.lora.mod_bw = 2; /* 500KHz BW */ + break; + default: + return -ENXIO; + } + hdr->u.lora.ppm_offset = 0; /* TODO no ppm offset? */ hdr->u.lora.invert_pol = 0; /* TODO set no inverted polarity */ @@ -632,7 +688,7 @@ static int sx1301_tx(struct sx1301_priv *priv, void *data, int len) /* Copy the TX data into the buffer ready to go */ - memcpy((void *)&buff[16], data, len); + memcpy((void *)&buff[16], frame->data, frame->len); /* Reset any transmissions */ ret = regmap_write(priv->regmap, SX1301_TX_TRIG, 0); @@ -644,7 +700,7 @@ static int sx1301_tx(struct sx1301_priv *priv, void *data, int len) if (ret) return ret; ret = regmap_noinc_write(priv->regmap, SX1301_TX_DATA_BUF_DATA, buff, - len + 16); + frame->len + 16); if (ret) return ret; @@ -653,8 +709,8 @@ static int sx1301_tx(struct sx1301_priv *priv, void *data, int len) if (ret) return ret; - dev_dbg(priv->dev, "Transmitting packet of size %d: ", len); - for (i = 0; i < len + 16; i++) + dev_dbg(priv->dev, "Transmitting packet of size %d: ", frame->len); + for (i = 0; i < frame->len + 16; i++) dev_dbg(priv->dev, "%X", buff[i]); return ret; @@ -683,17 +739,18 @@ static void sx1301_tx_work_handler(struct work_struct *ws) struct sx1301_priv *priv = container_of(ws, struct sx1301_priv, tx_work); struct net_device *netdev = dev_get_drvdata(priv->dev); + struct lora_frame *frame = (struct lora_frame *)priv->tx_skb->data; int ret; netdev_dbg(netdev, "%s\n", __func__); if (priv->tx_skb) { - ret = sx1301_tx(priv, priv->tx_skb->data, priv->tx_skb->len); + ret = sx1301_tx(priv, frame); if (ret) { netdev->stats.tx_errors++; } else { netdev->stats.tx_packets++; - netdev->stats.tx_bytes += priv->tx_skb->len; + netdev->stats.tx_bytes += frame->len; } if (!(netdev->flags & IFF_ECHO) || diff --git a/include/uapi/linux/lora.h b/include/uapi/linux/lora.h index 4ff00b9c3c20..d675025d2a6e 100644 --- a/include/uapi/linux/lora.h +++ b/include/uapi/linux/lora.h @@ -21,4 +21,50 @@ struct sockaddr_lora { } lora_addr; }; +#define LORA_MAX_DLEN 256 + +enum lora_bw { + LORA_BW_125KHZ, + LORA_BW_250KHZ, + LORA_BW_500KHZ, +}; + +enum lora_cr { + LORA_CR_4_5, + LORA_CR_4_6, + LORA_CR_4_7, + LORA_CR_4_8, +}; + +enum lora_sf { + LORA_SF_6, + LORA_SF_7, + LORA_SF_8, + LORA_SF_9, + LORA_SF_10, + LORA_SF_11, + LORA_SF_12, +}; + +/** + * struct lora_frame - LoRa frame structure + * @freq: Frequency of LoRa transmission in Hz + * @power: Power of transmission in dBm + * @bw: bandwidth, 125, 250 or 500 KHz + * @cr: coding rate, 4/5 to 4/8 + * @sf: spreading factor from SF6 to 12 + * @data: LoRa frame payload (up to LORA_MAX_DLEN byte) + */ +struct lora_frame { + __u32 freq; /* transmission frequency in Hz */ + __u8 power; /* transmission power in dBm */ + enum lora_bw bw; /* bandwidth */ + enum lora_cr cr; /* coding rate */ + enum lora_sf sf; /* spreading factor */ + __u8 len; + __u8 data[LORA_MAX_DLEN] __attribute__((aligned(8))); +}; + +#define LORA_MTU (sizeof(struct lora_frame)) + #endif /* _UAPI_LINUX_LORA_H */