aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/generic_nvram.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /drivers/char/generic_nvram.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'drivers/char/generic_nvram.c')
-rw-r--r--drivers/char/generic_nvram.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/drivers/char/generic_nvram.c b/drivers/char/generic_nvram.c
new file mode 100644
index 000000000000..1b5e01e6e129
--- /dev/null
+++ b/drivers/char/generic_nvram.c
@@ -0,0 +1,145 @@
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
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>
25
26#define NVRAM_SIZE 8192
27
28static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
29{
30 lock_kernel();
31 switch (origin) {
32 case 1:
33 offset += file->f_pos;
34 break;
35 case 2:
36 offset += NVRAM_SIZE;
37 break;
38 }
39 if (offset < 0) {
40 unlock_kernel();
41 return -EINVAL;
42 }
43 file->f_pos = offset;
44 unlock_kernel();
45 return file->f_pos;
46}
47
48static ssize_t read_nvram(struct file *file, char __user *buf,
49 size_t count, loff_t *ppos)
50{
51 unsigned int i;
52 char __user *p = buf;
53
54 if (!access_ok(VERIFY_WRITE, buf, count))
55 return -EFAULT;
56 if (*ppos >= NVRAM_SIZE)
57 return 0;
58 for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
59 if (__put_user(nvram_read_byte(i), p))
60 return -EFAULT;
61 *ppos = i;
62 return p - buf;
63}
64
65static ssize_t write_nvram(struct file *file, const char __user *buf,
66 size_t count, loff_t *ppos)
67{
68 unsigned int i;
69 const char __user *p = buf;
70 char c;
71
72 if (!access_ok(VERIFY_READ, buf, count))
73 return -EFAULT;
74 if (*ppos >= NVRAM_SIZE)
75 return 0;
76 for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
77 if (__get_user(c, p))
78 return -EFAULT;
79 nvram_write_byte(c, i);
80 }
81 *ppos = i;
82 return p - buf;
83}
84
85static int nvram_ioctl(struct inode *inode, struct file *file,
86 unsigned int cmd, unsigned long arg)
87{
88 switch(cmd) {
89#ifdef CONFIG_PPC_PMAC
90 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
91 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
92 case IOC_NVRAM_GET_OFFSET: {
93 int part, offset;
94
95 if (_machine != _MACH_Pmac)
96 return -EINVAL;
97 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
98 return -EFAULT;
99 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
100 return -EINVAL;
101 offset = pmac_get_partition(part);
102 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
103 return -EFAULT;
104 break;
105 }
106#endif /* CONFIG_PPC_PMAC */
107 case IOC_NVRAM_SYNC:
108 nvram_sync();
109 break;
110 default:
111 return -EINVAL;
112 }
113
114 return 0;
115}
116
117struct file_operations nvram_fops = {
118 .owner = THIS_MODULE,
119 .llseek = nvram_llseek,
120 .read = read_nvram,
121 .write = write_nvram,
122 .ioctl = nvram_ioctl,
123};
124
125static struct miscdevice nvram_dev = {
126 NVRAM_MINOR,
127 "nvram",
128 &nvram_fops
129};
130
131int __init nvram_init(void)
132{
133 printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
134 NVRAM_VERSION);
135 return misc_register(&nvram_dev);
136}
137
138void __exit nvram_cleanup(void)
139{
140 misc_deregister( &nvram_dev );
141}
142
143module_init(nvram_init);
144module_exit(nvram_cleanup);
145MODULE_LICENSE("GPL");