core.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/init.h>
  28. #include <linux/miscdevice.h>
  29. #include <linux/delay.h>
  30. #include <asm/uaccess.h>
  31. #define RNG_MODULE_NAME "hw_random"
  32. #define PFX RNG_MODULE_NAME ": "
  33. #define RNG_MISCDEV_MINOR 183 /* official */
  34. static struct hwrng *current_rng;
  35. static LIST_HEAD(rng_list);
  36. static DEFINE_MUTEX(rng_mutex);
  37. static inline int hwrng_init(struct hwrng *rng)
  38. {
  39. if (!rng->init)
  40. return 0;
  41. return rng->init(rng);
  42. }
  43. static inline void hwrng_cleanup(struct hwrng *rng)
  44. {
  45. if (rng && rng->cleanup)
  46. rng->cleanup(rng);
  47. }
  48. static inline int hwrng_data_present(struct hwrng *rng)
  49. {
  50. if (!rng->data_present)
  51. return 1;
  52. return rng->data_present(rng);
  53. }
  54. static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
  55. {
  56. return rng->data_read(rng, data);
  57. }
  58. static int rng_dev_open(struct inode *inode, struct file *filp)
  59. {
  60. /* enforce read-only access to this chrdev */
  61. if ((filp->f_mode & FMODE_READ) == 0)
  62. return -EINVAL;
  63. if (filp->f_mode & FMODE_WRITE)
  64. return -EINVAL;
  65. return 0;
  66. }
  67. static ssize_t rng_dev_read(struct file *filp, char __user *buf,
  68. size_t size, loff_t *offp)
  69. {
  70. u32 data;
  71. ssize_t ret = 0;
  72. int i, err = 0;
  73. int data_present;
  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. if (filp->f_flags & O_NONBLOCK) {
  85. data_present = hwrng_data_present(current_rng);
  86. } else {
  87. /* Some RNG require some time between data_reads to gather
  88. * new entropy. Poll it.
  89. */
  90. for (i = 0; i < 20; i++) {
  91. data_present = hwrng_data_present(current_rng);
  92. if (data_present)
  93. break;
  94. udelay(10);
  95. }
  96. }
  97. bytes_read = 0;
  98. if (data_present)
  99. bytes_read = hwrng_data_read(current_rng, &data);
  100. mutex_unlock(&rng_mutex);
  101. err = -EAGAIN;
  102. if (!bytes_read && (filp->f_flags & O_NONBLOCK))
  103. goto out;
  104. err = -EFAULT;
  105. while (bytes_read && size) {
  106. if (put_user((u8)data, buf++))
  107. goto out;
  108. size--;
  109. ret++;
  110. bytes_read--;
  111. data >>= 8;
  112. }
  113. if (need_resched())
  114. schedule_timeout_interruptible(1);
  115. err = -ERESTARTSYS;
  116. if (signal_pending(current))
  117. goto out;
  118. }
  119. out:
  120. return ret ? : err;
  121. }
  122. static const struct file_operations rng_chrdev_ops = {
  123. .owner = THIS_MODULE,
  124. .open = rng_dev_open,
  125. .read = rng_dev_read,
  126. };
  127. static struct miscdevice rng_miscdev = {
  128. .minor = RNG_MISCDEV_MINOR,
  129. .name = RNG_MODULE_NAME,
  130. .fops = &rng_chrdev_ops,
  131. };
  132. static ssize_t hwrng_attr_current_store(struct class_device *class,
  133. const char *buf, size_t len)
  134. {
  135. int err;
  136. struct hwrng *rng;
  137. err = mutex_lock_interruptible(&rng_mutex);
  138. if (err)
  139. return -ERESTARTSYS;
  140. err = -ENODEV;
  141. list_for_each_entry(rng, &rng_list, list) {
  142. if (strcmp(rng->name, buf) == 0) {
  143. if (rng == current_rng) {
  144. err = 0;
  145. break;
  146. }
  147. err = hwrng_init(rng);
  148. if (err)
  149. break;
  150. hwrng_cleanup(current_rng);
  151. current_rng = rng;
  152. err = 0;
  153. break;
  154. }
  155. }
  156. mutex_unlock(&rng_mutex);
  157. return err ? : len;
  158. }
  159. static ssize_t hwrng_attr_current_show(struct class_device *class,
  160. char *buf)
  161. {
  162. int err;
  163. ssize_t ret;
  164. const char *name = "none";
  165. err = mutex_lock_interruptible(&rng_mutex);
  166. if (err)
  167. return -ERESTARTSYS;
  168. if (current_rng)
  169. name = current_rng->name;
  170. ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
  171. mutex_unlock(&rng_mutex);
  172. return ret;
  173. }
  174. static ssize_t hwrng_attr_available_show(struct class_device *class,
  175. char *buf)
  176. {
  177. int err;
  178. ssize_t ret = 0;
  179. struct hwrng *rng;
  180. err = mutex_lock_interruptible(&rng_mutex);
  181. if (err)
  182. return -ERESTARTSYS;
  183. buf[0] = '\0';
  184. list_for_each_entry(rng, &rng_list, list) {
  185. strncat(buf, rng->name, PAGE_SIZE - ret - 1);
  186. ret += strlen(rng->name);
  187. strncat(buf, " ", PAGE_SIZE - ret - 1);
  188. ret++;
  189. }
  190. strncat(buf, "\n", PAGE_SIZE - ret - 1);
  191. ret++;
  192. mutex_unlock(&rng_mutex);
  193. return ret;
  194. }
  195. static CLASS_DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  196. hwrng_attr_current_show,
  197. hwrng_attr_current_store);
  198. static CLASS_DEVICE_ATTR(rng_available, S_IRUGO,
  199. hwrng_attr_available_show,
  200. NULL);
  201. static void unregister_miscdev(void)
  202. {
  203. class_device_remove_file(rng_miscdev.class,
  204. &class_device_attr_rng_available);
  205. class_device_remove_file(rng_miscdev.class,
  206. &class_device_attr_rng_current);
  207. misc_deregister(&rng_miscdev);
  208. }
  209. static int register_miscdev(void)
  210. {
  211. int err;
  212. err = misc_register(&rng_miscdev);
  213. if (err)
  214. goto out;
  215. err = class_device_create_file(rng_miscdev.class,
  216. &class_device_attr_rng_current);
  217. if (err)
  218. goto err_misc_dereg;
  219. err = class_device_create_file(rng_miscdev.class,
  220. &class_device_attr_rng_available);
  221. if (err)
  222. goto err_remove_current;
  223. out:
  224. return err;
  225. err_remove_current:
  226. class_device_remove_file(rng_miscdev.class,
  227. &class_device_attr_rng_current);
  228. err_misc_dereg:
  229. misc_deregister(&rng_miscdev);
  230. goto out;
  231. }
  232. int hwrng_register(struct hwrng *rng)
  233. {
  234. int must_register_misc;
  235. int err = -EINVAL;
  236. struct hwrng *old_rng, *tmp;
  237. if (rng->name == NULL ||
  238. rng->data_read == NULL)
  239. goto out;
  240. mutex_lock(&rng_mutex);
  241. /* Must not register two RNGs with the same name. */
  242. err = -EEXIST;
  243. list_for_each_entry(tmp, &rng_list, list) {
  244. if (strcmp(tmp->name, rng->name) == 0)
  245. goto out_unlock;
  246. }
  247. must_register_misc = (current_rng == NULL);
  248. old_rng = current_rng;
  249. if (!old_rng) {
  250. err = hwrng_init(rng);
  251. if (err)
  252. goto out_unlock;
  253. current_rng = rng;
  254. }
  255. err = 0;
  256. if (must_register_misc) {
  257. err = register_miscdev();
  258. if (err) {
  259. if (!old_rng) {
  260. hwrng_cleanup(rng);
  261. current_rng = NULL;
  262. }
  263. goto out_unlock;
  264. }
  265. }
  266. INIT_LIST_HEAD(&rng->list);
  267. list_add_tail(&rng->list, &rng_list);
  268. out_unlock:
  269. mutex_unlock(&rng_mutex);
  270. out:
  271. return err;
  272. }
  273. EXPORT_SYMBOL_GPL(hwrng_register);
  274. void hwrng_unregister(struct hwrng *rng)
  275. {
  276. int err;
  277. mutex_lock(&rng_mutex);
  278. list_del(&rng->list);
  279. if (current_rng == rng) {
  280. hwrng_cleanup(rng);
  281. if (list_empty(&rng_list)) {
  282. current_rng = NULL;
  283. } else {
  284. current_rng = list_entry(rng_list.prev, struct hwrng, list);
  285. err = hwrng_init(current_rng);
  286. if (err)
  287. current_rng = NULL;
  288. }
  289. }
  290. if (list_empty(&rng_list))
  291. unregister_miscdev();
  292. mutex_unlock(&rng_mutex);
  293. }
  294. EXPORT_SYMBOL_GPL(hwrng_unregister);
  295. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  296. MODULE_LICENSE("GPL");