kernel: support reading hex MAC address from NVMEM

In addition to binary and ASCII-formatted MAC addresses, add support
for processing hexadecimal encoded MAC addresses from NVMEM.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This commit is contained in:
Daniel Golle 2023-10-26 03:50:40 +01:00
parent f32baf6a65
commit 7db87d7c68
2 changed files with 70 additions and 8 deletions

View File

@ -20,10 +20,11 @@ string.
--- a/drivers/nvmem/core.c --- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c
@@ -7,9 +7,11 @@ @@ -7,9 +7,12 @@
*/ */
#include <linux/device.h> #include <linux/device.h>
+#include <linux/ctype.h>
+#include <linux/etherdevice.h> +#include <linux/etherdevice.h>
#include <linux/export.h> #include <linux/export.h>
#include <linux/fs.h> #include <linux/fs.h>
@ -32,7 +33,7 @@ string.
#include <linux/init.h> #include <linux/init.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/module.h> #include <linux/module.h>
@@ -696,6 +698,37 @@ static int nvmem_validate_keepouts(struc @@ -696,6 +699,62 @@ static int nvmem_validate_keepouts(struc
return 0; return 0;
} }
@ -66,25 +67,55 @@ string.
+ +
+ return 0; + return 0;
+} +}
+
+static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
+ void *buf, size_t bytes)
+{
+ u8 mac[ETH_ALEN], *hexstr;
+ int i;
+
+ if (WARN_ON(bytes != 2 * ETH_ALEN))
+ return -EINVAL;
+
+ hexstr = (u8 *)buf;
+ for (i = 0; i < ETH_ALEN; i++) {
+ if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
+ return -EINVAL;
+
+ mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
+ }
+
+ if (index)
+ eth_addr_add(mac, index);
+
+ ether_addr_copy(buf, mac);
+
+ return 0;
+}
+ +
static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
{ {
struct nvmem_layout *layout = nvmem->layout; struct nvmem_layout *layout = nvmem->layout;
@@ -731,6 +764,20 @@ static int nvmem_add_cells_from_dt(struc @@ -731,6 +790,25 @@ static int nvmem_add_cells_from_dt(struc
if (layout && layout->fixup_cell_info) if (layout && layout->fixup_cell_info)
layout->fixup_cell_info(nvmem, layout, &info); layout->fixup_cell_info(nvmem, layout, &info);
+ if (of_device_is_compatible(np, "fixed-layout")) { + if (of_device_is_compatible(np, "fixed-layout")) {
+ if (of_device_is_compatible(child, "mac-base")) { + if (of_device_is_compatible(child, "mac-base")) {
+ if (info.bytes == 6) { + if (info.bytes == ETH_ALEN) {
+ info.raw_len = info.bytes; + info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN; + info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_raw_read; + info.read_post_process = nvmem_mac_base_raw_read;
+ } else if (info.bytes == 2 * ETH_ALEN) {
+ info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_hex_read;
+ } else if (info.bytes == 3 * ETH_ALEN - 1) { + } else if (info.bytes == 3 * ETH_ALEN - 1) {
+ info.raw_len = info.bytes; + info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN; + info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_ascii_read; + info.read_post_process = nvmem_mac_base_ascii_read;
+ } + }
+
+ } + }
+ } + }
+ +

View File

@ -20,10 +20,11 @@ string.
--- a/drivers/nvmem/core.c --- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c
@@ -7,9 +7,11 @@ @@ -7,9 +7,12 @@
*/ */
#include <linux/device.h> #include <linux/device.h>
+#include <linux/ctype.h>
+#include <linux/etherdevice.h> +#include <linux/etherdevice.h>
#include <linux/export.h> #include <linux/export.h>
#include <linux/fs.h> #include <linux/fs.h>
@ -32,7 +33,7 @@ string.
#include <linux/init.h> #include <linux/init.h>
#include <linux/kref.h> #include <linux/kref.h>
#include <linux/module.h> #include <linux/module.h>
@@ -696,6 +698,37 @@ static int nvmem_validate_keepouts(struc @@ -696,6 +699,62 @@ static int nvmem_validate_keepouts(struc
return 0; return 0;
} }
@ -66,25 +67,55 @@ string.
+ +
+ return 0; + return 0;
+} +}
+
+static int nvmem_mac_base_hex_read(void *context, const char *id, int index, unsigned int offset,
+ void *buf, size_t bytes)
+{
+ u8 mac[ETH_ALEN], *hexstr;
+ int i;
+
+ if (WARN_ON(bytes != 2 * ETH_ALEN))
+ return -EINVAL;
+
+ hexstr = (u8 *)buf;
+ for (i = 0; i < ETH_ALEN; i++) {
+ if (!isxdigit(hexstr[i * 2]) || !isxdigit(hexstr[i * 2 + 1]))
+ return -EINVAL;
+
+ mac[i] = (hex_to_bin(hexstr[i * 2]) << 4) | hex_to_bin(hexstr[i * 2 + 1]);
+ }
+
+ if (index)
+ eth_addr_add(mac, index);
+
+ ether_addr_copy(buf, mac);
+
+ return 0;
+}
+ +
static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np) static int nvmem_add_cells_from_dt(struct nvmem_device *nvmem, struct device_node *np)
{ {
struct nvmem_layout *layout = nvmem->layout; struct nvmem_layout *layout = nvmem->layout;
@@ -731,6 +764,20 @@ static int nvmem_add_cells_from_dt(struc @@ -731,6 +790,25 @@ static int nvmem_add_cells_from_dt(struc
if (layout && layout->fixup_cell_info) if (layout && layout->fixup_cell_info)
layout->fixup_cell_info(nvmem, layout, &info); layout->fixup_cell_info(nvmem, layout, &info);
+ if (of_device_is_compatible(np, "fixed-layout")) { + if (of_device_is_compatible(np, "fixed-layout")) {
+ if (of_device_is_compatible(child, "mac-base")) { + if (of_device_is_compatible(child, "mac-base")) {
+ if (info.bytes == 6) { + if (info.bytes == ETH_ALEN) {
+ info.raw_len = info.bytes; + info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN; + info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_raw_read; + info.read_post_process = nvmem_mac_base_raw_read;
+ } else if (info.bytes == 2 * ETH_ALEN) {
+ info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_hex_read;
+ } else if (info.bytes == 3 * ETH_ALEN - 1) { + } else if (info.bytes == 3 * ETH_ALEN - 1) {
+ info.raw_len = info.bytes; + info.raw_len = info.bytes;
+ info.bytes = ETH_ALEN; + info.bytes = ETH_ALEN;
+ info.read_post_process = nvmem_mac_base_ascii_read; + info.read_post_process = nvmem_mac_base_ascii_read;
+ } + }
+
+ } + }
+ } + }
+ +