io_uring: do not always copy iovec in io_req_map_rw()
[linux/fpc-iii.git] / arch / um / drivers / random.c
blob1d5d3057e6f1f607ecec1ba0b941a80150c0dcb6
1 /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */
3 /* Much of this ripped from drivers/char/hw_random.c, see there for other
4 * copyright.
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>
21 * core module and version information
23 #define RNG_VERSION "1.0.0"
24 #define RNG_MODULE_NAME "hw_random"
26 #define RNG_MISCDEV_MINOR 183 /* official */
28 /* 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.
32 static int random_fd = -1;
33 static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
35 static int rng_dev_open (struct inode *inode, struct file *filp)
37 /* enforce read-only access to this chrdev */
38 if ((filp->f_mode & FMODE_READ) == 0)
39 return -EINVAL;
40 if ((filp->f_mode & FMODE_WRITE) != 0)
41 return -EINVAL;
43 return 0;
46 static atomic_t host_sleep_count = ATOMIC_INIT(0);
48 static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
49 loff_t *offp)
51 u32 data;
52 int n, ret = 0, have_data;
54 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;
63 size--;
64 ret++;
65 have_data--;
66 data >>= 8;
69 else if (n == -EAGAIN) {
70 DECLARE_WAITQUEUE(wait, current);
72 if (filp->f_flags & O_NONBLOCK)
73 return ret ? : -EAGAIN;
75 atomic_inc(&host_sleep_count);
76 add_sigio_fd(random_fd);
78 add_wait_queue(&host_read_wait, &wait);
79 set_current_state(TASK_INTERRUPTIBLE);
81 schedule();
82 remove_wait_queue(&host_read_wait, &wait);
84 if (atomic_dec_and_test(&host_sleep_count)) {
85 ignore_sigio_fd(random_fd);
86 deactivate_fd(random_fd, RANDOM_IRQ);
89 else
90 return n;
92 if (signal_pending (current))
93 return ret ? : -ERESTARTSYS;
95 return ret;
98 static const struct file_operations rng_chrdev_ops = {
99 .owner = THIS_MODULE,
100 .open = rng_dev_open,
101 .read = rng_dev_read,
102 .llseek = noop_llseek,
105 /* rng_init shouldn't be called more than once at boot time */
106 static struct miscdevice rng_miscdev = {
107 RNG_MISCDEV_MINOR,
108 RNG_MODULE_NAME,
109 &rng_chrdev_ops,
112 static irqreturn_t random_interrupt(int irq, void *data)
114 wake_up(&host_read_wait);
116 return IRQ_HANDLED;
120 * rng_init - initialize RNG module
122 static int __init rng_init (void)
124 int err;
126 err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
127 if (err < 0)
128 goto out;
130 random_fd = err;
132 err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
133 0, "random", NULL);
134 if (err)
135 goto err_out_cleanup_hw;
137 sigio_broken(random_fd, 1);
139 err = misc_register (&rng_miscdev);
140 if (err) {
141 printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
142 "failed\n");
143 goto err_out_cleanup_hw;
145 out:
146 return err;
148 err_out_cleanup_hw:
149 os_close_file(random_fd);
150 random_fd = -1;
151 goto out;
155 * rng_cleanup - shutdown RNG module
158 static void cleanup(void)
160 free_irq_by_fd(random_fd);
161 os_close_file(random_fd);
164 static void __exit rng_cleanup(void)
166 os_close_file(random_fd);
167 misc_deregister (&rng_miscdev);
170 module_init (rng_init);
171 module_exit (rng_cleanup);
172 __uml_exitcall(cleanup);
174 MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
175 MODULE_LICENSE("GPL");