2 * Generic /dev/nvram driver for architectures providing some
3 * "generic" hooks, that is :
5 * nvram_read_byte, nvram_write_byte, nvram_sync, nvram_get_size
7 * Note that an additional hook is supported for PowerMac only
8 * for getting the nvram "partition" informations
12 #define NVRAM_VERSION "1.1"
14 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/errno.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 #ifdef CONFIG_PPC_PMAC
26 #include <asm/machdep.h>
29 #define NVRAM_SIZE 8192
31 static ssize_t nvram_len
;
33 static loff_t
nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
37 offset
+= file
->f_pos
;
51 static ssize_t
read_nvram(struct file
*file
, char __user
*buf
,
52 size_t count
, loff_t
*ppos
)
57 if (!access_ok(VERIFY_WRITE
, buf
, count
))
59 if (*ppos
>= nvram_len
)
61 for (i
= *ppos
; count
> 0 && i
< nvram_len
; ++i
, ++p
, --count
)
62 if (__put_user(nvram_read_byte(i
), p
))
68 static ssize_t
write_nvram(struct file
*file
, const char __user
*buf
,
69 size_t count
, loff_t
*ppos
)
72 const char __user
*p
= buf
;
75 if (!access_ok(VERIFY_READ
, buf
, count
))
77 if (*ppos
>= nvram_len
)
79 for (i
= *ppos
; count
> 0 && i
< nvram_len
; ++i
, ++p
, --count
) {
82 nvram_write_byte(c
, i
);
88 static int nvram_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
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
: {
97 if (!machine_is(powermac
))
99 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
101 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
103 offset
= pmac_get_partition(part
);
104 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
108 #endif /* CONFIG_PPC_PMAC */
119 static long nvram_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
124 ret
= nvram_ioctl(file
, cmd
, arg
);
130 const struct file_operations nvram_fops
= {
131 .owner
= THIS_MODULE
,
132 .llseek
= nvram_llseek
,
134 .write
= write_nvram
,
135 .unlocked_ioctl
= nvram_unlocked_ioctl
,
138 static struct miscdevice nvram_dev
= {
144 int __init
nvram_init(void)
148 printk(KERN_INFO
"Generic non-volatile memory driver v%s\n",
150 ret
= misc_register(&nvram_dev
);
154 nvram_len
= nvram_get_size();
156 nvram_len
= NVRAM_SIZE
;
162 void __exit
nvram_cleanup(void)
164 misc_deregister( &nvram_dev
);
167 module_init(nvram_init
);
168 module_exit(nvram_cleanup
);
169 MODULE_LICENSE("GPL");