Loading...
1/*
2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
4 *
5 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
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/mutex.h>
23#include <asm/uaccess.h>
24#include <asm/nvram.h>
25#ifdef CONFIG_PPC_PMAC
26#include <asm/machdep.h>
27#endif
28
29#define NVRAM_SIZE 8192
30
31static DEFINE_MUTEX(nvram_mutex);
32static ssize_t nvram_len;
33
34static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
35{
36 switch (origin) {
37 case 0:
38 break;
39 case 1:
40 offset += file->f_pos;
41 break;
42 case 2:
43 offset += nvram_len;
44 break;
45 default:
46 offset = -1;
47 }
48 if (offset < 0)
49 return -EINVAL;
50
51 file->f_pos = offset;
52
53 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;
64 if (*ppos >= nvram_len)
65 return 0;
66 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
67 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;
82 if (*ppos >= nvram_len)
83 return 0;
84 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
85 if (__get_user(c, p))
86 return -EFAULT;
87 nvram_write_byte(c, i);
88 }
89 *ppos = i;
90 return p - buf;
91}
92
93static int nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
94{
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
102 if (!machine_is(powermac))
103 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
124static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
125{
126 int ret;
127
128 mutex_lock(&nvram_mutex);
129 ret = nvram_ioctl(file, cmd, arg);
130 mutex_unlock(&nvram_mutex);
131
132 return ret;
133}
134
135const struct file_operations nvram_fops = {
136 .owner = THIS_MODULE,
137 .llseek = nvram_llseek,
138 .read = read_nvram,
139 .write = write_nvram,
140 .unlocked_ioctl = nvram_unlocked_ioctl,
141};
142
143static struct miscdevice nvram_dev = {
144 NVRAM_MINOR,
145 "nvram",
146 &nvram_fops
147};
148
149int __init nvram_init(void)
150{
151 int ret = 0;
152
153 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
154 NVRAM_VERSION);
155 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;
165}
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");
1/*
2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
4 *
5 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
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/mutex.h>
23#include <linux/pagemap.h>
24#include <linux/uaccess.h>
25#include <asm/nvram.h>
26#ifdef CONFIG_PPC_PMAC
27#include <asm/machdep.h>
28#endif
29
30#define NVRAM_SIZE 8192
31
32static DEFINE_MUTEX(nvram_mutex);
33static ssize_t nvram_len;
34
35static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
36{
37 return generic_file_llseek_size(file, offset, origin,
38 MAX_LFS_FILESIZE, nvram_len);
39}
40
41static ssize_t read_nvram(struct file *file, char __user *buf,
42 size_t count, loff_t *ppos)
43{
44 unsigned int i;
45 char __user *p = buf;
46
47 if (!access_ok(VERIFY_WRITE, buf, count))
48 return -EFAULT;
49 if (*ppos >= nvram_len)
50 return 0;
51 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
52 if (__put_user(nvram_read_byte(i), p))
53 return -EFAULT;
54 *ppos = i;
55 return p - buf;
56}
57
58static ssize_t write_nvram(struct file *file, const char __user *buf,
59 size_t count, loff_t *ppos)
60{
61 unsigned int i;
62 const char __user *p = buf;
63 char c;
64
65 if (!access_ok(VERIFY_READ, buf, count))
66 return -EFAULT;
67 if (*ppos >= nvram_len)
68 return 0;
69 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
70 if (__get_user(c, p))
71 return -EFAULT;
72 nvram_write_byte(c, i);
73 }
74 *ppos = i;
75 return p - buf;
76}
77
78static int nvram_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
79{
80 switch(cmd) {
81#ifdef CONFIG_PPC_PMAC
82 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
83 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
84 case IOC_NVRAM_GET_OFFSET: {
85 int part, offset;
86
87 if (!machine_is(powermac))
88 return -EINVAL;
89 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
90 return -EFAULT;
91 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
92 return -EINVAL;
93 offset = pmac_get_partition(part);
94 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
95 return -EFAULT;
96 break;
97 }
98#endif /* CONFIG_PPC_PMAC */
99 case IOC_NVRAM_SYNC:
100 nvram_sync();
101 break;
102 default:
103 return -EINVAL;
104 }
105
106 return 0;
107}
108
109static long nvram_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
110{
111 int ret;
112
113 mutex_lock(&nvram_mutex);
114 ret = nvram_ioctl(file, cmd, arg);
115 mutex_unlock(&nvram_mutex);
116
117 return ret;
118}
119
120const struct file_operations nvram_fops = {
121 .owner = THIS_MODULE,
122 .llseek = nvram_llseek,
123 .read = read_nvram,
124 .write = write_nvram,
125 .unlocked_ioctl = nvram_unlocked_ioctl,
126};
127
128static struct miscdevice nvram_dev = {
129 NVRAM_MINOR,
130 "nvram",
131 &nvram_fops
132};
133
134int __init nvram_init(void)
135{
136 int ret = 0;
137
138 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
139 NVRAM_VERSION);
140 ret = misc_register(&nvram_dev);
141 if (ret != 0)
142 goto out;
143
144 nvram_len = nvram_get_size();
145 if (nvram_len < 0)
146 nvram_len = NVRAM_SIZE;
147
148out:
149 return ret;
150}
151
152void __exit nvram_cleanup(void)
153{
154 misc_deregister( &nvram_dev );
155}
156
157module_init(nvram_init);
158module_exit(nvram_cleanup);
159MODULE_LICENSE("GPL");