1 /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
3 /* Much of this ripped from drivers/char/hw_random.c, see there for other
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License, incorporated herein by reference.
9 #include <linux/sched/signal.h>
10 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/miscdevice.h>
14 #include <linux/hw_random.h>
15 #include <linux/delay.h>
16 #include <linux/uaccess.h>
22 * core module information
24 #define RNG_MODULE_NAME "hw_random"
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 struct hwrng hwrng;
32 static DECLARE_COMPLETION(have_data);
34 static int rng_dev_read(struct hwrng *rng, void *buf, size_t max, bool block)
39 ret = os_read_file(random_fd, buf, max);
40 if (block && ret == -EAGAIN) {
41 add_sigio_fd(random_fd);
43 ret = wait_for_completion_killable(&have_data);
45 ignore_sigio_fd(random_fd);
46 deactivate_fd(random_fd, RANDOM_IRQ);
55 return ret != -EAGAIN ? ret : 0;
58 static irqreturn_t random_interrupt(int irq, void *data)
66 * rng_init - initialize RNG module
68 static int __init rng_init (void)
72 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
77 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
80 goto err_out_cleanup_hw;
82 sigio_broken(random_fd);
83 hwrng.name = RNG_MODULE_NAME;
84 hwrng.read = rng_dev_read;
87 err = hwrng_register(&hwrng);
89 pr_err(RNG_MODULE_NAME " registering failed (%d)\n", err);
90 goto err_out_cleanup_hw;
96 os_close_file(random_fd);
102 * rng_cleanup - shutdown RNG module
105 static void cleanup(void)
107 free_irq_by_fd(random_fd);
108 os_close_file(random_fd);
111 static void __exit rng_cleanup(void)
113 hwrng_unregister(&hwrng);
114 os_close_file(random_fd);
117 module_init (rng_init);
118 module_exit (rng_cleanup);
119 __uml_exitcall(cleanup);
121 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
122 MODULE_LICENSE("GPL");