random.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 2005 - 2008 Jeff Dike <jdike@{linux.intel,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) != 0)
  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
  77. return n;
  78. if (signal_pending (current))
  79. return ret ? : -ERESTARTSYS;
  80. }
  81. return ret;
  82. }
  83. static const struct file_operations rng_chrdev_ops = {
  84. .owner = THIS_MODULE,
  85. .open = rng_dev_open,
  86. .read = rng_dev_read,
  87. .llseek = noop_llseek,
  88. };
  89. /* rng_init shouldn't be called more than once at boot time */
  90. static struct miscdevice rng_miscdev = {
  91. RNG_MISCDEV_MINOR,
  92. RNG_MODULE_NAME,
  93. &rng_chrdev_ops,
  94. };
  95. static irqreturn_t random_interrupt(int irq, void *data)
  96. {
  97. wake_up(&host_read_wait);
  98. return IRQ_HANDLED;
  99. }
  100. /*
  101. * rng_init - initialize RNG module
  102. */
  103. static int __init rng_init (void)
  104. {
  105. int err;
  106. err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
  107. if (err < 0)
  108. goto out;
  109. random_fd = err;
  110. err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
  111. IRQF_SAMPLE_RANDOM, "random",
  112. NULL);
  113. if (err)
  114. goto err_out_cleanup_hw;
  115. sigio_broken(random_fd, 1);
  116. err = misc_register (&rng_miscdev);
  117. if (err) {
  118. printk (KERN_ERR RNG_MODULE_NAME ": misc device register "
  119. "failed\n");
  120. goto err_out_cleanup_hw;
  121. }
  122. out:
  123. return err;
  124. err_out_cleanup_hw:
  125. os_close_file(random_fd);
  126. random_fd = -1;
  127. goto out;
  128. }
  129. /*
  130. * rng_cleanup - shutdown RNG module
  131. */
  132. static void __exit rng_cleanup (void)
  133. {
  134. os_close_file(random_fd);
  135. misc_deregister (&rng_miscdev);
  136. }
  137. module_init (rng_init);
  138. module_exit (rng_cleanup);
  139. MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
  140. MODULE_LICENSE("GPL");