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> |
Arnd Bergmann | 7eb500d | 2008-05-20 19:16:57 +0200 | [diff] [blame] | 10 | #include <linux/smp_lock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/fs.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/miscdevice.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <asm/uaccess.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 17 | #include "irq_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "os.h" |
| 19 | |
| 20 | /* |
| 21 | * core module and version information |
| 22 | */ |
| 23 | #define RNG_VERSION "1.0.0" |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 24 | #define RNG_MODULE_NAME "hw_random" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #define RNG_MISCDEV_MINOR 183 /* official */ |
| 27 | |
Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 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. |
| 31 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static int random_fd = -1; |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 33 | static DECLARE_WAIT_QUEUE_HEAD(host_read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | static int rng_dev_open (struct inode *inode, struct file *filp) |
| 36 | { |
Arnd Bergmann | 7eb500d | 2008-05-20 19:16:57 +0200 | [diff] [blame] | 37 | cycle_kernel_lock(); |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* enforce read-only access to this chrdev */ |
| 40 | if ((filp->f_mode & FMODE_READ) == 0) |
| 41 | return -EINVAL; |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 42 | if ((filp->f_mode & FMODE_WRITE) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return -EINVAL; |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 48 | static atomic_t host_sleep_count = ATOMIC_INIT(0); |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | 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] | 51 | loff_t *offp) |
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 | u32 data; |
| 54 | int n, ret = 0, have_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 56 | while (size) { |
| 57 | n = os_read_file(random_fd, &data, sizeof(data)); |
| 58 | if (n > 0) { |
| 59 | have_data = n; |
| 60 | while (have_data && size) { |
| 61 | if (put_user((u8) data, buf++)) { |
| 62 | ret = ret ? : -EFAULT; |
| 63 | break; |
| 64 | } |
| 65 | size--; |
| 66 | ret++; |
| 67 | have_data--; |
| 68 | data >>= 8; |
| 69 | } |
| 70 | } |
| 71 | else if (n == -EAGAIN) { |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 72 | DECLARE_WAITQUEUE(wait, current); |
| 73 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 74 | if (filp->f_flags & O_NONBLOCK) |
| 75 | return ret ? : -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 77 | atomic_inc(&host_sleep_count); |
| 78 | reactivate_fd(random_fd, RANDOM_IRQ); |
| 79 | add_sigio_fd(random_fd); |
| 80 | |
| 81 | add_wait_queue(&host_read_wait, &wait); |
| 82 | set_task_state(current, TASK_INTERRUPTIBLE); |
| 83 | |
| 84 | schedule(); |
| 85 | set_task_state(current, TASK_RUNNING); |
| 86 | remove_wait_queue(&host_read_wait, &wait); |
| 87 | |
| 88 | if (atomic_dec_and_test(&host_sleep_count)) { |
| 89 | ignore_sigio_fd(random_fd); |
| 90 | deactivate_fd(random_fd, RANDOM_IRQ); |
| 91 | } |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 92 | } |
| 93 | else |
| 94 | return n; |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (signal_pending (current)) |
| 97 | return ret ? : -ERESTARTSYS; |
| 98 | } |
| 99 | return ret; |
| 100 | } |
| 101 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 102 | static const struct file_operations rng_chrdev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .owner = THIS_MODULE, |
| 104 | .open = rng_dev_open, |
| 105 | .read = rng_dev_read, |
| 106 | }; |
| 107 | |
Jeff Dike | 99b0278 | 2007-02-10 01:44:05 -0800 | [diff] [blame] | 108 | /* rng_init shouldn't be called more than once at boot time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | static struct miscdevice rng_miscdev = { |
| 110 | RNG_MISCDEV_MINOR, |
| 111 | RNG_MODULE_NAME, |
| 112 | &rng_chrdev_ops, |
| 113 | }; |
| 114 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 115 | static irqreturn_t random_interrupt(int irq, void *data) |
| 116 | { |
| 117 | wake_up(&host_read_wait); |
| 118 | |
| 119 | return IRQ_HANDLED; |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* |
| 123 | * rng_init - initialize RNG module |
| 124 | */ |
| 125 | static int __init rng_init (void) |
| 126 | { |
| 127 | int err; |
| 128 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 129 | err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0); |
| 130 | if (err < 0) |
| 131 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 133 | random_fd = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 135 | err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt, |
| 136 | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random", |
| 137 | NULL); |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 138 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | goto err_out_cleanup_hw; |
| 140 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 141 | sigio_broken(random_fd, 1); |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | err = misc_register (&rng_miscdev); |
| 144 | if (err) { |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 145 | printk (KERN_ERR RNG_MODULE_NAME ": misc device register " |
| 146 | "failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | goto err_out_cleanup_hw; |
| 148 | } |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 149 | out: |
| 150 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 152 | err_out_cleanup_hw: |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 153 | os_close_file(random_fd); |
Jeff Dike | 3d88958 | 2008-05-12 14:01:59 -0700 | [diff] [blame] | 154 | random_fd = -1; |
| 155 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * rng_cleanup - shutdown RNG module |
| 160 | */ |
| 161 | static void __exit rng_cleanup (void) |
| 162 | { |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame] | 163 | os_close_file(random_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | misc_deregister (&rng_miscdev); |
| 165 | } |
| 166 | |
| 167 | module_init (rng_init); |
| 168 | module_exit (rng_cleanup); |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 169 | |
| 170 | MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver"); |
| 171 | MODULE_LICENSE("GPL"); |