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