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 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/miscdevice.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <asm/uaccess.h> |
| 13 | #include "os.h" |
| 14 | |
| 15 | /* |
| 16 | * core module and version information |
| 17 | */ |
| 18 | #define RNG_VERSION "1.0.0" |
| 19 | #define RNG_MODULE_NAME "random" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #define RNG_MISCDEV_MINOR 183 /* official */ |
| 22 | |
Jeff Dike | 730760e | 2006-09-29 01:58:50 -0700 | [diff] [blame^] | 23 | /* Changed at init time, in the non-modular case, and at module load |
| 24 | * time, in the module case. Presumably, the module subsystem |
| 25 | * protects against a module being loaded twice at the same time. |
| 26 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static int random_fd = -1; |
| 28 | |
| 29 | static int rng_dev_open (struct inode *inode, struct file *filp) |
| 30 | { |
| 31 | /* enforce read-only access to this chrdev */ |
| 32 | if ((filp->f_mode & FMODE_READ) == 0) |
| 33 | return -EINVAL; |
| 34 | if (filp->f_mode & FMODE_WRITE) |
| 35 | return -EINVAL; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size, |
| 41 | loff_t * offp) |
| 42 | { |
| 43 | u32 data; |
| 44 | int n, ret = 0, have_data; |
| 45 | |
| 46 | while(size){ |
| 47 | n = os_read_file(random_fd, &data, sizeof(data)); |
| 48 | if(n > 0){ |
| 49 | have_data = n; |
| 50 | while (have_data && size) { |
| 51 | if (put_user((u8)data, buf++)) { |
| 52 | ret = ret ? : -EFAULT; |
| 53 | break; |
| 54 | } |
| 55 | size--; |
| 56 | ret++; |
| 57 | have_data--; |
| 58 | data>>=8; |
| 59 | } |
| 60 | } |
| 61 | else if(n == -EAGAIN){ |
| 62 | if (filp->f_flags & O_NONBLOCK) |
| 63 | return ret ? : -EAGAIN; |
| 64 | |
Nishanth Aravamudan | bc874d1 | 2005-11-07 01:01:14 -0800 | [diff] [blame] | 65 | if(need_resched()) |
| 66 | schedule_timeout_interruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | else return n; |
| 69 | if (signal_pending (current)) |
| 70 | return ret ? : -ERESTARTSYS; |
| 71 | } |
| 72 | return ret; |
| 73 | } |
| 74 | |
Jeff Dike | 5e7672e | 2006-09-27 01:50:33 -0700 | [diff] [blame] | 75 | static const struct file_operations rng_chrdev_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | .owner = THIS_MODULE, |
| 77 | .open = rng_dev_open, |
| 78 | .read = rng_dev_read, |
| 79 | }; |
| 80 | |
| 81 | static struct miscdevice rng_miscdev = { |
| 82 | RNG_MISCDEV_MINOR, |
| 83 | RNG_MODULE_NAME, |
| 84 | &rng_chrdev_ops, |
| 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * rng_init - initialize RNG module |
| 89 | */ |
| 90 | static int __init rng_init (void) |
| 91 | { |
| 92 | int err; |
| 93 | |
| 94 | err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0); |
| 95 | if(err < 0) |
| 96 | goto out; |
| 97 | |
| 98 | random_fd = err; |
| 99 | |
| 100 | err = os_set_fd_block(random_fd, 0); |
| 101 | if(err) |
| 102 | goto err_out_cleanup_hw; |
| 103 | |
| 104 | err = misc_register (&rng_miscdev); |
| 105 | if (err) { |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 106 | printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | goto err_out_cleanup_hw; |
| 108 | } |
| 109 | |
| 110 | out: |
| 111 | return err; |
| 112 | |
| 113 | err_out_cleanup_hw: |
| 114 | random_fd = -1; |
| 115 | goto out; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * rng_cleanup - shutdown RNG module |
| 120 | */ |
| 121 | static void __exit rng_cleanup (void) |
| 122 | { |
| 123 | misc_deregister (&rng_miscdev); |
| 124 | } |
| 125 | |
| 126 | module_init (rng_init); |
| 127 | module_exit (rng_cleanup); |
Paolo 'Blaisorblade' Giarrusso | 567b565 | 2005-05-28 15:51:58 -0700 | [diff] [blame] | 128 | |
| 129 | MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver"); |
| 130 | MODULE_LICENSE("GPL"); |