]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - arch/um/drivers/random.c
Merge tag 'x86_urgent_for_v5.15_rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / arch / um / drivers / random.c
CommitLineData
3d88958e
JD
1/* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
2
567b5650
PBG
3/* Much of this ripped from drivers/char/hw_random.c, see there for other
4 * copyright.
5 *
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License, incorporated herein by reference.
8 */
174cd4b1 9#include <linux/sched/signal.h>
1da177e4
LT
10#include <linux/module.h>
11#include <linux/fs.h>
5d33e4d7 12#include <linux/interrupt.h>
1da177e4 13#include <linux/miscdevice.h>
72d3e093 14#include <linux/hw_random.h>
1da177e4 15#include <linux/delay.h>
7c0f6ba6 16#include <linux/uaccess.h>
ff6a1798 17#include <init.h>
37185b33
AV
18#include <irq_kern.h>
19#include <os.h>
1da177e4
LT
20
21/*
72d3e093 22 * core module information
1da177e4 23 */
5d33e4d7 24#define RNG_MODULE_NAME "hw_random"
1da177e4 25
730760e9
JD
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.
29 */
1da177e4 30static int random_fd = -1;
72d3e093
CO
31static struct hwrng hwrng = { 0, };
32static DECLARE_COMPLETION(have_data);
1da177e4 33
72d3e093 34static int rng_dev_read(struct hwrng *rng, void *buf, size_t max, bool block)
1da177e4 35{
72d3e093 36 int ret;
1da177e4 37
72d3e093
CO
38 for (;;) {
39 ret = os_read_file(random_fd, buf, max);
40 if (block && ret == -EAGAIN) {
5d33e4d7
JD
41 add_sigio_fd(random_fd);
42
72d3e093 43 ret = wait_for_completion_killable(&have_data);
5d33e4d7 44
72d3e093
CO
45 ignore_sigio_fd(random_fd);
46 deactivate_fd(random_fd, RANDOM_IRQ);
5d33e4d7 47
72d3e093
CO
48 if (ret < 0)
49 break;
50 } else {
51 break;
3d88958e 52 }
1da177e4 53 }
1da177e4 54
72d3e093
CO
55 return ret != -EAGAIN ? ret : 0;
56}
1da177e4 57
5d33e4d7
JD
58static irqreturn_t random_interrupt(int irq, void *data)
59{
72d3e093 60 complete(&have_data);
5d33e4d7
JD
61
62 return IRQ_HANDLED;
63}
64
1da177e4
LT
65/*
66 * rng_init - initialize RNG module
67 */
68static int __init rng_init (void)
69{
70 int err;
71
3d88958e
JD
72 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
73 if (err < 0)
74 goto out;
1da177e4 75
3d88958e 76 random_fd = err;
5d33e4d7 77 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
aab94460 78 0, "random", NULL);
36d46a59 79 if (err < 0)
1da177e4
LT
80 goto err_out_cleanup_hw;
81
2fccfcc0 82 sigio_broken(random_fd);
72d3e093
CO
83 hwrng.name = RNG_MODULE_NAME;
84 hwrng.read = rng_dev_read;
85 hwrng.quality = 1024;
5d33e4d7 86
72d3e093 87 err = hwrng_register(&hwrng);
1da177e4 88 if (err) {
72d3e093 89 pr_err(RNG_MODULE_NAME " registering failed (%d)\n", err);
1da177e4
LT
90 goto err_out_cleanup_hw;
91 }
3d88958e
JD
92out:
93 return err;
1da177e4 94
3d88958e 95err_out_cleanup_hw:
5d33e4d7 96 os_close_file(random_fd);
3d88958e
JD
97 random_fd = -1;
98 goto out;
1da177e4
LT
99}
100
101/*
102 * rng_cleanup - shutdown RNG module
103 */
ff6a1798
AI
104
105static void cleanup(void)
106{
107 free_irq_by_fd(random_fd);
108 os_close_file(random_fd);
109}
110
111static void __exit rng_cleanup(void)
1da177e4 112{
72d3e093 113 hwrng_unregister(&hwrng);
5d33e4d7 114 os_close_file(random_fd);
1da177e4
LT
115}
116
117module_init (rng_init);
118module_exit (rng_cleanup);
ff6a1798 119__uml_exitcall(cleanup);
567b5650
PBG
120
121MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
122MODULE_LICENSE("GPL");