Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / char / generic_nvram.c
blob1b5e01e6e129994e36b285ee29a842c241fd0c70
1 /*
2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
5 * nvram_read_byte, nvram_write_byte, nvram_sync
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 <linux/smp_lock.h>
23 #include <asm/uaccess.h>
24 #include <asm/nvram.h>
26 #define NVRAM_SIZE 8192
28 static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
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;
39 if (offset < 0) {
40 unlock_kernel();
41 return -EINVAL;
43 file->f_pos = offset;
44 unlock_kernel();
45 return file->f_pos;
48 static ssize_t read_nvram(struct file *file, char __user *buf,
49 size_t count, loff_t *ppos)
51 unsigned int i;
52 char __user *p = buf;
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;
65 static ssize_t write_nvram(struct file *file, const char __user *buf,
66 size_t count, loff_t *ppos)
68 unsigned int i;
69 const char __user *p = buf;
70 char c;
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);
81 *ppos = i;
82 return p - buf;
85 static int nvram_ioctl(struct inode *inode, struct file *file,
86 unsigned int cmd, unsigned long arg)
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;
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;
106 #endif /* CONFIG_PPC_PMAC */
107 case IOC_NVRAM_SYNC:
108 nvram_sync();
109 break;
110 default:
111 return -EINVAL;
114 return 0;
117 struct file_operations nvram_fops = {
118 .owner = THIS_MODULE,
119 .llseek = nvram_llseek,
120 .read = read_nvram,
121 .write = write_nvram,
122 .ioctl = nvram_ioctl,
125 static struct miscdevice nvram_dev = {
126 NVRAM_MINOR,
127 "nvram",
128 &nvram_fops
131 int __init nvram_init(void)
133 printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
134 NVRAM_VERSION);
135 return misc_register(&nvram_dev);
138 void __exit nvram_cleanup(void)
140 misc_deregister( &nvram_dev );
143 module_init(nvram_init);
144 module_exit(nvram_cleanup);
145 MODULE_LICENSE("GPL");