blob: 6c4f4b5a9dd3aebb80920b2cae781c4a858fccac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
4 *
Martyn Welchd331d832009-08-13 09:03:02 +01005 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Bergmann613655f2010-06-02 14:28:52 +020022#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/uaccess.h>
24#include <asm/nvram.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110025#ifdef CONFIG_PPC_PMAC
26#include <asm/machdep.h>
27#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NVRAM_SIZE 8192
30
Arnd Bergmann613655f2010-06-02 14:28:52 +020031static DEFINE_MUTEX(nvram_mutex);
Martyn Welchd331d832009-08-13 09:03:02 +010032static ssize_t nvram_len;
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
35{
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 switch (origin) {
Josef Bacik22735062011-07-18 13:21:39 -040037 case 0:
38 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 case 1:
40 offset += file->f_pos;
41 break;
42 case 2:
Martyn Welchd331d832009-08-13 09:03:02 +010043 offset += nvram_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
Josef Bacik22735062011-07-18 13:21:39 -040045 default:
46 offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020048 if (offset < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 return -EINVAL;
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 file->f_pos = offset;
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return file->f_pos;
54}
55
56static ssize_t read_nvram(struct file *file, char __user *buf,
57 size_t count, loff_t *ppos)
58{
59 unsigned int i;
60 char __user *p = buf;
61
62 if (!access_ok(VERIFY_WRITE, buf, count))
63 return -EFAULT;
Martyn Welchd331d832009-08-13 09:03:02 +010064 if (*ppos >= nvram_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return 0;
Martyn Welchd331d832009-08-13 09:03:02 +010066 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (__put_user(nvram_read_byte(i), p))
68 return -EFAULT;
69 *ppos = i;
70 return p - buf;
71}
72
73static ssize_t write_nvram(struct file *file, const char __user *buf,
74 size_t count, loff_t *ppos)
75{
76 unsigned int i;
77 const char __user *p = buf;
78 char c;
79
80 if (!access_ok(VERIFY_READ, buf, count))
81 return -EFAULT;
Martyn Welchd331d832009-08-13 09:03:02 +010082 if (*ppos >= nvram_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
Martyn Welchd331d832009-08-13 09:03:02 +010084 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (__get_user(c, p))
86 return -EFAULT;
87 nvram_write_byte(c, i);
88 }
89 *ppos = i;
90 return p - buf;
91}
92
Arnd Bergmann55929332010-04-27 00:24:05 +020093static int nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 switch(cmd) {
96#ifdef CONFIG_PPC_PMAC
97 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
98 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
99 case IOC_NVRAM_GET_OFFSET: {
100 int part, offset;
101
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100102 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return -EINVAL;
104 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
105 return -EFAULT;
106 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
107 return -EINVAL;
108 offset = pmac_get_partition(part);
109 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
110 return -EFAULT;
111 break;
112 }
113#endif /* CONFIG_PPC_PMAC */
114 case IOC_NVRAM_SYNC:
115 nvram_sync();
116 break;
117 default:
118 return -EINVAL;
119 }
120
121 return 0;
122}
123
Arnd Bergmann55929332010-04-27 00:24:05 +0200124static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
125{
126 int ret;
127
Arnd Bergmann613655f2010-06-02 14:28:52 +0200128 mutex_lock(&nvram_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200129 ret = nvram_ioctl(file, cmd, arg);
Arnd Bergmann613655f2010-06-02 14:28:52 +0200130 mutex_unlock(&nvram_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +0200131
132 return ret;
133}
134
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800135const struct file_operations nvram_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .owner = THIS_MODULE,
137 .llseek = nvram_llseek,
138 .read = read_nvram,
139 .write = write_nvram,
Arnd Bergmann55929332010-04-27 00:24:05 +0200140 .unlocked_ioctl = nvram_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143static struct miscdevice nvram_dev = {
144 NVRAM_MINOR,
145 "nvram",
146 &nvram_fops
147};
148
149int __init nvram_init(void)
150{
Martyn Welchd331d832009-08-13 09:03:02 +0100151 int ret = 0;
152
Philippe De Muytercfc53f62008-06-12 15:21:46 -0700153 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 NVRAM_VERSION);
Martyn Welchd331d832009-08-13 09:03:02 +0100155 ret = misc_register(&nvram_dev);
156 if (ret != 0)
157 goto out;
158
159 nvram_len = nvram_get_size();
160 if (nvram_len < 0)
161 nvram_len = NVRAM_SIZE;
162
163out:
164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167void __exit nvram_cleanup(void)
168{
169 misc_deregister( &nvram_dev );
170}
171
172module_init(nvram_init);
173module_exit(nvram_cleanup);
174MODULE_LICENSE("GPL");