Merge branch 'for-linus-4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/mason...
[deliverable/linux.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 */
8192ab42 9#include <linux/sched.h>
1da177e4
LT
10#include <linux/module.h>
11#include <linux/fs.h>
5d33e4d7 12#include <linux/interrupt.h>
1da177e4
LT
13#include <linux/miscdevice.h>
14#include <linux/delay.h>
15#include <asm/uaccess.h>
37185b33
AV
16#include <irq_kern.h>
17#include <os.h>
1da177e4
LT
18
19/*
20 * core module and version information
21 */
22#define RNG_VERSION "1.0.0"
5d33e4d7 23#define RNG_MODULE_NAME "hw_random"
1da177e4
LT
24
25#define RNG_MISCDEV_MINOR 183 /* official */
26
730760e9
JD
27/* 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 */
1da177e4 31static int random_fd = -1;
5d33e4d7 32static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
1da177e4
LT
33
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;
3d88958e 39 if ((filp->f_mode & FMODE_WRITE) != 0)
1da177e4
LT
40 return -EINVAL;
41
42 return 0;
43}
44
5d33e4d7
JD
45static atomic_t host_sleep_count = ATOMIC_INIT(0);
46
1da177e4 47static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
3d88958e 48 loff_t *offp)
1da177e4 49{
3d88958e
JD
50 u32 data;
51 int n, ret = 0, have_data;
52
53 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) {
5d33e4d7
JD
69 DECLARE_WAITQUEUE(wait, current);
70
3d88958e
JD
71 if (filp->f_flags & O_NONBLOCK)
72 return ret ? : -EAGAIN;
1da177e4 73
5d33e4d7
JD
74 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();
5d33e4d7
JD
82 remove_wait_queue(&host_read_wait, &wait);
83
84 if (atomic_dec_and_test(&host_sleep_count)) {
85 ignore_sigio_fd(random_fd);
86 deactivate_fd(random_fd, RANDOM_IRQ);
87 }
3d88958e
JD
88 }
89 else
90 return n;
91
1da177e4
LT
92 if (signal_pending (current))
93 return ret ? : -ERESTARTSYS;
94 }
95 return ret;
96}
97
5e7672ec 98static const struct file_operations rng_chrdev_ops = {
1da177e4
LT
99 .owner = THIS_MODULE,
100 .open = rng_dev_open,
101 .read = rng_dev_read,
6038f373 102 .llseek = noop_llseek,
1da177e4
LT
103};
104
99b0278f 105/* rng_init shouldn't be called more than once at boot time */
1da177e4
LT
106static struct miscdevice rng_miscdev = {
107 RNG_MISCDEV_MINOR,
108 RNG_MODULE_NAME,
109 &rng_chrdev_ops,
110};
111
5d33e4d7
JD
112static irqreturn_t random_interrupt(int irq, void *data)
113{
114 wake_up(&host_read_wait);
115
116 return IRQ_HANDLED;
117}
118
1da177e4
LT
119/*
120 * rng_init - initialize RNG module
121 */
122static int __init rng_init (void)
123{
124 int err;
125
3d88958e
JD
126 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
127 if (err < 0)
128 goto out;
1da177e4 129
3d88958e 130 random_fd = err;
1da177e4 131
5d33e4d7 132 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
aab94460 133 0, "random", NULL);
3d88958e 134 if (err)
1da177e4
LT
135 goto err_out_cleanup_hw;
136
5d33e4d7
JD
137 sigio_broken(random_fd, 1);
138
1da177e4
LT
139 err = misc_register (&rng_miscdev);
140 if (err) {
3d88958e
JD
141 printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
142 "failed\n");
1da177e4
LT
143 goto err_out_cleanup_hw;
144 }
3d88958e
JD
145out:
146 return err;
1da177e4 147
3d88958e 148err_out_cleanup_hw:
5d33e4d7 149 os_close_file(random_fd);
3d88958e
JD
150 random_fd = -1;
151 goto out;
1da177e4
LT
152}
153
154/*
155 * rng_cleanup - shutdown RNG module
156 */
157static void __exit rng_cleanup (void)
158{
5d33e4d7 159 os_close_file(random_fd);
1da177e4
LT
160 misc_deregister (&rng_miscdev);
161}
162
163module_init (rng_init);
164module_exit (rng_cleanup);
567b5650
PBG
165
166MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
167MODULE_LICENSE("GPL");
This page took 0.782113 seconds and 5 git commands to generate.