Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp2718779imm; Sun, 1 Jul 2018 04:13:09 -0700 (PDT) X-Google-Smtp-Source: AAOMgpccoALa789XfzLaZ6kF75LpRfNZj1LvNR6LN+DH9BEBGNg5NAhPtj6uXBECN65llOlxwC3Z X-Received: by 2002:aa7:81d3:: with SMTP id c19-v6mr21392188pfn.224.1530443589670; Sun, 01 Jul 2018 04:13:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530443589; cv=none; d=google.com; s=arc-20160816; b=zJ3WmePvsFMIQFK5cpmzjCcKuG6iTRzcXBaVzAk5USi7IaVBLsYSdDtIVlrbOi91P1 jKth3zixcEp5M2hEOCZbrPwnQEO8d+3wyWWGXabHTwIpAtSd904DGVWrAY90gHrqRx6F 5AHnMC06+v+P7z4LOW4IZfi76cx/kzMLy9pYVE7lNzQfzXiHiosPp3KchT9DXzDgWbo0 WG8KXc3V9OKVwaF1j4HPhDFO0ca0MSXXQ9T5JHr7H0uVBJ0f7gZ6e1cGez7QPJAWxs+5 SnH6Bg6CsaG1azMUz0J3cHn4aMN4GR94Hle+XQz3ncCse1SYz7sRTb/cbeyvEFdRNRt3 xNDw== 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=Ez/xMbXuDHcwptL8+PHSidi65anhkepRJJ6WcRovdG/OcH9DCec1XOZAMruCJHZ6ph 9odyNXvaoMSVbh8EO3F6IxJlF0IKjOXojZx7sHgAltXDEetlwbnmEK+H44aSqfzvZERS 68ivB6uUFqXZoVgVGtf9QF66ny1EMFVWy0kMfymvBUGlsorLKSCmwcfugEmVevsv/b7S wDZqSZsZ93y/aNItND5HjWjeBNMTSX0GwlySeCeQf1tVe2CGs9X1b1IDzXAHSjEUrfvx 6dbNtc5yFcQ2KCVybEZjfG9Px/4PHoY2aNYGmvIKAh/c8zkfDa0QnmsPAinSBAKgW9Fr 6xFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 u190-v6si14443417pfb.325.2018.07.01.04.12.48; Sun, 01 Jul 2018 04:13:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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 linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752593AbeGALKW (ORCPT + 99 others); Sun, 1 Jul 2018 07:10:22 -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=20F=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=20R=C3=B6der?= , Dollar Chen , Ken Yu , =?UTF-8?q?Andreas=20F=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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 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"); -- 2.16.4