Staging: wlags49: fix kconfigs dependancy
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / generic_nvram.c
blobfda4181b5e67af6f84927ed8807cba101deb8544
1 /*
2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
5 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
7 * Note that an additional hook is supported for PowerMac only
8 * for getting the nvram "partition" informations
12 #define NVRAM_VERSION "1.1"
14 #include <linux/module.h>
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 <asm/uaccess.h>
23 #include <asm/nvram.h>
24 #ifdef CONFIG_PPC_PMAC
25 #include <asm/machdep.h>
26 #endif
28 #define NVRAM_SIZE 8192
30 static ssize_t nvram_len;
32 static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
34 switch (origin) {
35 case 1:
36 offset += file->f_pos;
37 break;
38 case 2:
39 offset += nvram_len;
40 break;
42 if (offset < 0)
43 return -EINVAL;
45 file->f_pos = offset;
47 return file->f_pos;
50 static ssize_t read_nvram(struct file *file, char __user *buf,
51 size_t count, loff_t *ppos)
53 unsigned int i;
54 char __user *p = buf;
56 if (!access_ok(VERIFY_WRITE, buf, count))
57 return -EFAULT;
58 if (*ppos >= nvram_len)
59 return 0;
60 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
61 if (__put_user(nvram_read_byte(i), p))
62 return -EFAULT;
63 *ppos = i;
64 return p - buf;
67 static ssize_t write_nvram(struct file *file, const char __user *buf,
68 size_t count, loff_t *ppos)
70 unsigned int i;
71 const char __user *p = buf;
72 char c;
74 if (!access_ok(VERIFY_READ, buf, count))
75 return -EFAULT;
76 if (*ppos >= nvram_len)
77 return 0;
78 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
79 if (__get_user(c, p))
80 return -EFAULT;
81 nvram_write_byte(c, i);
83 *ppos = i;
84 return p - buf;
87 static int nvram_ioctl(struct inode *inode, struct file *file,
88 unsigned int cmd, unsigned long arg)
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;
97 if (!machine_is(powermac))
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;
108 #endif /* CONFIG_PPC_PMAC */
109 case IOC_NVRAM_SYNC:
110 nvram_sync();
111 break;
112 default:
113 return -EINVAL;
116 return 0;
119 const struct file_operations nvram_fops = {
120 .owner = THIS_MODULE,
121 .llseek = nvram_llseek,
122 .read = read_nvram,
123 .write = write_nvram,
124 .ioctl = nvram_ioctl,
127 static struct miscdevice nvram_dev = {
128 NVRAM_MINOR,
129 "nvram",
130 &nvram_fops
133 int __init nvram_init(void)
135 int ret = 0;
137 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
138 NVRAM_VERSION);
139 ret = misc_register(&nvram_dev);
140 if (ret != 0)
141 goto out;
143 nvram_len = nvram_get_size();
144 if (nvram_len < 0)
145 nvram_len = NVRAM_SIZE;
147 out:
148 return ret;
151 void __exit nvram_cleanup(void)
153 misc_deregister( &nvram_dev );
156 module_init(nvram_init);
157 module_exit(nvram_cleanup);
158 MODULE_LICENSE("GPL");