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/interrupt.h>
12 #include <linux/miscdevice.h>
13 #include <linux/delay.h>
14 #include <asm/uaccess.h>
19 * core module and version information
21 #define RNG_VERSION "1.0.0"
22 #define RNG_MODULE_NAME "hw_random"
24 #define RNG_MISCDEV_MINOR 183 /* official */
26 /* Changed at init time, in the non-modular case, and at module load
27 * time, in the module case. Presumably, the module subsystem
28 * protects against a module being loaded twice at the same time.
30 static int random_fd = -1;
31 static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
33 static int rng_dev_open (struct inode *inode, struct file *filp)
35 /* enforce read-only access to this chrdev */
36 if ((filp->f_mode & FMODE_READ) == 0)
38 if (filp->f_mode & FMODE_WRITE)
44 static atomic_t host_sleep_count = ATOMIC_INIT(0);
46 static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
50 int n, ret = 0, have_data;
53 n = os_read_file(random_fd, &data, sizeof(data));
56 while (have_data && size) {
57 if (put_user((u8)data, buf++)) {
58 ret = ret ? : -EFAULT;
67 else if(n == -EAGAIN){
68 DECLARE_WAITQUEUE(wait, current);
70 if (filp->f_flags & O_NONBLOCK)
71 return ret ? : -EAGAIN;
73 atomic_inc(&host_sleep_count);
74 reactivate_fd(random_fd, RANDOM_IRQ);
75 add_sigio_fd(random_fd);
77 add_wait_queue(&host_read_wait, &wait);
78 set_task_state(current, TASK_INTERRUPTIBLE);
81 set_task_state(current, TASK_RUNNING);
82 remove_wait_queue(&host_read_wait, &wait);
84 if (atomic_dec_and_test(&host_sleep_count)) {
85 ignore_sigio_fd(random_fd);
86 deactivate_fd(random_fd, RANDOM_IRQ);
90 if (signal_pending (current))
91 return ret ? : -ERESTARTSYS;
96 static const struct file_operations rng_chrdev_ops = {
102 /* rng_init shouldn't be called more than once at boot time */
103 static struct miscdevice rng_miscdev = {
109 static irqreturn_t random_interrupt(int irq, void *data)
111 wake_up(&host_read_wait);
117 * rng_init - initialize RNG module
119 static int __init rng_init (void)
123 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
129 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
130 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random",
133 goto err_out_cleanup_hw;
135 sigio_broken(random_fd, 1);
137 err = misc_register (&rng_miscdev);
139 printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n");
140 goto err_out_cleanup_hw;
147 os_close_file(random_fd);
153 * rng_cleanup - shutdown RNG module
155 static void __exit rng_cleanup (void)
157 os_close_file(random_fd);
158 misc_deregister (&rng_miscdev);
161 module_init (rng_init);
162 module_exit (rng_cleanup);
164 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
165 MODULE_LICENSE("GPL");