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: 10498651 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 2EDFD60284 for ; Sun, 1 Jul 2018 11:21:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1137428518 for ; Sun, 1 Jul 2018 11:21:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0257128524; Sun, 1 Jul 2018 11:21:43 +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 6790828518 for ; Sun, 1 Jul 2018 11:21:43 +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=MUEyoXnYALwG2HlM8czTqfTGFyQruCJJc1XdMRWbUpk=; b=V1IPerkSyUsqa2 GSwlTtf/keY2tHsgv7AoFojMwc6H3N9klehjsXUCwqQYO6Rg5MjjHs/106EqTo4thMfJEjr2IsgH2 a4b3iXmR7F3h0KJ5MKVH++YOCOzKFB7elnZvy86F8D1XjJWoG2mwk7q1HxLd3zHHgjGf/6/GyfFkD Zf3vzBBcvca0aVatc4OpzFU/+r0SmxoB4tWbvcYdFSRkJB3YS81ctXtUfr+uBia6kuYpcNshJ1o0y Y4Zo753ftM4cA0NtirSNKPrNCtLe5laioZ3kTRQhTv1WIWYpVOj9uxlrBCgJXRevaGFX4+7QRdUHr PgIA5IkxyWjViGoJ1DHA==; 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 1fZaQC-0004Fx-Nc; Sun, 01 Jul 2018 11:21:36 +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-0004aS-9k for linux-arm-kernel@lists.infradead.org; Sun, 01 Jul 2018 11:09:41 +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 F2471AFA5; Sun, 1 Jul 2018 11:09:02 +0000 (UTC) From: =?UTF-8?q?Andreas=20F=C3=A4rber?= To: netdev@vger.kernel.org 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180701_040926_620734_48C5FC4B X-CRM114-Status: GOOD ( 16.47 ) 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 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 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");