Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,addtoit}.com> */ |
| 2 | |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 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 | */ |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 9 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | #include <linux/fs.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/miscdevice.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <asm/uaccess.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 16 | #include "irq_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "os.h" |
| 18 | |
| 19 | /* |
| 20 | * core module and version information |
| 21 | */ |
| 22 | #define RNG_VERSION "1.0.0" |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 23 | #define RNG_MODULE_NAME "hw_random" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #define RNG_MISCDEV_MINOR 183 /* official */ |
| 26 | |
Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static int random_fd = -1; |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 32 | static DECLARE_WAIT_QUEUE_HEAD(host_read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | static 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 Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 39 | if ((filp->f_mode & FMODE_WRITE) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return -EINVAL; |
| 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 45 | static atomic_t host_sleep_count = ATOMIC_INIT(0); |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 48 | loff_t *offp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 50 | u32 data; |
| 51 | int n, ret = 0, have_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 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) { |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 69 | DECLARE_WAITQUEUE(wait, current); |
| 70 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 71 | if (filp->f_flags & O_NONBLOCK) |
| 72 | return ret ? : -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 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(); |
| 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 Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 89 | } |
| 90 | else |
| 91 | return n; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (signal_pending (current)) |
| 94 | return ret ? : -ERESTARTSYS; |
| 95 | } |
| 96 | return ret; |
| 97 | } |
| 98 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 99 | static const struct file_operations rng_chrdev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | .owner = THIS_MODULE, |
| 101 | .open = rng_dev_open, |
| 102 | .read = rng_dev_read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 103 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Jeff Dike | 99b0278 | 2007-02-10 01:44:05 -0800 | [diff] [blame] | 106 | /* rng_init shouldn't be called more than once at boot time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static struct miscdevice rng_miscdev = { |
| 108 | RNG_MISCDEV_MINOR, |
| 109 | RNG_MODULE_NAME, |
| 110 | &rng_chrdev_ops, |
| 111 | }; |
| 112 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 113 | static irqreturn_t random_interrupt(int irq, void *data) |
| 114 | { |
| 115 | wake_up(&host_read_wait); |
| 116 | |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | /* |
| 121 | * rng_init - initialize RNG module |
| 122 | */ |
| 123 | static int __init rng_init (void) |
| 124 | { |
| 125 | int err; |
| 126 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 127 | err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0); |
| 128 | if (err < 0) |
| 129 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 131 | random_fd = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 133 | err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt, |
Yong Zhang | c0b79a9 | 2011-09-22 16:58:46 +0800 | [diff] [blame] | 134 | IRQF_SAMPLE_RANDOM, "random", |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 135 | NULL); |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 136 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | goto err_out_cleanup_hw; |
| 138 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 139 | sigio_broken(random_fd, 1); |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | err = misc_register (&rng_miscdev); |
| 142 | if (err) { |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 143 | printk (KERN_ERR RNG_MODULE_NAME ": misc device register " |
| 144 | "failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | goto err_out_cleanup_hw; |
| 146 | } |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 147 | out: |
| 148 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 150 | err_out_cleanup_hw: |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 151 | os_close_file(random_fd); |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 152 | random_fd = -1; |
| 153 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * rng_cleanup - shutdown RNG module |
| 158 | */ |
| 159 | static void __exit rng_cleanup (void) |
| 160 | { |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 161 | os_close_file(random_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | misc_deregister (&rng_miscdev); |
| 163 | } |
| 164 | |
| 165 | module_init (rng_init); |
| 166 | module_exit (rng_cleanup); |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 167 | |
| 168 | MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver"); |
| 169 | MODULE_LICENSE("GPL"); |