core.c 7.2 KB

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