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