1 /*
2  * /dev/nvram driver for Power Macintosh.
3  */
4 
5 #define NVRAM_VERSION "1.0"
6 
7 #include <linux/module.h>
8 
9 #include <linux/types.h>
10 #include <linux/errno.h>
11 #include <linux/fs.h>
12 #include <linux/miscdevice.h>
13 #include <linux/fcntl.h>
14 #include <linux/nvram.h>
15 #include <linux/init.h>
16 #include <asm/uaccess.h>
17 #include <asm/nvram.h>
18 
19 #define NVRAM_SIZE	8192
20 
nvram_llseek(struct file * file,loff_t offset,int origin)21 static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
22 {
23 	switch (origin) {
24 	case 1:
25 		offset += file->f_pos;
26 		break;
27 	case 2:
28 		offset += NVRAM_SIZE;
29 		break;
30 	}
31 	if (offset < 0)
32 		return -EINVAL;
33 	file->f_pos = offset;
34 	return file->f_pos;
35 }
36 
read_nvram(struct file * file,char * buf,size_t count,loff_t * ppos)37 static ssize_t read_nvram(struct file *file, char *buf,
38 			  size_t count, loff_t *ppos)
39 {
40 	loff_t n = *ppos;
41 	unsigned int i = n;
42 	char *p = buf;
43 
44 	if (verify_area(VERIFY_WRITE, buf, count))
45 		return -EFAULT;
46 	if (i != n || i >= NVRAM_SIZE)
47 		return 0;
48 	for (; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
49 		if (__put_user(nvram_read_byte(i), p))
50 			return -EFAULT;
51 	*ppos = i;
52 	return p - buf;
53 }
54 
write_nvram(struct file * file,const char * buf,size_t count,loff_t * ppos)55 static ssize_t write_nvram(struct file *file, const char *buf,
56 			   size_t count, loff_t *ppos)
57 {
58 	loff_t n = *ppos;
59 	unsigned int i = n;
60 	const char *p = buf;
61 	char c;
62 
63 	if (verify_area(VERIFY_READ, buf, count))
64 		return -EFAULT;
65 	if (i != n || i >= NVRAM_SIZE)
66 		return 0;
67 	for (; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
68 		if (__get_user(c, p))
69 			return -EFAULT;
70 		nvram_write_byte(c, i);
71 	}
72 	*ppos = i;
73 	return p - buf;
74 }
75 
nvram_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)76 static int nvram_ioctl(struct inode *inode, struct file *file,
77 	unsigned int cmd, unsigned long arg)
78 {
79 	switch(cmd) {
80 		case PMAC_NVRAM_GET_OFFSET:
81 		{
82 			int part, offset;
83 			if (copy_from_user(&part,(void*)arg,sizeof(part))!=0)
84 				return -EFAULT;
85 			if (part < pmac_nvram_OF || part > pmac_nvram_NR)
86 				return -EINVAL;
87 			offset = pmac_get_partition(part);
88 			if (copy_to_user((void*)arg,&offset,sizeof(offset))!=0)
89 				return -EFAULT;
90 			break;
91 		}
92 
93 		default:
94 			return -EINVAL;
95 	}
96 
97 	return 0;
98 }
99 
100 struct file_operations nvram_fops = {
101 	owner:		THIS_MODULE,
102 	llseek:		nvram_llseek,
103 	read:		read_nvram,
104 	write:		write_nvram,
105 	ioctl:		nvram_ioctl,
106 };
107 
108 static struct miscdevice nvram_dev = {
109 	NVRAM_MINOR,
110 	"nvram",
111 	&nvram_fops
112 };
113 
nvram_init(void)114 int __init nvram_init(void)
115 {
116 	printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
117 		NVRAM_VERSION);
118 	return misc_register(&nvram_dev);
119 }
120 
nvram_cleanup(void)121 void __exit nvram_cleanup(void)
122 {
123         misc_deregister( &nvram_dev );
124 }
125 
126 module_init(nvram_init);
127 module_exit(nvram_cleanup);
128 MODULE_LICENSE("GPL");
129