blob: a00869c650d5b31a67f0c8d2675435dd6eeb1ebe [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 *
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>
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
31static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
32{
33 lock_kernel();
34 switch (origin) {
35 case 1:
36 offset += file->f_pos;
37 break;
38 case 2:
39 offset += NVRAM_SIZE;
40 break;
41 }
42 if (offset < 0) {
43 unlock_kernel();
44 return -EINVAL;
45 }
46 file->f_pos = offset;
47 unlock_kernel();
48 return file->f_pos;
49}
50
51static ssize_t read_nvram(struct file *file, char __user *buf,
52 size_t count, loff_t *ppos)
53{
54 unsigned int i;
55 char __user *p = buf;
56
57 if (!access_ok(VERIFY_WRITE, buf, count))
58 return -EFAULT;
59 if (*ppos >= NVRAM_SIZE)
60 return 0;
61 for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
62 if (__put_user(nvram_read_byte(i), p))
63 return -EFAULT;
64 *ppos = i;
65 return p - buf;
66}
67
68static ssize_t write_nvram(struct file *file, const char __user *buf,
69 size_t count, loff_t *ppos)
70{
71 unsigned int i;
72 const char __user *p = buf;
73 char c;
74
75 if (!access_ok(VERIFY_READ, buf, count))
76 return -EFAULT;
77 if (*ppos >= NVRAM_SIZE)
78 return 0;
79 for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
80 if (__get_user(c, p))
81 return -EFAULT;
82 nvram_write_byte(c, i);
83 }
84 *ppos = i;
85 return p - buf;
86}
87
88static int nvram_ioctl(struct inode *inode, struct file *file,
89 unsigned int cmd, unsigned long arg)
90{
91 switch(cmd) {
92#ifdef CONFIG_PPC_PMAC
93 case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
94 printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
95 case IOC_NVRAM_GET_OFFSET: {
96 int part, offset;
97
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110098 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
100 if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
101 return -EFAULT;
102 if (part < pmac_nvram_OF || part > pmac_nvram_NR)
103 return -EINVAL;
104 offset = pmac_get_partition(part);
105 if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
106 return -EFAULT;
107 break;
108 }
109#endif /* CONFIG_PPC_PMAC */
110 case IOC_NVRAM_SYNC:
111 nvram_sync();
112 break;
113 default:
114 return -EINVAL;
115 }
116
117 return 0;
118}
119
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800120const struct file_operations nvram_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .owner = THIS_MODULE,
122 .llseek = nvram_llseek,
123 .read = read_nvram,
124 .write = write_nvram,
125 .ioctl = nvram_ioctl,
126};
127
128static struct miscdevice nvram_dev = {
129 NVRAM_MINOR,
130 "nvram",
131 &nvram_fops
132};
133
134int __init nvram_init(void)
135{
Philippe De Muytercfc53f62008-06-12 15:21:46 -0700136 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 NVRAM_VERSION);
138 return misc_register(&nvram_dev);
139}
140
141void __exit nvram_cleanup(void)
142{
143 misc_deregister( &nvram_dev );
144}
145
146module_init(nvram_init);
147module_exit(nvram_cleanup);
148MODULE_LICENSE("GPL");