From patchwork Tue Nov 13 14:01:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 10680711 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 794BD14E2 for ; Tue, 13 Nov 2018 14:02:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68E022A9BD for ; Tue, 13 Nov 2018 14:02:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5BE872A9C7; Tue, 13 Nov 2018 14:02:16 +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 E8D062A9C1 for ; Tue, 13 Nov 2018 14:02:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387888AbeKNAAa (ORCPT ); Tue, 13 Nov 2018 19:00:30 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:39235 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387853AbeKNAA3 (ORCPT ); Tue, 13 Nov 2018 19:00:29 -0500 Received: by mail-wr1-f65.google.com with SMTP id b13so13407283wrx.6 for ; Tue, 13 Nov 2018 06:02:12 -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=Y1Ikk8NgQXMqwpoVUP121s+8HRZcWcJ2uhzlSaFno9Jt35RotfDNIizNFi2pJSERdE JY3i7MHMmIdMxBfGAOZXFfscqNPbJorgU6YlbYEPchiKNNSmauwS+CqByewU7L5P2Pb2 Ty+35fPZUl9PamqqCj1kh83hinbakmM/par85eA1i9xi7NSmU7BaXT5njEonCBrre3bp ZjjKoYd0lZFz9AkNwYz30Cpt/TT+zvrwVg9+76xjFlrdXY9Ie8Ata8EZ1rlmlC/9A9XG hZENno7gq9eKRmpVgJ7OziWoK2RubCX81HdDrJ74k73B+qpIt9XZzAfY/RQpLZ5lsH+B ZPQw== 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=efQLK5WC9oH588CPjDdzCEKAqHHovnjdOVdIj5mTKRoVgWIqQGyXBnES8iHYHdbUAA m49dv+/pLEvarKaXGTH1Tl/JvHafR8hgPb7neJPTEJt0w70uANNEzfAFJksQBGH8ign2 NfFJYMUFAmGDptYfBLMb5JLYIpLlLtluvRE1hVc5ZnL5UuWQxkQrR7789sTnoKski1px xeZD37Ye+zRonFk8giyWFPTKMiofJRYU/8+j57L7kdqQPXfDKh3HEEwKKxG8T6315AXl o3AvI47/S9gEzFzb+X+SGb7DJ2Yc+1v4uNOFF1B/QCkbVgnTus9uAqJuqlFDflWKnjIo ZcmA== X-Gm-Message-State: AGRZ1gKAAG3ILkl7RkCNvwh6ORmSNVbORC46v4Ot/881VvPi1b+GxEdg Rmd2hO0u50u+KrkjTMAy+VD+yQ== X-Google-Smtp-Source: AJdET5eG9CTFTnCYCng22fl6MqKtw+y6SlVDe065gFz+CvwaX0lJzR27KK+1HT0w6VQ4Oij2wpEgHA== X-Received: by 2002:a5d:4b05:: with SMTP id v5-v6mr4999594wrq.5.1542117731700; Tue, 13 Nov 2018 06:02:11 -0800 (PST) Received: from debian-brgl.home ([2a01:cb1d:af:5b00:6d6c:8493:1ab5:dad7]) by smtp.gmail.com with ESMTPSA id m69-v6sm16604122wmb.8.2018.11.13.06.02.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Nov 2018 06:02:11 -0800 (PST) From: Bartosz Golaszewski To: Sekhar Nori , Kevin Hilman , Russell King , Arnd Bergmann , Greg Kroah-Hartman , David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Nicolas Ferre , "David S . Miller" , Grygorii Strashko , Srinivas Kandagatla , Andrew Lunn , Florian Fainelli , Rob Herring , Frank Rowand , Wolfram Sang Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-i2c@vger.kernel.org, linux-mtd@lists.infradead.org, netdev@vger.kernel.org, linux-omap@vger.kernel.org, devicetree@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 10/25] net: ethernet: provide nvmem_get_mac_address() Date: Tue, 13 Nov 2018 15:01:18 +0100 Message-Id: <20181113140133.17385-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181113140133.17385-1-brgl@bgdev.pl> References: <20181113140133.17385-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);