[PATCH v8 05/25] char/nvram: Adopt arch_nvram_ops

From: Finn Thain
Date: Tue Dec 25 2018 - 19:44:23 EST


Different platforms and architectures offer different NVRAM sizes and
access methods. E.g. PPC32 has byte-at-a-time accessor functions whereas
PPC64 has byte-range accessor functions. Adopt the nvram_ops struct so
the nvram module can call such functions as are defined by the various
platforms and architectures.

Signed-off-by: Finn Thain <fthain@xxxxxxxxxxxxxxxxxxx>
---
The procfs code here could be moved to arch/x86 (like the earlier patch
did for m68k code that was here). The nvram_ops struct could be
implemented and exported by the rtc-cmos driver instead, to eliminate
the remaining #ifdefs.
---
drivers/char/nvram.c | 33 +++++++++++++++++++++++++++------
include/linux/nvram.h | 2 ++
2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/drivers/char/nvram.c b/drivers/char/nvram.c
index c660cff9faf4..00897daa0643 100644
--- a/drivers/char/nvram.c
+++ b/drivers/char/nvram.c
@@ -52,9 +52,11 @@ static DEFINE_MUTEX(nvram_mutex);
static DEFINE_SPINLOCK(nvram_state_lock);
static int nvram_open_cnt; /* #times opened */
static int nvram_open_mode; /* special open modes */
+static ssize_t nvram_size;
#define NVRAM_WRITE 1 /* opened for writing (exclusive) */
#define NVRAM_EXCL 2 /* opened with O_EXCL */

+#ifdef CONFIG_X86
/*
* These functions are provided to be called internally or by other parts of
* the kernel. It's up to the caller to ensure correct checksum before reading
@@ -162,6 +164,19 @@ void nvram_set_checksum(void)
}
#endif /* 0 */

+static ssize_t nvram_get_size(void)
+{
+ return NVRAM_BYTES;
+}
+
+const struct nvram_ops arch_nvram_ops = {
+ .read_byte = nvram_read_byte,
+ .write_byte = nvram_write_byte,
+ .get_size = nvram_get_size,
+};
+EXPORT_SYMBOL(arch_nvram_ops);
+#endif /* CONFIG_X86 */
+
/*
* The are the file operation function for user access to /dev/nvram
*/
@@ -169,7 +184,7 @@ void nvram_set_checksum(void)
static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
{
return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
- NVRAM_BYTES);
+ nvram_size);
}

static ssize_t nvram_misc_read(struct file *file, char __user *buf,
@@ -320,8 +335,7 @@ static int nvram_misc_release(struct inode *inode, struct file *file)
return 0;
}

-#ifdef CONFIG_PROC_FS
-
+#if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
static const char * const floppy_types[] = {
"none", "5.25'' 360k", "5.25'' 1.2M", "3.5'' 720k", "3.5'' 1.44M",
"3.5'' 2.88M", "3.5'' 2.88M"
@@ -411,7 +425,7 @@ static int nvram_proc_read(struct seq_file *seq, void *offset)

return 0;
}
-#endif /* CONFIG_PROC_FS */
+#endif /* CONFIG_X86 && CONFIG_PROC_FS */

static const struct file_operations nvram_misc_fops = {
.owner = THIS_MODULE,
@@ -433,13 +447,20 @@ static int __init nvram_module_init(void)
{
int ret;

+ if (arch_nvram_ops.get_size == NULL)
+ return -ENODEV;
+
+ nvram_size = arch_nvram_ops.get_size();
+ if (nvram_size < 0)
+ return nvram_size;
+
ret = misc_register(&nvram_misc);
if (ret) {
pr_err("nvram: can't misc_register on minor=%d\n", NVRAM_MINOR);
return ret;
}

-#ifdef CONFIG_PROC_FS
+#if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
if (!proc_create_single("driver/nvram", 0, NULL, nvram_proc_read)) {
pr_err("nvram: can't create /proc/driver/nvram\n");
misc_deregister(&nvram_misc);
@@ -453,7 +474,7 @@ static int __init nvram_module_init(void)

static void __exit nvram_module_exit(void)
{
-#ifdef CONFIG_PROC_FS
+#if defined(CONFIG_X86) && defined(CONFIG_PROC_FS)
remove_proc_entry("driver/nvram", NULL);
#endif
misc_deregister(&nvram_misc);
diff --git a/include/linux/nvram.h b/include/linux/nvram.h
index 4f78147e74d9..d1bdee50d6a8 100644
--- a/include/linux/nvram.h
+++ b/include/linux/nvram.h
@@ -15,6 +15,8 @@ extern int nvram_check_checksum(void);
struct nvram_ops {
ssize_t (*read)(char *, size_t, loff_t *);
ssize_t (*write)(char *, size_t, loff_t *);
+ unsigned char (*read_byte)(int);
+ void (*write_byte)(unsigned char, int);
ssize_t (*get_size)(void);
};

--
2.19.2