xref: /openbmc/linux/arch/um/drivers/random.c (revision 74be2d3b)
1 /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
2 
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  */
9 #include <linux/sched/signal.h>
10 #include <linux/module.h>
11 #include <linux/fs.h>
12 #include <linux/interrupt.h>
13 #include <linux/miscdevice.h>
14 #include <linux/delay.h>
15 #include <linux/uaccess.h>
16 #include <init.h>
17 #include <irq_kern.h>
18 #include <os.h>
19 
20 /*
21  * core module and version information
22  */
23 #define RNG_VERSION "1.0.0"
24 #define RNG_MODULE_NAME "hw_random"
25 
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  */
30 static int random_fd = -1;
31 static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
32 
33 static int rng_dev_open (struct inode *inode, struct file *filp)
34 {
35 	/* enforce read-only access to this chrdev */
36 	if ((filp->f_mode & FMODE_READ) == 0)
37 		return -EINVAL;
38 	if ((filp->f_mode & FMODE_WRITE) != 0)
39 		return -EINVAL;
40 
41 	return 0;
42 }
43 
44 static atomic_t host_sleep_count = ATOMIC_INIT(0);
45 
46 static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
47 			     loff_t *offp)
48 {
49 	u32 data;
50 	int n, ret = 0, have_data;
51 
52 	while (size) {
53 		n = os_read_file(random_fd, &data, sizeof(data));
54 		if (n > 0) {
55 			have_data = n;
56 			while (have_data && size) {
57 				if (put_user((u8) data, buf++)) {
58 					ret = ret ? : -EFAULT;
59 					break;
60 				}
61 				size--;
62 				ret++;
63 				have_data--;
64 				data >>= 8;
65 			}
66 		}
67 		else if (n == -EAGAIN) {
68 			DECLARE_WAITQUEUE(wait, current);
69 
70 			if (filp->f_flags & O_NONBLOCK)
71 				return ret ? : -EAGAIN;
72 
73 			atomic_inc(&host_sleep_count);
74 			add_sigio_fd(random_fd);
75 
76 			add_wait_queue(&host_read_wait, &wait);
77 			set_current_state(TASK_INTERRUPTIBLE);
78 
79 			schedule();
80 			remove_wait_queue(&host_read_wait, &wait);
81 
82 			if (atomic_dec_and_test(&host_sleep_count)) {
83 				ignore_sigio_fd(random_fd);
84 				deactivate_fd(random_fd, RANDOM_IRQ);
85 			}
86 		}
87 		else
88 			return n;
89 
90 		if (signal_pending (current))
91 			return ret ? : -ERESTARTSYS;
92 	}
93 	return ret;
94 }
95 
96 static const struct file_operations rng_chrdev_ops = {
97 	.owner		= THIS_MODULE,
98 	.open		= rng_dev_open,
99 	.read		= rng_dev_read,
100 	.llseek		= noop_llseek,
101 };
102 
103 /* rng_init shouldn't be called more than once at boot time */
104 static struct miscdevice rng_miscdev = {
105 	HWRNG_MINOR,
106 	RNG_MODULE_NAME,
107 	&rng_chrdev_ops,
108 };
109 
110 static irqreturn_t random_interrupt(int irq, void *data)
111 {
112 	wake_up(&host_read_wait);
113 
114 	return IRQ_HANDLED;
115 }
116 
117 /*
118  * rng_init - initialize RNG module
119  */
120 static int __init rng_init (void)
121 {
122 	int err;
123 
124 	err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
125 	if (err < 0)
126 		goto out;
127 
128 	random_fd = err;
129 
130 	err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
131 			     0, "random", NULL);
132 	if (err)
133 		goto err_out_cleanup_hw;
134 
135 	sigio_broken(random_fd, 1);
136 
137 	err = misc_register (&rng_miscdev);
138 	if (err) {
139 		printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
140 			"failed\n");
141 		goto err_out_cleanup_hw;
142 	}
143 out:
144 	return err;
145 
146 err_out_cleanup_hw:
147 	os_close_file(random_fd);
148 	random_fd = -1;
149 	goto out;
150 }
151 
152 /*
153  * rng_cleanup - shutdown RNG module
154  */
155 
156 static void cleanup(void)
157 {
158 	free_irq_by_fd(random_fd);
159 	os_close_file(random_fd);
160 }
161 
162 static void __exit rng_cleanup(void)
163 {
164 	os_close_file(random_fd);
165 	misc_deregister (&rng_miscdev);
166 }
167 
168 module_init (rng_init);
169 module_exit (rng_cleanup);
170 __uml_exitcall(cleanup);
171 
172 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
173 MODULE_LICENSE("GPL");
174