blob: 778a0e52d5a5c618fbeebf82679060ab3e7a6520 [file] [log] [blame]
Jeff Dike3d889582008-05-12 14:01:59 -07001/* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
2
Paolo 'Blaisorblade' Giarrusso567b5652005-05-28 15:51:58 -07003/* 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 */
Ingo Molnar174cd4b2017-02-02 19:15:33 +01009#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/fs.h>
Jeff Dike5d33e4d2008-05-12 14:01:58 -070012#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/miscdevice.h>
14#include <linux/delay.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080015#include <linux/uaccess.h>
Anton Ivanovff6a1792017-11-20 21:17:58 +000016#include <init.h>
Al Viro37185b32012-10-08 03:27:32 +010017#include <irq_kern.h>
18#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * core module and version information
22 */
23#define RNG_VERSION "1.0.0"
Jeff Dike5d33e4d2008-05-12 14:01:58 -070024#define RNG_MODULE_NAME "hw_random"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define RNG_MISCDEV_MINOR 183 /* official */
27
Jeff Dike730760e2006-09-29 01:58:50 -070028/* Changed at init time, in the non-modular case, and at module load
29 * time, in the module case. Presumably, the module subsystem
30 * protects against a module being loaded twice at the same time.
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int random_fd = -1;
Jeff Dike5d33e4d2008-05-12 14:01:58 -070033static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35static int rng_dev_open (struct inode *inode, struct file *filp)
36{
37 /* enforce read-only access to this chrdev */
38 if ((filp->f_mode & FMODE_READ) == 0)
39 return -EINVAL;
Jeff Dike3d889582008-05-12 14:01:59 -070040 if ((filp->f_mode & FMODE_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -EINVAL;
42
43 return 0;
44}
45
Jeff Dike5d33e4d2008-05-12 14:01:58 -070046static atomic_t host_sleep_count = ATOMIC_INIT(0);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
Jeff Dike3d889582008-05-12 14:01:59 -070049 loff_t *offp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Jeff Dike3d889582008-05-12 14:01:59 -070051 u32 data;
52 int n, ret = 0, have_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jeff Dike3d889582008-05-12 14:01:59 -070054 while (size) {
55 n = os_read_file(random_fd, &data, sizeof(data));
56 if (n > 0) {
57 have_data = n;
58 while (have_data && size) {
59 if (put_user((u8) data, buf++)) {
60 ret = ret ? : -EFAULT;
61 break;
62 }
63 size--;
64 ret++;
65 have_data--;
66 data >>= 8;
67 }
68 }
69 else if (n == -EAGAIN) {
Jeff Dike5d33e4d2008-05-12 14:01:58 -070070 DECLARE_WAITQUEUE(wait, current);
71
Jeff Dike3d889582008-05-12 14:01:59 -070072 if (filp->f_flags & O_NONBLOCK)
73 return ret ? : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Jeff Dike5d33e4d2008-05-12 14:01:58 -070075 atomic_inc(&host_sleep_count);
76 reactivate_fd(random_fd, RANDOM_IRQ);
77 add_sigio_fd(random_fd);
78
79 add_wait_queue(&host_read_wait, &wait);
Davidlohr Bueso642fa442017-01-03 13:43:14 -080080 set_current_state(TASK_INTERRUPTIBLE);
Jeff Dike5d33e4d2008-05-12 14:01:58 -070081
82 schedule();
Jeff Dike5d33e4d2008-05-12 14:01:58 -070083 remove_wait_queue(&host_read_wait, &wait);
84
85 if (atomic_dec_and_test(&host_sleep_count)) {
86 ignore_sigio_fd(random_fd);
87 deactivate_fd(random_fd, RANDOM_IRQ);
88 }
Jeff Dike3d889582008-05-12 14:01:59 -070089 }
90 else
91 return n;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (signal_pending (current))
94 return ret ? : -ERESTARTSYS;
95 }
96 return ret;
97}
98
Jeff Dike5e7672e2006-09-27 01:50:33 -070099static const struct file_operations rng_chrdev_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .owner = THIS_MODULE,
101 .open = rng_dev_open,
102 .read = rng_dev_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200103 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Jeff Dike99b02782007-02-10 01:44:05 -0800106/* rng_init shouldn't be called more than once at boot time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct miscdevice rng_miscdev = {
108 RNG_MISCDEV_MINOR,
109 RNG_MODULE_NAME,
110 &rng_chrdev_ops,
111};
112
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700113static irqreturn_t random_interrupt(int irq, void *data)
114{
115 wake_up(&host_read_wait);
116
117 return IRQ_HANDLED;
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*
121 * rng_init - initialize RNG module
122 */
123static int __init rng_init (void)
124{
125 int err;
126
Jeff Dike3d889582008-05-12 14:01:59 -0700127 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
128 if (err < 0)
129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Jeff Dike3d889582008-05-12 14:01:59 -0700131 random_fd = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700133 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
Theodore Ts'oaab94462012-07-17 14:18:23 -0400134 0, "random", NULL);
Jeff Dike3d889582008-05-12 14:01:59 -0700135 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto err_out_cleanup_hw;
137
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700138 sigio_broken(random_fd, 1);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 err = misc_register (&rng_miscdev);
141 if (err) {
Jeff Dike3d889582008-05-12 14:01:59 -0700142 printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
143 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 goto err_out_cleanup_hw;
145 }
Jeff Dike3d889582008-05-12 14:01:59 -0700146out:
147 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jeff Dike3d889582008-05-12 14:01:59 -0700149err_out_cleanup_hw:
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700150 os_close_file(random_fd);
Jeff Dike3d889582008-05-12 14:01:59 -0700151 random_fd = -1;
152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
156 * rng_cleanup - shutdown RNG module
157 */
Anton Ivanovff6a1792017-11-20 21:17:58 +0000158
159static void cleanup(void)
160{
161 free_irq_by_fd(random_fd);
162 os_close_file(random_fd);
163}
164
165static void __exit rng_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700167 os_close_file(random_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 misc_deregister (&rng_miscdev);
169}
170
171module_init (rng_init);
172module_exit (rng_cleanup);
Anton Ivanovff6a1792017-11-20 21:17:58 +0000173__uml_exitcall(cleanup);
Paolo 'Blaisorblade' Giarrusso567b5652005-05-28 15:51:58 -0700174
175MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
176MODULE_LICENSE("GPL");