Merge HEAD from master.kernel.org:/home/rmk/linux-2.6-arm.git
[linux-2.6] / drivers / char / generic_nvram.c
1 /*
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>
25
26 #define NVRAM_SIZE      8192
27
28 static loff_t nvram_llseek(struct file *file, loff_t offset, int origin)
29 {
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;
38         }
39         if (offset < 0) {
40                 unlock_kernel();
41                 return -EINVAL;
42         }
43         file->f_pos = offset;
44         unlock_kernel();
45         return file->f_pos;
46 }
47
48 static ssize_t read_nvram(struct file *file, char __user *buf,
49                           size_t count, loff_t *ppos)
50 {
51         unsigned int i;
52         char __user *p = buf;
53
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;
63 }
64
65 static ssize_t write_nvram(struct file *file, const char __user *buf,
66                            size_t count, loff_t *ppos)
67 {
68         unsigned int i;
69         const char __user *p = buf;
70         char c;
71
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);
80         }
81         *ppos = i;
82         return p - buf;
83 }
84
85 static int nvram_ioctl(struct inode *inode, struct file *file,
86         unsigned int cmd, unsigned long arg)
87 {
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;
94
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;
105         }
106 #endif /* CONFIG_PPC_PMAC */
107         case IOC_NVRAM_SYNC:
108                 nvram_sync();
109                 break;
110         default:
111                 return -EINVAL;
112         }
113
114         return 0;
115 }
116
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,
123 };
124
125 static struct miscdevice nvram_dev = {
126         NVRAM_MINOR,
127         "nvram",
128         &nvram_fops
129 };
130
131 int __init nvram_init(void)
132 {
133         printk(KERN_INFO "Macintosh non-volatile memory driver v%s\n",
134                 NVRAM_VERSION);
135         return misc_register(&nvram_dev);
136 }
137
138 void __exit nvram_cleanup(void)
139 {
140         misc_deregister( &nvram_dev );
141 }
142
143 module_init(nvram_init);
144 module_exit(nvram_cleanup);
145 MODULE_LICENSE("GPL");