Commit 109b3a89 authored by Finn Thain's avatar Finn Thain Committed by Greg Kroah-Hartman

char/nvram: Implement NVRAM read/write methods

Refactor the RTC "CMOS" NVRAM functions so that they can be used as
arch_nvram_ops methods. Checksumming logic is moved from the misc device
operations to the nvram read/write operations. This makes the misc device
implementation more generic.

This preserves the locking mechanism such that "read if checksum valid"
and "write and update checksum" remain atomic operations.

Some platforms implement byte-range read/write methods which are similar
to file_operations struct methods. Other platforms provide only
byte-at-a-time methods. The former are more efficient but may be
unavailable so fall back on the latter methods when necessary.
Tested-by: default avatarStan Johnson <userm57@yahoo.com>
Signed-off-by: default avatarFinn Thain <fthain@telegraphics.com.au>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 2d58636e
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/proc_fs.h> #include <linux/proc_fs.h>
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/slab.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
...@@ -161,7 +162,46 @@ static ssize_t pc_nvram_get_size(void) ...@@ -161,7 +162,46 @@ static ssize_t pc_nvram_get_size(void)
return NVRAM_BYTES; return NVRAM_BYTES;
} }
static ssize_t pc_nvram_read(char *buf, size_t count, loff_t *ppos)
{
char *p = buf;
loff_t i;
spin_lock_irq(&rtc_lock);
if (!__nvram_check_checksum()) {
spin_unlock_irq(&rtc_lock);
return -EIO;
}
for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
*p = __nvram_read_byte(i);
spin_unlock_irq(&rtc_lock);
*ppos = i;
return p - buf;
}
static ssize_t pc_nvram_write(char *buf, size_t count, loff_t *ppos)
{
char *p = buf;
loff_t i;
spin_lock_irq(&rtc_lock);
if (!__nvram_check_checksum()) {
spin_unlock_irq(&rtc_lock);
return -EIO;
}
for (i = *ppos; count > 0 && i < NVRAM_BYTES; --count, ++i, ++p)
__nvram_write_byte(*p, i);
__nvram_set_checksum();
spin_unlock_irq(&rtc_lock);
*ppos = i;
return p - buf;
}
const struct nvram_ops arch_nvram_ops = { const struct nvram_ops arch_nvram_ops = {
.read = pc_nvram_read,
.write = pc_nvram_write,
.read_byte = pc_nvram_read_byte, .read_byte = pc_nvram_read_byte,
.write_byte = pc_nvram_write_byte, .write_byte = pc_nvram_write_byte,
.get_size = pc_nvram_get_size, .get_size = pc_nvram_get_size,
...@@ -184,69 +224,57 @@ static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin) ...@@ -184,69 +224,57 @@ static loff_t nvram_misc_llseek(struct file *file, loff_t offset, int origin)
static ssize_t nvram_misc_read(struct file *file, char __user *buf, static ssize_t nvram_misc_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
unsigned char contents[NVRAM_BYTES]; char *tmp;
unsigned i = *ppos; ssize_t ret;
unsigned char *tmp;
spin_lock_irq(&rtc_lock);
if (!__nvram_check_checksum())
goto checksum_err;
for (tmp = contents; count-- > 0 && i < NVRAM_BYTES; ++i, ++tmp) if (!access_ok(buf, count))
*tmp = __nvram_read_byte(i); return -EFAULT;
if (*ppos >= nvram_size)
return 0;
spin_unlock_irq(&rtc_lock); count = min_t(size_t, count, nvram_size - *ppos);
count = min_t(size_t, count, PAGE_SIZE);
if (copy_to_user(buf, contents, tmp - contents)) tmp = kmalloc(count, GFP_KERNEL);
return -EFAULT; if (!tmp)
return -ENOMEM;
*ppos = i; ret = nvram_read(tmp, count, ppos);
if (ret <= 0)
goto out;
return tmp - contents; if (copy_to_user(buf, tmp, ret)) {
*ppos -= ret;
ret = -EFAULT;
}
checksum_err: out:
spin_unlock_irq(&rtc_lock); kfree(tmp);
return -EIO; return ret;
} }
static ssize_t nvram_misc_write(struct file *file, const char __user *buf, static ssize_t nvram_misc_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos) size_t count, loff_t *ppos)
{ {
unsigned char contents[NVRAM_BYTES]; char *tmp;
unsigned i = *ppos; ssize_t ret;
unsigned char *tmp;
if (i >= NVRAM_BYTES)
return 0; /* Past EOF */
if (count > NVRAM_BYTES - i)
count = NVRAM_BYTES - i;
if (count > NVRAM_BYTES)
return -EFAULT; /* Can't happen, but prove it to gcc */
if (copy_from_user(contents, buf, count)) if (!access_ok(buf, count))
return -EFAULT; return -EFAULT;
if (*ppos >= nvram_size)
return 0;
spin_lock_irq(&rtc_lock); count = min_t(size_t, count, nvram_size - *ppos);
count = min_t(size_t, count, PAGE_SIZE);
if (!__nvram_check_checksum())
goto checksum_err;
for (tmp = contents; count--; ++i, ++tmp)
__nvram_write_byte(*tmp, i);
__nvram_set_checksum(); tmp = memdup_user(buf, count);
if (IS_ERR(tmp))
spin_unlock_irq(&rtc_lock); return PTR_ERR(tmp);
*ppos = i; ret = nvram_write(tmp, count, ppos);
kfree(tmp);
return tmp - contents; return ret;
checksum_err:
spin_unlock_irq(&rtc_lock);
return -EIO;
} }
static long nvram_misc_ioctl(struct file *file, unsigned int cmd, static long nvram_misc_ioctl(struct file *file, unsigned int cmd,
......
...@@ -66,18 +66,46 @@ static inline void nvram_write_byte(unsigned char val, int addr) ...@@ -66,18 +66,46 @@ static inline void nvram_write_byte(unsigned char val, int addr)
#endif #endif
} }
static inline ssize_t nvram_read_bytes(char *buf, size_t count, loff_t *ppos)
{
ssize_t nvram_size = nvram_get_size();
loff_t i;
char *p = buf;
if (nvram_size < 0)
return nvram_size;
for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
*p = nvram_read_byte(i);
*ppos = i;
return p - buf;
}
static inline ssize_t nvram_write_bytes(char *buf, size_t count, loff_t *ppos)
{
ssize_t nvram_size = nvram_get_size();
loff_t i;
char *p = buf;
if (nvram_size < 0)
return nvram_size;
for (i = *ppos; count > 0 && i < nvram_size; ++i, ++p, --count)
nvram_write_byte(*p, i);
*ppos = i;
return p - buf;
}
static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos) static inline ssize_t nvram_read(char *buf, size_t count, loff_t *ppos)
{ {
if (arch_nvram_ops.read) if (arch_nvram_ops.read)
return arch_nvram_ops.read(buf, count, ppos); return arch_nvram_ops.read(buf, count, ppos);
return -ENODEV; return nvram_read_bytes(buf, count, ppos);
} }
static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos) static inline ssize_t nvram_write(char *buf, size_t count, loff_t *ppos)
{ {
if (arch_nvram_ops.write) if (arch_nvram_ops.write)
return arch_nvram_ops.write(buf, count, ppos); return arch_nvram_ops.write(buf, count, ppos);
return -ENODEV; return nvram_write_bytes(buf, count, ppos);
} }
#endif /* _LINUX_NVRAM_H */ #endif /* _LINUX_NVRAM_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment