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