summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2019-01-28 22:55:43 -0800
committerSascha Hauer <s.hauer@pengutronix.de>2019-02-06 08:31:13 +0100
commit504ac299a531a0bd601e6db05dc1c2d3b86a9700 (patch)
tree10679e0f9fe48ec39e2f706f48af684cc606ec7d
parentd60e9aa5f5a21374950dfd15bedafeccf76c3b1d (diff)
downloadbarebox-504ac299a531a0bd601e6db05dc1c2d3b86a9700.tar.gz
fs: Share code between mem_write()/mem_read()
Rename memcpy_sz() to mem_copy() and move all of the identical code from mem_write()/mem_read() there. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--fs/devfs-core.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index d703c06..2b93a95 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -511,12 +511,22 @@ void cdev_remove_loop(struct cdev *cdev)
free(cdev);
}
-static void memcpy_sz(void *dst, const void *src, size_t count, int rwsize)
+static ssize_t mem_copy(struct device_d *dev, void *dst, const void *src,
+ resource_size_t count, resource_size_t offset,
+ unsigned long flags)
{
+ ssize_t size;
+ int rwsize = flags & O_RWSIZE_MASK;
+
+ if (!dev || dev->num_resources < 1)
+ return -1;
+
+ count = size = min(count, resource_size(&dev->resource[0]) - offset);
+
/* no rwsize specification given. Do whatever memcpy likes best */
if (!rwsize) {
memcpy(dst, src, count);
- return;
+ goto out;
}
rwsize = rwsize >> O_RWSIZE_SHIFT;
@@ -542,40 +552,32 @@ static void memcpy_sz(void *dst, const void *src, size_t count, int rwsize)
src += rwsize;
count -= rwsize;
}
+out:
+ return size;
}
ssize_t mem_read(struct cdev *cdev, void *buf, size_t count, loff_t offset,
unsigned long flags)
{
- unsigned long size;
- struct device_d *dev;
+ struct device_d *dev = cdev->dev;
- if (!cdev->dev || cdev->dev->num_resources < 1)
+ if (!dev)
return -1;
- dev = cdev->dev;
- size = min((resource_size_t)count,
- resource_size(&dev->resource[0]) -
- (resource_size_t)offset);
- memcpy_sz(buf, dev_get_mem_region(dev, 0) + offset, size, flags & O_RWSIZE_MASK);
- return size;
+ return mem_copy(dev, buf, dev_get_mem_region(dev, 0) + offset,
+ count, offset, flags);
}
EXPORT_SYMBOL(mem_read);
-ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count, loff_t offset,
- unsigned long flags)
+ssize_t mem_write(struct cdev *cdev, const void *buf, size_t count,
+ loff_t offset, unsigned long flags)
{
- unsigned long size;
- struct device_d *dev;
+ struct device_d *dev = cdev->dev;
- if (!cdev->dev || cdev->dev->num_resources < 1)
+ if (!dev)
return -1;
- dev = cdev->dev;
- size = min((resource_size_t)count,
- resource_size(&dev->resource[0]) -
- (resource_size_t)offset);
- memcpy_sz(dev_get_mem_region(dev, 0) + offset, buf, size, flags & O_RWSIZE_MASK);
- return size;
+ return mem_copy(dev, dev_get_mem_region(dev, 0) + offset, buf,
+ count, offset, flags);
}
EXPORT_SYMBOL(mem_write);