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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/uaccess.h> |
| 23 | #include <asm/nvram.h> |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 24 | #ifdef CONFIG_PPC_PMAC |
| 25 | #include <asm/machdep.h> |
| 26 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #define NVRAM_SIZE 8192 |
| 29 | |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 30 | static ssize_t nvram_len; |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) |
| 33 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | switch (origin) { |
| 35 | case 1: |
| 36 | offset += file->f_pos; |
| 37 | break; |
| 38 | case 2: |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 39 | offset += nvram_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | break; |
| 41 | } |
Frederic Weisbecker | 6783b9c | 2009-10-09 21:20:30 +0200 | [diff] [blame] | 42 | if (offset < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return -EINVAL; |
Frederic Weisbecker | 6783b9c | 2009-10-09 21:20:30 +0200 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | file->f_pos = offset; |
Frederic Weisbecker | 6783b9c | 2009-10-09 21:20:30 +0200 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | return file->f_pos; |
| 48 | } |
| 49 | |
| 50 | static ssize_t read_nvram(struct file *file, char __user *buf, |
| 51 | size_t count, loff_t *ppos) |
| 52 | { |
| 53 | unsigned int i; |
| 54 | char __user *p = buf; |
| 55 | |
| 56 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 57 | return -EFAULT; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 58 | if (*ppos >= nvram_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return 0; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 60 | for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (__put_user(nvram_read_byte(i), p)) |
| 62 | return -EFAULT; |
| 63 | *ppos = i; |
| 64 | return p - buf; |
| 65 | } |
| 66 | |
| 67 | static ssize_t write_nvram(struct file *file, const char __user *buf, |
| 68 | size_t count, loff_t *ppos) |
| 69 | { |
| 70 | unsigned int i; |
| 71 | const char __user *p = buf; |
| 72 | char c; |
| 73 | |
| 74 | if (!access_ok(VERIFY_READ, buf, count)) |
| 75 | return -EFAULT; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 76 | if (*ppos >= nvram_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return 0; |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 78 | for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | if (__get_user(c, p)) |
| 80 | return -EFAULT; |
| 81 | nvram_write_byte(c, i); |
| 82 | } |
| 83 | *ppos = i; |
| 84 | return p - buf; |
| 85 | } |
| 86 | |
| 87 | static int nvram_ioctl(struct inode *inode, struct file *file, |
| 88 | unsigned int cmd, unsigned long arg) |
| 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 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 119 | const struct file_operations nvram_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | .owner = THIS_MODULE, |
| 121 | .llseek = nvram_llseek, |
| 122 | .read = read_nvram, |
| 123 | .write = write_nvram, |
| 124 | .ioctl = nvram_ioctl, |
| 125 | }; |
| 126 | |
| 127 | static struct miscdevice nvram_dev = { |
| 128 | NVRAM_MINOR, |
| 129 | "nvram", |
| 130 | &nvram_fops |
| 131 | }; |
| 132 | |
| 133 | int __init nvram_init(void) |
| 134 | { |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 135 | int ret = 0; |
| 136 | |
Philippe De Muyter | cfc53f6 | 2008-06-12 15:21:46 -0700 | [diff] [blame] | 137 | printk(KERN_INFO "Generic non-volatile memory driver v%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | NVRAM_VERSION); |
Martyn Welch | d331d83 | 2009-08-13 09:03:02 +0100 | [diff] [blame] | 139 | ret = misc_register(&nvram_dev); |
| 140 | if (ret != 0) |
| 141 | goto out; |
| 142 | |
| 143 | nvram_len = nvram_get_size(); |
| 144 | if (nvram_len < 0) |
| 145 | nvram_len = NVRAM_SIZE; |
| 146 | |
| 147 | out: |
| 148 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void __exit nvram_cleanup(void) |
| 152 | { |
| 153 | misc_deregister( &nvram_dev ); |
| 154 | } |
| 155 | |
| 156 | module_init(nvram_init); |
| 157 | module_exit(nvram_cleanup); |
| 158 | MODULE_LICENSE("GPL"); |