blob: fda4181b5e67af6f84927ed8807cba101deb8544 [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 *
Martyn Welchd331d832009-08-13 09:03:02 +01005 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include <asm/nvram.h>
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110024#ifdef CONFIG_PPC_PMAC
25#include <asm/machdep.h>
26#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#define NVRAM_SIZE 8192
29
Martyn Welchd331d832009-08-13 09:03:02 +010030static ssize_t nvram_len;
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
33{
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 switch (origin) {
35 case 1:
36 offset += file->f_pos;
37 break;
38 case 2:
Martyn Welchd331d832009-08-13 09:03:02 +010039 offset += nvram_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 break;
41 }
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020042 if (offset < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 return -EINVAL;
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 file->f_pos = offset;
Frederic Weisbecker6783b9c2009-10-09 21:20:30 +020046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 return file->f_pos;
48}
49
50static ssize_t read_nvram(struct file *file, char __user *buf,
51 size_t count, loff_t *ppos)
52{
53 unsigned int i;
54 char __user *p = buf;
55
56 if (!access_ok(VERIFY_WRITE, buf, count))
57 return -EFAULT;
Martyn Welchd331d832009-08-13 09:03:02 +010058 if (*ppos >= nvram_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
Martyn Welchd331d832009-08-13 09:03:02 +010060 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (__put_user(nvram_read_byte(i), p))
62 return -EFAULT;
63 *ppos = i;
64 return p - buf;
65}
66
67static ssize_t write_nvram(struct file *file, const char __user *buf,
68 size_t count, loff_t *ppos)
69{
70 unsigned int i;
71 const char __user *p = buf;
72 char c;
73
74 if (!access_ok(VERIFY_READ, buf, count))
75 return -EFAULT;
Martyn Welchd331d832009-08-13 09:03:02 +010076 if (*ppos >= nvram_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
Martyn Welchd331d832009-08-13 09:03:02 +010078 for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (__get_user(c, p))
80 return -EFAULT;
81 nvram_write_byte(c, i);
82 }
83 *ppos = i;
84 return p - buf;
85}
86
87static int nvram_ioctl(struct inode *inode, struct file *file,
88 unsigned int cmd, unsigned long arg)
89{
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;
96
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +110097 if (!machine_is(powermac))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 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;
107 }
108#endif /* CONFIG_PPC_PMAC */
109 case IOC_NVRAM_SYNC:
110 nvram_sync();
111 break;
112 default:
113 return -EINVAL;
114 }
115
116 return 0;
117}
118
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800119const struct file_operations nvram_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 .owner = THIS_MODULE,
121 .llseek = nvram_llseek,
122 .read = read_nvram,
123 .write = write_nvram,
124 .ioctl = nvram_ioctl,
125};
126
127static struct miscdevice nvram_dev = {
128 NVRAM_MINOR,
129 "nvram",
130 &nvram_fops
131};
132
133int __init nvram_init(void)
134{
Martyn Welchd331d832009-08-13 09:03:02 +0100135 int ret = 0;
136
Philippe De Muytercfc53f62008-06-12 15:21:46 -0700137 printk(KERN_INFO "Generic non-volatile memory driver v%s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 NVRAM_VERSION);
Martyn Welchd331d832009-08-13 09:03:02 +0100139 ret = misc_register(&nvram_dev);
140 if (ret != 0)
141 goto out;
142
143 nvram_len = nvram_get_size();
144 if (nvram_len < 0)
145 nvram_len = NVRAM_SIZE;
146
147out:
148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void __exit nvram_cleanup(void)
152{
153 misc_deregister( &nvram_dev );
154}
155
156module_init(nvram_init);
157module_exit(nvram_cleanup);
158MODULE_LICENSE("GPL");