1 /* Much of this ripped from hw_random.c */
3 #include <linux/module.h>
5 #include <linux/miscdevice.h>
6 #include <linux/delay.h>
7 #include <asm/uaccess.h>
11 * core module and version information
13 #define RNG_VERSION "1.0.0"
14 #define RNG_MODULE_NAME "random"
15 #define RNG_DRIVER_NAME RNG_MODULE_NAME " virtual driver " RNG_VERSION
16 #define PFX RNG_MODULE_NAME ": "
18 #define RNG_MISCDEV_MINOR 183 /* official */
20 static int random_fd = -1;
22 static int rng_dev_open (struct inode *inode, struct file *filp)
24 /* enforce read-only access to this chrdev */
25 if ((filp->f_mode & FMODE_READ) == 0)
27 if (filp->f_mode & FMODE_WRITE)
33 static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
37 int n, ret = 0, have_data;
40 n = os_read_file(random_fd, &data, sizeof(data));
43 while (have_data && size) {
44 if (put_user((u8)data, buf++)) {
45 ret = ret ? : -EFAULT;
54 else if(n == -EAGAIN){
55 if (filp->f_flags & O_NONBLOCK)
56 return ret ? : -EAGAIN;
59 current->state = TASK_INTERRUPTIBLE;
64 if (signal_pending (current))
65 return ret ? : -ERESTARTSYS;
70 static struct file_operations rng_chrdev_ops = {
76 static struct miscdevice rng_miscdev = {
83 * rng_init - initialize RNG module
85 static int __init rng_init (void)
89 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
95 err = os_set_fd_block(random_fd, 0);
97 goto err_out_cleanup_hw;
99 err = misc_register (&rng_miscdev);
101 printk (KERN_ERR PFX "misc device register failed\n");
102 goto err_out_cleanup_hw;
114 * rng_cleanup - shutdown RNG module
116 static void __exit rng_cleanup (void)
118 misc_deregister (&rng_miscdev);
121 module_init (rng_init);
122 module_exit (rng_cleanup);