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