core.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. if (bytes_read < 0) {
  93. err = bytes_read;
  94. goto out;
  95. }
  96. err = -EFAULT;
  97. while (bytes_read && size) {
  98. if (put_user((u8)data, buf++))
  99. goto out;
  100. size--;
  101. ret++;
  102. bytes_read--;
  103. data >>= 8;
  104. }
  105. if (need_resched())
  106. schedule_timeout_interruptible(1);
  107. err = -ERESTARTSYS;
  108. if (signal_pending(current))
  109. goto out;
  110. }
  111. out:
  112. return ret ? : err;
  113. }
  114. static const struct file_operations rng_chrdev_ops = {
  115. .owner = THIS_MODULE,
  116. .open = rng_dev_open,
  117. .read = rng_dev_read,
  118. };
  119. static struct miscdevice rng_miscdev = {
  120. .minor = RNG_MISCDEV_MINOR,
  121. .name = RNG_MODULE_NAME,
  122. .fops = &rng_chrdev_ops,
  123. };
  124. static ssize_t hwrng_attr_current_store(struct device *dev,
  125. struct device_attribute *attr,
  126. const char *buf, size_t len)
  127. {
  128. int err;
  129. struct hwrng *rng;
  130. err = mutex_lock_interruptible(&rng_mutex);
  131. if (err)
  132. return -ERESTARTSYS;
  133. err = -ENODEV;
  134. list_for_each_entry(rng, &rng_list, list) {
  135. if (strcmp(rng->name, buf) == 0) {
  136. if (rng == current_rng) {
  137. err = 0;
  138. break;
  139. }
  140. err = hwrng_init(rng);
  141. if (err)
  142. break;
  143. hwrng_cleanup(current_rng);
  144. current_rng = rng;
  145. err = 0;
  146. break;
  147. }
  148. }
  149. mutex_unlock(&rng_mutex);
  150. return err ? : len;
  151. }
  152. static ssize_t hwrng_attr_current_show(struct device *dev,
  153. struct device_attribute *attr,
  154. char *buf)
  155. {
  156. int err;
  157. ssize_t ret;
  158. const char *name = "none";
  159. err = mutex_lock_interruptible(&rng_mutex);
  160. if (err)
  161. return -ERESTARTSYS;
  162. if (current_rng)
  163. name = current_rng->name;
  164. ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
  165. mutex_unlock(&rng_mutex);
  166. return ret;
  167. }
  168. static ssize_t hwrng_attr_available_show(struct device *dev,
  169. struct device_attribute *attr,
  170. char *buf)
  171. {
  172. int err;
  173. ssize_t ret = 0;
  174. struct hwrng *rng;
  175. err = mutex_lock_interruptible(&rng_mutex);
  176. if (err)
  177. return -ERESTARTSYS;
  178. buf[0] = '\0';
  179. list_for_each_entry(rng, &rng_list, list) {
  180. strncat(buf, rng->name, PAGE_SIZE - ret - 1);
  181. ret += strlen(rng->name);
  182. strncat(buf, " ", PAGE_SIZE - ret - 1);
  183. ret++;
  184. }
  185. strncat(buf, "\n", PAGE_SIZE - ret - 1);
  186. ret++;
  187. mutex_unlock(&rng_mutex);
  188. return ret;
  189. }
  190. static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
  191. hwrng_attr_current_show,
  192. hwrng_attr_current_store);
  193. static DEVICE_ATTR(rng_available, S_IRUGO,
  194. hwrng_attr_available_show,
  195. NULL);
  196. static void unregister_miscdev(void)
  197. {
  198. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
  199. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  200. misc_deregister(&rng_miscdev);
  201. }
  202. static int register_miscdev(void)
  203. {
  204. int err;
  205. err = misc_register(&rng_miscdev);
  206. if (err)
  207. goto out;
  208. err = device_create_file(rng_miscdev.this_device,
  209. &dev_attr_rng_current);
  210. if (err)
  211. goto err_misc_dereg;
  212. err = device_create_file(rng_miscdev.this_device,
  213. &dev_attr_rng_available);
  214. if (err)
  215. goto err_remove_current;
  216. out:
  217. return err;
  218. err_remove_current:
  219. device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
  220. err_misc_dereg:
  221. misc_deregister(&rng_miscdev);
  222. goto out;
  223. }
  224. int hwrng_register(struct hwrng *rng)
  225. {
  226. int must_register_misc;
  227. int err = -EINVAL;
  228. struct hwrng *old_rng, *tmp;
  229. if (rng->name == NULL ||
  230. rng->data_read == NULL)
  231. goto out;
  232. mutex_lock(&rng_mutex);
  233. /* Must not register two RNGs with the same name. */
  234. err = -EEXIST;
  235. list_for_each_entry(tmp, &rng_list, list) {
  236. if (strcmp(tmp->name, rng->name) == 0)
  237. goto out_unlock;
  238. }
  239. must_register_misc = (current_rng == NULL);
  240. old_rng = current_rng;
  241. if (!old_rng) {
  242. err = hwrng_init(rng);
  243. if (err)
  244. goto out_unlock;
  245. current_rng = rng;
  246. }
  247. err = 0;
  248. if (must_register_misc) {
  249. err = register_miscdev();
  250. if (err) {
  251. if (!old_rng) {
  252. hwrng_cleanup(rng);
  253. current_rng = NULL;
  254. }
  255. goto out_unlock;
  256. }
  257. }
  258. INIT_LIST_HEAD(&rng->list);
  259. list_add_tail(&rng->list, &rng_list);
  260. out_unlock:
  261. mutex_unlock(&rng_mutex);
  262. out:
  263. return err;
  264. }
  265. EXPORT_SYMBOL_GPL(hwrng_register);
  266. void hwrng_unregister(struct hwrng *rng)
  267. {
  268. int err;
  269. mutex_lock(&rng_mutex);
  270. list_del(&rng->list);
  271. if (current_rng == rng) {
  272. hwrng_cleanup(rng);
  273. if (list_empty(&rng_list)) {
  274. current_rng = NULL;
  275. } else {
  276. current_rng = list_entry(rng_list.prev, struct hwrng, list);
  277. err = hwrng_init(current_rng);
  278. if (err)
  279. current_rng = NULL;
  280. }
  281. }
  282. if (list_empty(&rng_list))
  283. unregister_miscdev();
  284. mutex_unlock(&rng_mutex);
  285. }
  286. EXPORT_SYMBOL_GPL(hwrng_unregister);
  287. MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
  288. MODULE_LICENSE("GPL");