2 Added support for the AMD Geode LX RNG
3 (c) Copyright 2004-2005 Advanced Micro Devices, Inc.
7 Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 (c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
12 Hardware driver for the AMD 768 Random Number Generator (RNG)
13 (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
17 Hardware driver for Intel i810 Random Number Generator (RNG)
18 Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
22 Copyright 2006 Michael Buesch <mbuesch@freenet.de>
23 Copyright 2005 (c) MontaVista Software, Inc.
25 Please read Documentation/hw_random.txt for details on use.
27 ----------------------------------------------------------
28 This software may be used and distributed according to the terms
29 of the GNU General Public License, incorporated herein by reference.
34 #include <linux/device.h>
35 #include <linux/hw_random.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #include <linux/init.h>
40 #include <linux/miscdevice.h>
41 #include <linux/delay.h>
42 #include <asm/uaccess.h>
45 #define RNG_MODULE_NAME "hw_random"
46 #define PFX RNG_MODULE_NAME ": "
47 #define RNG_MISCDEV_MINOR 183 /* official */
50 static struct hwrng *current_rng;
51 static LIST_HEAD(rng_list);
52 static DEFINE_MUTEX(rng_mutex);
55 static inline int hwrng_init(struct hwrng *rng)
59 return rng->init(rng);
62 static inline void hwrng_cleanup(struct hwrng *rng)
64 if (rng && rng->cleanup)
68 static inline int hwrng_data_present(struct hwrng *rng)
70 if (!rng->data_present)
72 return rng->data_present(rng);
75 static inline int hwrng_data_read(struct hwrng *rng, u32 *data)
77 return rng->data_read(rng, data);
81 static int rng_dev_open(struct inode *inode, struct file *filp)
83 /* enforce read-only access to this chrdev */
84 if ((filp->f_mode & FMODE_READ) == 0)
86 if (filp->f_mode & FMODE_WRITE)
91 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
92 size_t size, loff_t *offp)
102 if (mutex_lock_interruptible(&rng_mutex))
105 mutex_unlock(&rng_mutex);
109 if (filp->f_flags & O_NONBLOCK) {
110 data_present = hwrng_data_present(current_rng);
112 /* Some RNG require some time between data_reads to gather
113 * new entropy. Poll it.
115 for (i = 0; i < 20; i++) {
116 data_present = hwrng_data_present(current_rng);
124 bytes_read = hwrng_data_read(current_rng, &data);
125 mutex_unlock(&rng_mutex);
128 if (!bytes_read && (filp->f_flags & O_NONBLOCK))
132 while (bytes_read && size) {
133 if (put_user((u8)data, buf++))
142 schedule_timeout_interruptible(1);
144 if (signal_pending(current))
152 static const struct file_operations rng_chrdev_ops = {
153 .owner = THIS_MODULE,
154 .open = rng_dev_open,
155 .read = rng_dev_read,
158 static struct miscdevice rng_miscdev = {
159 .minor = RNG_MISCDEV_MINOR,
160 .name = RNG_MODULE_NAME,
161 .fops = &rng_chrdev_ops,
165 static ssize_t hwrng_attr_current_store(struct device *dev,
166 struct device_attribute *attr,
167 const char *buf, size_t len)
172 err = mutex_lock_interruptible(&rng_mutex);
176 list_for_each_entry(rng, &rng_list, list) {
177 if (strcmp(rng->name, buf) == 0) {
178 if (rng == current_rng) {
182 err = hwrng_init(rng);
185 hwrng_cleanup(current_rng);
191 mutex_unlock(&rng_mutex);
196 static ssize_t hwrng_attr_current_show(struct device *dev,
197 struct device_attribute *attr,
202 const char *name = "none";
204 err = mutex_lock_interruptible(&rng_mutex);
208 name = current_rng->name;
209 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
210 mutex_unlock(&rng_mutex);
215 static ssize_t hwrng_attr_available_show(struct device *dev,
216 struct device_attribute *attr,
223 err = mutex_lock_interruptible(&rng_mutex);
227 list_for_each_entry(rng, &rng_list, list) {
228 strncat(buf, rng->name, PAGE_SIZE - ret - 1);
229 ret += strlen(rng->name);
230 strncat(buf, " ", PAGE_SIZE - ret - 1);
233 strncat(buf, "\n", PAGE_SIZE - ret - 1);
235 mutex_unlock(&rng_mutex);
240 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
241 hwrng_attr_current_show,
242 hwrng_attr_current_store);
243 static DEVICE_ATTR(rng_available, S_IRUGO,
244 hwrng_attr_available_show,
248 static void unregister_miscdev(void)
250 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
251 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
252 misc_deregister(&rng_miscdev);
255 static int register_miscdev(void)
259 err = misc_register(&rng_miscdev);
262 err = device_create_file(rng_miscdev.this_device,
263 &dev_attr_rng_current);
266 err = device_create_file(rng_miscdev.this_device,
267 &dev_attr_rng_available);
269 goto err_remove_current;
274 device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
276 misc_deregister(&rng_miscdev);
280 int hwrng_register(struct hwrng *rng)
282 int must_register_misc;
284 struct hwrng *old_rng, *tmp;
286 if (rng->name == NULL ||
287 rng->data_read == NULL)
290 mutex_lock(&rng_mutex);
292 /* Must not register two RNGs with the same name. */
294 list_for_each_entry(tmp, &rng_list, list) {
295 if (strcmp(tmp->name, rng->name) == 0)
299 must_register_misc = (current_rng == NULL);
300 old_rng = current_rng;
302 err = hwrng_init(rng);
308 if (must_register_misc) {
309 err = register_miscdev();
318 INIT_LIST_HEAD(&rng->list);
319 list_add_tail(&rng->list, &rng_list);
321 mutex_unlock(&rng_mutex);
325 EXPORT_SYMBOL_GPL(hwrng_register);
327 void hwrng_unregister(struct hwrng *rng)
331 mutex_lock(&rng_mutex);
333 list_del(&rng->list);
334 if (current_rng == rng) {
336 if (list_empty(&rng_list)) {
339 current_rng = list_entry(rng_list.prev, struct hwrng, list);
340 err = hwrng_init(current_rng);
345 if (list_empty(&rng_list))
346 unregister_miscdev();
348 mutex_unlock(&rng_mutex);
350 EXPORT_SYMBOL_GPL(hwrng_unregister);
353 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
354 MODULE_LICENSE("GPL");