From patchwork Sun Jul 1 11:07:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Andreas_F=C3=A4rber?= X-Patchwork-Id: 10498657 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id BA294602D6 for ; Sun, 1 Jul 2018 11:36:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8099728A51 for ; Sun, 1 Jul 2018 11:36:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 724A528A53; Sun, 1 Jul 2018 11:36:51 +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=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI 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 7F55D28A51 for ; Sun, 1 Jul 2018 11:36:50 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=Cl51hJYDouXJSObKkzi6UpOA4ohGdlyShGs8GFLxgLE=; b=D5fPdSm2tnZuji ZM+aZz1zUmrMf8HhQj2XJcirB9M/dhZfpIb7a+0OxnV6cKhudIS1+IDSZEsOSiJKNiRBlXsXOvWmj D1JbHnyJnRGVclKzHjgRI+fa7m6WwuOKSRIXfynibIMAeueQiosc7s9eD01jIGbW+wzjCzJ3mXY5L +pVuaBK+6gEwMWAWshRqIxYUresowH7QGDJwbRDBfcGQnvc5IFrp8zRNhVQOvz3jpixU8hp+6MRs9 K8/RXwYjk5dQY5vKG4UNdEnKfcpfha7Dllu2WjpVoV17/vUzHmr81sU12yzV27Dk2qNI8GVn7m2eN rqfwavaAda2slbqzP2oA==; 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 1fZaes-0001l7-CY; Sun, 01 Jul 2018 11:36:46 +0000 Received: from mx2.suse.de ([195.135.220.15] helo=mx1.suse.de) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fZaEP-0004Y0-95 for linux-arm-kernel@lists.infradead.org; Sun, 01 Jul 2018 11:10:01 +0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id C79AAAF62; Sun, 1 Jul 2018 11:09:00 +0000 (UTC) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: netdev@vger.kernel.org Subject: [RFC net-next 10/15] net: lora: Add Microchip RN2483 Date: Sun, 1 Jul 2018 13:07:59 +0200 Message-Id: <20180701110804.32415-11-afaerber@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20180701110804.32415-1-afaerber@suse.de> References: <20180701110804.32415-1-afaerber@suse.de> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180701_040926_356201_747F43AC X-CRM114-Status: GOOD ( 19.34 ) 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: Matthias Brugger , Jiri Pirko , Marcel Holtmann , Dollar Chen , linux-kernel@vger.kernel.org, =?UTF-8?q?Michael=20R=C3=B6der?= , Janus Piwek , =?UTF-8?q?Andreas=20F=C3=A4rber?= , Jian-Hong Pan , Ken Yu , "David S . Miller" , linux-arm-kernel@lists.infradead.org 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 The Microchip RN2483 and RN2903 are UART based modules exposing both LoRaWAN and LoRa. The RN2483 supports switching between 433 and 868 MHz. Signed-off-by: Andreas Färber --- drivers/net/lora/Kconfig | 7 + drivers/net/lora/Makefile | 4 + drivers/net/lora/rn2483.c | 344 ++++++++++++++++++++++++++++++++++++++++++ drivers/net/lora/rn2483.h | 40 +++++ drivers/net/lora/rn2483_cmd.c | 130 ++++++++++++++++ 5 files changed, 525 insertions(+) create mode 100644 drivers/net/lora/rn2483.c create mode 100644 drivers/net/lora/rn2483.h create mode 100644 drivers/net/lora/rn2483_cmd.c diff --git a/drivers/net/lora/Kconfig b/drivers/net/lora/Kconfig index 0436f6b09a1c..940bd2cbe106 100644 --- a/drivers/net/lora/Kconfig +++ b/drivers/net/lora/Kconfig @@ -17,6 +17,13 @@ config LORA_DEV if LORA_DEV +config LORA_RN2483 + tristate "Microchip RN2483/RN2903 driver" + default y + depends on SERIAL_DEV_BUS + help + Microchip RN2483/2903 + config LORA_SX1276 tristate "Semtech SX127x SPI driver" default y diff --git a/drivers/net/lora/Makefile b/drivers/net/lora/Makefile index 8845542dba50..07839c3ce9f8 100644 --- a/drivers/net/lora/Makefile +++ b/drivers/net/lora/Makefile @@ -9,5 +9,9 @@ lora-dev-y := dev.o # Alphabetically sorted. # +obj-$(CONFIG_LORA_RN2483) += lora-rn2483.o +lora-rn2483-y := rn2483.o +lora-rn2483-y += rn2483_cmd.o + obj-$(CONFIG_LORA_SX1276) += lora-sx1276.o lora-sx1276-y := sx1276.o diff --git a/drivers/net/lora/rn2483.c b/drivers/net/lora/rn2483.c new file mode 100644 index 000000000000..8b9ec2575ee2 --- /dev/null +++ b/drivers/net/lora/rn2483.c @@ -0,0 +1,344 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Microchip RN2483/RN2903 + * + * Copyright (c) 2017-2018 Andreas Färber + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "rn2483.h" + +struct rn2483_priv { + struct lora_priv lora; +}; + +static netdev_tx_t rn2483_loradev_start_xmit(struct sk_buff *skb, struct net_device *netdev) +{ + if (skb->protocol != htons(ETH_P_LORA)) { + kfree_skb(skb); + netdev->stats.tx_dropped++; + return NETDEV_TX_OK; + } + + netif_stop_queue(netdev); + + /* TODO */ + return NETDEV_TX_OK; +} + +static int rn2483_loradev_open(struct net_device *netdev) +{ + int ret; + + netdev_dbg(netdev, "%s", __func__); + + ret = open_loradev(netdev); + if (ret) + return ret; + + netif_start_queue(netdev); + + return 0; +} + +static int rn2483_loradev_stop(struct net_device *netdev) +{ + netdev_dbg(netdev, "%s", __func__); + + netif_stop_queue(netdev); + close_loradev(netdev); + + return 0; +} + +static const struct net_device_ops rn2483_net_device_ops = { + .ndo_open = rn2483_loradev_open, + .ndo_stop = rn2483_loradev_stop, + .ndo_start_xmit = rn2483_loradev_start_xmit, +}; + +int rn2483_readline_timeout(struct rn2483_device *rndev, char **line, unsigned long timeout) +{ + timeout = wait_for_completion_timeout(&rndev->line_recv_comp, timeout); + if (!timeout) + return -ETIMEDOUT; + + *line = devm_kstrdup(&rndev->serdev->dev, rndev->buf, GFP_KERNEL); + complete(&rndev->line_read_comp); + if (!*line) + return -ENOMEM; + + return 0; +} + +static void rn2483_receive_line(struct rn2483_device *rndev, const char *sz, size_t len) +{ + dev_dbg(&rndev->serdev->dev, "Received line '%s' (%d)", sz, (int)len); + + reinit_completion(&rndev->line_read_comp); + complete(&rndev->line_recv_comp); + wait_for_completion(&rndev->line_read_comp); + reinit_completion(&rndev->line_recv_comp); +} + +static int rn2483_receive_buf(struct serdev_device *serdev, const u8 *data, size_t count) +{ + struct rn2483_device *rndev = serdev_device_get_drvdata(serdev); + size_t i; + + dev_dbg(&serdev->dev, "Receive (%d)", (int)count); + if (!rndev->buf) { + rndev->buf = devm_kmalloc(&serdev->dev, count, GFP_KERNEL); + if (!rndev->buf) + return 0; + rndev->buflen = 0; + } else { + void *tmp = devm_kmalloc(&serdev->dev, rndev->buflen + count, GFP_KERNEL); + if (!tmp) + return 0; + memcpy(tmp, rndev->buf, rndev->buflen); + devm_kfree(&serdev->dev, rndev->buf); + rndev->buf = tmp; + } + + for (i = 0; i < count; i++) { + if (data[i] == '\r') { + rndev->saw_cr = true; + } else if (data[i] == '\n' && rndev->saw_cr) { + if (i > 1) + memcpy(rndev->buf + rndev->buflen, data, i - 1); + ((char *)rndev->buf)[rndev->buflen + i - 1] = 0; + rn2483_receive_line(rndev, rndev->buf, rndev->buflen + i - 1); + rndev->saw_cr = false; + devm_kfree(&serdev->dev, rndev->buf); + rndev->buf = NULL; + rndev->buflen = 0; + return i + 1; + } else + rndev->saw_cr = false; + } + + memcpy(rndev->buf + rndev->buflen, data, count); + rndev->buflen += count; + return count; +} + +static const struct serdev_device_ops rn2483_serdev_client_ops = { + .receive_buf = rn2483_receive_buf, +}; + +static int rn2483_probe(struct serdev_device *sdev) +{ + struct rn2483_device *rndev; + char *line, *cmd; + char sz[5]; + u32 status; + int ret; + + dev_info(&sdev->dev, "Probing"); + + rndev = devm_kzalloc(&sdev->dev, sizeof(struct rn2483_device), GFP_KERNEL); + if (!rndev) + return -ENOMEM; + + rndev->serdev = sdev; + init_completion(&rndev->line_recv_comp); + init_completion(&rndev->line_read_comp); + mutex_init(&rndev->cmd_lock); + serdev_device_set_drvdata(sdev, rndev); + + rndev->reset_gpio = devm_gpiod_get_optional(&sdev->dev, "reset", GPIOD_OUT_LOW); + if (IS_ERR(rndev->reset_gpio)) + return PTR_ERR(rndev->reset_gpio); + + ret = serdev_device_open(sdev); + if (ret) { + dev_err(&sdev->dev, "Failed to open (%d)", ret); + return ret; + } + + serdev_device_set_baudrate(sdev, 57600); + serdev_device_set_flow_control(sdev, false); + + gpiod_set_value_cansleep(rndev->reset_gpio, 0); + msleep(5); + serdev_device_set_client_ops(sdev, &rn2483_serdev_client_ops); + gpiod_set_value_cansleep(rndev->reset_gpio, 1); + msleep(100); + + ret = rn2483_readline_timeout(rndev, &line, HZ); + if (ret) { + if (ret != -ENOMEM) + dev_err(&sdev->dev, "Timeout waiting for firmware identification"); + goto err_timeout; + } + + if (strlen(line) < strlen("RNxxxx X.Y.Z MMM DD YYYY HH:MM:SS") || line[6] != ' ' || + strncmp(line, "RN", 2) != 0) { + dev_err(&sdev->dev, "Unexpected response '%s'", line); + devm_kfree(&sdev->dev, line); + ret = -EINVAL; + goto err_version; + } + dev_info(&sdev->dev, "Firmware '%s'", line); + strncpy(sz, line + 2, 4); + sz[4] = 0; + devm_kfree(&sdev->dev, line); + ret = kstrtouint(sz, 10, &rndev->model); + if (ret) + goto err_model; + if (!(rndev->model == 2483 || rndev->model == 2903)) { + dev_err(&sdev->dev, "Unknown model %u", rndev->model); + ret = -ENOTSUPP; + goto err_model; + } + dev_info(&sdev->dev, "Detected RN%u", rndev->model); + + ret = rn2483_sys_get_hweui(rndev, &rndev->hweui); + if (ret) { + if (ret != -ENOMEM) + dev_err(&sdev->dev, "Failed to read HWEUI (%d)", ret); + goto err_hweui; + } + dev_info(&sdev->dev, "HWEUI " PRIxLORAEUI, LORA_EUI(rndev->hweui)); + + switch (rndev->model) { + case 2483: + ret = rn2483_mac_get_band(rndev, &rndev->band); + if (ret) { + dev_err(&sdev->dev, "Failed to read band (%d)", ret); + goto err_band; + } + dev_info(&sdev->dev, "Frequency band %u MHz", rndev->band); + + ret = rn2483_mac_reset_band(rndev, 433); + if (ret) { + dev_err(&sdev->dev, "Failed to reset band (%d)", ret); + goto err_band; + } + rndev->band = 433; + + ret = rn2483_mac_get_band(rndev, &rndev->band); + if (!ret) + dev_info(&sdev->dev, "New frequency band: %u MHz", rndev->band); + break; + case 2903: + /* No "mac get band" command available */ + rndev->band = 915; + break; + } + + ret = rn2483_mac_get_status(rndev, &status); + if (!ret) + dev_info(&sdev->dev, "MAC status %08x", status); + + if (true) { + u32 pause; + ret = rn2483_mac_pause(rndev, &pause); + if (!ret) + dev_info(&sdev->dev, "MAC pausing (0x%08x)", pause); + ret = rn2483_mac_resume(rndev); + if (!ret) + dev_info(&sdev->dev, "MAC resuming"); + } + + cmd = "mac get sync"; + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, cmd, &line, HZ); + mutex_unlock(&rndev->cmd_lock); + if (!ret) { + dev_info(&sdev->dev, "%s => '%s'", cmd, line); + devm_kfree(&sdev->dev, line); + } + + rndev->netdev = alloc_loradev(sizeof(struct rn2483_priv)); + if (!rndev->netdev) { + ret = -ENOMEM; + goto err_alloc_netdev; + } + + rndev->netdev->netdev_ops = &rn2483_net_device_ops; + SET_NETDEV_DEV(rndev->netdev, &sdev->dev); + + ret = register_loradev(rndev->netdev); + if (ret) + goto err_register_netdev; + + dev_info(&sdev->dev, "Done."); + + return 0; + +err_register_netdev: + free_loradev(rndev->netdev); +err_alloc_netdev: +err_band: +err_hweui: +err_model: +err_version: +err_timeout: + gpiod_set_value_cansleep(rndev->reset_gpio, 0); + return ret; +} + +static void rn2483_remove(struct serdev_device *sdev) +{ + struct rn2483_device *rndev = serdev_device_get_drvdata(sdev); + + unregister_loradev(rndev->netdev); + free_loradev(rndev->netdev); + + gpiod_set_value_cansleep(rndev->reset_gpio, 0); + + complete(&rndev->line_read_comp); + + serdev_device_close(sdev); + + dev_info(&sdev->dev, "Removed"); +} + +static const struct of_device_id rn2483_of_match[] = { + { .compatible = "microchip,rn2483" }, + { .compatible = "microchip,rn2903" }, + {} +}; +MODULE_DEVICE_TABLE(of, rn2483_of_match); + +static struct serdev_device_driver rn2483_serdev_driver = { + .probe = rn2483_probe, + .remove = rn2483_remove, + .driver = { + .name = "rn2483", + .of_match_table = rn2483_of_match, + }, +}; + +static int __init rn2483_init(void) +{ + int ret; + + ret = serdev_device_driver_register(&rn2483_serdev_driver); + if (ret) + return ret; + + return 0; +} + +static void __exit rn2483_exit(void) +{ + serdev_device_driver_unregister(&rn2483_serdev_driver); +} + +module_init(rn2483_init); +module_exit(rn2483_exit); + +MODULE_DESCRIPTION("RN2483 serdev driver"); +MODULE_AUTHOR("Andreas Färber "); +MODULE_LICENSE("GPL"); diff --git a/drivers/net/lora/rn2483.h b/drivers/net/lora/rn2483.h new file mode 100644 index 000000000000..f92660286f15 --- /dev/null +++ b/drivers/net/lora/rn2483.h @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2017-2018 Andreas Färber + */ +#ifndef _RN2483_H +#define _RN2483_H + +#include +#include +#include +#include +#include + +struct rn2483_device { + struct serdev_device *serdev; + struct gpio_desc *reset_gpio; + struct net_device *netdev; + unsigned model; + lora_eui hweui; + unsigned band; + bool saw_cr; + void *buf; + size_t buflen; + struct completion line_recv_comp; + struct completion line_read_comp; + struct mutex cmd_lock; +}; + +int rn2483_readline_timeout(struct rn2483_device *rndev, char **line, unsigned long timeout); +int rn2483_send_command_timeout(struct rn2483_device *rndev, + const char *cmd, char **resp, unsigned long timeout); + +int rn2483_sys_get_hweui(struct rn2483_device *rndev, lora_eui *val); +int rn2483_mac_get_band(struct rn2483_device *rndev, uint *val); +int rn2483_mac_get_status(struct rn2483_device *rndev, u32 *val); +int rn2483_mac_reset_band(struct rn2483_device *rndev, unsigned band); +int rn2483_mac_pause(struct rn2483_device *rndev, u32 *max_pause); +int rn2483_mac_resume(struct rn2483_device *rndev); + +#endif diff --git a/drivers/net/lora/rn2483_cmd.c b/drivers/net/lora/rn2483_cmd.c new file mode 100644 index 000000000000..6d6fca8fa93c --- /dev/null +++ b/drivers/net/lora/rn2483_cmd.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Microchip RN2483/RN2903 - UART commands + * + * Copyright (c) 2017-2018 Andreas Färber + */ +#include "rn2483.h" + +#define RN2483_CMD_TIMEOUT HZ + +int rn2483_send_command_timeout(struct rn2483_device *rndev, + const char *cmd, char **resp, unsigned long timeout) +{ + int ret; + + ret = serdev_device_write_buf(rndev->serdev, cmd, strlen(cmd)); + if (ret < 0) + return ret; + + ret = serdev_device_write_buf(rndev->serdev, "\r\n", 2); + if (ret < 0) + return ret; + + return rn2483_readline_timeout(rndev, resp, timeout); +} + +int rn2483_sys_get_hweui(struct rn2483_device *rndev, lora_eui *val) +{ + int ret; + char *line; + + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, "sys get hweui", &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + if (ret) + return ret; + + ret = lora_strtoeui(line, val); + devm_kfree(&rndev->serdev->dev, line); + return ret; +} + +int rn2483_mac_get_band(struct rn2483_device *rndev, uint *val) +{ + int ret; + char *line; + + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, "mac get band", &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + if (ret) + return ret; + + ret = kstrtouint(line, 10, val); + devm_kfree(&rndev->serdev->dev, line); + + return ret; +} + +int rn2483_mac_get_status(struct rn2483_device *rndev, u32 *val) +{ + int ret; + char *line; + + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, "mac get status", &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + if (ret) + return ret; + + ret = kstrtou32(line, 16, val); + devm_kfree(&rndev->serdev->dev, line); + return ret; +} + +int rn2483_mac_reset_band(struct rn2483_device *rndev, unsigned band) +{ + int ret; + char *line, *cmd; + + cmd = devm_kasprintf(&rndev->serdev->dev, GFP_KERNEL, "mac reset %u", band); + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, cmd, &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + devm_kfree(&rndev->serdev->dev, cmd); + if (ret) + return ret; + + if (strcmp(line, "ok") == 0) + ret = 0; + else if (strcmp(line, "invalid_param") == 0) + ret = -EINVAL; + else + ret = -EPROTO; + + devm_kfree(&rndev->serdev->dev, line); + return ret; +} + +int rn2483_mac_pause(struct rn2483_device *rndev, u32 *max_pause) +{ + int ret; + char *line; + + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, "mac pause", &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + if (ret) + return ret; + + ret = kstrtou32(line, 10, max_pause); + devm_kfree(&rndev->serdev->dev, line); + return ret; +} + +int rn2483_mac_resume(struct rn2483_device *rndev) +{ + int ret; + char *line; + + mutex_lock(&rndev->cmd_lock); + ret = rn2483_send_command_timeout(rndev, "mac resume", &line, RN2483_CMD_TIMEOUT); + mutex_unlock(&rndev->cmd_lock); + if (ret) + return ret; + + ret = (strcmp(line, "ok") == 0) ? 0 : -EPROTO; + devm_kfree(&rndev->serdev->dev, line); + return ret; +}