Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic /dev/nvram driver for architectures providing some |
| 3 | * "generic" hooks, that is : |
| 4 | * |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 5 | * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Note that an additional hook is supported for PowerMac only |
| 8 | * for getting the nvram "partition" informations |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #define NVRAM_VERSION "1.1" |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/fs.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/fcntl.h> |
| 21 | #include <linux/init.h> |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 22 | #include <linux/mutex.h> |
Al Viro | b808b1d | 2015-12-05 21:39:06 -0500 | [diff] [blame] | 23 | #include <linux/pagemap.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/nvram.h> |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 26 | #ifdef CONFIG_PPC_PMAC |
| 27 | #include <asm/machdep.h> |
| 28 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #define NVRAM_SIZE 8192 |
| 31 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 32 | static DEFINE_MUTEX(nvram_mutex); |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 33 | static ssize_t nvram_len; |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) |
| 36 | { |
Al Viro | b808b1d | 2015-12-05 21:39:06 -0500 | [diff] [blame] | 37 | return generic_file_llseek_size(file, offset, origin, |
| 38 | MAX_LFS_FILESIZE, nvram_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static ssize_t read_nvram(struct file *file, char __user *buf, |
| 42 | size_t count, loff_t *ppos) |
| 43 | { |
| 44 | unsigned int i; |
| 45 | char __user *p = buf; |
| 46 | |
| 47 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 48 | return -EFAULT; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 49 | if (*ppos >= nvram_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return 0; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 51 | for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | if (__put_user(nvram_read_byte(i), p)) |
| 53 | return -EFAULT; |
| 54 | *ppos = i; |
| 55 | return p - buf; |
| 56 | } |
| 57 | |
| 58 | static ssize_t write_nvram(struct file *file, const char __user *buf, |
| 59 | size_t count, loff_t *ppos) |
| 60 | { |
| 61 | unsigned int i; |
| 62 | const char __user *p = buf; |
| 63 | char c; |
| 64 | |
| 65 | if (!access_ok(VERIFY_READ, buf, count)) |
| 66 | return -EFAULT; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 67 | if (*ppos >= nvram_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return 0; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 69 | for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (__get_user(c, p)) |
| 71 | return -EFAULT; |
| 72 | nvram_write_byte(c, i); |
| 73 | } |
| 74 | *ppos = i; |
| 75 | return p - buf; |
| 76 | } |
| 77 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 78 | static int nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
| 80 | switch(cmd) { |
| 81 | #ifdef CONFIG_PPC_PMAC |
| 82 | case OBSOLETE_PMAC_NVRAM_GET_OFFSET: |
| 83 | printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); |
| 84 | case IOC_NVRAM_GET_OFFSET: { |
| 85 | int part, offset; |
| 86 | |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 87 | if (!machine_is(powermac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -EINVAL; |
| 89 | if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) |
| 90 | return -EFAULT; |
| 91 | if (part < pmac_nvram_OF || part > pmac_nvram_NR) |
| 92 | return -EINVAL; |
| 93 | offset = pmac_get_partition(part); |
| 94 | if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) |
| 95 | return -EFAULT; |
| 96 | break; |
| 97 | } |
| 98 | #endif /* CONFIG_PPC_PMAC */ |
| 99 | case IOC_NVRAM_SYNC: |
| 100 | nvram_sync(); |
| 101 | break; |
| 102 | default: |
| 103 | return -EINVAL; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 109 | static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 110 | { |
| 111 | int ret; |
| 112 | |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 113 | mutex_lock(&nvram_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 114 | ret = nvram_ioctl(file, cmd, arg); |
Arnd Bergmann | 613655f | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 115 | mutex_unlock(&nvram_mutex); |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 120 | const struct file_operations nvram_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .owner = THIS_MODULE, |
| 122 | .llseek = nvram_llseek, |
| 123 | .read = read_nvram, |
| 124 | .write = write_nvram, |
Arnd Bergmann | 5592933 | 2010-04-27 00:24:05 +0200 | [diff] [blame] | 125 | .unlocked_ioctl = nvram_unlocked_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static struct miscdevice nvram_dev = { |
| 129 | NVRAM_MINOR, |
| 130 | "nvram", |
| 131 | &nvram_fops |
| 132 | }; |
| 133 | |
| 134 | int __init nvram_init(void) |
| 135 | { |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 136 | int ret = 0; |
| 137 | |
Philippe De Muyter | cfc53f6 | 2008-06-12 15:21:46 -0700 | [diff] [blame] | 138 | printk(KERN_INFO "Generic non-volatile memory driver v%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | NVRAM_VERSION); |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 140 | ret = misc_register(&nvram_dev); |
| 141 | if (ret != 0) |
| 142 | goto out; |
| 143 | |
| 144 | nvram_len = nvram_get_size(); |
| 145 | if (nvram_len < 0) |
| 146 | nvram_len = NVRAM_SIZE; |
| 147 | |
| 148 | out: |
| 149 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void __exit nvram_cleanup(void) |
| 153 | { |
| 154 | misc_deregister( &nvram_dev ); |
| 155 | } |
| 156 | |
| 157 | module_init(nvram_init); |
| 158 | module_exit(nvram_cleanup); |
| 159 | MODULE_LICENSE("GPL"); |