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/mutex.h>
23 #include <linux/pagemap.h>
24 #include <asm/uaccess.h>
25 #include <asm/nvram.h>
26 #ifdef CONFIG_PPC_PMAC
27 #include <asm/machdep.h>
30 #define NVRAM_SIZE 8192
32 static DEFINE_MUTEX(nvram_mutex
);
33 static ssize_t nvram_len
;
35 static loff_t
nvram_llseek(struct file
*file
, loff_t offset
, int origin
)
37 return generic_file_llseek_size(file
, offset
, origin
,
38 MAX_LFS_FILESIZE
, nvram_len
);
41 static ssize_t
read_nvram(struct file
*file
, char __user
*buf
,
42 size_t count
, loff_t
*ppos
)
47 if (!access_ok(VERIFY_WRITE
, buf
, count
))
49 if (*ppos
>= nvram_len
)
51 for (i
= *ppos
; count
> 0 && i
< nvram_len
; ++i
, ++p
, --count
)
52 if (__put_user(nvram_read_byte(i
), p
))
58 static ssize_t
write_nvram(struct file
*file
, const char __user
*buf
,
59 size_t count
, loff_t
*ppos
)
62 const char __user
*p
= buf
;
65 if (!access_ok(VERIFY_READ
, buf
, count
))
67 if (*ppos
>= nvram_len
)
69 for (i
= *ppos
; count
> 0 && i
< nvram_len
; ++i
, ++p
, --count
) {
72 nvram_write_byte(c
, i
);
78 static int nvram_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
81 #ifdef CONFIG_PPC_PMAC
82 case OBSOLETE_PMAC_NVRAM_GET_OFFSET
:
83 printk(KERN_WARNING
"nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
84 case IOC_NVRAM_GET_OFFSET
: {
87 if (!machine_is(powermac
))
89 if (copy_from_user(&part
, (void __user
*)arg
, sizeof(part
)) != 0)
91 if (part
< pmac_nvram_OF
|| part
> pmac_nvram_NR
)
93 offset
= pmac_get_partition(part
);
94 if (copy_to_user((void __user
*)arg
, &offset
, sizeof(offset
)) != 0)
98 #endif /* CONFIG_PPC_PMAC */
109 static long nvram_unlocked_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
113 mutex_lock(&nvram_mutex
);
114 ret
= nvram_ioctl(file
, cmd
, arg
);
115 mutex_unlock(&nvram_mutex
);
120 const struct file_operations nvram_fops
= {
121 .owner
= THIS_MODULE
,
122 .llseek
= nvram_llseek
,
124 .write
= write_nvram
,
125 .unlocked_ioctl
= nvram_unlocked_ioctl
,
128 static struct miscdevice nvram_dev
= {
134 int __init
nvram_init(void)
138 printk(KERN_INFO
"Generic non-volatile memory driver v%s\n",
140 ret
= misc_register(&nvram_dev
);
144 nvram_len
= nvram_get_size();
146 nvram_len
= NVRAM_SIZE
;
152 void __exit
nvram_cleanup(void)
154 misc_deregister( &nvram_dev
);
157 module_init(nvram_init
);
158 module_exit(nvram_cleanup
);
159 MODULE_LICENSE("GPL");