random.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/interrupt.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/delay.h>
  14. #include <asm/uaccess.h>
  15. #include "irq_kern.h"
  16. #include "os.h"
  17. /*
  18. * core module and version information
  19. */
  20. #define RNG_VERSION "1.0.0"
  21. #define RNG_MODULE_NAME "hw_random"
  22. #define RNG_MISCDEV_MINOR 183 /* official */
  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. */
  27. static int random_fd = -1;
  28. static DECLARE_WAIT_QUEUE_HEAD(host_read_wait);
  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. return 0;
  37. }
  38. static atomic_t host_sleep_count = ATOMIC_INIT(0);
  39. static ssize_t rng_dev_read (struct file *filp, char __user *buf, size_t size,
  40. loff_t * offp)
  41. {
  42. u32 data;
  43. int n, ret = 0, have_data;
  44. while(size){
  45. n = os_read_file(random_fd, &data, sizeof(data));
  46. if(n > 0){
  47. have_data = n;
  48. while (have_data && size) {
  49. if (put_user((u8)data, buf++)) {
  50. ret = ret ? : -EFAULT;
  51. break;
  52. }
  53. size--;
  54. ret++;
  55. have_data--;
  56. data>>=8;
  57. }
  58. }
  59. else if(n == -EAGAIN){
  60. DECLARE_WAITQUEUE(wait, current);
  61. if (filp->f_flags & O_NONBLOCK)
  62. return ret ? : -EAGAIN;
  63. atomic_inc(&host_sleep_count);
  64. reactivate_fd(random_fd, RANDOM_IRQ);
  65. add_sigio_fd(random_fd);
  66. add_wait_queue(&host_read_wait, &wait);
  67. set_task_state(current, TASK_INTERRUPTIBLE);
  68. schedule();
  69. set_task_state(current, TASK_RUNNING);
  70. remove_wait_queue(&host_read_wait, &wait);
  71. if (atomic_dec_and_test(&host_sleep_count)) {
  72. ignore_sigio_fd(random_fd);
  73. deactivate_fd(random_fd, RANDOM_IRQ);
  74. }
  75. }
  76. else return n;
  77. if (signal_pending (current))
  78. return ret ? : -ERESTARTSYS;
  79. }
  80. return ret;
  81. }
  82. static const struct file_operations rng_chrdev_ops = {
  83. .owner = THIS_MODULE,
  84. .open = rng_dev_open,
  85. .read = rng_dev_read,
  86. };
  87. /* rng_init shouldn't be called more than once at boot time */
  88. static struct miscdevice rng_miscdev = {
  89. RNG_MISCDEV_MINOR,
  90. RNG_MODULE_NAME,
  91. &rng_chrdev_ops,
  92. };
  93. static irqreturn_t random_interrupt(int irq, void *data)
  94. {
  95. wake_up(&host_read_wait);
  96. return IRQ_HANDLED;
  97. }
  98. /*
  99. * rng_init - initialize RNG module
  100. */
  101. static int __init rng_init (void)
  102. {
  103. int err;
  104. err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
  105. if(err < 0)
  106. goto out;
  107. random_fd = err;
  108. err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
  109. IRQF_DISABLED | IRQF_SAMPLE_RANDOM, "random",
  110. NULL);
  111. if(err)
  112. goto err_out_cleanup_hw;
  113. sigio_broken(random_fd, 1);
  114. err = misc_register (&rng_miscdev);
  115. if (err) {
  116. printk (KERN_ERR RNG_MODULE_NAME ": misc device register failed\n");
  117. goto err_out_cleanup_hw;
  118. }
  119. out:
  120. return err;
  121. err_out_cleanup_hw:
  122. os_close_file(random_fd);
  123. random_fd = -1;
  124. goto out;
  125. }
  126. /*
  127. * rng_cleanup - shutdown RNG module
  128. */
  129. static void __exit rng_cleanup (void)
  130. {
  131. os_close_file(random_fd);
  132. misc_deregister (&rng_miscdev);
  133. }
  134. module_init (rng_init);
  135. module_exit (rng_cleanup);
  136. MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
  137. MODULE_LICENSE("GPL");