|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 Eistec AB |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser General |
| 5 | + * Public License v2.1. See the file LICENSE in the top level directory for more |
| 6 | + * details. |
| 7 | + */ |
| 8 | + |
| 9 | +#if MODULE_VFS |
| 10 | + |
| 11 | +#include <fcntl.h> |
| 12 | +#include <errno.h> |
| 13 | +#include <unistd.h> |
| 14 | + |
| 15 | +#include "nvram.h" |
| 16 | +#include "vfs.h" |
| 17 | + |
| 18 | +/** |
| 19 | + * @ingroup nvram |
| 20 | + * @{ |
| 21 | + * |
| 22 | + * @file |
| 23 | + * |
| 24 | + * @brief NVRAM generic VFS operations |
| 25 | + * |
| 26 | + * This allows the nvram driver to register as a node on DevFS |
| 27 | + * |
| 28 | + * See boards/mulle or tests/unittests/tests-devfs for examples on how to use. |
| 29 | + * |
| 30 | + * Tested with nvram_spi on Mulle |
| 31 | + * |
| 32 | + * @author Joakim Nohlgård <joakim.nohlgard@eistec.se> |
| 33 | + */ |
| 34 | + |
| 35 | +static int nvram_vfs_fstat(vfs_file_t *filp, struct stat *buf); |
| 36 | +static off_t nvram_vfs_lseek(vfs_file_t *filp, off_t off, int whence); |
| 37 | +static ssize_t nvram_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes); |
| 38 | +static ssize_t nvram_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes); |
| 39 | + |
| 40 | +const vfs_file_ops_t nvram_vfs_ops = { |
| 41 | + .fstat = nvram_vfs_fstat, |
| 42 | + .lseek = nvram_vfs_lseek, |
| 43 | + .read = nvram_vfs_read, |
| 44 | + .write = nvram_vfs_write, |
| 45 | +}; |
| 46 | + |
| 47 | +static int nvram_vfs_fstat(vfs_file_t *filp, struct stat *buf) |
| 48 | +{ |
| 49 | + if (buf == NULL) { |
| 50 | + return -EFAULT; |
| 51 | + } |
| 52 | + nvram_t *dev = filp->private_data; |
| 53 | + if (dev == NULL) { |
| 54 | + return -EFAULT; |
| 55 | + } |
| 56 | + buf->st_nlink = 1; |
| 57 | + buf->st_size = dev->size; |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static off_t nvram_vfs_lseek(vfs_file_t *filp, off_t off, int whence) |
| 62 | +{ |
| 63 | + nvram_t *dev = filp->private_data; |
| 64 | + if (dev == NULL) { |
| 65 | + return -EFAULT; |
| 66 | + } |
| 67 | + switch (whence) { |
| 68 | + case SEEK_SET: |
| 69 | + break; |
| 70 | + case SEEK_CUR: |
| 71 | + off += filp->pos; |
| 72 | + break; |
| 73 | + case SEEK_END: |
| 74 | + off += dev->size; |
| 75 | + break; |
| 76 | + default: |
| 77 | + return -EINVAL; |
| 78 | + } |
| 79 | + if (off < 0) { |
| 80 | + /* the resulting file offset would be negative */ |
| 81 | + return -EINVAL; |
| 82 | + } |
| 83 | + /* POSIX allows seeking past the end of the file */ |
| 84 | + filp->pos = off; |
| 85 | + return off; |
| 86 | +} |
| 87 | + |
| 88 | +static ssize_t nvram_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes) |
| 89 | +{ |
| 90 | + nvram_t *dev = filp->private_data; |
| 91 | + if (dev == NULL) { |
| 92 | + return -EFAULT; |
| 93 | + } |
| 94 | + uint32_t src = filp->pos; |
| 95 | + if (src >= dev->size) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + if (src + nbytes > dev->size) { |
| 99 | + nbytes = dev->size - src; |
| 100 | + } |
| 101 | + int res = dev->read(dev, dest, src, nbytes); |
| 102 | + if (res < 0) { |
| 103 | + return res; |
| 104 | + } |
| 105 | + /* Advance file position */ |
| 106 | + filp->pos += res; |
| 107 | + return res; |
| 108 | +} |
| 109 | + |
| 110 | +static ssize_t nvram_vfs_write(vfs_file_t *filp, const void *src, size_t nbytes) |
| 111 | +{ |
| 112 | + nvram_t *dev = filp->private_data; |
| 113 | + if (dev == NULL) { |
| 114 | + return -EFAULT; |
| 115 | + } |
| 116 | + uint32_t dest = filp->pos; |
| 117 | + if (dest >= dev->size) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + if (dest + nbytes > dev->size) { |
| 121 | + nbytes = dev->size - dest; |
| 122 | + } |
| 123 | + int res = dev->write(dev, src, dest, nbytes); |
| 124 | + if (res < 0) { |
| 125 | + return res; |
| 126 | + } |
| 127 | + /* Advance file position */ |
| 128 | + filp->pos += res; |
| 129 | + return res; |
| 130 | +} |
| 131 | + |
| 132 | +/** @} */ |
| 133 | + |
| 134 | +#else |
| 135 | +typedef int dont_be_pedantic; |
| 136 | +#endif /* MODULE_VFS */ |
0 commit comments