From patchwork Sun Jul 1 11:08:02 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: 140700 Delivered-To: patch@linaro.org Received: by 2002:a2e:9754:0:0:0:0:0 with SMTP id f20-v6csp2848539ljj; Sun, 1 Jul 2018 04:10:22 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeW0NsTtIud0cxzNP+jMkIV3srjtmnBXl+HgyLw8J4lxUysnNiBQ2badM+3IR0x83Sl5/6a X-Received: by 2002:a65:4541:: with SMTP id x1-v6mr13248133pgr.26.1530443422873; Sun, 01 Jul 2018 04:10:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530443422; cv=none; d=google.com; s=arc-20160816; b=jj7+chXLaWnHzPcWwTgz8Rn5r+P/JQQOXsscYyGMJAHHbjbK0Is+9FFtJ5y+RjHVhB Tt+XPA8HZHf17COGPv885gr22Pemma1qa0YDZz2HRZGnOF8YHi8wGolYziuicpQ8arvY z9XWWzVbsyJyx7FpJsN8gxchkBK4/xg/DiTSnYdMA1Wj7vNlQOaIrWE7faOkd9ecJqr2 fT1KXl2KuxTb3VGPevQW6/mMJrRnE3FGvB8TvNg3GrUprE5PN5fSChuIaFoAjL7oIZs3 lubu3v0sn/hFmaDja6xDy0EUusEXeXlhanmYzCXtHZqiGkgUSEFf4Dznl0uczEHQUBqv d1BA== 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=9Xt1jV+HFlmqnrPDQH0JwfRRmK8DX5nicTFPRsKaDvk=; b=Z5iDKZ6D+3qftRBzHqIwolXYDtKCgMSo6Buo6kzM2Jz2r6z9LaxBT5QnsbdbFRjqLJ sPzfHbbm2ZuaKJ/iTjUFflwQ7yOeVMxCSL5Ec6KNMqy8H28HwcT3bND1meJazP++srHW odp0q+YuJ9OzrviZsWq+9KXCwbLli2D/Ba0Yu4u3e23nUcfSwD6ut2gsuLjHcbInIEuX sey5ZT7A8xbzp9wVqA0uK+KF01cC17QTfY8iJ90c+OmL64JWMkkgqoq0BHW+wlOjFIsu xKFpRcZbFZcC1YQbUJG6Q61b+FFzmeJOKWHsCRYiAUXBnJI/eeSkTiO8qlPy1+6u1QUn hY8g== 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 q130-v6si11406707pgq.526.2018.07.01.04.10.22; Sun, 01 Jul 2018 04:10:22 -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 S1752339AbeGALKU (ORCPT + 9 others); Sun, 1 Jul 2018 07:10:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:47958 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752334AbeGALJE (ORCPT ); Sun, 1 Jul 2018 07:09:04 -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 F2471AFA5; Sun, 1 Jul 2018 11:09:02 +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 13/15] net: lora: Prepare RAK RAK811 Date: Sun, 1 Jul 2018 13:08:02 +0200 Message-Id: <20180701110804.32415-14-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 RAK811 and RAK812 offer a UART based AT command interface. It allows both LoRaWAN and LoRa modes. Cc: Ken Yu (禹凯) Signed-off-by: Andreas Färber --- drivers/net/lora/Kconfig | 7 ++ drivers/net/lora/Makefile | 3 + drivers/net/lora/rak811.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 229 insertions(+) create mode 100644 drivers/net/lora/rak811.c -- 2.16.4 diff --git a/drivers/net/lora/Kconfig b/drivers/net/lora/Kconfig index 72a9d2a0f2be..3e384493cbdd 100644 --- a/drivers/net/lora/Kconfig +++ b/drivers/net/lora/Kconfig @@ -17,6 +17,13 @@ config LORA_DEV if LORA_DEV +config LORA_RAK811 + tristate "RAK RAK811 driver" + default y + depends on SERIAL_DEV_BUS + help + RAK RAK811/RAK812 + config LORA_RN2483 tristate "Microchip RN2483/RN2903 driver" default y diff --git a/drivers/net/lora/Makefile b/drivers/net/lora/Makefile index dfa9a43dcfb3..6b6870ffbfd8 100644 --- a/drivers/net/lora/Makefile +++ b/drivers/net/lora/Makefile @@ -9,6 +9,9 @@ lora-dev-y := dev.o # Alphabetically sorted. # +obj-$(CONFIG_LORA_RAK811) += lora-rak811.o +lora-rak811-y := rak811.o + obj-$(CONFIG_LORA_RN2483) += lora-rn2483.o lora-rn2483-y := rn2483.o lora-rn2483-y += rn2483_cmd.o diff --git a/drivers/net/lora/rak811.c b/drivers/net/lora/rak811.c new file mode 100644 index 000000000000..ad3d0980c489 --- /dev/null +++ b/drivers/net/lora/rak811.c @@ -0,0 +1,219 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * RAK RAK811 + * + * Copyright (c) 2017-2018 Andreas Färber + */ + +#include +#include +#include +#include +#include +#include +#include + +struct rak811_device { + struct serdev_device *serdev; + + char rx_buf[4096]; + int rx_len; + + struct completion line_recv_comp; +}; + +static int rak811_send_command(struct rak811_device *rakdev, const char *cmd, char **data, unsigned long timeout) +{ + struct serdev_device *sdev = rakdev->serdev; + const char *crlf = "\r\n"; + char *resp; + + serdev_device_write_buf(sdev, cmd, strlen(cmd)); + serdev_device_write_buf(sdev, crlf, 2); + + timeout = wait_for_completion_timeout(&rakdev->line_recv_comp, timeout); + if (!timeout) + return -ETIMEDOUT; + + resp = rakdev->rx_buf; + dev_dbg(&sdev->dev, "Received: '%s'\n", resp); + if (data) + *data = kstrdup(resp, GFP_KERNEL); + + rakdev->rx_len = 0; + reinit_completion(&rakdev->line_recv_comp); + + return 0; +} + +static int rak811_simple_cmd(struct rak811_device *rakdev, const char *cmd, unsigned long timeout) +{ + char *resp; + int ret; + + ret = rak811_send_command(rakdev, cmd, &resp, timeout); + if (ret) + return ret; + + if (strncmp(resp, "OK", 2) == 0) { + kfree(resp); + return 0; + } + + kfree(resp); + + return -EINVAL; +} + +static int rak811_get_version(struct rak811_device *rakdev, char **version, unsigned long timeout) +{ + char *resp; + int ret; + + ret = rak811_send_command(rakdev, "at+version", &resp, timeout); + if (ret) + return ret; + + if (strncmp(resp, "OK", 2) == 0) { + *version = kstrdup(resp + 2, GFP_KERNEL); + kfree(resp); + return 0; + } + + kfree(resp); + + return -EINVAL; +} + +static int rak811_receive_buf(struct serdev_device *sdev, const u8 *data, size_t count) +{ + struct rak811_device *rakdev = serdev_device_get_drvdata(sdev); + size_t i = 0; + int len = 0; + + dev_dbg(&sdev->dev, "Receive (%d)\n", (int)count); + + for (i = 0; i < count; i++) { + dev_dbg(&sdev->dev, "Receive: 0x%02x\n", (int)data[i]); + } + + if (completion_done(&rakdev->line_recv_comp)) { + dev_info(&sdev->dev, "RX waiting on completion\n"); + return 0; + } + if (rakdev->rx_len == sizeof(rakdev->rx_buf) - 1) { + dev_warn(&sdev->dev, "RX buffer full\n"); + return 0; + } + + i = min(count, sizeof(rakdev->rx_buf) - 1 - rakdev->rx_len); + if (i > 0) { + memcpy(&rakdev->rx_buf[rakdev->rx_len], data, i); + rakdev->rx_len += i; + len += i; + } + if (rakdev->rx_len >= 2 && strncmp(&rakdev->rx_buf[rakdev->rx_len - 2], "\r\n", 2) == 0) { + rakdev->rx_len -= 2; + rakdev->rx_buf[rakdev->rx_len] = '\0'; + complete(&rakdev->line_recv_comp); + } + + return len; +} + +static const struct serdev_device_ops rak811_serdev_client_ops = { + .receive_buf = rak811_receive_buf, +}; + +static int rak811_probe(struct serdev_device *sdev) +{ + struct rak811_device *rakdev; + char *sz; + int ret; + + dev_info(&sdev->dev, "Probing\n"); + + rakdev = devm_kzalloc(&sdev->dev, sizeof(struct rak811_device), GFP_KERNEL); + if (!rakdev) + return -ENOMEM; + + rakdev->serdev = sdev; + init_completion(&rakdev->line_recv_comp); + serdev_device_set_drvdata(sdev, rakdev); + + ret = serdev_device_open(sdev); + if (ret) { + dev_err(&sdev->dev, "Failed to open (%d)\n", ret); + return ret; + } + + serdev_device_set_baudrate(sdev, 115200); + serdev_device_set_flow_control(sdev, false); + serdev_device_set_client_ops(sdev, &rak811_serdev_client_ops); + + ret = rak811_get_version(rakdev, &sz, HZ); + if (ret) { + dev_err(&sdev->dev, "Failed to get version (%d)\n", ret); + serdev_device_close(sdev); + return ret; + } + + dev_info(&sdev->dev, "firmware version: %s\n", sz); + kfree(sz); + + ret = rak811_simple_cmd(rakdev, "at+mode=1", 2 * HZ); + if (ret) { + dev_err(&sdev->dev, "Failed to set mode to P2P (%d)\n", ret); + serdev_device_close(sdev); + return ret; + } + + dev_info(&sdev->dev, "Done.\n"); + + return 0; +} + +static void rak811_remove(struct serdev_device *sdev) +{ + serdev_device_close(sdev); + + dev_info(&sdev->dev, "Removed\n"); +} + +static const struct of_device_id rak811_of_match[] = { + { .compatible = "rakwireless,rak811" }, + {} +}; +MODULE_DEVICE_TABLE(of, rak811_of_match); + +static struct serdev_device_driver rak811_serdev_driver = { + .probe = rak811_probe, + .remove = rak811_remove, + .driver = { + .name = "rak811", + .of_match_table = rak811_of_match, + }, +}; + +static int __init rak811_init(void) +{ + int ret; + + ret = serdev_device_driver_register(&rak811_serdev_driver); + if (ret) + return ret; + + return 0; +} + +static void __exit rak811_exit(void) +{ + serdev_device_driver_unregister(&rak811_serdev_driver); +} + +module_init(rak811_init); +module_exit(rak811_exit); + +MODULE_DESCRIPTION("RAK811 serdev driver"); +MODULE_AUTHOR("Andreas Färber "); +MODULE_LICENSE("GPL");