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: 140703 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2848672ljj; Sun, 1 Jul 2018 04:10:34 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdqzn3ATxxmZ9AE64yUBRClFncZ7TTE7/GfBJ2D993+KoRRXXWeSilB0BXDmutCLfeUz/Vq X-Received: by 2002:a17:902:5381:: with SMTP id c1-v6mr17130682pli.137.1530443434631; Sun, 01 Jul 2018 04:10:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530443434; cv=none; d=google.com; s=arc-20160816; b=uX1e9qk7xMpdbWX3ikbPnSERzQw6chtrpFPud/r5pFj0OBBoCHLiDbIN3+AnINzvAj mpnkfsBFQe0GZKnTab4/+Ok2MY9CUbpJeHzyhHc8K5jYLyl1SSwhP/sONbW8QId1sBdU 4NXrGArnI4zlkcWbHW137c6rE9R6JA5lFlhns6J/U8RXWU3NwZYvXStNh82Qt6ypjJIu dhmn+dOst1tlPSVJyVBL1qVdJNJ1PYrOx3YM+dd90KJr+aVRdB1fstG2K7WixfrCeaXK VIWBDDT7Z2cQ5VHBwIwQ6SEht8yHkzxukSSTFmd0pYprJOtMC0/9564hu8mFcL8cfwLM bQEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=X/P1PSbYoYsioTvQvPR3jQCWLF3Lg+1jXZl9XTjgg/4=; b=rbUIyKYErTi9B3uHlWTlLi7vjnSnE8sIqhHEU8/0i3C0vpEPzfW4tLYcMIfcuk315C w0CSkvt9GDB45m/9gqXP7Hmg4FRNjlFhKjoSNFm51kSSYEWogeUyNp5BSuF6tFMbGP2o lIZL1lfF2x3+K4Onr0hgaO4nMVUh75cFiSqpB20cnIWAb3BThETltbWxTwbXaj0iXlhh 5m0iVjxCudqUIALRDS7BpZ/guW+JdAH76ObY+XwcqwGqfNwJflEVt7Uucj1nPLkB3chb 2j1K+seog094MWvOtdCVzWiBcq8xOvUyQ0Vqxtbsx+x2OSYWLu1iNHRo8jlLCS/MY9jD WCKw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l68-v6si12115640pgl.84.2018.07.01.04.10.34; Sun, 01 Jul 2018 04:10:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752682AbeGALKb (ORCPT + 9 others); Sun, 1 Jul 2018 07:10:31 -0400 Received: from mx2.suse.de ([195.135.220.15]:47858 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752311AbeGALJC (ORCPT ); Sun, 1 Jul 2018 07:09:02 -0400 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_F=C3=A4rber?= To: netdev@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Jian-Hong Pan , Jiri Pirko , Marcel Holtmann , "David S . Miller" , Matthias Brugger , Janus Piwek , =?utf-8?q?Michael_R=C3=B6der?= , Dollar Chen , Ken Yu , =?utf-8?q?Andreas_F=C3=A4rber?= 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 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org 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 -- 2.16.4 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; +}