random.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. remove_wait_queue(&host_read_wait, &wait);
  70. if (atomic_dec_and_test(&host_sleep_count)) {
  71. ignore_sigio_fd(random_fd);
  72. deactivate_fd(random_fd, RANDOM_IRQ);
  73. }
  74. }
  75. else
  76. 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. .llseek = noop_llseek,
  87. };
  88. /* rng_init shouldn't be called more than once at boot time */
  89. static struct miscdevice rng_miscdev = {
  90. RNG_MISCDEV_MINOR,
  91. RNG_MODULE_NAME,
  92. &rng_chrdev_ops,
  93. };
  94. static irqreturn_t random_interrupt(int irq, void *data)
  95. {
  96. wake_up(&host_read_wait);
  97. return IRQ_HANDLED;
  98. }
  99. /*
  100. * rng_init - initialize RNG module
  101. */
  102. static int __init rng_init (void)
  103. {
  104. int err;
  105. err = os_open_file("/dev/random", of_read(OPENFLAGS()), 0);
  106. if (err < 0)
  107. goto out;
  108. random_fd = err;
  109. err = um_request_irq(RANDOM_IRQ, random_fd, IRQ_READ, random_interrupt,
  110. 0, "random", 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 "
  117. "failed\n");
  118. goto err_out_cleanup_hw;
  119. }
  120. out:
  121. return err;
  122. err_out_cleanup_hw:
  123. os_close_file(random_fd);
  124. random_fd = -1;
  125. goto out;
  126. }
  127. /*
  128. * rng_cleanup - shutdown RNG module
  129. */
  130. static void __exit rng_cleanup (void)
  131. {
  132. os_close_file(random_fd);
  133. misc_deregister (&rng_miscdev);
  134. }
  135. module_init (rng_init);
  136. module_exit (rng_cleanup);
  137. MODULE_DESCRIPTION("UML Host Random Number Generator (RNG) driver");
  138. MODULE_LICENSE("GPL");