blob: 4949044773ba78b93797d43701ab751b0a1bce23 [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 */
Jeff Dike8192ab42008-02-04 22:30:53 -08009#include <linux/sched.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>
15#include <asm/uaccess.h>
Jeff Dike5d33e4d2008-05-12 14:01:58 -070016#include "irq_kern.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "os.h"
18
19/*
20 * core module and version information
21 */
22#define RNG_VERSION "1.0.0"
Jeff Dike5d33e4d2008-05-12 14:01:58 -070023#define RNG_MODULE_NAME "hw_random"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define RNG_MISCDEV_MINOR 183 /* official */
26
Jeff Dike730760e2006-09-29 01:58:50 -070027/* Changed at init time, in the non-modular case, and at module load
28 * time, in the module case. Presumably, the module subsystem
29 * protects against a module being loaded twice at the same time.
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int random_fd = -1;
Jeff Dike5d33e4d2008-05-12 14:01:58 -070032static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34static int rng_dev_open (struct inode *inode, struct file *filp)
35{
36 /* enforce read-only access to this chrdev */
37 if ((filp->f_mode & FMODE_READ) == 0)
38 return -EINVAL;
Jeff Dike3d889582008-05-12 14:01:59 -070039 if ((filp->f_mode & FMODE_WRITE) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return -EINVAL;
41
42 return 0;
43}
44
Jeff Dike5d33e4d2008-05-12 14:01:58 -070045static atomic_t host_sleep_count = ATOMIC_INIT(0);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
Jeff Dike3d889582008-05-12 14:01:59 -070048 loff_t *offp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Jeff Dike3d889582008-05-12 14:01:59 -070050 u32 data;
51 int n, ret = 0, have_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Jeff Dike3d889582008-05-12 14:01:59 -070053 while (size) {
54 n = os_read_file(random_fd, &data, sizeof(data));
55 if (n > 0) {
56 have_data = n;
57 while (have_data && size) {
58 if (put_user((u8) data, buf++)) {
59 ret = ret ? : -EFAULT;
60 break;
61 }
62 size--;
63 ret++;
64 have_data--;
65 data >>= 8;
66 }
67 }
68 else if (n == -EAGAIN) {
Jeff Dike5d33e4d2008-05-12 14:01:58 -070069 DECLARE_WAITQUEUE(wait, current);
70
Jeff Dike3d889582008-05-12 14:01:59 -070071 if (filp->f_flags & O_NONBLOCK)
72 return ret ? : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Jeff Dike5d33e4d2008-05-12 14:01:58 -070074 atomic_inc(&host_sleep_count);
75 reactivate_fd(random_fd, RANDOM_IRQ);
76 add_sigio_fd(random_fd);
77
78 add_wait_queue(&host_read_wait, &wait);
79 set_task_state(current, TASK_INTERRUPTIBLE);
80
81 schedule();
82 set_task_state(current, TASK_RUNNING);
83 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,
103};
104
Jeff Dike99b02782007-02-10 01:44:05 -0800105/* rng_init shouldn't be called more than once at boot time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static struct miscdevice rng_miscdev = {
107 RNG_MISCDEV_MINOR,
108 RNG_MODULE_NAME,
109 &rng_chrdev_ops,
110};
111
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700112static irqreturn_t random_interrupt(int irq, void *data)
113{
114 wake_up(&host_read_wait);
115
116 return IRQ_HANDLED;
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*
120 * rng_init - initialize RNG module
121 */
122static int __init rng_init (void)
123{
124 int err;
125
Jeff Dike3d889582008-05-12 14:01:59 -0700126 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
127 if (err < 0)
128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jeff Dike3d889582008-05-12 14:01:59 -0700130 random_fd = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700132 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
133 IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random",
134 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 */
158static void __exit rng_cleanup (void)
159{
Jeff Dike5d33e4d2008-05-12 14:01:58 -0700160 os_close_file(random_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 misc_deregister (&rng_miscdev);
162}
163
164module_init (rng_init);
165module_exit (rng_cleanup);
Paolo 'Blaisorblade' Giarrusso567b5652005-05-28 15:51:58 -0700166
167MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
168MODULE_LICENSE("GPL");