From patchwork Fri Nov 30 08:20:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10705945 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EE12B1057 for ; Fri, 30 Nov 2018 08:21:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DFB7E2FBF1 for ; Fri, 30 Nov 2018 08:21:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CFF0F2FBFF; Fri, 30 Nov 2018 08:21:22 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 385272FBF1 for ; Fri, 30 Nov 2018 08:21:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727041AbeK3T3u (ORCPT ); Fri, 30 Nov 2018 14:29:50 -0500 Received: from mail-wm1-f65.google.com ([209.85.128.65]:53490 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726971AbeK3T3t (ORCPT ); Fri, 30 Nov 2018 14:29:49 -0500 Received: by mail-wm1-f65.google.com with SMTP id y1so4813482wmi.3 for ; Fri, 30 Nov 2018 00:21:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ccuz9dZlPq5ZTOzECk8TCynM67LjW14hECdEnE8yAoo=; b=uX7QS7g4JDsPrxezfmeCVmdxMKHX0QZ3kZ6AfJtpusnADX1lEV8tF79PS2JbrpqIY7 Q5nYud/F2oFMyW8keHTPVAq4jk81aYEx4zW2HInP5+PJyVGehVT30JBtTr2h7PwgflIo XgjYte0RcD5NpvCC4gqrCaYL7y7eTeUADR887kVBvamV4s1+2ZMsBicHgwatcyZ9vovq wc88AOijPR7YcG8NRa4p7tbkb9yjOe/RhLKu8EbsUnCJly64cAVaNwiHhypwxYihLSpW eBnWt28r/oCyYoQftm/8nWtK/x2l7UCvBxiuHTaT6Av8E+/LbEDfnxEiyXqI7qONXYSC WwLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ccuz9dZlPq5ZTOzECk8TCynM67LjW14hECdEnE8yAoo=; b=SHHzfwItsXxVX4EUJpdFAbZ0j3susxW8dKiwhTVifOFk9yPgoKYfQOgxU4cjuXNOkP x+/dQ0F41t/vQqSBbNA3SBc3sviYpL88+8/mQUGo5xho3pAdO6g2EP29pTddUG2urICk UQDlz5FuBCrXRYpMtHeN6nnRXDndDLo1WAsnIIHHgaG253ep5UnLoSLelQQ16th+G3k+ qPdOgvtWMjdRjOEAidvubdu+1TqzRzdacqt7F3xtrO8wZsWA1XOmR0pLMqbWNLa0RnII uNAil9GrSCCPPWy3yL9f+NWFxOHSsmXR9l3/r1ayr7nucWZBLh4nR1lk0fjdSLKEvDMX 397w== X-Gm-Message-State: AA+aEWYvp+3OBjirliqJbskxWecj0XxnBLBcUYaRqy33n0BC+yks/6TX vt9ABWgpg5Yj55oDM5ihXiSmXA== X-Google-Smtp-Source: AFSGD/WSQb4UKxUJp/cZxMhoCDD6o9879iKyE70hXRqynkc8QHsrqcU9k/BhxMJDtgeuSzFpRHOBzQ== X-Received: by 2002:a1c:9314:: with SMTP id v20mr4695071wmd.15.1543566078364; Fri, 30 Nov 2018 00:21:18 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id x197sm6473374wmd.2.2018.11.30.00.21.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 30 Nov 2018 00:21:17 -0800 (PST) From: Bartosz Golaszewski To: Grygorii Strashko , Nicolas Ferre , "David S . Miller" , Andrew Lunn , Florian Fainelli , Heiner Kallweit , Rob Herring , Frank Rowand , Sekhar Nori , Kevin Hilman Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-omap@vger.kernel.org, Bartosz Golaszewski Subject: [net-next][PATCH 1/4] net: ethernet: provide nvmem_get_mac_address() Date: Fri, 30 Nov 2018 09:20:57 +0100 Message-Id: <20181130082100.6241-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181130082100.6241-1-brgl@bgdev.pl> References: <20181130082100.6241-1-brgl@bgdev.pl> MIME-Version: 1.0 Sender: linux-omap-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-omap@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Bartosz Golaszewski We already have of_get_nvmem_mac_address() but some non-DT systems want to read the MAC address from NVMEM too. Implement a generalized routine that takes struct device as argument. Signed-off-by: Bartosz Golaszewski --- include/linux/etherdevice.h | 1 + net/ethernet/eth.c | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/linux/etherdevice.h b/include/linux/etherdevice.h index 572e11bb8696..2c0af7b00715 100644 --- a/include/linux/etherdevice.h +++ b/include/linux/etherdevice.h @@ -32,6 +32,7 @@ struct device; int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr); unsigned char *arch_get_platform_mac_address(void); +int nvmem_get_mac_address(struct device *dev, void *addrbuf); u32 eth_get_headlen(void *data, unsigned int max_len); __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev); extern const struct header_ops eth_header_ops; diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c index fd8faa0dfa61..df38593d1bb4 100644 --- a/net/ethernet/eth.c +++ b/net/ethernet/eth.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -548,3 +549,40 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr) return 0; } EXPORT_SYMBOL(eth_platform_get_mac_address); + +/** + * Obtain the MAC address from an nvmem cell named 'mac-address' associated + * with given device. + * + * @dev: Device with which the mac-address cell is associated. + * @addrbuf: Buffer to which the MAC address will be copied on success. + * + * Returns 0 on success or a negative error number on failure. + */ +int nvmem_get_mac_address(struct device *dev, void *addrbuf) +{ + struct nvmem_cell *cell; + const void *mac; + size_t len; + + cell = nvmem_cell_get(dev, "mac-address"); + if (IS_ERR(cell)) + return PTR_ERR(cell); + + mac = nvmem_cell_read(cell, &len); + nvmem_cell_put(cell); + + if (IS_ERR(mac)) + return PTR_ERR(mac); + + if (len != ETH_ALEN || !is_valid_ether_addr(mac)) { + kfree(mac); + return -EINVAL; + } + + ether_addr_copy(addrbuf, mac); + kfree(mac); + + return 0; +} +EXPORT_SYMBOL(nvmem_get_mac_address);