random.c 3.3 KB

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