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/module.h>
10 #include <linux/miscdevice.h>
11 #include <linux/delay.h>
12 #include <asm/uaccess.h>
16 * core module and version information
18 #define RNG_VERSION "1.0.0"
19 #define RNG_MODULE_NAME "random"
21 #define RNG_MISCDEV_MINOR 183 /* official */
23 static int random_fd
= -1;
25 static int rng_dev_open (struct inode
*inode
, struct file
*filp
)
27 /* enforce read-only access to this chrdev */
28 if ((filp
->f_mode
& FMODE_READ
) == 0)
30 if (filp
->f_mode
& FMODE_WRITE
)
36 static ssize_t
rng_dev_read (struct file
*filp
, char __user
*buf
, size_t size
,
40 int n
, ret
= 0, have_data
;
43 n
= os_read_file(random_fd
, &data
, sizeof(data
));
46 while (have_data
&& size
) {
47 if (put_user((u8
)data
, buf
++)) {
48 ret
= ret
? : -EFAULT
;
57 else if(n
== -EAGAIN
){
58 if (filp
->f_flags
& O_NONBLOCK
)
59 return ret
? : -EAGAIN
;
62 current
->state
= TASK_INTERRUPTIBLE
;
67 if (signal_pending (current
))
68 return ret
? : -ERESTARTSYS
;
73 static struct file_operations rng_chrdev_ops
= {
79 static struct miscdevice rng_miscdev
= {
86 * rng_init - initialize RNG module
88 static int __init
rng_init (void)
92 err
= os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
98 err
= os_set_fd_block(random_fd
, 0);
100 goto err_out_cleanup_hw
;
102 err
= misc_register (&rng_miscdev
);
104 printk (KERN_ERR RNG_MODULE_NAME
": misc device register failed\n");
105 goto err_out_cleanup_hw
;
117 * rng_cleanup - shutdown RNG module
119 static void __exit
rng_cleanup (void)
121 misc_deregister (&rng_miscdev
);
124 module_init (rng_init
);
125 module_exit (rng_cleanup
);
127 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
128 MODULE_LICENSE("GPL");