1 /* Copyright (C) 2005 Jeff Dike <jdike@addtoit.com> */
2 /* Much of this ripped from drivers/char/hw_random.c, see there for other
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 #include <linux/sched.h>
9 #include <linux/module.h>
11 #include <linux/miscdevice.h>
12 #include <linux/delay.h>
13 #include <asm/uaccess.h>
17 * core module and version information
19 #define RNG_VERSION "1.0.0"
20 #define RNG_MODULE_NAME "random"
22 #define RNG_MISCDEV_MINOR 183 /* official */
24 /* Changed at init time, in the non-modular case, and at module load
25 * time, in the module case. Presumably, the module subsystem
26 * protects against a module being loaded twice at the same time.
28 static int random_fd = -1;
30 static int rng_dev_open (struct inode *inode, struct file *filp)
32 /* enforce read-only access to this chrdev */
33 if ((filp->f_mode & FMODE_READ) == 0)
35 if (filp->f_mode & FMODE_WRITE)
41 static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
45 int n, ret = 0, have_data;
48 n = os_read_file(random_fd, &data, sizeof(data));
51 while (have_data && size) {
52 if (put_user((u8)data, buf++)) {
53 ret = ret ? : -EFAULT;
62 else if(n == -EAGAIN){
63 if (filp->f_flags & O_NONBLOCK)
64 return ret ? : -EAGAIN;
67 schedule_timeout_interruptible(1);
70 if (signal_pending (current))
71 return ret ? : -ERESTARTSYS;
76 static const struct file_operations rng_chrdev_ops = {
82 /* rng_init shouldn't be called more than once at boot time */
83 static struct miscdevice rng_miscdev = {
90 * rng_init - initialize RNG module
92 static int __init rng_init (void)
96 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
102 err = os_set_fd_block(random_fd, 0);
104 goto err_out_cleanup_hw;
106 err = misc_register (&rng_miscdev);
108 printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n");
109 goto err_out_cleanup_hw;
121 * rng_cleanup - shutdown RNG module
123 static void __exit rng_cleanup (void)
125 misc_deregister (&rng_miscdev);
128 module_init (rng_init);
129 module_exit (rng_cleanup);
131 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
132 MODULE_LICENSE("GPL");