core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Added support for the AMD Geode LX RNG
  3. (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
  4. derived from
  5. Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
  6. (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
  7. derived from
  8. Hardware driver for the AMD 768 Random Number Generator (RNG)
  9. (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
  10. derived from
  11. Hardware driver for Intel i810 Random Number Generator (RNG)
  12. Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
  13. Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
  14. Added generic RNG API
  15. Copyright 2006 Michael Buesch <mbuesch@freenet.de>
  16. Copyright 2005 (c) MontaVista Software, Inc.
  17. Please read Documentation/hw_random.txt for details on use.
  18. ----------------------------------------------------------
  19. This software may be used and distributed according to the terms
  20. of the GNU General Public License, incorporated herein by reference.
  21. */
  22. #include <linux/device.h>
  23. #include <linux/hw_random.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/fs.h>
  27. #include <linux/sched.h>
  28. #include <linux/init.h>
  29. #include <linux/miscdevice.h>
  30. #include <linux/delay.h>
  31. #include <asm/uaccess.h>
  32. #define RNG_MODULE_NAME "hw_random"
  33. #define PFX RNG_MODULE_NAME ": "
  34. #define RNG_MISCDEV_MINOR 183 /* official */
  35. static struct hwrng *current_rng;
  36. static LIST_HEAD(rng_list);
  37. static DEFINE_MUTEX(rng_mutex);
  38. static inline int hwrng_init(struct hwrng *rng)
  39. {
  40. if (!rng->init)
  41. return 0;
  42. return rng->init(rng);
  43. }
  44. static inline void hwrng_cleanup(struct hwrng *rng)
  45. {
  46. if (rng && rng->cleanup)
  47. rng->cleanup(rng);
  48. }
  49. static inline int hwrng_data_present(struct hwrng *rng, int wait)
  50. {
  51. if (!rng->data_present)
  52. return 1;
  53. return rng->data_present(rng, wait);
  54. }
  55. static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
  56. {
  57. return rng->data_read(rng, data);
  58. }
  59. static int rng_dev_open(struct inode *inode, struct file *filp)
  60. {
  61. /* enforce read-only access to this chrdev */
  62. if ((filp->f_mode & FMODE_READ) == 0)
  63. return -EINVAL;
  64. if (filp->f_mode & FMODE_WRITE)
  65. return -EINVAL;
  66. return 0;
  67. }
  68. static ssize_t rng_dev_read(struct file *filp, char __user *buf,
  69. size_t size, loff_t *offp)
  70. {
  71. u32 data;
  72. ssize_t ret = 0;
  73. int err = 0;
  74. int bytes_read;
  75. while (size) {
  76. err = -ERESTARTSYS;
  77. if (mutex_lock_interruptible(&rng_mutex))
  78. goto out;
  79. if (!current_rng) {
  80. mutex_unlock(&rng_mutex);
  81. err = -ENODEV;
  82. goto out;
  83. }
  84. bytes_read = 0;
  85. if (hwrng_data_present(current_rng,
  86. !(filp->f_flags & O_NONBLOCK)))
  87. bytes_read = hwrng_data_read(current_rng, &data);
  88. mutex_unlock(&rng_mutex);
  89. err = -EAGAIN;
  90. if (!bytes_read && (filp->f_flags & O_NONBLOCK))
  91. goto out;
  92. err = -EFAULT;
  93. while (bytes_read && size) {
  94. if (put_user((u8)data, buf++))
  95. goto out;
  96. size--;
  97. ret++;
  98. bytes_read--;
  99. data >>= 8;
  100. }
  101. if (need_resched())
  102. schedule_timeout_interruptible(1);
  103. err = -ERESTARTSYS;
  104. if (signal_pending(current))
  105. goto out;
  106. }
  107. out:
  108. return ret ? : err;
  109. }
  110. static const struct file_operations rng_chrdev_ops = {
  111. .owner = THIS_MODULE,
  112. .open = rng_dev_open,
  113. .read = rng_dev_read,
  114. };
  115. static struct miscdevice rng_miscdev = {
  116. .minor = RNG_MISCDEV_MINOR,
  117. .name = RNG_MODULE_NAME,
  118. .fops = &rng_chrdev_ops,
  119. };
  120. static ssize_t hwrng_attr_current_store(struct device *dev,
  121. struct device_attribute *attr,
  122. const char *buf, size_t len)
  123. {
  124. int err;
  125. struct hwrng *rng;
  126. err = mutex_lock_interruptible(&rng_mutex);
  127. if (err)
  128. return -ERESTARTSYS;
  129. err = -ENODEV;
  130. list_for_each_entry(rng, &rng_list, list) {
  131. if (strcmp(rng->name, buf) == 0) {
  132. if (rng == current_rng) {
  133. err = 0;
  134. break;
  135. }
  136. err = hwrng_init(rng);
  137. if (err)
  138. break;
  139. hwrng_cleanup(current_rng);
  140. current_rng = rng;
  141. err = 0;
  142. break;
  143. }
  144. }
  145. mutex_unlock(&rng_mutex);
  146. return err ? : len;
  147. }
  148. static ssize_t hwrng_attr_current_show(struct device *dev,
  149. struct device_attribute *attr,
  150. char *buf)
  151. {
  152. int err;
  153. ssize_t ret;
  154. const char *name = "none";
  155. err = mutex_lock_interruptible(&rng_mutex);
  156. if (err)
  157. return -ERESTARTSYS;
  158. if (current_rng)
  159. name = current_rng->name;
  160. ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
  161. mutex_unlock(&rng_mutex);
  162. return ret;
  163. }
  164. static ssize_t hwrng_attr_available_show(struct device *dev,
  165. struct device_attribute *attr,
  166. char *buf)
  167. {
  168. int err;
  169. ssize_t ret = 0;
  170. struct hwrng *rng;
  171. err = mutex_lock_interruptible(&rng_mutex);
  172. if (err)
  173. return -ERESTARTSYS;
  174. buf[0] = '\0';
  175. list_for_each_entry(rng, &rng_list, list) {
  176. strncat(buf, rng->name, PAGE_SIZE - ret - 1);
  177. ret += strlen(rng->name);
  178. strncat(buf, " ", PAGE_SIZE - ret - 1);
  179. ret++;
  180. }
  181. strncat(buf, "\n", PAGE_SIZE - ret - 1);
  182. ret++;
  183. mutex_unlock(&rng_mutex);
  184. return ret;
  185. }
  186. static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  187. hwrng_attr_current_show,
  188. hwrng_attr_current_store);
  189. static DEVICE_ATTR(rng_available, S_IRUGO,
  190. hwrng_attr_available_show,
  191. NULL);
  192. static void unregister_miscdev(void)
  193. {
  194. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
  195. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  196. misc_deregister(&rng_miscdev);
  197. }
  198. static int register_miscdev(void)
  199. {
  200. int err;
  201. err = misc_register(&rng_miscdev);
  202. if (err)
  203. goto out;
  204. err = device_create_file(rng_miscdev.this_device,
  205. &dev_attr_rng_current);
  206. if (err)
  207. goto err_misc_dereg;
  208. err = device_create_file(rng_miscdev.this_device,
  209. &dev_attr_rng_available);
  210. if (err)
  211. goto err_remove_current;
  212. out:
  213. return err;
  214. err_remove_current:
  215. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  216. err_misc_dereg:
  217. misc_deregister(&rng_miscdev);
  218. goto out;
  219. }
  220. int hwrng_register(struct hwrng *rng)
  221. {
  222. int must_register_misc;
  223. int err = -EINVAL;
  224. struct hwrng *old_rng, *tmp;
  225. if (rng->name == NULL ||
  226. rng->data_read == NULL)
  227. goto out;
  228. mutex_lock(&rng_mutex);
  229. /* Must not register two RNGs with the same name. */
  230. err = -EEXIST;
  231. list_for_each_entry(tmp, &rng_list, list) {
  232. if (strcmp(tmp->name, rng->name) == 0)
  233. goto out_unlock;
  234. }
  235. must_register_misc = (current_rng == NULL);
  236. old_rng = current_rng;
  237. if (!old_rng) {
  238. err = hwrng_init(rng);
  239. if (err)
  240. goto out_unlock;
  241. current_rng = rng;
  242. }
  243. err = 0;
  244. if (must_register_misc) {
  245. err = register_miscdev();
  246. if (err) {
  247. if (!old_rng) {
  248. hwrng_cleanup(rng);
  249. current_rng = NULL;
  250. }
  251. goto out_unlock;
  252. }
  253. }
  254. INIT_LIST_HEAD(&rng->list);
  255. list_add_tail(&rng->list, &rng_list);
  256. out_unlock:
  257. mutex_unlock(&rng_mutex);
  258. out:
  259. return err;
  260. }
  261. EXPORT_SYMBOL_GPL(hwrng_register);
  262. void hwrng_unregister(struct hwrng *rng)
  263. {
  264. int err;
  265. mutex_lock(&rng_mutex);
  266. list_del(&rng->list);
  267. if (current_rng == rng) {
  268. hwrng_cleanup(rng);
  269. if (list_empty(&rng_list)) {
  270. current_rng = NULL;
  271. } else {
  272. current_rng = list_entry(rng_list.prev, struct hwrng, list);
  273. err = hwrng_init(current_rng);
  274. if (err)
  275. current_rng = NULL;
  276. }
  277. }
  278. if (list_empty(&rng_list))
  279. unregister_miscdev();
  280. mutex_unlock(&rng_mutex);
  281. }
  282. EXPORT_SYMBOL_GPL(hwrng_unregister);
  283. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  284. MODULE_LICENSE("GPL");