core.c 7.2 KB

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