File: /usr/src/linux/drivers/macintosh/nvram.c

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     
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     
37     static ssize_t read_nvram(struct file *file, char *buf,
38     			  size_t count, loff_t *ppos)
39     {
40     	unsigned int i;
41     	char *p = buf;
42     
43     	if (verify_area(VERIFY_WRITE, buf, count))
44     		return -EFAULT;
45     	if (*ppos >= NVRAM_SIZE)
46     		return 0;
47     	for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count)
48     		if (__put_user(nvram_read_byte(i), p))
49     			return -EFAULT;
50     	*ppos = i;
51     	return p - buf;
52     }
53     
54     static ssize_t write_nvram(struct file *file, const char *buf,
55     			   size_t count, loff_t *ppos)
56     {
57     	unsigned int i;
58     	const char *p = buf;
59     	char c;
60     
61     	if (verify_area(VERIFY_READ, buf, count))
62     		return -EFAULT;
63     	if (*ppos >= NVRAM_SIZE)
64     		return 0;
65     	for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) {
66     		if (__get_user(c, p))
67     			return -EFAULT;
68     		nvram_write_byte(c, i);
69     	}
70     	*ppos = i;
71     	return p - buf;
72     }
73     
74     static int nvram_ioctl(struct inode *inode, struct file *file,
75     	unsigned int cmd, unsigned long arg)
76     {
77     	switch(cmd) {
78     		case PMAC_NVRAM_GET_OFFSET:
79     		{
80     			int part, offset;
81     			if (copy_from_user(&part,(void*)arg,sizeof(part))!=0)
82     				return -EFAULT;
83     			if (part < pmac_nvram_OF || part > pmac_nvram_NR)
84     				return -EINVAL;
85     			offset = pmac_get_partition(part);
86     			if (copy_to_user((void*)arg,&offset,sizeof(offset))!=0)
87     				return -EFAULT;
88     			break;
89     		}
90     
91     		default:
92     			return -EINVAL;
93     	}
94     
95     	return 0;
96     }
97     
98     struct file_operations nvram_fops = {
99     	owner:		THIS_MODULE,
100     	llseek:		nvram_llseek,
101     	read:		read_nvram,
102     	write:		write_nvram,
103     	ioctl:		nvram_ioctl,
104     };
105     
106     static struct miscdevice nvram_dev = {
107     	NVRAM_MINOR,
108     	"nvram",
109     	&nvram_fops
110     };
111     
112     int __init nvram_init(void)
113     {
114     	printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
115     		NVRAM_VERSION);
116     	misc_register(&nvram_dev);
117     	return 0;
118     }
119     
120     void __exit nvram_cleanup(void)
121     {
122             misc_deregister( &nvram_dev );
123     }
124     
125     module_init(nvram_init);
126     module_exit(nvram_cleanup);
127