Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2005 Jeff Dike <jdike@addtoit.com> */ |
| 2 | /* Much of this ripped from drivers/char/hw_random.c, see there for other |
| 3 | * copyright. |
| 4 | * |
| 5 | * This software may be used and distributed according to the terms |
| 6 | * of the GNU General Public License, incorporated herein by reference. |
| 7 | */ |
Jeff Dike | 8192ab4 | 2008-02-04 22:30:53 -0800 | [diff] [blame] | 8 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
| 10 | #include <linux/fs.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 11 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/miscdevice.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <asm/uaccess.h> |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 15 | #include "irq_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "os.h" |
| 17 | |
| 18 | /* |
| 19 | * core module and version information |
| 20 | */ |
| 21 | #define RNG_VERSION "1.0.0" |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 22 | #define RNG_MODULE_NAME "hw_random" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define RNG_MISCDEV_MINOR 183 /* official */ |
| 25 | |
Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame] | 26 | /* Changed at init time, in the non-modular case, and at module load |
| 27 | * time, in the module case. Presumably, the module subsystem |
| 28 | * protects against a module being loaded twice at the same time. |
| 29 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int random_fd = -1; |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 31 | static DECLARE_WAIT_QUEUE_HEAD(host_read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | static int rng_dev_open (struct inode *inode, struct file *filp) |
| 34 | { |
| 35 | /* enforce read-only access to this chrdev */ |
| 36 | if ((filp->f_mode & FMODE_READ) == 0) |
| 37 | return -EINVAL; |
| 38 | if (filp->f_mode & FMODE_WRITE) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 44 | static atomic_t host_sleep_count = ATOMIC_INIT(0); |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, |
| 47 | loff_t * offp) |
| 48 | { |
| 49 | u32 data; |
| 50 | int n, ret = 0, have_data; |
| 51 | |
| 52 | while(size){ |
Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 53 | n = os_read_file(random_fd, &data, sizeof(data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if(n > 0){ |
| 55 | have_data = n; |
| 56 | while (have_data && size) { |
| 57 | if (put_user((u8)data, buf++)) { |
| 58 | ret = ret ? : -EFAULT; |
| 59 | break; |
| 60 | } |
| 61 | size--; |
| 62 | ret++; |
| 63 | have_data--; |
| 64 | data>>=8; |
| 65 | } |
| 66 | } |
| 67 | else if(n == -EAGAIN){ |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 68 | DECLARE_WAITQUEUE(wait, current); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (filp->f_flags & O_NONBLOCK) |
| 71 | return ret ? : -EAGAIN; |
| 72 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 73 | atomic_inc(&host_sleep_count); |
| 74 | reactivate_fd(random_fd, RANDOM_IRQ); |
| 75 | add_sigio_fd(random_fd); |
| 76 | |
| 77 | add_wait_queue(&host_read_wait, &wait); |
| 78 | set_task_state(current, TASK_INTERRUPTIBLE); |
| 79 | |
| 80 | schedule(); |
| 81 | set_task_state(current, TASK_RUNNING); |
| 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | else return n; |
| 90 | if (signal_pending (current)) |
| 91 | return ret ? : -ERESTARTSYS; |
| 92 | } |
| 93 | return ret; |
| 94 | } |
| 95 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 96 | static const struct file_operations rng_chrdev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | .owner = THIS_MODULE, |
| 98 | .open = rng_dev_open, |
| 99 | .read = rng_dev_read, |
| 100 | }; |
| 101 | |
Jeff Dike | 99b0278 | 2007-02-10 01:44:05 -0800 | [diff] [blame] | 102 | /* rng_init shouldn't be called more than once at boot time */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static struct miscdevice rng_miscdev = { |
| 104 | RNG_MISCDEV_MINOR, |
| 105 | RNG_MODULE_NAME, |
| 106 | &rng_chrdev_ops, |
| 107 | }; |
| 108 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 109 | static irqreturn_t random_interrupt(int irq, void *data) |
| 110 | { |
| 111 | wake_up(&host_read_wait); |
| 112 | |
| 113 | return IRQ_HANDLED; |
| 114 | } |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* |
| 117 | * rng_init - initialize RNG module |
| 118 | */ |
| 119 | static int __init rng_init (void) |
| 120 | { |
| 121 | int err; |
| 122 | |
| 123 | err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0); |
| 124 | if(err < 0) |
| 125 | goto out; |
| 126 | |
| 127 | random_fd = err; |
| 128 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 129 | err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt, |
| 130 | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random", |
| 131 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if(err) |
| 133 | goto err_out_cleanup_hw; |
| 134 | |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 135 | sigio_broken(random_fd, 1); |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | err = misc_register (&rng_miscdev); |
| 138 | if (err) { |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 139 | printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | goto err_out_cleanup_hw; |
| 141 | } |
| 142 | |
| 143 | out: |
| 144 | return err; |
| 145 | |
| 146 | err_out_cleanup_hw: |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 147 | os_close_file(random_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | random_fd = -1; |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * rng_cleanup - shutdown RNG module |
| 154 | */ |
| 155 | static void __exit rng_cleanup (void) |
| 156 | { |
Jeff Dike | 5d33e4d | 2008-05-12 14:01:58 -0700 | [diff] [blame^] | 157 | os_close_file(random_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | misc_deregister (&rng_miscdev); |
| 159 | } |
| 160 | |
| 161 | module_init (rng_init); |
| 162 | module_exit (rng_cleanup); |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 163 | |
| 164 | MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver"); |
| 165 | MODULE_LICENSE("GPL"); |